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