-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathselect.js
56 lines (46 loc) · 1.7 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
46
47
48
49
50
51
52
53
54
55
56
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
module.exports = Select
inherits(Select, EventEmitter)
function Select (root, opts, theme, uuid) {
if (!(this instanceof Select)) return new Select(root, opts, theme, uuid)
var self = this
var i, container, input, downTriangle, upTriangle, key, option, el, keys
container = require('./container')(root, opts.label)
require('./label')(container, opts.label, theme)
input = document.createElement('select')
input.className = 'control-panel-select-' + uuid + '-dropdown'
downTriangle = document.createElement('span')
downTriangle.className = 'control-panel-select-' + uuid + '-triangle control-panel-select-' + uuid + '-triangle--down'
upTriangle = document.createElement('span')
upTriangle.className = 'control-panel-select-' + uuid + '-triangle control-panel-select-' + uuid + '-triangle--up'
container.appendChild(downTriangle)
container.appendChild(upTriangle)
if (Array.isArray(opts.options)) {
for (i = 0; i < opts.options.length; i++) {
option = opts.options[i]
el = document.createElement('option')
el.value = el.textContent = option
if (opts.initial === option) {
el.selected = 'selected'
}
input.appendChild(el)
}
} else {
keys = Object.keys(opts.options)
for (i = 0; i < keys.length; i++) {
key = keys[i]
el = document.createElement('option')
el.value = key
if (opts.initial === key) {
el.selected = 'selected'
}
el.textContent = opts.options[key]
input.appendChild(el)
}
}
container.appendChild(input)
input.onchange = function (data) {
self.emit('input', data.target.value)
}
}