rivets.js
3.83 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
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
// Generated by CoffeeScript 1.3.1
(function() {
var attributeBinding, bidirectionalBindings, bindings, getInputValue, registerBinding, rivets, setAttribute,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
registerBinding = function(el, adapter, type, context, keypath) {
var bind;
bind = bindings[type] || attributeBinding(type);
bind(el, adapter.read(context, keypath));
adapter.subscribe(context, keypath, function(value) {
return bind(el, value);
});
if (__indexOf.call(bidirectionalBindings, type) >= 0) {
return el.addEventListener('change', function() {
return adapter.publish(context, keypath, getInputValue(this));
});
}
};
setAttribute = function(el, attr, value, mirrored) {
if (mirrored == null) {
mirrored = false;
}
if (value) {
return el.setAttribute(attr, mirrored ? attr : value);
} else {
return el.removeAttribute(attr);
}
};
getInputValue = function(el) {
switch (el.type) {
case 'text':
case 'textarea':
case 'password':
case 'select-one':
return el.value;
case 'checkbox':
return el.checked;
}
};
bindings = {
show: function(el, value) {
return el.style.display = value ? '' : 'none';
},
hide: function(el, value) {
return el.style.display = value ? 'none' : '';
},
enabled: function(el, value) {
return setAttribute(el, 'disabled', !value, true);
},
disabled: function(el, value) {
return setAttribute(el, 'disabled', value, true);
},
checked: function(el, value) {
return setAttribute(el, 'checked', value, true);
},
unchecked: function(el, value) {
return setAttribute(el, 'checked', !value, true);
},
selected: function(el, value) {
return setAttribute(el, 'selected', value, true);
},
unselected: function(el, value) {
return setAttribute(el, 'checked', !value, true);
},
text: function(el, value) {
return el.innerHTML = value || '';
},
value: function(el, value) {
return el.value = value;
}
};
attributeBinding = function(attr) {
return function(el, value) {
return setAttribute(el, attr, value);
};
};
bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
rivets = {
bind: function(el, adapter, contexts) {
var nodes, _i, _ref, _results;
if (contexts == null) {
contexts = {};
}
nodes = el.getElementsByTagName('*');
return (function() {
_results = [];
for (var _i = 0, _ref = nodes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this).forEach(function(n) {
var node, _i, _ref, _results;
node = nodes[n];
if (node.attributes.length > 0) {
return (function() {
_results = [];
for (var _i = 0, _ref = node.attributes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this).forEach(function(n) {
var attribute, context, keypath, path, type;
attribute = node.attributes[n];
if (/^data-/.test(attribute.name)) {
type = attribute.name.replace('data-', '');
path = attribute.value.split('.');
context = path.shift();
keypath = path.join('.');
return registerBinding(node, adapter, type, contexts[context], keypath);
}
});
}
});
}
};
if (typeof module !== "undefined" && module !== null) {
module.exports = rivets;
} else {
this.rivets = rivets;
}
}).call(this);