PageSourceSearch

https://yoby.fun/assets/js/yoby-confirm.js?v=NJrPt0zsdf3KvogO

js yoby.fun collected 2026-10-04 08:01:04 UTC 9,336 bytes, 212 lines download raw bytes

1/* Lightbox confirm / alert / prompt — sito e dashboard (niente dialog di sistema). */
2(function (global) {
3    var STYLE_ID = 'yoby-confirm-style';
4    var ROOT_ID = 'yoby-confirm-root';
5
6    function ensureStyle() {
7        if (document.getElementById(STYLE_ID)) return;
8        var css = document.createElement('style');
9        css.id = STYLE_ID;
10        css.textContent = [
11            '#' + ROOT_ID + '{position:fixed;inset:0;z-index:500000;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,.55);padding:1rem;}',
12            '#' + ROOT_ID + '[hidden]{display:none!important;}',
13            '#' + ROOT_ID + ' .yoby-confirm-panel{background:#1e1e2e;color:#fff;border-radius:16px;padding:1.25rem 1.35rem;max-width:min(92vw,28rem);width:100%;box-shadow:0 8px 32px rgba(0,0,0,.4);}',
14            '#' + ROOT_ID + ' .yoby-confirm-title{margin:0 0 .5rem;font-size:1.1rem;font-weight:650;}',
15            '#' + ROOT_ID + ' .yoby-confirm-msg{margin:0 0 .85rem;font-size:.95rem;line-height:1.45;opacity:.92;white-space:pre-wrap;}',
16            '#' + ROOT_ID + ' .yoby-confirm-input,#' + ROOT_ID + ' .yoby-confirm-copy{width:100%;box-sizing:border-box;margin:0 0 1rem;padding:.55rem .7rem;border-radius:10px;border:1px solid rgba(255,255,255,.18);background:rgba(0,0,0,.25);color:#fff;font:inherit;}',
17            '#' + ROOT_ID + ' .yoby-confirm-input:focus,#' + ROOT_ID + ' .yoby-confirm-copy:focus{outline:2px solid #2a8fc4;outline-offset:1px;}',
18            '#' + ROOT_ID + ' .yoby-confirm-actions{display:flex;flex-wrap:wrap;gap:.55rem;justify-content:flex-end;}',
19            '#' + ROOT_ID + ' .yoby-confirm-actions button{appearance:none;border:0;border-radius:999px;padding:.55rem 1.1rem;font:inherit;font-weight:600;cursor:pointer;}',
20            '#' + ROOT_ID + ' .yoby-confirm-cancel{background:rgba(255,255,255,.12);color:#fff;}',
21            '#' + ROOT_ID + ' .yoby-confirm-ok{background:#2a8fc4;color:#fff;}',
22            '#' + ROOT_ID + ' .yoby-confirm-ok.is-danger{background:#b91c1c;}',
23            '#' + ROOT_ID + ' .yoby-confirm-cancel:hover{background:rgba(255,255,255,.18);}',
24            '#' + ROOT_ID + ' .yoby-confirm-ok:hover{filter:brightness(1.06);}',
25        ].join('');
26        document.head.appendChild(css);
27    }
28
29    function t(key, fallback) {
30        if (global.YOBY_DashI18n && typeof global.YOBY_DashI18n.t === 'function') {
31            var v = global.YOBY_DashI18n.t(key);
32            if (v && v !== key) return v;
33        }
34        if (global.YOBY_i18n && typeof global.YOBY_i18n.t === 'function') {
35            var v2 = global.YOBY_i18n.t(key);
36            if (v2 && v2 !== key) return v2;
37        }
38        return fallback;
39    }
40
41    function closeRoot(root) {
42        if (!root) return;
43        root.hidden = true;
44        root.innerHTML = '';
45        root.setAttribute('aria-hidden', 'true');
46    }
47
48    /**
49     * @param {string} message
50     * @param {object} [opts]
51     * @returns {Promise<boolean|string|null>}
52     */
53    function show(message, opts) {
54        opts = opts || {};
55        ensureStyle();
56        return new Promise(function (resolve) {
57            var existing = document.getElementById(ROOT_ID);
58            if (existing) existing.remove();
59
60            var root = document.createElement('div');
61            root.id = ROOT_ID;
62            root.setAttribute('role', 'dialog');
63            root.setAttribute('aria-modal', 'true');
64            root.setAttribute('aria-hidden', 'false');
65
66            var title = opts.title || (opts.alertOnly
67                ? 'YOBY'
68                : t('dash.common.confirmTitle', 'Conferma'));
69            var okLabel = opts.confirmLabel || (opts.alertOnly
70                ? t('dash.common.ok', 'OK')
71                : t('dash.common.confirm', 'Conferma'));
72            var cancelLabel = opts.cancelLabel || t('dash.common.cancel', 'Annulla');
73
74            root.innerHTML =
75                '<div class="yoby-confirm-panel">' +
76                '<h3 class="yoby-confirm-title"></h3>' +
77                '<p class="yoby-confirm-msg"></p>' +
78                '<div class="yoby-confirm-fields"></div>' +
79                '<div class="yoby-confirm-actions"></div>' +
80                '</div>';
81            root.querySelector('.yoby-confirm-title').textContent = title;
82            root.querySelector('.yoby-confirm-msg').textContent = String(message || '');
83
84            var fields = root.querySelector('.yoby-confirm-fields');
85            var inputEl = null;
86            var copyEl = null;
87            if (opts.copyValue != null && String(opts.copyValue).length) {
88                copyEl = document.createElement('input');
89                copyEl.type = 'text';
90                copyEl.className = 'yoby-confirm-copy';
91                copyEl.readOnly = true;
92                copyEl.value = String(opts.copyValue);
93                fields.appendChild(copyEl);
94            }
95            if (opts.input) {
96                inputEl = document.createElement('input');
97                inputEl.type = 'text';
98                inputEl.className = 'yoby-confirm-input';
99                inputEl.value = opts.inputValue != null ? String(opts.inputValue) : '';
100                if (opts.inputPlaceholder) inputEl.placeholder = String(opts.inputPlaceholder);
101                fields.appendChild(inputEl);
102            }
103
104            var actions = root.querySelector('.yoby-confirm-actions');
105            if (!opts.alertOnly) {
106                var cancelBtn = document.createElement('button');
107                cancelBtn.type = 'button';
108                cancelBtn.className = 'yoby-confirm-cancel';
109                cancelBtn.textContent = cancelLabel;
110                cancelBtn.addEventListener('click', function () {
111                    cleanup(opts.input ? null : false);
112                });
113                actions.appendChild(cancelBtn);
114            }
115            var okBtn = document.createElement('button');
116            okBtn.type = 'button';
117            okBtn.className = 'yoby-confirm-ok' + (opts.danger ? ' is-danger' : '');
118            okBtn.textContent = okLabel;
119            okBtn.addEventListener('click', function () {
120                if (opts.input) cleanup(inputEl ? inputEl.value : '');
121                else cleanup(true);
122            });
123            actions.appendChild(okBtn);
124
125            function onKey(ev) {
126                if (ev.key === 'Escape' && !opts.alertOnly) {
127                    ev.preventDefault();
128                    cleanup(opts.input ? null : false);
129                } else if (ev.key === 'Enter') {
130                    if (opts.input && document.activeElement === inputEl) {
131                        ev.preventDefault();
132                        cleanup(inputEl ? inputEl.value : '');
133                    } else if (!opts.input) {
134                        ev.preventDefault();
135                        cleanup(true);
136                    }
137                }
138            }
139
140            function cleanup(result) {
141                document.removeEventListener('keydown', onKey, true);
142                closeRoot(root);
143                root.remove();
144                resolve(result);
145            }
146
147            root.addEventListener('click', function (ev) {
148                if (ev.target === root && !opts.alertOnly) cleanup(opts.input ? null : false);
149            });
150
151            document.addEventListener('keydown', onKey, true);
152            document.body.appendChild(root);
153            if (inputEl) inputEl.focus();
154            else if (copyEl) {
155                copyEl.focus();
156                try { copyEl.select(); } catch (e) { /* ignore */ }
157            } else {
158                okBtn.focus();
159            }
160        });
161    }
162
163    function confirm(message, opts) {
164        return show(message, Object.assign({}, opts || {}, { alertOnly: false, input: false }));
165    }
166
167    function alert(message, opts) {
168        return show(message, Object.assign({}, opts || {}, { alertOnly: true, input: false }));
169    }
170
171    /** @returns {Promise<string|null>} null se annullato */
172    function prompt(message, defaultValue, opts) {
173        return show(message, Object.assign({}, opts || {}, {
174            alertOnly: false,
175            input: true,
176            inputValue: defaultValue != null ? String(defaultValue) : '',
177        }));
178    }
179
180    function copyText(text) {
181        var value = String(text || '');
182        if (!value) return Promise.reject(new Error('empty'));
183        if (navigator.clipboard && navigator.clipboard.writeText) {
184            return navigator.clipboard.writeText(value);
185        }
186        return new Promise(function (resolve, reject) {
187            try {
188                var ta = document.createElement('textarea');
189                ta.value = value;
190                ta.setAttribute('readonly', '');
191                ta.style.position = 'fixed';
192                ta.style.left = '-9999px';
193                document.body.appendChild(ta);
194                ta.select();
195                var ok = document.execCommand('copy');
196                ta.remove();
197                if (ok) resolve();
198                else reject(new Error('copy failed'));
199            } catch (err) {
200                reject(err);
201            }
202        });
203    }
204
205    global.YOBY_Confirm = {
206        show: show,
207        confirm: confirm,
208        alert: alert,
209        prompt: prompt,
210        copyText: copyText,
211    };
212})(window);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.