rivets.coffee
2.17 KB
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
# rivets.js
# version : 0.1.2
# author : Michael Richards
# license : MIT
registerBinding = (el, adapter, type, context, keypath) ->
bind = bindings[type] || attributeBinding type
bind el, adapter.read context, keypath
adapter.subscribe context, keypath, (value) ->
bind el, value
if type in bidirectionalBindings
el.addEventListener 'change', ->
adapter.publish context, keypath, getInputValue this
setAttribute = (el, attr, value, mirrored=false) ->
if value
el.setAttribute attr, if mirrored then attr else value
else
el.removeAttribute attr
getInputValue = (el) ->
switch el.type
when 'text', 'textarea', 'password', 'select-one' then el.value
when 'checkbox' then el.checked
bindings =
show: (el, value) ->
el.style.display = if value then '' else 'none'
hide: (el, value) ->
el.style.display = if value then 'none' else ''
enabled: (el, value) ->
setAttribute el, 'disabled', !value, true
disabled: (el, value) ->
setAttribute el, 'disabled', value, true
checked: (el, value) ->
setAttribute el, 'checked', value, true
unchecked: (el, value) ->
setAttribute el, 'checked', !value, true
selected: (el, value) ->
setAttribute el, 'selected', value, true
unselected: (el, value) ->
setAttribute el, 'checked', !value, true
text: (el, value) ->
el.innerHTML = value or ''
value: (el, value) ->
el.value = value
attributeBinding = (attr) ->
(el, value) ->
setAttribute el, attr, value
bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']
rivets =
bind: (el, adapter, contexts={}) ->
nodes = el.getElementsByTagName '*'
[0..(nodes.length - 1)].forEach (n) ->
node = nodes[n]
if node.attributes.length > 0
[0..(node.attributes.length - 1)].forEach (n) ->
attribute = node.attributes[n]
if /^data-/.test attribute.name
type = attribute.name.replace 'data-', ''
path = attribute.value.split '.'
context = path.shift()
keypath = path.join '.'
registerBinding node, adapter, type, contexts[context], keypath
if module?
module.exports = rivets
else
@rivets = rivets