forked from MithrilJS/mithril-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
386 lines (348 loc) · 9.76 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
'use strict'
var m = require('mithril/render/hyperscript')
var cssauron = require('cssauron')
var code = require('yields-keycode')
var PD = '//'
function copyObj (data) {
var output = {}
for (var i in data) output[i] = data[i]
return output
}
function identity (thing) {
return thing
}
function isBoolean (thing) {
return typeof thing === 'boolean'
}
function isString (thing) {
return Object.prototype.toString.call(thing) === '[object String]'
}
function isNumber (thing) {
return typeof thing === 'number'
}
function isStringOrNumber (thing) {
return isString(thing) || isNumber(thing)
}
function getContent (thing) {
if (!thing) {
return ''
}
if (isString(thing)) {
return thing
}
if (isNumber(thing)) {
return '' + thing
}
if (thing.tag === '#') {
return getContent(thing.children)
}
if (isArray(thing)) {
return thing.map(getContent).join('')
}
return ''
}
function isArray (thing) {
return Object.prototype.toString.call(thing) === '[object Array]'
}
function isComponent (thing) {
return thing && (typeof thing === 'object' && thing.view) || isFunction(thing) || isClass(thing)
}
function isFunction (thing) {
return typeof thing === 'function' && !isClass(thing)
}
function isClass (thing) {
return thing.prototype != null && typeof thing.prototype.view === "function"
}
function call (thing) {
return thing()
}
var language = cssauron({
tag: 'tag',
contents: function (node) {
var content = node.text == null ? '' : ('' + node.text)
if (isStringOrNumber(node.children) || isBoolean(node.children)) {
return '' + content + node.children
}
return '' + content + getContent(node.renderedChildren)
},
id: function (node) {
if (node.attrs) {
return node.attrs.id || ''
}
return ''
},
class: function (node) {
if (node.attrs) {
return node.attrs.className
}
return ''
},
parent: 'parent',
children: function (node) {
return isArray(node.renderedChildren) ? node.renderedChildren.filter(identity) : []
},
attr: function (node, attr) {
if (node.attrs) {
return node.attrs[attr]
}
}
})
function join (arrays) {
return arrays.reduce(function (result, array) {
return result.concat(array)
}, [])
}
function renderComponents (states, onremovers) {
function renderComponent (component, treePath) {
if (!states[treePath]) {
component.state = copyObj(component.tag)
if (component.tag.oninit) {
component.tag.oninit(component)
states[treePath] = component.state
}
if (component.tag.onremove) {
onremovers.push(function () {
component.tag.onremove(component)
})
}
if (component.tag._captureVnode) {
component.tag._captureVnode(component)
}
} else {
component.state = states[treePath]
if (component.tag.onupdate) {
component.tag.onupdate(component)
}
}
var node
if (component.tag.view) {
node = component.tag.view(component)
} else if (isFunction(component.tag)) {
node = component.tag(component).view(component)
} else if (isClass(component.tag)) {
var c = new component.tag(component)
node = c.view(component)
}
if (node) {
node.parent = component.parent
}
return node
}
return function renderNode (node, treePath) {
if (!node) {
return ''
}
if (isArray(node)) {
return node.map(function (subnode, index) {
return renderNode(subnode, treePath + PD + index)
})
}
if (isComponent(node.tag)) {
return renderNode(renderComponent(node, treePath), treePath + PD + (node.key || ''))
}
if (node.children) {
node.renderedChildren = renderNode(node.children, treePath + PD + (node.key || ''))
}
return node
}
}
function scan (render) {
var states = {}
var onremovers = []
var renderNode = renderComponents(states, onremovers)
var api = {
onremovers: onremovers,
redraw: function () {
api.rootNode = renderNode(render(api), 'ROOT')
}
}
api.redraw()
function find (selectorString, node) {
return select(language(selectorString))(node)
}
function select (matchesSelector) {
return function matches (node) {
if (!node) {
return []
}
if (isArray(node)) {
return join(node.filter(identity).map(function (childNode) {
return matches(childNode)
}))
}
var foundNodes = []
if (matchesSelector(node)) {
foundNodes.push(node)
}
if (isBoolean(node.children) || isStringOrNumber(node.children) || !node.children) {
return foundNodes
}
node.renderedChildren.filter(identity).map(function (child) {
if ((typeof child === 'string') || (typeof child === 'number')) {
return
}
child.parent = node
child.inspect = function () {
return {
tag: child.tag,
children: child.children,
text: child.text,
attrs: child.attrs
}
}
})
return foundNodes.concat(matches(node.renderedChildren))
}
}
function first (selector) {
var node = find(selector, api.rootNode)[0]
if (!node) {
throw new Error('No element matches ' + selector)
}
return node
}
function has (selector) {
return find(selector, api.rootNode).length > 0
}
function contains (value, node) {
return !!find(':contains(' + value + ')', node).length
}
function shouldHaveAtLeast (minCount, selector) {
var actualCount = find(selector, api.rootNode).length
if (actualCount < minCount) {
throw new Error('Wrong count of elements that matches "' + selector +
'"\n expected: >=' + minCount + '\n actual: ' + actualCount)
}
}
function shouldHave (expectedCount, selector) {
if (!selector) {
return isArray(expectedCount)
? shouldHaveCollection(expectedCount)
: shouldHaveAtLeast(1, expectedCount)
}
var actualCount = find(selector, api.rootNode).length
if (actualCount !== expectedCount) {
throw new Error('Wrong count of elements that matches "' + selector +
'"\n expected: ' + expectedCount + '\n actual: ' + actualCount)
}
}
function shouldHaveCollection (selectors) {
selectors.forEach(function (selector) {
shouldHaveAtLeast(1, selector)
})
}
function shouldNotHave (selector) {
shouldHave(0, selector)
}
function shouldContain (string) {
if (!contains(string, api.rootNode)) {
throw new Error('Expected "' + string + '" not found!')
}
}
function shouldNotContain (string) {
if (contains(string, api.rootNode)) {
throw new Error('Unexpected "' + string + '" found!')
}
}
function setValue (selector, string, silent) {
var attrs = first(selector).attrs
var event = {
currentTarget: {value: string},
target: {value: string}
}
attrs.oninput && attrs.oninput(event)
attrs.onchange && attrs.onchange(event)
attrs.onkeyup && attrs.onkeyup(event)
silent || api.redraw()
}
function trigger (eventName) {
return function (selector, event, silent) {
var attrs = first(selector).attrs
attrs[eventName](event)
silent = silent || (event && event.redraw === false)
silent || api.redraw()
}
}
function triggerKey (eventName) {
var fire = trigger('on' + eventName)
return function handleEvent (selector, key, options) {
options = options || {}
var keyCode = isString(key) ? code(key) : key
fire(selector, {
target: options.target || {value: options.value},
altKey: !!options.altKey,
shiftKey: !!options.shiftKey,
ctrlKey: !!options.ctrlKey,
type: eventName,
keyCode: keyCode,
which: keyCode
}, !!options.silent)
}
}
shouldHave.at = {
least: shouldHaveAtLeast
}
api.first = first
api.has = has
api.contains = function (value) {
return contains(value, api.rootNode)
}
api.find = function (selector) {
return find(selector, api.rootNode)
}
api.setValue = setValue
;['focus', 'click', 'blur', 'mousedown', 'mouseup', 'mouseover', 'mouseout', 'mouseenter', 'mouseleave'].map(function (eventName) {
api[eventName] = trigger('on' + eventName)
})
api.contextMenu = trigger('contextmenu')
api.keydown = triggerKey('keydown')
api.keypress = triggerKey('keypress')
api.keyup = triggerKey('keyup')
api.trigger = function (selector, eventName, event, silent) {
trigger(eventName)(selector, event, silent)
}
api.should = {
not: {
have: shouldNotHave,
contain: shouldNotContain
},
have: shouldHave,
contain: shouldContain
}
api.log = function (selector, logFn) {
var util = require('util')
;(logFn || console.log)(util.inspect(api.find(selector), {
colors: true,
depth: null
}))
return api
}
return api
}
function init (viewOrComponentOrRootNode, nodeOrAttrs) {
var api = {}
var isViewFunction = isFunction(viewOrComponentOrRootNode)
if (isViewFunction) {
console.warn('Using a view function as first argument is deprecated in order to support closure components. Please use object notation (`{ view: () => viewFunction(arg) }`).')
api = scan(function () {
return viewOrComponentOrRootNode(nodeOrAttrs)
})
} else if (isComponent(viewOrComponentOrRootNode)) {
api = scan(function (api) {
viewOrComponentOrRootNode._captureVnode = function (vnode) {
api.vnode = vnode
}
return m(viewOrComponentOrRootNode, nodeOrAttrs)
})
} else {
// assume that first argument is rendered view
api = scan(function () {
return viewOrComponentOrRootNode
})
}
api.onremove = function () {
api.onremovers.filter(isFunction).map(call)
}
return api
}
module.exports = init