PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.11
Post Views Counter v1.3.11
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 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
admin-dashboard.js
291 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 onClick: function( e, element, legend ) {
148 var index = element.datasetIndex;
149 var ci = legend.chart;
150 var meta = ci.getDatasetMeta( index );
151
152 // set new hidden value
153 if ( ci.isDatasetVisible( index ) )
154 meta.hidden = true;
155 else
156 meta.hidden = false;
157
158 // rerender the chart
159 ci.update();
160
161 // update user options
162 pvcUpdateUserOptions( {
163 post_type: ci.data.datasets[index].post_type,
164 hidden: meta.hidden
165 } );
166 },
167 labels: {
168 boxWidth: 0,
169 boxHeight: 0,
170 fontSize: 12,
171 padding: 10,
172 usePointStyle: false,
173 textAlign: 'center'
174 }
175 }
176 },
177 scales: {
178 x: {
179 display: true,
180 title: {
181 display: false
182 }
183 },
184 y: {
185 display: true,
186 grace: 0,
187 beginAtZero: true,
188 title: {
189 display: false
190 },
191 ticks: {
192 precision: 0,
193 maxTicksLimit: 12
194 }
195 }
196 },
197 hover: {
198 mode: 'label'
199 }
200 }
201 };
202
203 config = pvcUpdateConfig( config, response );
204
205 window.pvcPostViewsChart = new Chart( document.getElementById( 'pvc-post-views-chart' ).getContext( '2d' ), config );
206 } else {
207 bindMonthEvents( response.months, container );
208
209 window.pvcPostViewsChart.config = pvcUpdateConfig( window.pvcPostViewsChart.config, response );
210 window.pvcPostViewsChart.update();
211 }
212 }
213 } );
214 }
215
216 /**
217 * Update post views widget.
218 */
219 function updatePostViewsWidget( period ) {
220 var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' );
221
222 if ( $( container ).length > 0 ) {
223 bindMonthEvents( false, container );
224
225 getPostViewsData( true, period, container );
226 }
227 }
228
229 /**
230 * Update post most viewed widget.
231 */
232 function updatePostMostViewedWidget( period ) {
233 var container = $( '#pvc-post-most-viewed' ).find( '.pvc-dashboard-container' );
234
235 if ( $( container ).length > 0 ) {
236 bindMonthEvents( false, container );
237
238 getPostMostViewedData( true, period, container );
239 }
240 }
241
242 /**
243 * Bind month events.
244 */
245 function bindMonthEvents( newMonths, container ) {
246 var months = $( container ).find( '.pvc-months' );
247
248 // replace months?
249 if ( newMonths !== false )
250 months[0].innerHTML = newMonths;
251
252 var prev = months[0].getElementsByClassName( 'prev' )[0];
253 var next = months[0].getElementsByClassName( 'next' )[0];
254
255 if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-post-most-viewed' )
256 prev.addEventListener( 'click', loadPostMostViewedData );
257 else
258 prev.addEventListener( 'click', loadPostViewsData );
259
260 // skip span
261 if ( next.tagName === 'A' ) {
262 if ( $( container ).closest( '.pvc-accordion-item' ).attr( 'id' ) === 'pvc-post-most-viewed' )
263 next.addEventListener( 'click', loadPostMostViewedData );
264 else
265 next.addEventListener( 'click', loadPostViewsData );
266 }
267 }
268
269 /**
270 * Load post views data.
271 */
272 function loadPostViewsData( e ) {
273 e.preventDefault();
274
275 var container = $( '#pvc-post-views' ).find( '.pvc-dashboard-container' );
276
277 getPostViewsData( false, e.target.dataset.date, container );
278 }
279
280 /**
281 * Load post most viewed data.
282 */
283 function loadPostMostViewedData( e ) {
284 e.preventDefault();
285
286 var container = $( '#pvc-post-most-viewed' ).find( '.pvc-dashboard-container' );
287
288 getPostMostViewedData( false, e.target.dataset.date, container );
289 }
290
291 } )( jQuery );