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