|
2 | 2 | * Project: SmartStore select wrapper
|
3 | 3 | * Author: Murat Cakir, SmartStore AG
|
4 | 4 | */
|
5 |
| -; |
| 5 | + |
6 | 6 | (function ($, window, document, undefined) {
|
7 | 7 |
|
8 | 8 | // Customize select2 defaults
|
|
26 | 26 | callback(data);
|
27 | 27 | }
|
28 | 28 | });
|
29 |
| - }; |
| 29 | + } |
30 | 30 |
|
31 | 31 | $.fn.select2.amd.define('select2/data/lazyAdapter', [
|
32 | 32 | 'select2/data/array',
|
|
55 | 55 | // Add the option tag to the select element,
|
56 | 56 | // otherwise the current val() will not be resolved.
|
57 | 57 | var $option = select.find('option').filter(function (i, elm) {
|
58 |
| - return elm.value == initId; |
| 58 | + return elm.value === initId; |
59 | 59 | });
|
60 | 60 |
|
61 | 61 | if ($option.length === 0) {
|
|
91 | 91 |
|
92 | 92 | list = lists[url];
|
93 | 93 |
|
94 |
| - var doQuery = function (data) { |
95 |
| - list = data; |
96 |
| - if (term) { |
97 |
| - var isGrouped = data.length && data[0].children; |
98 |
| - if (isGrouped) { |
99 |
| - // In a grouped list, find the optgroup marked with "main" |
100 |
| - var mainGroup = _.find(data, function (x) { return x.children && x.main }); |
101 |
| - data = mainGroup ? mainGroup.children : data[0].children; |
102 |
| - } |
103 |
| - list = _.filter(data, function (val) { |
104 |
| - var rg = new RegExp(term, "i"); |
105 |
| - return rg.test(val.text); |
106 |
| - }); |
107 |
| - } |
108 |
| - select.data("loaded", true); |
109 |
| - callback({ results: list }); |
110 |
| - } |
| 94 | + var doQuery = function (data) { |
| 95 | + list = data; |
| 96 | + if (term) { |
| 97 | + var isGrouped = data.length && data[0].children; |
| 98 | + if (isGrouped) { |
| 99 | + // In a grouped list, find the optgroup marked with "main" |
| 100 | + var mainGroup = _.find(data, function (x) { return x.children && x.main; }); |
| 101 | + data = mainGroup ? mainGroup.children : data[0].children; |
| 102 | + } |
| 103 | + list = _.filter(data, function (val) { |
| 104 | + var rg = new RegExp(term, "i"); |
| 105 | + return rg.test(val.text); |
| 106 | + }); |
| 107 | + } |
| 108 | + select.data("loaded", true); |
| 109 | + callback({ results: list }); |
| 110 | + }; |
111 | 111 |
|
112 | 112 | if (!list) {
|
113 | 113 | load(url, initId, doQuery);
|
|
124 | 124 | }
|
125 | 125 | );
|
126 | 126 |
|
127 |
| - $.fn.selectWrapper = function (options) { |
128 |
| - if (options && !_.str.isBlank(options.resetDataUrl) && lists[options.resetDataUrl]) { |
129 |
| - lists[options.resetDataUrl] = null; |
130 |
| - return this.each(function () { }); |
131 |
| - } |
| 127 | + $.fn.selectWrapper = function (options) { |
| 128 | + if (options && !_.str.isBlank(options.resetDataUrl) && lists[options.resetDataUrl]) { |
| 129 | + lists[options.resetDataUrl] = null; |
| 130 | + return this.each(function () { }); |
| 131 | + } |
132 | 132 |
|
133 |
| - options = options || {}; |
| 133 | + options = options || {}; |
134 | 134 |
|
135 | 135 | return this.each(function () {
|
136 |
| - var sel = $(this); |
| 136 | + var sel = $(this); |
137 | 137 |
|
138 |
| - if (sel.data("select2")) { |
| 138 | + if (sel.data("select2")) { |
139 | 139 | // skip process if select is skinned already
|
140 | 140 | return;
|
141 | 141 | }
|
142 |
| - |
| 142 | + |
143 | 143 | if (Modernizr.touchevents && !sel.hasClass("skin")) {
|
144 |
| - if (sel.find('option[data-color], option[data-imageurl]').length == 0) { |
145 |
| - // skip skinning if device is mobile and no rich content exists (color & image) |
146 |
| - return; |
147 |
| - } |
| 144 | + if (sel.find('option[data-color], option[data-imageurl]').length === 0) { |
| 145 | + // skip skinning if device is mobile and no rich content exists (color & image) |
| 146 | + return; |
| 147 | + } |
148 | 148 | }
|
149 | 149 |
|
150 |
| - var placeholder = getPlaceholder(); |
| 150 | + var placeholder = getPlaceholder(); |
151 | 151 |
|
152 | 152 | // following code only applicable to select boxes (not input:hidden)
|
153 | 153 | var firstOption = sel.children("option").first();
|
154 | 154 | var hasOptionLabel = firstOption.length &&
|
155 |
| - (firstOption[0].attributes['value'] === undefined || _.str.isBlank(firstOption.val())); |
| 155 | + (firstOption[0].attributes['value'] === undefined || _.str.isBlank(firstOption.val())); |
156 | 156 |
|
157 | 157 | if (placeholder && hasOptionLabel) {
|
158 | 158 | // clear first option text in nullable dropdowns.
|
|
166 | 166 | firstOption = $('<option></option>').prependTo(sel);
|
167 | 167 | }
|
168 | 168 |
|
169 |
| - if (!placeholder && hasOptionLabel && firstOption.text() && !sel.data("tags")) { |
| 169 | + if (!placeholder && hasOptionLabel && firstOption.text() && !sel.data("tags")) { |
170 | 170 | // use first option text as placeholder
|
171 | 171 | placeholder = firstOption.text();
|
172 | 172 | firstOption.text("");
|
173 | 173 | }
|
174 | 174 |
|
175 | 175 | function renderSelectItem(item, isResult) {
|
176 |
| - try { |
177 |
| - var option = $(item.element), |
178 |
| - imageUrl = option.data('imageurl'), |
179 |
| - color = option.data('color'), |
180 |
| - hint = option.data('hint'); |
181 |
| - |
182 |
| - if (imageUrl) { |
183 |
| - return $('<span><img class="choice-item-img" src="' + imageUrl + '" />' + item.text + '</span>'); |
184 |
| - } |
185 |
| - else if (color) { |
186 |
| - return $('<span><span class="choice-item-color" style="background-color: ' + color + '"></span>' + item.text + '</span>'); |
187 |
| - } |
188 |
| - else if (hint && isResult) { |
189 |
| - return $('<span class="select2-option"><span>' + item.text + '</span><span class="option-hint muted float-right">' + hint + '</span></span>'); |
190 |
| - } |
191 |
| - else { |
192 |
| - return $('<span class="select2-option">' + item.text + '</span>'); |
193 |
| - } |
194 |
| - } |
195 |
| - catch (e) { } |
196 |
| - |
197 |
| - return item.text; |
| 176 | + try { |
| 177 | + var option = $(item.element), |
| 178 | + imageUrl = option.data('imageurl'), |
| 179 | + color = option.data('color'), |
| 180 | + hint = option.data('hint'); |
| 181 | + |
| 182 | + if (imageUrl) { |
| 183 | + return $('<span><img class="choice-item-img" src="' + imageUrl + '" />' + item.text + '</span>'); |
| 184 | + } |
| 185 | + else if (color) { |
| 186 | + return $('<span><span class="choice-item-color" style="background-color: ' + color + '"></span>' + item.text + '</span>'); |
| 187 | + } |
| 188 | + else if (hint && isResult) { |
| 189 | + return $('<span class="select2-option"><span>' + item.text + '</span><span class="option-hint muted float-right">' + hint + '</span></span>'); |
| 190 | + } |
| 191 | + else { |
| 192 | + return $('<span class="select2-option">' + item.text + '</span>'); |
| 193 | + } |
| 194 | + } |
| 195 | + catch (e) { } |
| 196 | + |
| 197 | + return item.text; |
198 | 198 | }
|
199 | 199 |
|
200 | 200 | var opts = {
|
201 |
| - allowClear: !!(placeholder), // assuming that a placeholder indicates nullability |
| 201 | + allowClear: !!placeholder, // assuming that a placeholder indicates nullability |
202 | 202 | placeholder: placeholder,
|
203 |
| - templateResult: function (item) { |
204 |
| - return renderSelectItem(item, true); |
205 |
| - }, |
206 |
| - templateSelection: function (item) { |
207 |
| - return renderSelectItem(item, false); |
208 |
| - }, |
209 |
| - closeOnSelect: !(sel.prop('multiple') || sel.data("tags")), |
210 |
| - adaptContainerCssClass: function (c) { |
211 |
| - if (_.str.startsWith(c, "select-")) |
212 |
| - return c; |
213 |
| - else |
214 |
| - return null; |
215 |
| - }, |
216 |
| - adaptDropdownCssClass: function (c) { |
217 |
| - if (_.str.startsWith("drop-")) |
218 |
| - return c; |
219 |
| - else |
220 |
| - return null; |
221 |
| - } |
222 |
| - }; |
223 |
| - |
224 |
| - if (!options.lazy && sel.data("select-url")) { |
225 |
| - opts.lazy = { |
226 |
| - url: sel.data("select-url"), |
227 |
| - loaded: sel.data("select-loaded") |
228 |
| - } |
229 |
| - } |
| 203 | + templateResult: function (item) { |
| 204 | + return renderSelectItem(item, true); |
| 205 | + }, |
| 206 | + templateSelection: function (item) { |
| 207 | + return renderSelectItem(item, false); |
| 208 | + }, |
| 209 | + closeOnSelect: !(sel.prop('multiple') || sel.data("tags")), |
| 210 | + adaptContainerCssClass: function (c) { |
| 211 | + if (_.str.startsWith(c, "select-")) |
| 212 | + return c; |
| 213 | + else |
| 214 | + return null; |
| 215 | + }, |
| 216 | + adaptDropdownCssClass: function (c) { |
| 217 | + if (_.str.startsWith("drop-")) |
| 218 | + return c; |
| 219 | + else |
| 220 | + return null; |
| 221 | + } |
| 222 | + }; |
| 223 | + |
| 224 | + if (!options.lazy && sel.data("select-url")) { |
| 225 | + opts.lazy = { |
| 226 | + url: sel.data("select-url"), |
| 227 | + loaded: sel.data("select-loaded") |
| 228 | + }; |
| 229 | + } |
230 | 230 |
|
231 |
| - if (!options.init && sel.data("select-init-text") && sel.data("select-selected-id")) { |
232 |
| - opts.init = { |
233 |
| - id: sel.data("select-selected-id"), |
234 |
| - text: sel.data("select-init-text") |
235 |
| - } |
236 |
| - } |
| 231 | + if (!options.init && sel.data("select-init-text") && sel.data("select-selected-id")) { |
| 232 | + opts.init = { |
| 233 | + id: sel.data("select-selected-id"), |
| 234 | + text: sel.data("select-init-text") |
| 235 | + }; |
| 236 | + } |
237 | 237 |
|
238 |
| - if ($.isPlainObject(options)) { |
239 |
| - opts = $.extend({}, opts, options); |
240 |
| - } |
| 238 | + if ($.isPlainObject(options)) { |
| 239 | + opts = $.extend({}, opts, options); |
| 240 | + } |
241 | 241 |
|
242 |
| - if (opts.lazy && opts.lazy.url) { |
243 |
| - // url specified: load data remotely (lazily on first open)... |
244 |
| - opts.dataAdapter = $.fn.select2.amd.require('select2/data/lazyAdapter'); |
245 |
| - } |
246 |
| - else if (opts.ajax && opts.init && opts.init.text && sel.find('option[value="' + opts.init.text + '"]').length === 0) { |
247 |
| - // In AJAX mode: add initial option when missing |
248 |
| - sel.append('<option value="' + opts.init.id + '" selected>' + opts.init.text + '</option>'); |
249 |
| - } |
| 242 | + if (opts.lazy && opts.lazy.url) { |
| 243 | + // url specified: load data remotely (lazily on first open)... |
| 244 | + opts.dataAdapter = $.fn.select2.amd.require('select2/data/lazyAdapter'); |
| 245 | + } |
| 246 | + else if (opts.ajax && opts.init && opts.init.text && sel.find('option[value="' + opts.init.text + '"]').length === 0) { |
| 247 | + // In AJAX mode: add initial option when missing |
| 248 | + sel.append('<option value="' + opts.init.id + '" selected>' + opts.init.text + '</option>'); |
| 249 | + } |
250 | 250 |
|
251 |
| - sel.select2(opts); |
| 251 | + sel.select2(opts); |
252 | 252 |
|
253 |
| - if (sel.hasClass("autowidth")) { |
| 253 | + if (sel.hasClass("autowidth")) { |
254 | 254 | // move special "autowidth" class to plugin container,
|
255 |
| - // so we are able to omit min-width per css |
| 255 | + // so we are able to omit min-width per css |
256 | 256 | sel.data("select2").$container.addClass("autowidth");
|
257 | 257 | }
|
258 | 258 |
|
259 |
| - function getPlaceholder() { |
260 |
| - return options.placeholder || |
261 |
| - sel.attr("placeholder") || |
262 |
| - sel.data("placeholder") || |
263 |
| - sel.data("select-placeholder"); |
| 259 | + function getPlaceholder() { |
| 260 | + return options.placeholder || |
| 261 | + sel.attr("placeholder") || |
| 262 | + sel.data("placeholder") || |
| 263 | + sel.data("select-placeholder"); |
264 | 264 | }
|
265 | 265 |
|
266 | 266 | });
|
267 | 267 |
|
268 |
| - } |
| 268 | + }; |
269 | 269 |
|
270 | 270 | })(jQuery, window, document);
|
0 commit comments