-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
bidi.js
50 lines (44 loc) · 1.22 KB
/
bidi.js
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
function bidi(node, prop) {
var type = node.tag === 'select' ? (node.attrs.multi ? 'multi' : 'select') : node.attrs.type;
// Setup: bind listeners
if (type === 'multi') {
node.attrs.onchange = function () {
prop(
[].slice.call(this.selectedOptions, function (x) {
return x.value;
})
);
};
} else if (type === 'select') {
node.attrs.onchange = function (e) {
prop(this.selectedOptions[0].value);
};
} else if (type === 'checkbox') {
node.attrs.onchange = function (e) {
prop(this.checked);
};
} else {
node.attrs.onchange = node.attrs.oninput = function (e) {
prop(this.value);
};
}
if (node.tag === 'select') {
node.children.forEach(function (option) {
if (option.attrs.value === prop() || option.children[0] === prop()) {
option.attrs.selected = true;
}
});
} else if (type === 'checkbox') {
node.attrs.checked = prop();
} else if (type === 'radio') {
node.attrs.checked = prop() === node.attrs.value;
} else {
node.attrs.value = prop();
}
node.attrs.bidi = null;
return node;
}
bidi.view = function (ctrl, node, prop) {
return bidi(node, node.attrs.bidi);
};
export default bidi;