Sin descripción

smtp-dashboard-widget.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* global wp_mail_smtp_dashboard_widget, ajaxurl, moment, Chart */
  2. /**
  3. * WP Mail SMTP Dashboard Widget function.
  4. *
  5. * @since 2.9.0
  6. */
  7. 'use strict';
  8. var WPMailSMTPDashboardWidget = window.WPMailSMTPDashboardWidget || ( function( document, window, $ ) {
  9. /**
  10. * Elements reference.
  11. *
  12. * @since 2.9.0
  13. *
  14. * @type {object}
  15. */
  16. var el = {
  17. $canvas : $( '#wp-mail-smtp-dash-widget-chart' ),
  18. $settingsBtn : $( '#wp-mail-smtp-dash-widget-settings-button' ),
  19. $dismissBtn : $( '.wp-mail-smtp-dash-widget-dismiss-chart-upgrade' ),
  20. $summaryReportEmailBlock : $( '.wp-mail-smtp-dash-widget-summary-report-email-block' ),
  21. $summaryReportEmailDismissBtn : $( '.wp-mail-smtp-dash-widget-summary-report-email-dismiss' ),
  22. $summaryReportEmailEnableInput : $( '#wp-mail-smtp-dash-widget-summary-report-email-enable' ),
  23. };
  24. /**
  25. * Chart.js functions and properties.
  26. *
  27. * @since 2.9.0
  28. *
  29. * @type {object}
  30. */
  31. var chart = {
  32. /**
  33. * Chart.js instance.
  34. *
  35. * @since 2.9.0
  36. */
  37. instance: null,
  38. /**
  39. * Chart.js settings.
  40. *
  41. * @since 2.9.0
  42. */
  43. settings: {
  44. type: 'line',
  45. data: {
  46. labels: [],
  47. datasets: [
  48. {
  49. label: '',
  50. data: [],
  51. backgroundColor: 'rgba(34, 113, 177, 0.15)',
  52. borderColor: 'rgba(34, 113, 177, 1)',
  53. borderWidth: 2,
  54. pointRadius: 4,
  55. pointBorderWidth: 1,
  56. pointBackgroundColor: 'rgba(255, 255, 255, 1)',
  57. }
  58. ],
  59. },
  60. options: {
  61. maintainAspectRatio: false,
  62. scales: {
  63. xAxes: [ {
  64. type: 'time',
  65. time: {
  66. unit: 'day',
  67. tooltipFormat: 'MMM D',
  68. },
  69. distribution: 'series',
  70. ticks: {
  71. beginAtZero: true,
  72. source: 'labels',
  73. padding: 10,
  74. minRotation: 25,
  75. maxRotation: 25,
  76. callback: function( value, index, values ) {
  77. // Distribute the ticks equally starting from a right side of xAxis.
  78. var gap = Math.floor( values.length / 7 );
  79. if ( gap < 1 ) {
  80. return value;
  81. }
  82. if ( ( values.length - index - 1 ) % gap === 0 ) {
  83. return value;
  84. }
  85. },
  86. },
  87. } ],
  88. yAxes: [ {
  89. ticks: {
  90. beginAtZero: true,
  91. maxTicksLimit: 6,
  92. padding: 20,
  93. callback: function( value ) {
  94. // Make sure the tick value has no decimals.
  95. if ( Math.floor( value ) === value ) {
  96. return value;
  97. }
  98. },
  99. },
  100. } ],
  101. },
  102. elements: {
  103. line: {
  104. tension: 0,
  105. },
  106. },
  107. animation: {
  108. duration: 0,
  109. },
  110. hover: {
  111. animationDuration: 0,
  112. },
  113. legend: {
  114. display: false,
  115. },
  116. tooltips: {
  117. displayColors: false,
  118. },
  119. responsiveAnimationDuration: 0,
  120. },
  121. },
  122. /**
  123. * Init Chart.js.
  124. *
  125. * @since 2.9.0
  126. */
  127. init: function() {
  128. var ctx;
  129. if ( ! el.$canvas.length ) {
  130. return;
  131. }
  132. ctx = el.$canvas[ 0 ].getContext( '2d' );
  133. chart.instance = new Chart( ctx, chart.settings );
  134. chart.updateWithDummyData();
  135. chart.instance.update();
  136. },
  137. /**
  138. * Update Chart.js settings with dummy data.
  139. *
  140. * @since 2.9.0
  141. */
  142. updateWithDummyData: function() {
  143. var end = moment().startOf( 'day' ),
  144. days = 7,
  145. data = [ 55, 45, 34, 45, 32, 55, 65 ],
  146. date,
  147. i;
  148. for ( i = 1; i <= days; i++ ) {
  149. date = end.clone().subtract( i, 'days' );
  150. chart.settings.data.labels.push( date );
  151. chart.settings.data.datasets[ 0 ].data.push( {
  152. t: date,
  153. y: data[ i - 1 ],
  154. } );
  155. }
  156. },
  157. };
  158. /**
  159. * Public functions and properties.
  160. *
  161. * @since 2.9.0
  162. *
  163. * @type {object}
  164. */
  165. var app = {
  166. /**
  167. * Publicly accessible Chart.js functions and properties.
  168. *
  169. * @since 2.9.0
  170. */
  171. chart: chart,
  172. /**
  173. * Start the engine.
  174. *
  175. * @since 2.9.0
  176. */
  177. init: function() {
  178. $( app.ready );
  179. },
  180. /**
  181. * Document ready.
  182. *
  183. * @since 2.9.0
  184. */
  185. ready: function() {
  186. el.$settingsBtn.on( 'click', function() {
  187. $( this ).siblings( '.wp-mail-smtp-dash-widget-settings-menu' ).toggle();
  188. } );
  189. el.$dismissBtn.on( 'click', function( event ) {
  190. event.preventDefault();
  191. app.saveWidgetMeta( 'hide_graph', 1 );
  192. $( this ).closest( '.wp-mail-smtp-dash-widget-chart-block-container' ).remove();
  193. $( '#wp-mail-smtp-dash-widget-upgrade-footer' ).show();
  194. } );
  195. // Hide summary report email block on dismiss icon click.
  196. el.$summaryReportEmailDismissBtn.on( 'click', function( event ) {
  197. event.preventDefault();
  198. app.saveWidgetMeta( 'hide_summary_report_email_block', 1 );
  199. el.$summaryReportEmailBlock.slideUp();
  200. } );
  201. // Enable summary report email on checkbox enable.
  202. el.$summaryReportEmailEnableInput.on( 'change', function( event ) {
  203. event.preventDefault();
  204. var $self = $( this ),
  205. $loader = $self.next( 'i' );
  206. $self.hide();
  207. $loader.show();
  208. var data = {
  209. _wpnonce: wp_mail_smtp_dashboard_widget.nonce,
  210. action : 'wp_mail_smtp_' + wp_mail_smtp_dashboard_widget.slug + '_enable_summary_report_email'
  211. };
  212. $.post( ajaxurl, data )
  213. .done( function() {
  214. el.$summaryReportEmailBlock.find( '.wp-mail-smtp-dash-widget-summary-report-email-block-setting' )
  215. .addClass( 'hidden' );
  216. el.$summaryReportEmailBlock.find( '.wp-mail-smtp-dash-widget-summary-report-email-block-applied' )
  217. .removeClass( 'hidden' );
  218. } )
  219. .fail( function() {
  220. $self.show();
  221. $loader.hide();
  222. } );
  223. } );
  224. chart.init();
  225. app.removeOverlay( el.$canvas );
  226. },
  227. /**
  228. * Save dashboard widget meta in backend.
  229. *
  230. * @since 2.9.0
  231. *
  232. * @param {string} meta Meta name to save.
  233. * @param {number} value Value to save.
  234. */
  235. saveWidgetMeta: function( meta, value ) {
  236. var data = {
  237. _wpnonce: wp_mail_smtp_dashboard_widget.nonce,
  238. action : 'wp_mail_smtp_' + wp_mail_smtp_dashboard_widget.slug + '_save_widget_meta',
  239. meta : meta,
  240. value : value,
  241. };
  242. $.post( ajaxurl, data );
  243. },
  244. /**
  245. * Remove an overlay from a widget block containing $el.
  246. *
  247. * @since 2.9.0
  248. *
  249. * @param {object} $el jQuery element inside a widget block.
  250. */
  251. removeOverlay: function( $el ) {
  252. $el.siblings( '.wp-mail-smtp-dash-widget-overlay' ).remove();
  253. },
  254. };
  255. // Provide access to public functions/properties.
  256. return app;
  257. }( document, window, jQuery ) );
  258. // Initialize.
  259. WPMailSMTPDashboardWidget.init();