-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselect.js
45 lines (38 loc) · 1.28 KB
/
select.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
/*global m */
// Select ======================================================================
var mc = mc || {};
mc.Select = {
controller: function (value) {
this.value = mc.utils.setParam(value, '');
},
view: function (ctrl, options) {
options = options || {};
var items = options.items || {},
caption = options.caption || '',
selectors = options.selectors || {},
attrs = options.attrs || {};
var attrsParent = attrs._parent || {};
attrsParent.onchange = attrsParent.onchange || m.withAttr('value', ctrl.value);
attrsParent.value = attrsParent.value || ctrl.value;
return m('select' + (selectors.parent || ''),
mc.utils.extend({}, attrs.parent || {}, {
value: ctrl.value(),
onchange: attrs.parent && attrs.parent.onchange ? attrs.parent.onchange : m.withAttr('value', ctrl.value)
}),
[
caption ? m('option', caption) : '',
Object.keys(items).map(composeItem)
]
);
function composeItem(key) {
console.log(items[key]);
return m('option' + (selectors.item || '') + (selectors[key] || ''),
mc.utils.extend({}, attrs[key] || {}, {
value: key,
selected: key === ctrl.value()
}),
mc.utils.resolveChild(items[key])
);
}
}
};