@joist/element
Version:
Intelligently apply styles to WebComponents
150 lines • 8.87 kB
JavaScript
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
import { assert } from "chai";
import { attrChanged } from "./attr-changed.js";
import { attr } from "./attr.js";
import { element } from "./element.js";
it("should call specific attrbute callback", () => {
let args = [];
let MyElement = (() => {
let _classDecorators = [element({
tagName: "attr-changed-1",
})];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = HTMLElement;
let _instanceExtraInitializers = [];
let _test_decorators;
let _test_initializers = [];
let _test_extraInitializers = [];
let _onTestChanged_decorators;
var MyElement = class extends _classSuper {
static { _classThis = this; }
static {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_test_decorators = [attr()];
_onTestChanged_decorators = [attrChanged("test")];
__esDecorate(this, null, _test_decorators, { kind: "accessor", name: "test", static: false, private: false, access: { has: obj => "test" in obj, get: obj => obj.test, set: (obj, value) => { obj.test = value; } }, metadata: _metadata }, _test_initializers, _test_extraInitializers);
__esDecorate(this, null, _onTestChanged_decorators, { kind: "method", name: "onTestChanged", static: false, private: false, access: { has: obj => "onTestChanged" in obj, get: obj => obj.onTestChanged }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
MyElement = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
}
#test_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _test_initializers, "hello"));
get test() { return this.#test_accessor_storage; }
set test(value) { this.#test_accessor_storage = value; }
onTestChanged(name, oldValue, newValue) {
args = [name, oldValue, newValue];
}
constructor() {
super(...arguments);
__runInitializers(this, _test_extraInitializers);
}
};
return MyElement = _classThis;
})();
const el = new MyElement();
document.body.append(el);
assert.deepEqual(args, ["test", null, "hello"]);
el.setAttribute("test", "world");
assert.deepEqual(args, ["test", "hello", "world"]);
el.remove();
});
it("should call callback for multiple attributes", () => {
const args = [];
let MyElement = (() => {
let _classDecorators = [element({
tagName: "attr-changed-2",
})];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = HTMLElement;
let _instanceExtraInitializers = [];
let _test1_decorators;
let _test1_initializers = [];
let _test1_extraInitializers = [];
let _test2_decorators;
let _test2_initializers = [];
let _test2_extraInitializers = [];
let _onTestChanged_decorators;
var MyElement = class extends _classSuper {
static { _classThis = this; }
static {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_test1_decorators = [attr()];
_test2_decorators = [attr()];
_onTestChanged_decorators = [attrChanged("test1", "test2")];
__esDecorate(this, null, _test1_decorators, { kind: "accessor", name: "test1", static: false, private: false, access: { has: obj => "test1" in obj, get: obj => obj.test1, set: (obj, value) => { obj.test1 = value; } }, metadata: _metadata }, _test1_initializers, _test1_extraInitializers);
__esDecorate(this, null, _test2_decorators, { kind: "accessor", name: "test2", static: false, private: false, access: { has: obj => "test2" in obj, get: obj => obj.test2, set: (obj, value) => { obj.test2 = value; } }, metadata: _metadata }, _test2_initializers, _test2_extraInitializers);
__esDecorate(this, null, _onTestChanged_decorators, { kind: "method", name: "onTestChanged", static: false, private: false, access: { has: obj => "onTestChanged" in obj, get: obj => obj.onTestChanged }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
MyElement = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
}
#test1_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _test1_initializers, "hello"));
get test1() { return this.#test1_accessor_storage; }
set test1(value) { this.#test1_accessor_storage = value; }
#test2_accessor_storage = (__runInitializers(this, _test1_extraInitializers), __runInitializers(this, _test2_initializers, "world"));
get test2() { return this.#test2_accessor_storage; }
set test2(value) { this.#test2_accessor_storage = value; }
onTestChanged(attr, oldValue, newValue) {
args.push([attr, oldValue, newValue]);
}
constructor() {
super(...arguments);
__runInitializers(this, _test2_extraInitializers);
}
};
return MyElement = _classThis;
})();
const el = new MyElement();
document.body.append(el);
assert.deepEqual(args, [
["test1", null, "hello"],
["test2", null, "world"],
]);
el.setAttribute("test1", "world");
assert.deepEqual(args, [
["test1", null, "hello"],
["test2", null, "world"],
["test1", "hello", "world"],
]);
el.remove();
});
//# sourceMappingURL=attr-changed.test.js.map