rivets.js
5.44 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
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
// Generated by CoffeeScript 1.3.1
(function() {
var Rivets, attributeBinding, bidirectionals, getInputValue, rivets, stateBinding,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__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; };
Rivets = {};
Rivets.Binding = (function() {
Binding.name = 'Binding';
function Binding(el, type, context, keypath) {
this.el = el;
this.type = type;
this.context = context;
this.keypath = keypath;
this.bind = __bind(this.bind, this);
this.set = __bind(this.set, this);
this.routine = Rivets.bindings[this.type] || attributeBinding(this.type);
}
Binding.prototype.set = function(value) {
if (value == null) {
value = null;
}
return this.routine(this.el, value || Rivets.config.adapter.read(this.context, this.keypath));
};
Binding.prototype.bind = function() {
var _ref,
_this = this;
if (Rivets.config.preloadData) {
this.set();
}
Rivets.config.adapter.subscribe(this.context, this.keypath, function(value) {
return _this.set(value);
});
if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
return this.el.addEventListener('change', function(el) {
return Rivets.config.adapter.publish(_this.context, _this.keypath, getInputValue(el));
});
}
};
return Binding;
})();
Rivets.View = (function() {
View.name = 'View';
function View(el, contexts) {
this.el = el;
this.contexts = contexts;
this.bind = __bind(this.bind, this);
this.build = __bind(this.build, this);
this.build();
}
View.prototype.build = function() {
var attribute, context, keypath, node, path, type, _i, _len, _ref, _results;
this.bindings = [];
_ref = this.el.getElementsByTagName('*');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
node = _ref[_i];
_results.push((function() {
var _j, _len1, _ref1, _results1;
_ref1 = node.attributes;
_results1 = [];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
attribute = _ref1[_j];
if (/^data-/.test(attribute.name)) {
type = attribute.name.replace('data-', '');
path = attribute.value.split('.');
context = this.contexts[path.shift()];
keypath = path.join('.');
_results1.push(this.bindings.push(new Rivets.Binding(node, type, context, keypath)));
} else {
_results1.push(void 0);
}
}
return _results1;
}).call(this));
}
return _results;
};
View.prototype.bind = function() {
var binding, _i, _len, _ref, _results;
_ref = this.bindings;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
_results.push(binding.bind());
}
return _results;
};
return View;
})();
getInputValue = function(el) {
switch (el.type) {
case 'text':
case 'textarea':
case 'password':
case 'select-one':
return el.value;
case 'checkbox':
case 'radio':
return el.checked;
}
};
attributeBinding = function(attr) {
return function(el, value) {
if (value) {
return el.setAttribute(attr, value);
} else {
return el.removeAttribute(attr);
}
};
};
stateBinding = function(attr, inverse) {
if (inverse == null) {
inverse = false;
}
return function(el, value) {
var binding;
binding = attributeBinding(attr);
return binding(el, inverse === !value ? attr : false);
};
};
bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
Rivets.bindings = {
checked: stateBinding('checked'),
selected: stateBinding('selected'),
disabled: stateBinding('disabled'),
unchecked: stateBinding('checked', true),
unselected: stateBinding('selected', true),
enabled: stateBinding('disabled', true),
text: function(el, value) {
return el.innerText = value || '';
},
html: function(el, value) {
return el.innerHTML = value || '';
},
value: function(el, value) {
return el.value = value;
},
show: function(el, value) {
return el.style.display = value ? '' : 'none';
},
hide: function(el, value) {
return el.style.display = value ? 'none' : '';
}
};
Rivets.config = {
preloadData: true
};
rivets = {
configure: function(options) {
var property, value, _results;
if (options == null) {
options = {};
}
_results = [];
for (property in options) {
value = options[property];
_results.push(Rivets.config[property] = value);
}
return _results;
},
register: function(routine, routineFunction) {
return Rivets.bindings[routine] = routineFunction;
},
bind: function(el, contexts) {
var view;
if (contexts == null) {
contexts = {};
}
view = new Rivets.View(el, contexts);
view.bind();
return view;
}
};
if (typeof module !== "undefined" && module !== null) {
module.exports = rivets;
} else {
this.rivets = rivets;
}
}).call(this);