-
Notifications
You must be signed in to change notification settings - Fork 1
/
directives.coffee
72 lines (69 loc) · 2.2 KB
/
directives.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
{isString,clone,camelize} = require("./_helpers")
module.exports =
_name: "directives"
_v: 1
_prio: 800
_mergers: [
require("./_merger").concat source: "directives"
require("./_merger").copy source: "_attrLookup"
]
mixins: [
require "./setAttribute"
require "./parseElement"
require "./events"
require "./computed"
]
_attrLookup:
text:
":": (o) -> @$computed.orWatch o.value, (val) -> o.el.innerText = val
"#": (o) -> o.el.textContent = o.value
ref:
"#": (o) -> @[o.value] = o.el
methods:
$directive: (o) ->
@$parseElement.byObj(o)
cerror(!o.el, o.type, o.name," tried on not existing element")
if (lookupObj = @_attrLookup[o.name])?
if lookupObj[o.type]?
return lookupObj[o.type].call @, o
#cwarn(!lookupObj[o.type]?, o.type, o.name," found, but not expected")
switch o.type
when "$"
cb = ((el,name,val) -> el[name] = val).bind(@,o.el,camelize(o.name))
@$computed.orWatch o.value, [cb]
when ":"
cb = @$setAttribute.bind(@,o.el,o.name)
@$computed.orWatch o.value, [cb]
when "@"
o.cbs ?= [o.value]
o.event ?= o.name
if o.event and o.cbs.length > 0
@$on o
when "~"
unless @[o.name]?
@[o.name] = =>
for cb in @[o.name]._cbs
cb.apply null, arguments
if o.event
cb = ((el, value, e) -> el.dispatchEvent value, e).bind null, o.el, o.value
else
cb = ((el, value, args...) -> el[value].apply null, args).bind null, o.el, o.value
@[o.name]._cbs.push cb
else
@$setAttribute o.el, o.name, o.value
connectedCallback: ->
if @_isFirstConnect
for directives in @directives
for k,v of directives
o = clone(v)
o.el ?= k
if o.activate?
o.activated = false
@$computed.orWatch o.activate, (val) ->
if val and not o.activated
@$directive o
o.activated = true
else
@$directive o
test module.exports, {}, (el) ->
it "should work", ->