0c9223d6 by Michael Richards

Abstract common binding routine functions into reusable routine builders for att…

…ribute/state bindings.
1 parent e6440360
// Generated by CoffeeScript 1.3.1
(function() {
var attributeBinding, bidirectionalBindings, bindings, getInputValue, registerBinding, rivets, setAttribute,
var attributeBinding, bidirectionals, bindings, getInputValue, registerBinding, rivets, stateBinding,
__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; };
registerBinding = function(el, adapter, type, context, keypath) {
......@@ -10,24 +10,13 @@
adapter.subscribe(context, keypath, function(value) {
return bind(el, value);
});
if (__indexOf.call(bidirectionalBindings, type) >= 0) {
if (__indexOf.call(bidirectionals, type) >= 0) {
return el.addEventListener('change', function() {
return adapter.publish(context, keypath, getInputValue(this));
});
}
};
setAttribute = function(el, attr, value, mirrored) {
if (mirrored == null) {
mirrored = false;
}
if (value) {
return el.setAttribute(attr, mirrored ? attr : value);
} else {
return el.removeAttribute(attr);
}
};
getInputValue = function(el) {
switch (el.type) {
case 'text':
......@@ -40,31 +29,32 @@
}
};
attributeBinding = function(attr) {
return function(el, value) {
if (value) {
return el.setAttribute(attr, value);
} else {
return el.removeAttribute(attr);
}
};
};
stateBinding = function(attr, inverse) {
if (inverse == null) {
inverse = false;
}
return function(el, value) {
return attributeBinding(attr)(el, inverse === !value ? attr : false);
};
};
bindings = {
show: function(el, value) {
return el.style.display = value ? '' : 'none';
},
hide: function(el, value) {
return el.style.display = value ? 'none' : '';
},
enabled: function(el, value) {
return setAttribute(el, 'disabled', !value, true);
},
disabled: function(el, value) {
return setAttribute(el, 'disabled', value, true);
},
checked: function(el, value) {
return setAttribute(el, 'checked', value, true);
},
unchecked: function(el, value) {
return setAttribute(el, 'checked', !value, true);
},
selected: function(el, value) {
return setAttribute(el, 'selected', value, true);
},
unselected: function(el, value) {
return setAttribute(el, 'selected', !value, true);
},
checked: stateBinding('checked'),
selected: stateBinding('selected'),
disabled: stateBinding('disabled'),
unchecked: stateBinding('checked', true),
unselected: stateBinding('selected', true),
enabled: stateBinding('disabled', true),
text: function(el, value) {
return el.innerText = value || '';
},
......@@ -73,16 +63,16 @@
},
value: function(el, value) {
return el.value = value;
},
show: function(el, value) {
return el.style.display = value ? '' : 'none';
},
hide: function(el, value) {
return el.style.display = value ? 'none' : '';
}
};
attributeBinding = function(attr) {
return function(el, value) {
return setAttribute(el, attr, value);
};
};
bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
rivets = {
bind: function(el, adapter, contexts) {
......
......@@ -10,53 +10,49 @@ registerBinding = (el, adapter, type, context, keypath) ->
adapter.subscribe context, keypath, (value) ->
bind el, value
if type in bidirectionalBindings
if type in bidirectionals
el.addEventListener 'change', ->
adapter.publish context, keypath, getInputValue this
setAttribute = (el, attr, value, mirrored=false) ->
if value
el.setAttribute attr, if mirrored then attr else value
else
el.removeAttribute attr
getInputValue = (el) ->
switch el.type
when 'text', 'textarea', 'password', 'select-one' then el.value
when 'checkbox' then el.checked
attributeBinding = (attr) -> (el, value) ->
if value then el.setAttribute attr, value else el.removeAttribute attr
stateBinding = (attr, inverse = false) -> (el, value) ->
attributeBinding(attr) el, if inverse is !value then attr else false
bindings =
show: (el, value) ->
el.style.display = if value then '' else 'none'
hide: (el, value) ->
el.style.display = if value then 'none' else ''
enabled: (el, value) ->
setAttribute el, 'disabled', !value, true
disabled: (el, value) ->
setAttribute el, 'disabled', value, true
checked: (el, value) ->
setAttribute el, 'checked', value, true
unchecked: (el, value) ->
setAttribute el, 'checked', !value, true
selected: (el, value) ->
setAttribute el, 'selected', value, true
unselected: (el, value) ->
setAttribute el, 'selected', !value, true
checked:
stateBinding 'checked'
selected:
stateBinding 'selected'
disabled:
stateBinding 'disabled'
unchecked:
stateBinding 'checked', true
unselected:
stateBinding 'selected', true
enabled:
stateBinding 'disabled', true
text: (el, value) ->
el.innerText = value or ''
html: (el, value) ->
el.innerHTML = value or ''
value: (el, value) ->
el.value = value
show: (el, value) ->
el.style.display = if value then '' else 'none'
hide: (el, value) ->
el.style.display = if value then 'none' else ''
attributeBinding = (attr) ->
(el, value) ->
setAttribute el, attr, value
bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']
bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
rivets =
bind: (el, adapter, contexts={}) ->
bind: (el, adapter, contexts = {}) ->
nodes = el.getElementsByTagName '*'
[0..(nodes.length - 1)].forEach (n) ->
......