forked from Dogfalo/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdown.js
274 lines (239 loc) · 8.84 KB
/
dropdown.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
(function ($) {
// Add posibility to scroll to selected option
// usefull for select for example
$.fn.scrollTo = function(elem) {
$(this).scrollTop($(this).scrollTop() - $(this).offset().top + $(elem).offset().top);
return this;
};
$.fn.dropdown = function (options) {
var defaults = {
inDuration: 300,
outDuration: 225,
constrainWidth: true, // Constrains width of dropdown to the activator
hover: false,
gutter: 0, // Spacing from edge
belowOrigin: false,
alignment: 'left',
stopPropagation: false
};
// Open dropdown.
if (options === "open") {
this.each(function() {
$(this).trigger('open');
});
return false;
}
// Close dropdown.
if (options === "close") {
this.each(function() {
$(this).trigger('close');
});
return false;
}
this.each(function(){
var origin = $(this);
var curr_options = $.extend({}, defaults, options);
var isFocused = false;
// Dropdown menu
var activates = $("#"+ origin.attr('data-activates'));
function updateOptions() {
if (origin.data('induration') !== undefined)
curr_options.inDuration = origin.data('induration');
if (origin.data('outduration') !== undefined)
curr_options.outDuration = origin.data('outduration');
if (origin.data('constrainwidth') !== undefined)
curr_options.constrainWidth = origin.data('constrainwidth');
if (origin.data('hover') !== undefined)
curr_options.hover = origin.data('hover');
if (origin.data('gutter') !== undefined)
curr_options.gutter = origin.data('gutter');
if (origin.data('beloworigin') !== undefined)
curr_options.belowOrigin = origin.data('beloworigin');
if (origin.data('alignment') !== undefined)
curr_options.alignment = origin.data('alignment');
if (origin.data('stoppropagation') !== undefined)
curr_options.stopPropagation = origin.data('stoppropagation');
}
updateOptions();
// Attach dropdown to its activator
origin.after(activates);
/*
Helper function to position and resize dropdown.
Used in hover and click handler.
*/
function placeDropdown(eventType) {
// Check for simultaneous focus and click events.
if (eventType === 'focus') {
isFocused = true;
}
// Check html data attributes
updateOptions();
// Set Dropdown state
activates.addClass('active');
origin.addClass('active');
var originWidth = origin[0].getBoundingClientRect().width;
// Constrain width
if (curr_options.constrainWidth === true) {
activates.css('width', originWidth);
} else {
activates.css('white-space', 'nowrap');
}
// Offscreen detection
var windowHeight = window.innerHeight;
var originHeight = origin.innerHeight();
var offsetLeft = origin.offset().left;
var offsetTop = origin.offset().top - $(window).scrollTop();
var currAlignment = curr_options.alignment;
var gutterSpacing = 0;
var leftPosition = 0;
// Below Origin
var verticalOffset = 0;
if (curr_options.belowOrigin === true) {
verticalOffset = originHeight;
}
// Check for scrolling positioned container.
var scrollYOffset = 0;
var scrollXOffset = 0;
var wrapper = origin.parent();
if (!wrapper.is('body')) {
if (wrapper[0].scrollHeight > wrapper[0].clientHeight) {
scrollYOffset = wrapper[0].scrollTop;
}
if (wrapper[0].scrollWidth > wrapper[0].clientWidth) {
scrollXOffset = wrapper[0].scrollLeft;
}
}
if (offsetLeft + activates.innerWidth() > $(window).width()) {
// Dropdown goes past screen on right, force right alignment
currAlignment = 'right';
} else if (offsetLeft - activates.innerWidth() + origin.innerWidth() < 0) {
// Dropdown goes past screen on left, force left alignment
currAlignment = 'left';
}
// Vertical bottom offscreen detection
if (offsetTop + activates.innerHeight() > windowHeight) {
// If going upwards still goes offscreen, just crop height of dropdown.
if (offsetTop + originHeight - activates.innerHeight() < 0) {
var adjustedHeight = windowHeight - offsetTop - verticalOffset;
activates.css('max-height', adjustedHeight);
} else {
// Flow upwards.
if (!verticalOffset) {
verticalOffset += originHeight;
}
verticalOffset -= activates.innerHeight();
}
}
// Handle edge alignment
if (currAlignment === 'left') {
gutterSpacing = curr_options.gutter;
leftPosition = origin.position().left + gutterSpacing;
}
else if (currAlignment === 'right') {
// Material icons fix
activates
.stop(true, true)
.css({
opacity: 0,
left: 0
})
var offsetRight = origin.position().left + originWidth - activates.width();
gutterSpacing = -curr_options.gutter;
leftPosition = offsetRight + gutterSpacing;
}
// Position dropdown
activates.css({
position: 'absolute',
top: origin.position().top + verticalOffset + scrollYOffset,
left: leftPosition + scrollXOffset
});
// Show dropdown
activates
.slideDown({
queue: false,
duration: curr_options.inDuration,
easing: 'easeOutCubic',
complete: function() {
$(this).css('height', '');
}
})
.animate( {opacity: 1}, {queue: false, duration: curr_options.inDuration, easing: 'easeOutSine'});
// Add click close handler to document
setTimeout(function() {
$(document).on('click.'+ activates.attr('id'), function (e) {
hideDropdown();
$(document).off('click.'+ activates.attr('id'));
});
}, 0);
}
function hideDropdown() {
// Check for simultaneous focus and click events.
isFocused = false;
activates.fadeOut(curr_options.outDuration);
activates.removeClass('active');
origin.removeClass('active');
$(document).off('click.'+ activates.attr('id'));
setTimeout(function() { activates.css('max-height', ''); }, curr_options.outDuration);
}
// Hover
if (curr_options.hover) {
var open = false;
origin.off('click.' + origin.attr('id'));
// Hover handler to show dropdown
origin.on('mouseenter', function(e){ // Mouse over
if (open === false) {
placeDropdown();
open = true;
}
});
origin.on('mouseleave', function(e){
// If hover on origin then to something other than dropdown content, then close
var toEl = e.toElement || e.relatedTarget; // added browser compatibility for target element
if(!$(toEl).closest('.dropdown-content').is(activates)) {
activates.stop(true, true);
hideDropdown();
open = false;
}
});
activates.on('mouseleave', function(e){ // Mouse out
var toEl = e.toElement || e.relatedTarget;
if(!$(toEl).closest('.dropdown-button').is(origin)) {
activates.stop(true, true);
hideDropdown();
open = false;
}
});
// Click
} else {
// Click handler to show dropdown
origin.off('click.' + origin.attr('id'));
origin.on('click.'+origin.attr('id'), function(e){
if (!isFocused) {
if ( origin[0] == e.currentTarget &&
!origin.hasClass('active') &&
($(e.target).closest('.dropdown-content').length === 0)) {
e.preventDefault(); // Prevents button click from moving window
if (curr_options.stopPropagation) {
e.stopPropagation();
}
placeDropdown('click');
}
// If origin is clicked and menu is open, close menu
else if (origin.hasClass('active')) {
hideDropdown();
$(document).off('click.'+ activates.attr('id'));
}
}
});
} // End else
// Listen to open and close event - useful for select component
origin.on('open', function(e, eventType) {
placeDropdown(eventType);
});
origin.on('close', hideDropdown);
});
}; // End dropdown plugin
$(document).ready(function(){
$('.dropdown-button').dropdown();
});
}( jQuery ));