4f30463c by Michael Richards

Remove jQuery as a dependency.

1 parent 3f499b5a
......@@ -12,7 +12,7 @@
return bind(el, value);
});
if (__indexOf.call(bidirectionalBindings, type) >= 0) {
return $(el).bind('change', function() {
return el.addEventListener('change', function() {
return adapter.publish(context, keypath, getInputValue(this));
});
}
......@@ -22,36 +22,28 @@
mirrored = false;
}
if (value) {
return $(el).attr(attr, mirrored ? attr : value);
return el.setAttribute(attr, mirrored ? attr : value);
} else {
return $(el).removeAttr(attr);
return el.removeAttribute(attr);
}
};
getInputValue = function(el) {
switch ($(el).attr('type')) {
switch (el.type) {
case 'text':
case 'textarea':
case 'password':
case 'select-one':
return $(el).val();
return el.value;
case 'checkbox':
return $(el).is(':checked');
return el.checked;
}
};
bindings = {
show: function(el, value) {
if (value) {
return $(el).show();
} else {
return $(el).hide();
}
return el.style.display = value ? '' : 'none';
},
hide: function(el, value) {
if (value) {
return $(el).hide();
} else {
return $(el).show();
}
return el.style.display = value ? 'none' : '';
},
enabled: function(el, value) {
return setAttribute(el, 'disabled', !value, true);
......@@ -72,10 +64,10 @@
return setAttribute(el, 'checked', !value, true);
},
text: function(el, value) {
return $(el).text(value || '');
return el.innerHTML = value || '';
},
value: function(el, value) {
return $(el).val(value);
return el.value = value;
}
};
attributeBinding = function(attr) {
......@@ -86,27 +78,32 @@
bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
return {
bind: function(el, adapter, contexts) {
var nodes, _i, _ref, _results;
if (contexts == null) {
contexts = {};
}
return $(el).add($('*', el)).each(function() {
var nodeMap, target, _i, _ref, _results;
target = this;
nodeMap = target.attributes;
if (nodeMap.length > 0) {
nodes = el.getElementsByTagName('*');
return (function() {
_results = [];
for (var _i = 0, _ref = nodes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this).forEach(function(n) {
var node, _i, _ref, _results;
node = nodes[n];
if (node.attributes.length > 0) {
return (function() {
_results = [];
for (var _i = 0, _ref = nodeMap.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
for (var _i = 0, _ref = node.attributes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this).forEach(function(n) {
var context, keypath, node, path, type;
node = nodeMap[n];
if (/^data-/.test(node.name)) {
type = node.name.replace('data-', '');
path = node.value.split('.');
var attribute, context, keypath, path, type;
attribute = node.attributes[n];
if (/^data-/.test(attribute.name)) {
type = attribute.name.replace('data-', '');
path = attribute.value.split('.');
context = path.shift();
keypath = path.join('.');
return registerBinding($(target), adapter, type, contexts[context], keypath);
return registerBinding(node, adapter, type, contexts[context], keypath);
}
});
}
......
......@@ -12,25 +12,25 @@ window.rivets = do ->
bind el, value
if type in bidirectionalBindings
$(el).bind 'change', ->
el.addEventListener 'change', ->
adapter.publish context, keypath, getInputValue this
setAttribute = (el, attr, value, mirrored=false) ->
if value
$(el).attr attr, if mirrored then attr else value
el.setAttribute attr, if mirrored then attr else value
else
$(el).removeAttr attr
el.removeAttribute attr
getInputValue = (el) ->
switch $(el).attr 'type'
when 'text', 'textarea', 'password', 'select-one' then $(el).val()
when 'checkbox' then $(el).is ':checked'
switch el.type
when 'text', 'textarea', 'password', 'select-one' then el.value
when 'checkbox' then el.checked
bindings =
show: (el, value) ->
if value then $(el).show() else $(el).hide()
el.style.display = if value then '' else 'none'
hide: (el, value) ->
if value then $(el).hide() else $(el).show()
el.style.display = if value then 'none' else ''
enabled: (el, value) ->
setAttribute el, 'disabled', !value, true
disabled: (el, value) ->
......@@ -44,9 +44,9 @@ window.rivets = do ->
unselected: (el, value) ->
setAttribute el, 'checked', !value, true
text: (el, value) ->
$(el).text value or ''
el.innerHTML = value or ''
value: (el, value) ->
$(el).val value
el.value = value
attributeBinding = (attr) ->
(el, value) ->
......@@ -55,17 +55,18 @@ window.rivets = do ->
bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']
bind: (el, adapter, contexts={}) ->
$(el).add($('*', el)).each ->
target = this
nodeMap = target.attributes
nodes = el.getElementsByTagName '*'
if nodeMap.length > 0
[0..(nodeMap.length - 1)].forEach (n) ->
node = nodeMap[n]
[0..(nodes.length - 1)].forEach (n) ->
node = nodes[n]
if /^data-/.test node.name
type = node.name.replace 'data-', ''
path = node.value.split '.'
if node.attributes.length > 0
[0..(node.attributes.length - 1)].forEach (n) ->
attribute = node.attributes[n]
if /^data-/.test attribute.name
type = attribute.name.replace 'data-', ''
path = attribute.value.split '.'
context = path.shift()
keypath = path.join '.'
registerBinding $(target), adapter, type, contexts[context], keypath
registerBinding node, adapter, type, contexts[context], keypath
......