-
Notifications
You must be signed in to change notification settings - Fork 10
/
jquery-dropdown-dot.js
173 lines (150 loc) · 6.01 KB
/
jquery-dropdown-dot.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
/**
* dropdown.dot.js
*
* A super-flexible and clean JQuery Dropdown Plugin based on dot.js Templates.
*
* @author Roger Dudler <[email protected]>
*/
(function ($) {
$.fn.dropdown = function(options) {
// Default settings
var settings = $.extend({
'selected': '.selected',
'items': 'ul',
'item': 'li',
'openClass': 'open',
'changed': function(data) {}
}, options);
return this.each(function() {
var $this = $(this);
// hide select element
$this.hide();
// prepare dropdown template
var template = doT.template($($this.data('template')).text());
var templateSelected = doT.template($($this.data('template') + '-selected').text());
var data = { "selected": null, "items": [], "classes": $this.data('classes') };
var selectedIndex = 0;
// parse data for dropdown items
$.each($this.find('option'), function(key, option) {
var $option = $(option);
var item = {
'value': $option.attr('value'),
'label': $option.text(),
'selected': $option.is(':selected')
};
// read data attributes
var dataAttributes = $option.data();
$.each(dataAttributes, function(name, value) {
item[name] = value;
});
if ($option.is(':selected')) {
data.selected = item;
selectedIndex = key;
}
data.items.push(item);
});
// render dropdown
var $dropdown = $(template(data));
var $items = $dropdown.find(settings.items);
var $selected = $dropdown.find('> ' + settings.selected);
// handle dropdown toggling
$selected.on('mousedown', function() {
$dropdown.focus();
if ($items.is(':visible')) {
_closeDropdown();
} else {
_openDropdown();
}
return false;
});
$dropdown.on('focus', function() {
$this.addClass('focus');
});
$dropdown.on('blur', function() {
$this.removeClass('focus');
_closeDropdown();
});
$dropdown.on('keydown', function(e) {
var next = null;
switch (e.keyCode) {
case 40: // down
if (!$dropdown.hasClass(settings.openClass)) {
_openDropdown();
next = $items.find('.selected');
} else {
next = $items.find('.hover').next(settings.item).first();
if (!next.length) {
next = $items.find(settings.item).first();
}
}
_selectItem(next, true);
e.preventDefault();
break;
case 38: // up
if (!$dropdown.hasClass(settings.openClass)) {
_openDropdown();
next = $items.find('.selected');
} else {
next = $items.find('.hover').prev(settings.item).first();
if (!next.length) {
next = $items.find(settings.item).last();
}
}
_selectItem(next, true);
e.preventDefault();
break;
case 13:
if (!$dropdown.hasClass(settings.openClass)) {
_openDropdown();
} else {
var item = $items.find('.hover').first();
if (item) {
_chooseItem(item);
}
}
e.preventDefault();
break;
}
});
// hide selected item from list
$items.find('[data-index=' + selectedIndex + ']').addClass('selected');
// handle clicks on items
$items.on('mousedown', settings.item, function() {
_chooseItem(this);
return false;
});
$items.on('mouseover', settings.item, function() {
_selectItem(this, false);
});
function _selectItem(option, reveal) {
var $option = $(option);
$items.find(settings.item).removeClass('hover');
$option.addClass('hover');
if (reveal) {
$option.parent().scrollTop($option.position().top-$option.height());
}
}
function _chooseItem(option) {
var index = $(option).data('index');
var item = data.items[index];
$dropdown.find('> ' + settings.selected).html(templateSelected(item));
$items.find('[data-index=' + selectedIndex + ']').removeClass('selected');
selectedIndex = index;
$(option).addClass('selected');
$this.val(item.value);
settings.changed(item);
_closeDropdown();
}
function _openDropdown() {
$items.show();
$dropdown.addClass(settings.openClass);
}
function _closeDropdown() {
$items.hide();
$dropdown.removeClass(settings.openClass);
}
// insert dropdown into dom tree
return $this.before($dropdown);
});
};
})(jQuery);