-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (166 loc) · 4.88 KB
/
index.js
File metadata and controls
168 lines (166 loc) · 4.88 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
165
166
167
168
var compiler = document.createElement('div')
var elements = new Map()
var sessions = 0
function hyperbind (el, data, opts) {
if (!opts) opts = {}
if (typeof el === 'string') {
compiler.innerHTML = el
el = compiler.firstElementChild
el.remove()
}
if (data === undefined) {
if (arguments.length === 1) return el
data = ''
}
var session = sessions++
elements.set(el, session)
if (data === null) {
if (el.parentNode) {
el.parentNode.removeChild(el)
}
} else if (data instanceof Element) {
while (el.childNodes.length) {
el.removeChild(el.firstChild)
if (elements.get(el) !== session) break
}
el.appendChild(data)
} else if (typeof data === 'object') {
for (var selector in data) {
var value = data[selector]
switch (selector) {
case '$text':
el.textContent = value
break
case '$html':
el.innerHTML = value
break
case '$attribute':
case '$attr':
for (var attr in value) {
var val = value[attr]
if (val === null || val === undefined) {
el.removeAttribute(attr)
} else if (el.getAttribute(attr) !== val) {
el.setAttribute(attr, val)
}
}
break
case '$class':
for (var className in value) {
if (value[className]) {
el.classList.add(className)
} else {
el.classList.remove(className)
}
}
break
case '$value':
value = { value }
case '$prop':
for (var prop in value) {
el[prop] = value[prop]
}
break
case '$list':
var key = value.key
var CreateElement = value.createElement
var each = value.each
var empty = value.empty
var children = el.childNodes
var existing = {}
var items = value.items
if (!items) items = []
for (var i = 0; i < children.length; i++) {
var exists = false
var child = children[i]
var uid = key ? child.item && child.item[key] : child.textContent
for (var n = 0; n < items.length; n++) {
var item = items[n]
if (key) {
if (item[key] === uid) {
exists = true
break
}
} else {
if (item === uid) {
exists = true
break
}
}
}
if (exists) {
existing[uid] = child
} else {
el.removeChild(child)
if (elements.get(el) !== session) break
i--
}
}
for (i = 0; i < items.length; i++) {
item = items[i]
uid = key ? item[key] : item
var existingChild = existing[uid]
if (existingChild) {
if (key) {
existingChild.item = item
}
} else {
existingChild = CreateElement.prototype
? new CreateElement(item, i)
: CreateElement(item, i)
if (key) {
existingChild.item = item
} else {
existingChild.textContent = item
}
}
child = children[i]
if (child !== existingChild) {
el.insertBefore(existingChild, child)
if (elements.get(el) !== session) break
}
if (each) {
each(existingChild, item, i)
if (elements.get(el) !== session) break
}
}
if (items.length === 0 && empty) {
hyperbind(el, empty)
}
break
default:
var matches = el.querySelectorAll(selector)
for (i = 0; i < matches.length; i++) {
var match = matches[i]
if (opts.boundary) {
if (typeof opts.boundary !== 'object') {
opts.boundary = el.querySelectorAll(opts.boundary)
}
var withinBoundary = true
for (n = 0; n < opts.boundary.length; n++) {
var boundary = opts.boundary[n]
var parent = match.parentNode
while (parent !== el && parent !== boundary) {
parent = parent.parentNode
}
if (parent === boundary) {
withinBoundary = false
break
}
}
if (!withinBoundary) {
continue
}
}
hyperbind(match, value, opts)
if (elements.get(el) !== session) break
}
}
}
} else {
el.textContent = data
}
elements.delete(el)
return el
}
export default hyperbind