rivets-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ # npm
20
+ *.swp
21
+ node_modules/**/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rivets-rails.gemspec
4
+ gemspec
@@ -0,0 +1,56 @@
1
+ module.exports = (grunt) ->
2
+ grunt.initConfig
3
+ pkg: grunt.file.readJSON('package.json')
4
+
5
+ meta:
6
+ banner:
7
+ '// rivets.custom_by_vkill.js\n' +
8
+ '// version: <%= pkg.version %>\n' +
9
+ '// author: <%= pkg.author %>\n' +
10
+ '// license: <%= pkg.licenses[0].type %>\n'
11
+
12
+ coffee:
13
+ all:
14
+ files:
15
+ 'dist/rivets.custom.configure.js': 'src/rivets.custom.configure.coffee'
16
+ 'dist/rivets.custom.binders.js': 'src/rivets.custom.binders.coffee'
17
+ 'dist/rivets.custom.formatters.js': 'src/rivets.custom.formatters.coffee'
18
+ 'dist/rivets.custom.general_adapter.js': 'src/rivets.custom.general_adapter.coffee'
19
+ # 'dist/rivets.custom.Backbone_adapter.js': 'src/rivets.custom.Backbone_adapter.coffee'
20
+ # 'dist/rivets.custom.Watch_adapter.js': 'src/rivets.custom.Watch_adapter.coffee'
21
+ # 'dist/rivets.custom.CanJS_adapter.js': 'src/rivets.custom.CanJS_adapter.coffee'
22
+ # 'dist/rivets.custom.Spine_adapter.js': 'src/rivets.custom.Spine_adapter.coffee'
23
+ # 'dist/rivets.custom.Spine_adapter.js': 'src/rivets.custom.Spine_adapter.coffee'
24
+
25
+ concat:
26
+ all:
27
+ options:
28
+ banner: '<%= meta.banner %>'
29
+ files:
30
+ 'dist/rivets.general_custom.js': [
31
+ 'dist/rivets.custom.configure.js',
32
+ 'dist/rivets.custom.binders.js',
33
+ 'dist/rivets.custom.formatters.js',
34
+ 'dist/rivets.custom.general_adapter.js'
35
+ ]
36
+
37
+ uglify:
38
+ all:
39
+ options:
40
+ banner: '<%= meta.banner %>'
41
+ report: 'gzip'
42
+ files:
43
+ 'dist/rivets.general_custom.js.min.js': 'dist/rivets.general_custom.js'
44
+
45
+ watch:
46
+ all:
47
+ files: ['src/*.coffee']
48
+ tasks: ['build']
49
+
50
+ grunt.loadNpmTasks 'grunt-contrib-coffee'
51
+ grunt.loadNpmTasks 'grunt-contrib-concat'
52
+ grunt.loadNpmTasks 'grunt-contrib-uglify'
53
+ grunt.loadNpmTasks 'grunt-contrib-watch'
54
+
55
+ grunt.registerTask 'default', ['watch']
56
+ grunt.registerTask 'build', ['coffee', 'concat', 'uglify']
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 vkill
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # Rivets::Rails
2
+
3
+ [rivets](https://github.com/mikeric/rivets) plugin for Rails
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rivets-rails'
10
+
11
+ ## Usage
12
+
13
+ Add the following to your `app/assets/javascripts/application.js` file.
14
+
15
+ //= require rivets
16
+ //= require rivets.general_custom
17
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ TAG=v0.5.4
2
+ curl -s -o dist/rivets.js https://raw.github.com/mikeric/rivets/$TAG/dist/rivets.js
3
+ curl -s -o dist/rivets.min.js https://raw.github.com/mikeric/rivets/$TAG/dist/rivets.min.js
@@ -0,0 +1,22 @@
1
+ (function() {
2
+ var rivets;
3
+
4
+ rivets = this.rivets;
5
+
6
+ rivets.binders.content = {
7
+ routine: rivets.binders.text,
8
+ bind: function(el) {
9
+ var self;
10
+
11
+ self = this;
12
+ this.callback = function() {
13
+ return rivets.config.adapter.publish(self.model, self.keypath, el.textContent);
14
+ };
15
+ return el.addEventListener("input", this.callback, false);
16
+ },
17
+ unbind: function(el) {
18
+ return el.removeEventListener("input", this.callback, false);
19
+ }
20
+ };
21
+
22
+ }).call(this);
@@ -0,0 +1,10 @@
1
+ (function() {
2
+ var rivets;
3
+
4
+ rivets = this.rivets;
5
+
6
+ rivets.configure({
7
+ prefix: 'bind'
8
+ });
9
+
10
+ }).call(this);
@@ -0,0 +1,54 @@
1
+ (function() {
2
+ var rivets;
3
+
4
+ rivets = this.rivets;
5
+
6
+ rivets.formatters.shortdate = function(value) {
7
+ return moment(value).format("YYYY-MM-DD");
8
+ };
9
+
10
+ rivets.formatters.number = function(value) {
11
+ return +value;
12
+ };
13
+
14
+ rivets.formatters.string = function(value) {
15
+ return String(value);
16
+ };
17
+
18
+ rivets.formatters.negate = function(value) {
19
+ return !value;
20
+ };
21
+
22
+ rivets.formatters.eq = function(value, args) {
23
+ return value === args;
24
+ };
25
+
26
+ rivets.formatters.not_eq = function(value, args) {
27
+ return value !== args;
28
+ };
29
+
30
+ rivets.formatters.gt = function(value, arg) {
31
+ return value > arg;
32
+ };
33
+
34
+ rivets.formatters.lt = function(value, arg) {
35
+ return value < arg;
36
+ };
37
+
38
+ rivets.formatters.blank = function(value) {
39
+ return (value == null) || value === "";
40
+ };
41
+
42
+ rivets.formatters.preventDefault = function(value) {
43
+ return function(e) {
44
+ e.preventDefault();
45
+ value.call(this, e);
46
+ return false;
47
+ };
48
+ };
49
+
50
+ rivets.formatters.length = function(value) {
51
+ return value.length;
52
+ };
53
+
54
+ }).call(this);
@@ -0,0 +1,27 @@
1
+ (function() {
2
+ var rivets;
3
+
4
+ rivets = this.rivets;
5
+
6
+ rivets.configure({
7
+ adapter: {
8
+ subscribe: function(obj, keypath, callback) {
9
+ if (obj.on != null) {
10
+ return obj.on("change:" + keypath, callback);
11
+ }
12
+ },
13
+ unsubscribe: function(obj, keypath, callback) {
14
+ if (obj.off != null) {
15
+ return obj.off("change:" + keypath, callback);
16
+ }
17
+ },
18
+ read: function(obj, keypath) {
19
+ return obj[keypath];
20
+ },
21
+ publish: function(obj, keypath, value) {
22
+ return obj[keypath] = value;
23
+ }
24
+ }
25
+ });
26
+
27
+ }).call(this);
@@ -0,0 +1,120 @@
1
+ // rivets.custom_by_vkill.js
2
+ // version: 0.1.0
3
+ // author: vkill
4
+ // license: MIT
5
+ (function() {
6
+ var rivets;
7
+
8
+ rivets = this.rivets;
9
+
10
+ rivets.configure({
11
+ prefix: 'bind'
12
+ });
13
+
14
+ }).call(this);
15
+
16
+ (function() {
17
+ var rivets;
18
+
19
+ rivets = this.rivets;
20
+
21
+ rivets.binders.content = {
22
+ routine: rivets.binders.text,
23
+ bind: function(el) {
24
+ var self;
25
+
26
+ self = this;
27
+ this.callback = function() {
28
+ return rivets.config.adapter.publish(self.model, self.keypath, el.textContent);
29
+ };
30
+ return el.addEventListener("input", this.callback, false);
31
+ },
32
+ unbind: function(el) {
33
+ return el.removeEventListener("input", this.callback, false);
34
+ }
35
+ };
36
+
37
+ }).call(this);
38
+
39
+ (function() {
40
+ var rivets;
41
+
42
+ rivets = this.rivets;
43
+
44
+ rivets.formatters.shortdate = function(value) {
45
+ return moment(value).format("YYYY-MM-DD");
46
+ };
47
+
48
+ rivets.formatters.number = function(value) {
49
+ return +value;
50
+ };
51
+
52
+ rivets.formatters.string = function(value) {
53
+ return String(value);
54
+ };
55
+
56
+ rivets.formatters.negate = function(value) {
57
+ return !value;
58
+ };
59
+
60
+ rivets.formatters.eq = function(value, args) {
61
+ return value === args;
62
+ };
63
+
64
+ rivets.formatters.not_eq = function(value, args) {
65
+ return value !== args;
66
+ };
67
+
68
+ rivets.formatters.gt = function(value, arg) {
69
+ return value > arg;
70
+ };
71
+
72
+ rivets.formatters.lt = function(value, arg) {
73
+ return value < arg;
74
+ };
75
+
76
+ rivets.formatters.blank = function(value) {
77
+ return (value == null) || value === "";
78
+ };
79
+
80
+ rivets.formatters.preventDefault = function(value) {
81
+ return function(e) {
82
+ e.preventDefault();
83
+ value.call(this, e);
84
+ return false;
85
+ };
86
+ };
87
+
88
+ rivets.formatters.length = function(value) {
89
+ return value.length;
90
+ };
91
+
92
+ }).call(this);
93
+
94
+ (function() {
95
+ var rivets;
96
+
97
+ rivets = this.rivets;
98
+
99
+ rivets.configure({
100
+ adapter: {
101
+ subscribe: function(obj, keypath, callback) {
102
+ if (obj.on != null) {
103
+ return obj.on("change:" + keypath, callback);
104
+ }
105
+ },
106
+ unsubscribe: function(obj, keypath, callback) {
107
+ if (obj.off != null) {
108
+ return obj.off("change:" + keypath, callback);
109
+ }
110
+ },
111
+ read: function(obj, keypath) {
112
+ return obj[keypath];
113
+ },
114
+ publish: function(obj, keypath, value) {
115
+ return obj[keypath] = value;
116
+ }
117
+ }
118
+ });
119
+
120
+ }).call(this);
@@ -0,0 +1,5 @@
1
+ // rivets.custom_by_vkill.js
2
+ // version: 0.1.0
3
+ // author: vkill
4
+ // license: MIT
5
+ !function(){var a;a=this.rivets,a.configure({prefix:"bind"})}.call(this),function(){var a;a=this.rivets,a.binders.content={routine:a.binders.text,bind:function(b){var c;return c=this,this.callback=function(){return a.config.adapter.publish(c.model,c.keypath,b.textContent)},b.addEventListener("input",this.callback,!1)},unbind:function(a){return a.removeEventListener("input",this.callback,!1)}}}.call(this),function(){var a;a=this.rivets,a.formatters.shortdate=function(a){return moment(a).format("YYYY-MM-DD")},a.formatters.number=function(a){return+a},a.formatters.string=function(a){return String(a)},a.formatters.negate=function(a){return!a},a.formatters.eq=function(a,b){return a===b},a.formatters.not_eq=function(a,b){return a!==b},a.formatters.gt=function(a,b){return a>b},a.formatters.lt=function(a,b){return b>a},a.formatters.blank=function(a){return null==a||""===a},a.formatters.preventDefault=function(a){return function(b){return b.preventDefault(),a.call(this,b),!1}},a.formatters.length=function(a){return a.length}}.call(this),function(){var a;a=this.rivets,a.configure({adapter:{subscribe:function(a,b,c){return null!=a.on?a.on("change:"+b,c):void 0},unsubscribe:function(a,b,c){return null!=a.off?a.off("change:"+b,c):void 0},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}}})}.call(this);
@@ -0,0 +1,750 @@
1
+ // Rivets.js
2
+ // version: 0.5.4
3
+ // author: Michael Richards
4
+ // license: MIT
5
+ (function() {
6
+ var Rivets,
7
+ __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
8
+ __slice = [].slice,
9
+ __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; };
10
+
11
+ Rivets = {};
12
+
13
+ if (!String.prototype.trim) {
14
+ String.prototype.trim = function() {
15
+ return this.replace(/^\s+|\s+$/g, '');
16
+ };
17
+ }
18
+
19
+ Rivets.Binding = (function() {
20
+ function Binding(view, el, type, key, keypath, options) {
21
+ var identifier, regexp, value, _ref;
22
+
23
+ this.view = view;
24
+ this.el = el;
25
+ this.type = type;
26
+ this.key = key;
27
+ this.keypath = keypath;
28
+ this.options = options != null ? options : {};
29
+ this.update = __bind(this.update, this);
30
+ this.unbind = __bind(this.unbind, this);
31
+ this.bind = __bind(this.bind, this);
32
+ this.publish = __bind(this.publish, this);
33
+ this.sync = __bind(this.sync, this);
34
+ this.set = __bind(this.set, this);
35
+ this.eventHandler = __bind(this.eventHandler, this);
36
+ this.formattedValue = __bind(this.formattedValue, this);
37
+ if (!(this.binder = this.view.binders[type])) {
38
+ _ref = this.view.binders;
39
+ for (identifier in _ref) {
40
+ value = _ref[identifier];
41
+ if (identifier !== '*' && identifier.indexOf('*') !== -1) {
42
+ regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$");
43
+ if (regexp.test(type)) {
44
+ this.binder = value;
45
+ this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(type);
46
+ this.args.shift();
47
+ }
48
+ }
49
+ }
50
+ }
51
+ this.binder || (this.binder = this.view.binders['*']);
52
+ if (this.binder instanceof Function) {
53
+ this.binder = {
54
+ routine: this.binder
55
+ };
56
+ }
57
+ this.formatters = this.options.formatters || [];
58
+ this.model = this.key ? this.view.models[this.key] : this.view.models;
59
+ }
60
+
61
+ Binding.prototype.formattedValue = function(value) {
62
+ var args, formatter, id, _i, _len, _ref;
63
+
64
+ _ref = this.formatters;
65
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
66
+ formatter = _ref[_i];
67
+ args = formatter.split(/\s+/);
68
+ id = args.shift();
69
+ formatter = this.model[id] instanceof Function ? this.model[id] : this.view.formatters[id];
70
+ if ((formatter != null ? formatter.read : void 0) instanceof Function) {
71
+ value = formatter.read.apply(formatter, [value].concat(__slice.call(args)));
72
+ } else if (formatter instanceof Function) {
73
+ value = formatter.apply(null, [value].concat(__slice.call(args)));
74
+ }
75
+ }
76
+ return value;
77
+ };
78
+
79
+ Binding.prototype.eventHandler = function(fn) {
80
+ var binding, handler;
81
+
82
+ handler = (binding = this).view.config.handler;
83
+ return function(ev) {
84
+ return handler.call(fn, this, ev, binding);
85
+ };
86
+ };
87
+
88
+ Binding.prototype.set = function(value) {
89
+ var _ref;
90
+
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
+
102
+ value = Rivets.Util.getInputValue(this.el);
103
+ _ref = this.formatters.slice(0).reverse();
104
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
105
+ formatter = _ref[_i];
106
+ args = formatter.split(/\s+/);
107
+ id = args.shift();
108
+ if ((_ref1 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
109
+ value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
110
+ }
111
+ }
112
+ return this.view.config.adapter.publish(this.model, this.keypath, value);
113
+ };
114
+
115
+ Binding.prototype.bind = function() {
116
+ var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
117
+
118
+ if ((_ref = this.binder.bind) != null) {
119
+ _ref.call(this, this.el);
120
+ }
121
+ if (this.options.bypass) {
122
+ this.sync();
123
+ } else {
124
+ this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
125
+ if (this.view.config.preloadData) {
126
+ this.sync();
127
+ }
128
+ }
129
+ if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) {
130
+ _ref2 = this.options.dependencies;
131
+ _results = [];
132
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
133
+ dependency = _ref2[_i];
134
+ if (/^\./.test(dependency)) {
135
+ model = this.model;
136
+ keypath = dependency.substr(1);
137
+ } else {
138
+ dependency = dependency.split('.');
139
+ model = this.view.models[dependency.shift()];
140
+ keypath = dependency.join('.');
141
+ }
142
+ _results.push(this.view.config.adapter.subscribe(model, keypath, this.sync));
143
+ }
144
+ return _results;
145
+ }
146
+ };
147
+
148
+ Binding.prototype.unbind = function() {
149
+ var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
150
+
151
+ if ((_ref = this.binder.unbind) != null) {
152
+ _ref.call(this, this.el);
153
+ }
154
+ if (!this.options.bypass) {
155
+ this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync);
156
+ }
157
+ if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) {
158
+ _ref2 = this.options.dependencies;
159
+ _results = [];
160
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
161
+ dependency = _ref2[_i];
162
+ if (/^\./.test(dependency)) {
163
+ model = this.model;
164
+ keypath = dependency.substr(1);
165
+ } else {
166
+ dependency = dependency.split('.');
167
+ model = this.view.models[dependency.shift()];
168
+ keypath = dependency.join('.');
169
+ }
170
+ _results.push(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
171
+ }
172
+ return _results;
173
+ }
174
+ };
175
+
176
+ Binding.prototype.update = function() {
177
+ this.unbind();
178
+ this.model = this.key ? this.view.models[this.key] : this.view.models;
179
+ return this.bind();
180
+ };
181
+
182
+ return Binding;
183
+
184
+ })();
185
+
186
+ Rivets.View = (function() {
187
+ function View(els, models, options) {
188
+ var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
189
+
190
+ this.els = els;
191
+ this.models = models;
192
+ this.options = options != null ? options : {};
193
+ this.update = __bind(this.update, this);
194
+ this.publish = __bind(this.publish, this);
195
+ this.sync = __bind(this.sync, this);
196
+ this.unbind = __bind(this.unbind, this);
197
+ this.bind = __bind(this.bind, this);
198
+ this.select = __bind(this.select, this);
199
+ this.build = __bind(this.build, this);
200
+ this.bindingRegExp = __bind(this.bindingRegExp, this);
201
+ if (!(this.els.jquery || this.els instanceof Array)) {
202
+ this.els = [this.els];
203
+ }
204
+ _ref = ['config', 'binders', 'formatters'];
205
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
206
+ option = _ref[_i];
207
+ this[option] = {};
208
+ if (this.options[option]) {
209
+ _ref1 = this.options[option];
210
+ for (k in _ref1) {
211
+ v = _ref1[k];
212
+ this[option][k] = v;
213
+ }
214
+ }
215
+ _ref2 = Rivets[option];
216
+ for (k in _ref2) {
217
+ v = _ref2[k];
218
+ if ((_ref3 = (_base = this[option])[k]) == null) {
219
+ _base[k] = v;
220
+ }
221
+ }
222
+ }
223
+ this.build();
224
+ }
225
+
226
+ View.prototype.bindingRegExp = function() {
227
+ var prefix;
228
+
229
+ prefix = this.config.prefix;
230
+ if (prefix) {
231
+ return new RegExp("^data-" + prefix + "-");
232
+ } else {
233
+ return /^data-/;
234
+ }
235
+ };
236
+
237
+ View.prototype.build = function() {
238
+ var bindingRegExp, el, node, parse, skipNodes, _i, _j, _len, _len1, _ref, _ref1,
239
+ _this = this;
240
+
241
+ this.bindings = [];
242
+ skipNodes = [];
243
+ bindingRegExp = this.bindingRegExp();
244
+ parse = function(node) {
245
+ var attribute, attributes, binder, context, ctx, dependencies, identifier, key, keypath, n, options, path, pipe, pipes, regexp, splitPath, type, value, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3;
246
+
247
+ if (__indexOf.call(skipNodes, node) < 0) {
248
+ _ref = node.attributes;
249
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
250
+ attribute = _ref[_i];
251
+ if (bindingRegExp.test(attribute.name)) {
252
+ type = attribute.name.replace(bindingRegExp, '');
253
+ if (!(binder = _this.binders[type])) {
254
+ _ref1 = _this.binders;
255
+ for (identifier in _ref1) {
256
+ value = _ref1[identifier];
257
+ if (identifier !== '*' && identifier.indexOf('*') !== -1) {
258
+ regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$");
259
+ if (regexp.test(type)) {
260
+ binder = value;
261
+ }
262
+ }
263
+ }
264
+ }
265
+ binder || (binder = _this.binders['*']);
266
+ if (binder.block) {
267
+ _ref2 = node.getElementsByTagName('*');
268
+ for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
269
+ n = _ref2[_j];
270
+ skipNodes.push(n);
271
+ }
272
+ attributes = [attribute];
273
+ }
274
+ }
275
+ }
276
+ _ref3 = attributes || node.attributes;
277
+ for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
278
+ attribute = _ref3[_k];
279
+ if (bindingRegExp.test(attribute.name)) {
280
+ options = {};
281
+ type = attribute.name.replace(bindingRegExp, '');
282
+ pipes = (function() {
283
+ var _l, _len3, _ref4, _results;
284
+
285
+ _ref4 = attribute.value.split('|');
286
+ _results = [];
287
+ for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) {
288
+ pipe = _ref4[_l];
289
+ _results.push(pipe.trim());
290
+ }
291
+ return _results;
292
+ })();
293
+ context = (function() {
294
+ var _l, _len3, _ref4, _results;
295
+
296
+ _ref4 = pipes.shift().split('<');
297
+ _results = [];
298
+ for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) {
299
+ ctx = _ref4[_l];
300
+ _results.push(ctx.trim());
301
+ }
302
+ return _results;
303
+ })();
304
+ path = context.shift();
305
+ splitPath = path.split(/\.|:/);
306
+ options.formatters = pipes;
307
+ options.bypass = path.indexOf(':') !== -1;
308
+ if (splitPath[0]) {
309
+ key = splitPath.shift();
310
+ } else {
311
+ key = null;
312
+ splitPath.shift();
313
+ }
314
+ keypath = splitPath.join('.');
315
+ if (!key || (_this.models[key] != null)) {
316
+ if (dependencies = context.shift()) {
317
+ options.dependencies = dependencies.split(/\s+/);
318
+ }
319
+ _this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
320
+ }
321
+ }
322
+ }
323
+ if (attributes) {
324
+ attributes = null;
325
+ }
326
+ }
327
+ };
328
+ _ref = this.els;
329
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
330
+ el = _ref[_i];
331
+ parse(el);
332
+ _ref1 = el.getElementsByTagName('*');
333
+ for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
334
+ node = _ref1[_j];
335
+ if (node.attributes != null) {
336
+ parse(node);
337
+ }
338
+ }
339
+ }
340
+ };
341
+
342
+ View.prototype.select = function(fn) {
343
+ var binding, _i, _len, _ref, _results;
344
+
345
+ _ref = this.bindings;
346
+ _results = [];
347
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
348
+ binding = _ref[_i];
349
+ if (fn(binding)) {
350
+ _results.push(binding);
351
+ }
352
+ }
353
+ return _results;
354
+ };
355
+
356
+ View.prototype.bind = function() {
357
+ var binding, _i, _len, _ref, _results;
358
+
359
+ _ref = this.bindings;
360
+ _results = [];
361
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
362
+ binding = _ref[_i];
363
+ _results.push(binding.bind());
364
+ }
365
+ return _results;
366
+ };
367
+
368
+ View.prototype.unbind = function() {
369
+ var binding, _i, _len, _ref, _results;
370
+
371
+ _ref = this.bindings;
372
+ _results = [];
373
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
374
+ binding = _ref[_i];
375
+ _results.push(binding.unbind());
376
+ }
377
+ return _results;
378
+ };
379
+
380
+ View.prototype.sync = function() {
381
+ var binding, _i, _len, _ref, _results;
382
+
383
+ _ref = this.bindings;
384
+ _results = [];
385
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
386
+ binding = _ref[_i];
387
+ _results.push(binding.sync());
388
+ }
389
+ return _results;
390
+ };
391
+
392
+ View.prototype.publish = function() {
393
+ var binding, _i, _len, _ref, _results;
394
+
395
+ _ref = this.select(function(b) {
396
+ return b.binder.publishes;
397
+ });
398
+ _results = [];
399
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
400
+ binding = _ref[_i];
401
+ _results.push(binding.publish());
402
+ }
403
+ return _results;
404
+ };
405
+
406
+ View.prototype.update = function(models) {
407
+ var binding, key, model, _results;
408
+
409
+ if (models == null) {
410
+ models = {};
411
+ }
412
+ _results = [];
413
+ for (key in models) {
414
+ model = models[key];
415
+ this.models[key] = model;
416
+ _results.push((function() {
417
+ var _i, _len, _ref, _results1;
418
+
419
+ _ref = this.select(function(b) {
420
+ return b.key === key;
421
+ });
422
+ _results1 = [];
423
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
424
+ binding = _ref[_i];
425
+ _results1.push(binding.update());
426
+ }
427
+ return _results1;
428
+ }).call(this));
429
+ }
430
+ return _results;
431
+ };
432
+
433
+ return View;
434
+
435
+ })();
436
+
437
+ Rivets.Util = {
438
+ bindEvent: function(el, event, handler) {
439
+ if (window.jQuery != null) {
440
+ el = jQuery(el);
441
+ if (el.on != null) {
442
+ return el.on(event, handler);
443
+ } else {
444
+ return el.bind(event, handler);
445
+ }
446
+ } else if (window.addEventListener != null) {
447
+ return el.addEventListener(event, handler, false);
448
+ } else {
449
+ event = 'on' + event;
450
+ return el.attachEvent(event, handler);
451
+ }
452
+ },
453
+ unbindEvent: function(el, event, handler) {
454
+ if (window.jQuery != null) {
455
+ el = jQuery(el);
456
+ if (el.off != null) {
457
+ return el.off(event, handler);
458
+ } else {
459
+ return el.unbind(event, handler);
460
+ }
461
+ } else if (window.removeEventListener != null) {
462
+ return el.removeEventListener(event, handler, false);
463
+ } else {
464
+ event = 'on' + event;
465
+ return el.detachEvent(event, handler);
466
+ }
467
+ },
468
+ getInputValue: function(el) {
469
+ var o, _i, _len, _results;
470
+
471
+ if (window.jQuery != null) {
472
+ el = jQuery(el);
473
+ switch (el[0].type) {
474
+ case 'checkbox':
475
+ return el.is(':checked');
476
+ default:
477
+ return el.val();
478
+ }
479
+ } else {
480
+ switch (el.type) {
481
+ case 'checkbox':
482
+ return el.checked;
483
+ case 'select-multiple':
484
+ _results = [];
485
+ for (_i = 0, _len = el.length; _i < _len; _i++) {
486
+ o = el[_i];
487
+ if (o.selected) {
488
+ _results.push(o.value);
489
+ }
490
+ }
491
+ return _results;
492
+ break;
493
+ default:
494
+ return el.value;
495
+ }
496
+ }
497
+ }
498
+ };
499
+
500
+ Rivets.binders = {
501
+ enabled: function(el, value) {
502
+ return el.disabled = !value;
503
+ },
504
+ disabled: function(el, value) {
505
+ return el.disabled = !!value;
506
+ },
507
+ checked: {
508
+ publishes: true,
509
+ bind: function(el) {
510
+ return Rivets.Util.bindEvent(el, 'change', this.publish);
511
+ },
512
+ unbind: function(el) {
513
+ return Rivets.Util.unbindEvent(el, 'change', this.publish);
514
+ },
515
+ routine: function(el, value) {
516
+ var _ref;
517
+
518
+ if (el.type === 'radio') {
519
+ return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
520
+ } else {
521
+ return el.checked = !!value;
522
+ }
523
+ }
524
+ },
525
+ unchecked: {
526
+ publishes: true,
527
+ bind: function(el) {
528
+ return Rivets.Util.bindEvent(el, 'change', this.publish);
529
+ },
530
+ unbind: function(el) {
531
+ return Rivets.Util.unbindEvent(el, 'change', this.publish);
532
+ },
533
+ routine: function(el, value) {
534
+ var _ref;
535
+
536
+ if (el.type === 'radio') {
537
+ return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
538
+ } else {
539
+ return el.checked = !value;
540
+ }
541
+ }
542
+ },
543
+ show: function(el, value) {
544
+ return el.style.display = value ? '' : 'none';
545
+ },
546
+ hide: function(el, value) {
547
+ return el.style.display = value ? 'none' : '';
548
+ },
549
+ html: function(el, value) {
550
+ return el.innerHTML = value != null ? value : '';
551
+ },
552
+ value: {
553
+ publishes: true,
554
+ bind: function(el) {
555
+ return Rivets.Util.bindEvent(el, 'change', this.publish);
556
+ },
557
+ unbind: function(el) {
558
+ return Rivets.Util.unbindEvent(el, 'change', this.publish);
559
+ },
560
+ routine: function(el, value) {
561
+ var o, _i, _len, _ref, _ref1, _ref2, _results;
562
+
563
+ if (window.jQuery != null) {
564
+ el = jQuery(el);
565
+ if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
566
+ return el.val(value != null ? value : '');
567
+ }
568
+ } else {
569
+ if (el.type === 'select-multiple') {
570
+ if (value != null) {
571
+ _results = [];
572
+ for (_i = 0, _len = el.length; _i < _len; _i++) {
573
+ o = el[_i];
574
+ _results.push(o.selected = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
575
+ }
576
+ return _results;
577
+ }
578
+ } else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.toString() : void 0)) {
579
+ return el.value = value != null ? value : '';
580
+ }
581
+ }
582
+ }
583
+ },
584
+ text: function(el, value) {
585
+ if (el.innerText != null) {
586
+ return el.innerText = value != null ? value : '';
587
+ } else {
588
+ return el.textContent = value != null ? value : '';
589
+ }
590
+ },
591
+ "on-*": {
592
+ "function": true,
593
+ unbind: function(el) {
594
+ if (this.handler) {
595
+ return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
596
+ }
597
+ },
598
+ routine: function(el, value) {
599
+ if (this.handler) {
600
+ Rivets.Util.unbindEvent(el, this.args[0], this.handler);
601
+ }
602
+ return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
603
+ }
604
+ },
605
+ "each-*": {
606
+ block: true,
607
+ bind: function(el) {
608
+ var attr;
609
+
610
+ if (this.marker == null) {
611
+ attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
612
+ this.marker = document.createComment(" rivets: " + this.type + " ");
613
+ this.iterated = [];
614
+ el.removeAttribute(attr);
615
+ el.parentNode.insertBefore(this.marker, el);
616
+ return el.parentNode.removeChild(el);
617
+ }
618
+ },
619
+ unbind: function(el) {
620
+ var view, _i, _len, _ref, _results;
621
+
622
+ if (this.iterated != null) {
623
+ _ref = this.iterated;
624
+ _results = [];
625
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
626
+ view = _ref[_i];
627
+ _results.push(view.unbind());
628
+ }
629
+ return _results;
630
+ }
631
+ },
632
+ routine: function(el, collection) {
633
+ var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results;
634
+
635
+ modelName = this.args[0];
636
+ collection = collection || [];
637
+ if (this.iterated.length > collection.length) {
638
+ _ref = Array(this.iterated.length - collection.length);
639
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
640
+ i = _ref[_i];
641
+ view = this.iterated.pop();
642
+ view.unbind();
643
+ this.marker.parentNode.removeChild(view.els[0]);
644
+ }
645
+ }
646
+ _results = [];
647
+ for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
648
+ model = collection[index];
649
+ data = {};
650
+ data[modelName] = model;
651
+ if (this.iterated[index] == null) {
652
+ _ref1 = this.view.models;
653
+ for (key in _ref1) {
654
+ model = _ref1[key];
655
+ data[key] = model;
656
+ }
657
+ previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
658
+ options = {
659
+ binders: this.view.options.binders,
660
+ formatters: this.view.options.formatters,
661
+ config: {}
662
+ };
663
+ _ref2 = this.view.options.config;
664
+ for (k in _ref2) {
665
+ v = _ref2[k];
666
+ options.config[k] = v;
667
+ }
668
+ options.config.preloadData = true;
669
+ template = el.cloneNode(true);
670
+ view = new Rivets.View(template, data, options);
671
+ view.bind();
672
+ this.iterated.push(view);
673
+ _results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
674
+ } else if (this.iterated[index].models[modelName] !== model) {
675
+ _results.push(this.iterated[index].update(data));
676
+ } else {
677
+ _results.push(void 0);
678
+ }
679
+ }
680
+ return _results;
681
+ }
682
+ },
683
+ "class-*": function(el, value) {
684
+ var elClass;
685
+
686
+ elClass = " " + el.className + " ";
687
+ if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) {
688
+ return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim();
689
+ }
690
+ },
691
+ "*": function(el, value) {
692
+ if (value) {
693
+ return el.setAttribute(this.type, value);
694
+ } else {
695
+ return el.removeAttribute(this.type);
696
+ }
697
+ }
698
+ };
699
+
700
+ Rivets.config = {
701
+ preloadData: true,
702
+ handler: function(context, ev, binding) {
703
+ return this.call(context, ev, binding.view.models);
704
+ }
705
+ };
706
+
707
+ Rivets.formatters = {};
708
+
709
+ Rivets.factory = function(exports) {
710
+ exports.binders = Rivets.binders;
711
+ exports.formatters = Rivets.formatters;
712
+ exports.config = Rivets.config;
713
+ exports.configure = function(options) {
714
+ var property, value;
715
+
716
+ if (options == null) {
717
+ options = {};
718
+ }
719
+ for (property in options) {
720
+ value = options[property];
721
+ Rivets.config[property] = value;
722
+ }
723
+ };
724
+ return exports.bind = function(el, models, options) {
725
+ var view;
726
+
727
+ if (models == null) {
728
+ models = {};
729
+ }
730
+ if (options == null) {
731
+ options = {};
732
+ }
733
+ view = new Rivets.View(el, models, options);
734
+ view.bind();
735
+ return view;
736
+ };
737
+ };
738
+
739
+ if (typeof exports === 'object') {
740
+ Rivets.factory(exports);
741
+ } else if (typeof define === 'function' && define.amd) {
742
+ define(['exports'], function(exports) {
743
+ Rivets.factory(this.rivets = exports);
744
+ return exports;
745
+ });
746
+ } else {
747
+ Rivets.factory(this.rivets = {});
748
+ }
749
+
750
+ }).call(this);