-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (59 loc) · 1.82 KB
/
index.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
;(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['Regular'], factory);
} else if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object' ) {
module.exports = factory(require('regularjs'));
} else {
window['Regular'] = factory(window.Regular);
}
}(function (Regular) {
function registerElement(tagName, Component) {
var proto = Object.create(HTMLElement.prototype), // custom element prototype
data = {}, // Regular's data object
component, // Component instance
styleList = document.currentScript.ownerDocument.querySelectorAll('style'); // style nodes
proto.createdCallback = function() {
// copy attributes to data object
for(var attr, i=0, attrs =this.attributes; i < attrs.length; i++) {
attr = attrs[i];
// should improve this
try{
data[attr.name] = JSON.parse(attr.value);
}catch(e) {
data[attr.name] = attr.value;
}
}
// initialize
component = new Component({
data: data
});
//component.$inject(this.parentNode);
// create shadow Root and insert all css
var shadowRoot = this.createShadowRoot(),
cssNodeList = [].slice.call(styleList);
cssNodeList.forEach(function(node) {
shadowRoot.appendChild(node);
});
// inject our component
component.$inject(shadowRoot);
};
// called when when modify attribute
// doesn't fire when we declare for the first time
proto.attributeChangedCallback = function(attr, oldVal, newVal) {
try{
data[attr] = JSON.parse(newVal);
}catch(e) {
data[attr] = newVal;
}
component.$update();
};
proto.detachedCallback = function() {
component.destroy();
};
document.registerElement(tagName, {
prototype: proto
});
}
Regular.registerElement = registerElement;
return Regular;
}));