admin-dashboard.js
4 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
4 years ago
frontend.js
4 years ago
gutenberg.min.js
6 years ago
admin-dashboard.js
263 lines
| 1 | ( function ( $ ) { |
| 2 | |
| 3 | window.onload = function () { |
| 4 | pvcUpdateChart( 'this_month' ); |
| 5 | pvcUpdateMostViewed( 'this_month' ); |
| 6 | }; |
| 7 | |
| 8 | // ready event |
| 9 | $( function() { |
| 10 | $( '.pvc-accordion-header' ).on( 'click', function( e ) { |
| 11 | $( this ).closest( '.pvc-accordion-item' ).toggleClass( 'pvc-collapsed' ); |
| 12 | |
| 13 | var items = $( '#pvc-dashboard-accordion' ).find( '.pvc-accordion-item' ), |
| 14 | menuItems = {}; |
| 15 | |
| 16 | if ( items.length > 0 ) { |
| 17 | $( items ).each( function( index, item ) { |
| 18 | var itemName = $( item ).prop( 'id' ); |
| 19 | itemName = itemName.replace( 'pvc-', '' ); |
| 20 | |
| 21 | menuItems[itemName] = $( item ).hasClass( 'pvc-collapsed' ); |
| 22 | } ); |
| 23 | } |
| 24 | |
| 25 | // update user options |
| 26 | var userOptions = { |
| 27 | menu_items: menuItems |
| 28 | }; |
| 29 | |
| 30 | pvcUpdateUserOptions( userOptions ); |
| 31 | } ); |
| 32 | } ); |
| 33 | |
| 34 | function pvcGetViewedData( init, period, container ) { |
| 35 | $( container ).addClass( 'loading' ).find( '.spinner' ).addClass( 'is-active' ); |
| 36 | |
| 37 | $.ajax( { |
| 38 | url: pvcArgs.ajaxURL, |
| 39 | type: 'POST', |
| 40 | dataType: 'json', |
| 41 | data: { |
| 42 | action: 'pvc_dashboard_most_viewed', |
| 43 | nonce: pvcArgs.nonce, |
| 44 | period: period |
| 45 | }, |
| 46 | success: function ( response ) { |
| 47 | // remove loader |
| 48 | $( container ).removeClass( 'loading' ); |
| 49 | $( container ).find( '.spinner' ).removeClass( 'is-active' ); |
| 50 | |
| 51 | // first call? |
| 52 | if ( init ) { |
| 53 | $( container ).find( '#pvc-viewed' ).html( response.html ); |
| 54 | } else { |
| 55 | pvcBindMonthEvents( response.months, container ); |
| 56 | |
| 57 | $( container ).find( '#pvc-viewed' ).html( response.html ); |
| 58 | } |
| 59 | } |
| 60 | } ); |
| 61 | } |
| 62 | |
| 63 | function pvcGetChartData( init, period, container ) { |
| 64 | $( container ).addClass( 'loading' ).find( '.spinner' ).addClass( 'is-active' ); |
| 65 | |
| 66 | $.ajax( { |
| 67 | url: pvcArgs.ajaxURL, |
| 68 | type: 'POST', |
| 69 | dataType: 'json', |
| 70 | data: { |
| 71 | action: 'pvc_dashboard_chart', |
| 72 | nonce: pvcArgs.nonce, |
| 73 | period: period |
| 74 | }, |
| 75 | success: function ( response ) { |
| 76 | // remove loader |
| 77 | $( container ).removeClass( 'loading' ); |
| 78 | $( container ).find( '.spinner' ).removeClass( 'is-active' ); |
| 79 | |
| 80 | // first call? |
| 81 | if ( init ) { |
| 82 | var config = { |
| 83 | type: 'line', |
| 84 | options: { |
| 85 | responsive: true, |
| 86 | legend: { |
| 87 | display: true, |
| 88 | position: 'bottom', |
| 89 | onClick: function( e, element ) { |
| 90 | var index = element.datasetIndex, |
| 91 | ci = this.chart, |
| 92 | meta = ci.getDatasetMeta( index ); |
| 93 | |
| 94 | // set new hidden value |
| 95 | meta.hidden = ( meta.hidden === null ? ! ci.data.datasets[index].hidden : null ); |
| 96 | |
| 97 | // rerender the chart |
| 98 | ci.update(); |
| 99 | |
| 100 | // update user options |
| 101 | var userOptions = { |
| 102 | post_type: ci.data.datasets[index].post_type, |
| 103 | hidden: meta.hidden === null ? false : meta.hidden |
| 104 | } |
| 105 | |
| 106 | pvcUpdateUserOptions( userOptions ); |
| 107 | }, |
| 108 | labels: { |
| 109 | boxWidth: 0, |
| 110 | fontSize: 12, |
| 111 | padding: 10, |
| 112 | usePointStyle: false |
| 113 | } |
| 114 | }, |
| 115 | scales: { |
| 116 | xAxes: [ { |
| 117 | display: true, |
| 118 | scaleLabel: { |
| 119 | display: false, |
| 120 | labelString: response.text.xAxes |
| 121 | } |
| 122 | } ], |
| 123 | yAxes: [ { |
| 124 | display: true, |
| 125 | scaleLabel: { |
| 126 | display: false, |
| 127 | labelString: response.text.yAxes |
| 128 | }, |
| 129 | ticks: { |
| 130 | precision: 0, |
| 131 | beginAtZero: true, |
| 132 | maxTicksLimit: 12 |
| 133 | } |
| 134 | } ] |
| 135 | }, |
| 136 | hover: { |
| 137 | mode: 'label' |
| 138 | } |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | config = pvcUpdateConfig( config, response ); |
| 143 | |
| 144 | window.chartPVC = new Chart( document.getElementById( 'pvc-chart' ).getContext( '2d' ), config ); |
| 145 | } else { |
| 146 | pvcBindMonthEvents( response.months, container ); |
| 147 | |
| 148 | window.chartPVC.config = pvcUpdateConfig( window.chartPVC.config, response ); |
| 149 | window.chartPVC.update(); |
| 150 | } |
| 151 | } |
| 152 | } ); |
| 153 | } |
| 154 | |
| 155 | function pvcUpdateUserOptions( options ) { |
| 156 | $.ajax( { |
| 157 | url: pvcArgs.ajaxURL, |
| 158 | type: 'POST', |
| 159 | dataType: 'json', |
| 160 | data: { |
| 161 | action: 'pvc_dashboard_user_options', |
| 162 | nonce: pvcArgs.nonceUser, |
| 163 | options: options |
| 164 | }, |
| 165 | success: function ( ) {} |
| 166 | } ); |
| 167 | } |
| 168 | |
| 169 | function pvcUpdateConfig( config, args ) { |
| 170 | // update datasets |
| 171 | config.data = args.data; |
| 172 | |
| 173 | // update tooltips with new dates |
| 174 | config.options.tooltips = { |
| 175 | callbacks: { |
| 176 | title: function ( tooltip ) { |
| 177 | return args.data.dates[tooltip[0].index]; |
| 178 | } |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | // update labels |
| 183 | config.options.scales.xAxes[0].scaleLabel.labelString = args.text.xAxes; |
| 184 | config.options.scales.yAxes[0].scaleLabel.labelString = args.text.yAxes; |
| 185 | |
| 186 | // update colors |
| 187 | $.each( config.data.datasets, function ( i, dataset ) { |
| 188 | dataset.fill = args.design.fill; |
| 189 | dataset.borderColor = args.design.borderColor; |
| 190 | dataset.backgroundColor = args.design.backgroundColor; |
| 191 | dataset.borderWidth = args.design.borderWidth; |
| 192 | dataset.borderDash = args.design.borderDash; |
| 193 | dataset.pointBorderColor = args.design.pointBorderColor; |
| 194 | dataset.pointBackgroundColor = args.design.pointBackgroundColor; |
| 195 | dataset.pointBorderWidth = args.design.pointBorderWidth; |
| 196 | } ); |
| 197 | |
| 198 | return config; |
| 199 | } |
| 200 | |
| 201 | function pvcUpdateChart( period ) { |
| 202 | var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' ); |
| 203 | |
| 204 | if ( $( container ).length > 0 ) { |
| 205 | pvcBindMonthEvents( false, container ); |
| 206 | |
| 207 | pvcGetChartData( true, period, container ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | function pvcUpdateMostViewed( period ) { |
| 212 | var container = $( '#pvc-most-viewed' ).find( '.pvc-dashboard-container' ); |
| 213 | |
| 214 | if ( $( container ).length > 0 ) { |
| 215 | pvcBindMonthEvents( false, container ); |
| 216 | |
| 217 | pvcGetViewedData( true, period, container ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | function pvcBindMonthEvents( newMonths, container ) { |
| 222 | var months = $( container ).find( '.pvc-months' ); |
| 223 | |
| 224 | // replace months? |
| 225 | if ( newMonths !== false ) |
| 226 | months[0].innerHTML = newMonths; |
| 227 | |
| 228 | var prev = months[0].getElementsByClassName( 'prev' ); |
| 229 | var next = months[0].getElementsByClassName( 'next' ); |
| 230 | |
| 231 | if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-most-viewed' ) { |
| 232 | prev[0].addEventListener( 'click', pvcLoadMostViewedData ); |
| 233 | } else { |
| 234 | prev[0].addEventListener( 'click', pvcLoadChartData ); |
| 235 | } |
| 236 | |
| 237 | // skip span |
| 238 | if ( next[0].tagName === 'A' ) { |
| 239 | if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-most-viewed' ) { |
| 240 | next[0].addEventListener( 'click', pvcLoadMostViewedData ); |
| 241 | } else { |
| 242 | next[0].addEventListener( 'click', pvcLoadChartData ); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | function pvcLoadChartData( e ) { |
| 248 | e.preventDefault(); |
| 249 | |
| 250 | var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' ); |
| 251 | |
| 252 | pvcGetChartData( false, e.target.dataset.date, container ); |
| 253 | } |
| 254 | |
| 255 | function pvcLoadMostViewedData( e ) { |
| 256 | e.preventDefault(); |
| 257 | |
| 258 | var container = $( '#pvc-most-viewed' ).find( '.pvc-dashboard-container' ); |
| 259 | |
| 260 | pvcGetViewedData( false, e.target.dataset.date, container ); |
| 261 | } |
| 262 | |
| 263 | } )( jQuery ); |