rivets.js
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
window.rivets = (function() {
var attr, bindableAttributes, bindings, getInputValue, registerBinding, setAttribute, _fn, _i, _len;
registerBinding = function(el, interface, type, context, keypath) {
var inputValue;
bindings[type](el, interface.read(context, keypath));
interface.subscribe(context, keypath, function(value) {
return bindings[type](el, value);
});
if (inputValue = getInputValue(el)) {
return interface.publish(context, keypath, inputValue);
}
};
setAttribute = function(el, attr, value, mirrored) {
if (mirrored == null) mirrored = false;
if (value) {
return $(el).attr(attr, mirrored ? attr : value);
} else {
return $(el).removeAttr(attr);
}
};
getInputValue = function(el) {
switch ($(el).attr('type')) {
case 'text':
case 'textarea':
case 'password':
case 'select-one':
return $(this).val();
case 'checkbox':
return $(this).is(':checked');
}
};
bindings = {
show: function(el, value) {
if (value) {
return $(el).show();
} else {
return $(el).hide();
}
},
hide: function(el, value) {
if (value) {
return $(el).hide();
} else {
return $(el).show();
}
},
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, 'checked', !value, true);
},
text: function(el, value) {
return $(el).text(value || '');
},
value: function(el, value) {
return $(el).val(value);
}
};
bindableAttributes = ['id', 'class', 'name', 'src', 'href', 'alt', 'title', 'placeholder'];
_fn = function(attr) {
return bindings[attr] = function(el, value) {
return setAttribute(el, attr, value);
};
};
for (_i = 0, _len = bindableAttributes.length; _i < _len; _i++) {
attr = bindableAttributes[_i];
_fn(attr);
}
return {
bind: function(el, interface, contexts) {
if (contexts == null) contexts = {};
return $(el).add($('*', el)).each(function() {
var nodeMap, target, _j, _ref, _results;
target = this;
nodeMap = target.attributes;
if (nodeMap.length > 0) {
return (function() {
_results = [];
for (var _j = 0, _ref = nodeMap.length - 1; 0 <= _ref ? _j <= _ref : _j >= _ref; 0 <= _ref ? _j++ : _j--){ _results.push(_j); }
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-', '');
if (_.include(_.keys(bindings), type)) {
path = node.value.split('.');
context = path.shift();
keypath = path.join('.');
return registerBinding($(target), interface, type, contexts[context], keypath);
}
}
});
}
});
}
};
})();