admin-dashboard.js
1 year ago
admin-post.js
5 years ago
admin-quick-edit.js
1 year ago
admin-settings.js
1 year ago
admin-widgets.js
5 years ago
block-editor.min.js
1 year ago
counter.js
1 year ago
counter.min.js
1 year ago
dummy.js
1 year ago
frontend.js
1 year ago
frontend.min.js
1 year ago
admin-dashboard.js
388 lines
| 1 | ( function ( $ ) { |
| 2 | |
| 3 | /** |
| 4 | * Load initial data. |
| 5 | */ |
| 6 | window.addEventListener( 'load', function () { |
| 7 | pvcUpdatePostViewsWidget(); |
| 8 | pvcUpdatePostMostViewedWidget(); |
| 9 | } ); |
| 10 | |
| 11 | /** |
| 12 | * Ready event. |
| 13 | */ |
| 14 | $( function () { |
| 15 | // toggle collapse items |
| 16 | $( '.pvc-accordion-header' ).on( 'click', function ( e ) { |
| 17 | $( this ).closest( '.pvc-accordion-item' ).toggleClass( 'pvc-collapsed' ); |
| 18 | |
| 19 | var items = $( '#pvc-dashboard-accordion' ).find( '.pvc-accordion-item' ); |
| 20 | var menuItems = {}; |
| 21 | |
| 22 | if ( items.length > 0 ) { |
| 23 | $( items ).each( function ( index, item ) { |
| 24 | var itemName = $( item ).attr( 'id' ); |
| 25 | |
| 26 | itemName = itemName.replace( 'pvc-', '' ); |
| 27 | |
| 28 | menuItems[itemName] = $( item ).hasClass( 'pvc-collapsed' ); |
| 29 | } ); |
| 30 | } |
| 31 | |
| 32 | // update user options |
| 33 | pvcUpdateUserOptions( {menu_items: menuItems} ); |
| 34 | } ); |
| 35 | } ); |
| 36 | |
| 37 | // jQuery on an empty object, we are going to use this as our Queue |
| 38 | var pvcAjaxQueue = $( {} ); |
| 39 | |
| 40 | /** |
| 41 | * Put AJAX requests in a queue, run one request at a time and prevent overwriting user options. |
| 42 | * |
| 43 | * @param {type} ajaxOpts |
| 44 | */ |
| 45 | $.pvcAjaxQueue = function ( ajaxOpts ) { |
| 46 | var jqXHR, |
| 47 | dfd = $.Deferred(), |
| 48 | promise = dfd.promise(); |
| 49 | |
| 50 | // run the actual query |
| 51 | function doRequest( next ) { |
| 52 | jqXHR = $.ajax( ajaxOpts ); |
| 53 | jqXHR.done( dfd.resolve ) |
| 54 | .fail( dfd.reject ) |
| 55 | .then( next, next ); |
| 56 | } |
| 57 | |
| 58 | // queue our ajax request |
| 59 | pvcAjaxQueue.queue( doRequest ); |
| 60 | |
| 61 | // add the abort method |
| 62 | promise.abort = function ( statusText ) { |
| 63 | |
| 64 | // proxy abort to the jqXHR if it is active |
| 65 | if ( jqXHR ) { |
| 66 | return jqXHR.abort( statusText ); |
| 67 | } |
| 68 | |
| 69 | // if there wasn't already a jqXHR we need to remove from queue |
| 70 | var queue = pvcAjaxQueue.queue(), |
| 71 | index = $.inArray( doRequest, queue ); |
| 72 | |
| 73 | if ( index > - 1 ) { |
| 74 | queue.splice( index, 1 ); |
| 75 | } |
| 76 | |
| 77 | // and then reject the deferred |
| 78 | dfd.rejectWith( ajaxOpts.context || ajaxOpts, [promise, statusText, ""] ); |
| 79 | return promise; |
| 80 | }; |
| 81 | |
| 82 | return promise; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Update user options. |
| 87 | */ |
| 88 | pvcUpdateUserOptions = function ( options ) { |
| 89 | $.pvcAjaxQueue( { |
| 90 | url: pvcArgs.ajaxURL, |
| 91 | type: 'POST', |
| 92 | dataType: 'json', |
| 93 | data: { |
| 94 | action: 'pvc_dashboard_user_options', |
| 95 | nonce: pvcArgs.nonceUser, |
| 96 | options: options |
| 97 | }, |
| 98 | success: function () {} |
| 99 | } ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Update configuration. |
| 104 | */ |
| 105 | pvcUpdateConfig = function ( config, args ) { |
| 106 | // update datasets |
| 107 | config.data = args.data; |
| 108 | |
| 109 | // update tooltips with new dates |
| 110 | config.options.plugins.tooltip = { |
| 111 | callbacks: { |
| 112 | title: function ( tooltip ) { |
| 113 | return args.data.dates[tooltip[0].dataIndex]; |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | // update colors |
| 119 | $.each( config.data.datasets, function ( i, dataset ) { |
| 120 | dataset.fill = args.design.fill; |
| 121 | dataset.tension = 0.4; |
| 122 | dataset.borderColor = args.design.borderColor; |
| 123 | dataset.backgroundColor = args.design.backgroundColor; |
| 124 | dataset.borderWidth = args.design.borderWidth; |
| 125 | dataset.borderDash = args.design.borderDash; |
| 126 | dataset.pointBorderColor = args.design.pointBorderColor; |
| 127 | dataset.pointBackgroundColor = args.design.pointBackgroundColor; |
| 128 | dataset.pointBorderWidth = args.design.pointBorderWidth; |
| 129 | } ); |
| 130 | |
| 131 | return config; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get post most viewed data. |
| 136 | */ |
| 137 | function pvcGetPostMostViewedData( init, period, container ) { |
| 138 | $( container ).addClass( 'loading' ).find( '.spinner' ).addClass( 'is-active' ); |
| 139 | |
| 140 | $.ajax( { |
| 141 | url: pvcArgs.ajaxURL, |
| 142 | type: 'POST', |
| 143 | dataType: 'json', |
| 144 | data: { |
| 145 | action: 'pvc_dashboard_post_most_viewed', |
| 146 | nonce: pvcArgs.nonce, |
| 147 | period: period |
| 148 | }, |
| 149 | success: function ( response ) { |
| 150 | // remove loader |
| 151 | $( container ).removeClass( 'loading' ); |
| 152 | $( container ).find( '.spinner' ).removeClass( 'is-active' ); |
| 153 | |
| 154 | // next call? |
| 155 | if ( ! init ) |
| 156 | pvcBindDateEvents( response.dates, container ); |
| 157 | |
| 158 | $( container ).find( '#pvc-post-most-viewed-content' ).html( response.html ); |
| 159 | |
| 160 | // trigger js event |
| 161 | pvcTriggerEvent( 'pvc-dashboard-widget-loaded', response ); |
| 162 | } |
| 163 | } ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get post views data. |
| 168 | */ |
| 169 | function pvcGetPostViewsData( init, period, container ) { |
| 170 | $( container ).addClass( 'loading' ).find( '.spinner' ).addClass( 'is-active' ); |
| 171 | |
| 172 | $.ajax( { |
| 173 | url: pvcArgs.ajaxURL, |
| 174 | type: 'POST', |
| 175 | dataType: 'json', |
| 176 | data: { |
| 177 | action: 'pvc_dashboard_post_views_chart', |
| 178 | nonce: pvcArgs.nonce, |
| 179 | period: period |
| 180 | }, |
| 181 | success: function ( response ) { |
| 182 | // remove loader |
| 183 | $( container ).removeClass( 'loading' ); |
| 184 | $( container ).find( '.spinner' ).removeClass( 'is-active' ); |
| 185 | |
| 186 | // first call? |
| 187 | if ( init ) { |
| 188 | var config = { |
| 189 | type: 'line', |
| 190 | options: { |
| 191 | maintainAspectRatio: false, |
| 192 | responsive: true, |
| 193 | plugins: { |
| 194 | legend: { |
| 195 | display: true, |
| 196 | position: 'bottom', |
| 197 | align: 'center', |
| 198 | fullSize: true, |
| 199 | onHover: function ( e ) { |
| 200 | e.native.target.style.cursor = 'pointer'; |
| 201 | }, |
| 202 | onLeave: function ( e ) { |
| 203 | e.native.target.style.cursor = 'default'; |
| 204 | }, |
| 205 | onClick: function ( e, element, legend ) { |
| 206 | var index = element.datasetIndex; |
| 207 | var ci = legend.chart; |
| 208 | var meta = ci.getDatasetMeta( index ); |
| 209 | |
| 210 | // set new hidden value |
| 211 | if ( ci.isDatasetVisible( index ) ) |
| 212 | meta.hidden = true; |
| 213 | else |
| 214 | meta.hidden = false; |
| 215 | |
| 216 | // rerender the chart |
| 217 | ci.update(); |
| 218 | |
| 219 | // update user options |
| 220 | pvcUpdateUserOptions( { |
| 221 | post_type: ci.data.datasets[index].post_type, |
| 222 | hidden: meta.hidden |
| 223 | } ); |
| 224 | }, |
| 225 | labels: { |
| 226 | boxWidth: 8, |
| 227 | boxHeight: 8, |
| 228 | font: { |
| 229 | size: 13, |
| 230 | weight: 'normal', |
| 231 | family: "'-apple-system', 'BlinkMacSystemFont', 'Segoe UI', 'Roboto', 'Oxygen-Sans', 'Ubuntu', 'Cantarell', 'Helvetica Neue', 'sans-serif'" |
| 232 | }, |
| 233 | padding: 10, |
| 234 | usePointStyle: false, |
| 235 | textAlign: 'center' |
| 236 | } |
| 237 | } |
| 238 | }, |
| 239 | scales: { |
| 240 | x: { |
| 241 | display: true, |
| 242 | title: { |
| 243 | display: false |
| 244 | } |
| 245 | }, |
| 246 | y: { |
| 247 | display: true, |
| 248 | grace: 0, |
| 249 | beginAtZero: true, |
| 250 | title: { |
| 251 | display: false |
| 252 | }, |
| 253 | ticks: { |
| 254 | precision: 0, |
| 255 | maxTicksLimit: 12 |
| 256 | } |
| 257 | } |
| 258 | }, |
| 259 | hover: { |
| 260 | mode: 'label' |
| 261 | } |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | config = pvcUpdateConfig( config, response ); |
| 266 | |
| 267 | window.postViewsChart = new Chart( document.getElementById( 'pvc-post-views-chart' ).getContext( '2d' ), config ); |
| 268 | } else { |
| 269 | pvcBindDateEvents( response.dates, container ); |
| 270 | |
| 271 | window.postViewsChart.config = pvcUpdateConfig( window.postViewsChart.config, response ); |
| 272 | window.postViewsChart.update(); |
| 273 | } |
| 274 | |
| 275 | // trigger js event |
| 276 | pvcTriggerEvent( 'pvc-dashboard-widget-loaded', response ); |
| 277 | } |
| 278 | } ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Update post views widget. |
| 283 | */ |
| 284 | function pvcUpdatePostViewsWidget( period = '' ) { |
| 285 | var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' ); |
| 286 | |
| 287 | if ( $( container ).length > 0 ) { |
| 288 | pvcBindDateEvents( false, container ); |
| 289 | |
| 290 | pvcGetPostViewsData( true, period, container ); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Update post most viewed widget. |
| 296 | */ |
| 297 | function pvcUpdatePostMostViewedWidget( period = '' ) { |
| 298 | var container = $( '#pvc-post-most-viewed' ).find( '.pvc-dashboard-container' ); |
| 299 | |
| 300 | if ( $( container ).length > 0 ) { |
| 301 | pvcBindDateEvents( false, container ); |
| 302 | |
| 303 | pvcGetPostMostViewedData( true, period, container ); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Bind date events. |
| 309 | */ |
| 310 | function pvcBindDateEvents( newDates, container ) { |
| 311 | var dates = $( container ).find( '.pvc-date-nav' ); |
| 312 | |
| 313 | // replace dates? |
| 314 | if ( newDates !== false ) |
| 315 | dates[0].innerHTML = newDates; |
| 316 | |
| 317 | var prev = dates[0].getElementsByClassName( 'prev' )[0]; |
| 318 | var next = dates[0].getElementsByClassName( 'next' )[0]; |
| 319 | var id = $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ); |
| 320 | |
| 321 | if ( id === 'pvc-post-most-viewed' ) |
| 322 | prev.addEventListener( 'click', function ( e ) { |
| 323 | e.preventDefault(); |
| 324 | |
| 325 | pvcLoadPostMostViewedData( e.target.dataset.date ); |
| 326 | } ); |
| 327 | else if ( id === 'pvc-post-views' ) |
| 328 | prev.addEventListener( 'click', function ( e ) { |
| 329 | e.preventDefault(); |
| 330 | |
| 331 | pvcLoadPostViewsData( e.target.dataset.date ); |
| 332 | } ); |
| 333 | |
| 334 | // skip span |
| 335 | if ( next.tagName === 'A' ) { |
| 336 | if ( id === 'pvc-post-most-viewed' ) |
| 337 | next.addEventListener( 'click', function ( e ) { |
| 338 | e.preventDefault(); |
| 339 | |
| 340 | pvcLoadPostMostViewedData( e.target.dataset.date ); |
| 341 | } ); |
| 342 | else if ( id === 'pvc-post-views' ) |
| 343 | next.addEventListener( 'click', function ( e ) { |
| 344 | e.preventDefault(); |
| 345 | |
| 346 | pvcLoadPostViewsData( e.target.dataset.date ); |
| 347 | } ); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Load post views data. |
| 353 | */ |
| 354 | function pvcLoadPostViewsData( period = '' ) { |
| 355 | var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' ); |
| 356 | |
| 357 | pvcGetPostViewsData( false, period, container ); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Load post most viewed data. |
| 362 | */ |
| 363 | function pvcLoadPostMostViewedData( period = '' ) { |
| 364 | var container = $( '#pvc-post-most-viewed' ).find( '.pvc-dashboard-container' ); |
| 365 | |
| 366 | pvcGetPostMostViewedData( false, period, container ); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Trigger load widget JS event. |
| 371 | */ |
| 372 | function pvcTriggerEvent( name, response ) { |
| 373 | // remove unneeded data |
| 374 | const remove = ['dates', 'html', 'design'] |
| 375 | |
| 376 | remove.forEach( function ( prop ) { |
| 377 | delete response[prop]; |
| 378 | } ); |
| 379 | |
| 380 | // trigger event |
| 381 | const event = new CustomEvent( name, { |
| 382 | detail: response |
| 383 | } ); |
| 384 | |
| 385 | window.dispatchEvent( event ); |
| 386 | } |
| 387 | |
| 388 | } )( jQuery ); |