function update
This commit is contained in:
+66
-1
@@ -4,6 +4,7 @@
|
||||
var el = {};
|
||||
var _resolve = null;
|
||||
var _busy = false;
|
||||
var _mode = 'confirm'; // 'confirm' | 'prompt'
|
||||
|
||||
function build() {
|
||||
el.overlay = document.createElement('div');
|
||||
@@ -25,6 +26,20 @@
|
||||
el.msg = document.createElement('p');
|
||||
el.msg.style.cssText = 'margin:0 0 20px;color:#c9d1d9;font-size:14px;line-height:1.6;white-space:pre-wrap';
|
||||
|
||||
// Prompt input (hidden in confirm mode)
|
||||
el.inputWrap = document.createElement('div');
|
||||
el.inputWrap.style.cssText = 'margin-bottom:12px;display:none';
|
||||
|
||||
el.input = document.createElement('input');
|
||||
el.input.type = 'text';
|
||||
el.input.className = 'field-input';
|
||||
el.input.style.cssText = 'width:100%;box-sizing:border-box;font-family:"JetBrains Mono",monospace;font-size:13px';
|
||||
el.inputWrap.appendChild(el.input);
|
||||
|
||||
el.inputErr = document.createElement('div');
|
||||
el.inputErr.style.cssText = 'margin-top:6px;font-size:12px;color:#ff3b3b;display:none';
|
||||
el.inputWrap.appendChild(el.inputErr);
|
||||
|
||||
el.row = document.createElement('div');
|
||||
el.row.style.cssText = 'display:flex;gap:8px;justify-content:flex-end';
|
||||
|
||||
@@ -38,12 +53,14 @@
|
||||
el.row.appendChild(el.cancelBtn);
|
||||
el.row.appendChild(el.confirmBtn);
|
||||
el.box.appendChild(el.msg);
|
||||
el.box.appendChild(el.inputWrap);
|
||||
el.box.appendChild(el.row);
|
||||
el.overlay.appendChild(el.box);
|
||||
document.body.appendChild(el.overlay);
|
||||
|
||||
el.cancelBtn.addEventListener('click', function () { close(false); });
|
||||
el.confirmBtn.addEventListener('click', function () { close(true); });
|
||||
el.confirmBtn.addEventListener('click', function () { submitModal(); });
|
||||
el.input.addEventListener('keydown', function (e) { if (e.key === 'Enter') submitModal(); });
|
||||
el.overlay.addEventListener('click', function (e) {
|
||||
if (e.target === el.overlay) close(false);
|
||||
});
|
||||
@@ -52,17 +69,61 @@
|
||||
});
|
||||
}
|
||||
|
||||
var _promptPattern = null;
|
||||
|
||||
function openModal(msg, confirmLabel, isDanger, showCancel) {
|
||||
if (!el.overlay) build();
|
||||
_mode = 'confirm';
|
||||
_promptPattern = null;
|
||||
el.msg.textContent = msg;
|
||||
el.confirmBtn.textContent = confirmLabel || 'OK';
|
||||
el.confirmBtn.className = isDanger !== false ? 'btn-danger' : 'btn-primary';
|
||||
el.cancelBtn.style.display = showCancel === false ? 'none' : '';
|
||||
el.inputWrap.style.display = 'none';
|
||||
el.overlay.style.display = 'flex';
|
||||
el.confirmBtn.focus();
|
||||
return new Promise(function (res) { _resolve = res; });
|
||||
}
|
||||
|
||||
function openPrompt(msg, placeholder, pattern, confirmLabel) {
|
||||
if (!el.overlay) build();
|
||||
_mode = 'prompt';
|
||||
_promptPattern = pattern || null;
|
||||
el.msg.textContent = msg;
|
||||
el.confirmBtn.textContent = confirmLabel || 'Install';
|
||||
el.confirmBtn.className = 'btn-primary';
|
||||
el.cancelBtn.style.display = '';
|
||||
el.input.value = '';
|
||||
el.input.placeholder = placeholder || '';
|
||||
el.inputErr.style.display = 'none';
|
||||
el.inputErr.textContent = '';
|
||||
el.inputWrap.style.display = 'block';
|
||||
el.overlay.style.display = 'flex';
|
||||
setTimeout(function () { el.input.focus(); }, 50);
|
||||
return new Promise(function (res) { _resolve = res; });
|
||||
}
|
||||
|
||||
function submitModal() {
|
||||
if (_mode === 'prompt') {
|
||||
var val = el.input.value.trim();
|
||||
if (!val) {
|
||||
el.inputErr.textContent = 'Please enter a value.';
|
||||
el.inputErr.style.display = 'block';
|
||||
el.input.focus();
|
||||
return;
|
||||
}
|
||||
if (_promptPattern && !_promptPattern.test(val)) {
|
||||
el.inputErr.textContent = 'Invalid format. Use: author/item-name (e.g. crowdsecurity/ssh-bf)';
|
||||
el.inputErr.style.display = 'block';
|
||||
el.input.focus();
|
||||
return;
|
||||
}
|
||||
close(val);
|
||||
} else {
|
||||
close(true);
|
||||
}
|
||||
}
|
||||
|
||||
function close(result) {
|
||||
if (el.overlay) el.overlay.style.display = 'none';
|
||||
if (_resolve) { _resolve(result); _resolve = null; }
|
||||
@@ -76,6 +137,10 @@
|
||||
// Info/alert dialog — returns Promise<void>
|
||||
info: function (msg) {
|
||||
return openModal(msg, 'OK', false, false);
|
||||
},
|
||||
// Text input dialog — returns Promise<string|null>
|
||||
prompt: function (msg, placeholder, pattern, confirmLabel) {
|
||||
return openPrompt(msg, placeholder, pattern, confirmLabel);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user