2020-10-02 19:26:14 +00:00
|
|
|
import {FocusLayer} from 'app/client/lib/FocusLayer';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {reportError} from 'app/client/models/errors';
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
import {cssInput} from 'app/client/ui/cssInput';
|
2022-06-17 03:43:26 +00:00
|
|
|
import {prepareForTransition, TransitionWatcher} from 'app/client/ui/transitions';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {bigBasicButton, bigPrimaryButton, cssButton} from 'app/client/ui2018/buttons';
|
2022-01-14 02:55:55 +00:00
|
|
|
import {colors, mediaSmall, testId, vars} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {loadingSpinner} from 'app/client/ui2018/loaders';
|
2021-10-12 15:59:12 +00:00
|
|
|
import {waitGrainObs} from 'app/common/gutil';
|
2022-06-03 14:58:07 +00:00
|
|
|
import {Computed, Disposable, dom, DomContents, DomElementArg, input, keyframes,
|
|
|
|
MultiHolder, Observable, styled} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-10-12 15:59:12 +00:00
|
|
|
// IModalControl is passed into the function creating the body of the modal.
|
2020-10-02 15:10:00 +00:00
|
|
|
export interface IModalControl {
|
2021-10-12 15:59:12 +00:00
|
|
|
// Observable for whether there is work in progress that's delaying the closing of the modal. It
|
|
|
|
// is useful for disabling a Save or Close button.
|
|
|
|
workInProgress: Computed<boolean>;
|
|
|
|
|
|
|
|
// Focus the modal dialog.
|
2020-10-02 15:10:00 +00:00
|
|
|
focus(): void;
|
2021-10-12 15:59:12 +00:00
|
|
|
|
|
|
|
// Request to close, without waiting. It's possible for closing to get prevented.
|
|
|
|
close(): void;
|
|
|
|
|
|
|
|
// Returns true if closed, false if closing was prevented.
|
|
|
|
closeAndWait(): Promise<boolean>;
|
|
|
|
|
2022-02-19 09:46:49 +00:00
|
|
|
// Prevents closing, if close has been called and is pending. No-op otherwise.
|
2021-10-12 15:59:12 +00:00
|
|
|
preventClose(): void;
|
|
|
|
|
|
|
|
// Wraps the passed-in function, so that closing is delayed while the function is running. If
|
|
|
|
// {close: true} is passed in, then requests closing of the modal when the function is done.
|
|
|
|
//
|
|
|
|
// With catchErrors set, errors are caught and reported, and prevent the dialog from closing.
|
|
|
|
// Otherwise, only StayOpen exception prevents closing; other errors will be propagated.
|
|
|
|
doWork<Args extends any[]>(
|
|
|
|
func: (...args: Args) => Promise<unknown>,
|
|
|
|
options?: {close?: boolean, catchErrors?: boolean},
|
|
|
|
): (...args: Args) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
2022-04-27 17:46:24 +00:00
|
|
|
export class ModalControl extends Disposable implements IModalControl {
|
2021-10-12 15:59:12 +00:00
|
|
|
private _inProgress = Observable.create<number>(this, 0);
|
|
|
|
private _workInProgress = Computed.create(this, this._inProgress, (use, n) => (n > 0));
|
|
|
|
private _closePromise: Promise<boolean>|undefined;
|
|
|
|
private _shouldClose = false;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private _doClose: () => void,
|
2022-04-27 17:46:24 +00:00
|
|
|
private _doFocus?: () => void,
|
2021-10-12 15:59:12 +00:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
public focus() {
|
2022-04-27 17:46:24 +00:00
|
|
|
this._doFocus?.();
|
2021-10-12 15:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public close(): void {
|
|
|
|
this.closeAndWait().catch(() => {});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async closeAndWait(): Promise<boolean> {
|
|
|
|
return this._closePromise || (this._closePromise = this._doCloseAndWait());
|
|
|
|
}
|
|
|
|
|
|
|
|
public preventClose(): void {
|
|
|
|
this._shouldClose = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get workInProgress() {
|
|
|
|
return this._workInProgress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public doWork<Args extends any[]>(
|
|
|
|
func: (...args: Args) => Promise<unknown>,
|
|
|
|
options: {close?: boolean, catchErrors?: boolean} = {},
|
|
|
|
): (...args: Args) => Promise<void> {
|
|
|
|
return async (...args) => {
|
|
|
|
this._inProgress.set(this._inProgress.get() + 1);
|
|
|
|
const closePromise = options.close ? this.closeAndWait() : null;
|
|
|
|
try {
|
|
|
|
await func(...args);
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof StayOpen) {
|
|
|
|
this.preventClose();
|
|
|
|
} else if (options.catchErrors) {
|
|
|
|
reportError(err);
|
|
|
|
this.preventClose();
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._inProgress.set(this._inProgress.get() - 1);
|
|
|
|
if (closePromise) {
|
|
|
|
await closePromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _doCloseAndWait(): Promise<boolean> {
|
|
|
|
this._shouldClose = true;
|
|
|
|
try {
|
|
|
|
// Since some modals expect an immediate close; avoid an await when no work is pending.
|
|
|
|
if (this.workInProgress.get()) {
|
|
|
|
await waitGrainObs(this.workInProgress, wip => !wip);
|
|
|
|
}
|
|
|
|
if (this._shouldClose) { this._doClose(); }
|
|
|
|
return this._shouldClose;
|
|
|
|
} finally {
|
|
|
|
this._closePromise = undefined;
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 03:43:26 +00:00
|
|
|
/**
|
|
|
|
* The modal variant.
|
|
|
|
*
|
|
|
|
* Fade-in modals open with a fade-in background animation, and close immediately.
|
|
|
|
*
|
|
|
|
* Collapsing modals open with a expanding animation from a referenced DOM element, and
|
|
|
|
* close with a collapsing animation into the referenced element.
|
|
|
|
*/
|
|
|
|
export type IModalVariant = 'fade-in' | 'collapsing';
|
2021-10-12 15:59:12 +00:00
|
|
|
|
2022-06-17 03:43:26 +00:00
|
|
|
export interface IModalOptions {
|
|
|
|
// The modal variant. Defaults to "fade-in".
|
|
|
|
variant?: IModalVariant;
|
|
|
|
// Required for "collapsing" variant modals. This is the anchor element for animations.
|
|
|
|
refElement?: HTMLElement;
|
|
|
|
// If set, escape key does not close the dialog.
|
|
|
|
noEscapeKey?: boolean;
|
|
|
|
// If set, clicking into background does not close dialog.
|
|
|
|
noClickAway?: boolean;
|
2021-10-12 15:59:12 +00:00
|
|
|
// If given, call and wait for this before closing the dialog. If it returns false, don't close.
|
|
|
|
// Error also prevents closing, and is reported as an unexpected error.
|
|
|
|
beforeClose?: () => Promise<boolean>;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A custom error type to signal to the modal that it should stay open, but not report any error
|
|
|
|
// (presumably because the error was already reported).
|
|
|
|
export class StayOpen extends Error {
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ModalWidth =
|
|
|
|
'normal' | // Normal dialog, from 428px to 480px in width.
|
|
|
|
'fixed-wide'; // Fixed 600px width.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple modal. Shows up in the middle of the screen with a tinted backdrop.
|
|
|
|
* Created with the given body content and width.
|
|
|
|
*
|
|
|
|
* Closed via clicking anywhere outside the modal. May also be closed by
|
|
|
|
* calling ctl.close().
|
|
|
|
*
|
|
|
|
* The createFn callback may tie the disposal of temporary objects to its `owner` argument.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* modal((ctl, owner) => [
|
|
|
|
* cssModalTitle(`Pin doc`),
|
|
|
|
* cssModalBody('Are you sure you want to pin doc?')
|
|
|
|
* cssModalButtons(
|
|
|
|
* primary('Yes', dom.on('click', () => { onClick(true); ctl.close(); })),
|
|
|
|
* secondary('Cancel', dom.on('click', () => { onClick(false); ctl.close(); }))
|
|
|
|
* )
|
|
|
|
* ])
|
|
|
|
*/
|
2021-10-12 15:59:12 +00:00
|
|
|
export function modal(
|
|
|
|
createFn: (ctl: IModalControl, owner: MultiHolder) => DomElementArg,
|
|
|
|
options: IModalOptions = {}
|
|
|
|
): void {
|
2022-06-17 03:43:26 +00:00
|
|
|
const {noEscapeKey, noClickAway, refElement = document.body, variant = 'fade-in'} = options;
|
2021-10-12 15:59:12 +00:00
|
|
|
|
|
|
|
function doClose() {
|
|
|
|
if (!modalDom.isConnected) { return; }
|
2022-06-17 03:43:26 +00:00
|
|
|
|
|
|
|
variant === 'collapsing' ? collapseAndCloseModal() : closeModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeModal() {
|
2021-10-12 15:59:12 +00:00
|
|
|
document.body.removeChild(modalDom);
|
|
|
|
// Ensure we run the disposers for the DOM contained in the modal.
|
|
|
|
dom.domDispose(modalDom);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2022-06-17 03:43:26 +00:00
|
|
|
|
|
|
|
function collapseAndCloseModal() {
|
|
|
|
const watcher = new TransitionWatcher(dialogDom);
|
|
|
|
watcher.onDispose(() => closeModal());
|
|
|
|
modalDom.classList.add(cssModalBacker.className + '-collapsing');
|
|
|
|
collapseModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
function expandModal() {
|
|
|
|
prepareForTransition(dialogDom, () => collapseModal());
|
|
|
|
Object.assign(dialogDom.style, {
|
|
|
|
transform: '',
|
|
|
|
opacity: '',
|
|
|
|
visibility: 'visible',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function collapseModal() {
|
|
|
|
const rect = dialogDom.getBoundingClientRect();
|
|
|
|
const collapsedRect = refElement.getBoundingClientRect();
|
|
|
|
const originX = (collapsedRect.left + collapsedRect.width / 2) - rect.left;
|
|
|
|
const originY = (collapsedRect.top + collapsedRect.height / 2) - rect.top;
|
|
|
|
Object.assign(dialogDom.style, {
|
|
|
|
transform: `scale(${collapsedRect.width / rect.width}, ${collapsedRect.height / rect.height})`,
|
|
|
|
transformOrigin: `${originX}px ${originY}px`,
|
|
|
|
opacity: '0',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-12 15:59:12 +00:00
|
|
|
let close = doClose;
|
2022-06-17 03:43:26 +00:00
|
|
|
let dialogDom: HTMLElement;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
const modalDom = cssModalBacker(
|
|
|
|
dom.create((owner) => {
|
2022-06-17 03:43:26 +00:00
|
|
|
const focus = () => dialogDom.focus();
|
2021-10-12 15:59:12 +00:00
|
|
|
const ctl = ModalControl.create(owner, doClose, focus);
|
|
|
|
close = () => ctl.close();
|
|
|
|
|
2022-06-17 03:43:26 +00:00
|
|
|
dialogDom = cssModalDialog(
|
2021-10-12 15:59:12 +00:00
|
|
|
createFn(ctl, owner),
|
2022-06-17 03:43:26 +00:00
|
|
|
cssModalDialog.cls('-collapsing', variant === 'collapsing'),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.on('click', (ev) => ev.stopPropagation()),
|
2022-06-17 03:43:26 +00:00
|
|
|
noEscapeKey ? null : dom.onKeyDown({ Escape: close }),
|
|
|
|
testId('modal-dialog'),
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
2020-10-02 19:26:14 +00:00
|
|
|
FocusLayer.create(owner, {
|
2022-06-17 03:43:26 +00:00
|
|
|
defaultFocusElem: dialogDom,
|
2020-10-02 19:26:14 +00:00
|
|
|
allowFocus: (elem) => (elem !== document.body),
|
2022-04-27 17:46:24 +00:00
|
|
|
// Pause mousetrap keyboard shortcuts while the modal is shown. Without this, arrow keys
|
|
|
|
// will navigate in a grid underneath the modal, and Enter may open a cell there.
|
|
|
|
pauseMousetrap: true
|
2020-10-02 19:26:14 +00:00
|
|
|
});
|
2022-06-17 03:43:26 +00:00
|
|
|
return dialogDom;
|
2020-10-02 15:10:00 +00:00
|
|
|
}),
|
2022-06-17 03:43:26 +00:00
|
|
|
noClickAway ? null : dom.on('click', () => close()),
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
document.body.appendChild(modalDom);
|
2022-06-17 03:43:26 +00:00
|
|
|
if (variant === 'collapsing') { expandModal(); }
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ISaveModalOptions {
|
|
|
|
title: DomElementArg; // Normally just a string.
|
|
|
|
body: DomElementArg; // Content of the dialog.
|
|
|
|
saveLabel?: DomElementArg; // Normally just a string; defaults to "Save".
|
|
|
|
saveDisabled?: Observable<boolean>; // Optional observable for when to disable Save button.
|
|
|
|
saveFunc: () => Promise<unknown>; // Called on Save; dialog closes when promise is fulfilled.
|
|
|
|
hideCancel?: boolean; // If set, hide the Cancel button
|
|
|
|
width?: ModalWidth; // Set a width style for the dialog.
|
|
|
|
modalArgs?: DomElementArg; // Extra args to apply to the outer cssModalDialog element.
|
2020-12-14 17:42:09 +00:00
|
|
|
extraButtons?: DomContents; // More buttons!
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a modal dialog with a title, body, and Save/Cancel buttons. The provided createFunc()
|
|
|
|
* is called immediately to get the dialog's contents and options (see ISaveModalOptions for
|
|
|
|
* details). For example:
|
|
|
|
*
|
|
|
|
* saveModal((ctl, owner) => {
|
|
|
|
* const myObs = Computed.create(owner, ...);
|
|
|
|
* return {
|
|
|
|
* title: 'My Dialog',
|
|
|
|
* body: dom('div', 'Hello', dom.text(myObs)),
|
|
|
|
* saveDisabled: Computed.create(owner, (use) => !use(myObs)),
|
|
|
|
* saveFunc: () => server.ping(),
|
|
|
|
* modalArgs: {style: 'background-color: blue'},
|
|
|
|
* };
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* On Save, the dialog calls saveFunc(), disables the Save button, and stays open until saveFunc()
|
|
|
|
* is resolved. It then closes on success, or reports the error and stays open on rejection. To
|
|
|
|
* stay open without reporting an error (if one is already reported), throw StayOpen exception.
|
|
|
|
*
|
|
|
|
* The dialog interprets Enter/Escape keys as if the Save/Cancel buttons were clicked.
|
|
|
|
*
|
|
|
|
* Note that it's possible to close the dialog via Cancel while saveFunc() is pending. That's
|
|
|
|
* probably desirable, but keep in mind that the dialog may be disposed before saveFunc() returns.
|
|
|
|
*
|
|
|
|
* Error handling examples:
|
|
|
|
* 1. saveFunc: doSomething
|
|
|
|
* (Most common) If doSomething fails, the error is reported and the dialog stays open.
|
|
|
|
* 2. saveFunc: () => doSomething().catch(reportError)
|
|
|
|
* If doSomething fails, the error is reported but the dialog closes anyway.
|
|
|
|
* 3. saveFunc: () => doSomething().catch((e) => { alert("BOOM"); throw new StayOpen(); })
|
|
|
|
* If doSomething fails, an alert is shown, and the dialog stays open.
|
|
|
|
*/
|
|
|
|
export function saveModal(createFunc: (ctl: IModalControl, owner: MultiHolder) => ISaveModalOptions) {
|
|
|
|
return modal((ctl, owner) => {
|
|
|
|
const options = createFunc(ctl, owner);
|
|
|
|
|
|
|
|
const isSaveDisabled = Computed.create(owner, (use) =>
|
2021-10-12 15:59:12 +00:00
|
|
|
use(ctl.workInProgress) || (options.saveDisabled ? use(options.saveDisabled) : false));
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-10-12 15:59:12 +00:00
|
|
|
const save = ctl.doWork(options.saveFunc, {close: true, catchErrors: true});
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
cssModalTitle(options.title, testId('modal-title')),
|
|
|
|
cssModalBody(options.body),
|
|
|
|
cssModalButtons(
|
|
|
|
bigPrimaryButton(options.saveLabel || 'Save',
|
|
|
|
dom.boolAttr('disabled', isSaveDisabled),
|
|
|
|
dom.on('click', save),
|
|
|
|
testId('modal-confirm'),
|
|
|
|
),
|
2020-12-14 17:42:09 +00:00
|
|
|
options.extraButtons,
|
2020-10-02 15:10:00 +00:00
|
|
|
options.hideCancel ? null : bigBasicButton('Cancel',
|
|
|
|
dom.on('click', () => ctl.close()),
|
|
|
|
testId('modal-cancel'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
dom.onKeyDown({ Enter: () => isSaveDisabled.get() || save() }),
|
|
|
|
options.width && cssModalWidth(options.width),
|
|
|
|
options.modalArgs,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a simple confirm modal with 'Enter' bound to the confirm action.
|
|
|
|
*
|
|
|
|
* See saveModal() for error handling notes that here apply to the onConfirm callback.
|
|
|
|
*/
|
|
|
|
export function confirmModal(
|
|
|
|
title: string,
|
|
|
|
btnText: string,
|
|
|
|
onConfirm: () => Promise<void>,
|
|
|
|
explanation?: Element|string,
|
2020-12-14 17:42:09 +00:00
|
|
|
{hideCancel, extraButtons}: {hideCancel?: boolean, extraButtons?: DomContents} = {},
|
2020-10-02 15:10:00 +00:00
|
|
|
): void {
|
|
|
|
return saveModal((ctl, owner): ISaveModalOptions => ({
|
|
|
|
title,
|
|
|
|
body: explanation || null,
|
|
|
|
saveLabel: btnText,
|
|
|
|
saveFunc: onConfirm,
|
|
|
|
hideCancel,
|
|
|
|
width: 'normal',
|
2020-12-14 17:42:09 +00:00
|
|
|
extraButtons,
|
2020-10-02 15:10:00 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-05-04 09:54:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a simple prompt modal (replacement for the native one).
|
|
|
|
* Closed via clicking anywhere outside the modal or Cancel button.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* promptModal(
|
|
|
|
* "Enter your name",
|
|
|
|
* (name: string) => alert(`Hello ${name}`),
|
|
|
|
* "Ok" // Confirm button name,
|
|
|
|
* "John doe", // Initial text (can be empty or undefined)
|
|
|
|
* "Enter your name", // input placeholder
|
|
|
|
* () => console.log('User cancelled') // Called when user cancels, or clicks outside.
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param title: Prompt text.
|
|
|
|
* @param onConfirm: Handler for Confirm button.
|
|
|
|
* @param btnText: Text of the confirm button.
|
|
|
|
* @param initial: Initial value in the input element.
|
|
|
|
* @param placeholder: Placeholder for the input element.
|
|
|
|
* @param onCancel: Optional cancel handler.
|
|
|
|
*/
|
|
|
|
export function promptModal(
|
|
|
|
title: string,
|
|
|
|
onConfirm: (text: string) => Promise<unknown>,
|
|
|
|
btnText: string,
|
|
|
|
initial?: string,
|
|
|
|
placeholder?: string,
|
|
|
|
onCancel?: () => void
|
|
|
|
): void {
|
|
|
|
saveModal((ctl, owner): ISaveModalOptions => {
|
|
|
|
let confirmed = false;
|
|
|
|
const text = Observable.create(owner, initial ?? '');
|
|
|
|
const txtInput = input(text, { onInput : true }, { placeholder }, cssInput.cls(''), testId('modal-prompt'));
|
|
|
|
const options: ISaveModalOptions = {
|
|
|
|
title,
|
|
|
|
body: txtInput,
|
|
|
|
saveLabel: btnText,
|
|
|
|
saveFunc: () => {
|
|
|
|
// Mark that confirm was invoked.
|
|
|
|
confirmed = true;
|
|
|
|
return onConfirm(text.get() || '');
|
|
|
|
},
|
|
|
|
width: 'normal'
|
|
|
|
};
|
|
|
|
owner.onDispose(() => {
|
|
|
|
if (confirmed) { return; }
|
|
|
|
onCancel?.();
|
|
|
|
});
|
|
|
|
setTimeout(() => txtInput.focus(), 10);
|
|
|
|
return options;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps prompt modal in a promise that is resolved either when user confirms or cancels.
|
|
|
|
* When user cancels the returned value is always undefined.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* async handler() {
|
|
|
|
* const name = await invokePrompt("Please enter your name");
|
|
|
|
* if (name !== undefined) alert(`Hello ${name}`);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @param title: Prompt text.
|
|
|
|
* @param btnText: Text of the confirm button, default is "Ok".
|
|
|
|
* @param initial: Initial value in the input element.
|
|
|
|
* @param placeholder: Placeholder for the input element.
|
|
|
|
*/
|
|
|
|
export function invokePrompt(
|
|
|
|
title: string,
|
|
|
|
btnText?: string,
|
|
|
|
initial?: string,
|
|
|
|
placeholder?: string
|
|
|
|
): Promise<string|undefined> {
|
|
|
|
let onResolve: (text: string|undefined) => any;
|
|
|
|
const prom = new Promise<string|undefined>((resolve) => {
|
|
|
|
onResolve = resolve;
|
|
|
|
});
|
|
|
|
promptModal(title, onResolve!, btnText ?? 'Ok', initial, placeholder, () => {
|
|
|
|
if (onResolve) {
|
|
|
|
onResolve(undefined);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return prom;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Builds a simple spinner modal. The modal gets removed when `promise` resolves.
|
|
|
|
*/
|
|
|
|
export async function spinnerModal<T>(
|
|
|
|
title: string,
|
|
|
|
promise: Promise<T>): Promise<T> {
|
|
|
|
|
|
|
|
modal((ctl, owner) => {
|
|
|
|
|
|
|
|
// `finally` is missing from es2016, below is a work-around.
|
|
|
|
const close = () => ctl.close();
|
|
|
|
promise.then(close, close);
|
|
|
|
|
|
|
|
return [
|
|
|
|
cssModalSpinner.cls(''),
|
2021-06-14 21:58:22 +00:00
|
|
|
cssModalTitle(
|
|
|
|
title,
|
|
|
|
testId('modal-spinner-title'),
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssSpinner(loadingSpinner()),
|
|
|
|
testId('modal-spinner'),
|
|
|
|
];
|
|
|
|
}, {
|
|
|
|
noClickAway: true,
|
|
|
|
noEscapeKey: true,
|
|
|
|
});
|
|
|
|
return await promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply this to a modal as
|
|
|
|
* modal(() => [cssModalBody(...), cssModalWidth('normal')])
|
|
|
|
* or
|
|
|
|
* saveModal(() => {..., width: 'normal'})
|
|
|
|
*/
|
|
|
|
export function cssModalWidth(style: ModalWidth) {
|
|
|
|
return cssModalDialog.cls('-' + style);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CSS styled components */
|
|
|
|
|
2022-03-11 10:48:36 +00:00
|
|
|
// For centering, we use 'margin: auto' on the flex item instead of 'justify-content: center' on
|
|
|
|
// the flex container, to ensure the full item can be scrolled in case of overflow.
|
|
|
|
// See https://stackoverflow.com/a/33455342/328565
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssModalDialog = styled('div', `
|
|
|
|
background-color: white;
|
|
|
|
min-width: 428px;
|
|
|
|
color: black;
|
|
|
|
margin: auto;
|
|
|
|
border-radius: 3px;
|
|
|
|
box-shadow: 0 2px 18px 0 rgba(31,37,50,0.31), 0 0 1px 0 rgba(76,86,103,0.24);
|
|
|
|
padding: 40px 64px;
|
|
|
|
outline: none;
|
|
|
|
|
|
|
|
&-normal {
|
|
|
|
max-width: 480px;
|
|
|
|
}
|
|
|
|
&-fixed-wide {
|
|
|
|
width: 600px;
|
|
|
|
}
|
2022-06-17 03:43:26 +00:00
|
|
|
&-collapsing {
|
|
|
|
transition-property: opacity, transform;
|
|
|
|
transition-duration: 0.4s;
|
|
|
|
transition-timing-function: ease-in-out;
|
|
|
|
}
|
2022-01-14 02:55:55 +00:00
|
|
|
@media ${mediaSmall} {
|
|
|
|
& {
|
|
|
|
width: unset;
|
|
|
|
min-width: unset;
|
|
|
|
padding: 24px 16px;
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
export const cssModalTitle = styled('div', `
|
|
|
|
font-size: ${vars.xxxlargeFontSize};
|
|
|
|
font-weight: ${vars.headerControlTextWeight};
|
|
|
|
color: ${colors.dark};
|
|
|
|
margin: 0 0 16px 0;
|
|
|
|
line-height: 32px;
|
2022-04-21 17:57:33 +00:00
|
|
|
overflow-wrap: break-word;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
export const cssModalBody = styled('div', `
|
|
|
|
margin: 16px 0;
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const cssModalButtons = styled('div', `
|
|
|
|
margin: 40px 0 0 0;
|
|
|
|
|
|
|
|
& > button,
|
|
|
|
& > .${cssButton.className} {
|
|
|
|
margin: 0 8px 0 0;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2022-06-03 14:58:07 +00:00
|
|
|
const cssFadeIn = keyframes(`
|
|
|
|
from {background-color: transparent}
|
|
|
|
`);
|
|
|
|
|
2022-06-17 03:43:26 +00:00
|
|
|
const cssFadeOut = keyframes(`
|
|
|
|
from {background-color: ${colors.backdrop}}
|
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const cssModalBacker = styled('div', `
|
|
|
|
position: fixed;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
2022-01-14 02:55:55 +00:00
|
|
|
padding: 16px;
|
2020-10-02 15:10:00 +00:00
|
|
|
z-index: 999;
|
|
|
|
background-color: ${colors.backdrop};
|
|
|
|
overflow-y: auto;
|
2022-06-03 14:58:07 +00:00
|
|
|
animation-name: ${cssFadeIn};
|
|
|
|
animation-duration: 0.4s;
|
2022-06-17 03:43:26 +00:00
|
|
|
|
|
|
|
&-collapsing {
|
|
|
|
animation-name: ${cssFadeOut};
|
|
|
|
background-color: transparent;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSpinner = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
height: 80px;
|
|
|
|
margin: auto;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssModalSpinner = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
`);
|