rdcms 1.0.26 → 1.0.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/rdcms/active_admin.js +0 -1
  3. data/lib/rdcms/version.rb +1 -1
  4. metadata +2 -41
  5. data/app/assets/images/rdcms/select2/select2-spinner.gif +0 -0
  6. data/app/assets/images/rdcms/select2/select2.png +0 -0
  7. data/app/assets/images/rdcms/select2/select2x2.png +0 -0
  8. data/app/assets/javascripts/rdcms/init/select2.js +0 -15
  9. data/app/assets/javascripts/rdcms/plugins/select2/select2.js +0 -3065
  10. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_ca.js +0 -17
  11. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_cs.js +0 -49
  12. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_da.js +0 -17
  13. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_de.js +0 -15
  14. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_es.js +0 -15
  15. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_et.js +0 -17
  16. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_eu.js +0 -43
  17. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_fr.js +0 -15
  18. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_gl.js +0 -43
  19. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_he.js +0 -17
  20. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_hr.js +0 -42
  21. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_hu.js +0 -15
  22. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_is.js +0 -16
  23. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_it.js +0 -15
  24. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_ja.js +0 -15
  25. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_lt.js +0 -29
  26. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_lv.js +0 -16
  27. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_mk.js +0 -17
  28. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_nl.js +0 -15
  29. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_no.js +0 -18
  30. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_pl.js +0 -37
  31. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_pt-BR.js +0 -15
  32. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_pt-PT.js +0 -15
  33. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_ro.js +0 -15
  34. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_ru.js +0 -15
  35. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_sk.js +0 -48
  36. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_sv.js +0 -17
  37. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_tr.js +0 -17
  38. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_ua.js +0 -17
  39. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_vi.js +0 -18
  40. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_zh-CN.js +0 -14
  41. data/app/assets/javascripts/rdcms/plugins/select2/select2_locale_zh-TW.js +0 -14
  42. data/app/assets/stylesheets/rdcms/select2/select2.css +0 -652
  43. data/config/initializers/net.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43f0c587aa16463aacfca6062caee2a82379473a
4
- data.tar.gz: b111c93753c09c4f4b83f8cf95bf722ed1a8ab61
3
+ metadata.gz: 2bde1303dbcf29c817b270b4549dca59ffd9c2ac
4
+ data.tar.gz: 870f68377eb2c39e7445c24ed15f5e0d02996ad0
5
5
  SHA512:
6
- metadata.gz: 21b36fd6ea9b32b5e3186e511f24b896dd26c028d9334955bf6c5871887fca92fd89fff4501ce9a540534d9551ffca373462f097bc61a9e49b9185138bae737a
7
- data.tar.gz: 2d76a2460ca6ed8305521bb858f1b07b6984acc9e0530a856c4ec466ef91b8c711ef740c212be1e7af8ccde19e1737bf255d9e853a0850294b7ba2352317ef23
6
+ metadata.gz: bfd286376dbee641966de92ee4fb436e8ad62a5f74fff467f9436659c7829226bd8268c2353a42198ba5bf3b29eec042e8d9e61f88ea0006841f0bb3dd4bc16a
7
+ data.tar.gz: 062650b9e567269efd37ca9df2fd95173e759e459211232268ad2ae14387ce24c82164e394efcefd548b6973109cc8aa3a57c24f47c591b38b49219ec8f8cfdb
@@ -23,7 +23,6 @@
23
23
  //= require bootstrap-editable
24
24
 
25
25
  //= require rdcms/init/social
26
- //= require rdcms/init/select2
27
26
 
28
27
  //= require rdcms/sliders
29
28
 
@@ -1,3 +1,3 @@
1
1
  module Rdcms
2
- VERSION = "1.0.26"
2
+ VERSION = "1.0.27"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdcms
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.26
4
+ version: 1.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gullit Miranda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-29 00:00:00.000000000 Z
11
+ date: 2013-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano
@@ -1008,9 +1008,6 @@ files:
1008
1008
  - app/assets/images/rdcms/sliders/sprite.png
1009
1009
  - app/assets/images/rdcms/sliders/pattern_bg.png
1010
1010
  - app/assets/images/rdcms/sliders/grab.png
1011
- - app/assets/images/rdcms/select2/select2x2.png
1012
- - app/assets/images/rdcms/select2/select2-spinner.gif
1013
- - app/assets/images/rdcms/select2/select2.png
1014
1011
  - app/assets/images/rdcms/flags.png
1015
1012
  - app/assets/javascripts/ckeditor/plugins/pagebreak/plugin.js
1016
1013
  - app/assets/javascripts/ckeditor/plugins/pagebreak/images/pagebreak.gif
@@ -2266,39 +2263,6 @@ files:
2266
2263
  - app/assets/javascripts/rdcms/plugins/jquery.easing.1.3.js
2267
2264
  - app/assets/javascripts/rdcms/plugins/jquery.tools.min.js
2268
2265
  - app/assets/javascripts/rdcms/plugins/jquery.chosen.init.js
2269
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_pl.js
2270
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_zh-CN.js
2271
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_tr.js
2272
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_lt.js
2273
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_he.js
2274
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_ro.js
2275
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_eu.js
2276
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_hu.js
2277
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_it.js
2278
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_is.js
2279
- - app/assets/javascripts/rdcms/plugins/select2/select2.js
2280
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_nl.js
2281
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_zh-TW.js
2282
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_gl.js
2283
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_sv.js
2284
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_hr.js
2285
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_de.js
2286
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_sk.js
2287
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_cs.js
2288
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_et.js
2289
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_lv.js
2290
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_pt-BR.js
2291
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_ja.js
2292
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_vi.js
2293
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_mk.js
2294
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_fr.js
2295
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_no.js
2296
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_ca.js
2297
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_da.js
2298
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_es.js
2299
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_ru.js
2300
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_ua.js
2301
- - app/assets/javascripts/rdcms/plugins/select2/select2_locale_pt-PT.js
2302
2266
  - app/assets/javascripts/rdcms/plugins/jquery.metadata.js
2303
2267
  - app/assets/javascripts/rdcms/plugins/jquery.mousewheel.min.js
2304
2268
  - app/assets/javascripts/rdcms/plugins/interface.js
@@ -2310,7 +2274,6 @@ files:
2310
2274
  - app/assets/javascripts/rdcms/raphael.js
2311
2275
  - app/assets/javascripts/rdcms/full_screen.js
2312
2276
  - app/assets/javascripts/rdcms/init/hash.js
2313
- - app/assets/javascripts/rdcms/init/select2.js
2314
2277
  - app/assets/javascripts/rdcms/init/social.js.coffee
2315
2278
  - app/assets/javascripts/rdcms/init/bootstrap.js.coffee
2316
2279
  - app/assets/javascripts/rdcms/admin/farbtastic.js
@@ -2333,7 +2296,6 @@ files:
2333
2296
  - app/assets/stylesheets/rdcms/jquery.fileupload-ui.css.scss
2334
2297
  - app/assets/stylesheets/rdcms/sponsor.css.scss
2335
2298
  - app/assets/stylesheets/rdcms/formstyle.css.scss
2336
- - app/assets/stylesheets/rdcms/select2/select2.css
2337
2299
  - app/assets/stylesheets/rdcms/active_admin.css.scss
2338
2300
  - app/assets/stylesheets/rdcms/single_upload.css.scss
2339
2301
  - app/assets/stylesheets/rdcms/flags.css
@@ -2416,7 +2378,6 @@ files:
2416
2378
  - config/initializers/settings.rb
2417
2379
  - config/initializers/helper.rb
2418
2380
  - config/initializers/acts_as_taggable.rb
2419
- - config/initializers/net.rb
2420
2381
  - config/initializers/http_validator.rb
2421
2382
  - config/initializers/check_for_migrations.rb
2422
2383
  - config/initializers/compass.rb
@@ -1,15 +0,0 @@
1
- //= require rdcms/plugins/select2/select2
2
- //= require rdcms/plugins/select2/select2_locale_pt-BR
3
-
4
- $.loadSelect2 = function(options) {
5
- if ($.fn.select2) {
6
- $(".select2").select2();
7
- $(".select2-tag").select2($.extend({
8
- tags: [],
9
- tokenSeparators: [","]
10
- }, options));
11
- }
12
- return true;
13
- };
14
-
15
- jQuery(function() { return $.loadSelect2(); });
@@ -1,3065 +0,0 @@
1
- /*
2
- Copyright 2012 Igor Vaynberg
3
-
4
- Version: @@ver@@ Timestamp: @@timestamp@@
5
-
6
- This software is licensed under the Apache License, Version 2.0 (the "Apache License") or the GNU
7
- General Public License version 2 (the "GPL License"). You may choose either license to govern your
8
- use of this software only upon the condition that you accept all of the terms of either the Apache
9
- License or the GPL License.
10
-
11
- You may obtain a copy of the Apache License and the GPL License at:
12
-
13
- http://www.apache.org/licenses/LICENSE-2.0
14
- http://www.gnu.org/licenses/gpl-2.0.html
15
-
16
- Unless required by applicable law or agreed to in writing, software distributed under the
17
- Apache License or the GPL Licesnse is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
18
- CONDITIONS OF ANY KIND, either express or implied. See the Apache License and the GPL License for
19
- the specific language governing permissions and limitations under the Apache License and the GPL License.
20
- */
21
- (function ($) {
22
- if(typeof $.fn.each2 == "undefined"){
23
- $.fn.extend({
24
- /*
25
- * 4-10 times faster .each replacement
26
- * use it carefully, as it overrides jQuery context of element on each iteration
27
- */
28
- each2 : function (c) {
29
- var j = $([0]), i = -1, l = this.length;
30
- while (
31
- ++i < l
32
- && (j.context = j[0] = this[i])
33
- && c.call(j[0], i, j) !== false //"this"=DOM, i=index, j=jQuery object
34
- );
35
- return this;
36
- }
37
- });
38
- }
39
- })(jQuery);
40
-
41
- (function ($, undefined) {
42
- "use strict";
43
- /*global document, window, jQuery, console */
44
-
45
- if (window.Select2 !== undefined) {
46
- return;
47
- }
48
-
49
- var KEY, AbstractSelect2, SingleSelect2, MultiSelect2, nextUid, sizer,
50
- lastMousePosition={x:0,y:0}, $document, scrollBarDimensions,
51
-
52
- KEY = {
53
- TAB: 9,
54
- ENTER: 13,
55
- ESC: 27,
56
- SPACE: 32,
57
- LEFT: 37,
58
- UP: 38,
59
- RIGHT: 39,
60
- DOWN: 40,
61
- SHIFT: 16,
62
- CTRL: 17,
63
- ALT: 18,
64
- PAGE_UP: 33,
65
- PAGE_DOWN: 34,
66
- HOME: 36,
67
- END: 35,
68
- BACKSPACE: 8,
69
- DELETE: 46,
70
- isArrow: function (k) {
71
- k = k.which ? k.which : k;
72
- switch (k) {
73
- case KEY.LEFT:
74
- case KEY.RIGHT:
75
- case KEY.UP:
76
- case KEY.DOWN:
77
- return true;
78
- }
79
- return false;
80
- },
81
- isControl: function (e) {
82
- var k = e.which;
83
- switch (k) {
84
- case KEY.SHIFT:
85
- case KEY.CTRL:
86
- case KEY.ALT:
87
- return true;
88
- }
89
-
90
- if (e.metaKey) return true;
91
-
92
- return false;
93
- },
94
- isFunctionKey: function (k) {
95
- k = k.which ? k.which : k;
96
- return k >= 112 && k <= 123;
97
- }
98
- },
99
- MEASURE_SCROLLBAR_TEMPLATE = "<div class='select2-measure-scrollbar'></div>";
100
-
101
- $document = $(document);
102
-
103
- nextUid=(function() { var counter=1; return function() { return counter++; }; }());
104
-
105
- function indexOf(value, array) {
106
- var i = 0, l = array.length;
107
- for (; i < l; i = i + 1) {
108
- if (equal(value, array[i])) return i;
109
- }
110
- return -1;
111
- }
112
-
113
- function measureScrollbar () {
114
- var $template = $( MEASURE_SCROLLBAR_TEMPLATE );
115
- $template.appendTo('body');
116
-
117
- var dim = {
118
- width: $template.width() - $template[0].clientWidth,
119
- height: $template.height() - $template[0].clientHeight
120
- };
121
- $template.remove();
122
-
123
- return dim;
124
- }
125
-
126
- /**
127
- * Compares equality of a and b
128
- * @param a
129
- * @param b
130
- */
131
- function equal(a, b) {
132
- if (a === b) return true;
133
- if (a === undefined || b === undefined) return false;
134
- if (a === null || b === null) return false;
135
- if (a.constructor === String) return a+'' === b+''; // IE requires a+'' instead of just a
136
- if (b.constructor === String) return b+'' === a+''; // IE requires b+'' instead of just b
137
- return false;
138
- }
139
-
140
- /**
141
- * Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
142
- * strings
143
- * @param string
144
- * @param separator
145
- */
146
- function splitVal(string, separator) {
147
- var val, i, l;
148
- if (string === null || string.length < 1) return [];
149
- val = string.split(separator);
150
- for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
151
- return val;
152
- }
153
-
154
- function getSideBorderPadding(element) {
155
- return element.outerWidth(false) - element.width();
156
- }
157
-
158
- function installKeyUpChangeEvent(element) {
159
- var key="keyup-change-value";
160
- element.on("keydown", function () {
161
- if ($.data(element, key) === undefined) {
162
- $.data(element, key, element.val());
163
- }
164
- });
165
- element.on("keyup", function () {
166
- var val= $.data(element, key);
167
- if (val !== undefined && element.val() !== val) {
168
- $.removeData(element, key);
169
- element.trigger("keyup-change");
170
- }
171
- });
172
- }
173
-
174
- $document.on("mousemove", function (e) {
175
- lastMousePosition.x = e.pageX;
176
- lastMousePosition.y = e.pageY;
177
- });
178
-
179
- /**
180
- * filters mouse events so an event is fired only if the mouse moved.
181
- *
182
- * filters out mouse events that occur when mouse is stationary but
183
- * the elements under the pointer are scrolled.
184
- */
185
- function installFilteredMouseMove(element) {
186
- element.on("mousemove", function (e) {
187
- var lastpos = lastMousePosition;
188
- if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) {
189
- $(e.target).trigger("mousemove-filtered", e);
190
- }
191
- });
192
- }
193
-
194
- /**
195
- * Debounces a function. Returns a function that calls the original fn function only if no invocations have been made
196
- * within the last quietMillis milliseconds.
197
- *
198
- * @param quietMillis number of milliseconds to wait before invoking fn
199
- * @param fn function to be debounced
200
- * @param ctx object to be used as this reference within fn
201
- * @return debounced version of fn
202
- */
203
- function debounce(quietMillis, fn, ctx) {
204
- ctx = ctx || undefined;
205
- var timeout;
206
- return function () {
207
- var args = arguments;
208
- window.clearTimeout(timeout);
209
- timeout = window.setTimeout(function() {
210
- fn.apply(ctx, args);
211
- }, quietMillis);
212
- };
213
- }
214
-
215
- /**
216
- * A simple implementation of a thunk
217
- * @param formula function used to lazily initialize the thunk
218
- * @return {Function}
219
- */
220
- function thunk(formula) {
221
- var evaluated = false,
222
- value;
223
- return function() {
224
- if (evaluated === false) { value = formula(); evaluated = true; }
225
- return value;
226
- };
227
- };
228
-
229
- function installDebouncedScroll(threshold, element) {
230
- var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);});
231
- element.on("scroll", function (e) {
232
- if (indexOf(e.target, element.get()) >= 0) notify(e);
233
- });
234
- }
235
-
236
- function focus($el) {
237
- if ($el[0] === document.activeElement) return;
238
-
239
- /* set the focus in a 0 timeout - that way the focus is set after the processing
240
- of the current event has finished - which seems like the only reliable way
241
- to set focus */
242
- window.setTimeout(function() {
243
- var el=$el[0], pos=$el.val().length, range;
244
-
245
- $el.focus();
246
-
247
- /* make sure el received focus so we do not error out when trying to manipulate the caret.
248
- sometimes modals or others listeners may steal it after its set */
249
- if ($el.is(":visible") && el === document.activeElement) {
250
-
251
- /* after the focus is set move the caret to the end, necessary when we val()
252
- just before setting focus */
253
- if(el.setSelectionRange)
254
- {
255
- el.setSelectionRange(pos, pos);
256
- }
257
- else if (el.createTextRange) {
258
- range = el.createTextRange();
259
- range.collapse(false);
260
- range.select();
261
- }
262
- }
263
- }, 0);
264
- }
265
-
266
- function getCursorInfo(el) {
267
- el = $(el)[0];
268
- var offset = 0;
269
- var length = 0;
270
- if ('selectionStart' in el) {
271
- offset = el.selectionStart;
272
- length = el.selectionEnd - offset;
273
- } else if ('selection' in document) {
274
- el.focus();
275
- var sel = document.selection.createRange();
276
- length = document.selection.createRange().text.length;
277
- sel.moveStart('character', -el.value.length);
278
- offset = sel.text.length - length;
279
- }
280
- return { offset: offset, length: length };
281
- }
282
-
283
- function killEvent(event) {
284
- event.preventDefault();
285
- event.stopPropagation();
286
- }
287
- function killEventImmediately(event) {
288
- event.preventDefault();
289
- event.stopImmediatePropagation();
290
- }
291
-
292
- function measureTextWidth(e) {
293
- if (!sizer){
294
- var style = e[0].currentStyle || window.getComputedStyle(e[0], null);
295
- sizer = $(document.createElement("div")).css({
296
- position: "absolute",
297
- left: "-10000px",
298
- top: "-10000px",
299
- display: "none",
300
- fontSize: style.fontSize,
301
- fontFamily: style.fontFamily,
302
- fontStyle: style.fontStyle,
303
- fontWeight: style.fontWeight,
304
- letterSpacing: style.letterSpacing,
305
- textTransform: style.textTransform,
306
- whiteSpace: "nowrap"
307
- });
308
- sizer.attr("class","select2-sizer");
309
- $("body").append(sizer);
310
- }
311
- sizer.text(e.val());
312
- return sizer.width();
313
- }
314
-
315
- function syncCssClasses(dest, src, adapter) {
316
- var classes, replacements = [], adapted;
317
-
318
- classes = dest.attr("class");
319
- if (classes) {
320
- classes = '' + classes; // for IE which returns object
321
- $(classes.split(" ")).each2(function() {
322
- if (this.indexOf("select2-") === 0) {
323
- replacements.push(this);
324
- }
325
- });
326
- }
327
- classes = src.attr("class");
328
- if (classes) {
329
- classes = '' + classes; // for IE which returns object
330
- $(classes.split(" ")).each2(function() {
331
- if (this.indexOf("select2-") !== 0) {
332
- adapted = adapter(this);
333
- if (adapted) {
334
- replacements.push(this);
335
- }
336
- }
337
- });
338
- }
339
- dest.attr("class", replacements.join(" "));
340
- }
341
-
342
-
343
- function markMatch(text, term, markup, escapeMarkup) {
344
- var match=text.toUpperCase().indexOf(term.toUpperCase()),
345
- tl=term.length;
346
-
347
- if (match<0) {
348
- markup.push(escapeMarkup(text));
349
- return;
350
- }
351
-
352
- markup.push(escapeMarkup(text.substring(0, match)));
353
- markup.push("<span class='select2-match'>");
354
- markup.push(escapeMarkup(text.substring(match, match + tl)));
355
- markup.push("</span>");
356
- markup.push(escapeMarkup(text.substring(match + tl, text.length)));
357
- }
358
-
359
- /**
360
- * Produces an ajax-based query function
361
- *
362
- * @param options object containing configuration paramters
363
- * @param options.params parameter map for the transport ajax call, can contain such options as cache, jsonpCallback, etc. see $.ajax
364
- * @param options.transport function that will be used to execute the ajax request. must be compatible with parameters supported by $.ajax
365
- * @param options.url url for the data
366
- * @param options.data a function(searchTerm, pageNumber, context) that should return an object containing query string parameters for the above url.
367
- * @param options.dataType request data type: ajax, jsonp, other datatatypes supported by jQuery's $.ajax function or the transport function if specified
368
- * @param options.quietMillis (optional) milliseconds to wait before making the ajaxRequest, helps debounce the ajax function if invoked too often
369
- * @param options.results a function(remoteData, pageNumber) that converts data returned form the remote request to the format expected by Select2.
370
- * The expected format is an object containing the following keys:
371
- * results array of objects that will be used as choices
372
- * more (optional) boolean indicating whether there are more results available
373
- * Example: {results:[{id:1, text:'Red'},{id:2, text:'Blue'}], more:true}
374
- */
375
- function ajax(options) {
376
- var timeout, // current scheduled but not yet executed request
377
- requestSequence = 0, // sequence used to drop out-of-order responses
378
- handler = null,
379
- quietMillis = options.quietMillis || 100,
380
- ajaxUrl = options.url,
381
- self = this;
382
-
383
- return function (query) {
384
- window.clearTimeout(timeout);
385
- timeout = window.setTimeout(function () {
386
- requestSequence += 1; // increment the sequence
387
- var requestNumber = requestSequence, // this request's sequence number
388
- data = options.data, // ajax data function
389
- url = ajaxUrl, // ajax url string or function
390
- transport = options.transport || $.fn.select2.ajaxDefaults.transport,
391
- // deprecated - to be removed in 4.0 - use params instead
392
- deprecated = {
393
- type: options.type || 'GET', // set type of request (GET or POST)
394
- cache: options.cache || false,
395
- jsonpCallback: options.jsonpCallback||undefined,
396
- dataType: options.dataType||"json"
397
- },
398
- params = $.extend({}, $.fn.select2.ajaxDefaults.params, deprecated);
399
-
400
- data = data ? data.call(self, query.term, query.page, query.context) : null;
401
- url = (typeof url === 'function') ? url.call(self, query.term, query.page, query.context) : url;
402
-
403
- if( null !== handler) { handler.abort(); }
404
-
405
- if (options.params) {
406
- if ($.isFunction(options.params)) {
407
- $.extend(params, options.params.call(self));
408
- } else {
409
- $.extend(params, options.params);
410
- }
411
- }
412
-
413
- $.extend(params, {
414
- url: url,
415
- dataType: options.dataType,
416
- data: data,
417
- success: function (data) {
418
- if (requestNumber < requestSequence) {
419
- return;
420
- }
421
- // TODO - replace query.page with query so users have access to term, page, etc.
422
- var results = options.results(data, query.page);
423
- query.callback(results);
424
- }
425
- });
426
- handler = transport.call(self, params);
427
- }, quietMillis);
428
- };
429
- }
430
-
431
- /**
432
- * Produces a query function that works with a local array
433
- *
434
- * @param options object containing configuration parameters. The options parameter can either be an array or an
435
- * object.
436
- *
437
- * If the array form is used it is assumed that it contains objects with 'id' and 'text' keys.
438
- *
439
- * If the object form is used ti is assumed that it contains 'data' and 'text' keys. The 'data' key should contain
440
- * an array of objects that will be used as choices. These objects must contain at least an 'id' key. The 'text'
441
- * key can either be a String in which case it is expected that each element in the 'data' array has a key with the
442
- * value of 'text' which will be used to match choices. Alternatively, text can be a function(item) that can extract
443
- * the text.
444
- */
445
- function local(options) {
446
- var data = options, // data elements
447
- dataText,
448
- tmp,
449
- text = function (item) { return ""+item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
450
-
451
- if ($.isArray(data)) {
452
- tmp = data;
453
- data = { results: tmp };
454
- }
455
-
456
- if ($.isFunction(data) === false) {
457
- tmp = data;
458
- data = function() { return tmp; };
459
- }
460
-
461
- var dataItem = data();
462
- if (dataItem.text) {
463
- text = dataItem.text;
464
- // if text is not a function we assume it to be a key name
465
- if (!$.isFunction(text)) {
466
- dataText = dataItem.text; // we need to store this in a separate variable because in the next step data gets reset and data.text is no longer available
467
- text = function (item) { return item[dataText]; };
468
- }
469
- }
470
-
471
- return function (query) {
472
- var t = query.term, filtered = { results: [] }, process;
473
- if (t === "") {
474
- query.callback(data());
475
- return;
476
- }
477
-
478
- process = function(datum, collection) {
479
- var group, attr;
480
- datum = datum[0];
481
- if (datum.children) {
482
- group = {};
483
- for (attr in datum) {
484
- if (datum.hasOwnProperty(attr)) group[attr]=datum[attr];
485
- }
486
- group.children=[];
487
- $(datum.children).each2(function(i, childDatum) { process(childDatum, group.children); });
488
- if (group.children.length || query.matcher(t, text(group), datum)) {
489
- collection.push(group);
490
- }
491
- } else {
492
- if (query.matcher(t, text(datum), datum)) {
493
- collection.push(datum);
494
- }
495
- }
496
- };
497
-
498
- $(data().results).each2(function(i, datum) { process(datum, filtered.results); });
499
- query.callback(filtered);
500
- };
501
- }
502
-
503
- // TODO javadoc
504
- function tags(data) {
505
- var isFunc = $.isFunction(data);
506
- return function (query) {
507
- var t = query.term, filtered = {results: []};
508
- $(isFunc ? data() : data).each(function () {
509
- var isObject = this.text !== undefined,
510
- text = isObject ? this.text : this;
511
- if (t === "" || query.matcher(t, text)) {
512
- filtered.results.push(isObject ? this : {id: this, text: this});
513
- }
514
- });
515
- query.callback(filtered);
516
- };
517
- }
518
-
519
- /**
520
- * Checks if the formatter function should be used.
521
- *
522
- * Throws an error if it is not a function. Returns true if it should be used,
523
- * false if no formatting should be performed.
524
- *
525
- * @param formatter
526
- */
527
- function checkFormatter(formatter, formatterName) {
528
- if ($.isFunction(formatter)) return true;
529
- if (!formatter) return false;
530
- throw new Error("formatterName must be a function or a falsy value");
531
- }
532
-
533
- function evaluate(val) {
534
- return $.isFunction(val) ? val() : val;
535
- }
536
-
537
- function countResults(results) {
538
- var count = 0;
539
- $.each(results, function(i, item) {
540
- if (item.children) {
541
- count += countResults(item.children);
542
- } else {
543
- count++;
544
- }
545
- });
546
- return count;
547
- }
548
-
549
- /**
550
- * Default tokenizer. This function uses breaks the input on substring match of any string from the
551
- * opts.tokenSeparators array and uses opts.createSearchChoice to create the choice object. Both of those
552
- * two options have to be defined in order for the tokenizer to work.
553
- *
554
- * @param input text user has typed so far or pasted into the search field
555
- * @param selection currently selected choices
556
- * @param selectCallback function(choice) callback tho add the choice to selection
557
- * @param opts select2's opts
558
- * @return undefined/null to leave the current input unchanged, or a string to change the input to the returned value
559
- */
560
- function defaultTokenizer(input, selection, selectCallback, opts) {
561
- var original = input, // store the original so we can compare and know if we need to tell the search to update its text
562
- dupe = false, // check for whether a token we extracted represents a duplicate selected choice
563
- token, // token
564
- index, // position at which the separator was found
565
- i, l, // looping variables
566
- separator; // the matched separator
567
-
568
- if (!opts.createSearchChoice || !opts.tokenSeparators || opts.tokenSeparators.length < 1) return undefined;
569
-
570
- while (true) {
571
- index = -1;
572
-
573
- for (i = 0, l = opts.tokenSeparators.length; i < l; i++) {
574
- separator = opts.tokenSeparators[i];
575
- index = input.indexOf(separator);
576
- if (index >= 0) break;
577
- }
578
-
579
- if (index < 0) break; // did not find any token separator in the input string, bail
580
-
581
- token = input.substring(0, index);
582
- input = input.substring(index + separator.length);
583
-
584
- if (token.length > 0) {
585
- token = opts.createSearchChoice(token, selection);
586
- if (token !== undefined && token !== null && opts.id(token) !== undefined && opts.id(token) !== null) {
587
- dupe = false;
588
- for (i = 0, l = selection.length; i < l; i++) {
589
- if (equal(opts.id(token), opts.id(selection[i]))) {
590
- dupe = true; break;
591
- }
592
- }
593
-
594
- if (!dupe) selectCallback(token);
595
- }
596
- }
597
- }
598
-
599
- if (original!==input) return input;
600
- }
601
-
602
- /**
603
- * Creates a new class
604
- *
605
- * @param superClass
606
- * @param methods
607
- */
608
- function clazz(SuperClass, methods) {
609
- var constructor = function () {};
610
- constructor.prototype = new SuperClass;
611
- constructor.prototype.constructor = constructor;
612
- constructor.prototype.parent = SuperClass.prototype;
613
- constructor.prototype = $.extend(constructor.prototype, methods);
614
- return constructor;
615
- }
616
-
617
- AbstractSelect2 = clazz(Object, {
618
-
619
- // abstract
620
- bind: function (func) {
621
- var self = this;
622
- return function () {
623
- func.apply(self, arguments);
624
- };
625
- },
626
-
627
- // abstract
628
- init: function (opts) {
629
- var results, search, resultsSelector = ".select2-results", disabled, readonly;
630
-
631
- // prepare options
632
- this.opts = opts = this.prepareOpts(opts);
633
-
634
- this.id=opts.id;
635
-
636
- // destroy if called on an existing component
637
- if (opts.element.data("select2") !== undefined &&
638
- opts.element.data("select2") !== null) {
639
- this.destroy();
640
- }
641
-
642
- this.container = this.createContainer();
643
-
644
- this.containerId="s2id_"+(opts.element.attr("id") || "autogen"+nextUid());
645
- this.containerSelector="#"+this.containerId.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
646
- this.container.attr("id", this.containerId);
647
-
648
- // cache the body so future lookups are cheap
649
- this.body = thunk(function() { return opts.element.closest("body"); });
650
-
651
- syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
652
-
653
- this.container.css(evaluate(opts.containerCss));
654
- this.container.addClass(evaluate(opts.containerCssClass));
655
-
656
- this.elementTabIndex = this.opts.element.attr("tabindex");
657
-
658
- // swap container for the element
659
- this.opts.element
660
- .data("select2", this)
661
- .attr("tabindex", "-1")
662
- .before(this.container);
663
- this.container.data("select2", this);
664
-
665
- this.dropdown = this.container.find(".select2-drop");
666
- this.dropdown.addClass(evaluate(opts.dropdownCssClass));
667
- this.dropdown.data("select2", this);
668
-
669
- this.results = results = this.container.find(resultsSelector);
670
- this.search = search = this.container.find("input.select2-input");
671
-
672
- this.resultsPage = 0;
673
- this.context = null;
674
-
675
- // initialize the container
676
- this.initContainer();
677
-
678
- installFilteredMouseMove(this.results);
679
- this.dropdown.on("mousemove-filtered touchstart touchmove touchend", resultsSelector, this.bind(this.highlightUnderEvent));
680
-
681
- installDebouncedScroll(80, this.results);
682
- this.dropdown.on("scroll-debounced", resultsSelector, this.bind(this.loadMoreIfNeeded));
683
-
684
- // do not propagate change event from the search field out of the component
685
- $(this.container).on("change", ".select2-input", function(e) {e.stopPropagation();});
686
- $(this.dropdown).on("change", ".select2-input", function(e) {e.stopPropagation();});
687
-
688
- // if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel
689
- if ($.fn.mousewheel) {
690
- results.mousewheel(function (e, delta, deltaX, deltaY) {
691
- var top = results.scrollTop(), height;
692
- if (deltaY > 0 && top - deltaY <= 0) {
693
- results.scrollTop(0);
694
- killEvent(e);
695
- } else if (deltaY < 0 && results.get(0).scrollHeight - results.scrollTop() + deltaY <= results.height()) {
696
- results.scrollTop(results.get(0).scrollHeight - results.height());
697
- killEvent(e);
698
- }
699
- });
700
- }
701
-
702
- installKeyUpChangeEvent(search);
703
- search.on("keyup-change input paste", this.bind(this.updateResults));
704
- search.on("focus", function () { search.addClass("select2-focused"); });
705
- search.on("blur", function () { search.removeClass("select2-focused");});
706
-
707
- this.dropdown.on("mouseup", resultsSelector, this.bind(function (e) {
708
- if ($(e.target).closest(".select2-result-selectable").length > 0) {
709
- this.highlightUnderEvent(e);
710
- this.selectHighlighted(e);
711
- }
712
- }));
713
-
714
- // trap all mouse events from leaving the dropdown. sometimes there may be a modal that is listening
715
- // for mouse events outside of itself so it can close itself. since the dropdown is now outside the select2's
716
- // dom it will trigger the popup close, which is not what we want
717
- this.dropdown.on("click mouseup mousedown", function (e) { e.stopPropagation(); });
718
-
719
- if ($.isFunction(this.opts.initSelection)) {
720
- // initialize selection based on the current value of the source element
721
- this.initSelection();
722
-
723
- // if the user has provided a function that can set selection based on the value of the source element
724
- // we monitor the change event on the element and trigger it, allowing for two way synchronization
725
- this.monitorSource();
726
- }
727
-
728
- if (opts.maximumInputLength !== null) {
729
- this.search.attr("maxlength", opts.maximumInputLength);
730
- }
731
-
732
- var disabled = opts.element.prop("disabled");
733
- if (disabled === undefined) disabled = false;
734
- this.enable(!disabled);
735
-
736
- var readonly = opts.element.prop("readonly");
737
- if (readonly === undefined) readonly = false;
738
- this.readonly(readonly);
739
-
740
- // Calculate size of scrollbar
741
- scrollBarDimensions = scrollBarDimensions || measureScrollbar();
742
-
743
- this.autofocus = opts.element.prop("autofocus")
744
- opts.element.prop("autofocus", false);
745
- if (this.autofocus) this.focus();
746
- },
747
-
748
- // abstract
749
- destroy: function () {
750
- var select2 = this.opts.element.data("select2");
751
-
752
- if (this.propertyObserver) { delete this.propertyObserver; this.propertyObserver = null; }
753
-
754
- if (select2 !== undefined) {
755
-
756
- select2.container.remove();
757
- select2.dropdown.remove();
758
- select2.opts.element
759
- .removeClass("select2-offscreen")
760
- .removeData("select2")
761
- .off(".select2")
762
- .attr({"tabindex": this.elementTabIndex})
763
- .prop("autofocus", this.autofocus||false)
764
- .show();
765
- }
766
- },
767
-
768
- // abstract
769
- optionToData: function(element) {
770
- if (element.is("option")) {
771
- return {
772
- id:element.prop("value"),
773
- text:element.text(),
774
- element: element.get(),
775
- css: element.attr("class"),
776
- disabled: element.prop("disabled"),
777
- locked: equal(element.attr("locked"), "locked")
778
- };
779
- } else if (element.is("optgroup")) {
780
- return {
781
- text:element.attr("label"),
782
- children:[],
783
- element: element.get(),
784
- css: element.attr("class")
785
- };
786
- }
787
- },
788
-
789
- // abstract
790
- prepareOpts: function (opts) {
791
- var element, select, idKey, ajaxUrl, self = this;
792
-
793
- element = opts.element;
794
-
795
- if (element.get(0).tagName.toLowerCase() === "select") {
796
- this.select = select = opts.element;
797
- }
798
-
799
- if (select) {
800
- // these options are not allowed when attached to a select because they are picked up off the element itself
801
- $.each(["id", "multiple", "ajax", "query", "createSearchChoice", "initSelection", "data", "tags"], function () {
802
- if (this in opts) {
803
- throw new Error("Option '" + this + "' is not allowed for Select2 when attached to a <select> element.");
804
- }
805
- });
806
- }
807
-
808
- opts = $.extend({}, {
809
- populateResults: function(container, results, query) {
810
- var populate, data, result, children, id=this.opts.id;
811
-
812
- populate=function(results, container, depth) {
813
-
814
- var i, l, result, selectable, disabled, compound, node, label, innerContainer, formatted;
815
-
816
- results = opts.sortResults(results, container, query);
817
-
818
- for (i = 0, l = results.length; i < l; i = i + 1) {
819
-
820
- result=results[i];
821
-
822
- disabled = (result.disabled === true);
823
- selectable = (!disabled) && (id(result) !== undefined);
824
-
825
- compound=result.children && result.children.length > 0;
826
-
827
- node=$("<li></li>");
828
- node.addClass("select2-results-dept-"+depth);
829
- node.addClass("select2-result");
830
- node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
831
- if (disabled) { node.addClass("select2-disabled"); }
832
- if (compound) { node.addClass("select2-result-with-children"); }
833
- node.addClass(self.opts.formatResultCssClass(result));
834
-
835
- label=$(document.createElement("div"));
836
- label.addClass("select2-result-label");
837
-
838
- formatted=opts.formatResult(result, label, query, self.opts.escapeMarkup);
839
- if (formatted!==undefined) {
840
- label.html(formatted);
841
- }
842
-
843
- node.append(label);
844
-
845
- if (compound) {
846
-
847
- innerContainer=$("<ul></ul>");
848
- innerContainer.addClass("select2-result-sub");
849
- populate(result.children, innerContainer, depth+1);
850
- node.append(innerContainer);
851
- }
852
-
853
- node.data("select2-data", result);
854
- container.append(node);
855
- }
856
- };
857
-
858
- populate(results, container, 0);
859
- }
860
- }, $.fn.select2.defaults, opts);
861
-
862
- if (typeof(opts.id) !== "function") {
863
- idKey = opts.id;
864
- opts.id = function (e) { return e[idKey]; };
865
- }
866
-
867
- if ($.isArray(opts.element.data("select2Tags"))) {
868
- if ("tags" in opts) {
869
- throw "tags specified as both an attribute 'data-select2-tags' and in options of Select2 " + opts.element.attr("id");
870
- }
871
- opts.tags=opts.element.data("select2Tags");
872
- }
873
-
874
- if (select) {
875
- opts.query = this.bind(function (query) {
876
- var data = { results: [], more: false },
877
- term = query.term,
878
- children, firstChild, process;
879
-
880
- process=function(element, collection) {
881
- var group;
882
- if (element.is("option")) {
883
- if (query.matcher(term, element.text(), element)) {
884
- collection.push(self.optionToData(element));
885
- }
886
- } else if (element.is("optgroup")) {
887
- group=self.optionToData(element);
888
- element.children().each2(function(i, elm) { process(elm, group.children); });
889
- if (group.children.length>0) {
890
- collection.push(group);
891
- }
892
- }
893
- };
894
-
895
- children=element.children();
896
-
897
- // ignore the placeholder option if there is one
898
- if (this.getPlaceholder() !== undefined && children.length > 0) {
899
- firstChild = children[0];
900
- if ($(firstChild).text() === "") {
901
- children=children.not(firstChild);
902
- }
903
- }
904
-
905
- children.each2(function(i, elm) { process(elm, data.results); });
906
-
907
- query.callback(data);
908
- });
909
- // this is needed because inside val() we construct choices from options and there id is hardcoded
910
- opts.id=function(e) { return e.id; };
911
- opts.formatResultCssClass = function(data) { return data.css; };
912
- } else {
913
- if (!("query" in opts)) {
914
-
915
- if ("ajax" in opts) {
916
- ajaxUrl = opts.element.data("ajax-url");
917
- if (ajaxUrl && ajaxUrl.length > 0) {
918
- opts.ajax.url = ajaxUrl;
919
- }
920
- opts.query = ajax.call(opts.element, opts.ajax);
921
- } else if ("data" in opts) {
922
- opts.query = local(opts.data);
923
- } else if ("tags" in opts) {
924
- opts.query = tags(opts.tags);
925
- if (opts.createSearchChoice === undefined) {
926
- opts.createSearchChoice = function (term) { return {id: term, text: term}; };
927
- }
928
- if (opts.initSelection === undefined) {
929
- opts.initSelection = function (element, callback) {
930
- var data = [];
931
- $(splitVal(element.val(), opts.separator)).each(function () {
932
- var id = this, text = this, tags=opts.tags;
933
- if ($.isFunction(tags)) tags=tags();
934
- $(tags).each(function() { if (equal(this.id, id)) { text = this.text; return false; } });
935
- data.push({id: id, text: text});
936
- });
937
-
938
- callback(data);
939
- };
940
- }
941
- }
942
- }
943
- }
944
- if (typeof(opts.query) !== "function") {
945
- throw "query function not defined for Select2 " + opts.element.attr("id");
946
- }
947
-
948
- return opts;
949
- },
950
-
951
- /**
952
- * Monitor the original element for changes and update select2 accordingly
953
- */
954
- // abstract
955
- monitorSource: function () {
956
- var el = this.opts.element, sync;
957
-
958
- el.on("change.select2", this.bind(function (e) {
959
- if (this.opts.element.data("select2-change-triggered") !== true) {
960
- this.initSelection();
961
- }
962
- }));
963
-
964
- sync = this.bind(function () {
965
-
966
- var enabled, readonly, self = this;
967
-
968
- // sync enabled state
969
-
970
- var disabled = el.prop("disabled");
971
- if (disabled === undefined) disabled = false;
972
- this.enable(!disabled);
973
-
974
- var readonly = el.prop("readonly");
975
- if (readonly === undefined) readonly = false;
976
- this.readonly(readonly);
977
-
978
- syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
979
- this.container.addClass(evaluate(this.opts.containerCssClass));
980
-
981
- syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
982
- this.dropdown.addClass(evaluate(this.opts.dropdownCssClass));
983
-
984
- });
985
-
986
- // mozilla and IE
987
- el.on("propertychange.select2 DOMAttrModified.select2", sync);
988
-
989
-
990
- // hold onto a reference of the callback to work around a chromium bug
991
- if (this.mutationCallback === undefined) {
992
- this.mutationCallback = function (mutations) {
993
- mutations.forEach(sync);
994
- }
995
- }
996
-
997
- // safari and chrome
998
- if (typeof WebKitMutationObserver !== "undefined") {
999
- if (this.propertyObserver) { delete this.propertyObserver; this.propertyObserver = null; }
1000
- this.propertyObserver = new WebKitMutationObserver(this.mutationCallback);
1001
- this.propertyObserver.observe(el.get(0), { attributes:true, subtree:false });
1002
- }
1003
- },
1004
-
1005
- // abstract
1006
- triggerSelect: function(data) {
1007
- var evt = $.Event("select2-selecting", { val: this.id(data), object: data });
1008
- this.opts.element.trigger(evt);
1009
- return !evt.isDefaultPrevented();
1010
- },
1011
-
1012
- /**
1013
- * Triggers the change event on the source element
1014
- */
1015
- // abstract
1016
- triggerChange: function (details) {
1017
-
1018
- details = details || {};
1019
- details= $.extend({}, details, { type: "change", val: this.val() });
1020
- // prevents recursive triggering
1021
- this.opts.element.data("select2-change-triggered", true);
1022
- this.opts.element.trigger(details);
1023
- this.opts.element.data("select2-change-triggered", false);
1024
-
1025
- // some validation frameworks ignore the change event and listen instead to keyup, click for selects
1026
- // so here we trigger the click event manually
1027
- this.opts.element.click();
1028
-
1029
- // ValidationEngine ignorea the change event and listens instead to blur
1030
- // so here we trigger the blur event manually if so desired
1031
- if (this.opts.blurOnChange)
1032
- this.opts.element.blur();
1033
- },
1034
-
1035
- //abstract
1036
- isInterfaceEnabled: function()
1037
- {
1038
- return this.enabledInterface === true;
1039
- },
1040
-
1041
- // abstract
1042
- enableInterface: function() {
1043
- var enabled = this._enabled && !this._readonly,
1044
- disabled = !enabled;
1045
-
1046
- if (enabled === this.enabledInterface) return false;
1047
-
1048
- this.container.toggleClass("select2-container-disabled", disabled);
1049
- this.close();
1050
- this.enabledInterface = enabled;
1051
-
1052
- return true;
1053
- },
1054
-
1055
- // abstract
1056
- enable: function(enabled) {
1057
- if (enabled === undefined) enabled = true;
1058
- if (this._enabled === enabled) return false;
1059
- this._enabled = enabled;
1060
-
1061
- this.opts.element.prop("disabled", !enabled);
1062
- this.enableInterface();
1063
- return true;
1064
- },
1065
-
1066
- // abstract
1067
- readonly: function(enabled) {
1068
- if (enabled === undefined) enabled = false;
1069
- if (this._readonly === enabled) return false;
1070
- this._readonly = enabled;
1071
-
1072
- this.opts.element.prop("readonly", enabled);
1073
- this.enableInterface();
1074
- return true;
1075
- },
1076
-
1077
- // abstract
1078
- opened: function () {
1079
- return this.container.hasClass("select2-dropdown-open");
1080
- },
1081
-
1082
- // abstract
1083
- positionDropdown: function() {
1084
- var $dropdown = this.dropdown,
1085
- offset = this.container.offset(),
1086
- height = this.container.outerHeight(false),
1087
- width = this.container.outerWidth(false),
1088
- dropHeight = $dropdown.outerHeight(false),
1089
- viewPortRight = $(window).scrollLeft() + $(window).width(),
1090
- viewportBottom = $(window).scrollTop() + $(window).height(),
1091
- dropTop = offset.top + height,
1092
- dropLeft = offset.left,
1093
- enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
1094
- enoughRoomAbove = (offset.top - dropHeight) >= this.body().scrollTop(),
1095
- dropWidth = $dropdown.outerWidth(false),
1096
- enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight,
1097
- aboveNow = $dropdown.hasClass("select2-drop-above"),
1098
- bodyOffset,
1099
- above,
1100
- css,
1101
- resultsListNode;
1102
-
1103
- if (this.opts.dropdownAutoWidth) {
1104
- resultsListNode = $('.select2-results', $dropdown)[0];
1105
- $dropdown.addClass('select2-drop-auto-width');
1106
- $dropdown.css('width', '');
1107
- // Add scrollbar width to dropdown if vertical scrollbar is present
1108
- dropWidth = $dropdown.outerWidth(false) + (resultsListNode.scrollHeight === resultsListNode.clientHeight ? 0 : scrollBarDimensions.width);
1109
- dropWidth > width ? width = dropWidth : dropWidth = width;
1110
- enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight;
1111
- }
1112
- else {
1113
- this.container.removeClass('select2-drop-auto-width');
1114
- }
1115
-
1116
- //console.log("below/ droptop:", dropTop, "dropHeight", dropHeight, "sum", (dropTop+dropHeight)+" viewport bottom", viewportBottom, "enough?", enoughRoomBelow);
1117
- //console.log("above/ offset.top", offset.top, "dropHeight", dropHeight, "top", (offset.top-dropHeight), "scrollTop", this.body().scrollTop(), "enough?", enoughRoomAbove);
1118
-
1119
- // fix positioning when body has an offset and is not position: static
1120
-
1121
- if (this.body().css('position') !== 'static') {
1122
- bodyOffset = this.body().offset();
1123
- dropTop -= bodyOffset.top;
1124
- dropLeft -= bodyOffset.left;
1125
- }
1126
-
1127
- // always prefer the current above/below alignment, unless there is not enough room
1128
-
1129
- if (aboveNow) {
1130
- above = true;
1131
- if (!enoughRoomAbove && enoughRoomBelow) above = false;
1132
- } else {
1133
- above = false;
1134
- if (!enoughRoomBelow && enoughRoomAbove) above = true;
1135
- }
1136
-
1137
- if (!enoughRoomOnRight) {
1138
- dropLeft = offset.left + width - dropWidth;
1139
- }
1140
-
1141
- if (above) {
1142
- dropTop = offset.top - dropHeight;
1143
- this.container.addClass("select2-drop-above");
1144
- $dropdown.addClass("select2-drop-above");
1145
- }
1146
- else {
1147
- this.container.removeClass("select2-drop-above");
1148
- $dropdown.removeClass("select2-drop-above");
1149
- }
1150
-
1151
- css = $.extend({
1152
- top: dropTop,
1153
- left: dropLeft,
1154
- width: width
1155
- }, evaluate(this.opts.dropdownCss));
1156
-
1157
- $dropdown.css(css);
1158
- },
1159
-
1160
- // abstract
1161
- shouldOpen: function() {
1162
- var event;
1163
-
1164
- if (this.opened()) return false;
1165
-
1166
- if (this._enabled === false || this._readonly === true) return false;
1167
-
1168
- event = $.Event("select2-opening");
1169
- this.opts.element.trigger(event);
1170
- return !event.isDefaultPrevented();
1171
- },
1172
-
1173
- // abstract
1174
- clearDropdownAlignmentPreference: function() {
1175
- // clear the classes used to figure out the preference of where the dropdown should be opened
1176
- this.container.removeClass("select2-drop-above");
1177
- this.dropdown.removeClass("select2-drop-above");
1178
- },
1179
-
1180
- /**
1181
- * Opens the dropdown
1182
- *
1183
- * @return {Boolean} whether or not dropdown was opened. This method will return false if, for example,
1184
- * the dropdown is already open, or if the 'open' event listener on the element called preventDefault().
1185
- */
1186
- // abstract
1187
- open: function () {
1188
-
1189
- if (!this.shouldOpen()) return false;
1190
-
1191
- this.opening();
1192
-
1193
- return true;
1194
- },
1195
-
1196
- /**
1197
- * Performs the opening of the dropdown
1198
- */
1199
- // abstract
1200
- opening: function() {
1201
- var cid = this.containerId,
1202
- scroll = "scroll." + cid,
1203
- resize = "resize."+cid,
1204
- orient = "orientationchange."+cid,
1205
- mask;
1206
-
1207
- this.container.addClass("select2-dropdown-open").addClass("select2-container-active");
1208
-
1209
- this.clearDropdownAlignmentPreference();
1210
-
1211
- if(this.dropdown[0] !== this.body().children().last()[0]) {
1212
- this.dropdown.detach().appendTo(this.body());
1213
- }
1214
-
1215
- // create the dropdown mask if doesnt already exist
1216
- mask = $("#select2-drop-mask");
1217
- if (mask.length == 0) {
1218
- mask = $(document.createElement("div"));
1219
- mask.attr("id","select2-drop-mask").attr("class","select2-drop-mask");
1220
- mask.hide();
1221
- mask.appendTo(this.body());
1222
- mask.on("mousedown touchstart", function (e) {
1223
- var dropdown = $("#select2-drop"), self;
1224
- if (dropdown.length > 0) {
1225
- self=dropdown.data("select2");
1226
- if (self.opts.selectOnBlur) {
1227
- self.selectHighlighted({noFocus: true});
1228
- }
1229
- self.close();
1230
- e.preventDefault();
1231
- e.stopPropagation();
1232
- }
1233
- });
1234
- }
1235
-
1236
- // ensure the mask is always right before the dropdown
1237
- if (this.dropdown.prev()[0] !== mask[0]) {
1238
- this.dropdown.before(mask);
1239
- }
1240
-
1241
- // move the global id to the correct dropdown
1242
- $("#select2-drop").removeAttr("id");
1243
- this.dropdown.attr("id", "select2-drop");
1244
-
1245
- // show the elements
1246
- mask.css(_makeMaskCss());
1247
- mask.show();
1248
- this.dropdown.show();
1249
- this.positionDropdown();
1250
-
1251
- this.dropdown.addClass("select2-drop-active");
1252
- this.ensureHighlightVisible();
1253
-
1254
- // attach listeners to events that can change the position of the container and thus require
1255
- // the position of the dropdown to be updated as well so it does not come unglued from the container
1256
- var that = this;
1257
- this.container.parents().add(window).each(function () {
1258
- $(this).on(resize+" "+scroll+" "+orient, function (e) {
1259
- $("#select2-drop-mask").css(_makeMaskCss());
1260
- that.positionDropdown();
1261
- });
1262
- });
1263
-
1264
- function _makeMaskCss() {
1265
- return {
1266
- width : Math.max(document.documentElement.scrollWidth, $(window).width()),
1267
- height : Math.max(document.documentElement.scrollHeight, $(window).height())
1268
- }
1269
- }
1270
- },
1271
-
1272
- // abstract
1273
- close: function () {
1274
- if (!this.opened()) return;
1275
-
1276
- var cid = this.containerId,
1277
- scroll = "scroll." + cid,
1278
- resize = "resize."+cid,
1279
- orient = "orientationchange."+cid;
1280
-
1281
- // unbind event listeners
1282
- this.container.parents().add(window).each(function () { $(this).off(scroll).off(resize).off(orient); });
1283
-
1284
- this.clearDropdownAlignmentPreference();
1285
-
1286
- $("#select2-drop-mask").hide();
1287
- this.dropdown.removeAttr("id"); // only the active dropdown has the select2-drop id
1288
- this.dropdown.hide();
1289
- this.container.removeClass("select2-dropdown-open");
1290
- this.results.empty();
1291
-
1292
-
1293
- this.clearSearch();
1294
- this.search.removeClass("select2-active");
1295
- this.opts.element.trigger($.Event("select2-close"));
1296
- },
1297
-
1298
- // abstract
1299
- clearSearch: function () {
1300
-
1301
- },
1302
-
1303
- //abstract
1304
- getMaximumSelectionSize: function() {
1305
- return evaluate(this.opts.maximumSelectionSize);
1306
- },
1307
-
1308
- // abstract
1309
- ensureHighlightVisible: function () {
1310
- var results = this.results, children, index, child, hb, rb, y, more;
1311
-
1312
- index = this.highlight();
1313
-
1314
- if (index < 0) return;
1315
-
1316
- if (index == 0) {
1317
-
1318
- // if the first element is highlighted scroll all the way to the top,
1319
- // that way any unselectable headers above it will also be scrolled
1320
- // into view
1321
-
1322
- results.scrollTop(0);
1323
- return;
1324
- }
1325
-
1326
- children = this.findHighlightableChoices().find('.select2-result-label');
1327
-
1328
- child = $(children[index]);
1329
-
1330
- hb = child.offset().top + child.outerHeight(true);
1331
-
1332
- // if this is the last child lets also make sure select2-more-results is visible
1333
- if (index === children.length - 1) {
1334
- more = results.find("li.select2-more-results");
1335
- if (more.length > 0) {
1336
- hb = more.offset().top + more.outerHeight(true);
1337
- }
1338
- }
1339
-
1340
- rb = results.offset().top + results.outerHeight(true);
1341
- if (hb > rb) {
1342
- results.scrollTop(results.scrollTop() + (hb - rb));
1343
- }
1344
- y = child.offset().top - results.offset().top;
1345
-
1346
- // make sure the top of the element is visible
1347
- if (y < 0 && child.css('display') != 'none' ) {
1348
- results.scrollTop(results.scrollTop() + y); // y is negative
1349
- }
1350
- },
1351
-
1352
- // abstract
1353
- findHighlightableChoices: function() {
1354
- return this.results.find(".select2-result-selectable:not(.select2-selected):not(.select2-disabled)");
1355
- },
1356
-
1357
- // abstract
1358
- moveHighlight: function (delta) {
1359
- var choices = this.findHighlightableChoices(),
1360
- index = this.highlight();
1361
-
1362
- while (index > -1 && index < choices.length) {
1363
- index += delta;
1364
- var choice = $(choices[index]);
1365
- if (choice.hasClass("select2-result-selectable") && !choice.hasClass("select2-disabled") && !choice.hasClass("select2-selected")) {
1366
- this.highlight(index);
1367
- break;
1368
- }
1369
- }
1370
- },
1371
-
1372
- // abstract
1373
- highlight: function (index) {
1374
- var choices = this.findHighlightableChoices(),
1375
- choice,
1376
- data;
1377
-
1378
- if (arguments.length === 0) {
1379
- return indexOf(choices.filter(".select2-highlighted")[0], choices.get());
1380
- }
1381
-
1382
- if (index >= choices.length) index = choices.length - 1;
1383
- if (index < 0) index = 0;
1384
-
1385
- this.results.find(".select2-highlighted").removeClass("select2-highlighted");
1386
-
1387
- choice = $(choices[index]);
1388
- choice.addClass("select2-highlighted");
1389
-
1390
- this.ensureHighlightVisible();
1391
-
1392
- data = choice.data("select2-data");
1393
- if (data) {
1394
- this.opts.element.trigger({ type: "select2-highlight", val: this.id(data), choice: data });
1395
- }
1396
- },
1397
-
1398
- // abstract
1399
- countSelectableResults: function() {
1400
- return this.findHighlightableChoices().length;
1401
- },
1402
-
1403
- // abstract
1404
- highlightUnderEvent: function (event) {
1405
- var el = $(event.target).closest(".select2-result-selectable");
1406
- if (el.length > 0 && !el.is(".select2-highlighted")) {
1407
- var choices = this.findHighlightableChoices();
1408
- this.highlight(choices.index(el));
1409
- } else if (el.length == 0) {
1410
- // if we are over an unselectable item remove al highlights
1411
- this.results.find(".select2-highlighted").removeClass("select2-highlighted");
1412
- }
1413
- },
1414
-
1415
- // abstract
1416
- loadMoreIfNeeded: function () {
1417
- var results = this.results,
1418
- more = results.find("li.select2-more-results"),
1419
- below, // pixels the element is below the scroll fold, below==0 is when the element is starting to be visible
1420
- offset = -1, // index of first element without data
1421
- page = this.resultsPage + 1,
1422
- self=this,
1423
- term=this.search.val(),
1424
- context=this.context;
1425
-
1426
- if (more.length === 0) return;
1427
- below = more.offset().top - results.offset().top - results.height();
1428
-
1429
- if (below <= this.opts.loadMorePadding) {
1430
- more.addClass("select2-active");
1431
- this.opts.query({
1432
- element: this.opts.element,
1433
- term: term,
1434
- page: page,
1435
- context: context,
1436
- matcher: this.opts.matcher,
1437
- callback: this.bind(function (data) {
1438
-
1439
- // ignore a response if the select2 has been closed before it was received
1440
- if (!self.opened()) return;
1441
-
1442
-
1443
- self.opts.populateResults.call(this, results, data.results, {term: term, page: page, context:context});
1444
- self.postprocessResults(data, false, false);
1445
-
1446
- if (data.more===true) {
1447
- more.detach().appendTo(results).text(self.opts.formatLoadMore(page+1));
1448
- window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
1449
- } else {
1450
- more.remove();
1451
- }
1452
- self.positionDropdown();
1453
- self.resultsPage = page;
1454
- self.context = data.context;
1455
- })});
1456
- }
1457
- },
1458
-
1459
- /**
1460
- * Default tokenizer function which does nothing
1461
- */
1462
- tokenize: function() {
1463
-
1464
- },
1465
-
1466
- /**
1467
- * @param initial whether or not this is the call to this method right after the dropdown has been opened
1468
- */
1469
- // abstract
1470
- updateResults: function (initial) {
1471
- var search = this.search,
1472
- results = this.results,
1473
- opts = this.opts,
1474
- data,
1475
- self = this,
1476
- input,
1477
- term = search.val(),
1478
- lastTerm=$.data(this.container, "select2-last-term");
1479
-
1480
- // prevent duplicate queries against the same term
1481
- if (initial !== true && lastTerm && equal(term, lastTerm)) return;
1482
-
1483
- $.data(this.container, "select2-last-term", term);
1484
-
1485
- // if the search is currently hidden we do not alter the results
1486
- if (initial !== true && (this.showSearchInput === false || !this.opened())) {
1487
- return;
1488
- }
1489
-
1490
- function postRender() {
1491
- results.scrollTop(0);
1492
- search.removeClass("select2-active");
1493
- self.positionDropdown();
1494
- }
1495
-
1496
- function render(html) {
1497
- results.html(html);
1498
- postRender();
1499
- }
1500
-
1501
- var maxSelSize = this.getMaximumSelectionSize();
1502
- if (maxSelSize >=1) {
1503
- data = this.data();
1504
- if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
1505
- render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(maxSelSize) + "</li>");
1506
- return;
1507
- }
1508
- }
1509
-
1510
- if (search.val().length < opts.minimumInputLength) {
1511
- if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
1512
- render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>");
1513
- } else {
1514
- render("");
1515
- }
1516
- if (initial && this.showSearch) this.showSearch(true);
1517
- return;
1518
- }
1519
-
1520
- if (opts.maximumInputLength && search.val().length > opts.maximumInputLength) {
1521
- if (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) {
1522
- render("<li class='select2-no-results'>" + opts.formatInputTooLong(search.val(), opts.maximumInputLength) + "</li>");
1523
- } else {
1524
- render("");
1525
- }
1526
- return;
1527
- }
1528
-
1529
- if (opts.formatSearching && this.findHighlightableChoices().length === 0) {
1530
- render("<li class='select2-searching'>" + opts.formatSearching() + "</li>");
1531
- }
1532
-
1533
- search.addClass("select2-active");
1534
-
1535
- // give the tokenizer a chance to pre-process the input
1536
- input = this.tokenize();
1537
- if (input != undefined && input != null) {
1538
- search.val(input);
1539
- }
1540
-
1541
- this.resultsPage = 1;
1542
-
1543
- opts.query({
1544
- element: opts.element,
1545
- term: search.val(),
1546
- page: this.resultsPage,
1547
- context: null,
1548
- matcher: opts.matcher,
1549
- callback: this.bind(function (data) {
1550
- var def; // default choice
1551
-
1552
- // ignore a response if the select2 has been closed before it was received
1553
- if (!this.opened()) {
1554
- this.search.removeClass("select2-active");
1555
- return;
1556
- }
1557
-
1558
- // save context, if any
1559
- this.context = (data.context===undefined) ? null : data.context;
1560
- // create a default choice and prepend it to the list
1561
- if (this.opts.createSearchChoice && search.val() !== "") {
1562
- def = this.opts.createSearchChoice.call(null, search.val(), data.results);
1563
- if (def !== undefined && def !== null && self.id(def) !== undefined && self.id(def) !== null) {
1564
- if ($(data.results).filter(
1565
- function () {
1566
- return equal(self.id(this), self.id(def));
1567
- }).length === 0) {
1568
- data.results.unshift(def);
1569
- }
1570
- }
1571
- }
1572
-
1573
- if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) {
1574
- render("<li class='select2-no-results'>" + opts.formatNoMatches(search.val()) + "</li>");
1575
- return;
1576
- }
1577
-
1578
- results.empty();
1579
- self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null});
1580
-
1581
- if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
1582
- results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>");
1583
- window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
1584
- }
1585
-
1586
- this.postprocessResults(data, initial);
1587
-
1588
- postRender();
1589
-
1590
- this.opts.element.trigger({ type: "select2-loaded", data:data });
1591
- })});
1592
- },
1593
-
1594
- // abstract
1595
- cancel: function () {
1596
- this.close();
1597
- },
1598
-
1599
- // abstract
1600
- blur: function () {
1601
- // if selectOnBlur == true, select the currently highlighted option
1602
- if (this.opts.selectOnBlur)
1603
- this.selectHighlighted({noFocus: true});
1604
-
1605
- this.close();
1606
- this.container.removeClass("select2-container-active");
1607
- // synonymous to .is(':focus'), which is available in jquery >= 1.6
1608
- if (this.search[0] === document.activeElement) { this.search.blur(); }
1609
- this.clearSearch();
1610
- this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
1611
- },
1612
-
1613
- // abstract
1614
- focusSearch: function () {
1615
- focus(this.search);
1616
- },
1617
-
1618
- // abstract
1619
- selectHighlighted: function (options) {
1620
- var index=this.highlight(),
1621
- highlighted=this.results.find(".select2-highlighted"),
1622
- data = highlighted.closest('.select2-result').data("select2-data");
1623
-
1624
- if (data) {
1625
- this.highlight(index);
1626
- this.onSelect(data, options);
1627
- }
1628
- },
1629
-
1630
- // abstract
1631
- getPlaceholder: function () {
1632
- return this.opts.element.attr("placeholder") ||
1633
- this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
1634
- this.opts.element.data("placeholder") ||
1635
- this.opts.placeholder;
1636
- },
1637
-
1638
- /**
1639
- * Get the desired width for the container element. This is
1640
- * derived first from option `width` passed to select2, then
1641
- * the inline 'style' on the original element, and finally
1642
- * falls back to the jQuery calculated element width.
1643
- */
1644
- // abstract
1645
- initContainerWidth: function () {
1646
- function resolveContainerWidth() {
1647
- var style, attrs, matches, i, l;
1648
-
1649
- if (this.opts.width === "off") {
1650
- return null;
1651
- } else if (this.opts.width === "element"){
1652
- return this.opts.element.outerWidth(false) === 0 ? 'auto' : this.opts.element.outerWidth(false) + 'px';
1653
- } else if (this.opts.width === "copy" || this.opts.width === "resolve") {
1654
- // check if there is inline style on the element that contains width
1655
- style = this.opts.element.attr('style');
1656
- if (style !== undefined) {
1657
- attrs = style.split(';');
1658
- for (i = 0, l = attrs.length; i < l; i = i + 1) {
1659
- matches = attrs[i].replace(/\s/g, '')
1660
- .match(/width:(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc))/i);
1661
- if (matches !== null && matches.length >= 1)
1662
- return matches[1];
1663
- }
1664
- }
1665
-
1666
- // next check if css('width') can resolve a width that is percent based, this is sometimes possible
1667
- // when attached to input type=hidden or elements hidden via css
1668
- style = this.opts.element.css('width');
1669
- if (style && style.length > 0) return style;
1670
-
1671
- if (this.opts.width === "resolve") {
1672
- // finally, fallback on the calculated width of the element
1673
- return (this.opts.element.outerWidth(false) === 0 ? 'auto' : this.opts.element.outerWidth(false) + 'px');
1674
- }
1675
-
1676
- return null;
1677
- } else if ($.isFunction(this.opts.width)) {
1678
- return this.opts.width();
1679
- } else {
1680
- return this.opts.width;
1681
- }
1682
- };
1683
-
1684
- var width = resolveContainerWidth.call(this);
1685
- if (width !== null) {
1686
- this.container.css("width", width);
1687
- }
1688
- }
1689
- });
1690
-
1691
- SingleSelect2 = clazz(AbstractSelect2, {
1692
-
1693
- // single
1694
-
1695
- createContainer: function () {
1696
- var container = $(document.createElement("div")).attr({
1697
- "class": "select2-container"
1698
- }).html([
1699
- "<a href='javascript:void(0)' onclick='return false;' class='select2-choice' tabindex='-1'>",
1700
- " <span>&nbsp;</span><abbr class='select2-search-choice-close'></abbr>",
1701
- " <div><b></b></div>" ,
1702
- "</a>",
1703
- "<input class='select2-focusser select2-offscreen' type='text'/>",
1704
- "<div class='select2-drop select2-display-none'>" ,
1705
- " <div class='select2-search'>" ,
1706
- " <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input'/>" ,
1707
- " </div>" ,
1708
- " <ul class='select2-results'>" ,
1709
- " </ul>" ,
1710
- "</div>"].join(""));
1711
- return container;
1712
- },
1713
-
1714
- // single
1715
- enableInterface: function() {
1716
- if (this.parent.enableInterface.apply(this, arguments)) {
1717
- this.focusser.prop("disabled", !this.isInterfaceEnabled());
1718
- }
1719
- },
1720
-
1721
- // single
1722
- opening: function () {
1723
- var el, range;
1724
- this.parent.opening.apply(this, arguments);
1725
- if (this.showSearchInput !== false) {
1726
- // IE appends focusser.val() at the end of field :/ so we manually insert it at the beginning using a range
1727
- // all other browsers handle this just fine
1728
-
1729
- this.search.val(this.focusser.val());
1730
- }
1731
- this.search.focus();
1732
- // in IE we have to move the cursor to the end after focussing, otherwise it will be at the beginning and
1733
- // new text will appear *before* focusser.val()
1734
- el = this.search.get(0);
1735
- if (el.createTextRange) {
1736
- range = el.createTextRange();
1737
- range.collapse(false);
1738
- range.select();
1739
- }
1740
-
1741
- this.focusser.prop("disabled", true).val("");
1742
- this.updateResults(true);
1743
- this.opts.element.trigger($.Event("select2-open"));
1744
- },
1745
-
1746
- // single
1747
- close: function () {
1748
- if (!this.opened()) return;
1749
- this.parent.close.apply(this, arguments);
1750
- this.focusser.removeAttr("disabled");
1751
- this.focusser.focus();
1752
- },
1753
-
1754
- // single
1755
- focus: function () {
1756
- if (this.opened()) {
1757
- this.close();
1758
- } else {
1759
- this.focusser.removeAttr("disabled");
1760
- this.focusser.focus();
1761
- }
1762
- },
1763
-
1764
- // single
1765
- isFocused: function () {
1766
- return this.container.hasClass("select2-container-active");
1767
- },
1768
-
1769
- // single
1770
- cancel: function () {
1771
- this.parent.cancel.apply(this, arguments);
1772
- this.focusser.removeAttr("disabled");
1773
- this.focusser.focus();
1774
- },
1775
-
1776
- // single
1777
- initContainer: function () {
1778
-
1779
- var selection,
1780
- container = this.container,
1781
- dropdown = this.dropdown;
1782
-
1783
- this.showSearch(false);
1784
-
1785
- this.selection = selection = container.find(".select2-choice");
1786
-
1787
- this.focusser = container.find(".select2-focusser");
1788
-
1789
- // rewrite labels from original element to focusser
1790
- this.focusser.attr("id", "s2id_autogen"+nextUid());
1791
-
1792
- $("label[for='" + this.opts.element.attr("id") + "']")
1793
- .attr('for', this.focusser.attr('id'));
1794
-
1795
- this.focusser.attr("tabindex", this.elementTabIndex);
1796
-
1797
- this.search.on("keydown", this.bind(function (e) {
1798
- if (!this.isInterfaceEnabled()) return;
1799
-
1800
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
1801
- // prevent the page from scrolling
1802
- killEvent(e);
1803
- return;
1804
- }
1805
-
1806
- switch (e.which) {
1807
- case KEY.UP:
1808
- case KEY.DOWN:
1809
- this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
1810
- killEvent(e);
1811
- return;
1812
- case KEY.ENTER:
1813
- this.selectHighlighted();
1814
- killEvent(e);
1815
- return;
1816
- case KEY.TAB:
1817
- this.selectHighlighted({noFocus: true});
1818
- return;
1819
- case KEY.ESC:
1820
- this.cancel(e);
1821
- killEvent(e);
1822
- return;
1823
- }
1824
- }));
1825
-
1826
- this.search.on("blur", this.bind(function(e) {
1827
- // a workaround for chrome to keep the search field focussed when the scroll bar is used to scroll the dropdown.
1828
- // without this the search field loses focus which is annoying
1829
- if (document.activeElement === this.body().get(0)) {
1830
- window.setTimeout(this.bind(function() {
1831
- this.search.focus();
1832
- }), 0);
1833
- }
1834
- }));
1835
-
1836
- this.focusser.on("keydown", this.bind(function (e) {
1837
- if (!this.isInterfaceEnabled()) return;
1838
-
1839
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
1840
- return;
1841
- }
1842
-
1843
- if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
1844
- killEvent(e);
1845
- return;
1846
- }
1847
-
1848
- if (e.which == KEY.DOWN || e.which == KEY.UP
1849
- || (e.which == KEY.ENTER && this.opts.openOnEnter)) {
1850
- this.open();
1851
- killEvent(e);
1852
- return;
1853
- }
1854
-
1855
- if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) {
1856
- if (this.opts.allowClear) {
1857
- this.clear();
1858
- }
1859
- killEvent(e);
1860
- return;
1861
- }
1862
- }));
1863
-
1864
-
1865
- installKeyUpChangeEvent(this.focusser);
1866
- this.focusser.on("keyup-change input", this.bind(function(e) {
1867
- e.stopPropagation();
1868
- if (this.opened()) return;
1869
- this.open();
1870
- }));
1871
-
1872
- selection.on("mousedown", "abbr", this.bind(function (e) {
1873
- if (!this.isInterfaceEnabled()) return;
1874
- this.clear();
1875
- killEventImmediately(e);
1876
- this.close();
1877
- this.selection.focus();
1878
- }));
1879
-
1880
- selection.on("mousedown", this.bind(function (e) {
1881
-
1882
- if (!this.container.hasClass("select2-container-active")) {
1883
- this.opts.element.trigger($.Event("select2-focus"));
1884
- }
1885
-
1886
- if (this.opened()) {
1887
- this.close();
1888
- } else if (this.isInterfaceEnabled()) {
1889
- this.open();
1890
- }
1891
-
1892
- killEvent(e);
1893
- }));
1894
-
1895
- dropdown.on("mousedown", this.bind(function() { this.search.focus(); }));
1896
-
1897
- selection.on("focus", this.bind(function(e) {
1898
- killEvent(e);
1899
- }));
1900
-
1901
- this.focusser.on("focus", this.bind(function(){
1902
- if (!this.container.hasClass("select2-container-active")) {
1903
- this.opts.element.trigger($.Event("select2-focus"));
1904
- }
1905
- this.container.addClass("select2-container-active");
1906
- })).on("blur", this.bind(function() {
1907
- if (!this.opened()) {
1908
- this.container.removeClass("select2-container-active");
1909
- this.opts.element.trigger($.Event("select2-blur"));
1910
- }
1911
- }));
1912
- this.search.on("focus", this.bind(function(){
1913
- if (!this.container.hasClass("select2-container-active")) {
1914
- this.opts.element.trigger($.Event("select2-focus"));
1915
- }
1916
- this.container.addClass("select2-container-active");
1917
- }));
1918
-
1919
- this.initContainerWidth();
1920
- this.opts.element.addClass("select2-offscreen");
1921
- this.setPlaceholder();
1922
-
1923
- },
1924
-
1925
- // single
1926
- clear: function(triggerChange) {
1927
- var data=this.selection.data("select2-data");
1928
- if (data) { // guard against queued quick consecutive clicks
1929
- this.opts.element.val("");
1930
- this.selection.find("span").empty();
1931
- this.selection.removeData("select2-data");
1932
- this.setPlaceholder();
1933
-
1934
- if (triggerChange !== false){
1935
- this.opts.element.trigger({ type: "select2-removed", val: this.id(data), choice: data });
1936
- this.triggerChange({removed:data});
1937
- }
1938
- }
1939
- },
1940
-
1941
- /**
1942
- * Sets selection based on source element's value
1943
- */
1944
- // single
1945
- initSelection: function () {
1946
- var selected;
1947
- if (this.opts.element.val() === "" && this.opts.element.text() === "") {
1948
- this.updateSelection([]);
1949
- this.close();
1950
- this.setPlaceholder();
1951
- } else {
1952
- var self = this;
1953
- this.opts.initSelection.call(null, this.opts.element, function(selected){
1954
- if (selected !== undefined && selected !== null) {
1955
- self.updateSelection(selected);
1956
- self.close();
1957
- self.setPlaceholder();
1958
- }
1959
- });
1960
- }
1961
- },
1962
-
1963
- // single
1964
- prepareOpts: function () {
1965
- var opts = this.parent.prepareOpts.apply(this, arguments),
1966
- self=this;
1967
-
1968
- if (opts.element.get(0).tagName.toLowerCase() === "select") {
1969
- // install the selection initializer
1970
- opts.initSelection = function (element, callback) {
1971
- var selected = element.find(":selected");
1972
- // a single select box always has a value, no need to null check 'selected'
1973
- callback(self.optionToData(selected));
1974
- };
1975
- } else if ("data" in opts) {
1976
- // install default initSelection when applied to hidden input and data is local
1977
- opts.initSelection = opts.initSelection || function (element, callback) {
1978
- var id = element.val();
1979
- //search in data by id, storing the actual matching item
1980
- var match = null;
1981
- opts.query({
1982
- matcher: function(term, text, el){
1983
- var is_match = equal(id, opts.id(el));
1984
- if (is_match) {
1985
- match = el;
1986
- }
1987
- return is_match;
1988
- },
1989
- callback: !$.isFunction(callback) ? $.noop : function() {
1990
- callback(match);
1991
- }
1992
- });
1993
- };
1994
- }
1995
-
1996
- return opts;
1997
- },
1998
-
1999
- // single
2000
- getPlaceholder: function() {
2001
- // if a placeholder is specified on a single select without the first empty option ignore it
2002
- if (this.select) {
2003
- if (this.select.find("option").first().text() !== "") {
2004
- return undefined;
2005
- }
2006
- }
2007
-
2008
- return this.parent.getPlaceholder.apply(this, arguments);
2009
- },
2010
-
2011
- // single
2012
- setPlaceholder: function () {
2013
- var placeholder = this.getPlaceholder();
2014
-
2015
- if (this.opts.element.val() === "" && placeholder !== undefined) {
2016
-
2017
- // check for a first blank option if attached to a select
2018
- if (this.select && this.select.find("option:first").text() !== "") return;
2019
-
2020
- this.selection.find("span").html(this.opts.escapeMarkup(placeholder));
2021
-
2022
- this.selection.addClass("select2-default");
2023
-
2024
- this.container.removeClass("select2-allowclear");
2025
- }
2026
- },
2027
-
2028
- // single
2029
- postprocessResults: function (data, initial, noHighlightUpdate) {
2030
- var selected = 0, self = this, showSearchInput = true;
2031
-
2032
- // find the selected element in the result list
2033
-
2034
- this.findHighlightableChoices().each2(function (i, elm) {
2035
- if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
2036
- selected = i;
2037
- return false;
2038
- }
2039
- });
2040
-
2041
- // and highlight it
2042
- if (noHighlightUpdate !== false) {
2043
- this.highlight(selected);
2044
- }
2045
-
2046
- // show the search box if this is the first we got the results and there are enough of them for search
2047
-
2048
- if (initial === true && this.showSearchInput === false) {
2049
- var min=this.opts.minimumResultsForSearch;
2050
- if (min>=0) {
2051
- this.showSearch(countResults(data.results)>=min);
2052
- }
2053
- }
2054
-
2055
- },
2056
-
2057
- // single
2058
- showSearch: function(showSearchInput) {
2059
- this.showSearchInput = showSearchInput;
2060
-
2061
- this.dropdown.find(".select2-search").toggleClass("select2-search-hidden", !showSearchInput);
2062
- this.dropdown.find(".select2-search").toggleClass("select2-offscreen", !showSearchInput);
2063
- //add "select2-with-searchbox" to the container if search box is shown
2064
- $(this.dropdown, this.container).toggleClass("select2-with-searchbox", showSearchInput);
2065
- },
2066
-
2067
- // single
2068
- onSelect: function (data, options) {
2069
-
2070
- if (!this.triggerSelect(data)) { return; }
2071
-
2072
- var old = this.opts.element.val(),
2073
- oldData = this.data();
2074
-
2075
- this.opts.element.val(this.id(data));
2076
- this.updateSelection(data);
2077
-
2078
- this.opts.element.trigger({ type: "select2-selected", val: this.id(data), choice: data });
2079
-
2080
- this.close();
2081
-
2082
- if (!options || !options.noFocus)
2083
- this.selection.focus();
2084
-
2085
- if (!equal(old, this.id(data))) { this.triggerChange({added:data,removed:oldData}); }
2086
- },
2087
-
2088
- // single
2089
- updateSelection: function (data) {
2090
-
2091
- var container=this.selection.find("span"), formatted, cssClass;
2092
-
2093
- this.selection.data("select2-data", data);
2094
-
2095
- container.empty();
2096
- formatted=this.opts.formatSelection(data, container);
2097
- if (formatted !== undefined) {
2098
- container.append(this.opts.escapeMarkup(formatted));
2099
- }
2100
- cssClass=this.opts.formatSelectionCssClass(data, container);
2101
- if (cssClass !== undefined) {
2102
- container.addClass(cssClass);
2103
- }
2104
-
2105
- this.selection.removeClass("select2-default");
2106
-
2107
- if (this.opts.allowClear && this.getPlaceholder() !== undefined) {
2108
- this.container.addClass("select2-allowclear");
2109
- }
2110
- },
2111
-
2112
- // single
2113
- val: function () {
2114
- var val,
2115
- triggerChange = false,
2116
- data = null,
2117
- self = this,
2118
- oldData = this.data();
2119
-
2120
- if (arguments.length === 0) {
2121
- return this.opts.element.val();
2122
- }
2123
-
2124
- val = arguments[0];
2125
-
2126
- if (arguments.length > 1) {
2127
- triggerChange = arguments[1];
2128
- }
2129
-
2130
- if (this.select) {
2131
- this.select
2132
- .val(val)
2133
- .find(":selected").each2(function (i, elm) {
2134
- data = self.optionToData(elm);
2135
- return false;
2136
- });
2137
- this.updateSelection(data);
2138
- this.setPlaceholder();
2139
- if (triggerChange) {
2140
- this.triggerChange({added: data, removed:oldData});
2141
- }
2142
- } else {
2143
- if (this.opts.initSelection === undefined) {
2144
- throw new Error("cannot call val() if initSelection() is not defined");
2145
- }
2146
- // val is an id. !val is true for [undefined,null,'',0] - 0 is legal
2147
- if (!val && val !== 0) {
2148
- this.clear(triggerChange);
2149
- return;
2150
- }
2151
- this.opts.element.val(val);
2152
- this.opts.initSelection(this.opts.element, function(data){
2153
- self.opts.element.val(!data ? "" : self.id(data));
2154
- self.updateSelection(data);
2155
- self.setPlaceholder();
2156
- if (triggerChange) {
2157
- self.triggerChange({added: data, removed:oldData});
2158
- }
2159
- });
2160
- }
2161
- },
2162
-
2163
- // single
2164
- clearSearch: function () {
2165
- this.search.val("");
2166
- this.focusser.val("");
2167
- },
2168
-
2169
- // single
2170
- data: function(value, triggerChange) {
2171
- var data;
2172
-
2173
- if (arguments.length === 0) {
2174
- data = this.selection.data("select2-data");
2175
- if (data == undefined) data = null;
2176
- return data;
2177
- } else {
2178
- if (!value || value === "") {
2179
- this.clear(triggerChange);
2180
- } else {
2181
- data = this.data();
2182
- this.opts.element.val(!value ? "" : this.id(value));
2183
- this.updateSelection(value);
2184
- if (triggerChange) {
2185
- this.triggerChange({added: value, removed:data});
2186
- }
2187
- }
2188
- }
2189
- }
2190
- });
2191
-
2192
- MultiSelect2 = clazz(AbstractSelect2, {
2193
-
2194
- // multi
2195
- createContainer: function () {
2196
- var container = $(document.createElement("div")).attr({
2197
- "class": "select2-container select2-container-multi"
2198
- }).html([
2199
- " <ul class='select2-choices'>",
2200
- //"<li class='select2-search-choice'><span>California</span><a href="javascript:void(0)" class="select2-search-choice-close"></a></li>" ,
2201
- " <li class='select2-search-field'>" ,
2202
- " <input type='text' autocomplete='off' autocorrect='off' autocapitilize='off' spellcheck='false' class='select2-input'>" ,
2203
- " </li>" ,
2204
- "</ul>" ,
2205
- "<div class='select2-drop select2-drop-multi select2-display-none'>" ,
2206
- " <ul class='select2-results'>" ,
2207
- " </ul>" ,
2208
- "</div>"].join(""));
2209
- return container;
2210
- },
2211
-
2212
- // multi
2213
- prepareOpts: function () {
2214
- var opts = this.parent.prepareOpts.apply(this, arguments),
2215
- self=this;
2216
-
2217
- // TODO validate placeholder is a string if specified
2218
-
2219
- if (opts.element.get(0).tagName.toLowerCase() === "select") {
2220
- // install sthe selection initializer
2221
- opts.initSelection = function (element, callback) {
2222
-
2223
- var data = [];
2224
-
2225
- element.find(":selected").each2(function (i, elm) {
2226
- data.push(self.optionToData(elm));
2227
- });
2228
- callback(data);
2229
- };
2230
- } else if ("data" in opts) {
2231
- // install default initSelection when applied to hidden input and data is local
2232
- opts.initSelection = opts.initSelection || function (element, callback) {
2233
- var ids = splitVal(element.val(), opts.separator);
2234
- //search in data by array of ids, storing matching items in a list
2235
- var matches = [];
2236
- opts.query({
2237
- matcher: function(term, text, el){
2238
- var is_match = $.grep(ids, function(id) {
2239
- return equal(id, opts.id(el));
2240
- }).length;
2241
- if (is_match) {
2242
- matches.push(el);
2243
- }
2244
- return is_match;
2245
- },
2246
- callback: !$.isFunction(callback) ? $.noop : function() {
2247
- // reorder matches based on the order they appear in the ids array because right now
2248
- // they are in the order in which they appear in data array
2249
- var ordered = [];
2250
- for (var i = 0; i < ids.length; i++) {
2251
- var id = ids[i];
2252
- for (var j = 0; j < matches.length; j++) {
2253
- var match = matches[j];
2254
- if (equal(id, opts.id(match))) {
2255
- ordered.push(match);
2256
- matches.splice(j, 1);
2257
- break;
2258
- }
2259
- }
2260
- }
2261
- callback(ordered);
2262
- }
2263
- });
2264
- };
2265
- }
2266
-
2267
- return opts;
2268
- },
2269
-
2270
- selectChoice: function (choice) {
2271
-
2272
- var selected = this.container.find(".select2-search-choice-focus");
2273
- if (selected.length && choice && choice[0] == selected[0]) {
2274
-
2275
- } else {
2276
- if (selected.length) {
2277
- this.opts.element.trigger("choice-deselected", selected);
2278
- }
2279
- selected.removeClass("select2-search-choice-focus");
2280
- if (choice && choice.length) {
2281
- this.close();
2282
- choice.addClass("select2-search-choice-focus");
2283
- this.opts.element.trigger("choice-selected", choice);
2284
- }
2285
- }
2286
- },
2287
-
2288
- // multi
2289
- initContainer: function () {
2290
-
2291
- var selector = ".select2-choices", selection;
2292
-
2293
- this.searchContainer = this.container.find(".select2-search-field");
2294
- this.selection = selection = this.container.find(selector);
2295
-
2296
- var _this = this;
2297
- this.selection.on("mousedown", ".select2-search-choice", function (e) {
2298
- //killEvent(e);
2299
- _this.search[0].focus();
2300
- _this.selectChoice($(this));
2301
- })
2302
- //.sortable({
2303
- // items: " > li",
2304
- // tolerance: "pointer",
2305
- // revert: 100
2306
- //});
2307
-
2308
- // rewrite labels from original element to focusser
2309
- this.search.attr("id", "s2id_autogen"+nextUid());
2310
- $("label[for='" + this.opts.element.attr("id") + "']")
2311
- .attr('for', this.search.attr('id'));
2312
-
2313
- this.search.on("input paste", this.bind(function() {
2314
- if (!this.isInterfaceEnabled()) return;
2315
- if (!this.opened()) {
2316
- this.open();
2317
- }
2318
- }));
2319
-
2320
- this.search.attr("tabindex", this.elementTabIndex);
2321
-
2322
- this.keydowns = 0;
2323
- this.search.on("keydown", this.bind(function (e) {
2324
- if (!this.isInterfaceEnabled()) return;
2325
-
2326
- ++this.keydowns;
2327
- var selected = selection.find(".select2-search-choice-focus");
2328
- var prev = selected.prev(".select2-search-choice:not(.select2-locked)");
2329
- var next = selected.next(".select2-search-choice:not(.select2-locked)");
2330
- var pos = getCursorInfo(this.search);
2331
-
2332
- if (selected.length &&
2333
- (e.which == KEY.LEFT || e.which == KEY.RIGHT || e.which == KEY.BACKSPACE || e.which == KEY.DELETE || e.which == KEY.ENTER)) {
2334
- var selectedChoice = selected;
2335
- if (e.which == KEY.LEFT && prev.length) {
2336
- selectedChoice = prev;
2337
- }
2338
- else if (e.which == KEY.RIGHT) {
2339
- selectedChoice = next.length ? next : null;
2340
- }
2341
- else if (e.which === KEY.BACKSPACE) {
2342
- this.unselect(selected.first());
2343
- this.search.width(10);
2344
- selectedChoice = prev.length ? prev : next;
2345
- } else if (e.which == KEY.DELETE) {
2346
- this.unselect(selected.first());
2347
- this.search.width(10);
2348
- selectedChoice = next.length ? next : null;
2349
- } else if (e.which == KEY.ENTER) {
2350
- selectedChoice = null;
2351
- }
2352
-
2353
- this.selectChoice(selectedChoice);
2354
- killEvent(e);
2355
- if (!selectedChoice || !selectedChoice.length) {
2356
- this.open();
2357
- }
2358
- return;
2359
- } else if (((e.which === KEY.BACKSPACE && this.keydowns == 1)
2360
- || e.which == KEY.LEFT) && (pos.offset == 0 && !pos.length)) {
2361
-
2362
- this.selectChoice(selection.find(".select2-search-choice:not(.select2-locked)").last());
2363
- killEvent(e);
2364
- return;
2365
- } else {
2366
- this.selectChoice(null);
2367
- }
2368
-
2369
- if (this.opened()) {
2370
- switch (e.which) {
2371
- case KEY.UP:
2372
- case KEY.DOWN:
2373
- this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
2374
- killEvent(e);
2375
- return;
2376
- case KEY.ENTER:
2377
- this.selectHighlighted();
2378
- killEvent(e);
2379
- return;
2380
- case KEY.TAB:
2381
- this.selectHighlighted({noFocus:true});
2382
- return;
2383
- case KEY.ESC:
2384
- this.cancel(e);
2385
- killEvent(e);
2386
- return;
2387
- }
2388
- }
2389
-
2390
- if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e)
2391
- || e.which === KEY.BACKSPACE || e.which === KEY.ESC) {
2392
- return;
2393
- }
2394
-
2395
- if (e.which === KEY.ENTER) {
2396
- if (this.opts.openOnEnter === false) {
2397
- return;
2398
- } else if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
2399
- return;
2400
- }
2401
- }
2402
-
2403
- this.open();
2404
-
2405
- if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
2406
- // prevent the page from scrolling
2407
- killEvent(e);
2408
- }
2409
-
2410
- if (e.which === KEY.ENTER) {
2411
- // prevent form from being submitted
2412
- killEvent(e);
2413
- }
2414
-
2415
- }));
2416
-
2417
- this.search.on("keyup", this.bind(function (e) {
2418
- this.keydowns = 0;
2419
- this.resizeSearch();
2420
- })
2421
- );
2422
-
2423
- this.search.on("blur", this.bind(function(e) {
2424
- this.container.removeClass("select2-container-active");
2425
- this.search.removeClass("select2-focused");
2426
- this.selectChoice(null);
2427
- if (!this.opened()) this.clearSearch();
2428
- e.stopImmediatePropagation();
2429
- this.opts.element.trigger($.Event("select2-blur"));
2430
- }));
2431
-
2432
- this.container.on("mousedown", selector, this.bind(function (e) {
2433
- if (!this.isInterfaceEnabled()) return;
2434
- if ($(e.target).closest(".select2-search-choice").length > 0) {
2435
- // clicked inside a select2 search choice, do not open
2436
- return;
2437
- }
2438
- this.selectChoice(null);
2439
- this.clearPlaceholder();
2440
- if (!this.container.hasClass("select2-container-active")) {
2441
- this.opts.element.trigger($.Event("select2-focus"));
2442
- }
2443
- this.open();
2444
- this.focusSearch();
2445
- e.preventDefault();
2446
- }));
2447
-
2448
- this.container.on("focus", selector, this.bind(function () {
2449
- if (!this.isInterfaceEnabled()) return;
2450
- if (!this.container.hasClass("select2-container-active")) {
2451
- this.opts.element.trigger($.Event("select2-focus"));
2452
- }
2453
- this.container.addClass("select2-container-active");
2454
- this.dropdown.addClass("select2-drop-active");
2455
- this.clearPlaceholder();
2456
- }));
2457
-
2458
- this.initContainerWidth();
2459
- this.opts.element.addClass("select2-offscreen");
2460
-
2461
- // set the placeholder if necessary
2462
- this.clearSearch();
2463
- },
2464
-
2465
- // multi
2466
- enableInterface: function() {
2467
- if (this.parent.enableInterface.apply(this, arguments)) {
2468
- this.search.prop("disabled", !this.isInterfaceEnabled());
2469
- }
2470
- },
2471
-
2472
- // multi
2473
- initSelection: function () {
2474
- var data;
2475
- if (this.opts.element.val() === "" && this.opts.element.text() === "") {
2476
- this.updateSelection([]);
2477
- this.close();
2478
- // set the placeholder if necessary
2479
- this.clearSearch();
2480
- }
2481
- if (this.select || this.opts.element.val() !== "") {
2482
- var self = this;
2483
- this.opts.initSelection.call(null, this.opts.element, function(data){
2484
- if (data !== undefined && data !== null) {
2485
- self.updateSelection(data);
2486
- self.close();
2487
- // set the placeholder if necessary
2488
- self.clearSearch();
2489
- }
2490
- });
2491
- }
2492
- },
2493
-
2494
- // multi
2495
- clearSearch: function () {
2496
- var placeholder = this.getPlaceholder(),
2497
- maxWidth = this.getMaxSearchWidth();
2498
-
2499
- if (placeholder !== undefined && this.getVal().length === 0 && this.search.hasClass("select2-focused") === false) {
2500
- this.search.val(placeholder).addClass("select2-default");
2501
- // stretch the search box to full width of the container so as much of the placeholder is visible as possible
2502
- // we could call this.resizeSearch(), but we do not because that requires a sizer and we do not want to create one so early because of a firefox bug, see #944
2503
- this.search.width(maxWidth > 0 ? maxWidth : this.container.css("width"));
2504
- } else {
2505
- this.search.val("").width(10);
2506
- }
2507
- },
2508
-
2509
- // multi
2510
- clearPlaceholder: function () {
2511
- if (this.search.hasClass("select2-default")) {
2512
- this.search.val("").removeClass("select2-default");
2513
- }
2514
- },
2515
-
2516
- // multi
2517
- opening: function () {
2518
- this.clearPlaceholder(); // should be done before super so placeholder is not used to search
2519
- this.resizeSearch();
2520
-
2521
- this.parent.opening.apply(this, arguments);
2522
-
2523
- this.focusSearch();
2524
-
2525
- this.updateResults(true);
2526
- this.search.focus();
2527
- this.opts.element.trigger($.Event("select2-open"));
2528
- },
2529
-
2530
- // multi
2531
- close: function () {
2532
- if (!this.opened()) return;
2533
- this.parent.close.apply(this, arguments);
2534
- },
2535
-
2536
- // multi
2537
- focus: function () {
2538
- this.close();
2539
- this.search.focus();
2540
- //this.opts.element.triggerHandler("focus");
2541
- },
2542
-
2543
- // multi
2544
- isFocused: function () {
2545
- return this.search.hasClass("select2-focused");
2546
- },
2547
-
2548
- // multi
2549
- updateSelection: function (data) {
2550
- var ids = [], filtered = [], self = this;
2551
-
2552
- // filter out duplicates
2553
- $(data).each(function () {
2554
- if (indexOf(self.id(this), ids) < 0) {
2555
- ids.push(self.id(this));
2556
- filtered.push(this);
2557
- }
2558
- });
2559
- data = filtered;
2560
-
2561
- this.selection.find(".select2-search-choice").remove();
2562
- $(data).each(function () {
2563
- self.addSelectedChoice(this);
2564
- });
2565
- self.postprocessResults();
2566
- },
2567
-
2568
- // multi
2569
- tokenize: function() {
2570
- var input = this.search.val();
2571
- input = this.opts.tokenizer(input, this.data(), this.bind(this.onSelect), this.opts);
2572
- if (input != null && input != undefined) {
2573
- this.search.val(input);
2574
- if (input.length > 0) {
2575
- this.open();
2576
- }
2577
- }
2578
-
2579
- },
2580
-
2581
- // multi
2582
- onSelect: function (data, options) {
2583
-
2584
- if (!this.triggerSelect(data)) { return; }
2585
-
2586
- this.addSelectedChoice(data);
2587
-
2588
- this.opts.element.trigger({ type: "selected", val: this.id(data), choice: data });
2589
-
2590
- if (this.select || !this.opts.closeOnSelect) this.postprocessResults();
2591
-
2592
- if (this.opts.closeOnSelect) {
2593
- this.close();
2594
- this.search.width(10);
2595
- } else {
2596
- if (this.countSelectableResults()>0) {
2597
- this.search.width(10);
2598
- this.resizeSearch();
2599
- if (this.getMaximumSelectionSize() > 0 && this.val().length >= this.getMaximumSelectionSize()) {
2600
- // if we reached max selection size repaint the results so choices
2601
- // are replaced with the max selection reached message
2602
- this.updateResults(true);
2603
- }
2604
- this.positionDropdown();
2605
- } else {
2606
- // if nothing left to select close
2607
- this.close();
2608
- this.search.width(10);
2609
- }
2610
- }
2611
-
2612
- // since its not possible to select an element that has already been
2613
- // added we do not need to check if this is a new element before firing change
2614
- this.triggerChange({ added: data });
2615
-
2616
- if (!options || !options.noFocus)
2617
- this.focusSearch();
2618
- },
2619
-
2620
- // multi
2621
- cancel: function () {
2622
- this.close();
2623
- this.focusSearch();
2624
- },
2625
-
2626
- addSelectedChoice: function (data) {
2627
- var enableChoice = !data.locked,
2628
- enabledItem = $(
2629
- "<li class='select2-search-choice'>" +
2630
- " <div></div>" +
2631
- " <a href='#' onclick='return false;' class='select2-search-choice-close' tabindex='-1'></a>" +
2632
- "</li>"),
2633
- disabledItem = $(
2634
- "<li class='select2-search-choice select2-locked'>" +
2635
- "<div></div>" +
2636
- "</li>");
2637
- var choice = enableChoice ? enabledItem : disabledItem,
2638
- id = this.id(data),
2639
- val = this.getVal(),
2640
- formatted,
2641
- cssClass;
2642
-
2643
- formatted=this.opts.formatSelection(data, choice.find("div"));
2644
- if (formatted != undefined) {
2645
- choice.find("div").replaceWith("<div title='"+this.opts.escapeMarkup(formatted)+"'>"+this.opts.escapeMarkup(formatted)+"</div>");
2646
- }
2647
- cssClass=this.opts.formatSelectionCssClass(data, choice.find("div"));
2648
- if (cssClass != undefined) {
2649
- choice.addClass(cssClass);
2650
- }
2651
-
2652
- if(enableChoice){
2653
- choice.find(".select2-search-choice-close")
2654
- .on("mousedown", killEvent)
2655
- .on("click dblclick", this.bind(function (e) {
2656
- if (!this.isInterfaceEnabled()) return;
2657
-
2658
- $(e.target).closest(".select2-search-choice").fadeOut('fast', this.bind(function(){
2659
- this.unselect($(e.target));
2660
- this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
2661
- this.close();
2662
- this.focusSearch();
2663
- })).dequeue();
2664
- killEvent(e);
2665
- })).on("focus", this.bind(function () {
2666
- if (!this.isInterfaceEnabled()) return;
2667
- this.container.addClass("select2-container-active");
2668
- this.dropdown.addClass("select2-drop-active");
2669
- }));
2670
- }
2671
-
2672
- choice.data("select2-data", data);
2673
- choice.insertBefore(this.searchContainer);
2674
-
2675
- val.push(id);
2676
- this.setVal(val);
2677
- },
2678
-
2679
- // multi
2680
- unselect: function (selected) {
2681
- var val = this.getVal(),
2682
- data,
2683
- index;
2684
-
2685
- selected = selected.closest(".select2-search-choice");
2686
-
2687
- if (selected.length === 0) {
2688
- throw "Invalid argument: " + selected + ". Must be .select2-search-choice";
2689
- }
2690
-
2691
- data = selected.data("select2-data");
2692
-
2693
- if (!data) {
2694
- // prevent a race condition when the 'x' is clicked really fast repeatedly the event can be queued
2695
- // and invoked on an element already removed
2696
- return;
2697
- }
2698
-
2699
- index = indexOf(this.id(data), val);
2700
-
2701
- if (index >= 0) {
2702
- val.splice(index, 1);
2703
- this.setVal(val);
2704
- if (this.select) this.postprocessResults();
2705
- }
2706
- selected.remove();
2707
-
2708
- this.opts.element.trigger({ type: "removed", val: this.id(data), choice: data });
2709
- this.triggerChange({ removed: data });
2710
- },
2711
-
2712
- // multi
2713
- postprocessResults: function (data, initial, noHighlightUpdate) {
2714
- var val = this.getVal(),
2715
- choices = this.results.find(".select2-result"),
2716
- compound = this.results.find(".select2-result-with-children"),
2717
- self = this;
2718
-
2719
- choices.each2(function (i, choice) {
2720
- var id = self.id(choice.data("select2-data"));
2721
- if (indexOf(id, val) >= 0) {
2722
- choice.addClass("select2-selected");
2723
- // mark all children of the selected parent as selected
2724
- choice.find(".select2-result-selectable").addClass("select2-selected");
2725
- }
2726
- });
2727
-
2728
- compound.each2(function(i, choice) {
2729
- // hide an optgroup if it doesnt have any selectable children
2730
- if (!choice.is('.select2-result-selectable')
2731
- && choice.find(".select2-result-selectable:not(.select2-selected)").length === 0) {
2732
- choice.addClass("select2-selected");
2733
- }
2734
- });
2735
-
2736
- if (this.highlight() == -1 && noHighlightUpdate !== false){
2737
- self.highlight(0);
2738
- }
2739
-
2740
- //If all results are chosen render formatNoMAtches
2741
- if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){
2742
- this.results.append("<li class='select2-no-results'>" + self.opts.formatNoMatches(self.search.val()) + "</li>");
2743
- }
2744
-
2745
- },
2746
-
2747
- // multi
2748
- getMaxSearchWidth: function() {
2749
- return this.selection.width() - getSideBorderPadding(this.search);
2750
- },
2751
-
2752
- // multi
2753
- resizeSearch: function () {
2754
- var minimumWidth, left, maxWidth, containerLeft, searchWidth,
2755
- sideBorderPadding = getSideBorderPadding(this.search);
2756
-
2757
- minimumWidth = measureTextWidth(this.search) + 10;
2758
-
2759
- left = this.search.offset().left;
2760
-
2761
- maxWidth = this.selection.width();
2762
- containerLeft = this.selection.offset().left;
2763
-
2764
- searchWidth = maxWidth - (left - containerLeft) - sideBorderPadding;
2765
-
2766
- if (searchWidth < minimumWidth) {
2767
- searchWidth = maxWidth - sideBorderPadding;
2768
- }
2769
-
2770
- if (searchWidth < 40) {
2771
- searchWidth = maxWidth - sideBorderPadding;
2772
- }
2773
-
2774
- if (searchWidth <= 0) {
2775
- searchWidth = minimumWidth;
2776
- }
2777
-
2778
- this.search.width(searchWidth);
2779
- },
2780
-
2781
- // multi
2782
- getVal: function () {
2783
- var val;
2784
- if (this.select) {
2785
- val = this.select.val();
2786
- return val === null ? [] : val;
2787
- } else {
2788
- val = this.opts.element.val();
2789
- return splitVal(val, this.opts.separator);
2790
- }
2791
- },
2792
-
2793
- // multi
2794
- setVal: function (val) {
2795
- var unique;
2796
- if (this.select) {
2797
- this.select.val(val);
2798
- } else {
2799
- unique = [];
2800
- // filter out duplicates
2801
- $(val).each(function () {
2802
- if (indexOf(this, unique) < 0) unique.push(this);
2803
- });
2804
- this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator));
2805
- }
2806
- },
2807
-
2808
- // multi
2809
- buildChangeDetails: function (old, current) {
2810
- var current = current.slice(0),
2811
- old = old.slice(0);
2812
-
2813
- // remove intersection from each array
2814
- for (var i = 0; i < current.length; i++) {
2815
- for (var j = 0; j < old.length; j++) {
2816
- if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) {
2817
- current.splice(i, 1);
2818
- i--;
2819
- old.splice(j, 1);
2820
- j--;
2821
- }
2822
- }
2823
- }
2824
-
2825
- return {added: current, removed: old};
2826
- },
2827
-
2828
-
2829
- // multi
2830
- val: function (val, triggerChange) {
2831
- var oldData, self=this, changeDetails;
2832
-
2833
- if (arguments.length === 0) {
2834
- return this.getVal();
2835
- }
2836
-
2837
- oldData=this.data();
2838
- if (!oldData.length) oldData=[];
2839
-
2840
- // val is an id. !val is true for [undefined,null,'',0] - 0 is legal
2841
- if (!val && val !== 0) {
2842
- this.opts.element.val("");
2843
- this.updateSelection([]);
2844
- this.clearSearch();
2845
- if (triggerChange) {
2846
- this.triggerChange({added: this.data(), removed: oldData});
2847
- }
2848
- return;
2849
- }
2850
-
2851
- // val is a list of ids
2852
- this.setVal(val);
2853
-
2854
- if (this.select) {
2855
- this.opts.initSelection(this.select, this.bind(this.updateSelection));
2856
- if (triggerChange) {
2857
- this.triggerChange(this.buildChangeDetails(oldData, this.data()));
2858
- }
2859
- } else {
2860
- if (this.opts.initSelection === undefined) {
2861
- throw new Error("val() cannot be called if initSelection() is not defined");
2862
- }
2863
-
2864
- this.opts.initSelection(this.opts.element, function(data){
2865
- var ids=$(data).map(self.id);
2866
- self.setVal(ids);
2867
- self.updateSelection(data);
2868
- self.clearSearch();
2869
- if (triggerChange) {
2870
- self.triggerChange(this.buildChangeDetails(oldData, this.data()));
2871
- }
2872
- });
2873
- }
2874
- this.clearSearch();
2875
- },
2876
-
2877
- // multi
2878
- onSortStart: function() {
2879
- if (this.select) {
2880
- throw new Error("Sorting of elements is not supported when attached to <select>. Attach to <input type='hidden'/> instead.");
2881
- }
2882
-
2883
- // collapse search field into 0 width so its container can be collapsed as well
2884
- this.search.width(0);
2885
- // hide the container
2886
- this.searchContainer.hide();
2887
- },
2888
-
2889
- // multi
2890
- onSortEnd:function() {
2891
-
2892
- var val=[], self=this;
2893
-
2894
- // show search and move it to the end of the list
2895
- this.searchContainer.show();
2896
- // make sure the search container is the last item in the list
2897
- this.searchContainer.appendTo(this.searchContainer.parent());
2898
- // since we collapsed the width in dragStarted, we resize it here
2899
- this.resizeSearch();
2900
-
2901
- // update selection
2902
-
2903
- this.selection.find(".select2-search-choice").each(function() {
2904
- val.push(self.opts.id($(this).data("select2-data")));
2905
- });
2906
- this.setVal(val);
2907
- this.triggerChange();
2908
- },
2909
-
2910
- // multi
2911
- data: function(values, triggerChange) {
2912
- var self=this, ids, old;
2913
- if (arguments.length === 0) {
2914
- return this.selection
2915
- .find(".select2-search-choice")
2916
- .map(function() { return $(this).data("select2-data"); })
2917
- .get();
2918
- } else {
2919
- old = this.data();
2920
- if (!values) { values = []; }
2921
- ids = $.map(values, function(e) { return self.opts.id(e); });
2922
- this.setVal(ids);
2923
- this.updateSelection(values);
2924
- this.clearSearch();
2925
- if (triggerChange) {
2926
- this.triggerChange(this.buildChangeDetails(old, this.data()));
2927
- }
2928
- }
2929
- }
2930
- });
2931
-
2932
- $.fn.select2 = function () {
2933
-
2934
- var args = Array.prototype.slice.call(arguments, 0),
2935
- opts,
2936
- select2,
2937
- value, multiple,
2938
- allowedMethods = ["val", "destroy", "opened", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "readonly", "positionDropdown", "data"],
2939
- valueMethods = ["val", "opened", "isFocused", "container", "data"];
2940
-
2941
- this.each(function () {
2942
- if (args.length === 0 || typeof(args[0]) === "object") {
2943
- opts = args.length === 0 ? {} : $.extend({}, args[0]);
2944
- opts.element = $(this);
2945
-
2946
- if (opts.element.get(0).tagName.toLowerCase() === "select") {
2947
- multiple = opts.element.prop("multiple");
2948
- } else {
2949
- multiple = opts.multiple || false;
2950
- if ("tags" in opts) {opts.multiple = multiple = true;}
2951
- }
2952
-
2953
- select2 = multiple ? new MultiSelect2() : new SingleSelect2();
2954
- select2.init(opts);
2955
- } else if (typeof(args[0]) === "string") {
2956
-
2957
- if (indexOf(args[0], allowedMethods) < 0) {
2958
- throw "Unknown method: " + args[0];
2959
- }
2960
-
2961
- value = undefined;
2962
- select2 = $(this).data("select2");
2963
- if (select2 === undefined) return;
2964
- if (args[0] === "container") {
2965
- value=select2.container;
2966
- } else {
2967
- value = select2[args[0]].apply(select2, args.slice(1));
2968
- }
2969
- if (indexOf(args[0], valueMethods) >= 0) {
2970
- return false;
2971
- }
2972
- } else {
2973
- throw "Invalid arguments to select2 plugin: " + args;
2974
- }
2975
- });
2976
- return (value === undefined) ? this : value;
2977
- };
2978
-
2979
- // plugin defaults, accessible to users
2980
- $.fn.select2.defaults = {
2981
- width: "copy",
2982
- loadMorePadding: 0,
2983
- closeOnSelect: true,
2984
- openOnEnter: true,
2985
- containerCss: {},
2986
- dropdownCss: {},
2987
- containerCssClass: "",
2988
- dropdownCssClass: "",
2989
- formatResult: function(result, container, query, escapeMarkup) {
2990
- var markup=[];
2991
- markMatch(result.text, query.term, markup, escapeMarkup);
2992
- return markup.join("");
2993
- },
2994
- formatSelection: function (data, container) {
2995
- return data ? data.text : undefined;
2996
- },
2997
- sortResults: function (results, container, query) {
2998
- return results;
2999
- },
3000
- formatResultCssClass: function(data) {return undefined;},
3001
- formatSelectionCssClass: function(data, container) {return undefined;},
3002
- formatNoMatches: function () { return "No matches found"; },
3003
- formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " more character" + (n == 1? "" : "s"); },
3004
- formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); },
3005
- formatSelectionTooBig: function (limit) { return "You can only select " + limit + " item" + (limit == 1 ? "" : "s"); },
3006
- formatLoadMore: function (pageNumber) { return "Loading more results..."; },
3007
- formatSearching: function () { return "Searching..."; },
3008
- minimumResultsForSearch: 0,
3009
- minimumInputLength: 0,
3010
- maximumInputLength: null,
3011
- maximumSelectionSize: 0,
3012
- id: function (e) { return e.id; },
3013
- matcher: function(term, text) {
3014
- return (''+text).toUpperCase().indexOf((''+term).toUpperCase()) >= 0;
3015
- },
3016
- separator: ",",
3017
- tokenSeparators: [],
3018
- tokenizer: defaultTokenizer,
3019
- escapeMarkup: function (markup) {
3020
- var replace_map = {
3021
- '\\': '&#92;',
3022
- '&': '&amp;',
3023
- '<': '&lt;',
3024
- '>': '&gt;',
3025
- '"': '&quot;',
3026
- "'": '&#39;',
3027
- "/": '&#47;'
3028
- };
3029
-
3030
- return String(markup).replace(/[&<>"'\/\\]/g, function (match) {
3031
- return replace_map[match];
3032
- });
3033
- },
3034
- blurOnChange: false,
3035
- selectOnBlur: false,
3036
- adaptContainerCssClass: function(c) { return c; },
3037
- adaptDropdownCssClass: function(c) { return null; }
3038
- };
3039
-
3040
- $.fn.select2.ajaxDefaults = {
3041
- transport: $.ajax,
3042
- params: {
3043
- type: "GET",
3044
- cache: false,
3045
- dataType: "json"
3046
- }
3047
- };
3048
-
3049
- // exports
3050
- window.Select2 = {
3051
- query: {
3052
- ajax: ajax,
3053
- local: local,
3054
- tags: tags
3055
- }, util: {
3056
- debounce: debounce,
3057
- markMatch: markMatch
3058
- }, "class": {
3059
- "abstract": AbstractSelect2,
3060
- "single": SingleSelect2,
3061
- "multi": MultiSelect2
3062
- }
3063
- };
3064
-
3065
- }(jQuery));