|
1 | 1 | /**
|
2 |
| - * Call the plugin with ajax |
3 |
| - * |
| 2 | + * Add a click handler to the |
| 3 | + * element with the `plugin_stale` class |
4 | 4 | */
|
5 | 5 |
|
6 |
| -jQuery('.plugin_stale') |
7 |
| - .show() |
8 |
| - .click(function (e) { |
9 |
| - e.preventDefault(); |
10 |
| - |
11 |
| - // post the data |
12 |
| - jQuery.post( |
13 |
| - DOKU_BASE + 'lib/exe/ajax.php', |
14 |
| - { |
15 |
| - call: 'plugin_stale' |
16 |
| - }, |
17 |
| - // Display feedback |
18 |
| - // https://api.jqueryui.com/dialog/ |
19 |
| - function (result) { |
20 |
| - let dialogElement = jQuery(document.createElement('div')); |
21 |
| - dialogElement.html(result.message); |
22 |
| - |
23 |
| - /** |
24 |
| - * |
25 |
| - * @param {any | JQuery} jElement |
26 |
| - */ |
27 |
| - let remove = function (jElement) { |
28 |
| - if (jElement.parent().length > 0) { |
29 |
| - jElement.dialog('close'); |
30 |
| - jElement.remove(); |
31 |
| - jQuery(document.activeElement).blur(); |
32 |
| - } |
33 |
| - }; |
34 |
| - |
35 |
| - dialogElement.dialog({ |
36 |
| - dialogClass: "stale-dialog", |
37 |
| - closeOnEscape: true, |
38 |
| - modal: true, |
39 |
| - open: function () { |
40 |
| - // close it after 2 seconds (toast) |
41 |
| - setTimeout(function () { |
42 |
| - remove(dialogElement) |
43 |
| - }, 2000); |
44 |
| - } |
45 |
| - }); |
46 |
| - |
47 |
| - // Close it if the user click |
48 |
| - jQuery(document).on("click", function () { |
49 |
| - remove(dialogElement) |
50 |
| - }); |
51 |
| - |
52 |
| - } |
53 |
| - ); |
54 |
| - |
55 |
| - }); |
| 6 | +(function IIFE(){ |
| 7 | + const queryParameterKey = "stale"; |
| 8 | + jQuery('.plugin_stale') |
| 9 | + .show() |
| 10 | + .click(function (e) { |
| 11 | + e.preventDefault(); |
| 12 | + // post the data |
| 13 | + jQuery.post( |
| 14 | + DOKU_BASE + 'lib/exe/ajax.php', |
| 15 | + { |
| 16 | + call: 'plugin_stale' |
| 17 | + }, |
| 18 | + // Display feedback |
| 19 | + // https://api.jqueryui.com/dialog/ |
| 20 | + function (result) { |
| 21 | + const actualUrl = new URL(window.location.href); |
| 22 | + actualUrl.searchParams.set(queryParameterKey,result.message); |
| 23 | + window.location.assign(actualUrl.toString()); |
| 24 | + } |
| 25 | + ); |
| 26 | + }); |
| 27 | + jQuery(function(){ |
| 28 | + |
| 29 | + const actualUrl = new URL(window.location.href); |
| 30 | + if(actualUrl.searchParams.has(queryParameterKey)){ |
| 31 | + |
| 32 | + const sec = 10; |
| 33 | + const message = actualUrl.searchParams.get(queryParameterKey); |
| 34 | + let dialogElement = jQuery(document.createElement('div')); |
| 35 | + dialogElement.html(`<h3>Stale</h3><p>${message}</p><p>The page was reloaded.</p><p>Note: This window will close automatically after ${sec} second if you don't click or press any key.</p>`); |
| 36 | + |
| 37 | + /** |
| 38 | + * |
| 39 | + * @param {any | JQuery} jElement |
| 40 | + */ |
| 41 | + let remove = function (jElement) { |
| 42 | + if (jElement.parent().length > 0) { |
| 43 | + jElement.dialog('close'); |
| 44 | + jElement.remove(); |
| 45 | + jQuery(document.activeElement).blur(); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + dialogElement.dialog({ |
| 50 | + dialogClass: "stale-dialog", |
| 51 | + closeOnEscape: true, |
| 52 | + modal: true, |
| 53 | + open: function () { |
| 54 | + // close it after 2 seconds (toast) |
| 55 | + setTimeout(function () { |
| 56 | + remove(dialogElement) |
| 57 | + }, sec*1000); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + // Close it if the user click or press any key |
| 62 | + jQuery(document).on("click", function () { |
| 63 | + remove(dialogElement) |
| 64 | + }); |
| 65 | + jQuery(document).on("keypress", function () { |
| 66 | + remove(dialogElement) |
| 67 | + }); |
| 68 | + |
| 69 | + } |
| 70 | + }) |
| 71 | +})(); |
| 72 | + |
56 | 73 |
|
0 commit comments