-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropdown.coffee
133 lines (118 loc) · 4.16 KB
/
Dropdown.coffee
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
m = require 'mithril'
s = require 'mss-js'
downIcon = require 'mmsvg/google/msvg/navigation/expand-more'
style = require './style'
u = require './utils'
AutoHide = require './AutoHide'
class Dropdown
constructor: ({
@itemArray # [String]
, @currentIndex # Int | Undefined
, @placeholder = '' # String
, @onSelect = u.noOp # (String, Int) -> ...
, @ifAvailable = (-> true) # (String, Int) -> ture | false
, @allowEmptySelect = true # Bool
, @showDownArrow = true
}) ->
if (@allowEmptySelect == false) and !@itemArray[@currentIndex]
throw "currentIndex is illegal"
@filter = ''
@autoHideDropDown = new AutoHide
onHide: => @filter = ''
widget: view: =>
m 'ul.DropdownList', onclick: @onSelectInternal,
for item, i in @itemArray when ((item.indexOf @filter) != -1)
m 'li.DropdownItem'
,
oncreate: u.scrollToView
key: i
className:
(if @currentIndex == i then 'Current ' else '') +
(if @ifAvailable(item, i) then 'Available' else '')
'data-index': i
'data-content': item
, item
autoComplete: (e) =>
@filter = (u.getTarget e).value
if @filter == '' then @currentIndex = undefined
onSelectInternal: (e) =>
if u.targetHasClass (u.getTarget e), 'Available'
index = parseInt u.getTargetData(e, 'index')
content = u.getTargetData e, 'content'
unless isNaN index
@currentIndex = index
@filter = ''
@autoHideDropDown.hide()
@onSelect(content, index)
u.cancelBubble e
view: ->
m 'div',
onclick: @autoHideDropDown.show
className: if @autoHideDropDown.showWidget then "Dropdown Expanded" else "Dropdown"
,
m 'input.DropdownInput',
disabled: if @allowEmptySelect then '' else 'true'
onkeyup: @autoComplete
placeholder: @placeholder
value:
if @filter
@filter
else if @currentIndex?
@itemArray[@currentIndex]
else ''
if @showDownArrow = true
m '.DownArrow', downIcon
@autoHideDropDown.view()
Dropdown.mss =
Dropdown:
position: 'relative'
width: '200px'
DropdownInput:
display: 'block'
lineHeight: '2em'
fontSize: '0.9em'
width: '100%'
padding: 0
textAlign: 'center'
border: '1px solid ' + style.border[4]
WebkitAppearance: 'none'
borderRadius: 0
'DropdownInput[disabled]':
cursor: 'pointer'
DownArrow:
svg: fill: '#999'
position: 'absolute'
top: '0.1em'
right: '0.3em'
transition: 'transform .3s ease'
DropdownList:
position: 'absolute'
top: '1.9em'
border: '1px solid #ccc'
width: '198px'
height: '200px'
margin: 0
padding: 0
listStyle: 'none'
background: '#fff'
overflowY: 'auto'
zIndex: 999
DropdownItem: s.LineSize('2em', '0.9em')
textAlign: 'center'
overflowX: 'hidden'
padding: '0 4px'
margin: 0
color: style.text[5]
$hover:
cursor: 'pointer'
background: style.main[5]
color: style.text[8]
Available:
color: style.text[0]
Current:
background: style.main[4]
color: style.text[8]
Expanded:
DownArrow:
transform: 'rotate(180deg)'
module.exports = Dropdown