UNPKG

879 BJavaScriptView Raw
1import { EVENT_DISABLE, EVENT_ENABLE } from "../api.js";
2import { mixin } from "../mixin.js";
3/**
4 * Mixin class decorator, injects IEnable default implementation, incl.
5 * a `_enabled` property. If the target also implements the
6 * {@link @thi.ng/api#INotify} interface, {@link IEnable.enable} and
7 * {@link IEnable.disable} will automatically emit the respective
8 * events.
9 */
10export const IEnableMixin = mixin({
11 _enabled: true,
12 isEnabled() {
13 return this._enabled;
14 },
15 enable() {
16 $enable(this, true, EVENT_ENABLE);
17 },
18 disable() {
19 $enable(this, false, EVENT_DISABLE);
20 },
21 toggle() {
22 this._enabled ? this.disable() : this.enable();
23 return this._enabled;
24 },
25});
26const $enable = (target, state, id) => {
27 target._enabled = state;
28 if (target.notify) {
29 target.notify({ id, target });
30 }
31};