admin-dashboard.js
3 years ago
admin-post.js
5 years ago
admin-quick-edit.js
5 years ago
admin-settings.js
4 years ago
admin-widgets.js
5 years ago
block-editor.min.js
3 years ago
counter.js
3 years ago
counter.min.js
3 years ago
frontend.js
3 years ago
frontend.min.js
3 years ago
admin-dashboard.js
301 lines
| 1 | ( function( $ ) { |
| 2 | |
| 3 | /** |
| 4 | * Load initial data. |
| 5 | */ |
| 6 | window.addEventListener( 'load', function() { |
| 7 | updatePostViewsWidget( 'this_month' ); |
| 8 | updatePostMostViewedWidget( 'this_month' ); |
| 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 | /** |
| 38 | * Update user options. |
| 39 | */ |
| 40 | pvcUpdateUserOptions = function( options ) { |
| 41 | $.ajax( { |
| 42 | url: pvcArgs.ajaxURL, |
| 43 | type: 'POST', |
| 44 | dataType: 'json', |
| 45 | data: { |
| 46 | action: 'pvc_dashboard_user_options', |
| 47 | nonce: pvcArgs.nonceUser, |
| 48 | options: options |
| 49 | }, |
| 50 | success: function() {} |
| 51 | } ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Update configuration. |
| 56 | */ |
| 57 | pvcUpdateConfig = function( config, args ) { |
| 58 | // update datasets |
| 59 | config.data = args.data; |
| 60 | |
| 61 | // update tooltips with new dates |
| 62 | config.options.plugins.tooltip = { |
| 63 | callbacks: { |
| 64 | title: function( tooltip ) { |
| 65 | return args.data.dates[tooltip[0].dataIndex]; |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | // update colors |
| 71 | $.each( config.data.datasets, function( i, dataset ) { |
| 72 | dataset.fill = args.design.fill; |
| 73 | dataset.borderColor = args.design.borderColor; |
| 74 | dataset.backgroundColor = args.design.backgroundColor; |
| 75 | dataset.borderWidth = args.design.borderWidth; |
| 76 | dataset.borderDash = args.design.borderDash; |
| 77 | dataset.pointBorderColor = args.design.pointBorderColor; |
| 78 | dataset.pointBackgroundColor = args.design.pointBackgroundColor; |
| 79 | dataset.pointBorderWidth = args.design.pointBorderWidth; |
| 80 | } ); |
| 81 | |
| 82 | return config; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get post most viewed data. |
| 87 | */ |
| 88 | function getPostMostViewedData( init, period, container ) { |
| 89 | $( container ).addClass( 'loading' ).find( '.spinner' ).addClass( 'is-active' ); |
| 90 | |
| 91 | $.ajax( { |
| 92 | url: pvcArgs.ajaxURL, |
| 93 | type: 'POST', |
| 94 | dataType: 'json', |
| 95 | data: { |
| 96 | action: 'pvc_dashboard_post_most_viewed', |
| 97 | nonce: pvcArgs.nonce, |
| 98 | period: period |
| 99 | }, |
| 100 | success: function( response ) { |
| 101 | // remove loader |
| 102 | $( container ).removeClass( 'loading' ); |
| 103 | $( container ).find( '.spinner' ).removeClass( 'is-active' ); |
| 104 | |
| 105 | // next call? |
| 106 | if ( ! init ) |
| 107 | bindMonthEvents( response.months, container ); |
| 108 | |
| 109 | $( container ).find( '#pvc-post-most-viewed-content' ).html( response.html ); |
| 110 | } |
| 111 | } ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get post views data. |
| 116 | */ |
| 117 | function getPostViewsData( init, period, container ) { |
| 118 | $( container ).addClass( 'loading' ).find( '.spinner' ).addClass( 'is-active' ); |
| 119 | |
| 120 | $.ajax( { |
| 121 | url: pvcArgs.ajaxURL, |
| 122 | type: 'POST', |
| 123 | dataType: 'json', |
| 124 | data: { |
| 125 | action: 'pvc_dashboard_post_views_chart', |
| 126 | nonce: pvcArgs.nonce, |
| 127 | period: period |
| 128 | }, |
| 129 | success: function( response ) { |
| 130 | // remove loader |
| 131 | $( container ).removeClass( 'loading' ); |
| 132 | $( container ).find( '.spinner' ).removeClass( 'is-active' ); |
| 133 | |
| 134 | // first call? |
| 135 | if ( init ) { |
| 136 | var config = { |
| 137 | type: 'line', |
| 138 | options: { |
| 139 | maintainAspectRatio: true, |
| 140 | responsive: true, |
| 141 | plugins: { |
| 142 | legend: { |
| 143 | display: true, |
| 144 | position: 'bottom', |
| 145 | align: 'center', |
| 146 | fullSize: true, |
| 147 | onHover: function( e ) { |
| 148 | e.native.target.style.cursor = 'pointer'; |
| 149 | }, |
| 150 | onLeave: function( e ) { |
| 151 | e.native.target.style.cursor = 'default'; |
| 152 | }, |
| 153 | onClick: function( e, element, legend ) { |
| 154 | var index = element.datasetIndex; |
| 155 | var ci = legend.chart; |
| 156 | var meta = ci.getDatasetMeta( index ); |
| 157 | |
| 158 | // set new hidden value |
| 159 | if ( ci.isDatasetVisible( index ) ) |
| 160 | meta.hidden = true; |
| 161 | else |
| 162 | meta.hidden = false; |
| 163 | |
| 164 | // rerender the chart |
| 165 | ci.update(); |
| 166 | |
| 167 | // update user options |
| 168 | pvcUpdateUserOptions( { |
| 169 | post_type: ci.data.datasets[index].post_type, |
| 170 | hidden: meta.hidden |
| 171 | } ); |
| 172 | }, |
| 173 | labels: { |
| 174 | boxWidth: 8, |
| 175 | boxHeight: 8, |
| 176 | font: { |
| 177 | size: 13, |
| 178 | weight: 'normal', |
| 179 | family: "'-apple-system', 'BlinkMacSystemFont', 'Segoe UI', 'Roboto', 'Oxygen-Sans', 'Ubuntu', 'Cantarell', 'Helvetica Neue', 'sans-serif'" |
| 180 | }, |
| 181 | padding: 10, |
| 182 | usePointStyle: false, |
| 183 | textAlign: 'center' |
| 184 | } |
| 185 | } |
| 186 | }, |
| 187 | scales: { |
| 188 | x: { |
| 189 | display: true, |
| 190 | title: { |
| 191 | display: false |
| 192 | } |
| 193 | }, |
| 194 | y: { |
| 195 | display: true, |
| 196 | grace: 0, |
| 197 | beginAtZero: true, |
| 198 | title: { |
| 199 | display: false |
| 200 | }, |
| 201 | ticks: { |
| 202 | precision: 0, |
| 203 | maxTicksLimit: 12 |
| 204 | } |
| 205 | } |
| 206 | }, |
| 207 | hover: { |
| 208 | mode: 'label' |
| 209 | } |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | config = pvcUpdateConfig( config, response ); |
| 214 | |
| 215 | window.pvcPostViewsChart = new Chart( document.getElementById( 'pvc-post-views-chart' ).getContext( '2d' ), config ); |
| 216 | } else { |
| 217 | bindMonthEvents( response.months, container ); |
| 218 | |
| 219 | window.pvcPostViewsChart.config = pvcUpdateConfig( window.pvcPostViewsChart.config, response ); |
| 220 | window.pvcPostViewsChart.update(); |
| 221 | } |
| 222 | } |
| 223 | } ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Update post views widget. |
| 228 | */ |
| 229 | function updatePostViewsWidget( period ) { |
| 230 | var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' ); |
| 231 | |
| 232 | if ( $( container ).length > 0 ) { |
| 233 | bindMonthEvents( false, container ); |
| 234 | |
| 235 | getPostViewsData( true, period, container ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Update post most viewed widget. |
| 241 | */ |
| 242 | function updatePostMostViewedWidget( period ) { |
| 243 | var container = $( '#pvc-post-most-viewed' ).find( '.pvc-dashboard-container' ); |
| 244 | |
| 245 | if ( $( container ).length > 0 ) { |
| 246 | bindMonthEvents( false, container ); |
| 247 | |
| 248 | getPostMostViewedData( true, period, container ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Bind month events. |
| 254 | */ |
| 255 | function bindMonthEvents( newMonths, container ) { |
| 256 | var months = $( container ).find( '.pvc-months' ); |
| 257 | |
| 258 | // replace months? |
| 259 | if ( newMonths !== false ) |
| 260 | months[0].innerHTML = newMonths; |
| 261 | |
| 262 | var prev = months[0].getElementsByClassName( 'prev' )[0]; |
| 263 | var next = months[0].getElementsByClassName( 'next' )[0]; |
| 264 | |
| 265 | if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-post-most-viewed' ) |
| 266 | prev.addEventListener( 'click', loadPostMostViewedData ); |
| 267 | else |
| 268 | prev.addEventListener( 'click', loadPostViewsData ); |
| 269 | |
| 270 | // skip span |
| 271 | if ( next.tagName === 'A' ) { |
| 272 | if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-post-most-viewed' ) |
| 273 | next.addEventListener( 'click', loadPostMostViewedData ); |
| 274 | else |
| 275 | next.addEventListener( 'click', loadPostViewsData ); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Load post views data. |
| 281 | */ |
| 282 | function loadPostViewsData( e ) { |
| 283 | e.preventDefault(); |
| 284 | |
| 285 | var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' ); |
| 286 | |
| 287 | getPostViewsData( false, e.target.dataset.date, container ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Load post most viewed data. |
| 292 | */ |
| 293 | function loadPostMostViewedData( e ) { |
| 294 | e.preventDefault(); |
| 295 | |
| 296 | var container = $( '#pvc-post-most-viewed' ).find( '.pvc-dashboard-container' ); |
| 297 | |
| 298 | getPostMostViewedData( false, e.target.dataset.date, container ); |
| 299 | } |
| 300 | |
| 301 | } )( jQuery ); |