-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcan-view-callbacks.js
276 lines (230 loc) · 8.08 KB
/
can-view-callbacks.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var ObservationRecorder = require('can-observation-recorder');
var dev = require('can-log/dev/dev');
var getGlobal = require('can-globals/global/global');
var domMutate = require('can-dom-mutate/node');
var namespace = require('can-namespace');
var nodeLists = require('can-view-nodelist');
var makeFrag = require("can-util/dom/frag/frag");
var globals = require('can-globals');
//!steal-remove-start
var requestedAttributes = {};
//!steal-remove-end
var tags = {};
// WeakSet containing elements that have been rendered already
// and therefore do not need to be rendered again
var automountEnabled = function(){
return globals.getKeyValue("document").documentElement.getAttribute("data-can-automount") !== "false";
};
var renderedElements = new WeakSet();
var renderNodeAndChildren = function(node) {
var tagName = node.tagName && node.tagName.toLowerCase();
var tagHandler = tags[tagName];
var children;
// skip elements that already have a viewmodel or elements whose tags don't match a registered tag
// or elements that have already been rendered
if (tagHandler && !renderedElements.has(node)) {
tagHandler(node, {});
}
if (node.getElementsByTagName) {
children = node.getElementsByTagName("*");
for (var k=0, child; (child = children[k]) !== undefined; k++) {
renderNodeAndChildren(child);
}
}
};
var mutationObserverEnabled = false;
var globalMutationObserver;
var enableMutationObserver = function() {
if (mutationObserverEnabled) {
return;
}
var mutationHandler = function(mutationsList) {
var addedNodes;
for (var i=0, mutation; (mutation = mutationsList[i]) !== undefined; i++) {
if (mutation.type === "childList") {
addedNodes = mutation.addedNodes;
for (var j=0, addedNode; (addedNode = addedNodes[j]) !== undefined; j++) {
// skip elements that have already been rendered
if (!renderedElements.has(addedNode)) {
renderNodeAndChildren(addedNode);
}
}
}
}
};
var MutationObserver = globals.getKeyValue("MutationObserver");
if(MutationObserver) {
globalMutationObserver = new MutationObserver(mutationHandler);
globalMutationObserver.observe(getGlobal().document.documentElement, {
childList: true,
subtree: true
});
mutationObserverEnabled = true;
}
};
var renderTagsInDocument = function(tagName) {
var nodes = getGlobal().document.getElementsByTagName(tagName);
for (var i=0, node; (node = nodes[i]) !== undefined; i++) {
renderNodeAndChildren(node);
}
};
var attr = function (attributeName, attrHandler) {
if(attrHandler) {
if (typeof attributeName === "string") {
attributes[attributeName] = attrHandler;
//!steal-remove-start
if(requestedAttributes[attributeName]) {
dev.warn("can-view-callbacks: " + attributeName+ " custom attribute behavior requested before it was defined. Make sure "+attributeName+" is defined before it is needed.");
}
//!steal-remove-end
} else {
regExpAttributes.push({
match: attributeName,
handler: attrHandler
});
//!steal-remove-start
Object.keys(requestedAttributes).forEach(function(requested){
if(attributeName.test(requested)) {
dev.warn("can-view-callbacks: " + requested+ " custom attribute behavior requested before it was defined. Make sure "+attributeName+" is defined before it is needed.");
}
});
//!steal-remove-end
}
} else {
var cb = attributes[attributeName];
if( !cb ) {
for( var i = 0, len = regExpAttributes.length; i < len; i++) {
var attrMatcher = regExpAttributes[i];
if(attrMatcher.match.test(attributeName)) {
return attrMatcher.handler;
}
}
}
//!steal-remove-start
requestedAttributes[attributeName] = true;
//!steal-remove-end
return cb;
}
};
var attributes = {},
regExpAttributes = [],
automaticCustomElementCharacters = /[-\:]/;
var defaultCallback = function () {};
var tag = function (tagName, tagHandler) {
if(tagHandler) {
var GLOBAL = getGlobal();
var validCustomElementName = automaticCustomElementCharacters.test(tagName),
tagExists = typeof tags[tagName.toLowerCase()] !== 'undefined',
customElementExists;
//!steal-remove-start
if (tagExists) {
dev.warn("Custom tag: " + tagName.toLowerCase() + " is already defined");
}
if (!validCustomElementName && tagName !== "content") {
dev.warn("Custom tag: " + tagName.toLowerCase() + " hyphen missed");
}
//!steal-remove-end
// if we have html5shiv ... re-generate
if (GLOBAL.html5) {
GLOBAL.html5.elements += " " + tagName;
GLOBAL.html5.shivDocument();
}
tags[tagName.toLowerCase()] = tagHandler;
if(automountEnabled()) {
var customElements = globals.getKeyValue("customElements");
// automatically render elements that have tagHandlers
// If browser supports customElements, register the tag as a custom element
if (customElements) {
customElementExists = customElements.get(tagName.toLowerCase());
if (validCustomElementName && !customElementExists) {
var CustomElement = function() {
return Reflect.construct(HTMLElement, [], CustomElement);
};
CustomElement.prototype.connectedCallback = function() {
// don't re-render an element that has been rendered already
if (!renderedElements.has(this)) {
tags[tagName.toLowerCase()](this, {});
}
};
Object.setPrototypeOf(CustomElement.prototype, HTMLElement.prototype);
Object.setPrototypeOf(CustomElement, HTMLElement);
customElements.define(tagName, CustomElement);
}
}
// If browser doesn't support customElements, set up MutationObserver for
// rendering elements when they are inserted in the page
// and rendering elements that are already in the page
else {
enableMutationObserver();
renderTagsInDocument(tagName);
}
} else if(mutationObserverEnabled) {
globalMutationObserver.disconnect();
}
} else {
var cb;
// if null is passed as tagHandler, remove tag
if (tagHandler === null) {
delete tags[tagName.toLowerCase()];
} else {
cb = tags[tagName.toLowerCase()];
}
if(!cb && automaticCustomElementCharacters.test(tagName)) {
// empty callback for things that look like special tags
cb = defaultCallback;
}
return cb;
}
};
var callbacks = {
_tags: tags,
_attributes: attributes,
_regExpAttributes: regExpAttributes,
defaultCallback: defaultCallback,
tag: tag,
attr: attr,
// handles calling back a tag callback
tagHandler: function(el, tagName, tagData){
var scope = tagData.scope,
helperTagCallback = scope && scope.templateContext.tags.get(tagName),
tagCallback = helperTagCallback || tags[tagName],
res;
// If this was an element like <foo-bar> that doesn't have a component, just render its content
if(tagCallback) {
res = ObservationRecorder.ignore(tagCallback)(el, tagData);
// add the element to the Set of elements that have had their handlers called
// this will prevent the handler from being called again when the element is inserted
renderedElements.add(el);
} else {
res = scope;
}
//!steal-remove-start
if (!tagCallback) {
var GLOBAL = getGlobal();
var ceConstructor = GLOBAL.document.createElement(tagName).constructor;
// If not registered as a custom element, the browser will use default constructors
if (ceConstructor === GLOBAL.HTMLElement || ceConstructor === GLOBAL.HTMLUnknownElement) {
dev.warn('can-view-callbacks: No custom element found for ' + tagName);
}
}
//!steal-remove-end
// If the tagCallback gave us something to render with, and there is content within that element
// render it!
if (res && tagData.subtemplate) {
if (scope !== res) {
scope = scope.add(res);
}
var nodeList = nodeLists.register([], undefined, tagData.parentNodeList || true, false);
nodeList.expression = "<" + el.tagName + ">";
var result = tagData.subtemplate(scope, tagData.options, nodeList);
var frag = typeof result === "string" ? makeFrag(result) : result;
domMutate.appendChild.call(el, frag);
}
}
};
namespace.view = namespace.view || {};
if (namespace.view.callbacks) {
throw new Error("You can't have two versions of can-view-callbacks, check your dependencies");
} else {
module.exports = namespace.view.callbacks = callbacks;
}