rivets-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ // Rivets.js
2
+ // version: 0.5.4
3
+ // author: Michael Richards
4
+ // license: MIT
5
+ (function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,l,u,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(o in a)u=a[o],"*"!==o&&-1!==o.indexOf("*")&&(l=RegExp("^"+o.replace("*",".+")+"$"),l.test(n)&&(this.binder=u,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),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 n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,l,u,a;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),h=0,o=l.length;o>h;h++)n=l[h],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.bind)&&r.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!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(){return this.unbind(),this.model=this.key?this.view.models[this.key]:this.view.models,this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,l,u,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],u=0,a=d.length;a>u;u++){if(h=d[u],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(l=this[h])[r])&&(l[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,l,u,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,a,d,c,p,b,v,g,y,m,w,k,E,x,j,U,N,V,Q,R,B,A,F,H;if(0>n.call(h,e)){for(B=e.attributes,j=0,V=B.length;V>j;j++)if(s=B[j],i.test(s.name)){if(E=s.name.replace(i,""),!(o=f.binders[E])){A=f.binders;for(d in A)x=A[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(o=x))}if(o||(o=f.binders["*"]),o.block){for(F=e.getElementsByTagName("*"),U=0,Q=F.length;Q>U;U++)b=F[U],h.push(b);r=[s]}}for(H=r||e.attributes,N=0,R=H.length;R>N;N++)s=H[N],i.test(s.name)&&(v={},E=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),l=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)u=e[t],n.push(u.trim());return n}(),g=l.shift(),k=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((a=l.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,o=0,u=d.length;u>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),l=0,a=c.length;a>l;l++)s=c[l],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],s=0,r=t.length;r>s;s++)e=t[s],u.push(e.selected=(o=e.value,n.call(i,o)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,l,u,a,d,c,f,p,b,v,g,y,m,w,k,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,y=e.length;y>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],n[o]=l;d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},k=this.view.options.config;for(h in k)f=k[h],a.config[h]=f;a.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,a),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,d.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this);
@@ -0,0 +1,8 @@
1
+ require "rivets-rails/version"
2
+ require "rivets-rails/railtie"
3
+
4
+ module Rivets
5
+ module Rails
6
+ end
7
+ end
8
+
@@ -0,0 +1,14 @@
1
+ module Rivets
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+
5
+ initializer "sprockets.rivets-rails", after: "append_asset_paths", group: :all do |app|
6
+ next unless app.config.assets.enabled
7
+ path = File.expand_path("../../../dist", __FILE__)
8
+ app.config.assets.paths << path unless app.config.assets.paths.include?(path)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,5 @@
1
+ module Rivets
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "rivets.custom_by_vkill",
3
+ "description": "Rivets custom adapter, binders and formatters.",
4
+ "keywords": [
5
+ "rivets",
6
+ "adapter",
7
+ "binders",
8
+ "formatters"
9
+ ],
10
+ "version": "0.1.0",
11
+ "author": "vkill",
12
+ "url": "https://github.com/vkill/rivets-rails",
13
+ "main": "./dist/rivets.custom.binders.js",
14
+ "licenses": [
15
+ {
16
+ "type": "MIT",
17
+ "url": "https://github.com/vkill/rivets-rails/blob/master/LICENSE.txt"
18
+ }
19
+ ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/vkill/rivets-rails.git"
23
+ },
24
+ "dependencies": {
25
+ "rivets": "~0.5.4"
26
+ },
27
+ "devDependencies": {
28
+ "grunt": "~0.4.1",
29
+ "grunt-contrib-coffee": "~0.7.0",
30
+ "grunt-contrib-concat": "~0.3.0",
31
+ "grunt-contrib-uglify": "~0.2.0",
32
+ "grunt-contrib-watch": "~0.3.1"
33
+ },
34
+ "engines": {
35
+ "node": ">= 0.8.0"
36
+ }
37
+ }
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rivets-rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rivets-rails"
8
+ gem.version = Rivets::Rails::VERSION
9
+ gem.authors = ["vkill"]
10
+ gem.email = ["vkill.net@gmail.com"]
11
+ gem.description = %q{Rivets plugin for Rails}
12
+ gem.summary = %q{Rivets plugin for Rails}
13
+ gem.homepage = "https://github.com/vkill/rivets-rails"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,17 @@
1
+ rivets = @rivets
2
+
3
+ #
4
+ # Support of Contenteditable
5
+ # http://jsfiddle.net/3yLQY/4/
6
+ #
7
+ rivets.binders.content =
8
+ routine: rivets.binders.text
9
+ bind: (el) ->
10
+ self = this
11
+ @callback = ->
12
+ rivets.config.adapter.publish self.model, self.keypath, el.textContent
13
+
14
+ el.addEventListener "input", @callback, false
15
+
16
+ unbind: (el) ->
17
+ el.removeEventListener "input", @callback, false
@@ -0,0 +1,5 @@
1
+ rivets = @rivets
2
+
3
+ rivets.configure
4
+ prefix:
5
+ 'bind'
@@ -0,0 +1,63 @@
1
+ rivets = @rivets
2
+
3
+ #
4
+ # https://github.com/mikeric/rivets/wiki/Example-formatters
5
+ #
6
+ rivets.formatters.shortdate = (value) ->
7
+ moment(value).format "YYYY-MM-DD"
8
+
9
+ rivets.formatters.number = (value) ->
10
+ +value
11
+
12
+ rivets.formatters.string = (value) ->
13
+ String value
14
+
15
+ # Boolean negation
16
+ rivets.formatters.negate = (value) ->
17
+ not value
18
+
19
+ # data-class-disabled="model.valid | negate"
20
+
21
+ # Equals
22
+ rivets.formatters.eq = (value, args) ->
23
+ value is args
24
+
25
+ # data-class-disable="model.foo | number | eq 4"
26
+
27
+ # Not equal
28
+ rivets.formatters.not_eq = (value, args) ->
29
+ value isnt args
30
+
31
+ # data-class-disable="model.foo | not_eq 4"
32
+
33
+ # Greater than
34
+ rivets.formatters.gt = (value, arg) ->
35
+ value > arg
36
+
37
+ # data-class-disable="model.foo | gt 4"
38
+
39
+ # Lower than
40
+ rivets.formatters.lt = (value, arg) ->
41
+ value < arg
42
+
43
+ # data-class-disable="model.foo | lt 4"
44
+
45
+ # Blank
46
+ rivets.formatters.blank = (value) ->
47
+ not value? or value is ""
48
+
49
+ # data-hide="model.foo | blank"
50
+
51
+ # Event handler preventDefault
52
+ rivets.formatters.preventDefault = (value) ->
53
+ (e) ->
54
+ e.preventDefault()
55
+ value.call this, e
56
+ false
57
+
58
+ # data-on-submit="model:save | preventDefault"
59
+
60
+
61
+
62
+ rivets.formatters.length = (value) ->
63
+ value.length
@@ -0,0 +1,17 @@
1
+ rivets = @rivets
2
+
3
+ rivets.configure
4
+ adapter:
5
+ subscribe: (obj, keypath, callback) ->
6
+ if obj.on?
7
+ obj.on "change:" + keypath, callback
8
+
9
+ unsubscribe: (obj, keypath, callback) ->
10
+ if obj.off?
11
+ obj.off "change:" + keypath, callback
12
+
13
+ read: (obj, keypath) ->
14
+ obj[keypath]
15
+
16
+ publish: (obj, keypath, value) ->
17
+ obj[keypath] = value
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rivets-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - vkill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Rivets plugin for Rails
15
+ email:
16
+ - vkill.net@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gruntfile.coffee
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - build/update.sh
28
+ - dist/rivets.custom.binders.js
29
+ - dist/rivets.custom.configure.js
30
+ - dist/rivets.custom.formatters.js
31
+ - dist/rivets.custom.general_adapter.js
32
+ - dist/rivets.general_custom.js
33
+ - dist/rivets.general_custom.js.min.js
34
+ - dist/rivets.js
35
+ - dist/rivets.min.js
36
+ - lib/rivets-rails.rb
37
+ - lib/rivets-rails/railtie.rb
38
+ - lib/rivets-rails/version.rb
39
+ - package.json
40
+ - rivets-rails.gemspec
41
+ - src/rivets.custom.binders.coffee
42
+ - src/rivets.custom.configure.coffee
43
+ - src/rivets.custom.formatters.coffee
44
+ - src/rivets.custom.general_adapter.coffee
45
+ homepage: https://github.com/vkill/rivets-rails
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.24
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Rivets plugin for Rails
69
+ test_files: []