Aucune description

admin-notifications.js 3.8KB

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