← All changes
|
includes/extensions/Site_Preloader/assets/script.js
+46
-16
51.1.44
→
51.1.87
View file →
| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Site Preloader Frontend Script. |
| 3 | 3 | * |
| 4 | 4 | * Handles preloader initialization, hide strategies, and AJAX navigation support. |
| 5 | - * Apple-inspired smooth interactions. | |
| 5 | + * Premium style inspired smooth interactions. | |
| 6 | 6 | * |
| 7 | 7 | * @package King_Addons |
| 8 | 8 | * @since 1.0.0 |
| 9 | 9 | */ |
| @@ -25,8 +25,9 @@ | ||
| 25 | 25 | maxDisplayTime: 10000, |
| 26 | 26 | hideAnimation: 'fade', |
| 27 | 27 | animationDuration: 400, |
| 28 | 28 | triggerType: 'always', |
| 29 | + cookieName: 'kng_preloader_shown', | |
| 29 | 30 | enableAjax: false, |
| 30 | 31 | allowSkip: false, |
| 31 | 32 | skipMethod: 'click', |
| 32 | 33 | lockScroll: true, |
| @@ -119,8 +120,11 @@ | ||
| 119 | 120 | } |
| 120 | 121 | if (dataset.triggerType) { |
| 121 | 122 | this.config.triggerType = dataset.triggerType; |
| 122 | 123 | } |
| 124 | + if (dataset.cookieName) { | |
| 125 | + this.config.cookieName = dataset.cookieName; | |
| 126 | + } | |
| 123 | 127 | if (dataset.enableAjax) { |
| 124 | 128 | this.config.enableAjax = dataset.enableAjax === 'true'; |
| 125 | 129 | } |
| 126 | 130 | if (dataset.allowSkip) { |
| @@ -134,8 +138,23 @@ | ||
| 134 | 138 | } |
| 135 | 139 | }, |
| 136 | 140 | |
| 137 | 141 | /** |
| 142 | + * Set a cookie with optional expiry days. | |
| 143 | + */ | |
| 144 | + setCookie: function (name, value, days) { | |
| 145 | + let expires = ''; | |
| 146 | + if (typeof days === 'number') { | |
| 147 | + const date = new Date(); | |
| 148 | + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
| 149 | + expires = '; expires=' + date.toUTCString(); | |
| 150 | + } | |
| 151 | + | |
| 152 | + const secure = (window.location && window.location.protocol === 'https:') ? '; Secure' : ''; | |
| 153 | + document.cookie = name + '=' + encodeURIComponent(String(value)) + expires + '; path=/; SameSite=Lax' + secure; | |
| 154 | + }, | |
| 155 | + | |
| 156 | + /** | |
| 138 | 157 | * Set up hide strategy based on configuration |
| 139 | 158 | */ |
| 140 | 159 | setupHideStrategy: function () { |
| 141 | 160 | switch (this.config.hideStrategy) { |
| @@ -175,9 +194,9 @@ | ||
| 175 | 194 | */ |
| 176 | 195 | setupMaxTimeout: function () { |
| 177 | 196 | this.state.maxTimer = setTimeout(() => { |
| 178 | 197 | if (this.state.isShowing) { |
| 179 | - console.warn('[KngPreloader] Max display time reached, forcing hide'); | |
| 198 | + // console.warn('[KngPreloader] Max display time reached, forcing hide'); | |
| 180 | 199 | this.hide(); |
| 181 | 200 | } |
| 182 | 201 | }, this.config.maxDisplayTime); |
| 183 | 202 | }, |
| @@ -320,26 +339,36 @@ | ||
| 320 | 339 | /** |
| 321 | 340 | * Set cookie based on trigger type |
| 322 | 341 | */ |
| 323 | 342 | setTriggerCookie: function () { |
| 324 | - if (this.config.triggerType === 'always') { | |
| 343 | + const triggerType = this.config.triggerType || 'always'; | |
| 344 | + if (triggerType === 'always') { | |
| 325 | 345 | return; |
| 326 | 346 | } |
| 327 | 347 | |
| 328 | - const cookieName = 'kng_preloader_shown'; | |
| 329 | - let expires = ''; | |
| 348 | + const cookieName = this.config.cookieName || 'kng_preloader_shown'; | |
| 349 | + const nowSeconds = Math.floor(Date.now() / 1000); | |
| 330 | 350 | |
| 331 | - if (this.config.triggerType === 'once') { | |
| 332 | - // Set cookie for specified days | |
| 333 | - const date = new Date(); | |
| 334 | - date.setTime(date.getTime() + (this.config.cookieDays * 24 * 60 * 60 * 1000)); | |
| 335 | - expires = '; expires=' + date.toUTCString(); | |
| 336 | - } else if (this.config.triggerType === 'session') { | |
| 337 | - // Session cookie (no expires = session only) | |
| 338 | - expires = ''; | |
| 351 | + switch (triggerType) { | |
| 352 | + case 'first_visit': | |
| 353 | + // Persist "forever" (10 years) to represent first visit. | |
| 354 | + this.setCookie(cookieName, nowSeconds, 3650); | |
| 355 | + break; | |
| 356 | + | |
| 357 | + case 'once_per_session': | |
| 358 | + // Session cookie (no expiry). | |
| 359 | + this.setCookie(cookieName, '1'); | |
| 360 | + break; | |
| 361 | + | |
| 362 | + case 'once_per_day': | |
| 363 | + // Store a unix timestamp (seconds). Expiry slightly > 1 day. | |
| 364 | + this.setCookie(cookieName, nowSeconds, 2); | |
| 365 | + break; | |
| 366 | + | |
| 367 | + default: | |
| 368 | + // Unknown trigger type: do nothing. | |
| 369 | + break; | |
| 339 | 370 | } |
| 340 | - | |
| 341 | - document.cookie = cookieName + '=1' + expires + '; path=/; SameSite=Lax'; | |
| 342 | 371 | }, |
| 343 | 372 | |
| 344 | 373 | /** |
| 345 | 374 | * Check if preloader should be shown based on cookie |
| @@ -348,12 +377,13 @@ | ||
| 348 | 377 | if (this.config.triggerType === 'always') { |
| 349 | 378 | return true; |
| 350 | 379 | } |
| 351 | 380 | |
| 381 | + const cookieName = (this.config.cookieName || 'kng_preloader_shown') + '='; | |
| 352 | 382 | const cookies = document.cookie.split(';'); |
| 353 | 383 | for (let i = 0; i < cookies.length; i++) { |
| 354 | 384 | const cookie = cookies[i].trim(); |
| 355 | - if (cookie.startsWith('kng_preloader_shown=')) { | |
| 385 | + if (cookie.startsWith(cookieName)) { | |
| 356 | 386 | return false; |
| 357 | 387 | } |
| 358 | 388 | } |
| 359 | 389 | return true; |