|
1 |
| -/* global cachify_admin_bar_flush_ajax_object */ |
2 |
| -( function() { |
3 |
| - var is_flushing = false, |
4 |
| - admin_bar_cachify_list_item = document.getElementById( 'wp-admin-bar-cachify' ), |
5 |
| - flush_link = admin_bar_cachify_list_item.querySelector( 'a.ab-item' ), |
6 |
| - fallback_url = flush_link.getAttribute( 'href' ), |
7 |
| - aria_live_area = document.querySelector( '.ab-aria-live-area' ); |
| 1 | +/* global cachifyAdminBarFlushAjaxObject */ |
| 2 | +{ |
| 3 | + let isFlushing = false; |
| 4 | + const adminBarItem = document.getElementById('wp-admin-bar-cachify'); |
| 5 | + const flushLink = adminBarItem.querySelector('a.ab-item'); |
| 6 | + const fallbackUrl = flushLink.getAttribute('href'); |
| 7 | + const ariaLiveArea = document.querySelector('.ab-aria-live-area'); |
8 | 8 |
|
9 | 9 | // Replacing flush link with button because with AJAX action, it is semantically not a link anymore.
|
10 |
| - var button = document.createRange().createContextualFragment( '<button class="ab-item">' + flush_link.innerHTML + '</button>' ); |
11 |
| - flush_link.parentNode.replaceChild( button, flush_link ); |
| 10 | + const button = document |
| 11 | + .createRange() |
| 12 | + .createContextualFragment( |
| 13 | + '<button class="ab-item">' + flushLink.innerHTML + '</button>' |
| 14 | + ); |
| 15 | + flushLink.parentNode.replaceChild(button, flushLink); |
12 | 16 |
|
13 |
| - var admin_bar_icon = admin_bar_cachify_list_item.querySelector( '#wp-admin-bar-cachify .ab-icon' ); |
| 17 | + const adminBarIcon = adminBarItem.querySelector( |
| 18 | + '#wp-admin-bar-cachify .ab-icon' |
| 19 | + ); |
14 | 20 |
|
15 |
| - document.querySelector( '#wp-admin-bar-cachify .ab-item' ).addEventListener( 'click', flush ); |
| 21 | + document |
| 22 | + .querySelector('#wp-admin-bar-cachify .ab-item') |
| 23 | + .addEventListener('click', flush); |
16 | 24 |
|
17 |
| - admin_bar_icon.addEventListener( 'animationend', function() { |
18 |
| - admin_bar_icon.classList.remove( 'animate-fade' ); |
19 |
| - } ); |
| 25 | + adminBarIcon.addEventListener('animationend', function () { |
| 26 | + adminBarIcon.classList.remove('animate-fade'); |
| 27 | + }); |
20 | 28 |
|
21 |
| - function flush_icon_remove_classes() { |
22 |
| - var classes = [ |
| 29 | + /** |
| 30 | + * Remove classes used for animation from flush icon. |
| 31 | + */ |
| 32 | + function flushIconRemoveClasses() { |
| 33 | + const classes = [ |
23 | 34 | 'animate-fade',
|
24 | 35 | 'animate-pulse',
|
25 | 36 | 'dashicons-trash',
|
|
28 | 39 | 'dashicons-dismiss',
|
29 | 40 | ];
|
30 | 41 |
|
31 |
| - for ( var i = 0; i < classes.length; i++ ) { |
32 |
| - admin_bar_icon.classList.remove( classes[i] ); |
| 42 | + for (const element of classes) { |
| 43 | + adminBarIcon.classList.remove(element); |
33 | 44 | }
|
34 | 45 | }
|
35 | 46 |
|
36 |
| - function start_flush_icon_reset_timeout() { |
37 |
| - setTimeout( function() { |
38 |
| - flush_icon_remove_classes(); |
39 |
| - admin_bar_icon.classList.add( 'animate-fade' ); |
40 |
| - admin_bar_icon.classList.add( 'dashicons-trash' ); |
41 |
| - is_flushing = false; |
42 |
| - aria_live_area.textContent = ''; |
43 |
| - }, 2000 ); |
| 47 | + /** |
| 48 | + * Add animation classes to flush icon. |
| 49 | + */ |
| 50 | + function startFlushIconResetTimeout() { |
| 51 | + setTimeout(() => { |
| 52 | + flushIconRemoveClasses(); |
| 53 | + adminBarIcon.classList.add('animate-fade', 'dashicons-trash'); |
| 54 | + isFlushing = false; |
| 55 | + ariaLiveArea.textContent = ''; |
| 56 | + }, 2000); |
44 | 57 | }
|
45 | 58 |
|
46 |
| - function flush( event ) { |
| 59 | + /** |
| 60 | + * Event listener to flush the cache. |
| 61 | + * |
| 62 | + * @param {MouseEvent} event Click event |
| 63 | + */ |
| 64 | + function flush(event) { |
47 | 65 | event.preventDefault();
|
48 | 66 |
|
49 |
| - if ( is_flushing ) { |
| 67 | + if (isFlushing) { |
50 | 68 | return;
|
51 | 69 | }
|
52 |
| - is_flushing = true; |
53 |
| - aria_live_area.textContent = cachify_admin_bar_flush_ajax_object.flushing; |
| 70 | + isFlushing = true; |
| 71 | + ariaLiveArea.textContent = cachifyAdminBarFlushAjaxObject.flushing; |
54 | 72 |
|
55 |
| - if ( admin_bar_icon !== null ) { |
56 |
| - flush_icon_remove_classes(); |
57 |
| - admin_bar_icon.classList.add( 'animate-pulse' ); |
58 |
| - admin_bar_icon.classList.add( 'dashicons-trash' ); |
| 73 | + if (adminBarIcon !== null) { |
| 74 | + flushIconRemoveClasses(); |
| 75 | + adminBarIcon.classList.add('animate-pulse'); |
| 76 | + adminBarIcon.classList.add('dashicons-trash'); |
59 | 77 | }
|
60 | 78 |
|
61 |
| - var request = new XMLHttpRequest(); |
62 |
| - request.addEventListener( 'load', function() { |
63 |
| - if ( this.status === 200 ) { |
64 |
| - start_flush_icon_reset_timeout(); |
65 |
| - flush_icon_remove_classes(); |
66 |
| - admin_bar_icon.classList.add( 'animate-fade' ); |
67 |
| - admin_bar_icon.classList.add( cachify_admin_bar_flush_ajax_object.dashicon_success ); |
68 |
| - aria_live_area.textContent = cachify_admin_bar_flush_ajax_object.flushed; |
69 |
| - return; |
70 |
| - } |
71 |
| - |
72 |
| - window.location = fallback_url; |
73 |
| - } ); |
74 |
| - |
75 |
| - request.addEventListener( 'error', function() { |
76 |
| - window.location = fallback_url; |
77 |
| - } ); |
78 |
| - |
79 |
| - request.open( 'DELETE', cachify_admin_bar_flush_ajax_object.url ); |
80 |
| - request.setRequestHeader( 'X-WP-Nonce', cachify_admin_bar_flush_ajax_object.nonce ); |
81 |
| - request.send(); |
| 79 | + fetch(cachifyAdminBarFlushAjaxObject.url, { |
| 80 | + method: 'DELETE', |
| 81 | + headers: { 'X-WP-Nonce': cachifyAdminBarFlushAjaxObject.nonce }, |
| 82 | + }) |
| 83 | + .then(() => { |
| 84 | + startFlushIconResetTimeout(); |
| 85 | + flushIconRemoveClasses(); |
| 86 | + adminBarIcon.classList.add('animate-fade'); |
| 87 | + adminBarIcon.classList.add( |
| 88 | + cachifyAdminBarFlushAjaxObject.dashiconSuccess |
| 89 | + ); |
| 90 | + ariaLiveArea.textContent = |
| 91 | + cachifyAdminBarFlushAjaxObject.flushed; |
| 92 | + }) |
| 93 | + .catch(() => { |
| 94 | + window.location = fallbackUrl; |
| 95 | + }); |
82 | 96 | }
|
83 |
| -}() ); |
| 97 | +} |
0 commit comments