Nenhuma Descrição

smtp-tools-debug-events.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* global wp_mail_smtp_tools_debug_events, ajaxurl, flatpickr */
  2. /**
  3. * WPMailSMTP Debug Events functionality.
  4. *
  5. * @since 3.0.0
  6. */
  7. 'use strict';
  8. var WPMailSmtpDebugEvents = window.WPMailSmtpDebugEvents || ( function( document, window, $ ) {
  9. /**
  10. * Elements.
  11. *
  12. * @since 3.0.0
  13. *
  14. * @type {object}
  15. */
  16. var el = {
  17. $debugEventsPage: $( '.wp-mail-smtp-tab-tools-debug-events' ),
  18. $dateFlatpickr: $( '.wp-mail-smtp-filter-date-selector' ),
  19. };
  20. /**
  21. * Public functions and properties.
  22. *
  23. * @since 3.0.0
  24. *
  25. * @type {object}
  26. */
  27. var app = {
  28. /**
  29. * Start the engine.
  30. *
  31. * @since 3.0.0
  32. */
  33. init: function() {
  34. $( app.ready );
  35. },
  36. /**
  37. * Document ready.
  38. *
  39. * @since 3.0.0
  40. */
  41. ready: function() {
  42. app.initDateRange();
  43. app.events();
  44. },
  45. /**
  46. * Register JS events.
  47. *
  48. * @since 3.0.0
  49. */
  50. events: function() {
  51. el.$debugEventsPage.on( 'click', '#wp-mail-smtp-reset-filter .reset', app.resetFilter );
  52. el.$debugEventsPage.on( 'click', '#wp-mail-smtp-delete-all-debug-events-button', app.deleteAllDebugEvents );
  53. el.$debugEventsPage.on( 'click', '.js-wp-mail-smtp-debug-event-preview', app.eventClicked );
  54. },
  55. /**
  56. * Init Flatpickr at Date Range field.
  57. *
  58. * @since 3.0.0
  59. */
  60. initDateRange: function() {
  61. var langCode = wp_mail_smtp_tools_debug_events.lang_code,
  62. flatpickrLocale = {
  63. rangeSeparator: ' - ',
  64. };
  65. if (
  66. flatpickr !== 'undefined' &&
  67. Object.prototype.hasOwnProperty.call( flatpickr, 'l10ns' ) &&
  68. Object.prototype.hasOwnProperty.call( flatpickr.l10ns, langCode )
  69. ) {
  70. flatpickrLocale = flatpickr.l10ns[ langCode ];
  71. flatpickrLocale.rangeSeparator = ' - ';
  72. }
  73. el.$dateFlatpickr.flatpickr( {
  74. altInput : true,
  75. altFormat : 'M j, Y',
  76. dateFormat: 'Y-m-d',
  77. locale : flatpickrLocale,
  78. mode : 'range'
  79. } );
  80. },
  81. /**
  82. * Reset filter handler.
  83. *
  84. * @since 3.0.0
  85. */
  86. resetFilter: function() {
  87. var $form = $( this ).parents( 'form' );
  88. $form.find( $( this ).data( 'scope' ) ).find( 'input,select' ).each( function() {
  89. var $this = $( this );
  90. if ( app.isIgnoredForResetInput( $this ) ) {
  91. return;
  92. }
  93. app.resetInput( $this );
  94. } );
  95. // Submit the form.
  96. $form.submit();
  97. },
  98. /**
  99. * Reset input.
  100. *
  101. * @since 3.0.0
  102. *
  103. * @param {object} $input Input element.
  104. */
  105. resetInput: function( $input ) {
  106. switch ( $input.prop( 'tagName' ).toLowerCase() ) {
  107. case 'input':
  108. $input.val( '' );
  109. break;
  110. case 'select':
  111. $input.val( $input.find( 'option' ).first().val() );
  112. break;
  113. }
  114. },
  115. /**
  116. * Input is ignored for reset.
  117. *
  118. * @since 3.0.0
  119. *
  120. * @param {object} $input Input element.
  121. *
  122. * @returns {boolean} Is ignored.
  123. */
  124. isIgnoredForResetInput: function( $input ) {
  125. return [ 'submit', 'hidden' ].indexOf( ( $input.attr( 'type' ) || '' ).toLowerCase() ) !== -1 &&
  126. ! $input.hasClass( 'flatpickr-input' );
  127. },
  128. /**
  129. * Process the click on the delete all debug events button.
  130. *
  131. * @since 3.0.0
  132. *
  133. * @param {object} event jQuery event.
  134. */
  135. deleteAllDebugEvents: function( event ) {
  136. event.preventDefault();
  137. var $btn = $( event.target );
  138. $.confirm( {
  139. backgroundDismiss: false,
  140. escapeKey: true,
  141. animationBounce: 1,
  142. closeIcon: true,
  143. type: 'orange',
  144. icon: app.getModalIcon( 'exclamation-circle-solid-orange' ),
  145. title: wp_mail_smtp_tools_debug_events.texts.notice_title,
  146. content: wp_mail_smtp_tools_debug_events.texts.delete_all_notice,
  147. buttons: {
  148. confirm: {
  149. text: wp_mail_smtp_tools_debug_events.texts.yes,
  150. btnClass: 'btn-confirm',
  151. keys: [ 'enter' ],
  152. action: function() {
  153. app.executeAllDebugEventsDeletion( $btn );
  154. }
  155. },
  156. cancel: {
  157. text: wp_mail_smtp_tools_debug_events.texts.cancel,
  158. btnClass: 'btn-cancel',
  159. }
  160. }
  161. } );
  162. },
  163. /**
  164. * Process the click on the event item.
  165. *
  166. * @since 3.0.0
  167. *
  168. * @param {object} event jQuery event.
  169. */
  170. eventClicked: function( event ) {
  171. event.preventDefault();
  172. var data = {
  173. action: 'wp_mail_smtp_debug_event_preview',
  174. id: $( this ).data( 'event-id' ),
  175. nonce: $( '#wp-mail-smtp-debug-events-nonce', el.$debugEventsPage ).val()
  176. };
  177. var popup = $.alert( {
  178. backgroundDismiss: true,
  179. escapeKey: true,
  180. animationBounce: 1,
  181. type: 'blue',
  182. icon: app.getModalIcon( 'info-circle-blue' ),
  183. title: false,
  184. content: wp_mail_smtp_tools_debug_events.loader,
  185. boxWidth: '550px',
  186. buttons: {
  187. confirm: {
  188. text: wp_mail_smtp_tools_debug_events.texts.close,
  189. btnClass: 'btn-confirm',
  190. keys: [ 'enter' ]
  191. }
  192. }
  193. } );
  194. $.post( ajaxurl, data, function( response ) {
  195. if ( response.success ) {
  196. popup.setTitle( response.data.title );
  197. popup.setContent( response.data.content );
  198. } else {
  199. popup.setContent( response.data );
  200. }
  201. } ).fail( function() {
  202. popup.setContent( wp_mail_smtp_tools_debug_events.texts.error_occurred );
  203. } );
  204. },
  205. /**
  206. * AJAX call for deleting all debug events.
  207. *
  208. * @since 3.0.0
  209. *
  210. * @param {object} $btn jQuery object of the clicked button.
  211. */
  212. executeAllDebugEventsDeletion: function( $btn ) {
  213. $btn.prop( 'disabled', true );
  214. var data = {
  215. action: 'wp_mail_smtp_delete_all_debug_events',
  216. nonce: $( '#wp-mail-smtp-debug-events-nonce', el.$debugEventsPage ).val()
  217. };
  218. $.post( ajaxurl, data, function( response ) {
  219. var message = response.data,
  220. icon,
  221. type,
  222. callback;
  223. if ( response.success ) {
  224. icon = 'check-circle-solid-green';
  225. type = 'green';
  226. callback = function() {
  227. location.reload();
  228. return false;
  229. };
  230. } else {
  231. icon = 'exclamation-circle-regular-red';
  232. type = 'red';
  233. }
  234. app.displayModal( message, icon, type, callback );
  235. $btn.prop( 'disabled', false );
  236. } ).fail( function() {
  237. app.displayModal( wp_mail_smtp_tools_debug_events.texts.error_occurred, 'exclamation-circle-regular-red', 'red' );
  238. $btn.prop( 'disabled', false );
  239. } );
  240. },
  241. /**
  242. * Display the modal with provided text and icon.
  243. *
  244. * @since 3.0.0
  245. *
  246. * @param {string} message The message to be displayed in the modal.
  247. * @param {string} icon The icon name from /assets/images/font-awesome/ to be used in modal.
  248. * @param {string} type The type of the message (red, green, orange, blue, purple, dark).
  249. * @param {Function} actionCallback The action callback function.
  250. */
  251. displayModal: function( message, icon, type, actionCallback ) {
  252. type = type || 'default';
  253. actionCallback = actionCallback || function() {};
  254. $.alert( {
  255. backgroundDismiss: true,
  256. escapeKey: true,
  257. animationBounce: 1,
  258. type: type,
  259. closeIcon: true,
  260. title: false,
  261. icon: icon ? app.getModalIcon( icon ) : '',
  262. content: message,
  263. buttons: {
  264. confirm: {
  265. text: wp_mail_smtp_tools_debug_events.texts.ok,
  266. btnClass: 'wp-mail-smtp-btn wp-mail-smtp-btn-md',
  267. keys: [ 'enter' ],
  268. action: actionCallback
  269. }
  270. }
  271. } );
  272. },
  273. /**
  274. * Returns prepared modal icon.
  275. *
  276. * @since 3.0.0
  277. *
  278. * @param {string} icon The icon name from /assets/images/font-awesome/ to be used in modal.
  279. *
  280. * @returns {string} Modal icon HTML.
  281. */
  282. getModalIcon: function( icon ) {
  283. return '"></i><img src="' + wp_mail_smtp_tools_debug_events.plugin_url + '/assets/images/font-awesome/' + icon + '.svg" style="width: 40px; height: 40px;" alt=""><i class="';
  284. },
  285. };
  286. // Provide access to public functions/properties.
  287. return app;
  288. }( document, window, jQuery ) );
  289. // Initialize.
  290. WPMailSmtpDebugEvents.init();