UNPKG

verstak

Version:
100 lines (99 loc) 4.04 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { ToggleRef, runNonReactive } from "reactronic"; import { Fragment } from "../Elements.js"; export function OnClick(target, action, key) { if (action !== undefined) { Fragment({ key, signalArgs: { target }, body() { const pointer = target.sensors.pointer; if (target.dataForSensor.click !== undefined && pointer.clicked === target.dataForSensor.click || target.dataForSensor.click === undefined && pointer.clicked) { if (action instanceof Function) { runNonReactive(() => action(pointer)); } else if (action instanceof ToggleRef) { runNonReactive(() => action.toggle()); } } }, }); } } export function OnClickAsync(target, action, key) { if (action !== undefined) { Fragment({ key, signalArgs: { target }, bodyTask() { return __awaiter(this, void 0, void 0, function* () { const pointer = target.sensors.pointer; if (target.dataForSensor.click !== undefined && pointer.clicked === target.dataForSensor.click || target.dataForSensor.click === undefined && pointer.clicked) { if (action instanceof Function) { yield runNonReactive(() => action(pointer)); } else if (action instanceof ToggleRef) { runNonReactive(() => action.toggle()); } } }); }, }); } } export function OnResize(target, action, key) { if (action) { Fragment({ key, signalArgs: { target }, body() { const resize = target.sensors.resize; resize.resizedElements.forEach(x => { action(x); }); }, }); } } export function OnFocus(target, model, switchEditMode = undefined, key) { Fragment({ key, signalArgs: { target, model }, preparation() { this.node.configureReactivity({ throttling: 0 }); }, body() { if (switchEditMode === undefined && !(target instanceof HTMLInputElement || target.hasAttribute("tabindex"))) console.warn(`"${key !== null && key !== void 0 ? key : "noname"}" element must have "tabindex" attribute set in order to be focusable`); if (switchEditMode !== undefined) { switchEditMode(model); } else { model.isEditMode ? target.focus() : target.blur(); } }, }); } export class ResizeData { constructor(id, handler) { this.id = id; this.handler = handler; } } export function prepareResizeHandler(action) { return (element) => { const size = element.borderBoxSize[0]; if (action instanceof Function) runNonReactive(() => action(size.inlineSize, size.blockSize)); else if (action instanceof ResizeData && action.handler !== undefined) runNonReactive(() => action.handler(size.inlineSize, size.blockSize)); }; }