61c73853 by Michael Richards

Merge branch 'state-binding-refactor'

2 parents c60e14e6 57c11896
1 // Generated by CoffeeScript 1.3.3 1 // Generated by CoffeeScript 1.3.3
2 (function() { 2 (function() {
3 var Rivets, attributeBinding, bidirectionals, getInputValue, rivets, stateBinding, 3 var Rivets, attributeBinding, bidirectionals, getInputValue, rivets,
4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, 4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
5 __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; }; 5 __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; };
6 6
...@@ -151,59 +151,53 @@ ...@@ -151,59 +151,53 @@
151 151
152 attributeBinding = function(attr) { 152 attributeBinding = function(attr) {
153 return function(el, value) { 153 return function(el, value) {
154 switch (attr) { 154 if (value) {
155 case 'checked': 155 return el.setAttribute(attr, value);
156 el.setAttribute('checked', value); 156 } else {
157 el.checked = value; 157 return el.removeAttribute(attr);
158 return el.defaultChecked = value;
159 default:
160 if (value) {
161 return el.setAttribute(attr, value);
162 } else {
163 return el.removeAttribute(attr);
164 }
165 } 158 }
166 }; 159 };
167 }; 160 };
168 161
169 stateBinding = function(attr, inverse) {
170 if (inverse == null) {
171 inverse = false;
172 }
173 return function(el, value) {
174 var binding;
175 binding = attributeBinding(attr);
176 return binding(el, inverse === !value ? attr : false);
177 };
178 };
179
180 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']; 162 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
181 163
182 Rivets.routines = { 164 Rivets.routines = {
183 checked: stateBinding('checked'), 165 enabled: function(el, value) {
184 selected: stateBinding('selected'), 166 return el.disabled = !value;
185 disabled: stateBinding('disabled'),
186 unchecked: stateBinding('checked', true),
187 unselected: stateBinding('selected', true),
188 enabled: stateBinding('disabled', true),
189 text: function(el, value) {
190 if (el.innerText != null) {
191 return el.innerText = value || '';
192 } else {
193 return el.textContent = value || '';
194 }
195 }, 167 },
196 html: function(el, value) { 168 disabled: function(el, value) {
197 return el.innerHTML = value || ''; 169 return el.disabled = !!value;
198 }, 170 },
199 value: function(el, value) { 171 checked: function(el, value) {
200 return el.value = value; 172 return el.checked = !!value;
173 },
174 unchecked: function(el, value) {
175 return el.checked = !value;
176 },
177 selected: function(el, value) {
178 return el.selected = !!value;
179 },
180 unselected: function(el, value) {
181 return el.selected = !value;
201 }, 182 },
202 show: function(el, value) { 183 show: function(el, value) {
203 return el.style.display = value ? '' : 'none'; 184 return el.style.display = value ? '' : 'none';
204 }, 185 },
205 hide: function(el, value) { 186 hide: function(el, value) {
206 return el.style.display = value ? 'none' : ''; 187 return el.style.display = value ? 'none' : '';
188 },
189 html: function(el, value) {
190 return el.innerHTML = value || '';
191 },
192 value: function(el, value) {
193 return el.value = value;
194 },
195 text: function(el, value) {
196 if (el.innerText != null) {
197 return el.innerText = value || '';
198 } else {
199 return el.textContent = value || '';
200 }
207 } 201 }
208 }; 202 };
209 203
......
...@@ -86,23 +86,7 @@ getInputValue = (el) -> ...@@ -86,23 +86,7 @@ getInputValue = (el) ->
86 # Returns an attribute binding routine for the specified attribute. This is what 86 # Returns an attribute binding routine for the specified attribute. This is what
87 # `registerBinding` falls back to when there is no routine for the binding type. 87 # `registerBinding` falls back to when there is no routine for the binding type.
88 attributeBinding = (attr) -> (el, value) -> 88 attributeBinding = (attr) -> (el, value) ->
89 switch attr 89 if value then el.setAttribute attr, value else el.removeAttribute attr
90 when 'checked'
91 el.setAttribute( 'checked', value )
92 el.checked = value
93 el.defaultChecked = value
94 else
95 if value
96 el.setAttribute attr, value
97 else
98 el.removeAttribute attr
99
100 # Returns a state binding routine for the specified attribute. Can optionally be
101 # negatively evaluated. This is used to build a lot of the core state binding
102 # routines.
103 stateBinding = (attr, inverse = false) -> (el, value) ->
104 binding = attributeBinding(attr)
105 binding el, if inverse is !value then attr else false
106 90
107 # Bindings that should also be observed for changes on the DOM element in order 91 # Bindings that should also be observed for changes on the DOM element in order
108 # to propagate those changes back to the model object. 92 # to propagate those changes back to the model object.
...@@ -110,31 +94,31 @@ bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'] ...@@ -110,31 +94,31 @@ bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
110 94
111 # Core binding routines. 95 # Core binding routines.
112 Rivets.routines = 96 Rivets.routines =
113 checked: 97 enabled: (el, value) ->
114 stateBinding 'checked' 98 el.disabled = !value
115 selected: 99 disabled: (el, value) ->
116 stateBinding 'selected' 100 el.disabled = !!value
117 disabled: 101 checked: (el, value) ->
118 stateBinding 'disabled' 102 el.checked = !!value
119 unchecked: 103 unchecked: (el, value) ->
120 stateBinding 'checked', true 104 el.checked = !value
121 unselected: 105 selected: (el, value) ->
122 stateBinding 'selected', true 106 el.selected = !!value
123 enabled: 107 unselected: (el, value) ->
124 stateBinding 'disabled', true 108 el.selected = !value
109 show: (el, value) ->
110 el.style.display = if value then '' else 'none'
111 hide: (el, value) ->
112 el.style.display = if value then 'none' else ''
113 html: (el, value) ->
114 el.innerHTML = value or ''
115 value: (el, value) ->
116 el.value = value
125 text: (el, value) -> 117 text: (el, value) ->
126 if el.innerText? 118 if el.innerText?
127 el.innerText = value or '' 119 el.innerText = value or ''
128 else 120 else
129 el.textContent = value or '' 121 el.textContent = value or ''
130 html: (el, value) ->
131 el.innerHTML = value or ''
132 value: (el, value) ->
133 el.value = value
134 show: (el, value) ->
135 el.style.display = if value then '' else 'none'
136 hide: (el, value) ->
137 el.style.display = if value then 'none' else ''
138 122
139 # Default configuration. 123 # Default configuration.
140 Rivets.config = 124 Rivets.config =
......