admin-dashboard.js
6 years ago
admin-post.js
6 years ago
admin-quick-edit.js
6 years ago
admin-settings.js
6 years ago
admin-widgets.js
6 years ago
chart.min.js
6 years ago
frontend.js
6 years ago
gutenberg.min.js
6 years ago
admin-dashboard.js
167 lines
| 1 | ( function ( $ ) { |
| 2 | |
| 3 | window.onload = function () { |
| 4 | updateChart( 'this_month' ); |
| 5 | }; |
| 6 | |
| 7 | function ajaxGetChartData( init, period, container ) { |
| 8 | $.ajax( { |
| 9 | url: pvcArgs.ajaxURL, |
| 10 | type: 'POST', |
| 11 | dataType: 'json', |
| 12 | data: { |
| 13 | action: 'pvc_dashboard_chart', |
| 14 | nonce: pvcArgs.nonce, |
| 15 | period: period |
| 16 | }, |
| 17 | success: function ( args ) { |
| 18 | // first call? |
| 19 | if ( init ) { |
| 20 | container.removeClass( 'loading' ); |
| 21 | container.find( '.spinner' ).removeClass( 'is-active' ); |
| 22 | |
| 23 | var config = { |
| 24 | type: 'line', |
| 25 | options: { |
| 26 | responsive: true, |
| 27 | legend: { |
| 28 | display: true, |
| 29 | position: 'bottom', |
| 30 | onClick: function( e, element ) { |
| 31 | var index = element.datasetIndex, |
| 32 | ci = this.chart, |
| 33 | meta = ci.getDatasetMeta( index ); |
| 34 | |
| 35 | // set new hidden value |
| 36 | meta.hidden = ( meta.hidden === null ? ! ci.data.datasets[index].hidden : null ); |
| 37 | |
| 38 | // rerender the chart |
| 39 | ci.update(); |
| 40 | |
| 41 | ajaxUpdateChartPostTypes( ci.data.datasets[index].post_type, meta.hidden === null ? false : meta.hidden ); |
| 42 | }, |
| 43 | labels: { |
| 44 | boxWidth: 0, |
| 45 | fontSize: 12, |
| 46 | padding: 10, |
| 47 | usePointStyle: false |
| 48 | } |
| 49 | }, |
| 50 | scales: { |
| 51 | xAxes: [ { |
| 52 | display: true, |
| 53 | scaleLabel: { |
| 54 | display: false, |
| 55 | labelString: args.text.xAxes |
| 56 | } |
| 57 | } ], |
| 58 | yAxes: [ { |
| 59 | display: true, |
| 60 | scaleLabel: { |
| 61 | display: false, |
| 62 | labelString: args.text.yAxes |
| 63 | } |
| 64 | } ] |
| 65 | }, |
| 66 | hover: { |
| 67 | mode: 'label' |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | config = updateConfig( config, args ); |
| 73 | |
| 74 | window.chartPVC = new Chart( document.getElementById( 'pvc_chart' ).getContext( '2d' ), config ); |
| 75 | } else { |
| 76 | bindMonthEvents( args.months ); |
| 77 | |
| 78 | window.chartPVC.config = updateConfig( window.chartPVC.config, args ); |
| 79 | window.chartPVC.update(); |
| 80 | } |
| 81 | } |
| 82 | } ); |
| 83 | } |
| 84 | |
| 85 | function ajaxUpdateChartPostTypes( post_type, hidden ) { |
| 86 | $.ajax( { |
| 87 | url: pvcArgs.ajaxURL, |
| 88 | type: 'POST', |
| 89 | dataType: 'json', |
| 90 | data: { |
| 91 | action: 'pvc_dashboard_chart_user_post_types', |
| 92 | nonce: pvcArgs.nonceUser, |
| 93 | post_type: post_type, |
| 94 | hidden: hidden |
| 95 | }, |
| 96 | success: function ( ) {} |
| 97 | } ); |
| 98 | } |
| 99 | |
| 100 | function updateConfig( config, args ) { |
| 101 | // update datasets |
| 102 | config.data = args.data; |
| 103 | |
| 104 | // update tooltips with new dates |
| 105 | config.options.tooltips = { |
| 106 | callbacks: { |
| 107 | title: function ( tooltip ) { |
| 108 | return args.data.dates[tooltip[0].index]; |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | // update labels |
| 114 | config.options.scales.xAxes[0].scaleLabel.labelString = args.text.xAxes; |
| 115 | config.options.scales.yAxes[0].scaleLabel.labelString = args.text.yAxes; |
| 116 | |
| 117 | // update colors |
| 118 | $.each( config.data.datasets, function ( i, dataset ) { |
| 119 | dataset.fill = args.design.fill; |
| 120 | dataset.borderColor = args.design.borderColor; |
| 121 | dataset.backgroundColor = args.design.backgroundColor; |
| 122 | dataset.borderWidth = args.design.borderWidth; |
| 123 | dataset.borderDash = args.design.borderDash; |
| 124 | dataset.pointBorderColor = args.design.pointBorderColor; |
| 125 | dataset.pointBackgroundColor = args.design.pointBackgroundColor; |
| 126 | dataset.pointBorderWidth = args.design.pointBorderWidth; |
| 127 | } ); |
| 128 | |
| 129 | return config; |
| 130 | } |
| 131 | |
| 132 | function updateChart( period ) { |
| 133 | var container = document.getElementById( 'pvc_dashboard_container' ); |
| 134 | |
| 135 | if ( $( container ).length > 0 ) { |
| 136 | bindMonthEvents( false ); |
| 137 | |
| 138 | $( container ).addClass( 'loading' ).append( '<span class="spinner is-active"></span>' ); |
| 139 | |
| 140 | ajaxGetChartData( true, period, $( container ) ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | function bindMonthEvents( newMonths ) { |
| 145 | var months = document.getElementsByClassName( 'pvc_months' ); |
| 146 | |
| 147 | // replace months? |
| 148 | if ( newMonths !== false ) |
| 149 | months[0].innerHTML = newMonths; |
| 150 | |
| 151 | var prev = months[0].getElementsByClassName( 'prev' ); |
| 152 | var next = months[0].getElementsByClassName( 'next' ); |
| 153 | |
| 154 | prev[0].addEventListener( 'click', loadChartData ); |
| 155 | |
| 156 | // skip span |
| 157 | if ( next[0].tagName === 'A' ) |
| 158 | next[0].addEventListener( 'click', loadChartData ); |
| 159 | } |
| 160 | |
| 161 | function loadChartData( e ) { |
| 162 | e.preventDefault(); |
| 163 | |
| 164 | ajaxGetChartData( false, e.target.dataset.date ); |
| 165 | } |
| 166 | |
| 167 | } )( jQuery ); |