|
21 | 21 | "use strict"; |
22 | 22 |
|
23 | 23 | var Combobox = function ( element, options ) { |
24 | | - this.options = $.extend({}, $.fn.combobox.defaults, options) |
25 | | - this.$source = $(element) |
26 | | - this.$container = this.setup() |
27 | | - this.$element = this.$container.find('input[type=text]') |
28 | | - this.$target = this.$container.find('input[type=hidden]') |
29 | | - this.$button = this.$container.find('.dropdown-toggle') |
30 | | - this.$menu = $(this.options.menu).appendTo('body') |
31 | | - this.matcher = this.options.matcher || this.matcher |
32 | | - this.sorter = this.options.sorter || this.sorter |
33 | | - this.highlighter = this.options.highlighter || this.highlighter |
34 | | - this.shown = false |
35 | | - this.selected = false |
36 | | - this.refresh() |
37 | | - this.transferAttributes() |
38 | | - this.listen() |
39 | | - } |
| 24 | + this.options = $.extend({}, $.fn.combobox.defaults, options); |
| 25 | + this.$source = $(element); |
| 26 | + this.$container = this.setup(); |
| 27 | + this.$element = this.$container.find('input[type=text]'); |
| 28 | + this.$target = this.$container.find('input[type=hidden]'); |
| 29 | + this.$button = this.$container.find('.dropdown-toggle'); |
| 30 | + this.$menu = $(this.options.menu).appendTo('body'); |
| 31 | + this.matcher = this.options.matcher || this.matcher; |
| 32 | + this.sorter = this.options.sorter || this.sorter; |
| 33 | + this.highlighter = this.options.highlighter || this.highlighter; |
| 34 | + this.shown = false; |
| 35 | + this.selected = false; |
| 36 | + this.refresh(); |
| 37 | + this.transferAttributes(); |
| 38 | + this.listen(); |
| 39 | + }; |
40 | 40 |
|
41 | 41 | /* NOTE: COMBOBOX EXTENDS BOOTSTRAP-TYPEAHEAD.js |
42 | 42 | ========================================== */ |
|
46 | 46 | constructor: Combobox |
47 | 47 |
|
48 | 48 | , setup: function () { |
49 | | - var combobox = $(this.options.template) |
50 | | - this.$source.before(combobox) |
51 | | - this.$source.hide() |
52 | | - return combobox |
| 49 | + var combobox = $(this.options.template); |
| 50 | + this.$source.before(combobox); |
| 51 | + this.$source.hide(); |
| 52 | + return combobox; |
53 | 53 | } |
54 | 54 |
|
55 | 55 | , parse: function () { |
56 | 56 | var that = this |
57 | 57 | , map = {} |
58 | 58 | , source = [] |
59 | 59 | , selected = false |
60 | | - , selectedValue = '' |
| 60 | + , selectedValue = ''; |
61 | 61 | this.$source.find('option').each(function() { |
62 | | - var option = $(this) |
| 62 | + var option = $(this); |
63 | 63 | if (option.val() === '') { |
64 | | - that.options.placeholder = option.text() |
65 | | - return |
| 64 | + that.options.placeholder = option.text(); |
| 65 | + return; |
66 | 66 | } |
67 | | - map[option.text()] = option.val() |
68 | | - source.push(option.text()) |
| 67 | + map[option.text()] = option.val(); |
| 68 | + source.push(option.text()); |
69 | 69 | if (option.prop('selected')) { |
70 | | - selected = option.text() |
71 | | - selectedValue = option.val() |
| 70 | + selected = option.text(); |
| 71 | + selectedValue = option.val(); |
72 | 72 | } |
73 | 73 | }) |
74 | | - this.map = map |
| 74 | + this.map = map; |
75 | 75 | if (selected) { |
76 | | - this.$element.val(selected) |
77 | | - this.$target.val(selectedValue) |
78 | | - this.$container.addClass('combobox-selected') |
79 | | - this.selected = true |
| 76 | + this.$element.val(selected); |
| 77 | + this.$target.val(selectedValue); |
| 78 | + this.$container.addClass('combobox-selected'); |
| 79 | + this.selected = true; |
80 | 80 | } |
81 | | - return source |
| 81 | + return source; |
82 | 82 | } |
83 | 83 |
|
84 | 84 | , transferAttributes: function() { |
85 | | - this.options.placeholder = this.$source.attr('data-placeholder') || this.options.placeholder |
86 | | - this.$element.attr('placeholder', this.options.placeholder) |
87 | | - this.$target.prop('name', this.$source.prop('name')) |
88 | | - this.$target.val(this.$source.val()) |
89 | | - this.$source.removeAttr('name') // Remove from source otherwise form will pass parameter twice. |
90 | | - this.$element.attr('required', this.$source.attr('required')) |
91 | | - this.$element.attr('rel', this.$source.attr('rel')) |
92 | | - this.$element.attr('title', this.$source.attr('title')) |
93 | | - this.$element.attr('class', this.$source.attr('class')) |
94 | | - this.$element.attr('tabindex', this.$source.attr('tabindex')) |
95 | | - this.$source.removeAttr('tabindex') |
| 85 | + this.options.placeholder = this.$source.attr('data-placeholder') || this.options.placeholder; |
| 86 | + this.$element.attr('placeholder', this.options.placeholder); |
| 87 | + this.$target.prop('name', this.$source.prop('name')); |
| 88 | + this.$target.val(this.$source.val()); |
| 89 | + this.$source.removeAttr('name'); // Remove from source otherwise form will pass parameter twice. |
| 90 | + this.$element.attr('required', this.$source.attr('required')); |
| 91 | + this.$element.attr('rel', this.$source.attr('rel')); |
| 92 | + this.$element.attr('title', this.$source.attr('title')); |
| 93 | + this.$element.attr('class', this.$source.attr('class')); |
| 94 | + this.$element.attr('tabindex', this.$source.attr('tabindex')); |
| 95 | + this.$source.removeAttr('tabindex'); |
96 | 96 | } |
97 | 97 |
|
98 | 98 | , toggle: function () { |
99 | 99 | if (this.$container.hasClass('combobox-selected')) { |
100 | | - this.clearTarget() |
101 | | - this.triggerChange() |
102 | | - this.clearElement() |
| 100 | + this.clearTarget(); |
| 101 | + this.triggerChange(); |
| 102 | + this.clearElement(); |
103 | 103 | } else { |
104 | 104 | if (this.shown) { |
105 | | - this.hide() |
| 105 | + this.hide(); |
106 | 106 | } else { |
107 | | - this.clearElement() |
108 | | - this.lookup() |
| 107 | + this.clearElement(); |
| 108 | + this.lookup(); |
109 | 109 | } |
110 | 110 | } |
111 | 111 | } |
112 | 112 |
|
113 | 113 | , clearElement: function () { |
114 | | - this.$element.val('').focus() |
| 114 | + this.$element.val('').focus(); |
115 | 115 | } |
116 | 116 |
|
117 | 117 | , clearTarget: function () { |
118 | | - this.$source.val('') |
119 | | - this.$target.val('') |
120 | | - this.$container.removeClass('combobox-selected') |
121 | | - this.selected = false |
| 118 | + this.$source.val(''); |
| 119 | + this.$target.val(''); |
| 120 | + this.$container.removeClass('combobox-selected'); |
| 121 | + this.selected = false; |
122 | 122 | } |
123 | 123 |
|
124 | 124 | , triggerChange: function () { |
125 | | - this.$source.trigger('change') |
| 125 | + this.$source.trigger('change'); |
126 | 126 | } |
127 | 127 |
|
128 | 128 | , refresh: function () { |
129 | | - this.source = this.parse() |
130 | | - this.options.items = this.source.length |
| 129 | + this.source = this.parse(); |
| 130 | + this.options.items = this.source.length; |
131 | 131 | } |
132 | 132 |
|
133 | 133 | // modified typeahead function adding container and target handling |
134 | 134 | , select: function () { |
135 | | - var val = this.$menu.find('.active').attr('data-value') |
136 | | - this.$element.val(this.updater(val)).trigger('change') |
137 | | - this.$source.val(this.map[val]).trigger('change') |
138 | | - this.$target.val(this.map[val]).trigger('change') |
139 | | - this.$container.addClass('combobox-selected') |
140 | | - this.selected = true |
141 | | - return this.hide() |
| 135 | + var val = this.$menu.find('.active').attr('data-value'); |
| 136 | + this.$element.val(this.updater(val)).trigger('change'); |
| 137 | + this.$source.val(this.map[val]).trigger('change'); |
| 138 | + this.$target.val(this.map[val]).trigger('change'); |
| 139 | + this.$container.addClass('combobox-selected'); |
| 140 | + this.selected = true; |
| 141 | + return this.hide(); |
142 | 142 | } |
143 | 143 |
|
144 | 144 | // modified typeahead function removing the blank handling and source function handling |
145 | 145 | , lookup: function (event) { |
146 | | - this.query = this.$element.val() |
147 | | - return this.process(this.source) |
| 146 | + this.query = this.$element.val(); |
| 147 | + return this.process(this.source); |
148 | 148 | } |
149 | 149 |
|
150 | 150 | // modified typeahead function adding button handling and remove mouseleave |
|
153 | 153 | .on('focus', $.proxy(this.focus, this)) |
154 | 154 | .on('blur', $.proxy(this.blur, this)) |
155 | 155 | .on('keypress', $.proxy(this.keypress, this)) |
156 | | - .on('keyup', $.proxy(this.keyup, this)) |
| 156 | + .on('keyup', $.proxy(this.keyup, this)); |
157 | 157 |
|
158 | 158 | if (this.eventSupported('keydown')) { |
159 | | - this.$element.on('keydown', $.proxy(this.keydown, this)) |
| 159 | + this.$element.on('keydown', $.proxy(this.keydown, this)); |
160 | 160 | } |
161 | 161 |
|
162 | 162 | this.$menu |
163 | 163 | .on('click', $.proxy(this.click, this)) |
164 | 164 | .on('mouseenter', 'li', $.proxy(this.mouseenter, this)) |
165 | | - .on('mouseleave', 'li', $.proxy(this.mouseleave, this)) |
| 165 | + .on('mouseleave', 'li', $.proxy(this.mouseleave, this)); |
166 | 166 |
|
167 | 167 | this.$button |
168 | | - .on('click', $.proxy(this.toggle, this)) |
| 168 | + .on('click', $.proxy(this.toggle, this)); |
169 | 169 | } |
170 | 170 |
|
171 | 171 | // modified typeahead function to clear on type and prevent on moving around |
|
180 | 180 | case 16: // shift |
181 | 181 | case 17: // ctrl |
182 | 182 | case 18: // alt |
183 | | - break |
| 183 | + break; |
184 | 184 |
|
185 | 185 | case 9: // tab |
186 | | - case 13: // enter |
187 | | - if (!this.shown) return |
188 | | - this.select() |
189 | | - break |
| 186 | + case 13: // enter |
| 187 | + if (!this.shown) return; |
| 188 | + this.select(); |
| 189 | + break; |
190 | 190 |
|
191 | | - case 27: // escape |
192 | | - if (!this.shown) return |
193 | | - this.hide() |
194 | | - break |
| 191 | + case 27: // escape |
| 192 | + if (!this.shown) return; |
| 193 | + this.hide(); |
| 194 | + break; |
195 | 195 |
|
196 | 196 | default: |
197 | | - this.clearTarget() |
198 | | - this.lookup() |
| 197 | + this.clearTarget(); |
| 198 | + this.lookup(); |
199 | 199 | } |
200 | 200 |
|
201 | | - e.stopPropagation() |
202 | | - e.preventDefault() |
| 201 | + e.stopPropagation(); |
| 202 | + e.preventDefault(); |
203 | 203 | } |
204 | 204 |
|
205 | 205 | // modified typeahead function to force a match and add a delay on hide |
206 | 206 | , blur: function (e) { |
207 | | - var that = this |
208 | | - this.focused = false |
209 | | - var val = this.$element.val() |
| 207 | + var that = this; |
| 208 | + this.focused = false; |
| 209 | + var val = this.$element.val(); |
210 | 210 | if (!this.selected && val !== '' ) { |
211 | | - this.$element.val('') |
212 | | - this.$source.val('').trigger('change') |
213 | | - this.$target.val('').trigger('change') |
| 211 | + this.$element.val(''); |
| 212 | + this.$source.val('').trigger('change'); |
| 213 | + this.$target.val('').trigger('change'); |
214 | 214 | } |
215 | | - if (!this.mousedover && this.shown) setTimeout(function () { that.hide() }, 200) |
| 215 | + if (!this.mousedover && this.shown) {setTimeout(function () { that.hide(); }, 200);} |
216 | 216 | } |
217 | 217 |
|
218 | 218 | // modified typeahead function to not hide |
219 | 219 | , mouseleave: function (e) { |
220 | | - this.mousedover = false |
| 220 | + this.mousedover = false; |
221 | 221 | } |
222 | | - }) |
| 222 | + }); |
223 | 223 |
|
224 | 224 | /* COMBOBOX PLUGIN DEFINITION |
225 | 225 | * =========================== */ |
|
228 | 228 | return this.each(function () { |
229 | 229 | var $this = $(this) |
230 | 230 | , data = $this.data('combobox') |
231 | | - , options = typeof option == 'object' && option |
232 | | - if(!data) $this.data('combobox', (data = new Combobox(this, options))) |
233 | | - if (typeof option == 'string') data[option]() |
234 | | - }) |
235 | | - } |
| 231 | + , options = typeof option == 'object' && option; |
| 232 | + if(!data) {$this.data('combobox', (data = new Combobox(this, options)));} |
| 233 | + if (typeof option == 'string') {data[option]();} |
| 234 | + }); |
| 235 | + }; |
236 | 236 |
|
237 | 237 | $.fn.combobox.defaults = { |
238 | 238 | template: '<div class="combobox-container"><input type="hidden" /><input type="text" autocomplete="off" /><span class="add-on btn dropdown-toggle" data-dropdown="dropdown"><span class="caret"/><span class="combobox-clear"><i class="icon-remove"/></span></span></div>' |
239 | 239 | , menu: '<ul class="typeahead typeahead-long dropdown-menu"></ul>' |
240 | 240 | , item: '<li><a href="#"></a></li>' |
241 | | - } |
| 241 | + }; |
242 | 242 |
|
243 | | - $.fn.combobox.Constructor = Combobox |
| 243 | + $.fn.combobox.Constructor = Combobox; |
244 | 244 |
|
245 | 245 | }( window.jQuery ); |
0 commit comments