-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (136 loc) · 2.82 KB
/
index.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*!
*
* Dropdown
*
* MIT
*
*/
/**
* Module dependencies.
*/
var o = require('jquery')
var inherit = require('inherit')
var viewport = require('viewport')
var Menu = require('menu')
/**
* Exports.
*/
module.exports = Dropdown
/**
* Dropdown class.
*
* @api public
*/
function Dropdown (ref) {
if (!(this instanceof Dropdown)) return new Dropdown(ref)
Menu.call(this, this.dropdown)
this.el.addClass('dropdown')
this.ref = o(ref)
this.bindRefEvents()
this.reposition()
}
/**
* Inherit from Menu.
*/
inherit(Dropdown, Menu)
/**
* Bind reference element event handlers.
*
* @api private
*/
Dropdown.prototype.bindRefEvents = function () {
var self = this
var noShow = false
function onfocusRef (ev) {
ev.preventDefault()
ev.stopPropagation()
if (!noShow) self.show()
noShow = false
}
function onclickRef (ev) {
onfocusRef(ev)
}
function onblurRef (ev) {
if (!self.isSelecting()) {
setTimeout(function () {
self.hide()
}, 0)
}
}
function onkeydownRef (ev) {
switch (ev.which) {
case 9: // tab
self.hide()
break
case 39: // right
ev.stopPropagation()
break
case 27: // esc
if (!self.isOpen()) this.blur()
break
default: // typing anything
self._isSelecting = true
self.show()
break
}
}
function onmouseupRef (ev) {
self.toggle()
}
function onkeydownEl (ev) {
if (37 === ev.which) {
noShow = true
}
self.ref[0].focus()
}
function onmouseupBody (ev) {
ev.preventDefault()
ev.stopPropagation()
}
this.ref.on('keydown', onkeydownRef)
this.ref.on('mouseup', onmouseupRef)
this.ref.on('focus', onfocusRef)
this.ref.on('click', onclickRef)
this.ref.on('blur', onblurRef)
this.on('show', function () {
this.reposition()
setTimeout(function () {
this.reposition()
}.bind(this), 0)
this.el.on('keydown', onkeydownEl)
})
this.on('hide', function () {
this.el.off('keydown', onkeydownEl)
})
this.unbindRefEvents = function () {
this.ref.off('focus', onfocusRef)
this.ref.off('click', onclickRef)
this.ref.off('blur', onblurRef)
this.ref.off('keydown', onkeydownRef)
this.ref.off('mouseup', onmouseupRef)
}.bind(this)
}
/**
* Reposition dropdown relative to reference
* element.
*
* @api private
*/
Dropdown.prototype.reposition = function () {
var offset = this.ref[0].getBoundingClientRect()
viewport.refresh()
var x = offset.left+viewport.left
var y = offset.top+offset.height+viewport.top
var eh = this.el.outerHeight(true)
// show from above if below visible
if (y + eh > viewport.bottom) {
y = offset.top - eh + viewport.top
}
var w = offset.width
this.moveTo(x, y)
this.el.css({
maxWidth: w
, minWidth: w
, width: w
})
}