UNPKG

aspen-decorations

Version:

Complex styling for react-aspen w/ inheritance and negations

121 lines 4.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const aspen_core_1 = require("aspen-core"); const notificar_1 = require("notificar"); const types_1 = require("./types"); class Decoration { constructor(...cssClasslist) { this._appliedTargetsDisposables = new WeakMap(); this._negatedTargetsDisposables = new WeakMap(); this._appliedTargets = new Map(); this._negatedTargets = new Map(); this._disabled = false; this.events = new notificar_1.Notificar(); if (Array.isArray(cssClasslist)) { if (cssClasslist.every((classname) => typeof classname === 'string')) { this.cssClasslist = new Set(cssClasslist); } else { throw new TypeError('classlist must be of type `Array<string>`'); } } else { this.cssClasslist = new Set(); } } get disabled() { return this._disabled; } set disabled(disabled) { this._disabled = disabled; this.events.dispatch(disabled ? types_1.DecorationEvent.DecorationDisabled : types_1.DecorationEvent.DecorationEnabled, this); } get appliedTargets() { return this._appliedTargets; } get negatedTargets() { return this._negatedTargets; } addCSSClass(classname) { if (this.cssClasslist.has(classname)) { return; } this.cssClasslist.add(classname); this.events.dispatch(types_1.DecorationEvent.AddCSSClassname, this, classname); } removeCSSClass(classname) { if (!this.cssClasslist.has(classname)) { return; } this.cssClasslist.delete(classname); this.events.dispatch(types_1.DecorationEvent.RemoveCSSClassname, this, classname); } addTarget(target, flags = types_1.TargetMatchMode.Self) { const existingFlags = this._appliedTargets.get(target); if (existingFlags === flags) { return; } if (!(target instanceof aspen_core_1.FileEntry)) { return; } this._appliedTargets.set(target, flags); this.events.dispatch(types_1.DecorationEvent.AddTarget, this, target, flags); this._appliedTargetsDisposables.set(target, target.root.onOnceDisposed(target, () => this.removeTarget(target))); } removeTarget(target) { if (this._appliedTargets.delete(target)) { const disposable = this._appliedTargetsDisposables.get(target); if (disposable) { disposable.dispose(); } this.events.dispatch(types_1.DecorationEvent.RemoveTarget, this, target); } } negateTarget(target, flags = types_1.TargetMatchMode.Self) { const existingFlags = this._negatedTargets.get(target); if (existingFlags === flags) { return; } if (!(target instanceof aspen_core_1.FileEntry)) { return; } this._negatedTargets.set(target, flags); this.events.dispatch(types_1.DecorationEvent.NegateTarget, this, target, flags); this._negatedTargetsDisposables.set(target, target.root.onOnceDisposed(target, () => this.unNegateTarget(target))); } unNegateTarget(target) { if (this._negatedTargets.delete(target)) { const disposable = this._negatedTargetsDisposables.get(target); if (disposable) { disposable.dispose(); } this.events.dispatch(types_1.DecorationEvent.UnNegateTarget, this, target); } } onDidAddTarget(callback) { return this.events.add(types_1.DecorationEvent.AddTarget, callback); } onDidRemoveTarget(callback) { return this.events.add(types_1.DecorationEvent.RemoveTarget, callback); } onDidNegateTarget(callback) { return this.events.add(types_1.DecorationEvent.NegateTarget, callback); } onDidUnNegateTarget(callback) { return this.events.add(types_1.DecorationEvent.UnNegateTarget, callback); } onDidRemoveCSSClassname(callback) { return this.events.add(types_1.DecorationEvent.RemoveCSSClassname, callback); } onDidAddCSSClassname(callback) { return this.events.add(types_1.DecorationEvent.AddCSSClassname, callback); } onDidEnableDecoration(callback) { return this.events.add(types_1.DecorationEvent.DecorationEnabled, callback); } onDidDisableDecoration(callback) { return this.events.add(types_1.DecorationEvent.DecorationDisabled, callback); } } exports.Decoration = Decoration; //# sourceMappingURL=Decoration.js.map