1function randomId(prefix = 'dialog') { 2 return `${prefix}-${Math.random().toString(36).slice(2, 10)}`; 3} 4 5const Dialog = (() => { 6 let zIndex = 1050; 7 8 return { 9 show({ 10 title = '', 11 message = '', 12 type = 'info', 13 buttons = [], 14 backdropClose = true, 15 keyboard = true, 16 closable = true, 17 onLoad = null 18 }) { 19 return new Promise(resolve => { 20 zIndex++; 21 22 const overlay = document.createElement('div'); 23 overlay.id = randomId('dialog-overlay'); 24 overlay.className = 'dialog-overlay'; 25 overlay.style.zIndex = zIndex; 26 27 const dialog = document.createElement('div'); 28 dialog.className = `dialog dialog-${type}`; 29 30 dialog.innerHTML = ` 31 ${title ? ` 32 <div class="dialog-header"> 33 <div class="dialog-title">${title}</div> 34 ${closable ? `<button class="dialog-close" aria-label="Cerrar">Ã</button>` : ''} 35 </div>` : ''} 36 <div class="dialog-body">${message}</div> 37 <div class="dialog-footer"></div> 38 `; 39 40 overlay.appendChild(dialog); 41 document.body.appendChild(overlay); 42 43 const api = { 44 overlay, 45 close(value) { 46 cleanup(); 47 overlay.remove(); 48 resolve(value); 49 } 50 }; 51 52 // ð callback al cargar 53 if (typeof onLoad === 'function') { 54 onLoad({ dialog, overlay, api }); 55 } 56 57 const footer = dialog.querySelector('.dialog-footer'); 58 const closeBtn = dialog.querySelector('.dialog-close'); 59 60 if (closeBtn) { 61 closeBtn.onclick = () => api.close(false); 62 } 63 64 buttons.forEach(btn => { 65 const b = document.createElement('button'); 66 b.textContent = btn.label; 67 b.className = btn.class || 'btn-primary'; 68 b.onclick = () => btn.action(api); 69 footer.appendChild(b); 70 }); 71 72 overlay.onclick = e => { 73 if (e.target === overlay && backdropClose === true) { 74 api.close(false); 75 } 76 }; 77 78 function onKey(e) { 79 if (e.key === 'Escape' && keyboard && backdropClose !== 'static') { 80 api.close(false); 81 } 82 } 83 84 function cleanup() { 85 document.removeEventListener('keydown', onKey); 86 } 87 88 document.addEventListener('keydown', onKey); 89 }); 90 } 91 }; 92})();
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.