better_select 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/{better_select_rails.gemspec → better_select.gemspec} +4 -2
- data/lib/better_select/version.rb +1 -1
- data/lib/better_select.rb +1 -34
- data/vendor/LICENSE +22 -0
- data/vendor/Makefile +8 -0
- data/vendor/README.md +29 -0
- data/vendor/assets/LICENSE +22 -0
- data/vendor/assets/Makefile +8 -0
- data/vendor/assets/README.md +29 -0
- data/vendor/assets/better_select.gemspec +17 -0
- data/vendor/assets/build/better-select.css +16 -0
- data/vendor/assets/build/better-select.js +330 -0
- data/vendor/assets/javascripts/better-select.js.coffee +185 -0
- data/vendor/assets/lib/better-select.coffee +183 -0
- data/vendor/assets/lib/better-select.css.scss +32 -0
- data/vendor/assets/stylesheets/better-select.css.scss +32 -0
- data/vendor/build/better-select.css +16 -0
- data/vendor/build/better-select.js +330 -0
- data/vendor/lib/better-select.coffee +183 -0
- data/vendor/lib/better-select.css.scss +32 -0
- metadata +37 -4
@@ -0,0 +1,330 @@
|
|
1
|
+
// Generated by CoffeeScript 1.3.3
|
2
|
+
(function() {
|
3
|
+
var $$, BetterSelect, addClass, build_element, getClasses, getLeft, getPos, getTop, letters, numbers, removeClass, renderOption, renderOptionGroup, setClasses;
|
4
|
+
|
5
|
+
$$ = function(html) {
|
6
|
+
var elm;
|
7
|
+
elm = document.createElement('div');
|
8
|
+
elm.innerHTML = html;
|
9
|
+
if (elm.children.length > 1) {
|
10
|
+
return elm.children;
|
11
|
+
} else {
|
12
|
+
return elm.children[0];
|
13
|
+
}
|
14
|
+
};
|
15
|
+
|
16
|
+
getPos = function(tgt, method) {
|
17
|
+
var pos;
|
18
|
+
pos = 0;
|
19
|
+
while (tgt) {
|
20
|
+
pos += tgt[method];
|
21
|
+
tgt = tgt.offsetParent;
|
22
|
+
}
|
23
|
+
return pos;
|
24
|
+
};
|
25
|
+
|
26
|
+
getTop = function(tgt) {
|
27
|
+
return getPos(tgt, 'offsetTop');
|
28
|
+
};
|
29
|
+
|
30
|
+
getLeft = function(tgt) {
|
31
|
+
return getPos(tgt, 'offsetLeft');
|
32
|
+
};
|
33
|
+
|
34
|
+
getClasses = function(tgt) {
|
35
|
+
return tgt.getAttribute('class').split(' ');
|
36
|
+
};
|
37
|
+
|
38
|
+
setClasses = function(tgt, classes) {
|
39
|
+
tgt.setAttribute('class', classes.join(' '));
|
40
|
+
return tgt;
|
41
|
+
};
|
42
|
+
|
43
|
+
addClass = function(tgt, className) {
|
44
|
+
var classes;
|
45
|
+
classes = getClasses(tgt);
|
46
|
+
if (classes.indexOf(className) === -1) {
|
47
|
+
classes.push(className);
|
48
|
+
}
|
49
|
+
return setClasses(tgt, classes);
|
50
|
+
};
|
51
|
+
|
52
|
+
removeClass = function(tgt, className) {
|
53
|
+
var classes, index;
|
54
|
+
if (!(index = (classes = getClasses(tgt)).indexOf(className === -1))) {
|
55
|
+
classes.splice(index, 1);
|
56
|
+
}
|
57
|
+
return setClasses(tgt, classes);
|
58
|
+
};
|
59
|
+
|
60
|
+
letters = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.split(' ');
|
61
|
+
|
62
|
+
numbers = '0 1 2 3 4 5 6 7 8 9 0'.split(' ');
|
63
|
+
|
64
|
+
build_element = function(what, orig, obj) {
|
65
|
+
var elm;
|
66
|
+
elm = $$(_.template(obj["" + what + "_template"])(obj["process_" + what](orig)));
|
67
|
+
elm.orig = orig;
|
68
|
+
orig.better_version = elm;
|
69
|
+
elm.better_select = obj;
|
70
|
+
return elm;
|
71
|
+
};
|
72
|
+
|
73
|
+
renderOption = function(orig_option, bs) {
|
74
|
+
var first_char, option;
|
75
|
+
option = build_element('option', orig_option, bs);
|
76
|
+
option.reset = function() {
|
77
|
+
this.orig.selected = void 0;
|
78
|
+
this.setAttribute('class', 'option');
|
79
|
+
return bs.reset(this);
|
80
|
+
};
|
81
|
+
option.select = function() {
|
82
|
+
this.orig.selected = 'selected';
|
83
|
+
this.setAttribute('class', 'option selected');
|
84
|
+
if (bs.select.getAttribute('class').indexOf('open') !== -1) {
|
85
|
+
bs.toggle();
|
86
|
+
}
|
87
|
+
return bs.set_selected(option);
|
88
|
+
};
|
89
|
+
option.addEventListener('click', function() {
|
90
|
+
return option.select();
|
91
|
+
});
|
92
|
+
option.addEventListener('mouseover', function() {
|
93
|
+
return option.better_select.set_focused(option);
|
94
|
+
});
|
95
|
+
bs.options.push(option);
|
96
|
+
first_char = option.innerHTML.substr(0, 1).toLowerCase();
|
97
|
+
if (!bs.options_by_first_char[first_char]) {
|
98
|
+
bs.options_by_first_char[first_char] = [];
|
99
|
+
}
|
100
|
+
bs.options_by_first_char[first_char].push(option);
|
101
|
+
bs.options_by_first_char[first_char].sort();
|
102
|
+
return option;
|
103
|
+
};
|
104
|
+
|
105
|
+
renderOptionGroup = function(orig_group, bs) {
|
106
|
+
var child, group, _i, _len, _ref;
|
107
|
+
group = build_element('option_group', orig_group, bs);
|
108
|
+
_ref = orig_group.children;
|
109
|
+
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
110
|
+
child = _ref[_i];
|
111
|
+
group.appendChild(renderOption(child, bs));
|
112
|
+
}
|
113
|
+
bs.option_groups.push(group);
|
114
|
+
return group;
|
115
|
+
};
|
116
|
+
|
117
|
+
BetterSelect = (function() {
|
118
|
+
|
119
|
+
BetterSelect.prototype.defaults = {
|
120
|
+
positionDropdown: true
|
121
|
+
};
|
122
|
+
|
123
|
+
function BetterSelect(elm, options) {
|
124
|
+
var child, children, last_char, method, selected, _i, _len, _ref,
|
125
|
+
_this = this;
|
126
|
+
if (!(elm && elm.tagName && elm.tagName === 'SELECT')) {
|
127
|
+
return;
|
128
|
+
}
|
129
|
+
this.settings = _.extend({}, this.defaults, options);
|
130
|
+
this.options = [];
|
131
|
+
this.options_by_first_char = {};
|
132
|
+
this.option_groups = [];
|
133
|
+
selected = elm.selectedOptions;
|
134
|
+
this.select = build_element('select', elm, this);
|
135
|
+
_ref = this.select.children, this.selected_option = _ref[0], this.dropdown = _ref[1];
|
136
|
+
if (elm.id) {
|
137
|
+
this.select.id = "" + elm.id + "-better-select";
|
138
|
+
this.dropdown.id = "" + elm.id + "-better-select-dropdown";
|
139
|
+
}
|
140
|
+
this.default_selected = [elm.children[elm.selectedIndex]];
|
141
|
+
elm.parentNode.insertBefore(this.select, elm);
|
142
|
+
elm.parentNode.insertBefore(elm, this.select);
|
143
|
+
elm.style.display = 'none';
|
144
|
+
children = elm.children;
|
145
|
+
if (this.settings.positionDropdown) {
|
146
|
+
document.body.appendChild(this.dropdown);
|
147
|
+
this.dropdown.style.left = '-9999px';
|
148
|
+
}
|
149
|
+
for (_i = 0, _len = children.length; _i < _len; _i++) {
|
150
|
+
child = children[_i];
|
151
|
+
switch (child.tagName) {
|
152
|
+
case 'OPTION':
|
153
|
+
method = renderOption;
|
154
|
+
break;
|
155
|
+
case 'OPTGROUP':
|
156
|
+
method = renderOptionGroup;
|
157
|
+
}
|
158
|
+
this.dropdown.appendChild(method(child, this));
|
159
|
+
}
|
160
|
+
if (this.default_selected) {
|
161
|
+
this.default_selected[0].better_version.select();
|
162
|
+
}
|
163
|
+
this.selected_option.addEventListener('click', function() {
|
164
|
+
_this.toggle();
|
165
|
+
return _this.set_selected(_this.dropdown_selected_option);
|
166
|
+
});
|
167
|
+
window.addEventListener('click', function(e) {
|
168
|
+
if (!(e.target === _this.selected_option || e.target === _this.select || _this.options.indexOf(e.target) !== -1)) {
|
169
|
+
if (_this.open) {
|
170
|
+
return _this.toggle();
|
171
|
+
}
|
172
|
+
}
|
173
|
+
});
|
174
|
+
last_char = false;
|
175
|
+
this.selected_option.addEventListener('focus', function() {
|
176
|
+
document.body.style.overflow = 'hidden';
|
177
|
+
return addClass(_this.select, 'focus');
|
178
|
+
});
|
179
|
+
this.selected_option.addEventListener('blur', function() {
|
180
|
+
removeClass(_this.select, 'focus');
|
181
|
+
document.body.style.overflow = 'auto';
|
182
|
+
if (_this.open === true) {
|
183
|
+
_this.toggle();
|
184
|
+
}
|
185
|
+
return true;
|
186
|
+
});
|
187
|
+
this.selected_option.addEventListener('keydown', function(e) {
|
188
|
+
if ([38, 40].indexOf(e.keyCode) !== -1) {
|
189
|
+
return e.preventDefault();
|
190
|
+
}
|
191
|
+
});
|
192
|
+
this.selected_option.addEventListener('keyup', function(e) {
|
193
|
+
var char, keyCode, last_character, option;
|
194
|
+
if (_this.open === false) {
|
195
|
+
_this.toggle();
|
196
|
+
}
|
197
|
+
keyCode = e.keyCode;
|
198
|
+
switch (keyCode) {
|
199
|
+
case 38:
|
200
|
+
_this.set_focused(_this.options[(_this.focus_index -= 1) < 0 ? _this.focus_index = _this.options.length - 1 : _this.focus_index]);
|
201
|
+
break;
|
202
|
+
case 40:
|
203
|
+
_this.set_focused(_this.options[(_this.focus_index += 1) >= _this.options.length ? _this.focus_index = 0 : _this.focus_index]);
|
204
|
+
break;
|
205
|
+
case 13:
|
206
|
+
_this.focused_option.select();
|
207
|
+
break;
|
208
|
+
default:
|
209
|
+
if (keyCode > 47 && keyCode < 58) {
|
210
|
+
char = numbers[keyCode - 47];
|
211
|
+
} else if (keyCode > 64 && keyCode < 91) {
|
212
|
+
char = letters[keyCode - 65];
|
213
|
+
} else {
|
214
|
+
if (_this.focused_option) {
|
215
|
+
removeClass(_this.focused_option, 'focus');
|
216
|
+
_this.focused_option = false;
|
217
|
+
_this.focus_index = -1;
|
218
|
+
}
|
219
|
+
_this.toggle();
|
220
|
+
}
|
221
|
+
if (char && _this.options_by_first_char[char]) {
|
222
|
+
if (last_char !== char) {
|
223
|
+
_this.options_by_first_char[char].sort();
|
224
|
+
}
|
225
|
+
option = _this.options_by_first_char[char].shift();
|
226
|
+
_this.options_by_first_char[char].push(option);
|
227
|
+
_this.set_focused(option);
|
228
|
+
_this.focus_index = _this.options.indexOf(option);
|
229
|
+
last_character = char;
|
230
|
+
}
|
231
|
+
}
|
232
|
+
_this.dropdown.addEventListener('click', function() {
|
233
|
+
return console.log(arguments);
|
234
|
+
});
|
235
|
+
e.preventDefault();
|
236
|
+
e.stopPropagation();
|
237
|
+
e.returnValue = false;
|
238
|
+
return false;
|
239
|
+
});
|
240
|
+
}
|
241
|
+
|
242
|
+
BetterSelect.prototype.focused_option = false;
|
243
|
+
|
244
|
+
BetterSelect.prototype.focus_index = -1;
|
245
|
+
|
246
|
+
BetterSelect.prototype.set_focused = function(option) {
|
247
|
+
var class_for_selected,
|
248
|
+
_this = this;
|
249
|
+
class_for_selected = function(option) {
|
250
|
+
if (_this.selected_option && option.innerHTML === _this.selected_option.innerHTML) {
|
251
|
+
return " selected";
|
252
|
+
} else {
|
253
|
+
return "";
|
254
|
+
}
|
255
|
+
};
|
256
|
+
if (this.focused_option) {
|
257
|
+
this.focused_option.setAttribute('class', "option" + (class_for_selected(this.focused_option)));
|
258
|
+
}
|
259
|
+
this.focused_option = option;
|
260
|
+
this.focused_option.setAttribute("class", "option focus" + (class_for_selected(option)));
|
261
|
+
return this.focus_index = this.options.indexOf(this.focused_option);
|
262
|
+
};
|
263
|
+
|
264
|
+
BetterSelect.prototype.open = false;
|
265
|
+
|
266
|
+
BetterSelect.prototype.toggle = function() {
|
267
|
+
var height, top;
|
268
|
+
((this.open = !this.open) ? addClass : removeClass)(this.select, 'open');
|
269
|
+
if (this.settings.positionDropdown) {
|
270
|
+
if (this.dropdown.offsetHeight > window.innerHeight) {
|
271
|
+
height = window.innerHeight * .50;
|
272
|
+
this.dropdown.style.height = height + 'px';
|
273
|
+
this.dropdown.style['overflow-y'] = 'auto';
|
274
|
+
if (this.dropdown_selected_option.offsetTop > height || this.adjust_height) {
|
275
|
+
this.dropdown_selected_option.scrollIntoView();
|
276
|
+
this.adjust_height = true;
|
277
|
+
}
|
278
|
+
}
|
279
|
+
top = top || (getTop(this.select) - this.dropdown_selected_option.offsetTop);
|
280
|
+
top = getTop(this.select) - (this.dropdown_selected_option.offsetTop - this.dropdown.scrollTop);
|
281
|
+
this.dropdown.style.top = (top < 0 ? 0 : top) + 'px';
|
282
|
+
return this.dropdown.style.left = this.open ? getLeft(this.select) + 'px' : '-9999px';
|
283
|
+
}
|
284
|
+
};
|
285
|
+
|
286
|
+
BetterSelect.prototype.reset = function(option) {
|
287
|
+
if (this.default_selected) {
|
288
|
+
return this.default_selected[0].better_version.select();
|
289
|
+
}
|
290
|
+
};
|
291
|
+
|
292
|
+
BetterSelect.prototype.set_selected = function(option) {
|
293
|
+
var e;
|
294
|
+
if (this.selected_option.innerHTML !== option.innerHTML) {
|
295
|
+
if (this.dropdown_selected_option) {
|
296
|
+
removeClass(this.dropdown_selected_option, 'selected');
|
297
|
+
}
|
298
|
+
addClass(this.dropdown_selected_option = option, 'selected');
|
299
|
+
this.selected_option.innerHTML = option.innerHTML;
|
300
|
+
e = document.createEvent('Event');
|
301
|
+
e.initEvent('change', true, true);
|
302
|
+
return this.select.orig.dispatchEvent(e);
|
303
|
+
}
|
304
|
+
};
|
305
|
+
|
306
|
+
BetterSelect.prototype.option_template = '<div class="option"><%= innerHTML %></div>';
|
307
|
+
|
308
|
+
BetterSelect.prototype.option_group_template = '<div class="optgroup"><div class="option-group-label"><%= label %></div></div>';
|
309
|
+
|
310
|
+
BetterSelect.prototype.select_template = '<div class="select"><a href="javascript:void(0)" class="selected-option"></a><div class="better-select-dropdown dropdown"></div></div>';
|
311
|
+
|
312
|
+
BetterSelect.prototype.process_option = function(option) {
|
313
|
+
return option;
|
314
|
+
};
|
315
|
+
|
316
|
+
BetterSelect.prototype.process_option_group = function(option_group) {
|
317
|
+
return option_group;
|
318
|
+
};
|
319
|
+
|
320
|
+
BetterSelect.prototype.process_select = function(select) {
|
321
|
+
return select;
|
322
|
+
};
|
323
|
+
|
324
|
+
return BetterSelect;
|
325
|
+
|
326
|
+
})();
|
327
|
+
|
328
|
+
window.BetterSelect = BetterSelect;
|
329
|
+
|
330
|
+
}).call(this);
|
@@ -0,0 +1,183 @@
|
|
1
|
+
$$ = (html) ->
|
2
|
+
elm = document.createElement 'div'
|
3
|
+
elm.innerHTML = html
|
4
|
+
if elm.children.length > 1 then elm.children else elm.children[0]
|
5
|
+
|
6
|
+
getPos = (tgt, method) ->
|
7
|
+
pos = 0
|
8
|
+
while tgt
|
9
|
+
pos += tgt[method]
|
10
|
+
tgt = tgt.offsetParent
|
11
|
+
pos
|
12
|
+
|
13
|
+
getTop = (tgt) -> getPos tgt, 'offsetTop'
|
14
|
+
getLeft = (tgt) -> getPos tgt, 'offsetLeft'
|
15
|
+
|
16
|
+
getClasses = (tgt) -> tgt.getAttribute('class').split ' '
|
17
|
+
|
18
|
+
setClasses = (tgt, classes) ->
|
19
|
+
tgt.setAttribute 'class', classes.join ' '
|
20
|
+
tgt
|
21
|
+
|
22
|
+
addClass = (tgt, className) ->
|
23
|
+
classes = getClasses tgt
|
24
|
+
classes.push(className) if classes.indexOf(className) is -1
|
25
|
+
setClasses tgt, classes
|
26
|
+
|
27
|
+
removeClass = (tgt, className) ->
|
28
|
+
classes.splice(index, 1) unless index = (classes = getClasses tgt).indexOf className is -1
|
29
|
+
setClasses tgt, classes
|
30
|
+
|
31
|
+
letters = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.split ' '
|
32
|
+
numbers = '0 1 2 3 4 5 6 7 8 9 0'.split ' '
|
33
|
+
|
34
|
+
build_element = (what, orig, obj) ->
|
35
|
+
((elm = $$ _.template(obj["#{what}_template"]) obj["process_#{what}"] orig).orig = orig).better_version = elm
|
36
|
+
elm.better_select = obj
|
37
|
+
elm
|
38
|
+
|
39
|
+
renderOption = (orig_option, bs) ->
|
40
|
+
option = build_element 'option', orig_option, bs
|
41
|
+
option.reset = ->
|
42
|
+
@orig.selected = undefined
|
43
|
+
@setAttribute 'class', 'option'
|
44
|
+
bs.reset @
|
45
|
+
option.select = ->
|
46
|
+
@orig.selected = 'selected'
|
47
|
+
@setAttribute 'class', 'option selected'
|
48
|
+
bs.toggle() if bs.select.getAttribute('class').indexOf('open') isnt -1
|
49
|
+
bs.set_selected option
|
50
|
+
option.addEventListener 'click', -> option.select()
|
51
|
+
option.addEventListener 'mouseover', -> option.better_select.set_focused option
|
52
|
+
bs.options.push option
|
53
|
+
first_char = option.innerHTML.substr(0, 1).toLowerCase()
|
54
|
+
bs.options_by_first_char[first_char] = [] unless bs.options_by_first_char[first_char]
|
55
|
+
bs.options_by_first_char[first_char].push option
|
56
|
+
bs.options_by_first_char[first_char].sort()
|
57
|
+
option
|
58
|
+
|
59
|
+
renderOptionGroup = (orig_group, bs) ->
|
60
|
+
group = build_element 'option_group', orig_group, bs
|
61
|
+
group.appendChild(renderOption child, bs) for child in orig_group.children
|
62
|
+
bs.option_groups.push group
|
63
|
+
group
|
64
|
+
|
65
|
+
class BetterSelect
|
66
|
+
defaults:
|
67
|
+
positionDropdown: true
|
68
|
+
constructor: (elm, options) ->
|
69
|
+
return unless elm && elm.tagName && elm.tagName is 'SELECT'
|
70
|
+
@settings = _.extend {}, @defaults, options
|
71
|
+
@options = []
|
72
|
+
@options_by_first_char = {}
|
73
|
+
@option_groups = []
|
74
|
+
selected = elm.selectedOptions
|
75
|
+
@select = build_element 'select', elm, @
|
76
|
+
[@selected_option, @dropdown] = @select.children
|
77
|
+
if elm.id
|
78
|
+
@select.id = "#{elm.id}-better-select"
|
79
|
+
@dropdown.id = "#{elm.id}-better-select-dropdown"
|
80
|
+
@default_selected = [elm.children[elm.selectedIndex]]
|
81
|
+
elm.parentNode.insertBefore @select, elm
|
82
|
+
elm.parentNode.insertBefore elm, @select
|
83
|
+
elm.style.display = 'none'
|
84
|
+
children = elm.children
|
85
|
+
if @settings.positionDropdown
|
86
|
+
document.body.appendChild @dropdown
|
87
|
+
@dropdown.style.left = '-9999px'
|
88
|
+
for child in children
|
89
|
+
switch child.tagName
|
90
|
+
when 'OPTION'
|
91
|
+
method = renderOption
|
92
|
+
when 'OPTGROUP'
|
93
|
+
method = renderOptionGroup
|
94
|
+
@dropdown.appendChild(method child, @)
|
95
|
+
@default_selected[0].better_version.select() if @default_selected
|
96
|
+
@selected_option.addEventListener 'click', =>
|
97
|
+
@toggle()
|
98
|
+
@set_selected @dropdown_selected_option
|
99
|
+
window.addEventListener 'click', (e) =>
|
100
|
+
unless e.target == @selected_option || e.target == @select || @options.indexOf(e.target) isnt -1
|
101
|
+
@toggle() if @open
|
102
|
+
last_char = false
|
103
|
+
@selected_option.addEventListener 'focus', =>
|
104
|
+
document.body.style.overflow = 'hidden'
|
105
|
+
addClass @select, 'focus'
|
106
|
+
@selected_option.addEventListener 'blur', =>
|
107
|
+
removeClass @select, 'focus'
|
108
|
+
document.body.style.overflow = 'auto'
|
109
|
+
@toggle() if @open is true
|
110
|
+
true
|
111
|
+
@selected_option.addEventListener 'keydown', (e) => e.preventDefault() unless [38, 40].indexOf(e.keyCode) is -1
|
112
|
+
@selected_option.addEventListener 'keyup', (e) =>
|
113
|
+
@toggle() if @open is false
|
114
|
+
keyCode = e.keyCode
|
115
|
+
switch keyCode
|
116
|
+
when 38 then @set_focused @options[if (@focus_index -= 1) < 0 then @focus_index = @options.length - 1 else @focus_index]
|
117
|
+
when 40 then @set_focused @options[if (@focus_index += 1) >= @options.length then @focus_index = 0 else @focus_index]
|
118
|
+
when 13
|
119
|
+
@focused_option.select()
|
120
|
+
else
|
121
|
+
if keyCode > 47 && keyCode < 58
|
122
|
+
char = numbers[keyCode - 47]
|
123
|
+
else if keyCode > 64 && keyCode < 91
|
124
|
+
char = letters[keyCode - 65]
|
125
|
+
else
|
126
|
+
if @focused_option
|
127
|
+
removeClass @focused_option, 'focus'
|
128
|
+
@focused_option = false
|
129
|
+
@focus_index = -1
|
130
|
+
@toggle()
|
131
|
+
if char && @options_by_first_char[char]
|
132
|
+
@options_by_first_char[char].sort() unless last_char is char
|
133
|
+
option = @options_by_first_char[char].shift()
|
134
|
+
@options_by_first_char[char].push option
|
135
|
+
@set_focused option
|
136
|
+
@focus_index = @options.indexOf option
|
137
|
+
last_character = char
|
138
|
+
@dropdown.addEventListener('click', -> console.log arguments)
|
139
|
+
e.preventDefault()
|
140
|
+
e.stopPropagation()
|
141
|
+
e.returnValue = false
|
142
|
+
false
|
143
|
+
focused_option: false
|
144
|
+
focus_index: -1
|
145
|
+
set_focused: (option) ->
|
146
|
+
class_for_selected = (option) => if @selected_option && option.innerHTML is @selected_option.innerHTML then " selected" else ""
|
147
|
+
@focused_option.setAttribute('class', "option#{class_for_selected(@focused_option)}") if @focused_option
|
148
|
+
@focused_option = option
|
149
|
+
@focused_option.setAttribute("class", "option focus#{class_for_selected(option)}")
|
150
|
+
@focus_index = @options.indexOf @focused_option
|
151
|
+
open: false
|
152
|
+
toggle: ->
|
153
|
+
(if @open = !@open then addClass else removeClass)(@select, 'open')
|
154
|
+
if @settings.positionDropdown
|
155
|
+
if @dropdown.offsetHeight > window.innerHeight
|
156
|
+
height = window.innerHeight * .50
|
157
|
+
@dropdown.style.height = height + 'px'
|
158
|
+
@dropdown.style['overflow-y'] = 'auto'
|
159
|
+
if @dropdown_selected_option.offsetTop > height || @adjust_height
|
160
|
+
@dropdown_selected_option.scrollIntoView()
|
161
|
+
@adjust_height = true
|
162
|
+
top = top || (getTop(@select) - @dropdown_selected_option.offsetTop)
|
163
|
+
top = getTop(@select) - (@dropdown_selected_option.offsetTop - @dropdown.scrollTop)
|
164
|
+
@dropdown.style.top = (if top < 0 then 0 else top) + 'px'
|
165
|
+
@dropdown.style.left = if @open then getLeft(@select) + 'px' else '-9999px'
|
166
|
+
reset: (option) -> @default_selected[0].better_version.select() if @default_selected
|
167
|
+
set_selected: (option) ->
|
168
|
+
unless @selected_option.innerHTML == option.innerHTML
|
169
|
+
removeClass(@dropdown_selected_option, 'selected') if @dropdown_selected_option
|
170
|
+
addClass @dropdown_selected_option = option, 'selected'
|
171
|
+
@selected_option.innerHTML = option.innerHTML
|
172
|
+
e = document.createEvent('Event')
|
173
|
+
e.initEvent 'change', true, true
|
174
|
+
@select.orig.dispatchEvent e
|
175
|
+
option_template: '<div class="option"><%= innerHTML %></div>'
|
176
|
+
option_group_template: '<div class="optgroup"><div class="option-group-label"><%= label %></div></div>'
|
177
|
+
select_template: '<div class="select"><a href="javascript:void(0)" class="selected-option"></a><div class="better-select-dropdown dropdown"></div></div>'
|
178
|
+
process_option: (option) -> option
|
179
|
+
process_option_group: (option_group) -> option_group
|
180
|
+
process_select: (select) -> select
|
181
|
+
|
182
|
+
|
183
|
+
window.BetterSelect = BetterSelect
|
@@ -0,0 +1,32 @@
|
|
1
|
+
.better-select-dropdown {
|
2
|
+
position: fixed;
|
3
|
+
|
4
|
+
.option {
|
5
|
+
cursor: pointer;
|
6
|
+
}
|
7
|
+
}
|
8
|
+
|
9
|
+
.select {
|
10
|
+
position: static;
|
11
|
+
|
12
|
+
&.dont-position-dropdown {
|
13
|
+
position: relative;
|
14
|
+
|
15
|
+
.dropdown {
|
16
|
+
top: 100%;
|
17
|
+
left: -99999px;
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
&.open {
|
22
|
+
&.dont-position-dropdown {
|
23
|
+
.dropdown {
|
24
|
+
left: 0px;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
.selected-option {
|
30
|
+
cursor: pointer;
|
31
|
+
}
|
32
|
+
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_select
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,23 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-12-18 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
14
30
|
description: A better HTMLSelectElement replacement
|
15
31
|
email:
|
16
32
|
- bennyjbergstein@gmail.com
|
@@ -24,13 +40,30 @@ files:
|
|
24
40
|
- LICENSE
|
25
41
|
- README.md
|
26
42
|
- Rakefile
|
27
|
-
-
|
43
|
+
- better_select.gemspec
|
28
44
|
- lib/better_select.rb
|
29
45
|
- lib/better_select/engine.rb
|
30
46
|
- lib/better_select/version.rb
|
47
|
+
- vendor/assets/better_select.gemspec
|
48
|
+
- vendor/assets/build/better-select.css
|
49
|
+
- vendor/assets/build/better-select.js
|
50
|
+
- vendor/assets/javascripts/better-select.js.coffee
|
51
|
+
- vendor/assets/lib/better-select.coffee
|
52
|
+
- vendor/assets/lib/better-select.css.scss
|
53
|
+
- vendor/assets/LICENSE
|
54
|
+
- vendor/assets/Makefile
|
55
|
+
- vendor/assets/README.md
|
56
|
+
- vendor/assets/stylesheets/better-select.css.scss
|
57
|
+
- vendor/build/better-select.css
|
58
|
+
- vendor/build/better-select.js
|
31
59
|
- vendor/javascripts/better-select.js.coffee
|
60
|
+
- vendor/lib/better-select.coffee
|
61
|
+
- vendor/lib/better-select.css.scss
|
62
|
+
- vendor/LICENSE
|
63
|
+
- vendor/Makefile
|
64
|
+
- vendor/README.md
|
32
65
|
- vendor/stylesheets/better-select.css.scss
|
33
|
-
homepage: http://www.
|
66
|
+
homepage: http://www.benjaminberstein.com
|
34
67
|
licenses: []
|
35
68
|
post_install_message:
|
36
69
|
rdoc_options: []
|