-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-element.js
152 lines (131 loc) · 4.78 KB
/
custom-element.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
(function(global){
if (!document.register) document.register = document.registerElement
var customElements = {},
create = Object.create || function(proto){
var f = function(){}
f.prototype = proto
return new f()
},
has = (has = Object.prototype.hasOwnProperty, has.call.bind(has)),
nop = function(){},
collect = function(from, collected){
if (document.querySelectorAll) return document.querySelectorAll('[data-id]', from)
collected = collected || []
if ('getAttribute' in from && from.getAttribute('data-id')){
collected.push(from)
}
for (var i = 0, max = from.children.length; i<max; i++){
collect(from.children[i], collected)
}
return collected
},
getProto = Object.getPrototypeOf || function(o){ return o.constructor.prototype }
document.register.tag = function(name, options){
options = options || {}
if (!customElements[name]){
customElements[name] = {
options: options
}
return;
}
var xtends = customElements[name].xtends,
ctor = customElements[name].ctor,
proto = xtends? create(getProto(document.createElement(xtends))) : create(HTMLElement.prototype),
created = options.created || nop,
ready = options.ready || nop,
onAttributeChange = options.attributeChangedCallback || nop,
attributes = customElements[name].attributes.split(' '),
template = customElements[name].content,
descriptor = function(attribute){
return {
enumerable: true,
get: function(){
return this.getAttribute(attribute)
},
set: function(value){
this.setAttribute(attribute, value)
}
}
}
delete options.created
delete options.ready
delete options.attributeChangedCallback
for (var any in options){
if (has(options, any)){
proto[any] = options[any]
}
}
proto.attributeChangedCallback = function(attributeName, oldValue, newValue){
if (oldValue === null && ~(' ' + attributes.join(' ') + ' ').indexOf(' ' + attributeName + ' ')) return
(this[attributeName+ 'Changed'] || nop)(oldValue, newValue) // implemented the {attributeName}Changed event
onAttributeChange.apply(this, arguments)
}
proto.createdCallback = function(){
var i, max
for (i = 0, max = attributes.length; i < max; i++){
var any = attributes[i]
if(any != ''){
if(this.getAttribute(any) === null) this.setAttribute(any, proto[any] || null)
Object.defineProperty(this, any, descriptor(any))
}
}
this.$ = {}
var fragment = document.createDocumentFragment()
while (this.firstChild){
fragment.appendChild(this.firstChild)
}
for (i = 0, max = template.childNodes.length; i < max; i++){
this.appendChild(template.childNodes[i].cloneNode(true))
}
var content = this.getElementsByTagName('content')[0]
if (content){
content.parentNode.replaceChild(fragment, content)
} else {
this.appendChild(fragment)
}
var shortcuts = collect(this)
for (i = 0, max = shortcuts.length; i < max; i++){
var shortcut = shortcuts[i]
this.$[shortcut.getAttribute('data-id')] = shortcut
}
ready.call(this)
}
var registered = document.register(name, {
prototype : proto
})
if (ctor){
global[ctor] = registered
}
created.call(registered)
}
var proto = create(HTMLElement.prototype)
proto.createdCallback = function(){
var template = this.getElementsByTagName('template')[0],
name = this.getAttribute('name'),
xtends = this.getAttribute('extends'),
ctor = this.getAttribute('constructor'),
attributes = this.getAttribute('attributes'),
noscript = this.getAttribute('noscript') != null,
options
if (customElements[name] && customElements[name].options){
options = customElements[name].options
}
customElements[name] = {
content: 'content' in template? template.content : template,
xtends: xtends,
ctor: ctor,
attributes: attributes || ''
}
if (options || noscript){
document.register.tag(name, noscript ? {} : options)
}
}
var injectionPoint = document.head || document.body || document.documentElement || {appendChild:nop}
var temp = document.createElement('section');
temp.innerHTML = '<style>template{display:none!important}/*Added by custom-element*/</style>'
injectionPoint.appendChild(temp.firstChild)
temp = null
document.register('custom-element', {
prototype: proto
})
})(this)