|
9 | 9 | * /,'
|
10 | 10 | * /'
|
11 | 11 | *
|
12 |
| - * Selectric Ϟ v1.8.1 (2014-09-01) - http://lcdsantos.github.io/jQuery-Selectric/ |
| 12 | + * Selectric Ϟ v1.8.2 (2014-09-22) - http://lcdsantos.github.io/jQuery-Selectric/ |
13 | 13 | *
|
14 | 14 | * Copyright (c) 2014 Leonardo Santos; Dual licensed: MIT/GPL
|
15 | 15 | *
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -;(function ($) { |
| 18 | +;(function($) { |
19 | 19 | 'use strict';
|
20 | 20 |
|
21 | 21 | var pluginName = 'selectric',
|
|
39 | 39 | },
|
40 | 40 | optionsItemBuilder: '{text}' // function(itemData, element, index)
|
41 | 41 | },
|
| 42 | + hooks = { |
| 43 | + add: function(callbackName, hookName, fn) { |
| 44 | + if ( !this[callbackName] ) |
| 45 | + this[callbackName] = {}; |
| 46 | + |
| 47 | + this[callbackName][hookName] = fn; |
| 48 | + }, |
| 49 | + remove: function(callbackName, hookName) { |
| 50 | + delete this[callbackName][hookName]; |
| 51 | + } |
| 52 | + }, |
42 | 53 | _utils = {
|
43 | 54 | // Replace diacritics
|
44 | 55 | replaceDiacritics: function(s) {
|
|
68 | 79 | while ( selectItems[ selected = (selected > 0 ? selected : selectItems.length) - 1 ].disabled ){}
|
69 | 80 | return selected;
|
70 | 81 | },
|
71 |
| - toDash: function(str){ |
| 82 | + toDash: function(str) { |
72 | 83 | return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
73 | 84 | },
|
74 |
| - triggerCallback: function(fn, scope){ |
| 85 | + triggerCallback: function(fn, scope) { |
75 | 86 | var elm = scope.element,
|
76 | 87 | func = scope.options['on' + fn];
|
77 | 88 |
|
78 | 89 | if ( $.isFunction(func) )
|
79 |
| - func.call(elm, elm); |
| 90 | + func.call(elm, elm, scope); |
80 | 91 |
|
81 |
| - $(elm).trigger(pluginName + '-' + _utils.toDash(fn)); |
| 92 | + if ( hooks[fn] ){ |
| 93 | + $.each(hooks[fn], function(){ |
| 94 | + this.call(elm, elm, scope); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + $(elm).trigger(pluginName + '-' + _utils.toDash(fn), scope); |
82 | 99 | }
|
83 | 100 | },
|
84 | 101 | $doc = $(document),
|
|
102 | 119 | function _init(opts) {
|
103 | 120 | _this.options = $.extend(true, {}, defaults, _this.options, opts);
|
104 | 121 | _this.classes = {};
|
105 |
| - _this.element = element, |
| 122 | + _this.element = element; |
106 | 123 |
|
107 | 124 | _utils.triggerCallback('BeforeInit', _this);
|
108 | 125 |
|
|
187 | 204 | $label.html(_this.items[currValue].text);
|
188 | 205 | }
|
189 | 206 |
|
190 |
| - $wrapper.add($original).off(bindSufix); |
| 207 | + $wrapper.add($original, $outerWrapper, $input).off(bindSufix); |
191 | 208 |
|
192 | 209 | $outerWrapper.prop('class', [_this.classes.wrapper, $original.prop('class').replace(/\S+/g, pluginName + '-$&'), _this.options.responsive ? _this.classes.responsive : ''].join(' '));
|
193 | 210 |
|
194 | 211 | if ( !$original.prop('disabled') ){
|
195 | 212 | // Not disabled, so... Removing disabled class and bind hover
|
196 |
| - $outerWrapper.removeClass(_this.classes.disabled).hover(function(){ |
| 213 | + $outerWrapper.removeClass(_this.classes.disabled).on('mouseenter' + bindSufix + ' mouseleave' + bindSufix, function(){ |
197 | 214 | $(this).toggleClass(_this.classes.hover);
|
198 | 215 | });
|
199 | 216 |
|
|
205 | 222 | isOpen ? _close() : _open(e);
|
206 | 223 | });
|
207 | 224 |
|
208 |
| - $input.prop({ |
209 |
| - tabindex: tabindex, |
210 |
| - disabled: false |
211 |
| - }).off().on({ |
212 |
| - keypress: _handleSystemKeys, |
213 |
| - keydown: function(e){ |
| 225 | + $input |
| 226 | + .prop({ |
| 227 | + tabindex: tabindex, |
| 228 | + disabled: false |
| 229 | + }) |
| 230 | + .on('keypress' + bindSufix, _handleSystemKeys) |
| 231 | + .on('keydown' + bindSufix, function(e){ |
214 | 232 | _handleSystemKeys(e);
|
215 | 233 |
|
216 | 234 | // Clear search
|
|
228 | 246 | // 40 => Down
|
229 | 247 | if ( key > 36 && key < 41 )
|
230 | 248 | _select(_utils[(key < 39 ? 'previous' : 'next') + 'EnabledItem'](_this.items, selected));
|
231 |
| - }, |
232 |
| - focusin: function(e){ |
| 249 | + }) |
| 250 | + .on('focusin' + bindSufix, function(e){ |
233 | 251 | // Stupid, but necessary... Prevent the flicker when
|
234 | 252 | // focusing out and back again in the browser window
|
235 | 253 | $input.one('blur', function(){
|
236 | 254 | $input.blur();
|
237 | 255 | });
|
238 | 256 |
|
239 | 257 | isOpen || _open(e);
|
240 |
| - } |
241 |
| - }).on('oninput' in $input[0] ? 'input' : 'keyup', function(){ |
242 |
| - if ( $input.val().length ){ |
243 |
| - // Search in select options |
244 |
| - $.each(_this.items, function(i, elm){ |
245 |
| - if ( RegExp('^' + $input.val(), 'i').test(elm.slug) && !elm.disabled ){ |
246 |
| - _select(i); |
247 |
| - return false; |
248 |
| - } |
249 |
| - }); |
250 |
| - } |
251 |
| - }); |
| 258 | + }) |
| 259 | + .on('oninput' in $input[0] ? 'input' : 'keyup', function(){ |
| 260 | + if ( $input.val().length ){ |
| 261 | + // Search in select options |
| 262 | + $.each(_this.items, function(i, elm){ |
| 263 | + if ( RegExp('^' + $input.val(), 'i').test(elm.slug) && !elm.disabled ){ |
| 264 | + _select(i); |
| 265 | + return false; |
| 266 | + } |
| 267 | + }); |
| 268 | + } |
| 269 | + }); |
252 | 270 |
|
253 | 271 | $original.prop('tabindex', false);
|
254 | 272 |
|
|
387 | 405 | }
|
388 | 406 |
|
389 | 407 | // Close the select options box
|
390 |
| - function _close(e) { |
| 408 | + function _close() { |
391 | 409 | _utils.triggerCallback('BeforeClose', _this);
|
392 | 410 |
|
393 |
| - if ( !e && currValue != selected ){ |
| 411 | + if ( currValue != selected ){ |
| 412 | + _utils.triggerCallback('BeforeChange', _this); |
| 413 | + |
394 | 414 | var text = _this.items[selected].text;
|
395 | 415 |
|
396 | 416 | // Apply changed value to original select
|
397 | 417 | $original
|
398 | 418 | .prop('selectedIndex', currValue = selected)
|
399 | 419 | .data('value', text);
|
400 | 420 |
|
401 |
| - _utils.triggerCallback('Change', _this); |
402 |
| - |
403 | 421 | // Change label text
|
404 | 422 | $label.html(text);
|
| 423 | + |
| 424 | + _utils.triggerCallback('Change', _this); |
405 | 425 | }
|
406 | 426 |
|
407 | 427 | // Remove custom events on document
|
|
466 | 486 | $.data(this, pluginName, new Selectric(this, args));
|
467 | 487 | });
|
468 | 488 | };
|
| 489 | + |
| 490 | + $.fn[pluginName].hooks = hooks; |
469 | 491 | }(jQuery));
|
0 commit comments