forked from featurist/hyperdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.js
164 lines (131 loc) · 3.76 KB
/
mount.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
var hyperdomMeta = require('./meta')
var runRender = require('./render')
var domComponent = require('./domComponent')
var Set = require('./set')
var refreshEventResult = require('./refreshEventResult')
var lastId = 0
function Mount (model, options) {
var win = (options && options.window) || window
var router = typeof options === 'object' && options.hasOwnProperty('router') ? options.router : undefined
this.requestRender = (options && options.requestRender) || win.requestAnimationFrame || win.setTimeout
this.document = (options && options.document) || document
this.model = model
this.renderQueued = false
this.mountRenderRequested = false
this.componentRendersRequested = undefined
this.id = ++lastId
this.renderId = 0
this.mounted = true
this.router = router
}
Mount.prototype.refreshify = function (fn, options) {
if (!fn) {
return fn
}
if (options && (options.norefresh === true || options.refresh === false)) {
return fn
}
var self = this
return function () {
var result = fn.apply(this, arguments)
return refreshEventResult(result, self, options)
}
}
Mount.prototype.transformFunctionAttribute = function (key, value) {
return this.refreshify(value)
}
Mount.prototype.queueRender = function () {
if (!this.renderQueued) {
var self = this
var requestRender = this.requestRender
this.renderQueued = true
requestRender(function () {
self.renderQueued = false
if (self.mounted) {
if (self.mountRenderRequested) {
self.refreshImmediately()
} else if (self.componentRendersRequested) {
self.refreshComponentsImmediately()
}
}
})
}
}
Mount.prototype.createDomComponent = function () {
return domComponent.create({ document: this.document })
}
Mount.prototype.render = function () {
if (this.router) {
return this.router.render(this.model)
} else {
return this.model
}
}
Mount.prototype.refresh = function () {
this.mountRenderRequested = true
this.queueRender()
}
Mount.prototype.refreshImmediately = function () {
var self = this
runRender(self, function () {
self.renderId++
var vdom = self.render()
self.component.update(vdom)
self.mountRenderRequested = false
})
}
Mount.prototype.refreshComponentsImmediately = function () {
var self = this
runRender(self, function () {
for (var i = 0, l = self.componentRendersRequested.length; i < l; i++) {
var w = self.componentRendersRequested[i]
w.refresh()
}
self.componentRendersRequested = undefined
})
}
Mount.prototype.refreshComponent = function (component) {
if (!this.componentRendersRequested) {
this.componentRendersRequested = []
}
this.componentRendersRequested.push(component)
this.queueRender()
}
Mount.prototype.isComponentInDom = function (component) {
var meta = hyperdomMeta(component)
return meta.lastRenderId === this.renderId
}
Mount.prototype.setupModelComponent = function (model) {
var self = this
var meta = hyperdomMeta(model)
if (!meta.mount) {
meta.mount = this
meta.components = new Set()
model.refresh = function () {
self.refresh()
}
model.refreshImmediately = function () {
self.refreshImmediately()
}
model.refreshComponent = function () {
var meta = hyperdomMeta(this)
meta.components.forEach(function (w) {
self.refreshComponent(w)
})
}
if (typeof model.onload === 'function') {
this.refreshify(function () { return model.onload() }, {refresh: 'promise'})()
}
}
}
Mount.prototype.detach = function () {
this.mounted = false
}
Mount.prototype.remove = function () {
if (this.router) {
this.router.reset()
}
this.component.destroy({removeElement: true})
this.mounted = false
}
module.exports = Mount