admin-modal.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. jQuery.fn.nfAdminModal = function( action, options ) {
  2. if ( 0 == jQuery( '#nf-admin-modal-backdrop' ).length ) {
  3. var modalHtml = '<div id="nf-admin-modal-backdrop" style="display: none;"></div><div id="nf-admin-modal-wrap" class="wp-core-ui" style="display: none;"><div id="nf-admin-modal" tabindex="-1"><div id="admin-modal-title"><span id="nf-modal-title"></span><button type="button" id="nf-admin-modal-close" class="modal-close"><span class="screen-reader-text modal-close">Close</span></button></div><div id="modal-contents-wrapper" style="padding:20px;"><div id="nf-admin-modal-content" class="admin-modal-inside"></div><div class="submitbox" style="display:block;"></div></div></div></div>';
  4. jQuery( 'body' ).append( modalHtml );
  5. }
  6. if ( 'object' === typeof action ) {
  7. options = action;
  8. }
  9. var defaults = { 'title' : '', 'buttons' : false, 'backgroundClose': false };
  10. if ( 'undefined' === typeof options ) {
  11. options = jQuery( this ).data( 'nfAdminModal' );
  12. if ( 'undefined' === typeof options ) {
  13. // Merge our default options with the options sent
  14. options = jQuery.extend( defaults, options );
  15. }
  16. } else {
  17. // Merge our default options with the options sent
  18. options = jQuery.extend( defaults, options );
  19. }
  20. // Set our data with the current options
  21. jQuery( this ).data( 'nfAdminModal', options );
  22. jQuery( this ).hide();
  23. jQuery( '#nf-admin-modal-content' ).html( this.html() );
  24. jQuery( '#nf-modal-title' ).html( options.title );
  25. if ( options.buttons ) {
  26. jQuery( options.buttons ).hide();
  27. var buttons = jQuery( options.buttons ).html();
  28. jQuery( '#modal-contents-wrapper' ).find( '.submitbox' ).html( buttons );
  29. jQuery( '#nf-admin-modal-content' ).addClass( 'admin-modal-inside' );
  30. jQuery( '#modal-contents-wrapper' ).find( '.submitbox' ).show();
  31. } else {
  32. jQuery( '#nf-admin-modal-content' ).removeClass( 'admin-modal-inside' );
  33. jQuery( '#modal-contents-wrapper' ).find( '.submitbox' ).hide();
  34. }
  35. jQuery( '#nf-admin-modal-backdrop' ).data( 'backgroundClose', options.backgroundClose );
  36. if ( 'close' == action ) {
  37. jQuery.fn.nfAdminModal.close();
  38. } else if ( 'open' == action ) {
  39. jQuery.fn.nfAdminModal.open();
  40. }
  41. };
  42. jQuery( document ).on( 'click', '#nf-admin-modal-backdrop', function( e ) {
  43. if ( jQuery( this ).data( 'backgroundClose' ) == true ) {
  44. jQuery.fn.nfAdminModal.close();
  45. }
  46. } );
  47. jQuery( document ).on( 'click', '.modal-close', function( e ) {
  48. e.preventDefault();
  49. jQuery.fn.nfAdminModal.close();
  50. } );
  51. jQuery.fn.nfAdminModal.close = function() {
  52. jQuery( '#nf-admin-modal-backdrop' ).hide();
  53. jQuery( '#nf-admin-modal-wrap' ).hide();
  54. jQuery( document ).triggerHandler( 'nfAdminModalClose' );
  55. }
  56. jQuery.fn.nfAdminModal.open = function() {
  57. jQuery( '#nf-admin-modal-backdrop' ).show();
  58. jQuery( '#nf-admin-modal-wrap' ).show();
  59. jQuery( document ).triggerHandler( 'nfAdminModalOpen' );
  60. }