|
5 | 5 | ; |
6 | 6 | (function ($, window, document, undefined) { |
7 | 7 |
|
| 8 | + // Customize select2 defaults |
| 9 | + $.extend($.fn.select2.defaults.defaults, { |
| 10 | + minimumResultsForSearch: 12, |
| 11 | + theme: 'bootstrap', |
| 12 | + width: 'style', // 'resolve', |
| 13 | + dropdownAutoWidth: false |
| 14 | + }); |
| 15 | + |
8 | 16 | var lists = []; |
9 | 17 |
|
10 | 18 | function load(url, selectedId, callback) { |
|
121 | 129 | lists[options.resetDataUrl] = null; |
122 | 130 | return this.each(function () { }); |
123 | 131 | } |
124 | | - |
| 132 | + |
125 | 133 | options = options || {}; |
126 | 134 |
|
127 | 135 | return this.each(function () { |
|
139 | 147 | } |
140 | 148 | } |
141 | 149 |
|
142 | | - if (!options.lazy && sel.data("select-url")) { |
143 | | - options.lazy = { |
144 | | - url: sel.data("select-url"), |
145 | | - loaded: sel.data("select-loaded") |
146 | | - } |
147 | | - } |
148 | | - |
149 | | - if (!options.init && sel.data("select-init-text") && sel.data("select-selected-id")) { |
150 | | - options.init = { |
151 | | - id: sel.data("select-selected-id"), |
152 | | - text: sel.data("select-init-text") |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - var autoWidth = sel.hasClass("autowidth"), |
157 | | - minResultsForSearch = sel.data("select-min-results-for-search"), |
158 | | - minInputLength = sel.data("select-min-input-length"), |
159 | | - noCache = sel.data("select-nocache"), // future use |
160 | | - lazy = options.lazy, |
161 | | - url = options.lazy ? options.lazy.url : null, |
162 | | - loaded = options.lazy ? options.lazy.loaded : null, |
163 | | - initText = options.init ? options.init.text : null, |
164 | | - selectedId = options.init ? options.init.id : null; |
165 | | - |
166 | 150 | var placeholder = getPlaceholder(); |
167 | 151 |
|
168 | 152 | // following code only applicable to select boxes (not input:hidden) |
|
210 | 194 | } |
211 | 195 |
|
212 | 196 | var opts = { |
213 | | - width: 'style', // 'resolve', |
214 | | - dropdownAutoWidth: false, |
215 | 197 | allowClear: !!(placeholder), // assuming that a placeholder indicates nullability |
216 | 198 | placeholder: placeholder, |
217 | | - minimumResultsForSearch: _.isNumber(minResultsForSearch) ? minResultsForSearch : 8, |
218 | | - minimumInputLength: _.isNumber(minInputLength) ? minInputLength : 0, |
219 | 199 | templateResult: renderSelectItem, |
220 | 200 | templateSelection: renderSelectItem, |
221 | | - theme: 'bootstrap', |
222 | 201 | closeOnSelect: !(sel.prop('multiple') || sel.data("tags")), |
223 | 202 | adaptContainerCssClass: function (c) { |
224 | 203 | if (c.startsWith("select-")) |
|
234 | 213 | } |
235 | 214 | }; |
236 | 215 |
|
| 216 | + if (!options.lazy && sel.data("select-url")) { |
| 217 | + opts.lazy = { |
| 218 | + url: sel.data("select-url"), |
| 219 | + loaded: sel.data("select-loaded") |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + if (!options.init && sel.data("select-init-text") && sel.data("select-selected-id")) { |
| 224 | + opts.init = { |
| 225 | + id: sel.data("select-selected-id"), |
| 226 | + text: sel.data("select-init-text") |
| 227 | + } |
| 228 | + } |
| 229 | + |
237 | 230 | if ($.isPlainObject(options)) { |
238 | 231 | opts = $.extend({}, opts, options); |
239 | 232 | } |
240 | 233 |
|
241 | | - if (url) { |
| 234 | + if (opts.lazy && opts.lazy.url) { |
242 | 235 | // url specified: load data remotely (lazily on first open)... |
243 | 236 | opts.dataAdapter = $.fn.select2.amd.require('select2/data/lazyAdapter'); |
244 | 237 | } |
|
249 | 242 |
|
250 | 243 | sel.select2(opts); |
251 | 244 |
|
252 | | - if (autoWidth) { |
| 245 | + if (sel.hasClass("autowidth")) { |
253 | 246 | // move special "autowidth" class to plugin container, |
254 | 247 | // so we are able to omit min-width per css |
255 | 248 | sel.data("select2").$container.addClass("autowidth"); |
|
0 commit comments