PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.4
Post Views Counter v1.3.4
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / js / admin-dashboard.js
post-views-counter / js Last commit date
admin-dashboard.js 5 years ago admin-post.js 5 years ago admin-quick-edit.js 5 years ago admin-settings.js 5 years ago admin-widgets.js 5 years ago frontend.js 5 years ago gutenberg.min.js 7 years ago
admin-dashboard.js
258 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 } ]
130 },
131 hover: {
132 mode: 'label'
133 }
134 }
135 };
136
137 config = pvcUpdateConfig( config, response );
138
139 window.chartPVC = new Chart( document.getElementById( 'pvc-chart' ).getContext( '2d' ), config );
140 } else {
141 pvcBindMonthEvents( response.months, container );
142
143 window.chartPVC.config = pvcUpdateConfig( window.chartPVC.config, response );
144 window.chartPVC.update();
145 }
146 }
147 } );
148 }
149
150 function pvcUpdateUserOptions( options ) {
151 $.ajax( {
152 url: pvcArgs.ajaxURL,
153 type: 'POST',
154 dataType: 'json',
155 data: {
156 action: 'pvc_dashboard_user_options',
157 nonce: pvcArgs.nonceUser,
158 options: options
159 },
160 success: function ( ) {}
161 } );
162 }
163
164 function pvcUpdateConfig( config, args ) {
165 // update datasets
166 config.data = args.data;
167
168 // update tooltips with new dates
169 config.options.tooltips = {
170 callbacks: {
171 title: function ( tooltip ) {
172 return args.data.dates[tooltip[0].index];
173 }
174 }
175 };
176
177 // update labels
178 config.options.scales.xAxes[0].scaleLabel.labelString = args.text.xAxes;
179 config.options.scales.yAxes[0].scaleLabel.labelString = args.text.yAxes;
180
181 // update colors
182 $.each( config.data.datasets, function ( i, dataset ) {
183 dataset.fill = args.design.fill;
184 dataset.borderColor = args.design.borderColor;
185 dataset.backgroundColor = args.design.backgroundColor;
186 dataset.borderWidth = args.design.borderWidth;
187 dataset.borderDash = args.design.borderDash;
188 dataset.pointBorderColor = args.design.pointBorderColor;
189 dataset.pointBackgroundColor = args.design.pointBackgroundColor;
190 dataset.pointBorderWidth = args.design.pointBorderWidth;
191 } );
192
193 return config;
194 }
195
196 function pvcUpdateChart( period ) {
197 var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' );
198
199 if ( $( container ).length > 0 ) {
200 pvcBindMonthEvents( false, container );
201
202 pvcGetChartData( true, period, container );
203 }
204 }
205
206 function pvcUpdateMostViewed( period ) {
207 var container = $( '#pvc-most-viewed' ).find( '.pvc-dashboard-container' );
208
209 if ( $( container ).length > 0 ) {
210 pvcBindMonthEvents( false, container );
211
212 pvcGetViewedData( true, period, container );
213 }
214 }
215
216 function pvcBindMonthEvents( newMonths, container ) {
217 var months = $( container ).find( '.pvc-months' );
218
219 // replace months?
220 if ( newMonths !== false )
221 months[0].innerHTML = newMonths;
222
223 var prev = months[0].getElementsByClassName( 'prev' );
224 var next = months[0].getElementsByClassName( 'next' );
225
226 if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-most-viewed' ) {
227 prev[0].addEventListener( 'click', pvcLoadMostViewedData );
228 } else {
229 prev[0].addEventListener( 'click', pvcLoadChartData );
230 }
231
232 // skip span
233 if ( next[0].tagName === 'A' ) {
234 if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-most-viewed' ) {
235 next[0].addEventListener( 'click', pvcLoadMostViewedData );
236 } else {
237 next[0].addEventListener( 'click', pvcLoadChartData );
238 }
239 }
240 }
241
242 function pvcLoadChartData( e ) {
243 e.preventDefault();
244
245 var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' );
246
247 pvcGetChartData( false, e.target.dataset.date, container );
248 }
249
250 function pvcLoadMostViewedData( e ) {
251 e.preventDefault();
252
253 var container = $( '#pvc-most-viewed' ).find( '.pvc-dashboard-container' );
254
255 pvcGetViewedData( false, e.target.dataset.date, container );
256 }
257
258 } )( jQuery );