2023-12-12 09:58:20 +00:00
|
|
|
import {useBindable} from 'app/common/gutil';
|
2024-01-12 17:35:24 +00:00
|
|
|
import {BindableValue, Computed, dom, IDisposableOwner, Observable, UseCB} from 'grainjs';
|
2023-08-30 14:35:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Version of makeTestId that can be appended conditionally.
|
|
|
|
*/
|
|
|
|
export function makeTestId(prefix: string) {
|
2023-12-12 09:58:20 +00:00
|
|
|
return (id: BindableValue<string>, obs?: BindableValue<boolean>) => {
|
|
|
|
return dom.cls(use => {
|
|
|
|
if (obs !== undefined && !useBindable(use, obs)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return `${useBindable(use, prefix)}${useBindable(use, id)}`;
|
|
|
|
});
|
|
|
|
};
|
2023-08-30 14:35:13 +00:00
|
|
|
}
|
2024-01-12 17:35:24 +00:00
|
|
|
|
|
|
|
export function autoFocus() {
|
|
|
|
return (el: HTMLElement) => void setTimeout(() => el.focus(), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function autoSelect() {
|
|
|
|
return (el: HTMLElement) => void setTimeout(() => (el as any).select?.(), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Async computed version of Computed.
|
|
|
|
*/
|
|
|
|
export const AsyncComputed = {
|
|
|
|
create<T>(owner: IDisposableOwner, cb: (use: UseCB) => Promise<T>): AsyncComputed<T> {
|
|
|
|
const backend: Observable<T|undefined> = Observable.create(owner, undefined);
|
|
|
|
const dirty = Observable.create(owner, true);
|
|
|
|
const computed: Computed<Promise<T>> = Computed.create(owner, cb as any);
|
|
|
|
let ticket = 0;
|
|
|
|
const listener = (prom: Promise<T>): void => {
|
|
|
|
dirty.set(true);
|
|
|
|
const myTicket = ++ticket;
|
|
|
|
prom.then(v => {
|
|
|
|
if (ticket !== myTicket) { return; }
|
|
|
|
if (backend.isDisposed()) { return; }
|
|
|
|
dirty.set(false);
|
|
|
|
backend.set(v);
|
|
|
|
}).catch(reportError);
|
|
|
|
};
|
|
|
|
owner?.autoDispose(computed.addListener(listener));
|
|
|
|
listener(computed.get());
|
|
|
|
return Object.assign(backend, {
|
|
|
|
dirty
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export interface AsyncComputed<T> extends Observable<T|undefined> {
|
|
|
|
/**
|
|
|
|
* Whether computed wasn't updated yet.
|
|
|
|
*/
|
|
|
|
dirty: Observable<boolean>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops propagation of the event, and prevents default action.
|
|
|
|
*/
|
|
|
|
export function stopEvent(ev: Event) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopImmediatePropagation();
|
|
|
|
}
|