PluginProbe
WebberZone Top 10 — Popular Posts / 4.4.0
WebberZone Top 10 — Popular Posts v4.4.0
4.5.1 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 All 117 releases
top-10 / includes / admin / js / admin-scripts.js

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

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