-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
149 lines (121 loc) · 3.69 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#*--------------------------------------------------------#
# Select Menu -> Dropdown
#*--------------------------------------------------------#
classie = require 'classie'
class Dropdown
constructor: (el) ->
@el = el
transformSelect: ->
optionsHTML = ''
label = ''
value = -1
[].forEach.call @el.children, (el) ->
val = el.getAttribute 'value'
classes = el.getAttribute 'class'
selected = el.getAttribute 'selected'
label = el.textContent
if val isnt -1
optionsHTML +=
if classes?
'<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>'
else
'<li data-value="' + val + '"><span>' + label + '</span></li>'
if selected?
selectLabel = label
value = val
@el.insertAdjacentHTML('afterend', '<div class="cd-dropdown"></div>')
@drop = @el.parentNode.querySelector('.cd-dropdown')
ul = document.createElement('ul')
@drop.appendChild(ul)
@listofOptions = @drop.querySelector('ul')
@listofOptions.innerHTML = optionsHTML
@listofOptions.insertAdjacentHTML('beforebegin', '<span></span>')
@selectLabel = @listofOptions.parentNode.querySelector('span')
@selectLabel.textContent = if selectLabel? then selectLabel else @listofOptions.firstChild.firstChild.textContent
@el.parentNode.removeChild(@el)
return value
layout: ->
_this = @
@zIndex = 1000
value = @transformSelect()
@options = @listofOptions.children
@optionsCount = @options.length
@size =
w: @drop.offsetWidth
h: @drop.offsetHeight
elName = @el.getAttribute 'name'
elId = @el.getAttribute 'id'
inputName = if el? then elName else (if elId? then elId else 'cd-dropdown-' + (new Date()).getTime() )
@listofOptions.insertAdjacentHTML('beforebegin', '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>')
@inputEl = @drop.querySelector 'input[name="' + inputName + '"]'
@selectLabel.style.zIndex = @zIndex + @optionsCount
@positioning()
# if Modernizr.csstransitions
setTimeout ->
[].forEach.call _this.options, (el) ->
el.style.transition = 'all 300ms ease-in'
return
, 25
return
positioning: (anim) ->
_this = @
@listofOptions.style.height = 'auto'
[].forEach.call @options, (el, i) ->
el.style.zIndex = _this.zIndex + _this.optionsCount - 1 - i
el.style.top = 0
el.style.let = 0
el.style.marginLeft = 0
el.style.opacity = 1
el.style.transform = 'none'
onOptionSelect: (opt) ->
return false
initEvents: ->
_this = @
@selectLabel.addEventListener 'mousedown', (event) ->
if _this.opened then _this.close() else _this.open()
return false
[].forEach.call @options, (el) ->
el.addEventListener 'click', ->
if _this.opened
option = @
console.log option
_this.inputEl.setAttribute('value', option.getAttribute('data-value') )
_this.selectLabel.innerHTML = option.innerHTML
_this.close()
return
return
open: ->
_this = @
classie.toggle(@drop, 'cd-active')
@listofOptions.style.height = ( @optionsCount + 1 ) * @size.w + 'px'
[].forEach.call @options, (el, i) ->
el.style.opacity = 1
el.style.top = ( i + 1 ) * ( _this.size.h ) + 'px'
el.style.left = 0
el.style.width = _this.size.w
el.style.marginLeft = 0
el.style.transform = 'none'
el.style.transitionDelay = 0
@opened = true
return
close: ->
_this = @
classie.toggle(@drop, 'cd-active')
@positioning( true )
@opened = false
return
init: ->
@layout()
@initEvents()
return
includePaths = ->
dropdownPaths = path.join(__dirname, 'styl');
dropdownPaths
module.exports = {
Dropdown
includePaths: includePaths()
with: ->
paths = Array.prototype.slice.call(arguments)
result = [].concat.apply(includePaths(), paths)
result
}