|
1 | 1 | /* jshint forin:true, noarg:true, noempty:true, eqeqeq:true, boss:true, undef:true, curly:true, browser:true, jquery:true */ |
2 | 2 | /* |
3 | | - * jQuery MultiSelect UI Widget Filtering Plugin 2.0.0 |
| 3 | + * jQuery MultiSelect UI Widget Filtering Plugin 3.0.0 |
4 | 4 | * Copyright (c) 2012 Eric Hynds |
5 | 5 | * |
6 | 6 | * http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ |
7 | 7 | * |
8 | 8 | * Depends: |
| 9 | + * - jQuery 1.7+ |
9 | 10 | * - jQuery UI MultiSelect widget |
10 | 11 | * |
11 | 12 | * Dual licensed under the MIT and GPL licenses: |
|
48 | 49 |
|
49 | 50 | _create: function() { |
50 | 51 | var opts = this.options; |
51 | | - var elem = $(this.element); |
| 52 | + var $elem = this.element; |
52 | 53 |
|
53 | 54 | // get the multiselect instance |
54 | | - this.instance = elem.multiselect('instance'); |
| 55 | + this.instance = $elem.multiselect('instance'); |
55 | 56 |
|
56 | 57 | // store header; add filter class so the close/check all/uncheck all links can be positioned correctly |
57 | | - this.header = this.instance.menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter'); |
| 58 | + this.$header = this.instance.$menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter'); |
58 | 59 |
|
59 | | - // wrapper elem |
60 | | - this.input = $("<input/>").attr({ |
| 60 | + // wrapper $elem |
| 61 | + this.$input = $("<input/>").attr({ |
61 | 62 | placeholder: opts.placeholder, |
62 | 63 | type: "search" |
63 | 64 | }).css({ |
64 | 65 | width: (/\d/.test(opts.width) ? opts.width + 'px' : null) |
65 | | - }).bind({ |
| 66 | + }).on({ |
66 | 67 | keydown: function(e) { |
67 | 68 | // prevent the enter key from submitting the form / closing the widget |
68 | 69 | if(e.which === 13) { |
69 | 70 | e.preventDefault(); |
70 | 71 | } else if(e.which === 27) { |
71 | | - elem.multiselect('close'); |
| 72 | + $elem.multiselect('close'); |
72 | 73 | e.preventDefault(); |
73 | 74 | } else if(e.which === 9 && e.shiftKey) { |
74 | | - elem.multiselect('close'); |
| 75 | + $elem.multiselect('close'); |
75 | 76 | e.preventDefault(); |
76 | 77 | } else if(e.altKey) { |
77 | 78 | switch(e.which) { |
|
80 | 81 | $(this).val('').trigger('input', ''); |
81 | 82 | break; |
82 | 83 | case 65: |
83 | | - elem.multiselect('checkAll'); |
| 84 | + $elem.multiselect('checkAll'); |
84 | 85 | break; |
85 | 86 | case 85: |
86 | | - elem.multiselect('uncheckAll'); |
| 87 | + $elem.multiselect('uncheckAll'); |
87 | 88 | break; |
88 | 89 | case 76: |
89 | | - elem.multiselect('instance').labels.first().trigger("mouseenter"); |
| 90 | + $elem.multiselect('instance').$labels.first().trigger("mouseenter"); |
90 | 91 | break; |
91 | 92 | } |
92 | 93 | } |
|
96 | 97 | }); |
97 | 98 | // automatically reset the widget on close? |
98 | 99 | if(this.options.autoReset) { |
99 | | - elem.bind('multiselectclose', $.proxy(this._reset, this)); |
| 100 | + $elem.on('multiselectclose', $.proxy(this._reset, this)); |
100 | 101 | } |
101 | 102 | // rebuild cache when multiselect is updated |
102 | | - elem.bind('multiselectrefresh', $.proxy(function() { |
| 103 | + $elem.on('multiselectrefresh', $.proxy(function() { |
103 | 104 | this.updateCache(); |
104 | 105 | this._handler(); |
105 | 106 | }, this)); |
106 | | - this.wrapper = $("<div/>").addClass("ui-multiselect-filter").text(opts.label).append(this.input).prependTo(this.header); |
| 107 | + this.$wrapper = $("<div/>").addClass("ui-multiselect-filter").text(opts.label).append(this.$input).prependTo(this.$header); |
107 | 108 |
|
108 | 109 | // reference to the actual inputs |
109 | | - this.inputs = this.instance.menu.find('input[type="checkbox"], input[type="radio"]'); |
| 110 | + this.$inputs = this.instance.$menu.find('input[type="checkbox"], input[type="radio"]'); |
110 | 111 |
|
111 | 112 | // cache input values for searching |
112 | 113 | this.updateCache(); |
113 | 114 |
|
114 | 115 | // rewrite internal _toggleChecked fn so that when checkAll/uncheckAll is fired, |
115 | | - // only the currently filtered elements are checked |
| 116 | + // only the currently filtered $elements are checked |
116 | 117 | this.instance._toggleChecked = function(flag, group) { |
117 | | - var $inputs = (group && group.length) ? group : this.labels.find('input'); |
| 118 | + var $inputs = (group && group.length) ? group : this.$labels.find('input'); |
118 | 119 | var _self = this; |
119 | 120 |
|
120 | 121 | // do not include hidden elems if the menu isn't open. |
|
149 | 150 |
|
150 | 151 | // thx for the logic here ben alman |
151 | 152 | _handler: function(e) { |
152 | | - var term = $.trim(this.input[0].value.toLowerCase()), |
| 153 | + var term = $.trim(this.$input[0].value.toLowerCase()), |
153 | 154 |
|
154 | 155 | // speed up lookups |
155 | | - rows = this.rows, inputs = this.inputs, cache = this.cache; |
156 | | - var $groups = this.instance.menu.find(".ui-multiselect-optgroup"); |
| 156 | + rows = this.rows, $inputs = this.$inputs, $cache = this.$cache; |
| 157 | + var $groups = this.instance.$menu.find(".ui-multiselect-optgroup"); |
157 | 158 | $groups.show(); |
158 | 159 | if(!term) { |
159 | 160 | rows.show(); |
|
162 | 163 |
|
163 | 164 | var regex = new RegExp(term.replace(rEscape, "\\$&"), 'gi'); |
164 | 165 |
|
165 | | - this._trigger("filter", e, $.map(cache, function(v, i) { |
| 166 | + this._trigger("filter", e, $.map($cache, function(v, i) { |
166 | 167 | if(v.search(regex) !== -1) { |
167 | 168 | rows.eq(i).show(); |
168 | | - return inputs.get(i); |
| 169 | + return $inputs.get(i); |
169 | 170 | } |
170 | 171 |
|
171 | 172 | return null; |
|
183 | 184 | }, |
184 | 185 |
|
185 | 186 | _reset: function() { |
186 | | - this.input.val('').trigger('input', ''); |
| 187 | + this.$input.val('').trigger('input', ''); |
187 | 188 | }, |
188 | 189 |
|
189 | 190 | updateCache: function() { |
190 | 191 | // each list item |
191 | | - this.rows = this.instance.labels.parent(); |
| 192 | + this.rows = this.instance.$labels.parent(); |
192 | 193 |
|
193 | 194 | // cache |
194 | | - this.cache = this.element.children().map(function() { |
195 | | - var elem = $(this); |
| 195 | + this.$cache = this.element.children().map(function() { |
| 196 | + var $elem = $(this); |
196 | 197 |
|
197 | 198 | // account for optgroups |
198 | 199 | if(this.tagName.toLowerCase() === "optgroup") { |
199 | | - elem = elem.children(); |
| 200 | + $elem = $elem.children(); |
200 | 201 | } |
201 | 202 |
|
202 | | - return elem.map(function() { |
| 203 | + return $elem.map(function() { |
203 | 204 | return this.innerHTML.toLowerCase(); |
204 | 205 | }).get(); |
205 | 206 | }).get(); |
206 | 207 | }, |
207 | 208 |
|
208 | 209 | widget: function() { |
209 | | - return this.wrapper; |
| 210 | + return this.$wrapper; |
210 | 211 | }, |
211 | 212 |
|
212 | 213 | destroy: function() { |
213 | 214 | $.Widget.prototype.destroy.call(this); |
214 | | - this.input.val('').trigger("keyup"); |
215 | | - this.wrapper.remove(); |
| 215 | + this.$input.val('').trigger("keyup"); |
| 216 | + this.$wrapper.remove(); |
216 | 217 | } |
217 | 218 | }); |
218 | 219 |
|
|
0 commit comments