Nav apraksta

smtp-notifications.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* global wp_mail_smtp, ajaxurl */
  2. /**
  3. * WP Mail SMTP Admin Notifications.
  4. *
  5. * @since 2.3.0
  6. */
  7. 'use strict';
  8. var WPMailSMTPAdminNotifications = window.WPMailSMTPAdminNotifications || ( function( document, window, $ ) {
  9. /**
  10. * Elements holder.
  11. *
  12. * @since 2.3.0
  13. *
  14. * @type {object}
  15. */
  16. var el = {
  17. $notifications: $( '#wp-mail-smtp-notifications' ),
  18. $nextButton: $( '#wp-mail-smtp-notifications .navigation .next' ),
  19. $prevButton: $( '#wp-mail-smtp-notifications .navigation .prev' ),
  20. $adminBarCounter: $( '#wp-admin-bar-wp-mail-smtp-menu .wp-mail-smtp-admin-bar-menu-notification-counter' ),
  21. };
  22. /**
  23. * Public functions and properties.
  24. *
  25. * @since 2.3.0
  26. *
  27. * @type {object}
  28. */
  29. var app = {
  30. /**
  31. * Start the engine.
  32. *
  33. * @since 2.3.0
  34. */
  35. init: function() {
  36. $( app.ready );
  37. },
  38. /**
  39. * Document ready.
  40. *
  41. * @since 2.3.0
  42. */
  43. ready: function() {
  44. app.updateNavigation();
  45. app.events();
  46. },
  47. /**
  48. * Register JS events.
  49. *
  50. * @since 2.3.0
  51. */
  52. events: function() {
  53. el.$notifications
  54. .on( 'click', '.dismiss', app.dismiss )
  55. .on( 'click', '.next', app.navNext )
  56. .on( 'click', '.prev', app.navPrev );
  57. },
  58. /**
  59. * Click on the Dismiss notification button.
  60. *
  61. * @since 2.3.0
  62. *
  63. * @param {object} event Event object.
  64. */
  65. dismiss: function( event ) {
  66. if ( el.$currentMessage.length === 0 ) {
  67. return;
  68. }
  69. // AJAX call - update option.
  70. var data = {
  71. action: 'wp_mail_smtp_notification_dismiss',
  72. nonce: wp_mail_smtp.nonce,
  73. id: el.$currentMessage.data( 'message-id' ),
  74. };
  75. $.post( ajaxurl, data, function( response ) {
  76. if ( ! response.success ) {
  77. return;
  78. }
  79. // Update counter.
  80. var count = parseInt( el.$adminBarCounter.text(), 10 );
  81. if ( count > 1 ) {
  82. --count;
  83. el.$adminBarCounter.html( '<span>' + count + '</span>' );
  84. } else {
  85. el.$adminBarCounter.remove();
  86. }
  87. // Remove notification.
  88. var $nextMessage = el.$nextMessage.length < 1 ? el.$prevMessage : el.$nextMessage;
  89. if ( $nextMessage.length === 0 ) {
  90. el.$notifications.remove();
  91. } else {
  92. el.$currentMessage.remove();
  93. $nextMessage.addClass( 'current' );
  94. app.updateNavigation();
  95. }
  96. } );
  97. },
  98. /**
  99. * Click on the Next notification button.
  100. *
  101. * @since 2.3.0
  102. *
  103. * @param {object} event Event object.
  104. */
  105. navNext: function( event ) {
  106. if ( el.$nextButton.hasClass( 'disabled' ) ) {
  107. return;
  108. }
  109. el.$currentMessage.removeClass( 'current' );
  110. el.$nextMessage.addClass( 'current' );
  111. app.updateNavigation();
  112. },
  113. /**
  114. * Click on the Previous notification button.
  115. *
  116. * @since 2.3.0
  117. *
  118. * @param {object} event Event object.
  119. */
  120. navPrev: function( event ) {
  121. if ( el.$prevButton.hasClass( 'disabled' ) ) {
  122. return;
  123. }
  124. el.$currentMessage.removeClass( 'current' );
  125. el.$prevMessage.addClass( 'current' );
  126. app.updateNavigation();
  127. },
  128. /**
  129. * Update navigation buttons.
  130. *
  131. * @since 2.3.0
  132. */
  133. updateNavigation: function() {
  134. el.$currentMessage = el.$notifications.find( '.wp-mail-smtp-notifications-message.current' );
  135. el.$nextMessage = el.$currentMessage.next( '.wp-mail-smtp-notifications-message' );
  136. el.$prevMessage = el.$currentMessage.prev( '.wp-mail-smtp-notifications-message' );
  137. if ( el.$nextMessage.length === 0 ) {
  138. el.$nextButton.addClass( 'disabled' );
  139. } else {
  140. el.$nextButton.removeClass( 'disabled' );
  141. }
  142. if ( el.$prevMessage.length === 0 ) {
  143. el.$prevButton.addClass( 'disabled' );
  144. } else {
  145. el.$prevButton.removeClass( 'disabled' );
  146. }
  147. },
  148. };
  149. return app;
  150. }( document, window, jQuery ) );
  151. // Initialize.
  152. WPMailSMTPAdminNotifications.init();