-
Notifications
You must be signed in to change notification settings - Fork 14
/
rendering.js
195 lines (173 loc) · 6.22 KB
/
rendering.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
var vhtml = require('./vhtml')
var domComponent = require('./domComponent')
var bindingMeta = require('./meta')
var toVdom = require('./toVdom')
var parseTag = require('virtual-dom/virtual-hyperscript/parse-tag')
var Mount = require('./mount')
var Component = require('./component')
var render = require('./render')
var deprecations = require('./deprecations')
var prepareAttributes = require('./prepareAttributes')
var binding = require('./binding')
var refreshAfter = require('./refreshAfter')
var refreshEventResult = require('./refreshEventResult')
exports.append = function (element, render, model, options) {
return startAttachment(render, model, options, function (mount, domComponentOptions) {
var component = domComponent.create(domComponentOptions)
var vdom = mount.render()
element.appendChild(component.create(vdom))
return component
})
}
exports.replace = function (element, render, model, options) {
return startAttachment(render, model, options, function (mount, domComponentOptions) {
var component = domComponent.create(domComponentOptions)
var vdom = mount.render()
element.parentNode.replaceChild(component.create(vdom), element)
return component
})
}
exports.appendVDom = function (vdom, render, model, options) {
return startAttachment(render, model, options, function (mount) {
var component = {
create: function (newVDom) {
vdom.children = []
if (newVDom) {
vdom.children.push(toVdom(newVDom))
}
},
update: function (newVDom) {
vdom.children = []
if (newVDom) {
vdom.children.push(toVdom(newVDom))
}
}
}
component.create(mount.render())
return component
})
}
function startAttachment (render, model, options, attachToDom) {
if (typeof render === 'object') {
return start(render, attachToDom, model)
} else {
deprecations.renderFunction('hyperdom.append and hyperdom.replace with render functions are deprecated, please pass a component')
return start({render: function () { return render(model) }}, attachToDom, options)
}
}
function start (model, attachToDom, options) {
var mount = new Mount(model, options)
render(mount, function () {
if (options) {
var domComponentOptions = {document: options.document}
}
try {
mount.component = attachToDom(mount, domComponentOptions)
} catch (e) {
mount.component = {
update: function () {},
destroy: function () {}
}
throw e
}
})
return mount
}
/**
* this function is quite ugly and you may be very tempted
* to refactor it into smaller functions, I certainly am.
* however, it was written like this for performance
* so think of that before refactoring! :)
*/
exports.html = function (hierarchySelector) {
var hasHierarchy = hierarchySelector.indexOf(' ') >= 0
var selector, selectorElements
if (hasHierarchy) {
selectorElements = hierarchySelector.match(/\S+/g)
selector = selectorElements[selectorElements.length - 1]
} else {
selector = hierarchySelector
}
var childElements
var vdom
var tag
var attributes = arguments[1]
if (attributes && attributes.constructor === Object && typeof attributes.render !== 'function') {
childElements = toVdom.recursive(Array.prototype.slice.call(arguments, 2))
prepareAttributes(selector, attributes, childElements)
tag = parseTag(selector, attributes)
vdom = vhtml(tag, attributes, childElements)
} else {
attributes = {}
childElements = toVdom.recursive(Array.prototype.slice.call(arguments, 1))
tag = parseTag(selector, attributes)
vdom = vhtml(tag, attributes, childElements)
}
if (hasHierarchy) {
for (var n = selectorElements.length - 2; n >= 0; n--) {
vdom = vhtml(selectorElements[n], {}, [vdom])
}
}
return vdom
}
exports.jsx = function (tag, attributes) {
var childElements = toVdom.recursive(Array.prototype.slice.call(arguments, 2))
if (typeof tag === 'string') {
if (attributes) {
prepareAttributes(tag, attributes, childElements)
}
return vhtml(tag, attributes || {}, childElements)
} else {
return new Component(new tag(attributes || {}, childElements), {viewComponent: true}) // eslint-disable-line new-cap
}
}
Object.defineProperty(exports.html, 'currentRender', {get: function () {
deprecations.currentRender('hyperdom.html.currentRender is deprecated, please use hyperdom.currentRender() instead')
return render._currentRender
}})
Object.defineProperty(exports.html, 'refresh', {get: function () {
deprecations.refresh('hyperdom.html.refresh is deprecated, please use component.refresh() instead')
if (render._currentRender) {
var currentRender = render._currentRender
return function (result) {
refreshEventResult(result, currentRender.mount)
}
} else {
throw new Error('Please assign hyperdom.html.refresh during a render cycle if you want to use it in event handlers. See https://github.com/featurist/hyperdom#refresh-outside-render-cycle')
}
}})
Object.defineProperty(exports.html, 'norefresh', {get: function () {
deprecations.norefresh('hyperdom.html.norefresh is deprecated, please use hyperdom.norefresh() instead')
return refreshEventResult.norefresh
}})
Object.defineProperty(exports.html, 'binding', {get: function () {
deprecations.htmlBinding('hyperdom.html.binding() is deprecated, please use hyperdom.binding() instead')
return binding
}})
Object.defineProperty(exports.html, 'refreshAfter', {get: function () {
deprecations.refreshAfter("hyperdom.html.refreshAfter() is deprecated, please use require('hyperdom/refreshAfter')() instead")
return refreshAfter
}})
exports.html.meta = bindingMeta
function rawHtml () {
var selector
var html
var options
if (arguments.length === 2) {
selector = arguments[0]
html = arguments[1]
options = {innerHTML: html}
return exports.html(selector, options)
} else {
selector = arguments[0]
options = arguments[1]
html = arguments[2]
options.innerHTML = html
return exports.html(selector, options)
}
}
exports.html.rawHtml = function () {
deprecations.htmlRawHtml('hyperdom.html.rawHtml() is deprecated, please use hyperdom.rawHtml() instead')
return rawHtml.apply(undefined, arguments)
}
exports.rawHtml = rawHtml