Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 94x 94x 94x 94x 94x 94x 94x 94x 188x 4x 4x 4x 4x 4x 94x 188x 4x 4x 4x 4x 4x 94x 188x 4x 4x 4x 4x 94x 12x 12x 42x 42x 40x 12x 12x 12x 8x 4x 3x 3x 94x 12x 12x 3x 3x 9x 15x 15x 5x 5x 5x 3x 3x 12x 9x | /** * @module Actions * * @categoryDescription Controlling triggers * {@link Enable} and {@link Disable} enable or disable a list of triggers * defined on the given element. * * {@link Run} runs or reverses a list of triggers defined on the given * element. */ import * as MC from "@lisn/globals/minification-constants"; import * as MH from "@lisn/globals/minification-helpers"; import { logWarn } from "@lisn/utils/log"; import { waitForDelay } from "@lisn/utils/tasks"; import { formatAsString } from "@lisn/utils/text"; import { Trigger } from "@lisn/triggers/trigger"; import { Action, registerAction } from "@lisn/actions/action"; /** * Enables or disables a list of triggers defined on the given element. * * **IMPORTANT:** When constructed, it disables all given triggers as a form of * initialization. * * ------- * * To use with auto-widgets (HTML API) as part of a trigger specification: * - Action name: "enable". * - Arguments (required): one or more unique IDs of triggers defined on the * given element * - Options: none * * @example * ```html * <button id="btn">Enable/disable</button> * <button data-lisn-on-click=" * @enable: triggerA,triggerB +target=#btn * @add-class: clsA +id=triggerA * " * data-lisn-on-click="@add-class: clsB +id=triggerB" * ></button> * ``` * * @category Controlling triggers */ export class Enable implements Action { /** * Enables the triggers with IDs given to the constructor. */ readonly do: () => Promise<void>; /** * Disables the triggers with IDs given to the constructor. */ readonly undo: () => Promise<void>; /** * Toggles the enabled state on each trigger given to the constructor. */ readonly toggle: () => Promise<void>; static register() { registerAction("enable", (element, ids) => new Enable(element, ...ids)); } constructor(element: Element, ...ids: string[]) { const { _enable, _disable, _toggleEnable } = getMethods(element, ids); _disable(); // initial state this.do = _enable; this.undo = _disable; this[MC.S_TOGGLE] = _toggleEnable; } } /** * Disables or enables a list of triggers defined on the given element. * * **IMPORTANT:** When constructed, it enables all given triggers as a form of * initialization. * * ------- * * To use with auto-widgets (HTML API) as part of a trigger specification: * - Action name: "disable". * - Arguments (required): one or more unique IDs of triggers defined on the * given element * - Options: none * * @example * ```html * <button id="btn">Enable/disable</button> * <button data-lisn-on-click=" * @disable: triggerA,triggerB +target=#btn * @add-class: clsA +id=triggerA * " * data-lisn-on-click="@add-class: clsB +id=triggerB" * ></button> * ``` * * @category Controlling triggers */ export class Disable implements Action { /** * Disables the triggers with IDs given to the constructor. */ readonly do: () => Promise<void>; /** * Enables the triggers with IDs given to the constructor. */ readonly undo: () => Promise<void>; /** * Toggles the enabled state on each trigger given to the constructor. */ readonly toggle: () => Promise<void>; static register() { registerAction("disable", (element, ids) => new Disable(element, ...ids)); } constructor(element: Element, ...ids: string[]) { const { _enable, _disable, _toggleEnable } = getMethods(element, ids); _enable(); // initial state this.do = _disable; this.undo = _enable; this[MC.S_TOGGLE] = _toggleEnable; } } /** * Runs or reverses a list of triggers defined on the given element. * * ------- * * To use with auto-widgets (HTML API) as part of a trigger specification: * - Action name: "run". * - Arguments (required): one or more unique IDs of triggers defined on the * given element * - Options: none * * @example * ```html * <button data-lisn-on-click=" * @run: triggerA,triggerB * @add-class: clsA +id=triggerA * " * data-lisn-on-run="@add-class: clsB +id=triggerB" * ></button> * ``` * * @category Controlling triggers */ export class Run implements Action { /** * Runs the triggers with IDs given to the constructor. */ readonly do: () => Promise<void>; /** * Reverses the triggers with IDs given to the constructor. */ readonly undo: () => Promise<void>; /** * Toggles the triggers with IDs given to the constructor. */ readonly toggle: () => Promise<void>; static register() { registerAction("run", (element, ids) => new Run(element, ...ids)); } constructor(element: Element, ...ids: string[]) { const { _run, _reverse, _toggle } = getMethods(element, ids); this.do = _run; this.undo = _reverse; this[MC.S_TOGGLE] = _toggle; } } // -------------------- const getMethods = (element: Element, ids: string[]) => { const triggerPromises = getTriggers(element, ids); const call = async ( method: | "enable" | "disable" | "toggleEnable" | "run" | "reverse" | "toggle", ) => { const triggers = await triggerPromises; for (const trigger of triggers) { trigger[method](); } }; return { _enable: () => call("enable"), _disable: () => call("disable"), _toggleEnable: () => call("toggleEnable"), _run: () => call("run"), _reverse: () => call("reverse"), _toggle: () => call(MC.S_TOGGLE), }; }; const getTriggers = async (element: Element, ids: string[]) => { const triggers: Trigger[] = []; if (!MH.lengthOf(ids)) { logWarn("At least 1 ID is required for enable action"); return triggers; } for (const id of ids) { let trigger = Trigger.get(element, id); if (!trigger) { await waitForDelay(0); // in case it's being processed now trigger = Trigger.get(element, id); if (!trigger) { logWarn( `No trigger with ID ${id} for element ${formatAsString(element)}`, ); continue; } } triggers.push(trigger); } return triggers; }; |