← All changes
|
includes/extensions/Maintenance_Mode/assets/admin.js
+115
-0
51.1.44
→
51.1.87
View file →
| @@ -106,6 +106,121 @@ | ||
| 106 | 106 | setThemeMode(isDark ? 'light' : 'dark', true); |
| 107 | 107 | }; |
| 108 | 108 | |
| 109 | 109 | setThemeMode(mode, false); |
| 110 | + | |
| 111 | + function nextIndex($container) { | |
| 112 | + var max = -1; | |
| 113 | + $container.find('.kng-repeat-row').each(function() { | |
| 114 | + var idx = parseInt($(this).attr('data-index'), 10); | |
| 115 | + if (!isNaN(idx)) { | |
| 116 | + max = Math.max(max, idx); | |
| 117 | + } | |
| 118 | + }); | |
| 119 | + return max + 1; | |
| 120 | + } | |
| 121 | + | |
| 122 | + function addRowFromTemplate(templateId, $target) { | |
| 123 | + var tmpl = document.getElementById(templateId); | |
| 124 | + if (!tmpl || !tmpl.content) { | |
| 125 | + return; | |
| 126 | + } | |
| 127 | + var idx = nextIndex($target); | |
| 128 | + var html = tmpl.innerHTML.replace(/__INDEX__/g, String(idx)); | |
| 129 | + $target.append($(html)); | |
| 130 | + } | |
| 131 | + | |
| 132 | + $(document).on('click', '#kng-add-window', function(e) { | |
| 133 | + e.preventDefault(); | |
| 134 | + addRowFromTemplate('kng-maintenance-window-template', $('#kng-maintenance-windows')); | |
| 135 | + }); | |
| 136 | + | |
| 137 | + $(document).on('click', '#kng-add-recurring', function(e) { | |
| 138 | + e.preventDefault(); | |
| 139 | + addRowFromTemplate('kng-maintenance-recurring-template', $('#kng-maintenance-recurring')); | |
| 140 | + }); | |
| 141 | + | |
| 142 | + $(document).on('click', '.kng-remove-row', function(e) { | |
| 143 | + e.preventDefault(); | |
| 144 | + var $row = $(this).closest('.kng-repeat-row'); | |
| 145 | + $row.remove(); | |
| 146 | + }); | |
| 147 | + | |
| 148 | + function updateRecurringRow($row) { | |
| 149 | + var freq = ($row.find('.kng-recurring-frequency').val() || 'daily').toString(); | |
| 150 | + $row.find('.kng-recurring-weekly').toggle(freq === 'weekly'); | |
| 151 | + $row.find('.kng-recurring-monthly').toggle(freq === 'monthly'); | |
| 152 | + } | |
| 153 | + | |
| 154 | + $(document).on('change', '.kng-recurring-frequency', function() { | |
| 155 | + updateRecurringRow($(this).closest('.kng-repeat-row')); | |
| 156 | + }); | |
| 157 | + | |
| 158 | + $('.kng-repeat-row').each(function() { | |
| 159 | + updateRecurringRow($(this)); | |
| 160 | + }); | |
| 161 | + | |
| 162 | + var copyTimer = null; | |
| 163 | + | |
| 164 | + function showCopyStatus($status, text) { | |
| 165 | + if (!$status || !$status.length) { | |
| 166 | + return; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $status.text(text || 'Copied'); | |
| 170 | + $status.addClass('is-visible'); | |
| 171 | + | |
| 172 | + if (copyTimer) { | |
| 173 | + window.clearTimeout(copyTimer); | |
| 174 | + } | |
| 175 | + | |
| 176 | + copyTimer = window.setTimeout(function() { | |
| 177 | + $status.removeClass('is-visible'); | |
| 178 | + }, 1400); | |
| 179 | + } | |
| 180 | + | |
| 181 | + async function copyToClipboard(text) { | |
| 182 | + if (!text) { | |
| 183 | + return false; | |
| 184 | + } | |
| 185 | + | |
| 186 | + try { | |
| 187 | + if (navigator.clipboard && navigator.clipboard.writeText) { | |
| 188 | + await navigator.clipboard.writeText(text); | |
| 189 | + return true; | |
| 190 | + } | |
| 191 | + } catch (e) { | |
| 192 | + // fall back below | |
| 193 | + } | |
| 194 | + | |
| 195 | + try { | |
| 196 | + var $tmp = $('<textarea readonly></textarea>') | |
| 197 | + .css({ position: 'absolute', left: '-9999px', top: '0', opacity: '0' }) | |
| 198 | + .val(text) | |
| 199 | + .appendTo('body'); | |
| 200 | + $tmp[0].select(); | |
| 201 | + $tmp[0].setSelectionRange(0, text.length); | |
| 202 | + var ok = document.execCommand('copy'); | |
| 203 | + $tmp.remove(); | |
| 204 | + return !!ok; | |
| 205 | + } catch (err) { | |
| 206 | + return false; | |
| 207 | + } | |
| 208 | + } | |
| 209 | + | |
| 210 | + $(document).on('click', '.kng-maintenance-copy-token', function(e) { | |
| 211 | + e.preventDefault(); | |
| 212 | + var $row = $(this).closest('.kng-maintenance-token-row'); | |
| 213 | + var $input = $row.find('.kng-maintenance-token-input').first(); | |
| 214 | + var $status = $row.find('.kng-maintenance-copy-status').first(); | |
| 215 | + var val = ($input.val() || '').toString(); | |
| 216 | + | |
| 217 | + copyToClipboard(val).then(function(ok) { | |
| 218 | + if (ok) { | |
| 219 | + showCopyStatus($status, 'Copied'); | |
| 220 | + } else { | |
| 221 | + showCopyStatus($status, 'Copy failed'); | |
| 222 | + } | |
| 223 | + }); | |
| 224 | + }); | |
| 110 | 225 | }); |
| 111 | 226 | })(jQuery); |