PluginProbe
WebberZone Top 10 — Popular Posts / trunk
WebberZone Top 10 — Popular Posts vtrunk
4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 1.6.2 All 116 releases
top-10 / includes / admin / js / admin-scripts.js

admin-scripts.js in WebberZone Top 10 — Popular Posts trunk, at includes/admin/js/admin-scripts.js

401 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function ($) {
2
3 $('button[name="tptn_cache_clear"]').on('click', function () {
4 var $button = $(this);
5 var networkWide = '1' === $button.attr('data-network-wide');
6 var confirmMessage = networkWide && top_ten_admin_data.strings.network_confirm_message
7 ? top_ten_admin_data.strings.network_confirm_message
8 : top_ten_admin_data.strings.confirm_message;
9
10 if (confirm(confirmMessage)) {
11 var originalText = $button.text();
12 var clearingText = top_ten_admin_data.strings.clearing_text ? top_ten_admin_data.strings.clearing_text : 'Clearing...';
13 $button.prop('disabled', true).text(clearingText).append(' <span class="spinner is-active"></span>');
14 clearCache($button, originalText, networkWide);
15 }
16 });
17
18 // Function to clear the cache.
19 function clearCache($button, originalText, networkWide) {
20 $.post(ajaxurl, {
21 action: 'tptn_clear_cache',
22 security: top_ten_admin_data.security,
23 network_wide: networkWide ? '1' : '0'
24 }, function (response) {
25 if (response.success) {
26 alert(response.data.message);
27 } else {
28 alert(top_ten_admin_data.strings.fail_message);
29 }
30 }).fail(function (jqXHR, textStatus) {
31 alert(top_ten_admin_data.strings.request_fail_message + textStatus);
32 }).always(function () {
33 if ($button && $button.length) {
34 $button.prop('disabled', false).text(originalText).find('.spinner').remove();
35 }
36 });
37 }
38
39 /**
40 * Handle style changes and enforce valid thumbnail location settings.
41 */
42 function tptnHandleThumbnailStyleChange() {
43 var styleSelect = document.querySelector('select[name="tptn_settings[tptn_styles]"]');
44 if (!styleSelect) {
45 return;
46 }
47
48 var strings = top_ten_admin_data.strings || {};
49
50 var messageConfig = {
51 'left_thumbs': 'left_thumbs_message',
52 'grid_thumbs': 'grid_thumbs_message',
53 'text_only': 'text_only_message'
54 };
55
56 function updateMessage(selectedStyle) {
57 var messageKey = messageConfig[selectedStyle];
58 var postThumbOptions = document.querySelectorAll('input[name="tptn_settings[post_thumb_op]"]');
59 if (postThumbOptions.length === 0) {
60 return;
61 }
62
63 var container = postThumbOptions[0].closest('td');
64 if (!container) {
65 return;
66 }
67
68 var existingMessage = container.querySelector('.tptn-js-message');
69 if (!messageKey) {
70 if (existingMessage) {
71 existingMessage.remove();
72 }
73 return;
74 }
75
76 var messageText = strings[messageKey] || '';
77 if (!existingMessage) {
78 var message = document.createElement('p');
79 message.className = 'description tptn-js-message';
80 message.style.color = '#9B0800';
81 message.textContent = messageText;
82 container.appendChild(message);
83 } else if (existingMessage.textContent !== messageText) {
84 existingMessage.textContent = messageText;
85 }
86 }
87
88 function updateFields() {
89 var selectedStyle = styleSelect.value;
90 var postThumbOptions = document.querySelectorAll('input[name="tptn_settings[post_thumb_op]"]');
91
92 // Update the inline message first.
93 updateMessage(selectedStyle);
94
95 // Reset all radios to enabled, then re-disable as needed.
96 postThumbOptions.forEach(function (el) {
97 el.disabled = false;
98 });
99
100 // Special handling for left/grid thumbs: restrict post_thumb_op to inline, after, or thumbs_only.
101 if ('left_thumbs' === selectedStyle || 'grid_thumbs' === selectedStyle) {
102 var allowed = ['inline', 'after', 'thumbs_only'];
103 var currentChecked = document.querySelector('input[name="tptn_settings[post_thumb_op]"]:checked');
104 var needsFallback = !currentChecked || -1 === allowed.indexOf(currentChecked.value);
105
106 postThumbOptions.forEach(function (el) {
107 if (-1 === allowed.indexOf(el.value)) {
108 el.disabled = true;
109 }
110 });
111
112 if (needsFallback) {
113 var inlineRadio = document.querySelector('input[name="tptn_settings[post_thumb_op]"][value="inline"]');
114 if (inlineRadio) {
115 inlineRadio.checked = true;
116 }
117 }
118 }
119
120 // Special handling for text_only: force post_thumb_op to text_only.
121 if ('text_only' === selectedStyle) {
122 postThumbOptions.forEach(function (el) {
123 el.disabled = true;
124 });
125
126 var textOnlyRadio = document.querySelector('input[name="tptn_settings[post_thumb_op]"][value="text_only"]');
127 if (textOnlyRadio) {
128 textOnlyRadio.checked = true;
129 }
130 }
131 }
132
133 updateFields();
134 styleSelect.addEventListener('change', updateFields);
135 }
136
137 tptnHandleThumbnailStyleChange();
138
139 function updateFastConfigUI($button, data) {
140 if (!data) {
141 return;
142 }
143
144 if (data.status_html) {
145 var $status = $button.closest('td').find('.tptn-fast-config-status').first();
146 if (!$status.length) {
147 $status = $('.tptn-fast-config-status').first();
148 }
149 if ($status.length) {
150 $status.replaceWith($(data.status_html));
151 }
152 }
153
154 if (data.generate_nonce) {
155 $('.tptn-generate-fast-config').data('nonce', data.generate_nonce);
156 }
157
158 if (data.delete_nonce) {
159 $('.tptn-delete-fast-config').data('nonce', data.delete_nonce);
160 }
161
162 if (typeof data.has_config !== 'undefined') {
163 var $deleteBtn = $('.tptn-delete-fast-config');
164 if ($deleteBtn.length) {
165 $deleteBtn.prop('disabled', !data.has_config);
166 }
167 }
168
169 if (data.message) {
170 alert(data.message);
171 }
172 }
173
174 // Function to generate fast config.
175 function generateFastConfig(button) {
176 var $button = $(button);
177 var originalText = $button.text();
178 $button.prop('disabled', true).text('Generating...');
179
180 $.post(ajaxurl, {
181 action: 'tptn_generate_fast_config',
182 security: $button.data('nonce')
183 }, function (response) {
184 if (response && response.data) {
185 if (response.data.nonce) {
186 $button.data('nonce', response.data.nonce);
187 }
188 updateFastConfigUI($button, response.data);
189 }
190 }).fail(function (jqXHR, textStatus) {
191 if (window.console && window.console.error) {
192 window.console.error(top_ten_admin_data.strings.request_fail_message + textStatus);
193 }
194 }).always(function () {
195 $button.prop('disabled', false).text(originalText);
196 });
197 }
198
199 // Function to delete fast config.
200 function deleteFastConfig(button) {
201 var $button = $(button);
202 var originalText = $button.text();
203 $button.prop('disabled', true).text('Deleting...');
204
205 $.post(ajaxurl, {
206 action: 'tptn_delete_fast_config',
207 security: $button.data('nonce')
208 }, function (response) {
209 if (response && response.data) {
210 if (response.data.nonce) {
211 $button.data('nonce', response.data.nonce);
212 }
213 updateFastConfigUI($button, response.data);
214 }
215 }).fail(function (jqXHR, textStatus) {
216 if (window.console && window.console.error) {
217 window.console.error(top_ten_admin_data.strings.request_fail_message + textStatus);
218 }
219 }).always(function () {
220 $button.text(originalText);
221 if ($button.data('nonce')) {
222 $button.prop('disabled', false);
223 }
224 });
225 }
226
227 // Handle fast config generation button click.
228 $('body').on('click', '.tptn-generate-fast-config', function () {
229 var confirmMessage = $(this).data('confirm');
230 if (confirm(confirmMessage)) {
231 generateFastConfig(this);
232 }
233 });
234
235 // Handle fast config deletion button click.
236 $('body').on('click', '.tptn-delete-fast-config', function () {
237 var confirmMessage = $(this).data('confirm');
238 if (confirm(confirmMessage)) {
239 deleteFastConfig(this);
240 }
241 });
242
243 $(function () {
244 var $tabsContainer = $("#dashboard-historical-visits");
245 if (!$tabsContainer.length) {
246 return;
247 }
248
249 function loadDashboardTab($panel) {
250 if (!$panel.length || $panel.data('tptn-loaded') || $panel.data('tptn-loading')) {
251 return;
252 }
253 var $content = $panel.find('.tptn-dashboard-tab-content');
254 if (!$content.length) {
255 return;
256 }
257
258 $panel.data('tptn-loading', true).attr('aria-busy', 'true');
259 $.post(
260 tptn_chart_data.tabs_url || ajaxurl,
261 {
262 action: 'tptn_dashboard_tab',
263 security: tptn_chart_data.security,
264 tab: $panel.data('tptn-tab'),
265 network: parseInt($tabsContainer.data('tptn-network'), 10) || 0
266 },
267 function (response) {
268 if (response && response.success && response.data && typeof response.data.html !== 'undefined') {
269 $content.html(response.data.html).data('tptn-loaded', true);
270 } else {
271 $content.html('<p>' + tptn_chart_data.tab_error + '</p>');
272 }
273 },
274 'json'
275 ).fail(function () {
276 $content.html('<p>' + tptn_chart_data.tab_error + '</p>');
277 }).always(function () {
278 $panel.data('tptn-loading', false).removeAttr('aria-busy');
279 });
280 }
281
282 $tabsContainer.tabs({
283 create: function (event, ui) {
284 $(ui.tab.find("a")).addClass("nav-tab-active");
285 },
286 activate: function (event, ui) {
287 $(ui.oldTab.find("a")).removeClass("nav-tab-active");
288 $(ui.newTab.find("a")).addClass("nav-tab-active");
289 loadDashboardTab(ui.newPanel);
290 }
291 });
292
293 // Wait for the tabs to be fully initialized
294 setTimeout(function () {
295 // Select Today tab (second tab) by default and add the nav-tab-active class
296 var $tabs = $tabsContainer.tabs();
297
298 // First find the Today tab index (usually 1)
299 var tabIndex = 0;
300 $tabsContainer.find(".nav-tab-wrapper li a").each(function (index) {
301 if ($(this).text().indexOf('Today') >= 0) {
302 tabIndex = index;
303 return false; // Break the loop
304 }
305 });
306
307 // Set the active tab
308 $tabs.tabs("option", "active", tabIndex);
309
310 // Manually add the active class to the Today tab
311 $tabsContainer.find(".nav-tab-wrapper li a").removeClass("nav-tab-active");
312 $tabsContainer.find(".nav-tab-wrapper li").eq(tabIndex).find("a").addClass("nav-tab-active");
313 }, 10);
314 });
315
316 // Datepicker.
317 $(function () {
318 var dateFormat = 'dd M yy',
319 from = $("#datepicker-from")
320 .datepicker({
321 changeMonth: true,
322 maxDate: 0,
323 dateFormat: dateFormat
324 })
325 .on("change", function () {
326 to.datepicker("option", "minDate", getDate(this));
327 }),
328 to = $("#datepicker-to")
329 .datepicker({
330 changeMonth: true,
331 maxDate: 0,
332 dateFormat: dateFormat
333 })
334 .on("change", function () {
335 from.datepicker("option", "maxDate", getDate(this));
336 });
337
338 function getDate(element) {
339 var date;
340 try {
341 date = $.datepicker.parseDate(dateFormat, element.value);
342 } catch (error) {
343 date = null;
344 }
345
346 return date;
347 }
348 });
349
350 // Editable table code.
351 $('.live_edit').on('click', function () {
352 $(this)
353 .addClass('live_edit_mode')
354 .removeClass('live_edit_mode_success live_edit_mode_error');
355 });
356
357 $(".live_edit").on('focusout keypress', function (e) {
358 if (e.type !== "focusout" && e.which !== 13) {
359 return;
360 }
361 if (e.which == 13) {
362 e.preventDefault();
363 }
364 var $element = $(this);
365 var post_id = $element.attr('data-wp-post-id');
366 var blog_id = $element.attr('data-wp-blog-id');
367 var count = $element.attr('data-wp-count');
368 var value = $element.text();
369
370 $element.removeClass("live_edit_mode");
371
372 var ajaxData = {
373 action: 'tptn_edit_count_ajax',
374 post_id: post_id,
375 total_count: value,
376 total_count_original: count,
377 top_ten_admin_nonce: top_ten_admin.nonce
378 };
379
380 // Include blog_id if available (network mode).
381 if (blog_id) {
382 ajaxData.blog_id = blog_id;
383 }
384
385 $.ajax({
386 type: 'POST',
387 dataType: 'json',
388 url: ajaxurl,
389 data: ajaxData,
390 success: function (response) {
391 if (response === false) {
392 $element.addClass("live_edit_mode_error");
393 $element.html(count);
394 } else if (response > 0) {
395 $element.addClass("live_edit_mode_success");
396 }
397 },
398 });
399 });
400 });
401