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