@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
202 lines (198 loc) • 7.87 kB
JavaScript
;
var dialog_web_CancelButton = require('./web/CancelButton.js');
var dialog_web_Dialog = require('./web/Dialog.js');
var dialog_web_Footer = require('./web/Footer.js');
var dialog_web_Input = require('./web/Input.js');
var dialog_web_OkButton = require('./web/OkButton.js');
var dialog_web_Progress = require('./web/Progress.js');
var dialog_web_Text = require('./web/Text.js');
var object = require('../object.js');
var dialog_web_file = require('./web/file.js');
/**
* The implementation of `dialog` module for the browser.
*
* Normally, we should just use the `dialog` module, however, if we don't want
* to include other parts that are not needed in the browser, we can use this
* module instead.
* @module
*/
async function alert(message, options = {}) {
await new Promise(_resolve => {
const resolve = () => {
timer && clearTimeout(timer);
_resolve();
};
const button = dialog_web_OkButton.default();
const dialog = dialog_web_Dialog.default({
onCancel: () => resolve(),
onOk: () => resolve(),
}, dialog_web_Text.default(message), dialog_web_Footer.default(button));
let remains = (options === null || options === void 0 ? void 0 : options.timeout) ? Math.max(1, Math.round(options.timeout / 1000)) : 0;
const timer = remains
? setInterval(() => {
button.textContent = `${dialog_web_OkButton.getOkText()} (${--remains})`;
if (remains === 0) {
dialog_web_Dialog.closeDialog(dialog, "OK");
}
}, 1000)
: undefined;
if (remains) {
button.textContent = `${dialog_web_OkButton.getOkText()} (${remains})`;
}
document.body.appendChild(dialog);
});
}
async function confirm(message, options = {}) {
return new Promise(_resolve => {
const resolve = (value) => {
timer && clearInterval(timer);
_resolve(value);
};
const cancelButton = dialog_web_CancelButton.default();
const dialog = dialog_web_Dialog.default({
onCancel: () => resolve(false),
onOk: () => resolve(true),
}, dialog_web_Text.default(message), dialog_web_Footer.default(cancelButton, dialog_web_OkButton.default()));
let remains = (options === null || options === void 0 ? void 0 : options.timeout) ? Math.max(1, Math.round(options.timeout / 1000)) : 0;
const timer = remains
? setInterval(() => {
cancelButton.textContent = `${dialog_web_CancelButton.getCancelText()} (${--remains})`;
if (remains === 0) {
dialog_web_Dialog.closeDialog(dialog, "Cancel");
}
}, 1000)
: undefined;
if (remains) {
cancelButton.textContent = `${dialog_web_CancelButton.getCancelText()} (${remains})`;
}
document.body.appendChild(dialog);
});
}
async function prompt(message, options = {}) {
const { type, defaultValue } = options;
return new Promise(_resolve => {
const inputDiv = dialog_web_Input.default({ type, value: defaultValue });
const cancelButton = dialog_web_CancelButton.default();
const okButton = dialog_web_OkButton.default();
const dialog = dialog_web_Dialog.default({
onCancel: () => resolve(null),
onOk: (dialog) => {
const input = dialog.querySelector("input");
resolve(input.value);
},
}, dialog_web_Text.default(message), inputDiv, dialog_web_Footer.default(cancelButton, okButton));
const hasDefaultValue = object.isValid(defaultValue); // in case of `null`
let remains = (options === null || options === void 0 ? void 0 : options.timeout) ? Math.max(1, Math.round(options.timeout / 1000)) : 0;
let timer = remains
? setInterval(() => {
if (hasDefaultValue) {
okButton.textContent = `${dialog_web_OkButton.getOkText()} (${--remains})`;
if (remains === 0) {
dialog_web_Dialog.closeDialog(dialog, "OK");
}
}
else {
cancelButton.textContent = `${dialog_web_CancelButton.getCancelText()} (${--remains})`;
if (remains === 0) {
dialog_web_Dialog.closeDialog(dialog, "Cancel");
}
}
}, 1000)
: undefined;
const resolve = (value) => {
timer && clearInterval(timer);
_resolve(value);
};
if (timer) {
const input = inputDiv.querySelector("input");
input === null || input === void 0 ? void 0 : input.addEventListener("input", () => {
clearInterval(timer);
timer = undefined;
if (hasDefaultValue) {
okButton.textContent = dialog_web_OkButton.getOkText();
}
else {
cancelButton.textContent = dialog_web_CancelButton.getCancelText();
}
});
}
if (remains) {
if (hasDefaultValue) {
okButton.textContent = `${dialog_web_OkButton.getOkText()} (${remains})`;
}
else {
cancelButton.textContent = `${dialog_web_CancelButton.getCancelText()} (${remains})`;
}
}
document.body.appendChild(dialog);
});
}
async function progress(message, fn, onAbort = undefined) {
const ctrl = new AbortController();
const signal = ctrl.signal;
let fallback = null;
const abort = !onAbort ? undefined : async () => {
try {
const result = await onAbort();
fallback = { value: result };
ctrl.abort();
}
catch (err) {
ctrl.abort(err);
}
};
const listenForAbort = !onAbort ? undefined : () => new Promise((resolve, reject) => {
signal.addEventListener("abort", () => {
if (fallback) {
resolve(fallback.value);
}
else {
reject(signal.reason);
}
});
});
const text = dialog_web_Text.default(message);
const { element: progressBar, setValue } = dialog_web_Progress.default();
const dialog = dialog_web_Dialog.default({ onCancel: abort }, text);
const set = (state) => {
if (signal.aborted) {
return;
}
if (state.message) {
text.innerHTML = state.message.replace(/ /g, " ").replace(/\n/g, "<br />");
}
if (state.percent !== undefined) {
setValue(state.percent);
}
};
if (abort) {
dialog.appendChild(dialog_web_Footer.default(progressBar, dialog_web_CancelButton.default()));
}
else {
dialog.appendChild(progressBar);
}
document.body.appendChild(dialog);
let job = fn(set, signal);
if (listenForAbort) {
job = Promise.race([job, listenForAbort()]);
}
try {
return await job;
}
finally {
signal.aborted || dialog_web_Dialog.closeDialog(dialog, "OK");
}
}
exports.downloadFile = dialog_web_file.downloadFile;
exports.openDirectory = dialog_web_file.openDirectory;
exports.openFile = dialog_web_file.openFile;
exports.openFiles = dialog_web_file.openFiles;
exports.pickDirectory = dialog_web_file.pickDirectory;
exports.pickFile = dialog_web_file.pickFile;
exports.pickFiles = dialog_web_file.pickFiles;
exports.saveFile = dialog_web_file.saveFile;
exports.alert = alert;
exports.confirm = confirm;
exports.progress = progress;
exports.prompt = prompt;
//# sourceMappingURL=web.js.map