-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructure.coffee
108 lines (105 loc) · 3.03 KB
/
structure.coffee
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
{isElement,isString,isFunction,arrayize,camelize} = require("./_helpers")
extract = (options, extr) ->
return [null, options] unless extr
opts = {}
for type, names of extr
t = if type == "@" then "on-" else type
for name in names
if (val = options[name]?[type])?
delete options[name][type]
delete options[name] if Object.keys(options[name]).length == 0
opts[camelize(t+name)] = val
return [options, opts]
module.exports =
_name: "structure"
_v: 1
_prio: 800
_mergers: [
require("./_merger").copy(source: "_elLookup")
]
_rebind: "$structure"
mixins: [
require "./directives"
]
methods:
"$structure":
beforeInsert: []
afterInsert: []
el: (name, options, children) ->
if (o = @_elLookup?[camelize(name)])?
o = cb: o unless o.cb?
[options, opts] = extract(options, o.extract)
el = o.cb.call(@, opts, children: children, name: name)
if el.el?
options ?= el.options
children = el.children
el = el.el
else
el = document.createElement(name)
if options?
for name, types of options
for type, value of types
if value.mods?
o = value.mods
o.value = value.val
else
o = value: value
o.el = el
o.type = type
o.name = if o.camel then camelize(name) else name
@$directive o
if children? and not isFunction(children)
for child in children
if isString(child)
@_slots[child] = el
else
el.appendChild child
return el
created: ->
@_slots = {}
connectedCallback: ->
if @_isFirstConnect and @structure?
structure = arrayize(@structure())
for fn in @$structure.beforeInsert
fn.call(@, structure)
for child in @children
if child?
slot = child.getAttribute "slot"
if slot?
@_slots[slot]?.appendChild(slot)
else
@_slots.default?.appendChild(child)
for el in structure
if isString(el)
@_slots[el] = @
else
@appendChild(el)
for fn in @$structure.afterInsert
fn.call(@)
@$structure = null
test module.exports, (merge) ->
el = null
spy = sinon.spy()
before ->
el = makeEl merge({
structure: template(1,"""
<div #ref="someDiv" @click="onClick" :text="someText" :bind="someBind" attr="someAttr">
<slot>
</slot>
</div>
""")
methods:
onClick: spy
data: ->
someText: "textContent"
someBind: "bindContent"
}),false
el.appendChild(document.createElement "div")
after -> el.remove()
it "should not work until attached", ->
should.not.exist el.someDiv
it "should have the right structure once on dom", (done) ->
document.body.appendChild el
el.$nextTick ->
el.should.have.html "<div bind=\"bindContent\" attr=\"someAttr\">textContent<div></div></div>"
done()