-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone.html
More file actions
164 lines (144 loc) · 6.12 KB
/
Copy pathstandalone.html
File metadata and controls
164 lines (144 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object Observer Implementation</title>
</head>
<body>
<h1>Dynamic Element Upgrades</h1>
<p>New elements added below will be dynamically modified by the registry.</p>
<div id="container">
<!-- Elements will be added here -->
</div>
<button onclick="addGenericElement()">Add Generic Element</button>
<button onclick="addMyComponent()">Add 'my-component-name'</button>
<script>
/**
* ---------------------------------------------------------------------
* CUSTOM ELEMENT REGISTRY IMPLEMENTATION
* ---------------------------------------------------------------------
*/
const customElement = {};
customElement.Registry = class Registry {
/**
* @param {Node} [domNode] - An optional DOM node to listen to immediately.
*/
constructor(domNode) {
this.expressions = [];
this.observer = null;
if (domNode) {
this.listen(domNode);
}
}
/**
* Defines a new expression to be run against new elements.
* @param {Function} expression - A function that takes a DOM element as its argument.
*/
define(expression) {
if (typeof expression !== 'function') {
throw new Error('Registry.define() expects a function.');
}
this.expressions.push(expression);
}
/**
* Starts listening for changes on a given DOM node.
* @param {Node} domNode - The DOM node to observe for added elements.
*/
listen(domNode) {
if (!domNode || typeof domNode.nodeType === 'undefined') {
throw new Error('Registry.listen() expects a valid DOM node.');
}
// Disconnect any previous observer
if (this.observer) {
this.observer.disconnect();
}
const observerCallback = (mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
// We only care about element nodes
if (node.nodeType === 1) {
this.expressions.forEach(expression => {
try {
expression(node);
} catch (e) {
console.error("Error applying expression:", e);
}
});
}
});
}
}
};
this.observer = new MutationObserver(observerCallback);
this.observer.observe(domNode, {
childList: true,
subtree: true
});
}
/**
* Stops the observer from listening for changes.
*/
disconnect() {
if (this.observer) {
this.observer.disconnect();
}
}
};
/**
* ---------------------------------------------------------------------
* EXAMPLE USAGE
* ---------------------------------------------------------------------
*/
// 1. Create a global registry listening on the entire document body
const myRegistry = new customElement.Registry(document.body);
// 2. Register an expression that applies a connectedCallback-like function to every new element.
// This expression will turn the border of any new element green.
const expressionOne = function(elem) {
console.log('Expression 1 applied to:', elem.tagName);
elem.style.border = '2px solid green';
// Mimic connectedCallback
if (typeof elem.connectedCallback === 'function') {
elem.connectedCallback();
}
};
myRegistry.define(expressionOne);
// 3. Register a more specific expression for elements with is="my-component-name"
const expressionTwo = function(elem) {
if (elem.getAttribute('is') === 'my-component-name') {
console.log('Expression 2 applied to my-component-name');
// Define and immediately call a "connectedCallback"
elem.connectedCallback = function() {
this.innerText = 'Yahh works';
this.style.backgroundColor = 'yellow';
this.style.padding = '10px';
this.style.display = 'block'; // Make div visible
};
elem.connectedCallback();
}
};
myRegistry.define(expressionTwo);
/**
* ---------------------------------------------------------------------
* DEMO FUNCTIONS
* ---------------------------------------------------------------------
*/
const container = document.getElementById('container');
function addGenericElement() {
const newDiv = document.createElement('div');
newDiv.innerText = 'I am a generic element.';
newDiv.style.margin = '10px 0';
container.appendChild(newDiv);
}
function addMyComponent() {
const newComp = document.createElement('div');
// 'is' is traditionally for extending built-in elements,
// but we use it here as a simple selector for our expression.
newComp.setAttribute('is', 'my-component-name');
newComp.style.margin = '10px 0';
container.appendChild(newComp);
}
</script>
</body>
</html>