d9e1bb5d by Michael Richards

Export library for both CommonJS and the browser.

1 parent 4f30463c
1 // Generated by CoffeeScript 1.3.1 1 // Generated by CoffeeScript 1.3.1
2 (function() { 2 (function() {
3 var __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; }; 3 var attributeBinding, bidirectionalBindings, bindings, getInputValue, registerBinding, rivets, setAttribute,
4 __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; };
4 5
5 window.rivets = (function() { 6 registerBinding = function(el, adapter, type, context, keypath) {
6 var attributeBinding, bidirectionalBindings, bindings, getInputValue, registerBinding, setAttribute; 7 var bind;
7 registerBinding = function(el, adapter, type, context, keypath) { 8 bind = bindings[type] || attributeBinding(type);
8 var bind; 9 bind(el, adapter.read(context, keypath));
9 bind = bindings[type] || attributeBinding(type); 10 adapter.subscribe(context, keypath, function(value) {
10 bind(el, adapter.read(context, keypath)); 11 return bind(el, value);
11 adapter.subscribe(context, keypath, function(value) { 12 });
12 return bind(el, value); 13 if (__indexOf.call(bidirectionalBindings, type) >= 0) {
14 return el.addEventListener('change', function() {
15 return adapter.publish(context, keypath, getInputValue(this));
13 }); 16 });
14 if (__indexOf.call(bidirectionalBindings, type) >= 0) { 17 }
15 return el.addEventListener('change', function() { 18 };
16 return adapter.publish(context, keypath, getInputValue(this)); 19
17 }); 20 setAttribute = function(el, attr, value, mirrored) {
18 } 21 if (mirrored == null) {
19 }; 22 mirrored = false;
20 setAttribute = function(el, attr, value, mirrored) { 23 }
21 if (mirrored == null) { 24 if (value) {
22 mirrored = false; 25 return el.setAttribute(attr, mirrored ? attr : value);
23 } 26 } else {
24 if (value) { 27 return el.removeAttribute(attr);
25 return el.setAttribute(attr, mirrored ? attr : value); 28 }
26 } else { 29 };
27 return el.removeAttribute(attr); 30
28 } 31 getInputValue = function(el) {
29 }; 32 switch (el.type) {
30 getInputValue = function(el) { 33 case 'text':
31 switch (el.type) { 34 case 'textarea':
32 case 'text': 35 case 'password':
33 case 'textarea': 36 case 'select-one':
34 case 'password': 37 return el.value;
35 case 'select-one': 38 case 'checkbox':
36 return el.value; 39 return el.checked;
37 case 'checkbox': 40 }
38 return el.checked; 41 };
39 } 42
43 bindings = {
44 show: function(el, value) {
45 return el.style.display = value ? '' : 'none';
46 },
47 hide: function(el, value) {
48 return el.style.display = value ? 'none' : '';
49 },
50 enabled: function(el, value) {
51 return setAttribute(el, 'disabled', !value, true);
52 },
53 disabled: function(el, value) {
54 return setAttribute(el, 'disabled', value, true);
55 },
56 checked: function(el, value) {
57 return setAttribute(el, 'checked', value, true);
58 },
59 unchecked: function(el, value) {
60 return setAttribute(el, 'checked', !value, true);
61 },
62 selected: function(el, value) {
63 return setAttribute(el, 'selected', value, true);
64 },
65 unselected: function(el, value) {
66 return setAttribute(el, 'checked', !value, true);
67 },
68 text: function(el, value) {
69 return el.innerHTML = value || '';
70 },
71 value: function(el, value) {
72 return el.value = value;
73 }
74 };
75
76 attributeBinding = function(attr) {
77 return function(el, value) {
78 return setAttribute(el, attr, value);
40 }; 79 };
41 bindings = { 80 };
42 show: function(el, value) { 81
43 return el.style.display = value ? '' : 'none'; 82 bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
44 }, 83
45 hide: function(el, value) { 84 rivets = {
46 return el.style.display = value ? 'none' : ''; 85 bind: function(el, adapter, contexts) {
47 }, 86 var nodes, _i, _ref, _results;
48 enabled: function(el, value) { 87 if (contexts == null) {
49 return setAttribute(el, 'disabled', !value, true); 88 contexts = {};
50 },
51 disabled: function(el, value) {
52 return setAttribute(el, 'disabled', value, true);
53 },
54 checked: function(el, value) {
55 return setAttribute(el, 'checked', value, true);
56 },
57 unchecked: function(el, value) {
58 return setAttribute(el, 'checked', !value, true);
59 },
60 selected: function(el, value) {
61 return setAttribute(el, 'selected', value, true);
62 },
63 unselected: function(el, value) {
64 return setAttribute(el, 'checked', !value, true);
65 },
66 text: function(el, value) {
67 return el.innerHTML = value || '';
68 },
69 value: function(el, value) {
70 return el.value = value;
71 } 89 }
72 }; 90 nodes = el.getElementsByTagName('*');
73 attributeBinding = function(attr) { 91 return (function() {
74 return function(el, value) { 92 _results = [];
75 return setAttribute(el, attr, value); 93 for (var _i = 0, _ref = nodes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
76 }; 94 return _results;
77 }; 95 }).apply(this).forEach(function(n) {
78 bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']; 96 var node, _i, _ref, _results;
79 return { 97 node = nodes[n];
80 bind: function(el, adapter, contexts) { 98 if (node.attributes.length > 0) {
81 var nodes, _i, _ref, _results; 99 return (function() {
82 if (contexts == null) { 100 _results = [];
83 contexts = {}; 101 for (var _i = 0, _ref = node.attributes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
102 return _results;
103 }).apply(this).forEach(function(n) {
104 var attribute, context, keypath, path, type;
105 attribute = node.attributes[n];
106 if (/^data-/.test(attribute.name)) {
107 type = attribute.name.replace('data-', '');
108 path = attribute.value.split('.');
109 context = path.shift();
110 keypath = path.join('.');
111 return registerBinding(node, adapter, type, contexts[context], keypath);
112 }
113 });
84 } 114 }
85 nodes = el.getElementsByTagName('*'); 115 });
86 return (function() { 116 }
87 _results = []; 117 };
88 for (var _i = 0, _ref = nodes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); } 118
89 return _results; 119 if (typeof module !== "undefined" && module !== null) {
90 }).apply(this).forEach(function(n) { 120 module.exports = rivets;
91 var node, _i, _ref, _results; 121 } else {
92 node = nodes[n]; 122 this.rivets = rivets;
93 if (node.attributes.length > 0) { 123 }
94 return (function() {
95 _results = [];
96 for (var _i = 0, _ref = node.attributes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
97 return _results;
98 }).apply(this).forEach(function(n) {
99 var attribute, context, keypath, path, type;
100 attribute = node.attributes[n];
101 if (/^data-/.test(attribute.name)) {
102 type = attribute.name.replace('data-', '');
103 path = attribute.value.split('.');
104 context = path.shift();
105 keypath = path.join('.');
106 return registerBinding(node, adapter, type, contexts[context], keypath);
107 }
108 });
109 }
110 });
111 }
112 };
113 })();
114 124
115 }).call(this); 125 }).call(this);
......
...@@ -3,63 +3,63 @@ ...@@ -3,63 +3,63 @@
3 # author : Michael Richards 3 # author : Michael Richards
4 # license : MIT 4 # license : MIT
5 5
6 window.rivets = do -> 6 registerBinding = (el, adapter, type, context, keypath) ->
7 registerBinding = (el, adapter, type, context, keypath) -> 7 bind = bindings[type] || attributeBinding type
8 bind = bindings[type] || attributeBinding type 8 bind el, adapter.read context, keypath
9 bind el, adapter.read context, keypath
10 9
11 adapter.subscribe context, keypath, (value) -> 10 adapter.subscribe context, keypath, (value) ->
12 bind el, value 11 bind el, value
13 12
14 if type in bidirectionalBindings 13 if type in bidirectionalBindings
15 el.addEventListener 'change', -> 14 el.addEventListener 'change', ->
16 adapter.publish context, keypath, getInputValue this 15 adapter.publish context, keypath, getInputValue this
17 16
18 setAttribute = (el, attr, value, mirrored=false) -> 17 setAttribute = (el, attr, value, mirrored=false) ->
19 if value 18 if value
20 el.setAttribute attr, if mirrored then attr else value 19 el.setAttribute attr, if mirrored then attr else value
21 else 20 else
22 el.removeAttribute attr 21 el.removeAttribute attr
23 22
24 getInputValue = (el) -> 23 getInputValue = (el) ->
25 switch el.type 24 switch el.type
26 when 'text', 'textarea', 'password', 'select-one' then el.value 25 when 'text', 'textarea', 'password', 'select-one' then el.value
27 when 'checkbox' then el.checked 26 when 'checkbox' then el.checked
28 27
29 bindings = 28 bindings =
30 show: (el, value) -> 29 show: (el, value) ->
31 el.style.display = if value then '' else 'none' 30 el.style.display = if value then '' else 'none'
32 hide: (el, value) -> 31 hide: (el, value) ->
33 el.style.display = if value then 'none' else '' 32 el.style.display = if value then 'none' else ''
34 enabled: (el, value) -> 33 enabled: (el, value) ->
35 setAttribute el, 'disabled', !value, true 34 setAttribute el, 'disabled', !value, true
36 disabled: (el, value) -> 35 disabled: (el, value) ->
37 setAttribute el, 'disabled', value, true 36 setAttribute el, 'disabled', value, true
38 checked: (el, value) -> 37 checked: (el, value) ->
39 setAttribute el, 'checked', value, true 38 setAttribute el, 'checked', value, true
40 unchecked: (el, value) -> 39 unchecked: (el, value) ->
41 setAttribute el, 'checked', !value, true 40 setAttribute el, 'checked', !value, true
42 selected: (el, value) -> 41 selected: (el, value) ->
43 setAttribute el, 'selected', value, true 42 setAttribute el, 'selected', value, true
44 unselected: (el, value) -> 43 unselected: (el, value) ->
45 setAttribute el, 'checked', !value, true 44 setAttribute el, 'checked', !value, true
46 text: (el, value) -> 45 text: (el, value) ->
47 el.innerHTML = value or '' 46 el.innerHTML = value or ''
48 value: (el, value) -> 47 value: (el, value) ->
49 el.value = value 48 el.value = value
50 49
51 attributeBinding = (attr) -> 50 attributeBinding = (attr) ->
52 (el, value) -> 51 (el, value) ->
53 setAttribute el, attr, value 52 setAttribute el, attr, value
54 53
55 bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'] 54 bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']
56 55
56 rivets =
57 bind: (el, adapter, contexts={}) -> 57 bind: (el, adapter, contexts={}) ->
58 nodes = el.getElementsByTagName '*' 58 nodes = el.getElementsByTagName '*'
59 59
60 [0..(nodes.length - 1)].forEach (n) -> 60 [0..(nodes.length - 1)].forEach (n) ->
61 node = nodes[n] 61 node = nodes[n]
62 62
63 if node.attributes.length > 0 63 if node.attributes.length > 0
64 [0..(node.attributes.length - 1)].forEach (n) -> 64 [0..(node.attributes.length - 1)].forEach (n) ->
65 attribute = node.attributes[n] 65 attribute = node.attributes[n]
...@@ -70,3 +70,8 @@ window.rivets = do -> ...@@ -70,3 +70,8 @@ window.rivets = do ->
70 context = path.shift() 70 context = path.shift()
71 keypath = path.join '.' 71 keypath = path.join '.'
72 registerBinding node, adapter, type, contexts[context], keypath 72 registerBinding node, adapter, type, contexts[context], keypath
73
74 if module?
75 module.exports = rivets
76 else
77 @rivets = rivets
...\ No newline at end of file ...\ No newline at end of file
......