Build 0.6.0.
Showing
4 changed files
with
857 additions
and
638 deletions
1 | { | 1 | { |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "repo": "mikeric/rivets", | 3 | "repo": "mikeric/rivets", |
4 | "description": "Declarative data binding facility.", | 4 | "description": "Declarative data binding + templating solution.", |
5 | "version": "0.5.13", | 5 | "version": "0.6.0", |
6 | "keywords": ["data binding", "templating"], | 6 | "keywords": ["data binding", "templating"], |
7 | "scripts": ["dist/rivets.js"], | 7 | "scripts": ["dist/rivets.js"], |
8 | "main": "dist/rivets.js", | 8 | "main": "dist/rivets.js", | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.5.13 | 2 | // version: 0.6.0 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
6 | var Rivets, jQuery, | 6 | var KeypathObserver, Rivets, |
7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | 7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, |
8 | __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; }, | ||
8 | __slice = [].slice, | 9 | __slice = [].slice, |
9 | __hasProp = {}.hasOwnProperty, | 10 | __hasProp = {}.hasOwnProperty, |
10 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | 11 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; |
11 | __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; }; | ||
12 | 12 | ||
13 | Rivets = {}; | 13 | Rivets = { |
14 | 14 | binders: {}, | |
15 | jQuery = window.jQuery || window.Zepto; | 15 | components: {}, |
16 | 16 | formatters: {}, | |
17 | if (!String.prototype.trim) { | 17 | adapters: {}, |
18 | String.prototype.trim = function() { | 18 | config: { |
19 | return this.replace(/^\s+|\s+$/g, ''); | 19 | prefix: 'rv', |
20 | }; | 20 | templateDelimiters: ['{', '}'], |
21 | } | 21 | rootInterface: '.', |
22 | 22 | preloadData: true, | |
23 | Rivets.Binding = (function() { | 23 | handler: function(context, ev, binding) { |
24 | function Binding(view, el, type, key, keypath, options) { | 24 | return this.call(context, ev, binding.view.models); |
25 | var identifier, regexp, value, _ref; | ||
26 | this.view = view; | ||
27 | this.el = el; | ||
28 | this.type = type; | ||
29 | this.key = key; | ||
30 | this.keypath = keypath; | ||
31 | this.options = options != null ? options : {}; | ||
32 | this.update = __bind(this.update, this); | ||
33 | this.unbind = __bind(this.unbind, this); | ||
34 | this.bind = __bind(this.bind, this); | ||
35 | this.publish = __bind(this.publish, this); | ||
36 | this.sync = __bind(this.sync, this); | ||
37 | this.set = __bind(this.set, this); | ||
38 | this.eventHandler = __bind(this.eventHandler, this); | ||
39 | this.formattedValue = __bind(this.formattedValue, this); | ||
40 | if (!(this.binder = this.view.binders[type])) { | ||
41 | _ref = this.view.binders; | ||
42 | for (identifier in _ref) { | ||
43 | value = _ref[identifier]; | ||
44 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | ||
45 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | ||
46 | if (regexp.test(type)) { | ||
47 | this.binder = value; | ||
48 | this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(type); | ||
49 | this.args.shift(); | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | this.binder || (this.binder = this.view.binders['*']); | ||
55 | if (this.binder instanceof Function) { | ||
56 | this.binder = { | ||
57 | routine: this.binder | ||
58 | }; | ||
59 | } | 25 | } |
60 | this.formatters = this.options.formatters || []; | ||
61 | this.model = this.key ? this.view.models[this.key] : this.view.models; | ||
62 | } | 26 | } |
27 | }; | ||
63 | 28 | ||
64 | Binding.prototype.formattedValue = function(value) { | 29 | Rivets.Util = { |
65 | var args, formatter, id, _i, _len, _ref; | 30 | bindEvent: function(el, event, handler) { |
66 | _ref = this.formatters; | 31 | if (window.jQuery != null) { |
67 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 32 | el = jQuery(el); |
68 | formatter = _ref[_i]; | 33 | if (el.on != null) { |
69 | args = formatter.split(/\s+/); | 34 | return el.on(event, handler); |
70 | id = args.shift(); | 35 | } else { |
71 | formatter = this.model[id] instanceof Function ? this.model[id] : this.view.formatters[id]; | 36 | return el.bind(event, handler); |
72 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { | ||
73 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); | ||
74 | } else if (formatter instanceof Function) { | ||
75 | value = formatter.apply(null, [value].concat(__slice.call(args))); | ||
76 | } | ||
77 | } | ||
78 | return value; | ||
79 | }; | ||
80 | |||
81 | Binding.prototype.eventHandler = function(fn) { | ||
82 | var binding, handler; | ||
83 | handler = (binding = this).view.config.handler; | ||
84 | return function(ev) { | ||
85 | return handler.call(fn, this, ev, binding); | ||
86 | }; | ||
87 | }; | ||
88 | |||
89 | Binding.prototype.set = function(value) { | ||
90 | var _ref; | ||
91 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); | ||
92 | return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0; | ||
93 | }; | ||
94 | |||
95 | Binding.prototype.sync = function() { | ||
96 | return this.set(this.options.bypass ? this.model[this.keypath] : this.view.config.adapter.read(this.model, this.keypath)); | ||
97 | }; | ||
98 | |||
99 | Binding.prototype.publish = function() { | ||
100 | var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2; | ||
101 | value = Rivets.Util.getInputValue(this.el); | ||
102 | _ref = this.formatters.slice(0).reverse(); | ||
103 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
104 | formatter = _ref[_i]; | ||
105 | args = formatter.split(/\s+/); | ||
106 | id = args.shift(); | ||
107 | if ((_ref1 = this.view.formatters[id]) != null ? _ref1.publish : void 0) { | ||
108 | value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); | ||
109 | } | ||
110 | } | ||
111 | return this.view.config.adapter.publish(this.model, this.keypath, value); | ||
112 | }; | ||
113 | |||
114 | Binding.prototype.bind = function() { | ||
115 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | ||
116 | if ((_ref = this.binder.bind) != null) { | ||
117 | _ref.call(this, this.el); | ||
118 | } | ||
119 | if (this.options.bypass) { | ||
120 | this.sync(); | ||
121 | } else { | ||
122 | this.view.config.adapter.subscribe(this.model, this.keypath, this.sync); | ||
123 | if (this.view.config.preloadData) { | ||
124 | this.sync(); | ||
125 | } | ||
126 | } | ||
127 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | ||
128 | _ref2 = this.options.dependencies; | ||
129 | _results = []; | ||
130 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
131 | dependency = _ref2[_i]; | ||
132 | if (/^\./.test(dependency)) { | ||
133 | model = this.model; | ||
134 | keypath = dependency.substr(1); | ||
135 | } else { | ||
136 | dependency = dependency.split('.'); | ||
137 | model = this.view.models[dependency.shift()]; | ||
138 | keypath = dependency.join('.'); | ||
139 | } | ||
140 | _results.push(this.view.config.adapter.subscribe(model, keypath, this.sync)); | ||
141 | } | ||
142 | return _results; | ||
143 | } | ||
144 | }; | ||
145 | |||
146 | Binding.prototype.unbind = function() { | ||
147 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | ||
148 | if ((_ref = this.binder.unbind) != null) { | ||
149 | _ref.call(this, this.el); | ||
150 | } | ||
151 | if (!this.options.bypass) { | ||
152 | this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | ||
153 | } | ||
154 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | ||
155 | _ref2 = this.options.dependencies; | ||
156 | _results = []; | ||
157 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
158 | dependency = _ref2[_i]; | ||
159 | if (/^\./.test(dependency)) { | ||
160 | model = this.model; | ||
161 | keypath = dependency.substr(1); | ||
162 | } else { | ||
163 | dependency = dependency.split('.'); | ||
164 | model = this.view.models[dependency.shift()]; | ||
165 | keypath = dependency.join('.'); | ||
166 | } | ||
167 | _results.push(this.view.config.adapter.unsubscribe(model, keypath, this.sync)); | ||
168 | } | ||
169 | return _results; | ||
170 | } | ||
171 | }; | ||
172 | |||
173 | Binding.prototype.update = function(models) { | ||
174 | var _ref; | ||
175 | if (models == null) { | ||
176 | models = {}; | ||
177 | } | ||
178 | if (this.key) { | ||
179 | if (models[this.key]) { | ||
180 | if (!this.options.bypass) { | ||
181 | this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | ||
182 | } | ||
183 | this.model = models[this.key]; | ||
184 | if (this.options.bypass) { | ||
185 | this.sync(); | ||
186 | } else { | ||
187 | this.view.config.adapter.subscribe(this.model, this.keypath, this.sync); | ||
188 | if (this.view.config.preloadData) { | ||
189 | this.sync(); | ||
190 | } | ||
191 | } | ||
192 | } | 37 | } |
38 | } else if (window.addEventListener != null) { | ||
39 | return el.addEventListener(event, handler, false); | ||
193 | } else { | 40 | } else { |
194 | this.sync(); | 41 | event = 'on' + event; |
42 | return el.attachEvent(event, handler); | ||
195 | } | 43 | } |
196 | return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; | 44 | }, |
197 | }; | 45 | unbindEvent: function(el, event, handler) { |
198 | 46 | if (window.jQuery != null) { | |
199 | return Binding; | 47 | el = jQuery(el); |
200 | 48 | if (el.off != null) { | |
201 | })(); | 49 | return el.off(event, handler); |
202 | |||
203 | Rivets.ComponentBinding = (function(_super) { | ||
204 | __extends(ComponentBinding, _super); | ||
205 | |||
206 | function ComponentBinding(view, el, type) { | ||
207 | var attribute, _i, _len, _ref, _ref1; | ||
208 | this.view = view; | ||
209 | this.el = el; | ||
210 | this.type = type; | ||
211 | this.unbind = __bind(this.unbind, this); | ||
212 | this.bind = __bind(this.bind, this); | ||
213 | this.update = __bind(this.update, this); | ||
214 | this.locals = __bind(this.locals, this); | ||
215 | this.component = Rivets.components[this.type]; | ||
216 | this.attributes = {}; | ||
217 | this.inflections = {}; | ||
218 | _ref = this.el.attributes || []; | ||
219 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
220 | attribute = _ref[_i]; | ||
221 | if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { | ||
222 | this.attributes[attribute.name] = attribute.value; | ||
223 | } else { | 50 | } else { |
224 | this.inflections[attribute.name] = attribute.value; | 51 | return el.unbind(event, handler); |
225 | } | ||
226 | } | ||
227 | } | ||
228 | |||
229 | ComponentBinding.prototype.sync = function() {}; | ||
230 | |||
231 | ComponentBinding.prototype.locals = function(models) { | ||
232 | var inverse, key, model, path, result, _i, _len, _ref, _ref1; | ||
233 | if (models == null) { | ||
234 | models = this.view.models; | ||
235 | } | ||
236 | result = {}; | ||
237 | _ref = this.inflections; | ||
238 | for (key in _ref) { | ||
239 | inverse = _ref[key]; | ||
240 | _ref1 = inverse.split('.'); | ||
241 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
242 | path = _ref1[_i]; | ||
243 | result[key] = (result[key] || models)[path]; | ||
244 | } | 52 | } |
53 | } else if (window.removeEventListener != null) { | ||
54 | return el.removeEventListener(event, handler, false); | ||
55 | } else { | ||
56 | event = 'on' + event; | ||
57 | return el.detachEvent(event, handler); | ||
245 | } | 58 | } |
246 | for (key in models) { | 59 | }, |
247 | model = models[key]; | 60 | getInputValue: function(el) { |
248 | if (result[key] == null) { | 61 | var o, _i, _len, _results; |
249 | result[key] = model; | 62 | if (window.jQuery != null) { |
63 | el = jQuery(el); | ||
64 | switch (el[0].type) { | ||
65 | case 'checkbox': | ||
66 | return el.is(':checked'); | ||
67 | default: | ||
68 | return el.val(); | ||
250 | } | 69 | } |
251 | } | ||
252 | return result; | ||
253 | }; | ||
254 | |||
255 | ComponentBinding.prototype.update = function(models) { | ||
256 | var _ref; | ||
257 | return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; | ||
258 | }; | ||
259 | |||
260 | ComponentBinding.prototype.bind = function() { | ||
261 | var el, _ref; | ||
262 | if (this.componentView != null) { | ||
263 | return (_ref = this.componentView) != null ? _ref.bind() : void 0; | ||
264 | } else { | 70 | } else { |
265 | el = this.component.build.call(this.attributes); | 71 | switch (el.type) { |
266 | (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); | 72 | case 'checkbox': |
267 | return this.el.parentNode.replaceChild(el, this.el); | 73 | return el.checked; |
268 | } | 74 | case 'select-multiple': |
269 | }; | 75 | _results = []; |
270 | 76 | for (_i = 0, _len = el.length; _i < _len; _i++) { | |
271 | ComponentBinding.prototype.unbind = function() { | 77 | o = el[_i]; |
272 | var _ref; | 78 | if (o.selected) { |
273 | return (_ref = this.componentView) != null ? _ref.unbind() : void 0; | 79 | _results.push(o.value); |
274 | }; | 80 | } |
275 | 81 | } | |
276 | return ComponentBinding; | 82 | return _results; |
277 | 83 | break; | |
278 | })(Rivets.Binding); | 84 | default: |
279 | 85 | return el.value; | |
280 | Rivets.TextBinding = (function(_super) { | 86 | } |
281 | __extends(TextBinding, _super); | 87 | } |
282 | 88 | } | |
283 | function TextBinding(view, el, type, key, keypath, options) { | 89 | }; |
284 | this.view = view; | ||
285 | this.el = el; | ||
286 | this.type = type; | ||
287 | this.key = key; | ||
288 | this.keypath = keypath; | ||
289 | this.options = options != null ? options : {}; | ||
290 | this.sync = __bind(this.sync, this); | ||
291 | this.formatters = this.options.formatters || []; | ||
292 | this.model = this.key ? this.view.models[this.key] : this.view.models; | ||
293 | } | ||
294 | |||
295 | TextBinding.prototype.binder = { | ||
296 | routine: function(node, value) { | ||
297 | return node.data = value != null ? value : ''; | ||
298 | } | ||
299 | }; | ||
300 | |||
301 | TextBinding.prototype.sync = function() { | ||
302 | return TextBinding.__super__.sync.apply(this, arguments); | ||
303 | }; | ||
304 | |||
305 | return TextBinding; | ||
306 | |||
307 | })(Rivets.Binding); | ||
308 | 90 | ||
309 | Rivets.View = (function() { | 91 | Rivets.View = (function() { |
310 | function View(els, models, options) { | 92 | function View(els, models, options) { |
... | @@ -321,10 +103,10 @@ | ... | @@ -321,10 +103,10 @@ |
321 | this.build = __bind(this.build, this); | 103 | this.build = __bind(this.build, this); |
322 | this.componentRegExp = __bind(this.componentRegExp, this); | 104 | this.componentRegExp = __bind(this.componentRegExp, this); |
323 | this.bindingRegExp = __bind(this.bindingRegExp, this); | 105 | this.bindingRegExp = __bind(this.bindingRegExp, this); |
324 | if (typeof this.els.length === 'undefined') { | 106 | if (!(this.els.jquery || this.els instanceof Array)) { |
325 | this.els = [this.els]; | 107 | this.els = [this.els]; |
326 | } | 108 | } |
327 | _ref = ['config', 'binders', 'formatters']; | 109 | _ref = ['config', 'binders', 'formatters', 'adapters']; |
328 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 110 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
329 | option = _ref[_i]; | 111 | option = _ref[_i]; |
330 | this[option] = {}; | 112 | this[option] = {}; |
... | @@ -347,18 +129,11 @@ | ... | @@ -347,18 +129,11 @@ |
347 | } | 129 | } |
348 | 130 | ||
349 | View.prototype.bindingRegExp = function() { | 131 | View.prototype.bindingRegExp = function() { |
350 | var prefix; | 132 | return new RegExp("^" + this.config.prefix + "-"); |
351 | prefix = this.config.prefix; | ||
352 | if (prefix) { | ||
353 | return new RegExp("^data-" + prefix + "-"); | ||
354 | } else { | ||
355 | return /^data-/; | ||
356 | } | ||
357 | }; | 133 | }; |
358 | 134 | ||
359 | View.prototype.componentRegExp = function() { | 135 | View.prototype.componentRegExp = function() { |
360 | var _ref, _ref1; | 136 | return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); |
361 | return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-"); | ||
362 | }; | 137 | }; |
363 | 138 | ||
364 | View.prototype.build = function() { | 139 | View.prototype.build = function() { |
... | @@ -369,7 +144,7 @@ | ... | @@ -369,7 +144,7 @@ |
369 | bindingRegExp = this.bindingRegExp(); | 144 | bindingRegExp = this.bindingRegExp(); |
370 | componentRegExp = this.componentRegExp(); | 145 | componentRegExp = this.componentRegExp(); |
371 | buildBinding = function(binding, node, type, declaration) { | 146 | buildBinding = function(binding, node, type, declaration) { |
372 | var context, ctx, dependencies, key, keypath, options, path, pipe, pipes, splitPath; | 147 | var context, ctx, dependencies, keypath, options, pipe, pipes; |
373 | options = {}; | 148 | options = {}; |
374 | pipes = (function() { | 149 | pipes = (function() { |
375 | var _i, _len, _ref, _results; | 150 | var _i, _len, _ref, _results; |
... | @@ -391,21 +166,12 @@ | ... | @@ -391,21 +166,12 @@ |
391 | } | 166 | } |
392 | return _results; | 167 | return _results; |
393 | })(); | 168 | })(); |
394 | path = context.shift(); | 169 | keypath = context.shift(); |
395 | splitPath = path.split(/\.|:/); | ||
396 | options.formatters = pipes; | 170 | options.formatters = pipes; |
397 | options.bypass = path.indexOf(':') !== -1; | ||
398 | if (splitPath[0]) { | ||
399 | key = splitPath.shift(); | ||
400 | } else { | ||
401 | key = null; | ||
402 | splitPath.shift(); | ||
403 | } | ||
404 | keypath = splitPath.join('.'); | ||
405 | if (dependencies = context.shift()) { | 171 | if (dependencies = context.shift()) { |
406 | options.dependencies = dependencies.split(/\s+/); | 172 | options.dependencies = dependencies.split(/\s+/); |
407 | } | 173 | } |
408 | return _this.bindings.push(new Rivets[binding](_this, node, type, key, keypath, options)); | 174 | return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); |
409 | }; | 175 | }; |
410 | parse = function(node) { | 176 | parse = function(node) { |
411 | var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, restTokens, startToken, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results; | 177 | var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, restTokens, startToken, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results; |
... | @@ -522,52 +288,374 @@ | ... | @@ -522,52 +288,374 @@ |
522 | binding = _ref[_i]; | 288 | binding = _ref[_i]; |
523 | _results.push(binding.unbind()); | 289 | _results.push(binding.unbind()); |
524 | } | 290 | } |
525 | return _results; | 291 | return _results; |
292 | }; | ||
293 | |||
294 | View.prototype.sync = function() { | ||
295 | var binding, _i, _len, _ref, _results; | ||
296 | _ref = this.bindings; | ||
297 | _results = []; | ||
298 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
299 | binding = _ref[_i]; | ||
300 | _results.push(binding.sync()); | ||
301 | } | ||
302 | return _results; | ||
303 | }; | ||
304 | |||
305 | View.prototype.publish = function() { | ||
306 | var binding, _i, _len, _ref, _results; | ||
307 | _ref = this.select(function(b) { | ||
308 | return b.binder.publishes; | ||
309 | }); | ||
310 | _results = []; | ||
311 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
312 | binding = _ref[_i]; | ||
313 | _results.push(binding.publish()); | ||
314 | } | ||
315 | return _results; | ||
316 | }; | ||
317 | |||
318 | View.prototype.update = function(models) { | ||
319 | var binding, key, model, _i, _len, _ref, _results; | ||
320 | if (models == null) { | ||
321 | models = {}; | ||
322 | } | ||
323 | for (key in models) { | ||
324 | model = models[key]; | ||
325 | this.models[key] = model; | ||
326 | } | ||
327 | _ref = this.bindings; | ||
328 | _results = []; | ||
329 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
330 | binding = _ref[_i]; | ||
331 | _results.push(binding.update(models)); | ||
332 | } | ||
333 | return _results; | ||
334 | }; | ||
335 | |||
336 | return View; | ||
337 | |||
338 | })(); | ||
339 | |||
340 | Rivets.Binding = (function() { | ||
341 | function Binding(view, el, type, keypath, options) { | ||
342 | this.view = view; | ||
343 | this.el = el; | ||
344 | this.type = type; | ||
345 | this.keypath = keypath; | ||
346 | this.options = options != null ? options : {}; | ||
347 | this.update = __bind(this.update, this); | ||
348 | this.unbind = __bind(this.unbind, this); | ||
349 | this.bind = __bind(this.bind, this); | ||
350 | this.publish = __bind(this.publish, this); | ||
351 | this.sync = __bind(this.sync, this); | ||
352 | this.set = __bind(this.set, this); | ||
353 | this.eventHandler = __bind(this.eventHandler, this); | ||
354 | this.formattedValue = __bind(this.formattedValue, this); | ||
355 | this.setObserver = __bind(this.setObserver, this); | ||
356 | this.setBinder = __bind(this.setBinder, this); | ||
357 | this.formatters = this.options.formatters || []; | ||
358 | this.dependencies = []; | ||
359 | this.setBinder(); | ||
360 | this.setObserver(); | ||
361 | } | ||
362 | |||
363 | Binding.prototype.setBinder = function() { | ||
364 | var identifier, regexp, value, _ref; | ||
365 | if (!(this.binder = this.view.binders[this.type])) { | ||
366 | _ref = this.view.binders; | ||
367 | for (identifier in _ref) { | ||
368 | value = _ref[identifier]; | ||
369 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | ||
370 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | ||
371 | if (regexp.test(this.type)) { | ||
372 | this.binder = value; | ||
373 | this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); | ||
374 | this.args.shift(); | ||
375 | } | ||
376 | } | ||
377 | } | ||
378 | } | ||
379 | this.binder || (this.binder = this.view.binders['*']); | ||
380 | if (this.binder instanceof Function) { | ||
381 | return this.binder = { | ||
382 | routine: this.binder | ||
383 | }; | ||
384 | } | ||
385 | }; | ||
386 | |||
387 | Binding.prototype.setObserver = function() { | ||
388 | var _this = this; | ||
389 | this.observer = new KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { | ||
390 | if (_this.key) { | ||
391 | _this.unbind(true); | ||
392 | } | ||
393 | _this.model = obs.target; | ||
394 | if (_this.key) { | ||
395 | _this.bind(true); | ||
396 | } | ||
397 | return _this.sync(); | ||
398 | }); | ||
399 | this.key = this.observer.key; | ||
400 | return this.model = this.observer.target; | ||
401 | }; | ||
402 | |||
403 | Binding.prototype.formattedValue = function(value) { | ||
404 | var args, formatter, id, _i, _len, _ref; | ||
405 | _ref = this.formatters; | ||
406 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
407 | formatter = _ref[_i]; | ||
408 | args = formatter.split(/\s+/); | ||
409 | id = args.shift(); | ||
410 | formatter = this.view.formatters[id]; | ||
411 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { | ||
412 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); | ||
413 | } else if (formatter instanceof Function) { | ||
414 | value = formatter.apply(null, [value].concat(__slice.call(args))); | ||
415 | } | ||
416 | } | ||
417 | return value; | ||
418 | }; | ||
419 | |||
420 | Binding.prototype.eventHandler = function(fn) { | ||
421 | var binding, handler; | ||
422 | handler = (binding = this).view.config.handler; | ||
423 | return function(ev) { | ||
424 | return handler.call(fn, this, ev, binding); | ||
425 | }; | ||
426 | }; | ||
427 | |||
428 | Binding.prototype.set = function(value) { | ||
429 | var _ref; | ||
430 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); | ||
431 | return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0; | ||
432 | }; | ||
433 | |||
434 | Binding.prototype.sync = function() { | ||
435 | return this.set(this.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); | ||
436 | }; | ||
437 | |||
438 | Binding.prototype.publish = function() { | ||
439 | var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2; | ||
440 | value = Rivets.Util.getInputValue(this.el); | ||
441 | _ref = this.formatters.slice(0).reverse(); | ||
442 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
443 | formatter = _ref[_i]; | ||
444 | args = formatter.split(/\s+/); | ||
445 | id = args.shift(); | ||
446 | if ((_ref1 = this.view.formatters[id]) != null ? _ref1.publish : void 0) { | ||
447 | value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); | ||
448 | } | ||
449 | } | ||
450 | return this.view.adapters[this.key["interface"]].publish(this.model, this.key.path, value); | ||
451 | }; | ||
452 | |||
453 | Binding.prototype.bind = function(silent) { | ||
454 | var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, | ||
455 | _this = this; | ||
456 | if (silent == null) { | ||
457 | silent = false; | ||
458 | } | ||
459 | if (!silent) { | ||
460 | if ((_ref = this.binder.bind) != null) { | ||
461 | _ref.call(this, this.el); | ||
462 | } | ||
463 | } | ||
464 | if (this.key) { | ||
465 | this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); | ||
466 | } | ||
467 | if (!silent ? this.view.config.preloadData : void 0) { | ||
468 | this.sync(); | ||
469 | } | ||
470 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | ||
471 | _ref2 = this.options.dependencies; | ||
472 | _results = []; | ||
473 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
474 | dependency = _ref2[_i]; | ||
475 | observer = new KeypathObserver(this.view, this.model, dependency, function(obs, prev) { | ||
476 | var key; | ||
477 | key = obs.key; | ||
478 | _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); | ||
479 | _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); | ||
480 | return _this.sync(); | ||
481 | }); | ||
482 | key = observer.key; | ||
483 | this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); | ||
484 | _results.push(this.dependencies.push(observer)); | ||
485 | } | ||
486 | return _results; | ||
487 | } | ||
488 | }; | ||
489 | |||
490 | Binding.prototype.unbind = function(silent) { | ||
491 | var key, obs, _i, _len, _ref, _ref1; | ||
492 | if (silent == null) { | ||
493 | silent = false; | ||
494 | } | ||
495 | if (!silent) { | ||
496 | if ((_ref = this.binder.unbind) != null) { | ||
497 | _ref.call(this, this.el); | ||
498 | } | ||
499 | } | ||
500 | if (this.key) { | ||
501 | this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); | ||
502 | } | ||
503 | if (this.dependencies.length) { | ||
504 | _ref1 = this.dependencies; | ||
505 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
506 | obs = _ref1[_i]; | ||
507 | key = obs.key; | ||
508 | this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); | ||
509 | } | ||
510 | return this.dependencies = []; | ||
511 | } | ||
512 | }; | ||
513 | |||
514 | Binding.prototype.update = function(models) { | ||
515 | var _ref; | ||
516 | if (models == null) { | ||
517 | models = {}; | ||
518 | } | ||
519 | return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; | ||
520 | }; | ||
521 | |||
522 | return Binding; | ||
523 | |||
524 | })(); | ||
525 | |||
526 | Rivets.ComponentBinding = (function(_super) { | ||
527 | __extends(ComponentBinding, _super); | ||
528 | |||
529 | function ComponentBinding(view, el, type) { | ||
530 | var attribute, _i, _len, _ref, _ref1; | ||
531 | this.view = view; | ||
532 | this.el = el; | ||
533 | this.type = type; | ||
534 | this.unbind = __bind(this.unbind, this); | ||
535 | this.bind = __bind(this.bind, this); | ||
536 | this.update = __bind(this.update, this); | ||
537 | this.locals = __bind(this.locals, this); | ||
538 | this.component = Rivets.components[this.type]; | ||
539 | this.attributes = {}; | ||
540 | this.inflections = {}; | ||
541 | _ref = this.el.attributes || []; | ||
542 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
543 | attribute = _ref[_i]; | ||
544 | if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { | ||
545 | this.attributes[attribute.name] = attribute.value; | ||
546 | } else { | ||
547 | this.inflections[attribute.name] = attribute.value; | ||
548 | } | ||
549 | } | ||
550 | } | ||
551 | |||
552 | ComponentBinding.prototype.sync = function() {}; | ||
553 | |||
554 | ComponentBinding.prototype.locals = function(models) { | ||
555 | var inverse, key, model, path, result, _i, _len, _ref, _ref1; | ||
556 | if (models == null) { | ||
557 | models = this.view.models; | ||
558 | } | ||
559 | result = {}; | ||
560 | _ref = this.inflections; | ||
561 | for (key in _ref) { | ||
562 | inverse = _ref[key]; | ||
563 | _ref1 = inverse.split('.'); | ||
564 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
565 | path = _ref1[_i]; | ||
566 | result[key] = (result[key] || models)[path]; | ||
567 | } | ||
568 | } | ||
569 | for (key in models) { | ||
570 | model = models[key]; | ||
571 | if (result[key] == null) { | ||
572 | result[key] = model; | ||
573 | } | ||
574 | } | ||
575 | return result; | ||
576 | }; | ||
577 | |||
578 | ComponentBinding.prototype.update = function(models) { | ||
579 | var _ref; | ||
580 | return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; | ||
581 | }; | ||
582 | |||
583 | ComponentBinding.prototype.bind = function() { | ||
584 | var el, _ref; | ||
585 | if (this.componentView != null) { | ||
586 | return (_ref = this.componentView) != null ? _ref.bind() : void 0; | ||
587 | } else { | ||
588 | el = this.component.build.call(this.attributes); | ||
589 | (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); | ||
590 | return this.el.parentNode.replaceChild(el, this.el); | ||
591 | } | ||
526 | }; | 592 | }; |
527 | 593 | ||
528 | View.prototype.sync = function() { | 594 | ComponentBinding.prototype.unbind = function() { |
529 | var binding, _i, _len, _ref, _results; | 595 | var _ref; |
530 | _ref = this.bindings; | 596 | return (_ref = this.componentView) != null ? _ref.unbind() : void 0; |
531 | _results = []; | ||
532 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
533 | binding = _ref[_i]; | ||
534 | _results.push(binding.sync()); | ||
535 | } | ||
536 | return _results; | ||
537 | }; | 597 | }; |
538 | 598 | ||
539 | View.prototype.publish = function() { | 599 | return ComponentBinding; |
540 | var binding, _i, _len, _ref, _results; | 600 | |
541 | _ref = this.select(function(b) { | 601 | })(Rivets.Binding); |
542 | return b.binder.publishes; | 602 | |
543 | }); | 603 | Rivets.TextBinding = (function(_super) { |
544 | _results = []; | 604 | __extends(TextBinding, _super); |
545 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 605 | |
546 | binding = _ref[_i]; | 606 | function TextBinding(view, el, type, keypath, options) { |
547 | _results.push(binding.publish()); | 607 | this.view = view; |
608 | this.el = el; | ||
609 | this.type = type; | ||
610 | this.keypath = keypath; | ||
611 | this.options = options != null ? options : {}; | ||
612 | this.sync = __bind(this.sync, this); | ||
613 | this.formatters = this.options.formatters || []; | ||
614 | this.dependencies = []; | ||
615 | this.setObserver(); | ||
616 | } | ||
617 | |||
618 | TextBinding.prototype.binder = { | ||
619 | routine: function(node, value) { | ||
620 | return node.data = value != null ? value : ''; | ||
548 | } | 621 | } |
549 | return _results; | ||
550 | }; | 622 | }; |
551 | 623 | ||
552 | View.prototype.update = function(models) { | 624 | TextBinding.prototype.sync = function() { |
553 | var binding, key, model, _i, _len, _ref, _results; | 625 | return TextBinding.__super__.sync.apply(this, arguments); |
554 | if (models == null) { | 626 | }; |
555 | models = {}; | 627 | |
556 | } | 628 | return TextBinding; |
557 | for (key in models) { | 629 | |
558 | model = models[key]; | 630 | })(Rivets.Binding); |
559 | this.models[key] = model; | 631 | |
560 | } | 632 | Rivets.KeypathParser = (function() { |
561 | _ref = this.bindings; | 633 | function KeypathParser() {} |
562 | _results = []; | 634 | |
563 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 635 | KeypathParser.parse = function(keypath, interfaces, root) { |
564 | binding = _ref[_i]; | 636 | var char, current, index, tokens; |
565 | _results.push(binding.update(models)); | 637 | tokens = []; |
638 | current = { | ||
639 | "interface": root, | ||
640 | path: '' | ||
641 | }; | ||
642 | for (index in keypath) { | ||
643 | char = keypath[index]; | ||
644 | if (__indexOf.call(interfaces, char) >= 0) { | ||
645 | tokens.push(current); | ||
646 | current = { | ||
647 | "interface": char, | ||
648 | path: '' | ||
649 | }; | ||
650 | } else { | ||
651 | current.path += char; | ||
652 | } | ||
566 | } | 653 | } |
567 | return _results; | 654 | tokens.push(current); |
655 | return tokens; | ||
568 | }; | 656 | }; |
569 | 657 | ||
570 | return View; | 658 | return KeypathParser; |
571 | 659 | ||
572 | })(); | 660 | })(); |
573 | 661 | ||
... | @@ -630,357 +718,488 @@ | ... | @@ -630,357 +718,488 @@ |
630 | 718 | ||
631 | })(); | 719 | })(); |
632 | 720 | ||
633 | Rivets.Util = { | 721 | KeypathObserver = (function() { |
634 | bindEvent: function(el, event, handler) { | 722 | function KeypathObserver(view, model, keypath, callback) { |
635 | if (window.jQuery != null) { | 723 | this.view = view; |
636 | el = jQuery(el); | 724 | this.model = model; |
637 | if (el.on != null) { | 725 | this.keypath = keypath; |
638 | return el.on(event, handler); | 726 | this.callback = callback; |
639 | } else { | 727 | this.realize = __bind(this.realize, this); |
640 | return el.bind(event, handler); | 728 | this.update = __bind(this.update, this); |
729 | this.parse = __bind(this.parse, this); | ||
730 | this.parse(); | ||
731 | this.objectPath = []; | ||
732 | this.target = this.realize(); | ||
733 | } | ||
734 | |||
735 | KeypathObserver.prototype.parse = function() { | ||
736 | var interfaces, k, path, root, v, _ref; | ||
737 | interfaces = (function() { | ||
738 | var _ref, _results; | ||
739 | _ref = this.view.adapters; | ||
740 | _results = []; | ||
741 | for (k in _ref) { | ||
742 | v = _ref[k]; | ||
743 | _results.push(k); | ||
641 | } | 744 | } |
642 | } else if (window.addEventListener != null) { | 745 | return _results; |
643 | return el.addEventListener(event, handler, false); | 746 | }).call(this); |
747 | if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { | ||
748 | root = this.keypath[0]; | ||
749 | path = this.keypath.substr(1); | ||
644 | } else { | 750 | } else { |
645 | event = 'on' + event; | 751 | root = this.view.config.rootInterface; |
646 | return el.attachEvent(event, handler); | 752 | path = this.keypath; |
647 | } | 753 | } |
648 | }, | 754 | this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); |
649 | unbindEvent: function(el, event, handler) { | 755 | return this.key = this.tokens.pop(); |
650 | if (window.jQuery != null) { | 756 | }; |
651 | el = jQuery(el); | 757 | |
652 | if (el.off != null) { | 758 | KeypathObserver.prototype.update = function() { |
653 | return el.off(event, handler); | 759 | var next, prev; |
760 | if ((next = this.realize()) !== this.target) { | ||
761 | prev = this.target; | ||
762 | this.target = next; | ||
763 | return this.callback(this, prev); | ||
764 | } | ||
765 | }; | ||
766 | |||
767 | KeypathObserver.prototype.realize = function() { | ||
768 | var current, index, prev, token, _i, _len, _ref; | ||
769 | current = this.model; | ||
770 | _ref = this.tokens; | ||
771 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | ||
772 | token = _ref[index]; | ||
773 | if (this.objectPath[index] != null) { | ||
774 | if (current !== (prev = this.objectPath[index])) { | ||
775 | this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); | ||
776 | this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); | ||
777 | this.objectPath[index] = current; | ||
778 | } | ||
654 | } else { | 779 | } else { |
655 | return el.unbind(event, handler); | 780 | this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); |
781 | this.objectPath[index] = current; | ||
656 | } | 782 | } |
657 | } else if (window.removeEventListener != null) { | 783 | current = this.view.adapters[token["interface"]].read(current, token.path); |
658 | return el.removeEventListener(event, handler, false); | 784 | } |
785 | return current; | ||
786 | }; | ||
787 | |||
788 | return KeypathObserver; | ||
789 | |||
790 | })(); | ||
791 | |||
792 | Rivets.binders.enabled = function(el, value) { | ||
793 | return el.disabled = !value; | ||
794 | }; | ||
795 | |||
796 | Rivets.binders.disabled = function(el, value) { | ||
797 | return el.disabled = !!value; | ||
798 | }; | ||
799 | |||
800 | Rivets.binders.checked = { | ||
801 | publishes: true, | ||
802 | bind: function(el) { | ||
803 | return Rivets.Util.bindEvent(el, 'change', this.publish); | ||
804 | }, | ||
805 | unbind: function(el) { | ||
806 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | ||
807 | }, | ||
808 | routine: function(el, value) { | ||
809 | var _ref; | ||
810 | if (el.type === 'radio') { | ||
811 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0); | ||
659 | } else { | 812 | } else { |
660 | event = 'on' + event; | 813 | return el.checked = !!value; |
661 | return el.detachEvent(event, handler); | ||
662 | } | 814 | } |
815 | } | ||
816 | }; | ||
817 | |||
818 | Rivets.binders.unchecked = { | ||
819 | publishes: true, | ||
820 | bind: function(el) { | ||
821 | return Rivets.Util.bindEvent(el, 'change', this.publish); | ||
663 | }, | 822 | }, |
664 | getInputValue: function(el) { | 823 | unbind: function(el) { |
665 | var o, _i, _len, _results; | 824 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
825 | }, | ||
826 | routine: function(el, value) { | ||
827 | var _ref; | ||
828 | if (el.type === 'radio') { | ||
829 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0); | ||
830 | } else { | ||
831 | return el.checked = !value; | ||
832 | } | ||
833 | } | ||
834 | }; | ||
835 | |||
836 | Rivets.binders.show = function(el, value) { | ||
837 | return el.style.display = value ? '' : 'none'; | ||
838 | }; | ||
839 | |||
840 | Rivets.binders.hide = function(el, value) { | ||
841 | return el.style.display = value ? 'none' : ''; | ||
842 | }; | ||
843 | |||
844 | Rivets.binders.html = function(el, value) { | ||
845 | return el.innerHTML = value != null ? value : ''; | ||
846 | }; | ||
847 | |||
848 | Rivets.binders.value = { | ||
849 | publishes: true, | ||
850 | bind: function(el) { | ||
851 | return Rivets.Util.bindEvent(el, 'change', this.publish); | ||
852 | }, | ||
853 | unbind: function(el) { | ||
854 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | ||
855 | }, | ||
856 | routine: function(el, value) { | ||
857 | var o, _i, _len, _ref, _ref1, _ref2, _results; | ||
666 | if (window.jQuery != null) { | 858 | if (window.jQuery != null) { |
667 | el = jQuery(el); | 859 | el = jQuery(el); |
668 | switch (el[0].type) { | 860 | if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) { |
669 | case 'checkbox': | 861 | return el.val(value != null ? value : ''); |
670 | return el.is(':checked'); | ||
671 | default: | ||
672 | return el.val(); | ||
673 | } | 862 | } |
674 | } else { | 863 | } else { |
675 | switch (el.type) { | 864 | if (el.type === 'select-multiple') { |
676 | case 'checkbox': | 865 | if (value != null) { |
677 | return el.checked; | ||
678 | case 'select-multiple': | ||
679 | _results = []; | 866 | _results = []; |
680 | for (_i = 0, _len = el.length; _i < _len; _i++) { | 867 | for (_i = 0, _len = el.length; _i < _len; _i++) { |
681 | o = el[_i]; | 868 | o = el[_i]; |
682 | if (o.selected) { | 869 | _results.push(o.selected = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0)); |
683 | _results.push(o.value); | ||
684 | } | ||
685 | } | 870 | } |
686 | return _results; | 871 | return _results; |
687 | break; | 872 | } |
688 | default: | 873 | } else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.toString() : void 0)) { |
689 | return el.value; | 874 | return el.value = value != null ? value : ''; |
690 | } | 875 | } |
691 | } | 876 | } |
692 | } | 877 | } |
693 | }; | 878 | }; |
694 | 879 | ||
695 | Rivets.binders = { | 880 | Rivets.binders.text = function(el, value) { |
696 | enabled: function(el, value) { | 881 | if (el.innerText != null) { |
697 | return el.disabled = !value; | 882 | return el.innerText = value != null ? value : ''; |
698 | }, | 883 | } else { |
699 | disabled: function(el, value) { | 884 | return el.textContent = value != null ? value : ''; |
700 | return el.disabled = !!value; | 885 | } |
701 | }, | 886 | }; |
702 | checked: { | 887 | |
703 | publishes: true, | 888 | Rivets.binders["if"] = { |
704 | bind: function(el) { | 889 | block: true, |
705 | return Rivets.Util.bindEvent(el, 'change', this.publish); | 890 | bind: function(el) { |
706 | }, | 891 | var attr, declaration; |
707 | unbind: function(el) { | 892 | if (this.marker == null) { |
708 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | 893 | attr = [this.view.config.prefix, this.type].join('-').replace('--', '-'); |
709 | }, | 894 | declaration = el.getAttribute(attr); |
710 | routine: function(el, value) { | 895 | this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " "); |
711 | var _ref; | 896 | el.removeAttribute(attr); |
712 | if (el.type === 'radio') { | 897 | el.parentNode.insertBefore(this.marker, el); |
713 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0); | 898 | return el.parentNode.removeChild(el); |
714 | } else { | ||
715 | return el.checked = !!value; | ||
716 | } | ||
717 | } | 899 | } |
718 | }, | 900 | }, |
719 | unchecked: { | 901 | unbind: function() { |
720 | publishes: true, | 902 | var _ref; |
721 | bind: function(el) { | 903 | return (_ref = this.nested) != null ? _ref.unbind() : void 0; |
722 | return Rivets.Util.bindEvent(el, 'change', this.publish); | 904 | }, |
723 | }, | 905 | routine: function(el, value) { |
724 | unbind: function(el) { | 906 | var key, model, models, options, _ref; |
725 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | 907 | if (!!value === (this.nested == null)) { |
726 | }, | 908 | if (value) { |
727 | routine: function(el, value) { | 909 | models = {}; |
728 | var _ref; | 910 | _ref = this.view.models; |
729 | if (el.type === 'radio') { | 911 | for (key in _ref) { |
730 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0); | 912 | model = _ref[key]; |
913 | models[key] = model; | ||
914 | } | ||
915 | options = { | ||
916 | binders: this.view.options.binders, | ||
917 | formatters: this.view.options.formatters, | ||
918 | adapters: this.view.options.adapters, | ||
919 | config: this.view.options.config | ||
920 | }; | ||
921 | (this.nested = new Rivets.View(el, models, options)).bind(); | ||
922 | return this.marker.parentNode.insertBefore(el, this.marker.nextSibling); | ||
731 | } else { | 923 | } else { |
732 | return el.checked = !value; | 924 | el.parentNode.removeChild(el); |
925 | this.nested.unbind(); | ||
926 | return delete this.nested; | ||
733 | } | 927 | } |
734 | } | 928 | } |
735 | }, | 929 | }, |
736 | show: function(el, value) { | 930 | update: function(models) { |
737 | return el.style.display = value ? '' : 'none'; | 931 | var _ref; |
932 | return (_ref = this.nested) != null ? _ref.update(models) : void 0; | ||
933 | } | ||
934 | }; | ||
935 | |||
936 | Rivets.binders.unless = { | ||
937 | block: true, | ||
938 | bind: function(el) { | ||
939 | return Rivets.binders["if"].bind.call(this, el); | ||
738 | }, | 940 | }, |
739 | hide: function(el, value) { | 941 | unbind: function() { |
740 | return el.style.display = value ? 'none' : ''; | 942 | return Rivets.binders["if"].unbind.call(this); |
741 | }, | 943 | }, |
742 | html: function(el, value) { | 944 | routine: function(el, value) { |
743 | return el.innerHTML = value != null ? value : ''; | 945 | return Rivets.binders["if"].routine.call(this, el, !value); |
744 | }, | 946 | }, |
745 | value: { | 947 | update: function(models) { |
746 | publishes: true, | 948 | return Rivets.binders["if"].update.call(this, models); |
747 | bind: function(el) { | 949 | } |
748 | return Rivets.Util.bindEvent(el, 'change', this.publish); | 950 | }; |
749 | }, | 951 | |
750 | unbind: function(el) { | 952 | Rivets.binders['on-*'] = { |
751 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | 953 | "function": true, |
752 | }, | 954 | unbind: function(el) { |
753 | routine: function(el, value) { | 955 | if (this.handler) { |
754 | var o, _i, _len, _ref, _ref1, _ref2, _results; | 956 | return Rivets.Util.unbindEvent(el, this.args[0], this.handler); |
755 | if (window.jQuery != null) { | ||
756 | el = jQuery(el); | ||
757 | if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) { | ||
758 | return el.val(value != null ? value : ''); | ||
759 | } | ||
760 | } else { | ||
761 | if (el.type === 'select-multiple') { | ||
762 | if (value != null) { | ||
763 | _results = []; | ||
764 | for (_i = 0, _len = el.length; _i < _len; _i++) { | ||
765 | o = el[_i]; | ||
766 | _results.push(o.selected = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0)); | ||
767 | } | ||
768 | return _results; | ||
769 | } | ||
770 | } else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.toString() : void 0)) { | ||
771 | return el.value = value != null ? value : ''; | ||
772 | } | ||
773 | } | ||
774 | } | 957 | } |
775 | }, | 958 | }, |
776 | text: function(el, value) { | 959 | routine: function(el, value) { |
777 | if (el.innerText != null) { | 960 | if (this.handler) { |
778 | return el.innerText = value != null ? value : ''; | 961 | Rivets.Util.unbindEvent(el, this.args[0], this.handler); |
779 | } else { | ||
780 | return el.textContent = value != null ? value : ''; | ||
781 | } | 962 | } |
782 | }, | 963 | return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value)); |
783 | "if": { | 964 | } |
784 | block: true, | 965 | }; |
785 | bind: function(el) { | 966 | |
786 | var attr, declaration; | 967 | Rivets.binders['each-*'] = { |
787 | if (this.marker == null) { | 968 | block: true, |
788 | attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-'); | 969 | bind: function(el) { |
789 | declaration = el.getAttribute(attr); | 970 | var attr; |
790 | this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " "); | 971 | if (this.marker == null) { |
791 | el.removeAttribute(attr); | 972 | attr = [this.view.config.prefix, this.type].join('-').replace('--', '-'); |
792 | el.parentNode.insertBefore(this.marker, el); | 973 | this.marker = document.createComment(" rivets: " + this.type + " "); |
793 | return el.parentNode.removeChild(el); | 974 | this.iterated = []; |
794 | } | 975 | el.removeAttribute(attr); |
795 | }, | 976 | el.parentNode.insertBefore(this.marker, el); |
796 | unbind: function() { | 977 | return el.parentNode.removeChild(el); |
797 | var _ref; | ||
798 | return (_ref = this.nested) != null ? _ref.unbind() : void 0; | ||
799 | }, | ||
800 | routine: function(el, value) { | ||
801 | var key, model, models, options, _ref; | ||
802 | if (!!value === (this.nested == null)) { | ||
803 | if (value) { | ||
804 | models = {}; | ||
805 | _ref = this.view.models; | ||
806 | for (key in _ref) { | ||
807 | model = _ref[key]; | ||
808 | models[key] = model; | ||
809 | } | ||
810 | options = { | ||
811 | binders: this.view.options.binders, | ||
812 | formatters: this.view.options.formatters, | ||
813 | config: this.view.options.config | ||
814 | }; | ||
815 | (this.nested = new Rivets.View(el, models, options)).bind(); | ||
816 | return this.marker.parentNode.insertBefore(el, this.marker.nextSibling); | ||
817 | } else { | ||
818 | el.parentNode.removeChild(el); | ||
819 | this.nested.unbind(); | ||
820 | return delete this.nested; | ||
821 | } | ||
822 | } | ||
823 | }, | ||
824 | update: function(models) { | ||
825 | var _ref; | ||
826 | return (_ref = this.nested) != null ? _ref.update(models) : void 0; | ||
827 | } | 978 | } |
828 | }, | 979 | }, |
829 | unless: { | 980 | unbind: function(el) { |
830 | block: true, | 981 | var view, _i, _len, _ref, _results; |
831 | bind: function(el) { | 982 | if (this.iterated != null) { |
832 | return Rivets.binders["if"].bind.call(this, el); | 983 | _ref = this.iterated; |
833 | }, | 984 | _results = []; |
834 | unbind: function() { | 985 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
835 | return Rivets.binders["if"].unbind.call(this); | 986 | view = _ref[_i]; |
836 | }, | 987 | _results.push(view.unbind()); |
837 | routine: function(el, value) { | 988 | } |
838 | return Rivets.binders["if"].routine.call(this, el, !value); | 989 | return _results; |
839 | }, | ||
840 | update: function(models) { | ||
841 | return Rivets.binders["if"].update.call(this, models); | ||
842 | } | 990 | } |
843 | }, | 991 | }, |
844 | "on-*": { | 992 | routine: function(el, collection) { |
845 | "function": true, | 993 | var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results; |
846 | unbind: function(el) { | 994 | modelName = this.args[0]; |
847 | if (this.handler) { | 995 | collection = collection || []; |
848 | return Rivets.Util.unbindEvent(el, this.args[0], this.handler); | 996 | if (this.iterated.length > collection.length) { |
849 | } | 997 | _ref = Array(this.iterated.length - collection.length); |
850 | }, | 998 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
851 | routine: function(el, value) { | 999 | i = _ref[_i]; |
852 | if (this.handler) { | 1000 | view = this.iterated.pop(); |
853 | Rivets.Util.unbindEvent(el, this.args[0], this.handler); | 1001 | view.unbind(); |
1002 | this.marker.parentNode.removeChild(view.els[0]); | ||
854 | } | 1003 | } |
855 | return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value)); | ||
856 | } | 1004 | } |
857 | }, | 1005 | for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { |
858 | "each-*": { | 1006 | model = collection[index]; |
859 | block: true, | 1007 | data = {}; |
860 | bind: function(el) { | 1008 | data[modelName] = model; |
861 | var attr; | 1009 | if (this.iterated[index] == null) { |
862 | if (this.marker == null) { | 1010 | _ref1 = this.view.models; |
863 | attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-'); | 1011 | for (key in _ref1) { |
864 | this.marker = document.createComment(" rivets: " + this.type + " "); | 1012 | model = _ref1[key]; |
865 | this.iterated = []; | 1013 | if (data[key] == null) { |
866 | el.removeAttribute(attr); | 1014 | data[key] = model; |
867 | el.parentNode.insertBefore(this.marker, el); | 1015 | } |
868 | return el.parentNode.removeChild(el); | ||
869 | } | ||
870 | }, | ||
871 | unbind: function(el) { | ||
872 | var view, _i, _len, _ref, _results; | ||
873 | if (this.iterated != null) { | ||
874 | _ref = this.iterated; | ||
875 | _results = []; | ||
876 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
877 | view = _ref[_i]; | ||
878 | _results.push(view.unbind()); | ||
879 | } | 1016 | } |
880 | return _results; | 1017 | previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker; |
881 | } | 1018 | options = { |
882 | }, | 1019 | binders: this.view.options.binders, |
883 | routine: function(el, collection) { | 1020 | formatters: this.view.options.formatters, |
884 | var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results; | 1021 | adapters: this.view.options.adapters, |
885 | modelName = this.args[0]; | 1022 | config: {} |
886 | collection = collection || []; | 1023 | }; |
887 | if (this.iterated.length > collection.length) { | 1024 | _ref2 = this.view.options.config; |
888 | _ref = Array(this.iterated.length - collection.length); | 1025 | for (k in _ref2) { |
889 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 1026 | v = _ref2[k]; |
890 | i = _ref[_i]; | 1027 | options.config[k] = v; |
891 | view = this.iterated.pop(); | ||
892 | view.unbind(); | ||
893 | this.marker.parentNode.removeChild(view.els[0]); | ||
894 | } | 1028 | } |
1029 | options.config.preloadData = true; | ||
1030 | template = el.cloneNode(true); | ||
1031 | view = new Rivets.View(template, data, options); | ||
1032 | view.bind(); | ||
1033 | this.iterated.push(view); | ||
1034 | this.marker.parentNode.insertBefore(template, previous.nextSibling); | ||
1035 | } else if (this.iterated[index].models[modelName] !== model) { | ||
1036 | this.iterated[index].update(data); | ||
895 | } | 1037 | } |
1038 | } | ||
1039 | if (el.nodeName === 'OPTION') { | ||
1040 | _ref3 = this.view.bindings; | ||
896 | _results = []; | 1041 | _results = []; |
897 | for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { | 1042 | for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { |
898 | model = collection[index]; | 1043 | binding = _ref3[_k]; |
899 | data = {}; | 1044 | if (binding.el === this.marker.parentNode && binding.type === 'value') { |
900 | data[modelName] = model; | 1045 | _results.push(binding.sync()); |
901 | if (this.iterated[index] == null) { | ||
902 | _ref1 = this.view.models; | ||
903 | for (key in _ref1) { | ||
904 | model = _ref1[key]; | ||
905 | if (data[key] == null) { | ||
906 | data[key] = model; | ||
907 | } | ||
908 | } | ||
909 | previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker; | ||
910 | options = { | ||
911 | binders: this.view.options.binders, | ||
912 | formatters: this.view.options.formatters, | ||
913 | config: {} | ||
914 | }; | ||
915 | _ref2 = this.view.options.config; | ||
916 | for (k in _ref2) { | ||
917 | v = _ref2[k]; | ||
918 | options.config[k] = v; | ||
919 | } | ||
920 | options.config.preloadData = true; | ||
921 | template = el.cloneNode(true); | ||
922 | view = new Rivets.View(template, data, options); | ||
923 | view.bind(); | ||
924 | this.iterated.push(view); | ||
925 | _results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling)); | ||
926 | } else if (this.iterated[index].models[modelName] !== model) { | ||
927 | _results.push(this.iterated[index].update(data)); | ||
928 | } else { | 1046 | } else { |
929 | _results.push(void 0); | 1047 | _results.push(void 0); |
930 | } | 1048 | } |
931 | } | 1049 | } |
932 | return _results; | 1050 | return _results; |
933 | }, | ||
934 | update: function(models) { | ||
935 | var data, key, model, view, _i, _len, _ref, _results; | ||
936 | data = {}; | ||
937 | for (key in models) { | ||
938 | model = models[key]; | ||
939 | if (key !== this.args[0]) { | ||
940 | data[key] = model; | ||
941 | } | ||
942 | } | ||
943 | _ref = this.iterated; | ||
944 | _results = []; | ||
945 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
946 | view = _ref[_i]; | ||
947 | _results.push(view.update(data)); | ||
948 | } | ||
949 | return _results; | ||
950 | } | 1051 | } |
951 | }, | 1052 | }, |
952 | "class-*": function(el, value) { | 1053 | update: function(models) { |
953 | var elClass; | 1054 | var data, key, model, view, _i, _len, _ref, _results; |
954 | elClass = " " + el.className + " "; | 1055 | data = {}; |
955 | if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) { | 1056 | for (key in models) { |
956 | return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim(); | 1057 | model = models[key]; |
1058 | if (key !== this.args[0]) { | ||
1059 | data[key] = model; | ||
1060 | } | ||
957 | } | 1061 | } |
958 | }, | 1062 | _ref = this.iterated; |
959 | "*": function(el, value) { | 1063 | _results = []; |
960 | if (value) { | 1064 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
961 | return el.setAttribute(this.type, value); | 1065 | view = _ref[_i]; |
962 | } else { | 1066 | _results.push(view.update(data)); |
963 | return el.removeAttribute(this.type); | ||
964 | } | 1067 | } |
1068 | return _results; | ||
965 | } | 1069 | } |
966 | }; | 1070 | }; |
967 | 1071 | ||
968 | Rivets.components = {}; | 1072 | Rivets.binders['class-*'] = function(el, value) { |
1073 | var elClass; | ||
1074 | elClass = " " + el.className + " "; | ||
1075 | if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) { | ||
1076 | return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim(); | ||
1077 | } | ||
1078 | }; | ||
969 | 1079 | ||
970 | Rivets.config = { | 1080 | Rivets.binders['*'] = function(el, value) { |
971 | preloadData: true, | 1081 | if (value) { |
972 | handler: function(context, ev, binding) { | 1082 | return el.setAttribute(this.type, value); |
973 | return this.call(context, ev, binding.view.models); | 1083 | } else { |
1084 | return el.removeAttribute(this.type); | ||
974 | } | 1085 | } |
975 | }; | 1086 | }; |
976 | 1087 | ||
977 | Rivets.formatters = {}; | 1088 | Rivets.adapters['.'] = { |
1089 | id: '_rv', | ||
1090 | counter: 0, | ||
1091 | weakmap: {}, | ||
1092 | weakReference: function(obj) { | ||
1093 | var id; | ||
1094 | if (obj[this.id] == null) { | ||
1095 | id = this.counter++; | ||
1096 | this.weakmap[id] = { | ||
1097 | callbacks: {} | ||
1098 | }; | ||
1099 | Object.defineProperty(obj, this.id, { | ||
1100 | value: id | ||
1101 | }); | ||
1102 | } | ||
1103 | return this.weakmap[obj[this.id]]; | ||
1104 | }, | ||
1105 | stubFunction: function(obj, fn) { | ||
1106 | var map, original, weakmap; | ||
1107 | original = obj[fn]; | ||
1108 | map = this.weakReference(obj); | ||
1109 | weakmap = this.weakmap; | ||
1110 | return obj[fn] = function() { | ||
1111 | var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; | ||
1112 | response = original.apply(obj, arguments); | ||
1113 | _ref = map.pointers; | ||
1114 | for (r in _ref) { | ||
1115 | k = _ref[r]; | ||
1116 | _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; | ||
1117 | for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | ||
1118 | callback = _ref3[_i]; | ||
1119 | callback(); | ||
1120 | } | ||
1121 | } | ||
1122 | return response; | ||
1123 | }; | ||
1124 | }, | ||
1125 | observeMutations: function(obj, ref, keypath) { | ||
1126 | var fn, functions, map, _base, _i, _len; | ||
1127 | if (Array.isArray(obj)) { | ||
1128 | map = this.weakReference(obj); | ||
1129 | if (map.pointers == null) { | ||
1130 | map.pointers = {}; | ||
1131 | functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; | ||
1132 | for (_i = 0, _len = functions.length; _i < _len; _i++) { | ||
1133 | fn = functions[_i]; | ||
1134 | this.stubFunction(obj, fn); | ||
1135 | } | ||
1136 | } | ||
1137 | if ((_base = map.pointers)[ref] == null) { | ||
1138 | _base[ref] = []; | ||
1139 | } | ||
1140 | if (__indexOf.call(map.pointers[ref], keypath) < 0) { | ||
1141 | return map.pointers[ref].push(keypath); | ||
1142 | } | ||
1143 | } | ||
1144 | }, | ||
1145 | unobserveMutations: function(obj, ref, keypath) { | ||
1146 | var keypaths, _ref; | ||
1147 | if (Array.isArray(obj && (obj[this.id] != null))) { | ||
1148 | if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { | ||
1149 | return keypaths.splice(keypaths.indexOf(keypath), 1); | ||
1150 | } | ||
1151 | } | ||
1152 | }, | ||
1153 | subscribe: function(obj, keypath, callback) { | ||
1154 | var callbacks, value, | ||
1155 | _this = this; | ||
1156 | callbacks = this.weakReference(obj).callbacks; | ||
1157 | if (callbacks[keypath] == null) { | ||
1158 | callbacks[keypath] = []; | ||
1159 | value = obj[keypath]; | ||
1160 | Object.defineProperty(obj, keypath, { | ||
1161 | get: function() { | ||
1162 | return value; | ||
1163 | }, | ||
1164 | set: function(newValue) { | ||
1165 | var _i, _len, _ref; | ||
1166 | if (newValue !== value) { | ||
1167 | value = newValue; | ||
1168 | _ref = callbacks[keypath]; | ||
1169 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
1170 | callback = _ref[_i]; | ||
1171 | callback(); | ||
1172 | } | ||
1173 | return _this.observeMutations(newValue, obj[_this.id], keypath); | ||
1174 | } | ||
1175 | } | ||
1176 | }); | ||
1177 | } | ||
1178 | if (__indexOf.call(callbacks[keypath], callback) < 0) { | ||
1179 | callbacks[keypath].push(callback); | ||
1180 | } | ||
1181 | return this.observeMutations(obj[keypath], obj[this.id], keypath); | ||
1182 | }, | ||
1183 | unsubscribe: function(obj, keypath, callback) { | ||
1184 | var callbacks; | ||
1185 | callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; | ||
1186 | callbacks.splice(callbacks.indexOf(callback), 1); | ||
1187 | return this.unobserveMutations(obj[keypath], obj[this.id], keypath); | ||
1188 | }, | ||
1189 | read: function(obj, keypath) { | ||
1190 | return obj[keypath]; | ||
1191 | }, | ||
1192 | publish: function(obj, keypath, value) { | ||
1193 | return obj[keypath] = value; | ||
1194 | } | ||
1195 | }; | ||
978 | 1196 | ||
979 | Rivets.factory = function(exports) { | 1197 | Rivets.factory = function(exports) { |
980 | exports._ = Rivets; | 1198 | exports._ = Rivets; |
981 | exports.binders = Rivets.binders; | 1199 | exports.binders = Rivets.binders; |
982 | exports.components = Rivets.components; | 1200 | exports.components = Rivets.components; |
983 | exports.formatters = Rivets.formatters; | 1201 | exports.formatters = Rivets.formatters; |
1202 | exports.adapters = Rivets.adapters; | ||
984 | exports.config = Rivets.config; | 1203 | exports.config = Rivets.config; |
985 | exports.configure = function(options) { | 1204 | exports.configure = function(options) { |
986 | var property, value; | 1205 | var property, value; | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.5.13 | 2 | // version: 0.6.0 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | !function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].slice,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a},g=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};a={},b=window.jQuery||window.Zepto,String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function b(a,b,d,e,f,g){var h,i,j,k;if(this.view=a,this.el=b,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),!(this.binder=this.view.binders[d])){k=this.view.binders;for(h in k)j=k[h],"*"!==h&&-1!==h.indexOf("*")&&(i=new RegExp("^"+h.replace("*",".+")+"$"),i.test(d)&&(this.binder=j,this.args=new RegExp("^"+h.replace("*","(.+)")+"$").exec(d),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return b.prototype.formattedValue=function(a){var b,c,e,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),e=b.shift(),c=this.model[e]instanceof Function?this.model[e]:this.view.formatters[e],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(d.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(d.call(b))));return a},b.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},b.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},b.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},b.prototype.publish=function(){var b,c,e,f,g,h,i,j,k;for(f=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],b=c.split(/\s+/),e=b.shift(),(null!=(j=this.view.formatters[e])?j.publish:void 0)&&(f=(k=this.view.formatters[e]).publish.apply(k,[f].concat(d.call(b))));return this.view.config.adapter.publish(this.model,this.keypath,f)},b.prototype.bind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.subscribe(c,b,this.sync));return i}},b.prototype.unbind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.unsubscribe(c,b,this.sync));return i}},b.prototype.update=function(a){var b;return null==a&&(a={}),this.key?a[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=a[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function d(b,d,e){var f,h,i,j,k;for(this.view=b,this.el=d,this.type=e,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)f=j[h],k=f.name,g.call(this.component.attributes,k)>=0?this.attributes[f.name]=f.value:this.inflections[f.name]=f.value}return f(d,b),d.prototype.sync=function(){},d.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},d.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},d.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},d.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},d}(a.Binding),a.TextBinding=function(a){function b(a,b,d,e,f,g){this.view=a,this.el=b,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return f(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(a.Binding),a.View=function(){function b(b,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),"undefined"==typeof this.els.length&&(this.els=[this.els]),l=["config","binders","formatters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return b.prototype.bindingRegExp=function(){var a;return a=this.config.prefix,a?new RegExp("^data-"+a+"-"):/^data-/},b.prototype.componentRegExp=function(){var a,b;return new RegExp("^"+(null!=(a=null!=(b=this.config.prefix)?b.toUpperCase():void 0)?a:"RV")+"-")},b.prototype.build=function(){var b,c,e,f,h,i,j,k,l,m=this;for(this.bindings=[],i=[],b=this.bindingRegExp(),e=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,l,n,o,p;return k={},o=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)n=c[a],d.push(n.trim());return d}(),f=function(){var a,b,c,d;for(c=o.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),l=f.shift(),p=l.split(/\.|:/),k.formatters=o,k.bypass=-1!==l.indexOf(":"),p[0]?i=p.shift():(i=null,p.shift()),j=p.join("."),(h=f.shift())&&(k.dependencies=h.split(/\s+/)),m.bindings.push(new a[b](m,c,d,i,j,k))},h=function(f){var j,k,l,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P;if(g.call(i,f)<0){if(f.nodeType===Node.TEXT_NODE){if(r=a.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(f.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?d.call(x,1):[],f.data=u.value,0===u.type?f.data=u.value:c("TextBinding",f,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),f.parentNode.appendChild(v),1===w.type&&c("TextBinding",v,null,w.value)}else if(e.test(f.tagName))y=f.tagName.replace(e,"").toLowerCase(),m.bindings.push(new a.ComponentBinding(m,f,y));else if(null!=f.attributes){for(K=f.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],b.test(j.name)){if(y=j.name.replace(b,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=f.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||f.attributes,D=0,H=N.length;H>D;D++)j=N[D],b.test(j.name)&&(y=j.name.replace(b,""),c("Binding",f,y,j.value))}for(O=f.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)f=l[j],h(f)},b.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},b.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},b}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+2}return h},a}(),a.Util={bindEvent:function(a,c,d){return null!=window.jQuery?(a=b(a),null!=a.on?a.on(c,d):a.bind(c,d)):null!=window.addEventListener?a.addEventListener(c,d,!1):(c="on"+c,a.attachEvent(c,d))},unbindEvent:function(a,c,d){return null!=window.jQuery?(a=b(a),null!=a.off?a.off(c,d):a.unbind(c,d)):null!=window.removeEventListener?a.removeEventListener(c,d,!1):(c="on"+c,a.detachEvent(c,d))},getInputValue:function(a){var c,d,e,f;if(null!=window.jQuery)switch(a=b(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(f=[],d=0,e=a.length;e>d;d++)c=a[d],c.selected&&f.push(c.value);return f;default:return a.value}}},a.binders={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},unchecked:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},show:function(a,b){return a.style.display=b?"":"none"},hide:function(a,b){return a.style.display=b?"none":""},html:function(a,b){return a.innerHTML=null!=b?b:""},value:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,c){var d,e,f,h,i,j,k;if(null!=window.jQuery){if(a=b(a),(null!=c?c.toString():void 0)!==(null!=(h=a.val())?h.toString():void 0))return a.val(null!=c?c:"")}else if("select-multiple"===a.type){if(null!=c){for(k=[],e=0,f=a.length;f>e;e++)d=a[e],k.push(d.selected=(i=d.value,g.call(c,i)>=0));return k}}else if((null!=c?c.toString():void 0)!==(null!=(j=a.value)?j.toString():void 0))return a.value=null!=c?c:""}},text:function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},"if":{block:!0,bind:function(a){var b,c;return null==this.marker?(b=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},unless:{block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},"on-*":{"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},"each-*":{block:!0,bind:function(a){var b;return null==this.marker?(b=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;if(j=this.args[0],c=c||[],this.iterated.length>c.length)for(t=Array(this.iterated.length-c.length),p=0,r=t.length;r>p;p++)e=t[p],o=this.iterated.pop(),o.unbind(),this.marker.parentNode.removeChild(o.els[0]);for(w=[],f=q=0,s=c.length;s>q;f=++q)if(i=c[f],d={},d[j]=i,null==this.iterated[f]){u=this.view.models;for(h in u)i=u[h],null==d[h]&&(d[h]=i);l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,k={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},v=this.view.options.config;for(g in v)n=v[g],k.config[g]=n;k.config.preloadData=!0,m=b.cloneNode(!0),o=new a.View(m,d,k),o.bind(),this.iterated.push(o),w.push(this.marker.parentNode.insertBefore(m,l.nextSibling))}else this.iterated[f].models[j]!==i?w.push(this.iterated[f].update(d)):w.push(void 0);return w},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},"class-*":function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)}},a.components={},a.config={preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}},a.formatters={},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}.call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
5 | (function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},e=[].slice,f={}.hasOwnProperty,g=function(a,b){function c(){this.constructor=a}for(var d in b)f.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};b={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},b.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},b.View=function(){function a(a,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=a,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=b[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return a.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},a.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},a.prototype.build=function(){var a,c,f,g,h,i,j,k,l,m=this;for(this.bindings=[],i=[],a=this.bindingRegExp(),f=this.componentRegExp(),c=function(a,c,d,e){var f,g,h,i,j,k,l;return j={},l=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=l.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=l,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),m.bindings.push(new b[a](m,c,d,i,j))},h=function(g){var j,k,l,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P;if(d.call(i,g)<0){if(g.nodeType===Node.TEXT_NODE){if(r=b.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(g.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?e.call(x,1):[],g.data=u.value,0===u.type?g.data=u.value:c("TextBinding",g,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),g.parentNode.appendChild(v),1===w.type&&c("TextBinding",v,null,w.value)}else if(f.test(g.tagName))y=g.tagName.replace(f,"").toLowerCase(),m.bindings.push(new b.ComponentBinding(m,g,y));else if(null!=g.attributes){for(K=g.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],a.test(j.name)){if(y=j.name.replace(a,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=g.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||g.attributes,D=0,H=N.length;H>D;D++)j=N[D],a.test(j.name)&&(y=j.name.replace(a,""),c("Binding",g,y,j.value))}for(O=g.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)g=l[j],h(g)},a.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},a.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},a.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},a.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},a.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},a.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},a}(),b.Binding=function(){function d(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),this.setObserver=c(this.setObserver,this),this.setBinder=c(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return d.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},d.prototype.setObserver=function(){var b=this;return this.observer=new a(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},d.prototype.formattedValue=function(a){var b,c,d,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(e.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(e.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},d.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},d.prototype.publish=function(){var a,c,d,f,g,h,i,j,k;for(f=b.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],a=c.split(/\s+/),d=a.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(f=(k=this.view.formatters[d]).publish.apply(k,[f].concat(e.call(a))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},d.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},d.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||null!=(f=this.binder.unbind)&&f.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},d.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),b.ComponentBinding=function(a){function e(a,e,f){var g,h,i,j,k;for(this.view=a,this.el=e,this.type=f,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=b.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,d.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return g(e,a),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var a,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(a=this.component.build.call(this.attributes),(this.componentView=new b.View(a,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(a,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(b.Binding),b.TextBinding=function(a){function b(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return g(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(b.Binding),b.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var e,f,g,h;h=[],f={"interface":c,path:""};for(g in a)e=a[g],d.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),b.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+2}return h},a}(),a=function(){function a(a,b,d,e){this.view=a,this.model=b,this.keypath=d,this.callback=e,this.realize=c(this.realize,this),this.update=c(this.update,this),this.parse=c(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return a.prototype.parse=function(){var a,c,e,f,g,h;return a=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],d.call(a,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=b.KeypathParser.parse(e,a,f),this.key=this.tokens.pop()},a.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},a.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},a}(),b.binders.enabled=function(a,b){return a.disabled=!b},b.binders.disabled=function(a,b){return a.disabled=!!b},b.binders.checked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},b.binders.unchecked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},b.binders.show=function(a,b){return a.style.display=b?"":"none"},b.binders.hide=function(a,b){return a.style.display=b?"none":""},b.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},b.binders.value={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)c=a[e],j.push(c.selected=(h=c.value,d.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},b.binders.text=function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},b.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(a,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new b.View(a,f,g)).bind(),this.marker.parentNode.insertBefore(a,this.marker.nextSibling)}return a.parentNode.removeChild(a),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},b.binders.unless={block:!0,bind:function(a){return b.binders["if"].bind.call(this,a)},unbind:function(){return b.binders["if"].unbind.call(this)},routine:function(a,c){return b.binders["if"].routine.call(this,a,!c)},update:function(a){return b.binders["if"].update.call(this,a)}},b.binders["on-*"]={"function":!0,unbind:function(a){return this.handler?b.Util.unbindEvent(a,this.args[0],this.handler):void 0},routine:function(a,c){return this.handler&&b.Util.unbindEvent(a,this.args[0],this.handler),b.Util.bindEvent(a,this.args[0],this.handler=this.eventHandler(c))}},b.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(a,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=a.cloneNode(!0),p=new b.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===a.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},b.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},b.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},b.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),d.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(d){var h,i,j;if(d!==f){for(f=d,j=e[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(d,a[g.id],b)}}})),d.call(e[b],c)<0&&e[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},b.factory=function(a){return a._=b,a.binders=b.binders,a.components=b.components,a.formatters=b.formatters,a.adapters=b.adapters,a.config=b.config,a.configure=function(a){var c,d;null==a&&(a={});for(c in a)d=a[c],b.config[c]=d},a.bind=function(a,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new b.View(a,c,d),e.bind(),e}},"object"==typeof exports?b.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(a){return b.factory(this.rivets=a),a}):b.factory(this.rivets={})}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | { | 1 | { |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "description": "Declarative data binding facility.", | 3 | "description": "Declarative data binding + templating solution.", |
4 | "version": "0.5.13", | 4 | "version": "0.6.0", |
5 | "author": "Michael Richards", | 5 | "author": "Michael Richards", |
6 | "url": "http://rivetsjs.com", | 6 | "url": "http://rivetsjs.com", |
7 | "main": "./dist/rivets.js", | 7 | "main": "./dist/rivets.js", | ... | ... |
-
Please register or sign in to post a comment