@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/README.md +121 -0
- package/dist/attribute.d.ts +15 -0
- package/dist/attribute.js +30 -0
- package/dist/attribute.js.map +1 -0
- package/dist/custom-registry.d.ts +18 -0
- package/dist/custom-registry.js +155 -0
- package/dist/custom-registry.js.map +1 -0
- package/dist/decorators/index.d.ts +21 -0
- package/dist/decorators/index.js +54 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/helpers/index.d.ts +2 -0
- package/dist/helpers/index.js +34 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/index.d.ts +6 -55
- package/dist/index.js +16 -210
- package/dist/index.js.map +1 -1
- package/dist/media-attribute.d.ts +27 -0
- package/dist/media-attribute.js +69 -0
- package/dist/media-attribute.js.map +1 -0
- package/dist/types.d.ts +30 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +1 -1
- package/src/attribute.ts +35 -0
- package/src/custom-registry.ts +171 -0
- package/src/decorators/index.ts +58 -0
- package/src/helpers/index.ts +35 -0
- package/src/index.ts +6 -259
- package/src/media-attribute.ts +92 -0
- package/src/types.ts +34 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const noop = function() {
|
|
2
|
+
/* */
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export const observe = (target: unknown, memberName: string) => {
|
|
6
|
+
const prototype = target.constructor.prototype;
|
|
7
|
+
const OnInit = prototype.OnInit || noop;
|
|
8
|
+
const OnDestroy = prototype.OnDestroy || noop;
|
|
9
|
+
const OnUpdateAttribute = prototype.OnUpdateAttribute || noop;
|
|
10
|
+
|
|
11
|
+
let observer: MutationObserver;
|
|
12
|
+
prototype.OnInit = function() {
|
|
13
|
+
const element = this.element ?? this;
|
|
14
|
+
if (observer) {
|
|
15
|
+
observer.disconnect();
|
|
16
|
+
}
|
|
17
|
+
observer = new MutationObserver(() => {
|
|
18
|
+
OnUpdateAttribute.call(
|
|
19
|
+
this,
|
|
20
|
+
memberName,
|
|
21
|
+
element.getAttribute(memberName)
|
|
22
|
+
);
|
|
23
|
+
target[memberName] = element.getAttribute(memberName);
|
|
24
|
+
});
|
|
25
|
+
observer.observe(element, {
|
|
26
|
+
attributeFilter: [memberName],
|
|
27
|
+
attributes: true
|
|
28
|
+
});
|
|
29
|
+
return OnInit.call(this);
|
|
30
|
+
};
|
|
31
|
+
prototype.OnDestroy = function() {
|
|
32
|
+
observer.disconnect();
|
|
33
|
+
return OnDestroy.call(this);
|
|
34
|
+
};
|
|
35
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,259 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const noop = function() {
|
|
8
|
-
/* */
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
interface ModifierOptions {
|
|
12
|
-
selector: string;
|
|
13
|
-
registry?(this: HTMLElement): CustomAttributeRegistry;
|
|
14
|
-
observedAttributes?: string[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface InputOptions {
|
|
18
|
-
/**
|
|
19
|
-
* If enabled will trigger OnUpdate method on the Attribute
|
|
20
|
-
* */
|
|
21
|
-
observe: true;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const observe = (target: unknown, memberName: string) => {
|
|
25
|
-
const prototype = target.constructor.prototype;
|
|
26
|
-
const OnInit = prototype.OnInit || noop;
|
|
27
|
-
const OnDestroy = prototype.OnInit || noop;
|
|
28
|
-
const OnUpdateAttribute = prototype.OnUpdateAttribute || noop;
|
|
29
|
-
|
|
30
|
-
let observer: MutationObserver;
|
|
31
|
-
prototype.OnInit = function() {
|
|
32
|
-
const element = this.element ?? this;
|
|
33
|
-
if (observer) {
|
|
34
|
-
observer.disconnect();
|
|
35
|
-
}
|
|
36
|
-
observer = new MutationObserver(() =>
|
|
37
|
-
OnUpdateAttribute.call(this, memberName, element.getAttribute(memberName))
|
|
38
|
-
);
|
|
39
|
-
observer.observe(element, {
|
|
40
|
-
attributeFilter: [memberName],
|
|
41
|
-
attributes: true
|
|
42
|
-
});
|
|
43
|
-
return OnInit.call(this);
|
|
44
|
-
};
|
|
45
|
-
prototype.OnDestroy = function() {
|
|
46
|
-
observer.disconnect();
|
|
47
|
-
return OnDestroy.call(this);
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Decorator @Input
|
|
53
|
-
* Used to get attribute from element
|
|
54
|
-
*/
|
|
55
|
-
export const Input = (options?: InputOptions) => (
|
|
56
|
-
target: unknown,
|
|
57
|
-
memberName: string
|
|
58
|
-
) => {
|
|
59
|
-
Object.defineProperty(target, memberName, {
|
|
60
|
-
get: function() {
|
|
61
|
-
const element = this.element ?? this;
|
|
62
|
-
return element.getAttribute(memberName.toLowerCase());
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
if (options?.observe) {
|
|
66
|
-
observe(target, memberName);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Decorator @Modifier
|
|
72
|
-
* Accepts parameter options with selector and registry
|
|
73
|
-
*/
|
|
74
|
-
export const Modifier = (options: ModifierOptions) => {
|
|
75
|
-
return (target: Function) => {
|
|
76
|
-
target['options'] = options;
|
|
77
|
-
};
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/* Someone may like to use CustomAttribute instead of Modifier */
|
|
81
|
-
export const CustomAttribute = Modifier;
|
|
82
|
-
|
|
83
|
-
export abstract class Attribute<T = {}> {
|
|
84
|
-
public static options: ModifierOptions;
|
|
85
|
-
public element?: HTMLElement;
|
|
86
|
-
public value?: string;
|
|
87
|
-
public selector?: string;
|
|
88
|
-
public parent?: HTMLElement;
|
|
89
|
-
setStyles(keys: T) {
|
|
90
|
-
return (div: HTMLElement | Element | HTMLDivElement) => {
|
|
91
|
-
for (const [key, value] of Object.entries(keys)) {
|
|
92
|
-
div['style'][key] = value;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
OnInit(): void {
|
|
97
|
-
/* */
|
|
98
|
-
}
|
|
99
|
-
OnDestroy(): void {
|
|
100
|
-
/* */
|
|
101
|
-
}
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
103
|
-
OnUpdate(_oldValue: string, _newValue: string) {
|
|
104
|
-
/* */
|
|
105
|
-
}
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
107
|
-
OnUpdateAttribute(_name: string, _value: string) {
|
|
108
|
-
/* */
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export class CustomAttributeRegistry {
|
|
113
|
-
private _attrMap: Map<string, Constructor<Attribute>> = new Map();
|
|
114
|
-
private _elementMap: WeakMap<
|
|
115
|
-
HTMLElement,
|
|
116
|
-
Map<string, Attribute>
|
|
117
|
-
> = new WeakMap();
|
|
118
|
-
private observer: MutationObserver;
|
|
119
|
-
|
|
120
|
-
constructor(private parent: HTMLElement) {
|
|
121
|
-
if (!parent) {
|
|
122
|
-
throw new Error('Must be given a parent element');
|
|
123
|
-
}
|
|
124
|
-
this.observe();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
define(attrName: string, Constructor: Constructor<Attribute>) {
|
|
128
|
-
this._attrMap.set(attrName.toLowerCase(), Constructor);
|
|
129
|
-
this.upgradeAttribute(attrName);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
get(element: HTMLElement, attrName: string) {
|
|
133
|
-
const map = this._elementMap.get(element);
|
|
134
|
-
if (!map) return;
|
|
135
|
-
return map.get(attrName.toLowerCase());
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
private getConstructor(attrName: string) {
|
|
139
|
-
return this._attrMap.get(attrName.toLowerCase());
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private observe() {
|
|
143
|
-
this.observer = new MutationObserver(mutations => {
|
|
144
|
-
for (const mutation of mutations) {
|
|
145
|
-
if (mutation.type === 'attributes') {
|
|
146
|
-
const attr = this.getConstructor(mutation.attributeName);
|
|
147
|
-
if (attr) {
|
|
148
|
-
this.found(
|
|
149
|
-
mutation.attributeName,
|
|
150
|
-
mutation.target as never,
|
|
151
|
-
mutation.oldValue
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
} else {
|
|
155
|
-
for (const node of mutation.removedNodes) {
|
|
156
|
-
this.downgrade(node as never);
|
|
157
|
-
}
|
|
158
|
-
for (const node of mutation.addedNodes) {
|
|
159
|
-
this.upgradeElement(node as never);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
this.observer.observe(this.parent?.['shadowRoot'] ?? this.parent, {
|
|
165
|
-
childList: true,
|
|
166
|
-
subtree: true,
|
|
167
|
-
attributes: true,
|
|
168
|
-
attributeOldValue: true
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
unsubscribe() {
|
|
173
|
-
this.observer?.disconnect();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
private upgradeAttribute(attrName: string, doc?: HTMLElement) {
|
|
177
|
-
const parent = doc || this.parent;
|
|
178
|
-
|
|
179
|
-
const matches = parent.querySelectorAll('[' + attrName + ']');
|
|
180
|
-
|
|
181
|
-
for (const match of [...matches]) {
|
|
182
|
-
this.found(attrName, match as never);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private upgradeElement(element: HTMLElement) {
|
|
187
|
-
if (element.nodeType !== 1) {
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
for (const attr of element.attributes) {
|
|
191
|
-
if (this.getConstructor(attr.name)) {
|
|
192
|
-
this.found(attr.name, element);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
for (const [attr] of this._attrMap) {
|
|
196
|
-
this.upgradeAttribute(attr, element);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private downgrade(element: HTMLElement) {
|
|
201
|
-
const map = this._elementMap.get(element);
|
|
202
|
-
if (!map) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
for (const [, instance] of map) {
|
|
206
|
-
if (instance.OnDestroy) {
|
|
207
|
-
instance.OnDestroy();
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
this._elementMap.delete(element);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private found(attributeName: string, el: HTMLElement, oldVal?: string) {
|
|
214
|
-
let map = this._elementMap.get(el);
|
|
215
|
-
if (!map) {
|
|
216
|
-
map = new Map();
|
|
217
|
-
this._elementMap.set(el, map);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const modifier = map.get(attributeName);
|
|
221
|
-
const attribute = el.getAttribute(attributeName);
|
|
222
|
-
|
|
223
|
-
if (!modifier) {
|
|
224
|
-
const Modifier = this.getConstructor(attributeName);
|
|
225
|
-
const modifier = new Modifier();
|
|
226
|
-
if (Modifier.options?.observedAttributes?.length) {
|
|
227
|
-
for (const observedAttribute of Modifier.options.observedAttributes) {
|
|
228
|
-
observe(modifier, observedAttribute);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
map.set(attributeName, modifier);
|
|
232
|
-
modifier.element = el;
|
|
233
|
-
modifier.selector = attributeName;
|
|
234
|
-
modifier.value = attribute || modifier.value;
|
|
235
|
-
modifier.parent = this.parent;
|
|
236
|
-
|
|
237
|
-
if (modifier.OnInit) {
|
|
238
|
-
modifier.OnInit();
|
|
239
|
-
}
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (attribute == null && !!modifier.value) {
|
|
244
|
-
modifier.value = attribute;
|
|
245
|
-
if (modifier.OnDestroy) {
|
|
246
|
-
modifier.OnDestroy();
|
|
247
|
-
}
|
|
248
|
-
map.delete(attributeName);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
if (attribute !== modifier.value) {
|
|
252
|
-
modifier.value = attribute;
|
|
253
|
-
if (modifier.OnUpdate) {
|
|
254
|
-
modifier.OnUpdate(oldVal, attribute);
|
|
255
|
-
}
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
1
|
+
export * from './attribute';
|
|
2
|
+
export * from './custom-registry';
|
|
3
|
+
export * from './decorators';
|
|
4
|
+
export * from './helpers';
|
|
5
|
+
export * from './media-attribute';
|
|
6
|
+
export * from './types';
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Attribute } from './attribute';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Media query Attribute
|
|
5
|
+
* for performance reasons it is key value pair
|
|
6
|
+
*/
|
|
7
|
+
export const MediaMatchers = new Map([
|
|
8
|
+
['screen and (max-width: 599px)', 'xs'],
|
|
9
|
+
['screen and (min-width: 600px) and (max-width: 959px)', 'sm'],
|
|
10
|
+
['screen and (min-width: 960px) and (max-width: 1279px)', 'md'],
|
|
11
|
+
['screen and (min-width: 1280px) and (max-width: 1919px)', 'lg'],
|
|
12
|
+
['screen and (min-width: 1920px) and (max-width: 5000px)', 'xl'],
|
|
13
|
+
['screen and (max-width: 959px)', 'lt-md'],
|
|
14
|
+
['screen and (max-width: 1279px)', 'lt-lg'],
|
|
15
|
+
['screen and (max-width: 1919px)', 'lt-xl'],
|
|
16
|
+
['screen and (min-width: 600px)', 'gt-xs'],
|
|
17
|
+
['screen and (min-width: 960px)', 'gt-sm'],
|
|
18
|
+
['screen and (min-width: 1280px)', 'gt-md'],
|
|
19
|
+
['screen and (min-width: 1920px)', 'gt-lg']
|
|
20
|
+
]);
|
|
21
|
+
type MediaEvent = MediaQueryList | MediaQueryListEvent;
|
|
22
|
+
export type EnterMediaQueryAttributes = [MediaEvent, Attr];
|
|
23
|
+
export type ExitMediaQueryAttributes = [MediaEvent, string];
|
|
24
|
+
|
|
25
|
+
export interface OnUpdateMediaQuery {
|
|
26
|
+
OnEnterMediaQuery(tuple: [MediaEvent, Attr]): void;
|
|
27
|
+
OnExitMediaQuery(tuple: [MediaEvent, string]): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const Breakpoints = [...MediaMatchers.values()];
|
|
31
|
+
|
|
32
|
+
export const createFiltersFromSelector = (selector: string) => [
|
|
33
|
+
...Breakpoints.map(breakpoint => `${selector}.${breakpoint}`),
|
|
34
|
+
selector
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export abstract class MediaQueryAttribute<T> extends Attribute<T>
|
|
38
|
+
implements OnUpdateMediaQuery {
|
|
39
|
+
prevValue: string;
|
|
40
|
+
originalValue: string;
|
|
41
|
+
|
|
42
|
+
private matchers: Map<MediaQueryList, MediaQueryList> = new Map();
|
|
43
|
+
private cachedAttributes: Map<string, Attr> = new Map();
|
|
44
|
+
|
|
45
|
+
listener = (event: MediaQueryList | MediaQueryListEvent) => {
|
|
46
|
+
const key = `${this.selector.toLowerCase()}.${MediaMatchers.get(
|
|
47
|
+
event.media
|
|
48
|
+
)}`;
|
|
49
|
+
const attribute = this.cachedAttributes.get(key);
|
|
50
|
+
|
|
51
|
+
if (event.matches && attribute) {
|
|
52
|
+
return this.OnEnterMediaQuery([event, attribute]);
|
|
53
|
+
}
|
|
54
|
+
return this.OnExitMediaQuery([event, key]);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
OnInit() {
|
|
58
|
+
if (this.OnEnterMediaQuery || this.OnExitMediaQuery) {
|
|
59
|
+
this.originalValue = this.value;
|
|
60
|
+
for (const query of MediaMatchers.keys()) {
|
|
61
|
+
const matcher = window.matchMedia(query);
|
|
62
|
+
|
|
63
|
+
const attr = Object.values(this.element.attributes).find(
|
|
64
|
+
v =>
|
|
65
|
+
v.name ===
|
|
66
|
+
`${this.selector.toLowerCase()}.${MediaMatchers.get(query)}`
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
if (attr) {
|
|
70
|
+
this.cachedAttributes.set(attr.name, attr);
|
|
71
|
+
matcher.addEventListener('change', this.listener);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (attr && matcher.matches) {
|
|
75
|
+
this.listener(matcher);
|
|
76
|
+
}
|
|
77
|
+
this.matchers.set(matcher, matcher);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
OnDestroy() {
|
|
83
|
+
for (const matcher of this.matchers.values()) {
|
|
84
|
+
matcher.removeEventListener('change', this.listener);
|
|
85
|
+
}
|
|
86
|
+
this.cachedAttributes.clear();
|
|
87
|
+
this.matchers.clear();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
abstract OnEnterMediaQuery(tuple: [MediaEvent, Attr]): void;
|
|
91
|
+
abstract OnExitMediaQuery(tuple: [MediaEvent, string]): void;
|
|
92
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CustomAttributeRegistry } from './custom-registry';
|
|
2
|
+
|
|
3
|
+
export type C<T> = new (...args: never[]) => T;
|
|
4
|
+
|
|
5
|
+
export interface Constructor<T> extends C<T> {
|
|
6
|
+
options: ModifierOptions;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ModifierOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Main selector of the attribute
|
|
12
|
+
*/
|
|
13
|
+
selector: string;
|
|
14
|
+
/**
|
|
15
|
+
* Define custom attribute registry
|
|
16
|
+
*/
|
|
17
|
+
registry?(this: HTMLElement): CustomAttributeRegistry;
|
|
18
|
+
/**
|
|
19
|
+
* Specify attributes to be listened
|
|
20
|
+
*/
|
|
21
|
+
observedAttributes?: string[];
|
|
22
|
+
/**
|
|
23
|
+
* Define MutationObserver to listen for parent element changes
|
|
24
|
+
* Defining property will attach observer to this.element
|
|
25
|
+
* */
|
|
26
|
+
observe?: MutationObserverInit;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface InputOptions {
|
|
30
|
+
/**
|
|
31
|
+
* If enabled will trigger OnUpdate method on the Attribute
|
|
32
|
+
* */
|
|
33
|
+
observe: true;
|
|
34
|
+
}
|