2022-03-07 09:51:47 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { assert } from '@ember/debug';
|
|
|
|
|
|
|
|
const ATTRIBUTE_CHANGE = 'custom-element.attributeChange';
|
|
|
|
const elements = new Map();
|
|
|
|
const proxies = new WeakMap();
|
|
|
|
|
|
|
|
const typeCast = (attributeInfo, value) => {
|
|
|
|
let type = attributeInfo.type;
|
|
|
|
const d = attributeInfo.default;
|
|
|
|
value = value == null ? attributeInfo.default : value;
|
2022-09-15 08:43:17 +00:00
|
|
|
if (type.indexOf('|') !== -1) {
|
|
|
|
assert(
|
|
|
|
`"${value} is not of type '${type}'"`,
|
|
|
|
type
|
|
|
|
.split('|')
|
|
|
|
.map((item) => item.replaceAll('"', '').trim())
|
|
|
|
.includes(value)
|
|
|
|
);
|
2022-03-07 09:51:47 +00:00
|
|
|
type = 'string';
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
switch (type) {
|
2022-03-07 09:51:47 +00:00
|
|
|
case '<length>':
|
|
|
|
case '<percentage>':
|
|
|
|
case '<dimension>':
|
|
|
|
case 'number': {
|
|
|
|
const num = parseFloat(value);
|
2022-09-15 08:43:17 +00:00
|
|
|
if (isNaN(num)) {
|
2022-03-07 09:51:47 +00:00
|
|
|
return typeof d === 'undefined' ? 0 : d;
|
|
|
|
} else {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case '<integer>':
|
|
|
|
return parseInt(value);
|
|
|
|
case '<string>':
|
|
|
|
case 'string':
|
|
|
|
return (value || '').toString();
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
2022-03-07 09:51:47 +00:00
|
|
|
|
|
|
|
const attributeChangingElement = (name, Cls = HTMLElement, attributes = {}, cssprops = {}) => {
|
|
|
|
const attrs = Object.keys(attributes);
|
|
|
|
|
|
|
|
const customClass = class extends Cls {
|
|
|
|
static get observedAttributes() {
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
const prev = typeCast(attributes[name], oldValue);
|
|
|
|
const value = typeCast(attributes[name], newValue);
|
|
|
|
|
|
|
|
const cssProp = cssprops[`--${name}`];
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof cssProp !== 'undefined' && cssProp.track === `[${name}]`) {
|
|
|
|
this.style.setProperty(`--${name}`, value);
|
2022-03-07 09:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof super.attributeChangedCallback === 'function') {
|
2022-03-07 09:51:47 +00:00
|
|
|
super.attributeChangedCallback(name, prev, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dispatchEvent(
|
2022-09-15 08:43:17 +00:00
|
|
|
new CustomEvent(ATTRIBUTE_CHANGE, {
|
|
|
|
detail: {
|
|
|
|
name: name,
|
|
|
|
previousValue: prev,
|
|
|
|
value: value,
|
|
|
|
},
|
|
|
|
})
|
2022-03-07 09:51:47 +00:00
|
|
|
);
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
2022-03-07 09:51:47 +00:00
|
|
|
customElements.define(name, customClass);
|
|
|
|
return () => {};
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
2022-03-07 09:51:47 +00:00
|
|
|
|
|
|
|
const infoFromArray = (arr, keys) => {
|
|
|
|
return (arr || []).reduce((prev, info) => {
|
|
|
|
let key;
|
|
|
|
const obj = {};
|
|
|
|
keys.forEach((item, i) => {
|
2022-09-15 08:43:17 +00:00
|
|
|
if (item === '_') {
|
2022-03-07 09:51:47 +00:00
|
|
|
key = i;
|
|
|
|
return;
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
obj[item] = info[i];
|
2022-03-07 09:51:47 +00:00
|
|
|
});
|
|
|
|
prev[info[key]] = obj;
|
|
|
|
return prev;
|
|
|
|
}, {});
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
2022-03-07 09:51:47 +00:00
|
|
|
const debounceRAF = (cb, prev) => {
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof prev !== 'undefined') {
|
2022-03-07 09:51:47 +00:00
|
|
|
cancelAnimationFrame(prev);
|
|
|
|
}
|
|
|
|
return requestAnimationFrame(cb);
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
2022-03-07 09:51:47 +00:00
|
|
|
const createElementProxy = ($element, component) => {
|
|
|
|
return new Proxy($element, {
|
|
|
|
get: (target, prop, receiver) => {
|
2022-09-15 08:43:17 +00:00
|
|
|
switch (prop) {
|
2022-03-07 09:51:47 +00:00
|
|
|
case 'attrs':
|
|
|
|
return component.attributes;
|
|
|
|
default:
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof target[prop] === 'function') {
|
2022-03-07 09:51:47 +00:00
|
|
|
// need to ensure we use a MultiWeakMap here
|
|
|
|
// if(this.methods.has(prop)) {
|
|
|
|
// return this.methods.get(prop);
|
|
|
|
// }
|
|
|
|
const method = target[prop].bind(target);
|
|
|
|
// this.methods.set(prop, method);
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
},
|
2022-03-07 09:51:47 +00:00
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
};
|
2022-03-07 09:51:47 +00:00
|
|
|
|
|
|
|
export default class CustomElementComponent extends Component {
|
|
|
|
@tracked $element;
|
|
|
|
@tracked _attributes = {};
|
|
|
|
|
|
|
|
__attributes;
|
|
|
|
_attchange;
|
|
|
|
|
|
|
|
constructor(owner, args) {
|
|
|
|
super(...arguments);
|
2022-09-15 08:43:17 +00:00
|
|
|
if (!elements.has(args.element)) {
|
2022-03-07 09:51:47 +00:00
|
|
|
const cb = attributeChangingElement(
|
|
|
|
args.element,
|
|
|
|
args.class,
|
|
|
|
infoFromArray(args.attrs, ['_', 'type', 'default', 'description']),
|
|
|
|
infoFromArray(args.cssprops, ['_', 'type', 'track', 'description'])
|
2022-09-15 08:43:17 +00:00
|
|
|
);
|
2022-03-07 09:51:47 +00:00
|
|
|
elements.set(args.element, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get attributes() {
|
|
|
|
return this._attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
get element() {
|
2022-09-15 08:43:17 +00:00
|
|
|
if (this.$element) {
|
|
|
|
if (proxies.has(this.$element)) {
|
2022-03-07 09:51:47 +00:00
|
|
|
return proxies.get(this.$element);
|
|
|
|
}
|
|
|
|
const proxy = createElementProxy(this.$element, this);
|
|
|
|
proxies.set(this.$element, proxy);
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setHost(attachShadow, $element) {
|
|
|
|
attachShadow($element);
|
|
|
|
this.$element = $element;
|
|
|
|
this.$element.addEventListener(ATTRIBUTE_CHANGE, this.attributeChange);
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
(this.args.attrs || []).forEach((entry) => {
|
2022-03-07 09:51:47 +00:00
|
|
|
const value = $element.getAttribute(entry[0]);
|
2022-09-15 08:43:17 +00:00
|
|
|
$element.attributeChangedCallback(entry[0], value, value);
|
2022-03-07 09:51:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
disconnect() {
|
|
|
|
this.$element.removeEventListener(ATTRIBUTE_CHANGE, this.attributeChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
attributeChange(e) {
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
// currently if one single attribute changes
|
|
|
|
// they all change
|
|
|
|
this.__attributes = {
|
|
|
|
...this.__attributes,
|
2022-09-15 08:43:17 +00:00
|
|
|
[e.detail.name]: e.detail.value,
|
2022-03-07 09:51:47 +00:00
|
|
|
};
|
|
|
|
this._attchange = debounceRAF(() => {
|
|
|
|
// tell glimmer we changed the attrs
|
|
|
|
this._attributes = this.__attributes;
|
|
|
|
}, this._attchange);
|
|
|
|
}
|
|
|
|
}
|