@rhtml/custom-attributes 0.0.110 → 0.0.113

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,213 +1,19 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomAttributeRegistry = exports.Attribute = exports.CustomAttribute = exports.Modifier = exports.Input = void 0;
4
- const noop = function () {
5
- /* */
6
- };
7
- const observe = (target, memberName) => {
8
- const prototype = target.constructor.prototype;
9
- const OnInit = prototype.OnInit || noop;
10
- const OnDestroy = prototype.OnInit || noop;
11
- const OnUpdateAttribute = prototype.OnUpdateAttribute || noop;
12
- let observer;
13
- prototype.OnInit = function () {
14
- var _a;
15
- const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
16
- if (observer) {
17
- observer.disconnect();
18
- }
19
- observer = new MutationObserver(() => OnUpdateAttribute.call(this, memberName, element.getAttribute(memberName)));
20
- observer.observe(element, {
21
- attributeFilter: [memberName],
22
- attributes: true
23
- });
24
- return OnInit.call(this);
25
- };
26
- prototype.OnDestroy = function () {
27
- observer.disconnect();
28
- return OnDestroy.call(this);
29
- };
30
- };
31
- /**
32
- * Decorator @Input
33
- * Used to get attribute from element
34
- */
35
- exports.Input = (options) => (target, memberName) => {
36
- Object.defineProperty(target, memberName, {
37
- get: function () {
38
- var _a;
39
- const element = (_a = this.element) !== null && _a !== void 0 ? _a : this;
40
- return element.getAttribute(memberName.toLowerCase());
41
- }
42
- });
43
- if (options === null || options === void 0 ? void 0 : options.observe) {
44
- observe(target, memberName);
45
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
46
11
  };
47
- /**
48
- * Decorator @Modifier
49
- * Accepts parameter options with selector and registry
50
- */
51
- exports.Modifier = (options) => {
52
- return (target) => {
53
- target['options'] = options;
54
- };
55
- };
56
- /* Someone may like to use CustomAttribute instead of Modifier */
57
- exports.CustomAttribute = exports.Modifier;
58
- class Attribute {
59
- setStyles(keys) {
60
- return (div) => {
61
- for (const [key, value] of Object.entries(keys)) {
62
- div['style'][key] = value;
63
- }
64
- };
65
- }
66
- OnInit() {
67
- /* */
68
- }
69
- OnDestroy() {
70
- /* */
71
- }
72
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
73
- OnUpdate(_oldValue, _newValue) {
74
- /* */
75
- }
76
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
77
- OnUpdateAttribute(_name, _value) {
78
- /* */
79
- }
80
- }
81
- exports.Attribute = Attribute;
82
- class CustomAttributeRegistry {
83
- constructor(parent) {
84
- this.parent = parent;
85
- this._attrMap = new Map();
86
- this._elementMap = new WeakMap();
87
- if (!parent) {
88
- throw new Error('Must be given a parent element');
89
- }
90
- this.observe();
91
- }
92
- define(attrName, Constructor) {
93
- this._attrMap.set(attrName.toLowerCase(), Constructor);
94
- this.upgradeAttribute(attrName);
95
- }
96
- get(element, attrName) {
97
- const map = this._elementMap.get(element);
98
- if (!map)
99
- return;
100
- return map.get(attrName.toLowerCase());
101
- }
102
- getConstructor(attrName) {
103
- return this._attrMap.get(attrName.toLowerCase());
104
- }
105
- observe() {
106
- var _a, _b;
107
- this.observer = new MutationObserver(mutations => {
108
- for (const mutation of mutations) {
109
- if (mutation.type === 'attributes') {
110
- const attr = this.getConstructor(mutation.attributeName);
111
- if (attr) {
112
- this.found(mutation.attributeName, mutation.target, mutation.oldValue);
113
- }
114
- }
115
- else {
116
- for (const node of mutation.removedNodes) {
117
- this.downgrade(node);
118
- }
119
- for (const node of mutation.addedNodes) {
120
- this.upgradeElement(node);
121
- }
122
- }
123
- }
124
- });
125
- this.observer.observe((_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a['shadowRoot']) !== null && _b !== void 0 ? _b : this.parent, {
126
- childList: true,
127
- subtree: true,
128
- attributes: true,
129
- attributeOldValue: true
130
- });
131
- }
132
- unsubscribe() {
133
- var _a;
134
- (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
135
- }
136
- upgradeAttribute(attrName, doc) {
137
- const parent = doc || this.parent;
138
- const matches = parent.querySelectorAll('[' + attrName + ']');
139
- for (const match of [...matches]) {
140
- this.found(attrName, match);
141
- }
142
- }
143
- upgradeElement(element) {
144
- if (element.nodeType !== 1) {
145
- return;
146
- }
147
- for (const attr of element.attributes) {
148
- if (this.getConstructor(attr.name)) {
149
- this.found(attr.name, element);
150
- }
151
- }
152
- for (const [attr] of this._attrMap) {
153
- this.upgradeAttribute(attr, element);
154
- }
155
- }
156
- downgrade(element) {
157
- const map = this._elementMap.get(element);
158
- if (!map) {
159
- return;
160
- }
161
- for (const [, instance] of map) {
162
- if (instance.OnDestroy) {
163
- instance.OnDestroy();
164
- }
165
- }
166
- this._elementMap.delete(element);
167
- }
168
- found(attributeName, el, oldVal) {
169
- var _a, _b;
170
- let map = this._elementMap.get(el);
171
- if (!map) {
172
- map = new Map();
173
- this._elementMap.set(el, map);
174
- }
175
- const modifier = map.get(attributeName);
176
- const attribute = el.getAttribute(attributeName);
177
- if (!modifier) {
178
- const Modifier = this.getConstructor(attributeName);
179
- const modifier = new Modifier();
180
- if ((_b = (_a = Modifier.options) === null || _a === void 0 ? void 0 : _a.observedAttributes) === null || _b === void 0 ? void 0 : _b.length) {
181
- for (const observedAttribute of Modifier.options.observedAttributes) {
182
- observe(modifier, observedAttribute);
183
- }
184
- }
185
- map.set(attributeName, modifier);
186
- modifier.element = el;
187
- modifier.selector = attributeName;
188
- modifier.value = attribute || modifier.value;
189
- modifier.parent = this.parent;
190
- if (modifier.OnInit) {
191
- modifier.OnInit();
192
- }
193
- return;
194
- }
195
- if (attribute == null && !!modifier.value) {
196
- modifier.value = attribute;
197
- if (modifier.OnDestroy) {
198
- modifier.OnDestroy();
199
- }
200
- map.delete(attributeName);
201
- return;
202
- }
203
- if (attribute !== modifier.value) {
204
- modifier.value = attribute;
205
- if (modifier.OnUpdate) {
206
- modifier.OnUpdate(oldVal, attribute);
207
- }
208
- return;
209
- }
210
- }
211
- }
212
- exports.CustomAttributeRegistry = CustomAttributeRegistry;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./attribute"), exports);
14
+ __exportStar(require("./custom-registry"), exports);
15
+ __exportStar(require("./decorators"), exports);
16
+ __exportStar(require("./helpers"), exports);
17
+ __exportStar(require("./media-attribute"), exports);
18
+ __exportStar(require("./types"), exports);
213
19
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAMA,MAAM,IAAI,GAAG;IACX,MAAM;AACR,CAAC,CAAC;AAeF,MAAM,OAAO,GAAG,CAAC,MAAe,EAAE,UAAkB,EAAE,EAAE;IACtD,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC;IAC/C,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,IAAI,IAAI,CAAC;IACxC,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,IAAI,IAAI,CAAC;IAC3C,MAAM,iBAAiB,GAAG,SAAS,CAAC,iBAAiB,IAAI,IAAI,CAAC;IAE9D,IAAI,QAA0B,CAAC;IAC/B,SAAS,CAAC,MAAM,GAAG;;QACjB,MAAM,OAAO,SAAG,IAAI,CAAC,OAAO,mCAAI,IAAI,CAAC;QACrC,IAAI,QAAQ,EAAE;YACZ,QAAQ,CAAC,UAAU,EAAE,CAAC;SACvB;QACD,QAAQ,GAAG,IAAI,gBAAgB,CAAC,GAAG,EAAE,CACnC,iBAAi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3B,IAAI,QAAQ,CAAC,QAAQ,EAAE;gBACrB,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;aACtC;YACD,OAAO;SACR;IACH,CAAC;CACF;AAnJD,0DAmJC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,8CAA4B;AAC5B,oDAAkC;AAClC,+CAA6B;AAC7B,4CAA0B;AAC1B,oDAAkC;AAClC,0CAAwB"}
@@ -0,0 +1,27 @@
1
+ import { Attribute } from './attribute';
2
+ /**
3
+ * Media query Attribute
4
+ * for performance reasons it is key value pair
5
+ */
6
+ export declare const MediaMatchers: Map<string, string>;
7
+ declare type MediaEvent = MediaQueryList | MediaQueryListEvent;
8
+ export declare type EnterMediaQueryAttributes = [MediaEvent, Attr];
9
+ export declare type ExitMediaQueryAttributes = [MediaEvent, string];
10
+ export interface OnUpdateMediaQuery {
11
+ OnEnterMediaQuery(tuple: [MediaEvent, Attr]): void;
12
+ OnExitMediaQuery(tuple: [MediaEvent, string]): void;
13
+ }
14
+ export declare const Breakpoints: string[];
15
+ export declare const createFiltersFromSelector: (selector: string) => string[];
16
+ export declare abstract class MediaQueryAttribute<T> extends Attribute<T> implements OnUpdateMediaQuery {
17
+ prevValue: string;
18
+ originalValue: string;
19
+ private matchers;
20
+ private cachedAttributes;
21
+ listener: (event: MediaQueryList | MediaQueryListEvent) => void;
22
+ OnInit(): void;
23
+ OnDestroy(): void;
24
+ abstract OnEnterMediaQuery(tuple: [MediaEvent, Attr]): void;
25
+ abstract OnExitMediaQuery(tuple: [MediaEvent, string]): void;
26
+ }
27
+ export {};
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MediaQueryAttribute = exports.createFiltersFromSelector = exports.Breakpoints = exports.MediaMatchers = void 0;
4
+ const attribute_1 = require("./attribute");
5
+ /**
6
+ * Media query Attribute
7
+ * for performance reasons it is key value pair
8
+ */
9
+ exports.MediaMatchers = new Map([
10
+ ['screen and (max-width: 599px)', 'xs'],
11
+ ['screen and (min-width: 600px) and (max-width: 959px)', 'sm'],
12
+ ['screen and (min-width: 960px) and (max-width: 1279px)', 'md'],
13
+ ['screen and (min-width: 1280px) and (max-width: 1919px)', 'lg'],
14
+ ['screen and (min-width: 1920px) and (max-width: 5000px)', 'xl'],
15
+ ['screen and (max-width: 959px)', 'lt-md'],
16
+ ['screen and (max-width: 1279px)', 'lt-lg'],
17
+ ['screen and (max-width: 1919px)', 'lt-xl'],
18
+ ['screen and (min-width: 600px)', 'gt-xs'],
19
+ ['screen and (min-width: 960px)', 'gt-sm'],
20
+ ['screen and (min-width: 1280px)', 'gt-md'],
21
+ ['screen and (min-width: 1920px)', 'gt-lg']
22
+ ]);
23
+ exports.Breakpoints = [...exports.MediaMatchers.values()];
24
+ exports.createFiltersFromSelector = (selector) => [
25
+ ...exports.Breakpoints.map(breakpoint => `${selector}.${breakpoint}`),
26
+ selector
27
+ ];
28
+ class MediaQueryAttribute extends attribute_1.Attribute {
29
+ constructor() {
30
+ super(...arguments);
31
+ this.matchers = new Map();
32
+ this.cachedAttributes = new Map();
33
+ this.listener = (event) => {
34
+ const key = `${this.selector.toLowerCase()}.${exports.MediaMatchers.get(event.media)}`;
35
+ const attribute = this.cachedAttributes.get(key);
36
+ if (event.matches && attribute) {
37
+ return this.OnEnterMediaQuery([event, attribute]);
38
+ }
39
+ return this.OnExitMediaQuery([event, key]);
40
+ };
41
+ }
42
+ OnInit() {
43
+ if (this.OnEnterMediaQuery || this.OnExitMediaQuery) {
44
+ this.originalValue = this.value;
45
+ for (const query of exports.MediaMatchers.keys()) {
46
+ const matcher = window.matchMedia(query);
47
+ const attr = Object.values(this.element.attributes).find(v => v.name ===
48
+ `${this.selector.toLowerCase()}.${exports.MediaMatchers.get(query)}`);
49
+ if (attr) {
50
+ this.cachedAttributes.set(attr.name, attr);
51
+ matcher.addEventListener('change', this.listener);
52
+ }
53
+ if (attr && matcher.matches) {
54
+ this.listener(matcher);
55
+ }
56
+ this.matchers.set(matcher, matcher);
57
+ }
58
+ }
59
+ }
60
+ OnDestroy() {
61
+ for (const matcher of this.matchers.values()) {
62
+ matcher.removeEventListener('change', this.listener);
63
+ }
64
+ this.cachedAttributes.clear();
65
+ this.matchers.clear();
66
+ }
67
+ }
68
+ exports.MediaQueryAttribute = MediaQueryAttribute;
69
+ //# sourceMappingURL=media-attribute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media-attribute.js","sourceRoot":"","sources":["../src/media-attribute.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAExC;;;GAGG;AACU,QAAA,aAAa,GAAG,IAAI,GAAG,CAAC;IACnC,CAAC,+BAA+B,EAAE,IAAI,CAAC;IACvC,CAAC,sDAAsD,EAAE,IAAI,CAAC;IAC9D,CAAC,uDAAuD,EAAE,IAAI,CAAC;IAC/D,CAAC,wDAAwD,EAAE,IAAI,CAAC;IAChE,CAAC,wDAAwD,EAAE,IAAI,CAAC;IAChE,CAAC,+BAA+B,EAAE,OAAO,CAAC;IAC1C,CAAC,gCAAgC,EAAE,OAAO,CAAC;IAC3C,CAAC,gCAAgC,EAAE,OAAO,CAAC;IAC3C,CAAC,+BAA+B,EAAE,OAAO,CAAC;IAC1C,CAAC,+BAA+B,EAAE,OAAO,CAAC;IAC1C,CAAC,gCAAgC,EAAE,OAAO,CAAC;IAC3C,CAAC,gCAAgC,EAAE,OAAO,CAAC;CAC5C,CAAC,CAAC;AAUU,QAAA,WAAW,GAAG,CAAC,GAAG,qBAAa,CAAC,MAAM,EAAE,CAAC,CAAC;AAE1C,QAAA,yBAAyB,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC;IAC7D,GAAG,mBAAW,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,QAAQ,IAAI,UAAU,EAAE,CAAC;IAC7D,QAAQ;CACT,CAAC;AAEF,MAAsB,mBAAuB,SAAQ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E,IAAI,CAAC,CAAC;oBAC3C,OAAO,CAAC,gBAAgB,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;iBACnD;gBAED,IAAI,IAAI,IAAI,OAAO,CAAC,OAAO,EAAE;oBAC3B,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;iBACxB;gBACD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;aACrC;SACF;IACH,CAAC;IAED,SAAS;QACP,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE;YAC5C,OAAO,CAAC,mBAAmB,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;SACtD;QACD,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;QAC9B,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;CAIF;AAvDD,kDAuDC"}
@@ -0,0 +1,30 @@
1
+ import { CustomAttributeRegistry } from './custom-registry';
2
+ export declare type C<T> = new (...args: never[]) => T;
3
+ export interface Constructor<T> extends C<T> {
4
+ options: ModifierOptions;
5
+ }
6
+ export interface ModifierOptions {
7
+ /**
8
+ * Main selector of the attribute
9
+ */
10
+ selector: string;
11
+ /**
12
+ * Define custom attribute registry
13
+ */
14
+ registry?(this: HTMLElement): CustomAttributeRegistry;
15
+ /**
16
+ * Specify attributes to be listened
17
+ */
18
+ observedAttributes?: string[];
19
+ /**
20
+ * Define MutationObserver to listen for parent element changes
21
+ * Defining property will attach observer to this.element
22
+ * */
23
+ observe?: MutationObserverInit;
24
+ }
25
+ export interface InputOptions {
26
+ /**
27
+ * If enabled will trigger OnUpdate method on the Attribute
28
+ * */
29
+ observe: true;
30
+ }
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhtml/custom-attributes",
3
- "version": "0.0.110",
3
+ "version": "0.0.113",
4
4
  "description": "Custom Attribute Registry",
5
5
  "scripts": {
6
6
  "start": "npx parcel ./examples/index.html --out-dir build/examples",
@@ -0,0 +1,35 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import { ModifierOptions } from './types';
3
+
4
+ /* */
5
+ export abstract class Attribute<T = {}> {
6
+ public static options: ModifierOptions;
7
+ public element?: HTMLElement;
8
+ public value?: string;
9
+ public selector?: string;
10
+ public parent?: HTMLElement;
11
+ public observer?: MutationObserver;
12
+ setStyles(keys: T) {
13
+ return (div: HTMLElement | Element | HTMLDivElement) => {
14
+ for (const [key, value] of Object.entries(keys)) {
15
+ div['style'][key] = value;
16
+ }
17
+ };
18
+ }
19
+ OnInit(): void {
20
+ /* */
21
+ }
22
+ OnDestroy(): void {
23
+ /* */
24
+ }
25
+ OnUpdate(_oldValue: string, _newValue: string) {
26
+ /* */
27
+ }
28
+ OnUpdateAttribute(_name: string, _value: string) {
29
+ /* */
30
+ }
31
+
32
+ OnChange(_records: MutationRecord[]): void {
33
+ /* */
34
+ }
35
+ }
@@ -0,0 +1,171 @@
1
+ import { Attribute } from './attribute';
2
+ import { observe } from './helpers';
3
+ import { Constructor } from './types';
4
+
5
+ export class CustomAttributeRegistry {
6
+ private _attrMap: Map<string, Constructor<Attribute>> = new Map();
7
+ private _elementMap: Map<HTMLElement, Map<string, Attribute>> = new Map();
8
+ private observer: MutationObserver;
9
+
10
+ constructor(private parent: HTMLElement) {
11
+ if (!parent) {
12
+ throw new Error('Must be given a parent element');
13
+ }
14
+ this.observe();
15
+ }
16
+
17
+ define(attrName: string, Constructor: Constructor<Attribute>) {
18
+ this._attrMap.set(attrName.toLowerCase(), Constructor);
19
+ this.upgradeAttribute(attrName);
20
+ }
21
+
22
+ get(element: HTMLElement, attrName: string) {
23
+ const map = this._elementMap.get(element);
24
+ if (!map) return;
25
+ return map.get(attrName.toLowerCase());
26
+ }
27
+
28
+ private getConstructor(attrName: string) {
29
+ return this._attrMap.get(attrName.toLowerCase());
30
+ }
31
+
32
+ private observe() {
33
+ this.observer = new MutationObserver(mutations => {
34
+ for (const mutation of mutations) {
35
+ if (mutation.type === 'attributes') {
36
+ const attr = this.getConstructor(mutation.attributeName);
37
+ if (attr) {
38
+ this.found(
39
+ mutation.attributeName,
40
+ mutation.target as never,
41
+ mutation.oldValue
42
+ );
43
+ }
44
+ } else {
45
+ for (const node of mutation.removedNodes) {
46
+ this.downgrade(node as never);
47
+ }
48
+ for (const node of mutation.addedNodes) {
49
+ this.upgradeElement(node as never);
50
+ }
51
+ }
52
+ }
53
+ });
54
+ this.observer.observe(this.parent?.['shadowRoot'] ?? this.parent, {
55
+ childList: true,
56
+ subtree: true,
57
+ attributes: true,
58
+ attributeOldValue: true
59
+ });
60
+ }
61
+
62
+ unsubscribe() {
63
+ this.observer?.disconnect();
64
+ const values = [...this._elementMap.values()];
65
+ for (const elModifiers of values) {
66
+ const modifiers = [...elModifiers.values()];
67
+ for (const modifier of modifiers) {
68
+ modifier.OnDestroy();
69
+ if (modifier.observer) {
70
+ modifier.observer.disconnect();
71
+ }
72
+ }
73
+ elModifiers.clear();
74
+ }
75
+ this._elementMap.clear();
76
+ }
77
+
78
+ private upgradeAttribute(attrName: string, doc?: HTMLElement) {
79
+ const parent = doc || this.parent;
80
+
81
+ const matches = parent.querySelectorAll('[' + attrName + ']');
82
+
83
+ for (const match of [...matches]) {
84
+ this.found(attrName, match as never);
85
+ }
86
+ }
87
+
88
+ private upgradeElement(element: HTMLElement) {
89
+ if (element.nodeType !== 1) {
90
+ return;
91
+ }
92
+ for (const attr of element.attributes) {
93
+ if (this.getConstructor(attr.name)) {
94
+ this.found(attr.name, element);
95
+ }
96
+ }
97
+ for (const [attr] of this._attrMap) {
98
+ this.upgradeAttribute(attr, element);
99
+ }
100
+ }
101
+
102
+ private downgrade(element: HTMLElement) {
103
+ const map = this._elementMap.get(element);
104
+ if (!map) {
105
+ return;
106
+ }
107
+ for (const [, instance] of map) {
108
+ if (instance.OnDestroy) {
109
+ instance.OnDestroy();
110
+ }
111
+ }
112
+ this._elementMap.delete(element);
113
+ }
114
+
115
+ private found(attributeName: string, el: HTMLElement, oldVal?: string) {
116
+ let map = this._elementMap.get(el);
117
+ if (!map) {
118
+ map = new Map();
119
+ this._elementMap.set(el, map);
120
+ }
121
+
122
+ let modifier = map.get(attributeName);
123
+ const attribute = el.getAttribute(attributeName);
124
+
125
+ if (!modifier) {
126
+ const Modifier = this.getConstructor(attributeName);
127
+ modifier = new Modifier();
128
+ if (Modifier.options?.observedAttributes?.length) {
129
+ for (const observedAttribute of Modifier.options.observedAttributes) {
130
+ observe(modifier, observedAttribute);
131
+ }
132
+ }
133
+ modifier.element = el;
134
+ modifier.selector = attributeName;
135
+
136
+ modifier.value = attribute || modifier.value;
137
+ modifier.parent = this.parent;
138
+
139
+ if (modifier.OnInit) {
140
+ modifier.OnInit();
141
+ }
142
+ if (Modifier.options.observe) {
143
+ modifier.observer = new MutationObserver(records =>
144
+ modifier.OnChange(records)
145
+ );
146
+ modifier.observer.observe(modifier.element, Modifier.options.observe);
147
+ }
148
+ map.set(attributeName, modifier);
149
+ return;
150
+ }
151
+
152
+ if (attribute == null && !!modifier.value) {
153
+ modifier.value = attribute;
154
+ if (modifier.OnDestroy) {
155
+ modifier.OnDestroy();
156
+ }
157
+ if (modifier.observer) {
158
+ modifier.observer.disconnect();
159
+ }
160
+ map.delete(attributeName);
161
+ return;
162
+ }
163
+ if (attribute !== modifier.value) {
164
+ modifier.value = attribute;
165
+ if (modifier.OnUpdate) {
166
+ modifier.OnUpdate(oldVal, attribute);
167
+ }
168
+ return;
169
+ }
170
+ }
171
+ }
@@ -0,0 +1,58 @@
1
+ import { noop, observe } from '../helpers';
2
+ import { InputOptions, ModifierOptions } from '../types';
3
+
4
+ /**
5
+ * Decorator @Input
6
+ * Used to get attribute from element
7
+ */
8
+ export const Input = (options?: InputOptions) => (
9
+ target,
10
+ memberName: string
11
+ ) => {
12
+ const OnInit = target.OnInit || noop;
13
+ Object.defineProperty(target, 'OnInit', {
14
+ value() {
15
+ let originalValue = this[memberName];
16
+
17
+ const element = this.element ?? this;
18
+ Object.defineProperty(this, memberName, {
19
+ get: function() {
20
+ originalValue = element.getAttribute(memberName.toLowerCase());
21
+ return originalValue;
22
+ },
23
+ set(value) {
24
+ element.setAttribute(memberName.toLowerCase(), value);
25
+ originalValue = value;
26
+ },
27
+ configurable: true
28
+ });
29
+ return OnInit.call(this);
30
+ },
31
+ configurable: true
32
+ });
33
+
34
+ if (options?.observe) {
35
+ observe(target, memberName);
36
+ }
37
+ };
38
+
39
+ /**
40
+ * Decorator @Modifier
41
+ * Accepts parameter options with selector and registry
42
+ */
43
+ export const Modifier = (options: ModifierOptions) => {
44
+ return (target: Function) => {
45
+ target['options'] = options;
46
+ };
47
+ };
48
+
49
+ /**
50
+ * Decorator @CustomAttribute
51
+ * Accepts parameter options with selector and registry
52
+ */
53
+ export const CustomAttribute = Modifier;
54
+ /**
55
+ * Decorator @Directive
56
+ * Accepts parameter options with selector and registry
57
+ */
58
+ export const Directive = Modifier;