tjadmin 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tjadmin/version.rb +1 -1
- data/vendor/assets/font/DXI1ORHCpsQm3Vp6mXoaTXhCUOGz7vYGh680lGh-uXM.woff +0 -0
- data/vendor/assets/font/FontAwesome.otf +0 -0
- data/vendor/assets/font/cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff +0 -0
- data/vendor/assets/font/fontawesome-webfont.eot +0 -0
- data/vendor/assets/font/fontawesome-webfont.svg +399 -0
- data/vendor/assets/font/fontawesome-webfont.ttf +0 -0
- data/vendor/assets/font/fontawesome-webfont.woff +0 -0
- data/vendor/assets/javascripts/tjadmin/ace-elements.js +866 -0
- data/vendor/assets/javascripts/tjadmin/ace-extra.js +374 -0
- data/vendor/assets/javascripts/tjadmin/ace.js +310 -0
- data/vendor/assets/javascripts/tjadmin/additional-methods.js +617 -0
- data/vendor/assets/javascripts/tjadmin/bootbox.js +660 -0
- data/vendor/assets/javascripts/tjadmin/bootstrap-colorpicker.js +540 -0
- data/vendor/assets/javascripts/tjadmin/bootstrap-tag.js +206 -0
- data/vendor/assets/javascripts/tjadmin/bootstrap-wysiwyg.js +206 -0
- data/vendor/assets/javascripts/tjadmin/bootstrap.js +2280 -0
- data/vendor/assets/javascripts/tjadmin/chosen.jquery.js +1166 -0
- data/vendor/assets/javascripts/tjadmin/date-time/bootstrap-datepicker.js +1016 -0
- data/vendor/assets/javascripts/tjadmin/date-time/bootstrap-timepicker.js +888 -0
- data/vendor/assets/javascripts/tjadmin/date-time/daterangepicker.js +854 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.bg.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.ca.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.cs.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.da.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.de.js +16 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.el.js +13 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.es.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.fi.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.fr.js +16 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.he.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.hr.js +13 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.hu.js +16 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.id.js +13 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.is.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.it.js +16 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.ja.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.kr.js +13 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.lt.js +16 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.lv.js +16 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.ms.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.nb.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.nl.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.pl.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.pt-BR.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.pt.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.ro.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.rs-latin.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.rs.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.ru.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.sk.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.sl.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.sv.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.sw.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.th.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.tr.js +15 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.uk.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.zh-CN.js +14 -0
- data/vendor/assets/javascripts/tjadmin/date-time/locales/bootstrap-datepicker.zh-TW.js +13 -0
- data/vendor/assets/javascripts/tjadmin/date-time/moment.js +1400 -0
- data/vendor/assets/javascripts/tjadmin/excanvas.js +1428 -0
- data/vendor/assets/javascripts/tjadmin/flot/jquery.flot.js +3061 -0
- data/vendor/assets/javascripts/tjadmin/flot/jquery.flot.pie.js +817 -0
- data/vendor/assets/javascripts/tjadmin/flot/jquery.flot.resize.js +60 -0
- data/vendor/assets/javascripts/tjadmin/fuelux/data/fuelux.tree-sampledata.js +174 -0
- data/vendor/assets/javascripts/tjadmin/fuelux/fuelux.spinner.js +201 -0
- data/vendor/assets/javascripts/tjadmin/fuelux/fuelux.tree.js +173 -0
- data/vendor/assets/javascripts/tjadmin/fuelux/fuelux.wizard.js +172 -0
- data/vendor/assets/javascripts/tjadmin/fullcalendar.js +5973 -0
- data/vendor/assets/javascripts/tjadmin/jquery-1.10.2.js +9789 -0
- data/vendor/assets/javascripts/tjadmin/jquery-2.0.3.js +8829 -0
- data/vendor/assets/javascripts/tjadmin/jquery-ui-1.10.3.custom.js +5423 -0
- data/vendor/assets/javascripts/tjadmin/jquery.autosize.js +242 -0
- data/vendor/assets/javascripts/tjadmin/jquery.colorbox.js +1063 -0
- data/vendor/assets/javascripts/tjadmin/jquery.dataTables.js +12099 -0
- data/vendor/assets/javascripts/tjadmin/jquery.easy-pie-chart.js +200 -0
- data/vendor/assets/javascripts/tjadmin/jquery.gritter.js +418 -0
- data/vendor/assets/javascripts/tjadmin/jquery.hotkeys.js +113 -0
- data/vendor/assets/javascripts/tjadmin/jquery.inputlimiter.1.3.1.js +172 -0
- data/vendor/assets/javascripts/tjadmin/jquery.knob.js +656 -0
- data/vendor/assets/javascripts/tjadmin/jquery.maskedinput.js +338 -0
- data/vendor/assets/javascripts/tjadmin/jquery.mobile.custom.js +1081 -0
- data/vendor/assets/javascripts/tjadmin/jquery.slimscroll.js +448 -0
- data/vendor/assets/javascripts/tjadmin/jquery.sparkline.js +3054 -0
- data/vendor/assets/javascripts/tjadmin/jquery.ui.touch-punch.js +160 -0
- data/vendor/assets/javascripts/tjadmin/jquery.validate.js +1231 -0
- data/vendor/assets/javascripts/tjadmin/markdown/bootstrap-markdown.js +993 -0
- data/vendor/assets/javascripts/tjadmin/markdown/markdown.js +1616 -0
- data/vendor/assets/javascripts/tjadmin/markdown/to-markdown.js +184 -0
- data/vendor/assets/javascripts/tjadmin/select2.js +3168 -0
- data/vendor/assets/javascripts/tjadmin/spin.js +349 -0
- data/vendor/assets/javascripts/tjadmin/x-editable/ace-editable.js +368 -0
- data/vendor/assets/javascripts/tjadmin/x-editable/bootstrap-editable.js +6827 -0
- data/vendor/assets/javascripts/tjadmin.js +5 -0
- data/vendor/assets/stylesheets/tjadmin/ace-fonts.css.scss +13 -0
- data/vendor/assets/stylesheets/tjadmin/ace-ie.css +293 -0
- data/vendor/assets/stylesheets/tjadmin/ace-responsive.css +1113 -0
- data/vendor/assets/stylesheets/tjadmin/ace-skins.css +1301 -0
- data/vendor/assets/stylesheets/tjadmin/ace.css +14701 -0
- data/vendor/assets/stylesheets/tjadmin/bootstrap-responsive.css +1109 -0
- data/vendor/assets/stylesheets/tjadmin/bootstrap.css +6167 -0
- data/vendor/assets/stylesheets/tjadmin/chosen.css +430 -0
- data/vendor/assets/stylesheets/tjadmin/font-awesome-ie7.css +1203 -0
- data/vendor/assets/stylesheets/tjadmin/font-awesome.css +1479 -0
- data/vendor/assets/stylesheets/tjadmin/jquery-ui-1.10.3.custom.css +223 -0
- data/vendor/assets/stylesheets/tjadmin.css +9 -0
- metadata +106 -1
@@ -0,0 +1,3168 @@
|
|
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
|
+
DIACRITICS = {"\u24B6":"A","\uFF21":"A","\u00C0":"A","\u00C1":"A","\u00C2":"A","\u1EA6":"A","\u1EA4":"A","\u1EAA":"A","\u1EA8":"A","\u00C3":"A","\u0100":"A","\u0102":"A","\u1EB0":"A","\u1EAE":"A","\u1EB4":"A","\u1EB2":"A","\u0226":"A","\u01E0":"A","\u00C4":"A","\u01DE":"A","\u1EA2":"A","\u00C5":"A","\u01FA":"A","\u01CD":"A","\u0200":"A","\u0202":"A","\u1EA0":"A","\u1EAC":"A","\u1EB6":"A","\u1E00":"A","\u0104":"A","\u023A":"A","\u2C6F":"A","\uA732":"AA","\u00C6":"AE","\u01FC":"AE","\u01E2":"AE","\uA734":"AO","\uA736":"AU","\uA738":"AV","\uA73A":"AV","\uA73C":"AY","\u24B7":"B","\uFF22":"B","\u1E02":"B","\u1E04":"B","\u1E06":"B","\u0243":"B","\u0182":"B","\u0181":"B","\u24B8":"C","\uFF23":"C","\u0106":"C","\u0108":"C","\u010A":"C","\u010C":"C","\u00C7":"C","\u1E08":"C","\u0187":"C","\u023B":"C","\uA73E":"C","\u24B9":"D","\uFF24":"D","\u1E0A":"D","\u010E":"D","\u1E0C":"D","\u1E10":"D","\u1E12":"D","\u1E0E":"D","\u0110":"D","\u018B":"D","\u018A":"D","\u0189":"D","\uA779":"D","\u01F1":"DZ","\u01C4":"DZ","\u01F2":"Dz","\u01C5":"Dz","\u24BA":"E","\uFF25":"E","\u00C8":"E","\u00C9":"E","\u00CA":"E","\u1EC0":"E","\u1EBE":"E","\u1EC4":"E","\u1EC2":"E","\u1EBC":"E","\u0112":"E","\u1E14":"E","\u1E16":"E","\u0114":"E","\u0116":"E","\u00CB":"E","\u1EBA":"E","\u011A":"E","\u0204":"E","\u0206":"E","\u1EB8":"E","\u1EC6":"E","\u0228":"E","\u1E1C":"E","\u0118":"E","\u1E18":"E","\u1E1A":"E","\u0190":"E","\u018E":"E","\u24BB":"F","\uFF26":"F","\u1E1E":"F","\u0191":"F","\uA77B":"F","\u24BC":"G","\uFF27":"G","\u01F4":"G","\u011C":"G","\u1E20":"G","\u011E":"G","\u0120":"G","\u01E6":"G","\u0122":"G","\u01E4":"G","\u0193":"G","\uA7A0":"G","\uA77D":"G","\uA77E":"G","\u24BD":"H","\uFF28":"H","\u0124":"H","\u1E22":"H","\u1E26":"H","\u021E":"H","\u1E24":"H","\u1E28":"H","\u1E2A":"H","\u0126":"H","\u2C67":"H","\u2C75":"H","\uA78D":"H","\u24BE":"I","\uFF29":"I","\u00CC":"I","\u00CD":"I","\u00CE":"I","\u0128":"I","\u012A":"I","\u012C":"I","\u0130":"I","\u00CF":"I","\u1E2E":"I","\u1EC8":"I","\u01CF":"I","\u0208":"I","\u020A":"I","\u1ECA":"I","\u012E":"I","\u1E2C":"I","\u0197":"I","\u24BF":"J","\uFF2A":"J","\u0134":"J","\u0248":"J","\u24C0":"K","\uFF2B":"K","\u1E30":"K","\u01E8":"K","\u1E32":"K","\u0136":"K","\u1E34":"K","\u0198":"K","\u2C69":"K","\uA740":"K","\uA742":"K","\uA744":"K","\uA7A2":"K","\u24C1":"L","\uFF2C":"L","\u013F":"L","\u0139":"L","\u013D":"L","\u1E36":"L","\u1E38":"L","\u013B":"L","\u1E3C":"L","\u1E3A":"L","\u0141":"L","\u023D":"L","\u2C62":"L","\u2C60":"L","\uA748":"L","\uA746":"L","\uA780":"L","\u01C7":"LJ","\u01C8":"Lj","\u24C2":"M","\uFF2D":"M","\u1E3E":"M","\u1E40":"M","\u1E42":"M","\u2C6E":"M","\u019C":"M","\u24C3":"N","\uFF2E":"N","\u01F8":"N","\u0143":"N","\u00D1":"N","\u1E44":"N","\u0147":"N","\u1E46":"N","\u0145":"N","\u1E4A":"N","\u1E48":"N","\u0220":"N","\u019D":"N","\uA790":"N","\uA7A4":"N","\u01CA":"NJ","\u01CB":"Nj","\u24C4":"O","\uFF2F":"O","\u00D2":"O","\u00D3":"O","\u00D4":"O","\u1ED2":"O","\u1ED0":"O","\u1ED6":"O","\u1ED4":"O","\u00D5":"O","\u1E4C":"O","\u022C":"O","\u1E4E":"O","\u014C":"O","\u1E50":"O","\u1E52":"O","\u014E":"O","\u022E":"O","\u0230":"O","\u00D6":"O","\u022A":"O","\u1ECE":"O","\u0150":"O","\u01D1":"O","\u020C":"O","\u020E":"O","\u01A0":"O","\u1EDC":"O","\u1EDA":"O","\u1EE0":"O","\u1EDE":"O","\u1EE2":"O","\u1ECC":"O","\u1ED8":"O","\u01EA":"O","\u01EC":"O","\u00D8":"O","\u01FE":"O","\u0186":"O","\u019F":"O","\uA74A":"O","\uA74C":"O","\u01A2":"OI","\uA74E":"OO","\u0222":"OU","\u24C5":"P","\uFF30":"P","\u1E54":"P","\u1E56":"P","\u01A4":"P","\u2C63":"P","\uA750":"P","\uA752":"P","\uA754":"P","\u24C6":"Q","\uFF31":"Q","\uA756":"Q","\uA758":"Q","\u024A":"Q","\u24C7":"R","\uFF32":"R","\u0154":"R","\u1E58":"R","\u0158":"R","\u0210":"R","\u0212":"R","\u1E5A":"R","\u1E5C":"R","\u0156":"R","\u1E5E":"R","\u024C":"R","\u2C64":"R","\uA75A":"R","\uA7A6":"R","\uA782":"R","\u24C8":"S","\uFF33":"S","\u1E9E":"S","\u015A":"S","\u1E64":"S","\u015C":"S","\u1E60":"S","\u0160":"S","\u1E66":"S","\u1E62":"S","\u1E68":"S","\u0218":"S","\u015E":"S","\u2C7E":"S","\uA7A8":"S","\uA784":"S","\u24C9":"T","\uFF34":"T","\u1E6A":"T","\u0164":"T","\u1E6C":"T","\u021A":"T","\u0162":"T","\u1E70":"T","\u1E6E":"T","\u0166":"T","\u01AC":"T","\u01AE":"T","\u023E":"T","\uA786":"T","\uA728":"TZ","\u24CA":"U","\uFF35":"U","\u00D9":"U","\u00DA":"U","\u00DB":"U","\u0168":"U","\u1E78":"U","\u016A":"U","\u1E7A":"U","\u016C":"U","\u00DC":"U","\u01DB":"U","\u01D7":"U","\u01D5":"U","\u01D9":"U","\u1EE6":"U","\u016E":"U","\u0170":"U","\u01D3":"U","\u0214":"U","\u0216":"U","\u01AF":"U","\u1EEA":"U","\u1EE8":"U","\u1EEE":"U","\u1EEC":"U","\u1EF0":"U","\u1EE4":"U","\u1E72":"U","\u0172":"U","\u1E76":"U","\u1E74":"U","\u0244":"U","\u24CB":"V","\uFF36":"V","\u1E7C":"V","\u1E7E":"V","\u01B2":"V","\uA75E":"V","\u0245":"V","\uA760":"VY","\u24CC":"W","\uFF37":"W","\u1E80":"W","\u1E82":"W","\u0174":"W","\u1E86":"W","\u1E84":"W","\u1E88":"W","\u2C72":"W","\u24CD":"X","\uFF38":"X","\u1E8A":"X","\u1E8C":"X","\u24CE":"Y","\uFF39":"Y","\u1EF2":"Y","\u00DD":"Y","\u0176":"Y","\u1EF8":"Y","\u0232":"Y","\u1E8E":"Y","\u0178":"Y","\u1EF6":"Y","\u1EF4":"Y","\u01B3":"Y","\u024E":"Y","\u1EFE":"Y","\u24CF":"Z","\uFF3A":"Z","\u0179":"Z","\u1E90":"Z","\u017B":"Z","\u017D":"Z","\u1E92":"Z","\u1E94":"Z","\u01B5":"Z","\u0224":"Z","\u2C7F":"Z","\u2C6B":"Z","\uA762":"Z","\u24D0":"a","\uFF41":"a","\u1E9A":"a","\u00E0":"a","\u00E1":"a","\u00E2":"a","\u1EA7":"a","\u1EA5":"a","\u1EAB":"a","\u1EA9":"a","\u00E3":"a","\u0101":"a","\u0103":"a","\u1EB1":"a","\u1EAF":"a","\u1EB5":"a","\u1EB3":"a","\u0227":"a","\u01E1":"a","\u00E4":"a","\u01DF":"a","\u1EA3":"a","\u00E5":"a","\u01FB":"a","\u01CE":"a","\u0201":"a","\u0203":"a","\u1EA1":"a","\u1EAD":"a","\u1EB7":"a","\u1E01":"a","\u0105":"a","\u2C65":"a","\u0250":"a","\uA733":"aa","\u00E6":"ae","\u01FD":"ae","\u01E3":"ae","\uA735":"ao","\uA737":"au","\uA739":"av","\uA73B":"av","\uA73D":"ay","\u24D1":"b","\uFF42":"b","\u1E03":"b","\u1E05":"b","\u1E07":"b","\u0180":"b","\u0183":"b","\u0253":"b","\u24D2":"c","\uFF43":"c","\u0107":"c","\u0109":"c","\u010B":"c","\u010D":"c","\u00E7":"c","\u1E09":"c","\u0188":"c","\u023C":"c","\uA73F":"c","\u2184":"c","\u24D3":"d","\uFF44":"d","\u1E0B":"d","\u010F":"d","\u1E0D":"d","\u1E11":"d","\u1E13":"d","\u1E0F":"d","\u0111":"d","\u018C":"d","\u0256":"d","\u0257":"d","\uA77A":"d","\u01F3":"dz","\u01C6":"dz","\u24D4":"e","\uFF45":"e","\u00E8":"e","\u00E9":"e","\u00EA":"e","\u1EC1":"e","\u1EBF":"e","\u1EC5":"e","\u1EC3":"e","\u1EBD":"e","\u0113":"e","\u1E15":"e","\u1E17":"e","\u0115":"e","\u0117":"e","\u00EB":"e","\u1EBB":"e","\u011B":"e","\u0205":"e","\u0207":"e","\u1EB9":"e","\u1EC7":"e","\u0229":"e","\u1E1D":"e","\u0119":"e","\u1E19":"e","\u1E1B":"e","\u0247":"e","\u025B":"e","\u01DD":"e","\u24D5":"f","\uFF46":"f","\u1E1F":"f","\u0192":"f","\uA77C":"f","\u24D6":"g","\uFF47":"g","\u01F5":"g","\u011D":"g","\u1E21":"g","\u011F":"g","\u0121":"g","\u01E7":"g","\u0123":"g","\u01E5":"g","\u0260":"g","\uA7A1":"g","\u1D79":"g","\uA77F":"g","\u24D7":"h","\uFF48":"h","\u0125":"h","\u1E23":"h","\u1E27":"h","\u021F":"h","\u1E25":"h","\u1E29":"h","\u1E2B":"h","\u1E96":"h","\u0127":"h","\u2C68":"h","\u2C76":"h","\u0265":"h","\u0195":"hv","\u24D8":"i","\uFF49":"i","\u00EC":"i","\u00ED":"i","\u00EE":"i","\u0129":"i","\u012B":"i","\u012D":"i","\u00EF":"i","\u1E2F":"i","\u1EC9":"i","\u01D0":"i","\u0209":"i","\u020B":"i","\u1ECB":"i","\u012F":"i","\u1E2D":"i","\u0268":"i","\u0131":"i","\u24D9":"j","\uFF4A":"j","\u0135":"j","\u01F0":"j","\u0249":"j","\u24DA":"k","\uFF4B":"k","\u1E31":"k","\u01E9":"k","\u1E33":"k","\u0137":"k","\u1E35":"k","\u0199":"k","\u2C6A":"k","\uA741":"k","\uA743":"k","\uA745":"k","\uA7A3":"k","\u24DB":"l","\uFF4C":"l","\u0140":"l","\u013A":"l","\u013E":"l","\u1E37":"l","\u1E39":"l","\u013C":"l","\u1E3D":"l","\u1E3B":"l","\u017F":"l","\u0142":"l","\u019A":"l","\u026B":"l","\u2C61":"l","\uA749":"l","\uA781":"l","\uA747":"l","\u01C9":"lj","\u24DC":"m","\uFF4D":"m","\u1E3F":"m","\u1E41":"m","\u1E43":"m","\u0271":"m","\u026F":"m","\u24DD":"n","\uFF4E":"n","\u01F9":"n","\u0144":"n","\u00F1":"n","\u1E45":"n","\u0148":"n","\u1E47":"n","\u0146":"n","\u1E4B":"n","\u1E49":"n","\u019E":"n","\u0272":"n","\u0149":"n","\uA791":"n","\uA7A5":"n","\u01CC":"nj","\u24DE":"o","\uFF4F":"o","\u00F2":"o","\u00F3":"o","\u00F4":"o","\u1ED3":"o","\u1ED1":"o","\u1ED7":"o","\u1ED5":"o","\u00F5":"o","\u1E4D":"o","\u022D":"o","\u1E4F":"o","\u014D":"o","\u1E51":"o","\u1E53":"o","\u014F":"o","\u022F":"o","\u0231":"o","\u00F6":"o","\u022B":"o","\u1ECF":"o","\u0151":"o","\u01D2":"o","\u020D":"o","\u020F":"o","\u01A1":"o","\u1EDD":"o","\u1EDB":"o","\u1EE1":"o","\u1EDF":"o","\u1EE3":"o","\u1ECD":"o","\u1ED9":"o","\u01EB":"o","\u01ED":"o","\u00F8":"o","\u01FF":"o","\u0254":"o","\uA74B":"o","\uA74D":"o","\u0275":"o","\u01A3":"oi","\u0223":"ou","\uA74F":"oo","\u24DF":"p","\uFF50":"p","\u1E55":"p","\u1E57":"p","\u01A5":"p","\u1D7D":"p","\uA751":"p","\uA753":"p","\uA755":"p","\u24E0":"q","\uFF51":"q","\u024B":"q","\uA757":"q","\uA759":"q","\u24E1":"r","\uFF52":"r","\u0155":"r","\u1E59":"r","\u0159":"r","\u0211":"r","\u0213":"r","\u1E5B":"r","\u1E5D":"r","\u0157":"r","\u1E5F":"r","\u024D":"r","\u027D":"r","\uA75B":"r","\uA7A7":"r","\uA783":"r","\u24E2":"s","\uFF53":"s","\u00DF":"s","\u015B":"s","\u1E65":"s","\u015D":"s","\u1E61":"s","\u0161":"s","\u1E67":"s","\u1E63":"s","\u1E69":"s","\u0219":"s","\u015F":"s","\u023F":"s","\uA7A9":"s","\uA785":"s","\u1E9B":"s","\u24E3":"t","\uFF54":"t","\u1E6B":"t","\u1E97":"t","\u0165":"t","\u1E6D":"t","\u021B":"t","\u0163":"t","\u1E71":"t","\u1E6F":"t","\u0167":"t","\u01AD":"t","\u0288":"t","\u2C66":"t","\uA787":"t","\uA729":"tz","\u24E4":"u","\uFF55":"u","\u00F9":"u","\u00FA":"u","\u00FB":"u","\u0169":"u","\u1E79":"u","\u016B":"u","\u1E7B":"u","\u016D":"u","\u00FC":"u","\u01DC":"u","\u01D8":"u","\u01D6":"u","\u01DA":"u","\u1EE7":"u","\u016F":"u","\u0171":"u","\u01D4":"u","\u0215":"u","\u0217":"u","\u01B0":"u","\u1EEB":"u","\u1EE9":"u","\u1EEF":"u","\u1EED":"u","\u1EF1":"u","\u1EE5":"u","\u1E73":"u","\u0173":"u","\u1E77":"u","\u1E75":"u","\u0289":"u","\u24E5":"v","\uFF56":"v","\u1E7D":"v","\u1E7F":"v","\u028B":"v","\uA75F":"v","\u028C":"v","\uA761":"vy","\u24E6":"w","\uFF57":"w","\u1E81":"w","\u1E83":"w","\u0175":"w","\u1E87":"w","\u1E85":"w","\u1E98":"w","\u1E89":"w","\u2C73":"w","\u24E7":"x","\uFF58":"x","\u1E8B":"x","\u1E8D":"x","\u24E8":"y","\uFF59":"y","\u1EF3":"y","\u00FD":"y","\u0177":"y","\u1EF9":"y","\u0233":"y","\u1E8F":"y","\u00FF":"y","\u1EF7":"y","\u1E99":"y","\u1EF5":"y","\u01B4":"y","\u024F":"y","\u1EFF":"y","\u24E9":"z","\uFF5A":"z","\u017A":"z","\u1E91":"z","\u017C":"z","\u017E":"z","\u1E93":"z","\u1E95":"z","\u01B6":"z","\u0225":"z","\u0240":"z","\u2C6C":"z","\uA763":"z"};
|
102
|
+
|
103
|
+
$document = $(document);
|
104
|
+
|
105
|
+
nextUid=(function() { var counter=1; return function() { return counter++; }; }());
|
106
|
+
|
107
|
+
|
108
|
+
function stripDiacritics(str) {
|
109
|
+
var ret, i, l, c;
|
110
|
+
|
111
|
+
if (!str || str.length < 1) return str;
|
112
|
+
|
113
|
+
ret = "";
|
114
|
+
for (i = 0, l = str.length; i < l; i++) {
|
115
|
+
c = str.charAt(i);
|
116
|
+
ret += DIACRITICS[c] || c;
|
117
|
+
}
|
118
|
+
return ret;
|
119
|
+
}
|
120
|
+
|
121
|
+
function indexOf(value, array) {
|
122
|
+
var i = 0, l = array.length;
|
123
|
+
for (; i < l; i = i + 1) {
|
124
|
+
if (equal(value, array[i])) return i;
|
125
|
+
}
|
126
|
+
return -1;
|
127
|
+
}
|
128
|
+
|
129
|
+
function measureScrollbar () {
|
130
|
+
var $template = $( MEASURE_SCROLLBAR_TEMPLATE );
|
131
|
+
$template.appendTo('body');
|
132
|
+
|
133
|
+
var dim = {
|
134
|
+
width: $template.width() - $template[0].clientWidth,
|
135
|
+
height: $template.height() - $template[0].clientHeight
|
136
|
+
};
|
137
|
+
$template.remove();
|
138
|
+
|
139
|
+
return dim;
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* Compares equality of a and b
|
144
|
+
* @param a
|
145
|
+
* @param b
|
146
|
+
*/
|
147
|
+
function equal(a, b) {
|
148
|
+
if (a === b) return true;
|
149
|
+
if (a === undefined || b === undefined) return false;
|
150
|
+
if (a === null || b === null) return false;
|
151
|
+
// Check whether 'a' or 'b' is a string (primitive or object).
|
152
|
+
// The concatenation of an empty string (+'') converts its argument to a string's primitive.
|
153
|
+
if (a.constructor === String) return a+'' === b+''; // a+'' - in case 'a' is a String object
|
154
|
+
if (b.constructor === String) return b+'' === a+''; // b+'' - in case 'b' is a String object
|
155
|
+
return false;
|
156
|
+
}
|
157
|
+
|
158
|
+
/**
|
159
|
+
* Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
|
160
|
+
* strings
|
161
|
+
* @param string
|
162
|
+
* @param separator
|
163
|
+
*/
|
164
|
+
function splitVal(string, separator) {
|
165
|
+
var val, i, l;
|
166
|
+
if (string === null || string.length < 1) return [];
|
167
|
+
val = string.split(separator);
|
168
|
+
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
|
169
|
+
return val;
|
170
|
+
}
|
171
|
+
|
172
|
+
function getSideBorderPadding(element) {
|
173
|
+
return element.outerWidth(false) - element.width();
|
174
|
+
}
|
175
|
+
|
176
|
+
function installKeyUpChangeEvent(element) {
|
177
|
+
var key="keyup-change-value";
|
178
|
+
element.on("keydown", function () {
|
179
|
+
if ($.data(element, key) === undefined) {
|
180
|
+
$.data(element, key, element.val());
|
181
|
+
}
|
182
|
+
});
|
183
|
+
element.on("keyup", function () {
|
184
|
+
var val= $.data(element, key);
|
185
|
+
if (val !== undefined && element.val() !== val) {
|
186
|
+
$.removeData(element, key);
|
187
|
+
element.trigger("keyup-change");
|
188
|
+
}
|
189
|
+
});
|
190
|
+
}
|
191
|
+
|
192
|
+
$document.on("mousemove", function (e) {
|
193
|
+
lastMousePosition.x = e.pageX;
|
194
|
+
lastMousePosition.y = e.pageY;
|
195
|
+
});
|
196
|
+
|
197
|
+
/**
|
198
|
+
* filters mouse events so an event is fired only if the mouse moved.
|
199
|
+
*
|
200
|
+
* filters out mouse events that occur when mouse is stationary but
|
201
|
+
* the elements under the pointer are scrolled.
|
202
|
+
*/
|
203
|
+
function installFilteredMouseMove(element) {
|
204
|
+
element.on("mousemove", function (e) {
|
205
|
+
var lastpos = lastMousePosition;
|
206
|
+
if (lastpos === undefined || lastpos.x !== e.pageX || lastpos.y !== e.pageY) {
|
207
|
+
$(e.target).trigger("mousemove-filtered", e);
|
208
|
+
}
|
209
|
+
});
|
210
|
+
}
|
211
|
+
|
212
|
+
/**
|
213
|
+
* Debounces a function. Returns a function that calls the original fn function only if no invocations have been made
|
214
|
+
* within the last quietMillis milliseconds.
|
215
|
+
*
|
216
|
+
* @param quietMillis number of milliseconds to wait before invoking fn
|
217
|
+
* @param fn function to be debounced
|
218
|
+
* @param ctx object to be used as this reference within fn
|
219
|
+
* @return debounced version of fn
|
220
|
+
*/
|
221
|
+
function debounce(quietMillis, fn, ctx) {
|
222
|
+
ctx = ctx || undefined;
|
223
|
+
var timeout;
|
224
|
+
return function () {
|
225
|
+
var args = arguments;
|
226
|
+
window.clearTimeout(timeout);
|
227
|
+
timeout = window.setTimeout(function() {
|
228
|
+
fn.apply(ctx, args);
|
229
|
+
}, quietMillis);
|
230
|
+
};
|
231
|
+
}
|
232
|
+
|
233
|
+
/**
|
234
|
+
* A simple implementation of a thunk
|
235
|
+
* @param formula function used to lazily initialize the thunk
|
236
|
+
* @return {Function}
|
237
|
+
*/
|
238
|
+
function thunk(formula) {
|
239
|
+
var evaluated = false,
|
240
|
+
value;
|
241
|
+
return function() {
|
242
|
+
if (evaluated === false) { value = formula(); evaluated = true; }
|
243
|
+
return value;
|
244
|
+
};
|
245
|
+
};
|
246
|
+
|
247
|
+
function installDebouncedScroll(threshold, element) {
|
248
|
+
var notify = debounce(threshold, function (e) { element.trigger("scroll-debounced", e);});
|
249
|
+
element.on("scroll", function (e) {
|
250
|
+
if (indexOf(e.target, element.get()) >= 0) notify(e);
|
251
|
+
});
|
252
|
+
}
|
253
|
+
|
254
|
+
function focus($el) {
|
255
|
+
if ($el[0] === document.activeElement) return;
|
256
|
+
|
257
|
+
/* set the focus in a 0 timeout - that way the focus is set after the processing
|
258
|
+
of the current event has finished - which seems like the only reliable way
|
259
|
+
to set focus */
|
260
|
+
window.setTimeout(function() {
|
261
|
+
var el=$el[0], pos=$el.val().length, range;
|
262
|
+
|
263
|
+
$el.focus();
|
264
|
+
|
265
|
+
/* make sure el received focus so we do not error out when trying to manipulate the caret.
|
266
|
+
sometimes modals or others listeners may steal it after its set */
|
267
|
+
if ($el.is(":visible") && el === document.activeElement) {
|
268
|
+
|
269
|
+
/* after the focus is set move the caret to the end, necessary when we val()
|
270
|
+
just before setting focus */
|
271
|
+
if(el.setSelectionRange)
|
272
|
+
{
|
273
|
+
el.setSelectionRange(pos, pos);
|
274
|
+
}
|
275
|
+
else if (el.createTextRange) {
|
276
|
+
range = el.createTextRange();
|
277
|
+
range.collapse(false);
|
278
|
+
range.select();
|
279
|
+
}
|
280
|
+
}
|
281
|
+
}, 0);
|
282
|
+
}
|
283
|
+
|
284
|
+
function getCursorInfo(el) {
|
285
|
+
el = $(el)[0];
|
286
|
+
var offset = 0;
|
287
|
+
var length = 0;
|
288
|
+
if ('selectionStart' in el) {
|
289
|
+
offset = el.selectionStart;
|
290
|
+
length = el.selectionEnd - offset;
|
291
|
+
} else if ('selection' in document) {
|
292
|
+
el.focus();
|
293
|
+
var sel = document.selection.createRange();
|
294
|
+
length = document.selection.createRange().text.length;
|
295
|
+
sel.moveStart('character', -el.value.length);
|
296
|
+
offset = sel.text.length - length;
|
297
|
+
}
|
298
|
+
return { offset: offset, length: length };
|
299
|
+
}
|
300
|
+
|
301
|
+
function killEvent(event) {
|
302
|
+
event.preventDefault();
|
303
|
+
event.stopPropagation();
|
304
|
+
}
|
305
|
+
function killEventImmediately(event) {
|
306
|
+
event.preventDefault();
|
307
|
+
event.stopImmediatePropagation();
|
308
|
+
}
|
309
|
+
|
310
|
+
function measureTextWidth(e) {
|
311
|
+
if (!sizer){
|
312
|
+
var style = e[0].currentStyle || window.getComputedStyle(e[0], null);
|
313
|
+
sizer = $(document.createElement("div")).css({
|
314
|
+
position: "absolute",
|
315
|
+
left: "-10000px",
|
316
|
+
top: "-10000px",
|
317
|
+
display: "none",
|
318
|
+
fontSize: style.fontSize,
|
319
|
+
fontFamily: style.fontFamily,
|
320
|
+
fontStyle: style.fontStyle,
|
321
|
+
fontWeight: style.fontWeight,
|
322
|
+
letterSpacing: style.letterSpacing,
|
323
|
+
textTransform: style.textTransform,
|
324
|
+
whiteSpace: "nowrap"
|
325
|
+
});
|
326
|
+
sizer.attr("class","select2-sizer");
|
327
|
+
$("body").append(sizer);
|
328
|
+
}
|
329
|
+
sizer.text(e.val());
|
330
|
+
return sizer.width();
|
331
|
+
}
|
332
|
+
|
333
|
+
function syncCssClasses(dest, src, adapter) {
|
334
|
+
var classes, replacements = [], adapted;
|
335
|
+
|
336
|
+
classes = dest.attr("class");
|
337
|
+
if (classes) {
|
338
|
+
classes = '' + classes; // for IE which returns object
|
339
|
+
$(classes.split(" ")).each2(function() {
|
340
|
+
if (this.indexOf("select2-") === 0) {
|
341
|
+
replacements.push(this);
|
342
|
+
}
|
343
|
+
});
|
344
|
+
}
|
345
|
+
classes = src.attr("class");
|
346
|
+
if (classes) {
|
347
|
+
classes = '' + classes; // for IE which returns object
|
348
|
+
$(classes.split(" ")).each2(function() {
|
349
|
+
if (this.indexOf("select2-") !== 0) {
|
350
|
+
adapted = adapter(this);
|
351
|
+
if (adapted) {
|
352
|
+
replacements.push(this);
|
353
|
+
}
|
354
|
+
}
|
355
|
+
});
|
356
|
+
}
|
357
|
+
dest.attr("class", replacements.join(" "));
|
358
|
+
}
|
359
|
+
|
360
|
+
|
361
|
+
function markMatch(text, term, markup, escapeMarkup) {
|
362
|
+
var match=stripDiacritics(text.toUpperCase()).indexOf(stripDiacritics(term.toUpperCase())),
|
363
|
+
tl=term.length;
|
364
|
+
|
365
|
+
if (match<0) {
|
366
|
+
markup.push(escapeMarkup(text));
|
367
|
+
return;
|
368
|
+
}
|
369
|
+
|
370
|
+
markup.push(escapeMarkup(text.substring(0, match)));
|
371
|
+
markup.push("<span class='select2-match'>");
|
372
|
+
markup.push(escapeMarkup(text.substring(match, match + tl)));
|
373
|
+
markup.push("</span>");
|
374
|
+
markup.push(escapeMarkup(text.substring(match + tl, text.length)));
|
375
|
+
}
|
376
|
+
|
377
|
+
function defaultEscapeMarkup(markup) {
|
378
|
+
var replace_map = {
|
379
|
+
'\\': '\',
|
380
|
+
'&': '&',
|
381
|
+
'<': '<',
|
382
|
+
'>': '>',
|
383
|
+
'"': '"',
|
384
|
+
"'": ''',
|
385
|
+
"/": '/'
|
386
|
+
};
|
387
|
+
|
388
|
+
return String(markup).replace(/[&<>"'\/\\]/g, function (match) {
|
389
|
+
return replace_map[match];
|
390
|
+
});
|
391
|
+
}
|
392
|
+
|
393
|
+
/**
|
394
|
+
* Produces an ajax-based query function
|
395
|
+
*
|
396
|
+
* @param options object containing configuration paramters
|
397
|
+
* @param options.params parameter map for the transport ajax call, can contain such options as cache, jsonpCallback, etc. see $.ajax
|
398
|
+
* @param options.transport function that will be used to execute the ajax request. must be compatible with parameters supported by $.ajax
|
399
|
+
* @param options.url url for the data
|
400
|
+
* @param options.data a function(searchTerm, pageNumber, context) that should return an object containing query string parameters for the above url.
|
401
|
+
* @param options.dataType request data type: ajax, jsonp, other datatatypes supported by jQuery's $.ajax function or the transport function if specified
|
402
|
+
* @param options.quietMillis (optional) milliseconds to wait before making the ajaxRequest, helps debounce the ajax function if invoked too often
|
403
|
+
* @param options.results a function(remoteData, pageNumber) that converts data returned form the remote request to the format expected by Select2.
|
404
|
+
* The expected format is an object containing the following keys:
|
405
|
+
* results array of objects that will be used as choices
|
406
|
+
* more (optional) boolean indicating whether there are more results available
|
407
|
+
* Example: {results:[{id:1, text:'Red'},{id:2, text:'Blue'}], more:true}
|
408
|
+
*/
|
409
|
+
function ajax(options) {
|
410
|
+
var timeout, // current scheduled but not yet executed request
|
411
|
+
handler = null,
|
412
|
+
quietMillis = options.quietMillis || 100,
|
413
|
+
ajaxUrl = options.url,
|
414
|
+
self = this;
|
415
|
+
|
416
|
+
return function (query) {
|
417
|
+
window.clearTimeout(timeout);
|
418
|
+
timeout = window.setTimeout(function () {
|
419
|
+
var data = options.data, // ajax data function
|
420
|
+
url = ajaxUrl, // ajax url string or function
|
421
|
+
transport = options.transport || $.fn.select2.ajaxDefaults.transport,
|
422
|
+
// deprecated - to be removed in 4.0 - use params instead
|
423
|
+
deprecated = {
|
424
|
+
type: options.type || 'GET', // set type of request (GET or POST)
|
425
|
+
cache: options.cache || false,
|
426
|
+
jsonpCallback: options.jsonpCallback||undefined,
|
427
|
+
dataType: options.dataType||"json"
|
428
|
+
},
|
429
|
+
params = $.extend({}, $.fn.select2.ajaxDefaults.params, deprecated);
|
430
|
+
|
431
|
+
data = data ? data.call(self, query.term, query.page, query.context) : null;
|
432
|
+
url = (typeof url === 'function') ? url.call(self, query.term, query.page, query.context) : url;
|
433
|
+
|
434
|
+
if (handler) { handler.abort(); }
|
435
|
+
|
436
|
+
if (options.params) {
|
437
|
+
if ($.isFunction(options.params)) {
|
438
|
+
$.extend(params, options.params.call(self));
|
439
|
+
} else {
|
440
|
+
$.extend(params, options.params);
|
441
|
+
}
|
442
|
+
}
|
443
|
+
|
444
|
+
$.extend(params, {
|
445
|
+
url: url,
|
446
|
+
dataType: options.dataType,
|
447
|
+
data: data,
|
448
|
+
success: function (data) {
|
449
|
+
// TODO - replace query.page with query so users have access to term, page, etc.
|
450
|
+
var results = options.results(data, query.page);
|
451
|
+
query.callback(results);
|
452
|
+
}
|
453
|
+
});
|
454
|
+
handler = transport.call(self, params);
|
455
|
+
}, quietMillis);
|
456
|
+
};
|
457
|
+
}
|
458
|
+
|
459
|
+
/**
|
460
|
+
* Produces a query function that works with a local array
|
461
|
+
*
|
462
|
+
* @param options object containing configuration parameters. The options parameter can either be an array or an
|
463
|
+
* object.
|
464
|
+
*
|
465
|
+
* If the array form is used it is assumed that it contains objects with 'id' and 'text' keys.
|
466
|
+
*
|
467
|
+
* If the object form is used ti is assumed that it contains 'data' and 'text' keys. The 'data' key should contain
|
468
|
+
* an array of objects that will be used as choices. These objects must contain at least an 'id' key. The 'text'
|
469
|
+
* key can either be a String in which case it is expected that each element in the 'data' array has a key with the
|
470
|
+
* value of 'text' which will be used to match choices. Alternatively, text can be a function(item) that can extract
|
471
|
+
* the text.
|
472
|
+
*/
|
473
|
+
function local(options) {
|
474
|
+
var data = options, // data elements
|
475
|
+
dataText,
|
476
|
+
tmp,
|
477
|
+
text = function (item) { return ""+item.text; }; // function used to retrieve the text portion of a data item that is matched against the search
|
478
|
+
|
479
|
+
if ($.isArray(data)) {
|
480
|
+
tmp = data;
|
481
|
+
data = { results: tmp };
|
482
|
+
}
|
483
|
+
|
484
|
+
if ($.isFunction(data) === false) {
|
485
|
+
tmp = data;
|
486
|
+
data = function() { return tmp; };
|
487
|
+
}
|
488
|
+
|
489
|
+
var dataItem = data();
|
490
|
+
if (dataItem.text) {
|
491
|
+
text = dataItem.text;
|
492
|
+
// if text is not a function we assume it to be a key name
|
493
|
+
if (!$.isFunction(text)) {
|
494
|
+
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
|
495
|
+
text = function (item) { return item[dataText]; };
|
496
|
+
}
|
497
|
+
}
|
498
|
+
|
499
|
+
return function (query) {
|
500
|
+
var t = query.term, filtered = { results: [] }, process;
|
501
|
+
if (t === "") {
|
502
|
+
query.callback(data());
|
503
|
+
return;
|
504
|
+
}
|
505
|
+
|
506
|
+
process = function(datum, collection) {
|
507
|
+
var group, attr;
|
508
|
+
datum = datum[0];
|
509
|
+
if (datum.children) {
|
510
|
+
group = {};
|
511
|
+
for (attr in datum) {
|
512
|
+
if (datum.hasOwnProperty(attr)) group[attr]=datum[attr];
|
513
|
+
}
|
514
|
+
group.children=[];
|
515
|
+
$(datum.children).each2(function(i, childDatum) { process(childDatum, group.children); });
|
516
|
+
if (group.children.length || query.matcher(t, text(group), datum)) {
|
517
|
+
collection.push(group);
|
518
|
+
}
|
519
|
+
} else {
|
520
|
+
if (query.matcher(t, text(datum), datum)) {
|
521
|
+
collection.push(datum);
|
522
|
+
}
|
523
|
+
}
|
524
|
+
};
|
525
|
+
|
526
|
+
$(data().results).each2(function(i, datum) { process(datum, filtered.results); });
|
527
|
+
query.callback(filtered);
|
528
|
+
};
|
529
|
+
}
|
530
|
+
|
531
|
+
// TODO javadoc
|
532
|
+
function tags(data) {
|
533
|
+
var isFunc = $.isFunction(data);
|
534
|
+
return function (query) {
|
535
|
+
var t = query.term, filtered = {results: []};
|
536
|
+
$(isFunc ? data() : data).each(function () {
|
537
|
+
var isObject = this.text !== undefined,
|
538
|
+
text = isObject ? this.text : this;
|
539
|
+
if (t === "" || query.matcher(t, text)) {
|
540
|
+
filtered.results.push(isObject ? this : {id: this, text: this});
|
541
|
+
}
|
542
|
+
});
|
543
|
+
query.callback(filtered);
|
544
|
+
};
|
545
|
+
}
|
546
|
+
|
547
|
+
/**
|
548
|
+
* Checks if the formatter function should be used.
|
549
|
+
*
|
550
|
+
* Throws an error if it is not a function. Returns true if it should be used,
|
551
|
+
* false if no formatting should be performed.
|
552
|
+
*
|
553
|
+
* @param formatter
|
554
|
+
*/
|
555
|
+
function checkFormatter(formatter, formatterName) {
|
556
|
+
if ($.isFunction(formatter)) return true;
|
557
|
+
if (!formatter) return false;
|
558
|
+
throw new Error(formatterName +" must be a function or a falsy value");
|
559
|
+
}
|
560
|
+
|
561
|
+
function evaluate(val) {
|
562
|
+
return $.isFunction(val) ? val() : val;
|
563
|
+
}
|
564
|
+
|
565
|
+
function countResults(results) {
|
566
|
+
var count = 0;
|
567
|
+
$.each(results, function(i, item) {
|
568
|
+
if (item.children) {
|
569
|
+
count += countResults(item.children);
|
570
|
+
} else {
|
571
|
+
count++;
|
572
|
+
}
|
573
|
+
});
|
574
|
+
return count;
|
575
|
+
}
|
576
|
+
|
577
|
+
/**
|
578
|
+
* Default tokenizer. This function uses breaks the input on substring match of any string from the
|
579
|
+
* opts.tokenSeparators array and uses opts.createSearchChoice to create the choice object. Both of those
|
580
|
+
* two options have to be defined in order for the tokenizer to work.
|
581
|
+
*
|
582
|
+
* @param input text user has typed so far or pasted into the search field
|
583
|
+
* @param selection currently selected choices
|
584
|
+
* @param selectCallback function(choice) callback tho add the choice to selection
|
585
|
+
* @param opts select2's opts
|
586
|
+
* @return undefined/null to leave the current input unchanged, or a string to change the input to the returned value
|
587
|
+
*/
|
588
|
+
function defaultTokenizer(input, selection, selectCallback, opts) {
|
589
|
+
var original = input, // store the original so we can compare and know if we need to tell the search to update its text
|
590
|
+
dupe = false, // check for whether a token we extracted represents a duplicate selected choice
|
591
|
+
token, // token
|
592
|
+
index, // position at which the separator was found
|
593
|
+
i, l, // looping variables
|
594
|
+
separator; // the matched separator
|
595
|
+
|
596
|
+
if (!opts.createSearchChoice || !opts.tokenSeparators || opts.tokenSeparators.length < 1) return undefined;
|
597
|
+
|
598
|
+
while (true) {
|
599
|
+
index = -1;
|
600
|
+
|
601
|
+
for (i = 0, l = opts.tokenSeparators.length; i < l; i++) {
|
602
|
+
separator = opts.tokenSeparators[i];
|
603
|
+
index = input.indexOf(separator);
|
604
|
+
if (index >= 0) break;
|
605
|
+
}
|
606
|
+
|
607
|
+
if (index < 0) break; // did not find any token separator in the input string, bail
|
608
|
+
|
609
|
+
token = input.substring(0, index);
|
610
|
+
input = input.substring(index + separator.length);
|
611
|
+
|
612
|
+
if (token.length > 0) {
|
613
|
+
token = opts.createSearchChoice.call(this, token, selection);
|
614
|
+
if (token !== undefined && token !== null && opts.id(token) !== undefined && opts.id(token) !== null) {
|
615
|
+
dupe = false;
|
616
|
+
for (i = 0, l = selection.length; i < l; i++) {
|
617
|
+
if (equal(opts.id(token), opts.id(selection[i]))) {
|
618
|
+
dupe = true; break;
|
619
|
+
}
|
620
|
+
}
|
621
|
+
|
622
|
+
if (!dupe) selectCallback(token);
|
623
|
+
}
|
624
|
+
}
|
625
|
+
}
|
626
|
+
|
627
|
+
if (original!==input) return input;
|
628
|
+
}
|
629
|
+
|
630
|
+
/**
|
631
|
+
* Creates a new class
|
632
|
+
*
|
633
|
+
* @param superClass
|
634
|
+
* @param methods
|
635
|
+
*/
|
636
|
+
function clazz(SuperClass, methods) {
|
637
|
+
var constructor = function () {};
|
638
|
+
constructor.prototype = new SuperClass;
|
639
|
+
constructor.prototype.constructor = constructor;
|
640
|
+
constructor.prototype.parent = SuperClass.prototype;
|
641
|
+
constructor.prototype = $.extend(constructor.prototype, methods);
|
642
|
+
return constructor;
|
643
|
+
}
|
644
|
+
|
645
|
+
AbstractSelect2 = clazz(Object, {
|
646
|
+
|
647
|
+
// abstract
|
648
|
+
bind: function (func) {
|
649
|
+
var self = this;
|
650
|
+
return function () {
|
651
|
+
func.apply(self, arguments);
|
652
|
+
};
|
653
|
+
},
|
654
|
+
|
655
|
+
// abstract
|
656
|
+
init: function (opts) {
|
657
|
+
var results, search, resultsSelector = ".select2-results", disabled, readonly;
|
658
|
+
|
659
|
+
// prepare options
|
660
|
+
this.opts = opts = this.prepareOpts(opts);
|
661
|
+
|
662
|
+
this.id=opts.id;
|
663
|
+
|
664
|
+
// destroy if called on an existing component
|
665
|
+
if (opts.element.data("select2") !== undefined &&
|
666
|
+
opts.element.data("select2") !== null) {
|
667
|
+
opts.element.data("select2").destroy();
|
668
|
+
}
|
669
|
+
|
670
|
+
this.container = this.createContainer();
|
671
|
+
|
672
|
+
this.containerId="s2id_"+(opts.element.attr("id") || "autogen"+nextUid());
|
673
|
+
this.containerSelector="#"+this.containerId.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1');
|
674
|
+
this.container.attr("id", this.containerId);
|
675
|
+
|
676
|
+
// cache the body so future lookups are cheap
|
677
|
+
this.body = thunk(function() { return opts.element.closest("body"); });
|
678
|
+
|
679
|
+
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
|
680
|
+
|
681
|
+
this.container.attr("style", opts.element.attr("style"));
|
682
|
+
this.container.css(evaluate(opts.containerCss));
|
683
|
+
this.container.addClass(evaluate(opts.containerCssClass));
|
684
|
+
|
685
|
+
this.elementTabIndex = this.opts.element.attr("tabindex");
|
686
|
+
|
687
|
+
// swap container for the element
|
688
|
+
this.opts.element
|
689
|
+
.data("select2", this)
|
690
|
+
.attr("tabindex", "-1")
|
691
|
+
.before(this.container);
|
692
|
+
this.container.data("select2", this);
|
693
|
+
|
694
|
+
this.dropdown = this.container.find(".select2-drop");
|
695
|
+
this.dropdown.addClass(evaluate(opts.dropdownCssClass));
|
696
|
+
this.dropdown.data("select2", this);
|
697
|
+
|
698
|
+
this.results = results = this.container.find(resultsSelector);
|
699
|
+
this.search = search = this.container.find("input.select2-input");
|
700
|
+
|
701
|
+
this.queryCount = 0;
|
702
|
+
this.resultsPage = 0;
|
703
|
+
this.context = null;
|
704
|
+
|
705
|
+
// initialize the container
|
706
|
+
this.initContainer();
|
707
|
+
|
708
|
+
installFilteredMouseMove(this.results);
|
709
|
+
this.dropdown.on("mousemove-filtered touchstart touchmove touchend", resultsSelector, this.bind(this.highlightUnderEvent));
|
710
|
+
|
711
|
+
installDebouncedScroll(80, this.results);
|
712
|
+
this.dropdown.on("scroll-debounced", resultsSelector, this.bind(this.loadMoreIfNeeded));
|
713
|
+
|
714
|
+
// do not propagate change event from the search field out of the component
|
715
|
+
$(this.container).on("change", ".select2-input", function(e) {e.stopPropagation();});
|
716
|
+
$(this.dropdown).on("change", ".select2-input", function(e) {e.stopPropagation();});
|
717
|
+
|
718
|
+
// if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel
|
719
|
+
if ($.fn.mousewheel) {
|
720
|
+
results.mousewheel(function (e, delta, deltaX, deltaY) {
|
721
|
+
var top = results.scrollTop(), height;
|
722
|
+
if (deltaY > 0 && top - deltaY <= 0) {
|
723
|
+
results.scrollTop(0);
|
724
|
+
killEvent(e);
|
725
|
+
} else if (deltaY < 0 && results.get(0).scrollHeight - results.scrollTop() + deltaY <= results.height()) {
|
726
|
+
results.scrollTop(results.get(0).scrollHeight - results.height());
|
727
|
+
killEvent(e);
|
728
|
+
}
|
729
|
+
});
|
730
|
+
}
|
731
|
+
|
732
|
+
installKeyUpChangeEvent(search);
|
733
|
+
search.on("keyup-change input paste", this.bind(this.updateResults));
|
734
|
+
search.on("focus", function () { search.addClass("select2-focused"); });
|
735
|
+
search.on("blur", function () { search.removeClass("select2-focused");});
|
736
|
+
|
737
|
+
this.dropdown.on("mouseup", resultsSelector, this.bind(function (e) {
|
738
|
+
if ($(e.target).closest(".select2-result-selectable").length > 0) {
|
739
|
+
this.highlightUnderEvent(e);
|
740
|
+
this.selectHighlighted(e);
|
741
|
+
}
|
742
|
+
}));
|
743
|
+
|
744
|
+
// trap all mouse events from leaving the dropdown. sometimes there may be a modal that is listening
|
745
|
+
// for mouse events outside of itself so it can close itself. since the dropdown is now outside the select2's
|
746
|
+
// dom it will trigger the popup close, which is not what we want
|
747
|
+
this.dropdown.on("click mouseup mousedown", function (e) { e.stopPropagation(); });
|
748
|
+
|
749
|
+
if ($.isFunction(this.opts.initSelection)) {
|
750
|
+
// initialize selection based on the current value of the source element
|
751
|
+
this.initSelection();
|
752
|
+
|
753
|
+
// if the user has provided a function that can set selection based on the value of the source element
|
754
|
+
// we monitor the change event on the element and trigger it, allowing for two way synchronization
|
755
|
+
this.monitorSource();
|
756
|
+
}
|
757
|
+
|
758
|
+
if (opts.maximumInputLength !== null) {
|
759
|
+
this.search.attr("maxlength", opts.maximumInputLength);
|
760
|
+
}
|
761
|
+
|
762
|
+
var disabled = opts.element.prop("disabled");
|
763
|
+
if (disabled === undefined) disabled = false;
|
764
|
+
this.enable(!disabled);
|
765
|
+
|
766
|
+
var readonly = opts.element.prop("readonly");
|
767
|
+
if (readonly === undefined) readonly = false;
|
768
|
+
this.readonly(readonly);
|
769
|
+
|
770
|
+
// Calculate size of scrollbar
|
771
|
+
scrollBarDimensions = scrollBarDimensions || measureScrollbar();
|
772
|
+
|
773
|
+
this.autofocus = opts.element.prop("autofocus");
|
774
|
+
opts.element.prop("autofocus", false);
|
775
|
+
if (this.autofocus) this.focus();
|
776
|
+
},
|
777
|
+
|
778
|
+
// abstract
|
779
|
+
destroy: function () {
|
780
|
+
var element=this.opts.element, select2 = element.data("select2");
|
781
|
+
|
782
|
+
if (this.propertyObserver) { delete this.propertyObserver; this.propertyObserver = null; }
|
783
|
+
|
784
|
+
if (select2 !== undefined) {
|
785
|
+
select2.container.remove();
|
786
|
+
select2.dropdown.remove();
|
787
|
+
element
|
788
|
+
.removeClass("select2-offscreen")
|
789
|
+
.removeData("select2")
|
790
|
+
.off(".select2")
|
791
|
+
.prop("autofocus", this.autofocus || false);
|
792
|
+
if (this.elementTabIndex) {
|
793
|
+
element.attr({tabindex: this.elementTabIndex});
|
794
|
+
} else {
|
795
|
+
element.removeAttr("tabindex");
|
796
|
+
}
|
797
|
+
element.show();
|
798
|
+
}
|
799
|
+
},
|
800
|
+
|
801
|
+
// abstract
|
802
|
+
optionToData: function(element) {
|
803
|
+
if (element.is("option")) {
|
804
|
+
return {
|
805
|
+
id:element.prop("value"),
|
806
|
+
text:element.text(),
|
807
|
+
element: element.get(),
|
808
|
+
css: element.attr("class"),
|
809
|
+
disabled: element.prop("disabled"),
|
810
|
+
locked: equal(element.attr("locked"), "locked") || equal(element.data("locked"), true)
|
811
|
+
};
|
812
|
+
} else if (element.is("optgroup")) {
|
813
|
+
return {
|
814
|
+
text:element.attr("label"),
|
815
|
+
children:[],
|
816
|
+
element: element.get(),
|
817
|
+
css: element.attr("class")
|
818
|
+
};
|
819
|
+
}
|
820
|
+
},
|
821
|
+
|
822
|
+
// abstract
|
823
|
+
prepareOpts: function (opts) {
|
824
|
+
var element, select, idKey, ajaxUrl, self = this;
|
825
|
+
|
826
|
+
element = opts.element;
|
827
|
+
|
828
|
+
if (element.get(0).tagName.toLowerCase() === "select") {
|
829
|
+
this.select = select = opts.element;
|
830
|
+
}
|
831
|
+
|
832
|
+
if (select) {
|
833
|
+
// these options are not allowed when attached to a select because they are picked up off the element itself
|
834
|
+
$.each(["id", "multiple", "ajax", "query", "createSearchChoice", "initSelection", "data", "tags"], function () {
|
835
|
+
if (this in opts) {
|
836
|
+
throw new Error("Option '" + this + "' is not allowed for Select2 when attached to a <select> element.");
|
837
|
+
}
|
838
|
+
});
|
839
|
+
}
|
840
|
+
|
841
|
+
opts = $.extend({}, {
|
842
|
+
populateResults: function(container, results, query) {
|
843
|
+
var populate, data, result, children, id=this.opts.id;
|
844
|
+
|
845
|
+
populate=function(results, container, depth) {
|
846
|
+
|
847
|
+
var i, l, result, selectable, disabled, compound, node, label, innerContainer, formatted;
|
848
|
+
|
849
|
+
results = opts.sortResults(results, container, query);
|
850
|
+
|
851
|
+
for (i = 0, l = results.length; i < l; i = i + 1) {
|
852
|
+
|
853
|
+
result=results[i];
|
854
|
+
|
855
|
+
disabled = (result.disabled === true);
|
856
|
+
selectable = (!disabled) && (id(result) !== undefined);
|
857
|
+
|
858
|
+
compound=result.children && result.children.length > 0;
|
859
|
+
|
860
|
+
node=$("<li></li>");
|
861
|
+
node.addClass("select2-results-dept-"+depth);
|
862
|
+
node.addClass("select2-result");
|
863
|
+
node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
|
864
|
+
if (disabled) { node.addClass("select2-disabled"); }
|
865
|
+
if (compound) { node.addClass("select2-result-with-children"); }
|
866
|
+
node.addClass(self.opts.formatResultCssClass(result));
|
867
|
+
|
868
|
+
label=$(document.createElement("div"));
|
869
|
+
label.addClass("select2-result-label");
|
870
|
+
|
871
|
+
formatted=opts.formatResult(result, label, query, self.opts.escapeMarkup);
|
872
|
+
if (formatted!==undefined) {
|
873
|
+
label.html(formatted);
|
874
|
+
}
|
875
|
+
|
876
|
+
node.append(label);
|
877
|
+
|
878
|
+
if (compound) {
|
879
|
+
|
880
|
+
innerContainer=$("<ul></ul>");
|
881
|
+
innerContainer.addClass("select2-result-sub");
|
882
|
+
populate(result.children, innerContainer, depth+1);
|
883
|
+
node.append(innerContainer);
|
884
|
+
}
|
885
|
+
|
886
|
+
node.data("select2-data", result);
|
887
|
+
container.append(node);
|
888
|
+
}
|
889
|
+
};
|
890
|
+
|
891
|
+
populate(results, container, 0);
|
892
|
+
}
|
893
|
+
}, $.fn.select2.defaults, opts);
|
894
|
+
|
895
|
+
if (typeof(opts.id) !== "function") {
|
896
|
+
idKey = opts.id;
|
897
|
+
opts.id = function (e) { return e[idKey]; };
|
898
|
+
}
|
899
|
+
|
900
|
+
if ($.isArray(opts.element.data("select2Tags"))) {
|
901
|
+
if ("tags" in opts) {
|
902
|
+
throw "tags specified as both an attribute 'data-select2-tags' and in options of Select2 " + opts.element.attr("id");
|
903
|
+
}
|
904
|
+
opts.tags=opts.element.data("select2Tags");
|
905
|
+
}
|
906
|
+
|
907
|
+
if (select) {
|
908
|
+
opts.query = this.bind(function (query) {
|
909
|
+
var data = { results: [], more: false },
|
910
|
+
term = query.term,
|
911
|
+
children, placeholderOption, process;
|
912
|
+
|
913
|
+
process=function(element, collection) {
|
914
|
+
var group;
|
915
|
+
if (element.is("option")) {
|
916
|
+
if (query.matcher(term, element.text(), element)) {
|
917
|
+
collection.push(self.optionToData(element));
|
918
|
+
}
|
919
|
+
} else if (element.is("optgroup")) {
|
920
|
+
group=self.optionToData(element);
|
921
|
+
element.children().each2(function(i, elm) { process(elm, group.children); });
|
922
|
+
if (group.children.length>0) {
|
923
|
+
collection.push(group);
|
924
|
+
}
|
925
|
+
}
|
926
|
+
};
|
927
|
+
|
928
|
+
children=element.children();
|
929
|
+
|
930
|
+
// ignore the placeholder option if there is one
|
931
|
+
if (this.getPlaceholder() !== undefined && children.length > 0) {
|
932
|
+
placeholderOption = this.getPlaceholderOption();
|
933
|
+
if (placeholderOption) {
|
934
|
+
children=children.not(placeholderOption);
|
935
|
+
}
|
936
|
+
}
|
937
|
+
|
938
|
+
children.each2(function(i, elm) { process(elm, data.results); });
|
939
|
+
|
940
|
+
query.callback(data);
|
941
|
+
});
|
942
|
+
// this is needed because inside val() we construct choices from options and there id is hardcoded
|
943
|
+
opts.id=function(e) { return e.id; };
|
944
|
+
opts.formatResultCssClass = function(data) { return data.css; };
|
945
|
+
} else {
|
946
|
+
if (!("query" in opts)) {
|
947
|
+
|
948
|
+
if ("ajax" in opts) {
|
949
|
+
ajaxUrl = opts.element.data("ajax-url");
|
950
|
+
if (ajaxUrl && ajaxUrl.length > 0) {
|
951
|
+
opts.ajax.url = ajaxUrl;
|
952
|
+
}
|
953
|
+
opts.query = ajax.call(opts.element, opts.ajax);
|
954
|
+
} else if ("data" in opts) {
|
955
|
+
opts.query = local(opts.data);
|
956
|
+
} else if ("tags" in opts) {
|
957
|
+
opts.query = tags(opts.tags);
|
958
|
+
if (opts.createSearchChoice === undefined) {
|
959
|
+
opts.createSearchChoice = function (term) { return {id: $.trim(term), text: $.trim(term)}; };
|
960
|
+
}
|
961
|
+
if (opts.initSelection === undefined) {
|
962
|
+
opts.initSelection = function (element, callback) {
|
963
|
+
var data = [];
|
964
|
+
$(splitVal(element.val(), opts.separator)).each(function () {
|
965
|
+
var id = this, text = this, tags=opts.tags;
|
966
|
+
if ($.isFunction(tags)) tags=tags();
|
967
|
+
$(tags).each(function() { if (equal(this.id, id)) { text = this.text; return false; } });
|
968
|
+
data.push({id: id, text: text});
|
969
|
+
});
|
970
|
+
|
971
|
+
callback(data);
|
972
|
+
};
|
973
|
+
}
|
974
|
+
}
|
975
|
+
}
|
976
|
+
}
|
977
|
+
if (typeof(opts.query) !== "function") {
|
978
|
+
throw "query function not defined for Select2 " + opts.element.attr("id");
|
979
|
+
}
|
980
|
+
|
981
|
+
return opts;
|
982
|
+
},
|
983
|
+
|
984
|
+
/**
|
985
|
+
* Monitor the original element for changes and update select2 accordingly
|
986
|
+
*/
|
987
|
+
// abstract
|
988
|
+
monitorSource: function () {
|
989
|
+
var el = this.opts.element, sync;
|
990
|
+
|
991
|
+
el.on("change.select2", this.bind(function (e) {
|
992
|
+
if (this.opts.element.data("select2-change-triggered") !== true) {
|
993
|
+
this.initSelection();
|
994
|
+
}
|
995
|
+
}));
|
996
|
+
|
997
|
+
sync = this.bind(function () {
|
998
|
+
|
999
|
+
var enabled, readonly, self = this;
|
1000
|
+
|
1001
|
+
// sync enabled state
|
1002
|
+
var disabled = el.prop("disabled");
|
1003
|
+
if (disabled === undefined) disabled = false;
|
1004
|
+
this.enable(!disabled);
|
1005
|
+
|
1006
|
+
var readonly = el.prop("readonly");
|
1007
|
+
if (readonly === undefined) readonly = false;
|
1008
|
+
this.readonly(readonly);
|
1009
|
+
|
1010
|
+
syncCssClasses(this.container, this.opts.element, this.opts.adaptContainerCssClass);
|
1011
|
+
this.container.addClass(evaluate(this.opts.containerCssClass));
|
1012
|
+
|
1013
|
+
syncCssClasses(this.dropdown, this.opts.element, this.opts.adaptDropdownCssClass);
|
1014
|
+
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass));
|
1015
|
+
|
1016
|
+
});
|
1017
|
+
|
1018
|
+
// mozilla and IE
|
1019
|
+
el.on("propertychange.select2 DOMAttrModified.select2", sync);
|
1020
|
+
|
1021
|
+
|
1022
|
+
// hold onto a reference of the callback to work around a chromium bug
|
1023
|
+
if (this.mutationCallback === undefined) {
|
1024
|
+
this.mutationCallback = function (mutations) {
|
1025
|
+
mutations.forEach(sync);
|
1026
|
+
}
|
1027
|
+
}
|
1028
|
+
|
1029
|
+
// safari and chrome
|
1030
|
+
if (typeof WebKitMutationObserver !== "undefined") {
|
1031
|
+
if (this.propertyObserver) { delete this.propertyObserver; this.propertyObserver = null; }
|
1032
|
+
this.propertyObserver = new WebKitMutationObserver(this.mutationCallback);
|
1033
|
+
this.propertyObserver.observe(el.get(0), { attributes:true, subtree:false });
|
1034
|
+
}
|
1035
|
+
},
|
1036
|
+
|
1037
|
+
// abstract
|
1038
|
+
triggerSelect: function(data) {
|
1039
|
+
var evt = $.Event("select2-selecting", { val: this.id(data), object: data });
|
1040
|
+
this.opts.element.trigger(evt);
|
1041
|
+
return !evt.isDefaultPrevented();
|
1042
|
+
},
|
1043
|
+
|
1044
|
+
/**
|
1045
|
+
* Triggers the change event on the source element
|
1046
|
+
*/
|
1047
|
+
// abstract
|
1048
|
+
triggerChange: function (details) {
|
1049
|
+
|
1050
|
+
details = details || {};
|
1051
|
+
details= $.extend({}, details, { type: "change", val: this.val() });
|
1052
|
+
// prevents recursive triggering
|
1053
|
+
this.opts.element.data("select2-change-triggered", true);
|
1054
|
+
this.opts.element.trigger(details);
|
1055
|
+
this.opts.element.data("select2-change-triggered", false);
|
1056
|
+
|
1057
|
+
// some validation frameworks ignore the change event and listen instead to keyup, click for selects
|
1058
|
+
// so here we trigger the click event manually
|
1059
|
+
this.opts.element.click();
|
1060
|
+
|
1061
|
+
// ValidationEngine ignorea the change event and listens instead to blur
|
1062
|
+
// so here we trigger the blur event manually if so desired
|
1063
|
+
if (this.opts.blurOnChange)
|
1064
|
+
this.opts.element.blur();
|
1065
|
+
},
|
1066
|
+
|
1067
|
+
//abstract
|
1068
|
+
isInterfaceEnabled: function()
|
1069
|
+
{
|
1070
|
+
return this.enabledInterface === true;
|
1071
|
+
},
|
1072
|
+
|
1073
|
+
// abstract
|
1074
|
+
enableInterface: function() {
|
1075
|
+
var enabled = this._enabled && !this._readonly,
|
1076
|
+
disabled = !enabled;
|
1077
|
+
|
1078
|
+
if (enabled === this.enabledInterface) return false;
|
1079
|
+
|
1080
|
+
this.container.toggleClass("select2-container-disabled", disabled);
|
1081
|
+
this.close();
|
1082
|
+
this.enabledInterface = enabled;
|
1083
|
+
|
1084
|
+
return true;
|
1085
|
+
},
|
1086
|
+
|
1087
|
+
// abstract
|
1088
|
+
enable: function(enabled) {
|
1089
|
+
if (enabled === undefined) enabled = true;
|
1090
|
+
if (this._enabled === enabled) return false;
|
1091
|
+
this._enabled = enabled;
|
1092
|
+
|
1093
|
+
this.opts.element.prop("disabled", !enabled);
|
1094
|
+
this.enableInterface();
|
1095
|
+
return true;
|
1096
|
+
},
|
1097
|
+
|
1098
|
+
// abstract
|
1099
|
+
readonly: function(enabled) {
|
1100
|
+
if (enabled === undefined) enabled = false;
|
1101
|
+
if (this._readonly === enabled) return false;
|
1102
|
+
this._readonly = enabled;
|
1103
|
+
|
1104
|
+
this.opts.element.prop("readonly", enabled);
|
1105
|
+
this.enableInterface();
|
1106
|
+
return true;
|
1107
|
+
},
|
1108
|
+
|
1109
|
+
// abstract
|
1110
|
+
opened: function () {
|
1111
|
+
return this.container.hasClass("select2-dropdown-open");
|
1112
|
+
},
|
1113
|
+
|
1114
|
+
// abstract
|
1115
|
+
positionDropdown: function() {
|
1116
|
+
var $dropdown = this.dropdown,
|
1117
|
+
offset = this.container.offset(),
|
1118
|
+
height = this.container.outerHeight(false),
|
1119
|
+
width = this.container.outerWidth(false),
|
1120
|
+
dropHeight = $dropdown.outerHeight(false),
|
1121
|
+
viewPortRight = $(window).scrollLeft() + $(window).width(),
|
1122
|
+
viewportBottom = $(window).scrollTop() + $(window).height(),
|
1123
|
+
dropTop = offset.top + height,
|
1124
|
+
dropLeft = offset.left,
|
1125
|
+
enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
|
1126
|
+
enoughRoomAbove = (offset.top - dropHeight) >= this.body().scrollTop(),
|
1127
|
+
dropWidth = $dropdown.outerWidth(false),
|
1128
|
+
enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight,
|
1129
|
+
aboveNow = $dropdown.hasClass("select2-drop-above"),
|
1130
|
+
bodyOffset,
|
1131
|
+
above,
|
1132
|
+
css,
|
1133
|
+
resultsListNode;
|
1134
|
+
|
1135
|
+
if (this.opts.dropdownAutoWidth) {
|
1136
|
+
resultsListNode = $('.select2-results', $dropdown)[0];
|
1137
|
+
$dropdown.addClass('select2-drop-auto-width');
|
1138
|
+
$dropdown.css('width', '');
|
1139
|
+
// Add scrollbar width to dropdown if vertical scrollbar is present
|
1140
|
+
dropWidth = $dropdown.outerWidth(false) + (resultsListNode.scrollHeight === resultsListNode.clientHeight ? 0 : scrollBarDimensions.width);
|
1141
|
+
dropWidth > width ? width = dropWidth : dropWidth = width;
|
1142
|
+
enoughRoomOnRight = dropLeft + dropWidth <= viewPortRight;
|
1143
|
+
}
|
1144
|
+
else {
|
1145
|
+
this.container.removeClass('select2-drop-auto-width');
|
1146
|
+
}
|
1147
|
+
|
1148
|
+
//console.log("below/ droptop:", dropTop, "dropHeight", dropHeight, "sum", (dropTop+dropHeight)+" viewport bottom", viewportBottom, "enough?", enoughRoomBelow);
|
1149
|
+
//console.log("above/ offset.top", offset.top, "dropHeight", dropHeight, "top", (offset.top-dropHeight), "scrollTop", this.body().scrollTop(), "enough?", enoughRoomAbove);
|
1150
|
+
|
1151
|
+
// fix positioning when body has an offset and is not position: static
|
1152
|
+
if (this.body().css('position') !== 'static') {
|
1153
|
+
bodyOffset = this.body().offset();
|
1154
|
+
dropTop -= bodyOffset.top;
|
1155
|
+
dropLeft -= bodyOffset.left;
|
1156
|
+
}
|
1157
|
+
|
1158
|
+
// always prefer the current above/below alignment, unless there is not enough room
|
1159
|
+
if (aboveNow) {
|
1160
|
+
above = true;
|
1161
|
+
if (!enoughRoomAbove && enoughRoomBelow) above = false;
|
1162
|
+
} else {
|
1163
|
+
above = false;
|
1164
|
+
if (!enoughRoomBelow && enoughRoomAbove) above = true;
|
1165
|
+
}
|
1166
|
+
|
1167
|
+
if (!enoughRoomOnRight) {
|
1168
|
+
dropLeft = offset.left + width - dropWidth;
|
1169
|
+
}
|
1170
|
+
|
1171
|
+
if (above) {
|
1172
|
+
dropTop = offset.top - dropHeight;
|
1173
|
+
this.container.addClass("select2-drop-above");
|
1174
|
+
$dropdown.addClass("select2-drop-above");
|
1175
|
+
}
|
1176
|
+
else {
|
1177
|
+
this.container.removeClass("select2-drop-above");
|
1178
|
+
$dropdown.removeClass("select2-drop-above");
|
1179
|
+
}
|
1180
|
+
|
1181
|
+
css = $.extend({
|
1182
|
+
top: dropTop,
|
1183
|
+
left: dropLeft,
|
1184
|
+
width: width
|
1185
|
+
}, evaluate(this.opts.dropdownCss));
|
1186
|
+
|
1187
|
+
$dropdown.css(css);
|
1188
|
+
},
|
1189
|
+
|
1190
|
+
// abstract
|
1191
|
+
shouldOpen: function() {
|
1192
|
+
var event;
|
1193
|
+
|
1194
|
+
if (this.opened()) return false;
|
1195
|
+
|
1196
|
+
if (this._enabled === false || this._readonly === true) return false;
|
1197
|
+
|
1198
|
+
event = $.Event("select2-opening");
|
1199
|
+
this.opts.element.trigger(event);
|
1200
|
+
return !event.isDefaultPrevented();
|
1201
|
+
},
|
1202
|
+
|
1203
|
+
// abstract
|
1204
|
+
clearDropdownAlignmentPreference: function() {
|
1205
|
+
// clear the classes used to figure out the preference of where the dropdown should be opened
|
1206
|
+
this.container.removeClass("select2-drop-above");
|
1207
|
+
this.dropdown.removeClass("select2-drop-above");
|
1208
|
+
},
|
1209
|
+
|
1210
|
+
/**
|
1211
|
+
* Opens the dropdown
|
1212
|
+
*
|
1213
|
+
* @return {Boolean} whether or not dropdown was opened. This method will return false if, for example,
|
1214
|
+
* the dropdown is already open, or if the 'open' event listener on the element called preventDefault().
|
1215
|
+
*/
|
1216
|
+
// abstract
|
1217
|
+
open: function () {
|
1218
|
+
|
1219
|
+
if (!this.shouldOpen()) return false;
|
1220
|
+
|
1221
|
+
this.opening();
|
1222
|
+
|
1223
|
+
return true;
|
1224
|
+
},
|
1225
|
+
|
1226
|
+
/**
|
1227
|
+
* Performs the opening of the dropdown
|
1228
|
+
*/
|
1229
|
+
// abstract
|
1230
|
+
opening: function() {
|
1231
|
+
var cid = this.containerId,
|
1232
|
+
scroll = "scroll." + cid,
|
1233
|
+
resize = "resize."+cid,
|
1234
|
+
orient = "orientationchange."+cid,
|
1235
|
+
mask, maskCss;
|
1236
|
+
|
1237
|
+
this.container.addClass("select2-dropdown-open").addClass("select2-container-active");
|
1238
|
+
|
1239
|
+
this.clearDropdownAlignmentPreference();
|
1240
|
+
|
1241
|
+
if(this.dropdown[0] !== this.body().children().last()[0]) {
|
1242
|
+
this.dropdown.detach().appendTo(this.body());
|
1243
|
+
}
|
1244
|
+
|
1245
|
+
// create the dropdown mask if doesnt already exist
|
1246
|
+
mask = $("#select2-drop-mask");
|
1247
|
+
if (mask.length == 0) {
|
1248
|
+
mask = $(document.createElement("div"));
|
1249
|
+
mask.attr("id","select2-drop-mask").attr("class","select2-drop-mask");
|
1250
|
+
mask.hide();
|
1251
|
+
mask.appendTo(this.body());
|
1252
|
+
mask.on("mousedown touchstart click", function (e) {
|
1253
|
+
var dropdown = $("#select2-drop"), self;
|
1254
|
+
if (dropdown.length > 0) {
|
1255
|
+
self=dropdown.data("select2");
|
1256
|
+
if (self.opts.selectOnBlur) {
|
1257
|
+
self.selectHighlighted({noFocus: true});
|
1258
|
+
}
|
1259
|
+
self.close();
|
1260
|
+
e.preventDefault();
|
1261
|
+
e.stopPropagation();
|
1262
|
+
}
|
1263
|
+
});
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
// ensure the mask is always right before the dropdown
|
1267
|
+
if (this.dropdown.prev()[0] !== mask[0]) {
|
1268
|
+
this.dropdown.before(mask);
|
1269
|
+
}
|
1270
|
+
|
1271
|
+
// move the global id to the correct dropdown
|
1272
|
+
$("#select2-drop").removeAttr("id");
|
1273
|
+
this.dropdown.attr("id", "select2-drop");
|
1274
|
+
|
1275
|
+
// show the elements
|
1276
|
+
maskCss=_makeMaskCss();
|
1277
|
+
|
1278
|
+
mask.css(maskCss).show();
|
1279
|
+
|
1280
|
+
this.dropdown.show();
|
1281
|
+
this.positionDropdown();
|
1282
|
+
|
1283
|
+
this.dropdown.addClass("select2-drop-active");
|
1284
|
+
|
1285
|
+
// attach listeners to events that can change the position of the container and thus require
|
1286
|
+
// the position of the dropdown to be updated as well so it does not come unglued from the container
|
1287
|
+
var that = this;
|
1288
|
+
this.container.parents().add(window).each(function () {
|
1289
|
+
$(this).on(resize+" "+scroll+" "+orient, function (e) {
|
1290
|
+
var maskCss=_makeMaskCss();
|
1291
|
+
$("#select2-drop-mask").css(maskCss);
|
1292
|
+
that.positionDropdown();
|
1293
|
+
});
|
1294
|
+
});
|
1295
|
+
|
1296
|
+
function _makeMaskCss() {
|
1297
|
+
return {
|
1298
|
+
width : Math.max(document.documentElement.scrollWidth, $(window).width()),
|
1299
|
+
height : Math.max(document.documentElement.scrollHeight, $(window).height())
|
1300
|
+
}
|
1301
|
+
}
|
1302
|
+
},
|
1303
|
+
|
1304
|
+
// abstract
|
1305
|
+
close: function () {
|
1306
|
+
if (!this.opened()) return;
|
1307
|
+
|
1308
|
+
var cid = this.containerId,
|
1309
|
+
scroll = "scroll." + cid,
|
1310
|
+
resize = "resize."+cid,
|
1311
|
+
orient = "orientationchange."+cid;
|
1312
|
+
|
1313
|
+
// unbind event listeners
|
1314
|
+
this.container.parents().add(window).each(function () { $(this).off(scroll).off(resize).off(orient); });
|
1315
|
+
|
1316
|
+
this.clearDropdownAlignmentPreference();
|
1317
|
+
|
1318
|
+
$("#select2-drop-mask").hide();
|
1319
|
+
this.dropdown.removeAttr("id"); // only the active dropdown has the select2-drop id
|
1320
|
+
this.dropdown.hide();
|
1321
|
+
this.container.removeClass("select2-dropdown-open");
|
1322
|
+
this.results.empty();
|
1323
|
+
|
1324
|
+
|
1325
|
+
this.clearSearch();
|
1326
|
+
this.search.removeClass("select2-active");
|
1327
|
+
this.opts.element.trigger($.Event("select2-close"));
|
1328
|
+
},
|
1329
|
+
|
1330
|
+
/**
|
1331
|
+
* Opens control, sets input value, and updates results.
|
1332
|
+
*/
|
1333
|
+
// abstract
|
1334
|
+
externalSearch: function (term) {
|
1335
|
+
this.open();
|
1336
|
+
this.search.val(term);
|
1337
|
+
this.updateResults(false);
|
1338
|
+
},
|
1339
|
+
|
1340
|
+
// abstract
|
1341
|
+
clearSearch: function () {
|
1342
|
+
|
1343
|
+
},
|
1344
|
+
|
1345
|
+
//abstract
|
1346
|
+
getMaximumSelectionSize: function() {
|
1347
|
+
return evaluate(this.opts.maximumSelectionSize);
|
1348
|
+
},
|
1349
|
+
|
1350
|
+
// abstract
|
1351
|
+
ensureHighlightVisible: function () {
|
1352
|
+
var results = this.results, children, index, child, hb, rb, y, more;
|
1353
|
+
|
1354
|
+
index = this.highlight();
|
1355
|
+
|
1356
|
+
if (index < 0) return;
|
1357
|
+
|
1358
|
+
if (index == 0) {
|
1359
|
+
|
1360
|
+
// if the first element is highlighted scroll all the way to the top,
|
1361
|
+
// that way any unselectable headers above it will also be scrolled
|
1362
|
+
// into view
|
1363
|
+
|
1364
|
+
results.scrollTop(0);
|
1365
|
+
return;
|
1366
|
+
}
|
1367
|
+
|
1368
|
+
children = this.findHighlightableChoices().find('.select2-result-label');
|
1369
|
+
|
1370
|
+
child = $(children[index]);
|
1371
|
+
|
1372
|
+
hb = child.offset().top + child.outerHeight(true);
|
1373
|
+
|
1374
|
+
// if this is the last child lets also make sure select2-more-results is visible
|
1375
|
+
if (index === children.length - 1) {
|
1376
|
+
more = results.find("li.select2-more-results");
|
1377
|
+
if (more.length > 0) {
|
1378
|
+
hb = more.offset().top + more.outerHeight(true);
|
1379
|
+
}
|
1380
|
+
}
|
1381
|
+
|
1382
|
+
rb = results.offset().top + results.outerHeight(true);
|
1383
|
+
if (hb > rb) {
|
1384
|
+
results.scrollTop(results.scrollTop() + (hb - rb));
|
1385
|
+
}
|
1386
|
+
y = child.offset().top - results.offset().top;
|
1387
|
+
|
1388
|
+
// make sure the top of the element is visible
|
1389
|
+
if (y < 0 && child.css('display') != 'none' ) {
|
1390
|
+
results.scrollTop(results.scrollTop() + y); // y is negative
|
1391
|
+
}
|
1392
|
+
},
|
1393
|
+
|
1394
|
+
// abstract
|
1395
|
+
findHighlightableChoices: function() {
|
1396
|
+
return this.results.find(".select2-result-selectable:not(.select2-selected):not(.select2-disabled)");
|
1397
|
+
},
|
1398
|
+
|
1399
|
+
// abstract
|
1400
|
+
moveHighlight: function (delta) {
|
1401
|
+
var choices = this.findHighlightableChoices(),
|
1402
|
+
index = this.highlight();
|
1403
|
+
|
1404
|
+
while (index > -1 && index < choices.length) {
|
1405
|
+
index += delta;
|
1406
|
+
var choice = $(choices[index]);
|
1407
|
+
if (choice.hasClass("select2-result-selectable") && !choice.hasClass("select2-disabled") && !choice.hasClass("select2-selected")) {
|
1408
|
+
this.highlight(index);
|
1409
|
+
break;
|
1410
|
+
}
|
1411
|
+
}
|
1412
|
+
},
|
1413
|
+
|
1414
|
+
// abstract
|
1415
|
+
highlight: function (index) {
|
1416
|
+
var choices = this.findHighlightableChoices(),
|
1417
|
+
choice,
|
1418
|
+
data;
|
1419
|
+
|
1420
|
+
if (arguments.length === 0) {
|
1421
|
+
return indexOf(choices.filter(".select2-highlighted")[0], choices.get());
|
1422
|
+
}
|
1423
|
+
|
1424
|
+
if (index >= choices.length) index = choices.length - 1;
|
1425
|
+
if (index < 0) index = 0;
|
1426
|
+
|
1427
|
+
this.removeHighlight();
|
1428
|
+
|
1429
|
+
choice = $(choices[index]);
|
1430
|
+
choice.addClass("select2-highlighted");
|
1431
|
+
|
1432
|
+
this.ensureHighlightVisible();
|
1433
|
+
|
1434
|
+
data = choice.data("select2-data");
|
1435
|
+
if (data) {
|
1436
|
+
this.opts.element.trigger({ type: "select2-highlight", val: this.id(data), choice: data });
|
1437
|
+
}
|
1438
|
+
},
|
1439
|
+
|
1440
|
+
removeHighlight: function() {
|
1441
|
+
this.results.find(".select2-highlighted").removeClass("select2-highlighted");
|
1442
|
+
},
|
1443
|
+
|
1444
|
+
// abstract
|
1445
|
+
countSelectableResults: function() {
|
1446
|
+
return this.findHighlightableChoices().length;
|
1447
|
+
},
|
1448
|
+
|
1449
|
+
// abstract
|
1450
|
+
highlightUnderEvent: function (event) {
|
1451
|
+
var el = $(event.target).closest(".select2-result-selectable");
|
1452
|
+
if (el.length > 0 && !el.is(".select2-highlighted")) {
|
1453
|
+
var choices = this.findHighlightableChoices();
|
1454
|
+
this.highlight(choices.index(el));
|
1455
|
+
} else if (el.length == 0) {
|
1456
|
+
// if we are over an unselectable item remove all highlights
|
1457
|
+
this.removeHighlight();
|
1458
|
+
}
|
1459
|
+
},
|
1460
|
+
|
1461
|
+
// abstract
|
1462
|
+
loadMoreIfNeeded: function () {
|
1463
|
+
var results = this.results,
|
1464
|
+
more = results.find("li.select2-more-results"),
|
1465
|
+
below, // pixels the element is below the scroll fold, below==0 is when the element is starting to be visible
|
1466
|
+
offset = -1, // index of first element without data
|
1467
|
+
page = this.resultsPage + 1,
|
1468
|
+
self=this,
|
1469
|
+
term=this.search.val(),
|
1470
|
+
context=this.context;
|
1471
|
+
|
1472
|
+
if (more.length === 0) return;
|
1473
|
+
below = more.offset().top - results.offset().top - results.height();
|
1474
|
+
|
1475
|
+
if (below <= this.opts.loadMorePadding) {
|
1476
|
+
more.addClass("select2-active");
|
1477
|
+
this.opts.query({
|
1478
|
+
element: this.opts.element,
|
1479
|
+
term: term,
|
1480
|
+
page: page,
|
1481
|
+
context: context,
|
1482
|
+
matcher: this.opts.matcher,
|
1483
|
+
callback: this.bind(function (data) {
|
1484
|
+
|
1485
|
+
// ignore a response if the select2 has been closed before it was received
|
1486
|
+
if (!self.opened()) return;
|
1487
|
+
|
1488
|
+
|
1489
|
+
self.opts.populateResults.call(this, results, data.results, {term: term, page: page, context:context});
|
1490
|
+
self.postprocessResults(data, false, false);
|
1491
|
+
|
1492
|
+
if (data.more===true) {
|
1493
|
+
more.detach().appendTo(results).text(self.opts.formatLoadMore(page+1));
|
1494
|
+
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
|
1495
|
+
} else {
|
1496
|
+
more.remove();
|
1497
|
+
}
|
1498
|
+
self.positionDropdown();
|
1499
|
+
self.resultsPage = page;
|
1500
|
+
self.context = data.context;
|
1501
|
+
this.opts.element.trigger({ type: "select2-loaded", items: data });
|
1502
|
+
})});
|
1503
|
+
}
|
1504
|
+
},
|
1505
|
+
|
1506
|
+
/**
|
1507
|
+
* Default tokenizer function which does nothing
|
1508
|
+
*/
|
1509
|
+
tokenize: function() {
|
1510
|
+
|
1511
|
+
},
|
1512
|
+
|
1513
|
+
/**
|
1514
|
+
* @param initial whether or not this is the call to this method right after the dropdown has been opened
|
1515
|
+
*/
|
1516
|
+
// abstract
|
1517
|
+
updateResults: function (initial) {
|
1518
|
+
var search = this.search,
|
1519
|
+
results = this.results,
|
1520
|
+
opts = this.opts,
|
1521
|
+
data,
|
1522
|
+
self = this,
|
1523
|
+
input,
|
1524
|
+
term = search.val(),
|
1525
|
+
lastTerm = $.data(this.container, "select2-last-term"),
|
1526
|
+
// sequence number used to drop out-of-order responses
|
1527
|
+
queryNumber;
|
1528
|
+
|
1529
|
+
// prevent duplicate queries against the same term
|
1530
|
+
if (initial !== true && lastTerm && equal(term, lastTerm)) return;
|
1531
|
+
|
1532
|
+
$.data(this.container, "select2-last-term", term);
|
1533
|
+
|
1534
|
+
// if the search is currently hidden we do not alter the results
|
1535
|
+
if (initial !== true && (this.showSearchInput === false || !this.opened())) {
|
1536
|
+
return;
|
1537
|
+
}
|
1538
|
+
|
1539
|
+
function postRender() {
|
1540
|
+
search.removeClass("select2-active");
|
1541
|
+
self.positionDropdown();
|
1542
|
+
}
|
1543
|
+
|
1544
|
+
function render(html) {
|
1545
|
+
results.html(html);
|
1546
|
+
postRender();
|
1547
|
+
}
|
1548
|
+
|
1549
|
+
queryNumber = ++this.queryCount;
|
1550
|
+
|
1551
|
+
var maxSelSize = this.getMaximumSelectionSize();
|
1552
|
+
if (maxSelSize >=1) {
|
1553
|
+
data = this.data();
|
1554
|
+
if ($.isArray(data) && data.length >= maxSelSize && checkFormatter(opts.formatSelectionTooBig, "formatSelectionTooBig")) {
|
1555
|
+
render("<li class='select2-selection-limit'>" + opts.formatSelectionTooBig(maxSelSize) + "</li>");
|
1556
|
+
return;
|
1557
|
+
}
|
1558
|
+
}
|
1559
|
+
|
1560
|
+
if (search.val().length < opts.minimumInputLength) {
|
1561
|
+
if (checkFormatter(opts.formatInputTooShort, "formatInputTooShort")) {
|
1562
|
+
render("<li class='select2-no-results'>" + opts.formatInputTooShort(search.val(), opts.minimumInputLength) + "</li>");
|
1563
|
+
} else {
|
1564
|
+
render("");
|
1565
|
+
}
|
1566
|
+
if (initial && this.showSearch) this.showSearch(true);
|
1567
|
+
return;
|
1568
|
+
}
|
1569
|
+
|
1570
|
+
if (opts.maximumInputLength && search.val().length > opts.maximumInputLength) {
|
1571
|
+
if (checkFormatter(opts.formatInputTooLong, "formatInputTooLong")) {
|
1572
|
+
render("<li class='select2-no-results'>" + opts.formatInputTooLong(search.val(), opts.maximumInputLength) + "</li>");
|
1573
|
+
} else {
|
1574
|
+
render("");
|
1575
|
+
}
|
1576
|
+
return;
|
1577
|
+
}
|
1578
|
+
|
1579
|
+
if (opts.formatSearching && this.findHighlightableChoices().length === 0) {
|
1580
|
+
render("<li class='select2-searching'>" + opts.formatSearching() + "</li>");
|
1581
|
+
}
|
1582
|
+
|
1583
|
+
search.addClass("select2-active");
|
1584
|
+
|
1585
|
+
this.removeHighlight();
|
1586
|
+
|
1587
|
+
// give the tokenizer a chance to pre-process the input
|
1588
|
+
input = this.tokenize();
|
1589
|
+
if (input != undefined && input != null) {
|
1590
|
+
search.val(input);
|
1591
|
+
}
|
1592
|
+
|
1593
|
+
this.resultsPage = 1;
|
1594
|
+
|
1595
|
+
opts.query({
|
1596
|
+
element: opts.element,
|
1597
|
+
term: search.val(),
|
1598
|
+
page: this.resultsPage,
|
1599
|
+
context: null,
|
1600
|
+
matcher: opts.matcher,
|
1601
|
+
callback: this.bind(function (data) {
|
1602
|
+
var def; // default choice
|
1603
|
+
|
1604
|
+
// ignore old responses
|
1605
|
+
if (queryNumber != this.queryCount) {
|
1606
|
+
return;
|
1607
|
+
}
|
1608
|
+
|
1609
|
+
// ignore a response if the select2 has been closed before it was received
|
1610
|
+
if (!this.opened()) {
|
1611
|
+
this.search.removeClass("select2-active");
|
1612
|
+
return;
|
1613
|
+
}
|
1614
|
+
|
1615
|
+
// save context, if any
|
1616
|
+
this.context = (data.context===undefined) ? null : data.context;
|
1617
|
+
// create a default choice and prepend it to the list
|
1618
|
+
if (this.opts.createSearchChoice && search.val() !== "") {
|
1619
|
+
def = this.opts.createSearchChoice.call(self, search.val(), data.results);
|
1620
|
+
if (def !== undefined && def !== null && self.id(def) !== undefined && self.id(def) !== null) {
|
1621
|
+
if ($(data.results).filter(
|
1622
|
+
function () {
|
1623
|
+
return equal(self.id(this), self.id(def));
|
1624
|
+
}).length === 0) {
|
1625
|
+
data.results.unshift(def);
|
1626
|
+
}
|
1627
|
+
}
|
1628
|
+
}
|
1629
|
+
|
1630
|
+
if (data.results.length === 0 && checkFormatter(opts.formatNoMatches, "formatNoMatches")) {
|
1631
|
+
render("<li class='select2-no-results'>" + opts.formatNoMatches(search.val()) + "</li>");
|
1632
|
+
return;
|
1633
|
+
}
|
1634
|
+
|
1635
|
+
results.empty();
|
1636
|
+
self.opts.populateResults.call(this, results, data.results, {term: search.val(), page: this.resultsPage, context:null});
|
1637
|
+
|
1638
|
+
if (data.more === true && checkFormatter(opts.formatLoadMore, "formatLoadMore")) {
|
1639
|
+
results.append("<li class='select2-more-results'>" + self.opts.escapeMarkup(opts.formatLoadMore(this.resultsPage)) + "</li>");
|
1640
|
+
window.setTimeout(function() { self.loadMoreIfNeeded(); }, 10);
|
1641
|
+
}
|
1642
|
+
|
1643
|
+
this.postprocessResults(data, initial);
|
1644
|
+
|
1645
|
+
postRender();
|
1646
|
+
|
1647
|
+
this.opts.element.trigger({ type: "select2-loaded", items: data });
|
1648
|
+
})});
|
1649
|
+
},
|
1650
|
+
|
1651
|
+
// abstract
|
1652
|
+
cancel: function () {
|
1653
|
+
this.close();
|
1654
|
+
},
|
1655
|
+
|
1656
|
+
// abstract
|
1657
|
+
blur: function () {
|
1658
|
+
// if selectOnBlur == true, select the currently highlighted option
|
1659
|
+
if (this.opts.selectOnBlur)
|
1660
|
+
this.selectHighlighted({noFocus: true});
|
1661
|
+
|
1662
|
+
this.close();
|
1663
|
+
this.container.removeClass("select2-container-active");
|
1664
|
+
// synonymous to .is(':focus'), which is available in jquery >= 1.6
|
1665
|
+
if (this.search[0] === document.activeElement) { this.search.blur(); }
|
1666
|
+
this.clearSearch();
|
1667
|
+
this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
|
1668
|
+
},
|
1669
|
+
|
1670
|
+
// abstract
|
1671
|
+
focusSearch: function () {
|
1672
|
+
focus(this.search);
|
1673
|
+
},
|
1674
|
+
|
1675
|
+
// abstract
|
1676
|
+
selectHighlighted: function (options) {
|
1677
|
+
var index=this.highlight(),
|
1678
|
+
highlighted=this.results.find(".select2-highlighted"),
|
1679
|
+
data = highlighted.closest('.select2-result').data("select2-data");
|
1680
|
+
|
1681
|
+
if (data) {
|
1682
|
+
this.highlight(index);
|
1683
|
+
this.onSelect(data, options);
|
1684
|
+
} else if (options && options.noFocus) {
|
1685
|
+
this.close();
|
1686
|
+
}
|
1687
|
+
},
|
1688
|
+
|
1689
|
+
// abstract
|
1690
|
+
getPlaceholder: function () {
|
1691
|
+
var placeholderOption;
|
1692
|
+
return this.opts.element.attr("placeholder") ||
|
1693
|
+
this.opts.element.attr("data-placeholder") || // jquery 1.4 compat
|
1694
|
+
this.opts.element.data("placeholder") ||
|
1695
|
+
this.opts.placeholder ||
|
1696
|
+
((placeholderOption = this.getPlaceholderOption()) !== undefined ? placeholderOption.text() : undefined);
|
1697
|
+
},
|
1698
|
+
|
1699
|
+
// abstract
|
1700
|
+
getPlaceholderOption: function() {
|
1701
|
+
if (this.select) {
|
1702
|
+
var firstOption = this.select.children().first();
|
1703
|
+
if (this.opts.placeholderOption !== undefined ) {
|
1704
|
+
//Determine the placeholder option based on the specified placeholderOption setting
|
1705
|
+
return (this.opts.placeholderOption === "first" && firstOption) ||
|
1706
|
+
(typeof this.opts.placeholderOption === "function" && this.opts.placeholderOption(this.select));
|
1707
|
+
} else if (firstOption.text() === "" && firstOption.val() === "") {
|
1708
|
+
//No explicit placeholder option specified, use the first if it's blank
|
1709
|
+
return firstOption;
|
1710
|
+
}
|
1711
|
+
}
|
1712
|
+
},
|
1713
|
+
|
1714
|
+
/**
|
1715
|
+
* Get the desired width for the container element. This is
|
1716
|
+
* derived first from option `width` passed to select2, then
|
1717
|
+
* the inline 'style' on the original element, and finally
|
1718
|
+
* falls back to the jQuery calculated element width.
|
1719
|
+
*/
|
1720
|
+
// abstract
|
1721
|
+
initContainerWidth: function () {
|
1722
|
+
function resolveContainerWidth() {
|
1723
|
+
var style, attrs, matches, i, l;
|
1724
|
+
|
1725
|
+
if (this.opts.width === "off") {
|
1726
|
+
return null;
|
1727
|
+
} else if (this.opts.width === "element"){
|
1728
|
+
return this.opts.element.outerWidth(false) === 0 ? 'auto' : this.opts.element.outerWidth(false) + 'px';
|
1729
|
+
} else if (this.opts.width === "copy" || this.opts.width === "resolve") {
|
1730
|
+
// check if there is inline style on the element that contains width
|
1731
|
+
style = this.opts.element.attr('style');
|
1732
|
+
if (style !== undefined) {
|
1733
|
+
attrs = style.split(';');
|
1734
|
+
for (i = 0, l = attrs.length; i < l; i = i + 1) {
|
1735
|
+
matches = attrs[i].replace(/\s/g, '')
|
1736
|
+
.match(/width:(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc))/i);
|
1737
|
+
if (matches !== null && matches.length >= 1)
|
1738
|
+
return matches[1];
|
1739
|
+
}
|
1740
|
+
}
|
1741
|
+
|
1742
|
+
if (this.opts.width === "resolve") {
|
1743
|
+
// next check if css('width') can resolve a width that is percent based, this is sometimes possible
|
1744
|
+
// when attached to input type=hidden or elements hidden via css
|
1745
|
+
style = this.opts.element.css('width');
|
1746
|
+
if (style.indexOf("%") > 0) return style;
|
1747
|
+
|
1748
|
+
// finally, fallback on the calculated width of the element
|
1749
|
+
return (this.opts.element.outerWidth(false) === 0 ? 'auto' : this.opts.element.outerWidth(false) + 'px');
|
1750
|
+
}
|
1751
|
+
|
1752
|
+
return null;
|
1753
|
+
} else if ($.isFunction(this.opts.width)) {
|
1754
|
+
return this.opts.width();
|
1755
|
+
} else {
|
1756
|
+
return this.opts.width;
|
1757
|
+
}
|
1758
|
+
};
|
1759
|
+
|
1760
|
+
var width = resolveContainerWidth.call(this);
|
1761
|
+
if (width !== null) {
|
1762
|
+
this.container.css("width", width);
|
1763
|
+
}
|
1764
|
+
}
|
1765
|
+
});
|
1766
|
+
|
1767
|
+
SingleSelect2 = clazz(AbstractSelect2, {
|
1768
|
+
|
1769
|
+
// single
|
1770
|
+
|
1771
|
+
createContainer: function () {
|
1772
|
+
var container = $(document.createElement("div")).attr({
|
1773
|
+
"class": "select2-container"
|
1774
|
+
}).html([
|
1775
|
+
"<a href='javascript:void(0)' onclick='return false;' class='select2-choice' tabindex='-1'>",
|
1776
|
+
" <span class='select2-chosen'> </span><abbr class='select2-search-choice-close'></abbr>",
|
1777
|
+
" <span class='select2-arrow'><b></b></span>",
|
1778
|
+
"</a>",
|
1779
|
+
"<input class='select2-focusser select2-offscreen' type='text'/>",
|
1780
|
+
"<div class='select2-drop select2-display-none'>",
|
1781
|
+
" <div class='select2-search'>",
|
1782
|
+
" <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input'/>",
|
1783
|
+
" </div>",
|
1784
|
+
" <ul class='select2-results'>",
|
1785
|
+
" </ul>",
|
1786
|
+
"</div>"].join(""));
|
1787
|
+
return container;
|
1788
|
+
},
|
1789
|
+
|
1790
|
+
// single
|
1791
|
+
enableInterface: function() {
|
1792
|
+
if (this.parent.enableInterface.apply(this, arguments)) {
|
1793
|
+
this.focusser.prop("disabled", !this.isInterfaceEnabled());
|
1794
|
+
}
|
1795
|
+
},
|
1796
|
+
|
1797
|
+
// single
|
1798
|
+
opening: function () {
|
1799
|
+
var el, range, len;
|
1800
|
+
|
1801
|
+
if (this.opts.minimumResultsForSearch >= 0) {
|
1802
|
+
this.showSearch(true);
|
1803
|
+
}
|
1804
|
+
|
1805
|
+
this.parent.opening.apply(this, arguments);
|
1806
|
+
|
1807
|
+
if (this.showSearchInput !== false) {
|
1808
|
+
// IE appends focusser.val() at the end of field :/ so we manually insert it at the beginning using a range
|
1809
|
+
// all other browsers handle this just fine
|
1810
|
+
|
1811
|
+
this.search.val(this.focusser.val());
|
1812
|
+
}
|
1813
|
+
this.search.focus();
|
1814
|
+
// move the cursor to the end after focussing, otherwise it will be at the beginning and
|
1815
|
+
// new text will appear *before* focusser.val()
|
1816
|
+
el = this.search.get(0);
|
1817
|
+
if (el.createTextRange) {
|
1818
|
+
range = el.createTextRange();
|
1819
|
+
range.collapse(false);
|
1820
|
+
range.select();
|
1821
|
+
} else if (el.setSelectionRange) {
|
1822
|
+
len = this.search.val().length;
|
1823
|
+
el.setSelectionRange(len, len);
|
1824
|
+
}
|
1825
|
+
|
1826
|
+
this.focusser.prop("disabled", true).val("");
|
1827
|
+
this.updateResults(true);
|
1828
|
+
this.opts.element.trigger($.Event("select2-open"));
|
1829
|
+
},
|
1830
|
+
|
1831
|
+
// single
|
1832
|
+
close: function () {
|
1833
|
+
if (!this.opened()) return;
|
1834
|
+
this.parent.close.apply(this, arguments);
|
1835
|
+
this.focusser.removeAttr("disabled");
|
1836
|
+
this.focusser.focus();
|
1837
|
+
},
|
1838
|
+
|
1839
|
+
// single
|
1840
|
+
focus: function () {
|
1841
|
+
if (this.opened()) {
|
1842
|
+
this.close();
|
1843
|
+
} else {
|
1844
|
+
this.focusser.removeAttr("disabled");
|
1845
|
+
this.focusser.focus();
|
1846
|
+
}
|
1847
|
+
},
|
1848
|
+
|
1849
|
+
// single
|
1850
|
+
isFocused: function () {
|
1851
|
+
return this.container.hasClass("select2-container-active");
|
1852
|
+
},
|
1853
|
+
|
1854
|
+
// single
|
1855
|
+
cancel: function () {
|
1856
|
+
this.parent.cancel.apply(this, arguments);
|
1857
|
+
this.focusser.removeAttr("disabled");
|
1858
|
+
this.focusser.focus();
|
1859
|
+
},
|
1860
|
+
|
1861
|
+
// single
|
1862
|
+
initContainer: function () {
|
1863
|
+
|
1864
|
+
var selection,
|
1865
|
+
container = this.container,
|
1866
|
+
dropdown = this.dropdown;
|
1867
|
+
|
1868
|
+
if (this.opts.minimumResultsForSearch < 0) {
|
1869
|
+
this.showSearch(false);
|
1870
|
+
} else {
|
1871
|
+
this.showSearch(true);
|
1872
|
+
}
|
1873
|
+
|
1874
|
+
this.selection = selection = container.find(".select2-choice");
|
1875
|
+
|
1876
|
+
this.focusser = container.find(".select2-focusser");
|
1877
|
+
|
1878
|
+
// rewrite labels from original element to focusser
|
1879
|
+
this.focusser.attr("id", "s2id_autogen"+nextUid());
|
1880
|
+
|
1881
|
+
$("label[for='" + this.opts.element.attr("id") + "']")
|
1882
|
+
.attr('for', this.focusser.attr('id'));
|
1883
|
+
|
1884
|
+
this.focusser.attr("tabindex", this.elementTabIndex);
|
1885
|
+
|
1886
|
+
this.search.on("keydown", this.bind(function (e) {
|
1887
|
+
if (!this.isInterfaceEnabled()) return;
|
1888
|
+
|
1889
|
+
if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
|
1890
|
+
// prevent the page from scrolling
|
1891
|
+
killEvent(e);
|
1892
|
+
return;
|
1893
|
+
}
|
1894
|
+
|
1895
|
+
switch (e.which) {
|
1896
|
+
case KEY.UP:
|
1897
|
+
case KEY.DOWN:
|
1898
|
+
this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
|
1899
|
+
killEvent(e);
|
1900
|
+
return;
|
1901
|
+
case KEY.ENTER:
|
1902
|
+
this.selectHighlighted();
|
1903
|
+
killEvent(e);
|
1904
|
+
return;
|
1905
|
+
case KEY.TAB:
|
1906
|
+
this.selectHighlighted({noFocus: true});
|
1907
|
+
return;
|
1908
|
+
case KEY.ESC:
|
1909
|
+
this.cancel(e);
|
1910
|
+
killEvent(e);
|
1911
|
+
return;
|
1912
|
+
}
|
1913
|
+
}));
|
1914
|
+
|
1915
|
+
this.search.on("blur", this.bind(function(e) {
|
1916
|
+
// a workaround for chrome to keep the search field focussed when the scroll bar is used to scroll the dropdown.
|
1917
|
+
// without this the search field loses focus which is annoying
|
1918
|
+
if (document.activeElement === this.body().get(0)) {
|
1919
|
+
window.setTimeout(this.bind(function() {
|
1920
|
+
this.search.focus();
|
1921
|
+
}), 0);
|
1922
|
+
}
|
1923
|
+
}));
|
1924
|
+
|
1925
|
+
this.focusser.on("keydown", this.bind(function (e) {
|
1926
|
+
if (!this.isInterfaceEnabled()) return;
|
1927
|
+
|
1928
|
+
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
|
1929
|
+
return;
|
1930
|
+
}
|
1931
|
+
|
1932
|
+
if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
|
1933
|
+
killEvent(e);
|
1934
|
+
return;
|
1935
|
+
}
|
1936
|
+
|
1937
|
+
if (e.which == KEY.DOWN || e.which == KEY.UP
|
1938
|
+
|| (e.which == KEY.ENTER && this.opts.openOnEnter)) {
|
1939
|
+
|
1940
|
+
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) return;
|
1941
|
+
|
1942
|
+
this.open();
|
1943
|
+
killEvent(e);
|
1944
|
+
return;
|
1945
|
+
}
|
1946
|
+
|
1947
|
+
if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) {
|
1948
|
+
if (this.opts.allowClear) {
|
1949
|
+
this.clear();
|
1950
|
+
}
|
1951
|
+
killEvent(e);
|
1952
|
+
return;
|
1953
|
+
}
|
1954
|
+
}));
|
1955
|
+
|
1956
|
+
|
1957
|
+
installKeyUpChangeEvent(this.focusser);
|
1958
|
+
this.focusser.on("keyup-change input", this.bind(function(e) {
|
1959
|
+
if (this.opts.minimumResultsForSearch >= 0) {
|
1960
|
+
e.stopPropagation();
|
1961
|
+
if (this.opened()) return;
|
1962
|
+
this.open();
|
1963
|
+
}
|
1964
|
+
}));
|
1965
|
+
|
1966
|
+
selection.on("mousedown", "abbr", this.bind(function (e) {
|
1967
|
+
if (!this.isInterfaceEnabled()) return;
|
1968
|
+
this.clear();
|
1969
|
+
killEventImmediately(e);
|
1970
|
+
this.close();
|
1971
|
+
this.selection.focus();
|
1972
|
+
}));
|
1973
|
+
|
1974
|
+
selection.on("mousedown", this.bind(function (e) {
|
1975
|
+
|
1976
|
+
if (!this.container.hasClass("select2-container-active")) {
|
1977
|
+
this.opts.element.trigger($.Event("select2-focus"));
|
1978
|
+
}
|
1979
|
+
|
1980
|
+
if (this.opened()) {
|
1981
|
+
this.close();
|
1982
|
+
} else if (this.isInterfaceEnabled()) {
|
1983
|
+
this.open();
|
1984
|
+
}
|
1985
|
+
|
1986
|
+
killEvent(e);
|
1987
|
+
}));
|
1988
|
+
|
1989
|
+
dropdown.on("mousedown", this.bind(function() { this.search.focus(); }));
|
1990
|
+
|
1991
|
+
selection.on("focus", this.bind(function(e) {
|
1992
|
+
killEvent(e);
|
1993
|
+
}));
|
1994
|
+
|
1995
|
+
this.focusser.on("focus", this.bind(function(){
|
1996
|
+
if (!this.container.hasClass("select2-container-active")) {
|
1997
|
+
this.opts.element.trigger($.Event("select2-focus"));
|
1998
|
+
}
|
1999
|
+
this.container.addClass("select2-container-active");
|
2000
|
+
})).on("blur", this.bind(function() {
|
2001
|
+
if (!this.opened()) {
|
2002
|
+
this.container.removeClass("select2-container-active");
|
2003
|
+
this.opts.element.trigger($.Event("select2-blur"));
|
2004
|
+
}
|
2005
|
+
}));
|
2006
|
+
this.search.on("focus", this.bind(function(){
|
2007
|
+
if (!this.container.hasClass("select2-container-active")) {
|
2008
|
+
this.opts.element.trigger($.Event("select2-focus"));
|
2009
|
+
}
|
2010
|
+
this.container.addClass("select2-container-active");
|
2011
|
+
}));
|
2012
|
+
|
2013
|
+
this.initContainerWidth();
|
2014
|
+
this.opts.element.addClass("select2-offscreen");
|
2015
|
+
this.setPlaceholder();
|
2016
|
+
|
2017
|
+
},
|
2018
|
+
|
2019
|
+
// single
|
2020
|
+
clear: function(triggerChange) {
|
2021
|
+
var data=this.selection.data("select2-data");
|
2022
|
+
if (data) { // guard against queued quick consecutive clicks
|
2023
|
+
var placeholderOption = this.getPlaceholderOption();
|
2024
|
+
this.opts.element.val(placeholderOption ? placeholderOption.val() : "");
|
2025
|
+
this.selection.find(".select2-chosen").empty();
|
2026
|
+
this.selection.removeData("select2-data");
|
2027
|
+
this.setPlaceholder();
|
2028
|
+
|
2029
|
+
if (triggerChange !== false){
|
2030
|
+
this.opts.element.trigger({ type: "select2-removed", val: this.id(data), choice: data });
|
2031
|
+
this.triggerChange({removed:data});
|
2032
|
+
}
|
2033
|
+
}
|
2034
|
+
},
|
2035
|
+
|
2036
|
+
/**
|
2037
|
+
* Sets selection based on source element's value
|
2038
|
+
*/
|
2039
|
+
// single
|
2040
|
+
initSelection: function () {
|
2041
|
+
var selected;
|
2042
|
+
if (this.isPlaceholderOptionSelected()) {
|
2043
|
+
this.updateSelection([]);
|
2044
|
+
this.close();
|
2045
|
+
this.setPlaceholder();
|
2046
|
+
} else {
|
2047
|
+
var self = this;
|
2048
|
+
this.opts.initSelection.call(null, this.opts.element, function(selected){
|
2049
|
+
if (selected !== undefined && selected !== null) {
|
2050
|
+
self.updateSelection(selected);
|
2051
|
+
self.close();
|
2052
|
+
self.setPlaceholder();
|
2053
|
+
}
|
2054
|
+
});
|
2055
|
+
}
|
2056
|
+
},
|
2057
|
+
|
2058
|
+
isPlaceholderOptionSelected: function() {
|
2059
|
+
var placeholderOption;
|
2060
|
+
return ((placeholderOption = this.getPlaceholderOption()) !== undefined && placeholderOption.is(':selected')) ||
|
2061
|
+
(this.opts.element.val() === "") ||
|
2062
|
+
(this.opts.element.val() === undefined) ||
|
2063
|
+
(this.opts.element.val() === null);
|
2064
|
+
},
|
2065
|
+
|
2066
|
+
// single
|
2067
|
+
prepareOpts: function () {
|
2068
|
+
var opts = this.parent.prepareOpts.apply(this, arguments),
|
2069
|
+
self=this;
|
2070
|
+
|
2071
|
+
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
2072
|
+
// install the selection initializer
|
2073
|
+
opts.initSelection = function (element, callback) {
|
2074
|
+
var selected = element.find(":selected");
|
2075
|
+
// a single select box always has a value, no need to null check 'selected'
|
2076
|
+
callback(self.optionToData(selected));
|
2077
|
+
};
|
2078
|
+
} else if ("data" in opts) {
|
2079
|
+
// install default initSelection when applied to hidden input and data is local
|
2080
|
+
opts.initSelection = opts.initSelection || function (element, callback) {
|
2081
|
+
var id = element.val();
|
2082
|
+
//search in data by id, storing the actual matching item
|
2083
|
+
var match = null;
|
2084
|
+
opts.query({
|
2085
|
+
matcher: function(term, text, el){
|
2086
|
+
var is_match = equal(id, opts.id(el));
|
2087
|
+
if (is_match) {
|
2088
|
+
match = el;
|
2089
|
+
}
|
2090
|
+
return is_match;
|
2091
|
+
},
|
2092
|
+
callback: !$.isFunction(callback) ? $.noop : function() {
|
2093
|
+
callback(match);
|
2094
|
+
}
|
2095
|
+
});
|
2096
|
+
};
|
2097
|
+
}
|
2098
|
+
|
2099
|
+
return opts;
|
2100
|
+
},
|
2101
|
+
|
2102
|
+
// single
|
2103
|
+
getPlaceholder: function() {
|
2104
|
+
// if a placeholder is specified on a single select without a valid placeholder option ignore it
|
2105
|
+
if (this.select) {
|
2106
|
+
if (this.getPlaceholderOption() === undefined) {
|
2107
|
+
return undefined;
|
2108
|
+
}
|
2109
|
+
}
|
2110
|
+
|
2111
|
+
return this.parent.getPlaceholder.apply(this, arguments);
|
2112
|
+
},
|
2113
|
+
|
2114
|
+
// single
|
2115
|
+
setPlaceholder: function () {
|
2116
|
+
var placeholder = this.getPlaceholder();
|
2117
|
+
|
2118
|
+
if (this.isPlaceholderOptionSelected() && placeholder !== undefined) {
|
2119
|
+
|
2120
|
+
// check for a placeholder option if attached to a select
|
2121
|
+
if (this.select && this.getPlaceholderOption() === undefined) return;
|
2122
|
+
|
2123
|
+
this.selection.find(".select2-chosen").html(this.opts.escapeMarkup(placeholder));
|
2124
|
+
|
2125
|
+
this.selection.addClass("select2-default");
|
2126
|
+
|
2127
|
+
this.container.removeClass("select2-allowclear");
|
2128
|
+
}
|
2129
|
+
},
|
2130
|
+
|
2131
|
+
// single
|
2132
|
+
postprocessResults: function (data, initial, noHighlightUpdate) {
|
2133
|
+
var selected = 0, self = this, showSearchInput = true;
|
2134
|
+
|
2135
|
+
// find the selected element in the result list
|
2136
|
+
|
2137
|
+
this.findHighlightableChoices().each2(function (i, elm) {
|
2138
|
+
if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
|
2139
|
+
selected = i;
|
2140
|
+
return false;
|
2141
|
+
}
|
2142
|
+
});
|
2143
|
+
|
2144
|
+
// and highlight it
|
2145
|
+
if (noHighlightUpdate !== false) {
|
2146
|
+
if (initial === true && selected >= 0) {
|
2147
|
+
this.highlight(selected);
|
2148
|
+
} else {
|
2149
|
+
this.highlight(0);
|
2150
|
+
}
|
2151
|
+
}
|
2152
|
+
|
2153
|
+
// hide the search box if this is the first we got the results and there are enough of them for search
|
2154
|
+
|
2155
|
+
if (initial === true) {
|
2156
|
+
var min = this.opts.minimumResultsForSearch;
|
2157
|
+
if (min >= 0) {
|
2158
|
+
this.showSearch(countResults(data.results) >= min);
|
2159
|
+
}
|
2160
|
+
}
|
2161
|
+
},
|
2162
|
+
|
2163
|
+
// single
|
2164
|
+
showSearch: function(showSearchInput) {
|
2165
|
+
if (this.showSearchInput === showSearchInput) return;
|
2166
|
+
|
2167
|
+
this.showSearchInput = showSearchInput;
|
2168
|
+
|
2169
|
+
this.dropdown.find(".select2-search").toggleClass("select2-search-hidden", !showSearchInput);
|
2170
|
+
this.dropdown.find(".select2-search").toggleClass("select2-offscreen", !showSearchInput);
|
2171
|
+
//add "select2-with-searchbox" to the container if search box is shown
|
2172
|
+
$(this.dropdown, this.container).toggleClass("select2-with-searchbox", showSearchInput);
|
2173
|
+
},
|
2174
|
+
|
2175
|
+
// single
|
2176
|
+
onSelect: function (data, options) {
|
2177
|
+
|
2178
|
+
if (!this.triggerSelect(data)) { return; }
|
2179
|
+
|
2180
|
+
var old = this.opts.element.val(),
|
2181
|
+
oldData = this.data();
|
2182
|
+
|
2183
|
+
this.opts.element.val(this.id(data));
|
2184
|
+
this.updateSelection(data);
|
2185
|
+
|
2186
|
+
this.opts.element.trigger({ type: "select2-selected", val: this.id(data), choice: data });
|
2187
|
+
|
2188
|
+
this.close();
|
2189
|
+
|
2190
|
+
if (!options || !options.noFocus)
|
2191
|
+
this.selection.focus();
|
2192
|
+
|
2193
|
+
if (!equal(old, this.id(data))) { this.triggerChange({added:data,removed:oldData}); }
|
2194
|
+
},
|
2195
|
+
|
2196
|
+
// single
|
2197
|
+
updateSelection: function (data) {
|
2198
|
+
|
2199
|
+
var container=this.selection.find(".select2-chosen"), formatted, cssClass;
|
2200
|
+
|
2201
|
+
this.selection.data("select2-data", data);
|
2202
|
+
|
2203
|
+
container.empty();
|
2204
|
+
formatted=this.opts.formatSelection(data, container, this.opts.escapeMarkup);
|
2205
|
+
if (formatted !== undefined) {
|
2206
|
+
container.append(formatted);
|
2207
|
+
}
|
2208
|
+
cssClass=this.opts.formatSelectionCssClass(data, container);
|
2209
|
+
if (cssClass !== undefined) {
|
2210
|
+
container.addClass(cssClass);
|
2211
|
+
}
|
2212
|
+
|
2213
|
+
this.selection.removeClass("select2-default");
|
2214
|
+
|
2215
|
+
if (this.opts.allowClear && this.getPlaceholder() !== undefined) {
|
2216
|
+
this.container.addClass("select2-allowclear");
|
2217
|
+
}
|
2218
|
+
},
|
2219
|
+
|
2220
|
+
// single
|
2221
|
+
val: function () {
|
2222
|
+
var val,
|
2223
|
+
triggerChange = false,
|
2224
|
+
data = null,
|
2225
|
+
self = this,
|
2226
|
+
oldData = this.data();
|
2227
|
+
|
2228
|
+
if (arguments.length === 0) {
|
2229
|
+
return this.opts.element.val();
|
2230
|
+
}
|
2231
|
+
|
2232
|
+
val = arguments[0];
|
2233
|
+
|
2234
|
+
if (arguments.length > 1) {
|
2235
|
+
triggerChange = arguments[1];
|
2236
|
+
}
|
2237
|
+
|
2238
|
+
if (this.select) {
|
2239
|
+
this.select
|
2240
|
+
.val(val)
|
2241
|
+
.find(":selected").each2(function (i, elm) {
|
2242
|
+
data = self.optionToData(elm);
|
2243
|
+
return false;
|
2244
|
+
});
|
2245
|
+
this.updateSelection(data);
|
2246
|
+
this.setPlaceholder();
|
2247
|
+
if (triggerChange) {
|
2248
|
+
this.triggerChange({added: data, removed:oldData});
|
2249
|
+
}
|
2250
|
+
} else {
|
2251
|
+
// val is an id. !val is true for [undefined,null,'',0] - 0 is legal
|
2252
|
+
if (!val && val !== 0) {
|
2253
|
+
this.clear(triggerChange);
|
2254
|
+
return;
|
2255
|
+
}
|
2256
|
+
if (this.opts.initSelection === undefined) {
|
2257
|
+
throw new Error("cannot call val() if initSelection() is not defined");
|
2258
|
+
}
|
2259
|
+
this.opts.element.val(val);
|
2260
|
+
this.opts.initSelection(this.opts.element, function(data){
|
2261
|
+
self.opts.element.val(!data ? "" : self.id(data));
|
2262
|
+
self.updateSelection(data);
|
2263
|
+
self.setPlaceholder();
|
2264
|
+
if (triggerChange) {
|
2265
|
+
self.triggerChange({added: data, removed:oldData});
|
2266
|
+
}
|
2267
|
+
});
|
2268
|
+
}
|
2269
|
+
},
|
2270
|
+
|
2271
|
+
// single
|
2272
|
+
clearSearch: function () {
|
2273
|
+
this.search.val("");
|
2274
|
+
this.focusser.val("");
|
2275
|
+
},
|
2276
|
+
|
2277
|
+
// single
|
2278
|
+
data: function(value, triggerChange) {
|
2279
|
+
var data;
|
2280
|
+
|
2281
|
+
if (arguments.length === 0) {
|
2282
|
+
data = this.selection.data("select2-data");
|
2283
|
+
if (data == undefined) data = null;
|
2284
|
+
return data;
|
2285
|
+
} else {
|
2286
|
+
if (!value || value === "") {
|
2287
|
+
this.clear(triggerChange);
|
2288
|
+
} else {
|
2289
|
+
data = this.data();
|
2290
|
+
this.opts.element.val(!value ? "" : this.id(value));
|
2291
|
+
this.updateSelection(value);
|
2292
|
+
if (triggerChange) {
|
2293
|
+
this.triggerChange({added: value, removed:data});
|
2294
|
+
}
|
2295
|
+
}
|
2296
|
+
}
|
2297
|
+
}
|
2298
|
+
});
|
2299
|
+
|
2300
|
+
MultiSelect2 = clazz(AbstractSelect2, {
|
2301
|
+
|
2302
|
+
// multi
|
2303
|
+
createContainer: function () {
|
2304
|
+
var container = $(document.createElement("div")).attr({
|
2305
|
+
"class": "select2-container select2-container-multi"
|
2306
|
+
}).html([
|
2307
|
+
"<ul class='select2-choices'>",
|
2308
|
+
" <li class='select2-search-field'>",
|
2309
|
+
" <input type='text' autocomplete='off' autocorrect='off' autocapitilize='off' spellcheck='false' class='select2-input'>",
|
2310
|
+
" </li>",
|
2311
|
+
"</ul>",
|
2312
|
+
"<div class='select2-drop select2-drop-multi select2-display-none'>",
|
2313
|
+
" <ul class='select2-results'>",
|
2314
|
+
" </ul>",
|
2315
|
+
"</div>"].join(""));
|
2316
|
+
return container;
|
2317
|
+
},
|
2318
|
+
|
2319
|
+
// multi
|
2320
|
+
prepareOpts: function () {
|
2321
|
+
var opts = this.parent.prepareOpts.apply(this, arguments),
|
2322
|
+
self=this;
|
2323
|
+
|
2324
|
+
// TODO validate placeholder is a string if specified
|
2325
|
+
|
2326
|
+
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
2327
|
+
// install sthe selection initializer
|
2328
|
+
opts.initSelection = function (element, callback) {
|
2329
|
+
|
2330
|
+
var data = [];
|
2331
|
+
|
2332
|
+
element.find(":selected").each2(function (i, elm) {
|
2333
|
+
data.push(self.optionToData(elm));
|
2334
|
+
});
|
2335
|
+
callback(data);
|
2336
|
+
};
|
2337
|
+
} else if ("data" in opts) {
|
2338
|
+
// install default initSelection when applied to hidden input and data is local
|
2339
|
+
opts.initSelection = opts.initSelection || function (element, callback) {
|
2340
|
+
var ids = splitVal(element.val(), opts.separator);
|
2341
|
+
//search in data by array of ids, storing matching items in a list
|
2342
|
+
var matches = [];
|
2343
|
+
opts.query({
|
2344
|
+
matcher: function(term, text, el){
|
2345
|
+
var is_match = $.grep(ids, function(id) {
|
2346
|
+
return equal(id, opts.id(el));
|
2347
|
+
}).length;
|
2348
|
+
if (is_match) {
|
2349
|
+
matches.push(el);
|
2350
|
+
}
|
2351
|
+
return is_match;
|
2352
|
+
},
|
2353
|
+
callback: !$.isFunction(callback) ? $.noop : function() {
|
2354
|
+
// reorder matches based on the order they appear in the ids array because right now
|
2355
|
+
// they are in the order in which they appear in data array
|
2356
|
+
var ordered = [];
|
2357
|
+
for (var i = 0; i < ids.length; i++) {
|
2358
|
+
var id = ids[i];
|
2359
|
+
for (var j = 0; j < matches.length; j++) {
|
2360
|
+
var match = matches[j];
|
2361
|
+
if (equal(id, opts.id(match))) {
|
2362
|
+
ordered.push(match);
|
2363
|
+
matches.splice(j, 1);
|
2364
|
+
break;
|
2365
|
+
}
|
2366
|
+
}
|
2367
|
+
}
|
2368
|
+
callback(ordered);
|
2369
|
+
}
|
2370
|
+
});
|
2371
|
+
};
|
2372
|
+
}
|
2373
|
+
|
2374
|
+
return opts;
|
2375
|
+
},
|
2376
|
+
|
2377
|
+
selectChoice: function (choice) {
|
2378
|
+
|
2379
|
+
var selected = this.container.find(".select2-search-choice-focus");
|
2380
|
+
if (selected.length && choice && choice[0] == selected[0]) {
|
2381
|
+
|
2382
|
+
} else {
|
2383
|
+
if (selected.length) {
|
2384
|
+
this.opts.element.trigger("choice-deselected", selected);
|
2385
|
+
}
|
2386
|
+
selected.removeClass("select2-search-choice-focus");
|
2387
|
+
if (choice && choice.length) {
|
2388
|
+
this.close();
|
2389
|
+
choice.addClass("select2-search-choice-focus");
|
2390
|
+
this.opts.element.trigger("choice-selected", choice);
|
2391
|
+
}
|
2392
|
+
}
|
2393
|
+
},
|
2394
|
+
|
2395
|
+
// multi
|
2396
|
+
initContainer: function () {
|
2397
|
+
|
2398
|
+
var selector = ".select2-choices", selection;
|
2399
|
+
|
2400
|
+
this.searchContainer = this.container.find(".select2-search-field");
|
2401
|
+
this.selection = selection = this.container.find(selector);
|
2402
|
+
|
2403
|
+
var _this = this;
|
2404
|
+
this.selection.on("click", ".select2-search-choice", function (e) {
|
2405
|
+
//killEvent(e);
|
2406
|
+
_this.search[0].focus();
|
2407
|
+
_this.selectChoice($(this));
|
2408
|
+
});
|
2409
|
+
|
2410
|
+
// rewrite labels from original element to focusser
|
2411
|
+
this.search.attr("id", "s2id_autogen"+nextUid());
|
2412
|
+
$("label[for='" + this.opts.element.attr("id") + "']")
|
2413
|
+
.attr('for', this.search.attr('id'));
|
2414
|
+
|
2415
|
+
this.search.on("input paste", this.bind(function() {
|
2416
|
+
if (!this.isInterfaceEnabled()) return;
|
2417
|
+
if (!this.opened()) {
|
2418
|
+
this.open();
|
2419
|
+
}
|
2420
|
+
}));
|
2421
|
+
|
2422
|
+
this.search.attr("tabindex", this.elementTabIndex);
|
2423
|
+
|
2424
|
+
this.keydowns = 0;
|
2425
|
+
this.search.on("keydown", this.bind(function (e) {
|
2426
|
+
if (!this.isInterfaceEnabled()) return;
|
2427
|
+
|
2428
|
+
++this.keydowns;
|
2429
|
+
var selected = selection.find(".select2-search-choice-focus");
|
2430
|
+
var prev = selected.prev(".select2-search-choice:not(.select2-locked)");
|
2431
|
+
var next = selected.next(".select2-search-choice:not(.select2-locked)");
|
2432
|
+
var pos = getCursorInfo(this.search);
|
2433
|
+
|
2434
|
+
if (selected.length &&
|
2435
|
+
(e.which == KEY.LEFT || e.which == KEY.RIGHT || e.which == KEY.BACKSPACE || e.which == KEY.DELETE || e.which == KEY.ENTER)) {
|
2436
|
+
var selectedChoice = selected;
|
2437
|
+
if (e.which == KEY.LEFT && prev.length) {
|
2438
|
+
selectedChoice = prev;
|
2439
|
+
}
|
2440
|
+
else if (e.which == KEY.RIGHT) {
|
2441
|
+
selectedChoice = next.length ? next : null;
|
2442
|
+
}
|
2443
|
+
else if (e.which === KEY.BACKSPACE) {
|
2444
|
+
this.unselect(selected.first());
|
2445
|
+
this.search.width(10);
|
2446
|
+
selectedChoice = prev.length ? prev : next;
|
2447
|
+
} else if (e.which == KEY.DELETE) {
|
2448
|
+
this.unselect(selected.first());
|
2449
|
+
this.search.width(10);
|
2450
|
+
selectedChoice = next.length ? next : null;
|
2451
|
+
} else if (e.which == KEY.ENTER) {
|
2452
|
+
selectedChoice = null;
|
2453
|
+
}
|
2454
|
+
|
2455
|
+
this.selectChoice(selectedChoice);
|
2456
|
+
killEvent(e);
|
2457
|
+
if (!selectedChoice || !selectedChoice.length) {
|
2458
|
+
this.open();
|
2459
|
+
}
|
2460
|
+
return;
|
2461
|
+
} else if (((e.which === KEY.BACKSPACE && this.keydowns == 1)
|
2462
|
+
|| e.which == KEY.LEFT) && (pos.offset == 0 && !pos.length)) {
|
2463
|
+
|
2464
|
+
this.selectChoice(selection.find(".select2-search-choice:not(.select2-locked)").last());
|
2465
|
+
killEvent(e);
|
2466
|
+
return;
|
2467
|
+
} else {
|
2468
|
+
this.selectChoice(null);
|
2469
|
+
}
|
2470
|
+
|
2471
|
+
if (this.opened()) {
|
2472
|
+
switch (e.which) {
|
2473
|
+
case KEY.UP:
|
2474
|
+
case KEY.DOWN:
|
2475
|
+
this.moveHighlight((e.which === KEY.UP) ? -1 : 1);
|
2476
|
+
killEvent(e);
|
2477
|
+
return;
|
2478
|
+
case KEY.ENTER:
|
2479
|
+
this.selectHighlighted();
|
2480
|
+
killEvent(e);
|
2481
|
+
return;
|
2482
|
+
case KEY.TAB:
|
2483
|
+
this.selectHighlighted({noFocus:true});
|
2484
|
+
this.close();
|
2485
|
+
return;
|
2486
|
+
case KEY.ESC:
|
2487
|
+
this.cancel(e);
|
2488
|
+
killEvent(e);
|
2489
|
+
return;
|
2490
|
+
}
|
2491
|
+
}
|
2492
|
+
|
2493
|
+
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e)
|
2494
|
+
|| e.which === KEY.BACKSPACE || e.which === KEY.ESC) {
|
2495
|
+
return;
|
2496
|
+
}
|
2497
|
+
|
2498
|
+
if (e.which === KEY.ENTER) {
|
2499
|
+
if (this.opts.openOnEnter === false) {
|
2500
|
+
return;
|
2501
|
+
} else if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
|
2502
|
+
return;
|
2503
|
+
}
|
2504
|
+
}
|
2505
|
+
|
2506
|
+
this.open();
|
2507
|
+
|
2508
|
+
if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
|
2509
|
+
// prevent the page from scrolling
|
2510
|
+
killEvent(e);
|
2511
|
+
}
|
2512
|
+
|
2513
|
+
if (e.which === KEY.ENTER) {
|
2514
|
+
// prevent form from being submitted
|
2515
|
+
killEvent(e);
|
2516
|
+
}
|
2517
|
+
|
2518
|
+
}));
|
2519
|
+
|
2520
|
+
this.search.on("keyup", this.bind(function (e) {
|
2521
|
+
this.keydowns = 0;
|
2522
|
+
this.resizeSearch();
|
2523
|
+
})
|
2524
|
+
);
|
2525
|
+
|
2526
|
+
this.search.on("blur", this.bind(function(e) {
|
2527
|
+
this.container.removeClass("select2-container-active");
|
2528
|
+
this.search.removeClass("select2-focused");
|
2529
|
+
this.selectChoice(null);
|
2530
|
+
if (!this.opened()) this.clearSearch();
|
2531
|
+
e.stopImmediatePropagation();
|
2532
|
+
this.opts.element.trigger($.Event("select2-blur"));
|
2533
|
+
}));
|
2534
|
+
|
2535
|
+
this.container.on("click", selector, this.bind(function (e) {
|
2536
|
+
if (!this.isInterfaceEnabled()) return;
|
2537
|
+
if ($(e.target).closest(".select2-search-choice").length > 0) {
|
2538
|
+
// clicked inside a select2 search choice, do not open
|
2539
|
+
return;
|
2540
|
+
}
|
2541
|
+
this.selectChoice(null);
|
2542
|
+
this.clearPlaceholder();
|
2543
|
+
if (!this.container.hasClass("select2-container-active")) {
|
2544
|
+
this.opts.element.trigger($.Event("select2-focus"));
|
2545
|
+
}
|
2546
|
+
this.open();
|
2547
|
+
this.focusSearch();
|
2548
|
+
e.preventDefault();
|
2549
|
+
}));
|
2550
|
+
|
2551
|
+
this.container.on("focus", selector, this.bind(function () {
|
2552
|
+
if (!this.isInterfaceEnabled()) return;
|
2553
|
+
if (!this.container.hasClass("select2-container-active")) {
|
2554
|
+
this.opts.element.trigger($.Event("select2-focus"));
|
2555
|
+
}
|
2556
|
+
this.container.addClass("select2-container-active");
|
2557
|
+
this.dropdown.addClass("select2-drop-active");
|
2558
|
+
this.clearPlaceholder();
|
2559
|
+
}));
|
2560
|
+
|
2561
|
+
this.initContainerWidth();
|
2562
|
+
this.opts.element.addClass("select2-offscreen");
|
2563
|
+
|
2564
|
+
// set the placeholder if necessary
|
2565
|
+
this.clearSearch();
|
2566
|
+
},
|
2567
|
+
|
2568
|
+
// multi
|
2569
|
+
enableInterface: function() {
|
2570
|
+
if (this.parent.enableInterface.apply(this, arguments)) {
|
2571
|
+
this.search.prop("disabled", !this.isInterfaceEnabled());
|
2572
|
+
}
|
2573
|
+
},
|
2574
|
+
|
2575
|
+
// multi
|
2576
|
+
initSelection: function () {
|
2577
|
+
var data;
|
2578
|
+
if (this.opts.element.val() === "" && this.opts.element.text() === "") {
|
2579
|
+
this.updateSelection([]);
|
2580
|
+
this.close();
|
2581
|
+
// set the placeholder if necessary
|
2582
|
+
this.clearSearch();
|
2583
|
+
}
|
2584
|
+
if (this.select || this.opts.element.val() !== "") {
|
2585
|
+
var self = this;
|
2586
|
+
this.opts.initSelection.call(null, this.opts.element, function(data){
|
2587
|
+
if (data !== undefined && data !== null) {
|
2588
|
+
self.updateSelection(data);
|
2589
|
+
self.close();
|
2590
|
+
// set the placeholder if necessary
|
2591
|
+
self.clearSearch();
|
2592
|
+
}
|
2593
|
+
});
|
2594
|
+
}
|
2595
|
+
},
|
2596
|
+
|
2597
|
+
// multi
|
2598
|
+
clearSearch: function () {
|
2599
|
+
var placeholder = this.getPlaceholder(),
|
2600
|
+
maxWidth = this.getMaxSearchWidth();
|
2601
|
+
|
2602
|
+
if (placeholder !== undefined && this.getVal().length === 0 && this.search.hasClass("select2-focused") === false) {
|
2603
|
+
this.search.val(placeholder).addClass("select2-default");
|
2604
|
+
// stretch the search box to full width of the container so as much of the placeholder is visible as possible
|
2605
|
+
// 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
|
2606
|
+
this.search.width(maxWidth > 0 ? maxWidth : this.container.css("width"));
|
2607
|
+
} else {
|
2608
|
+
this.search.val("").width(10);
|
2609
|
+
}
|
2610
|
+
},
|
2611
|
+
|
2612
|
+
// multi
|
2613
|
+
clearPlaceholder: function () {
|
2614
|
+
if (this.search.hasClass("select2-default")) {
|
2615
|
+
this.search.val("").removeClass("select2-default");
|
2616
|
+
}
|
2617
|
+
},
|
2618
|
+
|
2619
|
+
// multi
|
2620
|
+
opening: function () {
|
2621
|
+
this.clearPlaceholder(); // should be done before super so placeholder is not used to search
|
2622
|
+
this.resizeSearch();
|
2623
|
+
|
2624
|
+
this.parent.opening.apply(this, arguments);
|
2625
|
+
|
2626
|
+
this.focusSearch();
|
2627
|
+
|
2628
|
+
this.updateResults(true);
|
2629
|
+
this.search.focus();
|
2630
|
+
this.opts.element.trigger($.Event("select2-open"));
|
2631
|
+
},
|
2632
|
+
|
2633
|
+
// multi
|
2634
|
+
close: function () {
|
2635
|
+
if (!this.opened()) return;
|
2636
|
+
this.parent.close.apply(this, arguments);
|
2637
|
+
},
|
2638
|
+
|
2639
|
+
// multi
|
2640
|
+
focus: function () {
|
2641
|
+
this.close();
|
2642
|
+
this.search.focus();
|
2643
|
+
},
|
2644
|
+
|
2645
|
+
// multi
|
2646
|
+
isFocused: function () {
|
2647
|
+
return this.search.hasClass("select2-focused");
|
2648
|
+
},
|
2649
|
+
|
2650
|
+
// multi
|
2651
|
+
updateSelection: function (data) {
|
2652
|
+
var ids = [], filtered = [], self = this;
|
2653
|
+
|
2654
|
+
// filter out duplicates
|
2655
|
+
$(data).each(function () {
|
2656
|
+
if (indexOf(self.id(this), ids) < 0) {
|
2657
|
+
ids.push(self.id(this));
|
2658
|
+
filtered.push(this);
|
2659
|
+
}
|
2660
|
+
});
|
2661
|
+
data = filtered;
|
2662
|
+
|
2663
|
+
this.selection.find(".select2-search-choice").remove();
|
2664
|
+
$(data).each(function () {
|
2665
|
+
self.addSelectedChoice(this);
|
2666
|
+
});
|
2667
|
+
self.postprocessResults();
|
2668
|
+
},
|
2669
|
+
|
2670
|
+
// multi
|
2671
|
+
tokenize: function() {
|
2672
|
+
var input = this.search.val();
|
2673
|
+
input = this.opts.tokenizer.call(this, input, this.data(), this.bind(this.onSelect), this.opts);
|
2674
|
+
if (input != null && input != undefined) {
|
2675
|
+
this.search.val(input);
|
2676
|
+
if (input.length > 0) {
|
2677
|
+
this.open();
|
2678
|
+
}
|
2679
|
+
}
|
2680
|
+
|
2681
|
+
},
|
2682
|
+
|
2683
|
+
// multi
|
2684
|
+
onSelect: function (data, options) {
|
2685
|
+
|
2686
|
+
if (!this.triggerSelect(data)) { return; }
|
2687
|
+
|
2688
|
+
this.addSelectedChoice(data);
|
2689
|
+
|
2690
|
+
this.opts.element.trigger({ type: "selected", val: this.id(data), choice: data });
|
2691
|
+
|
2692
|
+
if (this.select || !this.opts.closeOnSelect) this.postprocessResults();
|
2693
|
+
|
2694
|
+
if (this.opts.closeOnSelect) {
|
2695
|
+
this.close();
|
2696
|
+
this.search.width(10);
|
2697
|
+
} else {
|
2698
|
+
if (this.countSelectableResults()>0) {
|
2699
|
+
this.search.width(10);
|
2700
|
+
this.resizeSearch();
|
2701
|
+
if (this.getMaximumSelectionSize() > 0 && this.val().length >= this.getMaximumSelectionSize()) {
|
2702
|
+
// if we reached max selection size repaint the results so choices
|
2703
|
+
// are replaced with the max selection reached message
|
2704
|
+
this.updateResults(true);
|
2705
|
+
}
|
2706
|
+
this.positionDropdown();
|
2707
|
+
} else {
|
2708
|
+
// if nothing left to select close
|
2709
|
+
this.close();
|
2710
|
+
this.search.width(10);
|
2711
|
+
}
|
2712
|
+
}
|
2713
|
+
|
2714
|
+
// since its not possible to select an element that has already been
|
2715
|
+
// added we do not need to check if this is a new element before firing change
|
2716
|
+
this.triggerChange({ added: data });
|
2717
|
+
|
2718
|
+
if (!options || !options.noFocus)
|
2719
|
+
this.focusSearch();
|
2720
|
+
},
|
2721
|
+
|
2722
|
+
// multi
|
2723
|
+
cancel: function () {
|
2724
|
+
this.close();
|
2725
|
+
this.focusSearch();
|
2726
|
+
},
|
2727
|
+
|
2728
|
+
addSelectedChoice: function (data) {
|
2729
|
+
var enableChoice = !data.locked,
|
2730
|
+
enabledItem = $(
|
2731
|
+
"<li class='select2-search-choice'>" +
|
2732
|
+
" <div></div>" +
|
2733
|
+
" <a href='#' onclick='return false;' class='select2-search-choice-close' tabindex='-1'></a>" +
|
2734
|
+
"</li>"),
|
2735
|
+
disabledItem = $(
|
2736
|
+
"<li class='select2-search-choice select2-locked'>" +
|
2737
|
+
"<div></div>" +
|
2738
|
+
"</li>");
|
2739
|
+
var choice = enableChoice ? enabledItem : disabledItem,
|
2740
|
+
id = this.id(data),
|
2741
|
+
val = this.getVal(),
|
2742
|
+
formatted,
|
2743
|
+
cssClass;
|
2744
|
+
|
2745
|
+
formatted=this.opts.formatSelection(data, choice.find("div"), this.opts.escapeMarkup);
|
2746
|
+
if (formatted != undefined) {
|
2747
|
+
choice.find("div").replaceWith("<div>"+formatted+"</div>");
|
2748
|
+
}
|
2749
|
+
cssClass=this.opts.formatSelectionCssClass(data, choice.find("div"));
|
2750
|
+
if (cssClass != undefined) {
|
2751
|
+
choice.addClass(cssClass);
|
2752
|
+
}
|
2753
|
+
|
2754
|
+
if(enableChoice){
|
2755
|
+
choice.find(".select2-search-choice-close")
|
2756
|
+
.on("mousedown", killEvent)
|
2757
|
+
.on("click dblclick", this.bind(function (e) {
|
2758
|
+
if (!this.isInterfaceEnabled()) return;
|
2759
|
+
|
2760
|
+
$(e.target).closest(".select2-search-choice").fadeOut('fast', this.bind(function(){
|
2761
|
+
this.unselect($(e.target));
|
2762
|
+
this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
|
2763
|
+
this.close();
|
2764
|
+
this.focusSearch();
|
2765
|
+
})).dequeue();
|
2766
|
+
killEvent(e);
|
2767
|
+
})).on("focus", this.bind(function () {
|
2768
|
+
if (!this.isInterfaceEnabled()) return;
|
2769
|
+
this.container.addClass("select2-container-active");
|
2770
|
+
this.dropdown.addClass("select2-drop-active");
|
2771
|
+
}));
|
2772
|
+
}
|
2773
|
+
|
2774
|
+
choice.data("select2-data", data);
|
2775
|
+
choice.insertBefore(this.searchContainer);
|
2776
|
+
|
2777
|
+
val.push(id);
|
2778
|
+
this.setVal(val);
|
2779
|
+
},
|
2780
|
+
|
2781
|
+
// multi
|
2782
|
+
unselect: function (selected) {
|
2783
|
+
var val = this.getVal(),
|
2784
|
+
data,
|
2785
|
+
index;
|
2786
|
+
|
2787
|
+
selected = selected.closest(".select2-search-choice");
|
2788
|
+
|
2789
|
+
if (selected.length === 0) {
|
2790
|
+
throw "Invalid argument: " + selected + ". Must be .select2-search-choice";
|
2791
|
+
}
|
2792
|
+
|
2793
|
+
data = selected.data("select2-data");
|
2794
|
+
|
2795
|
+
if (!data) {
|
2796
|
+
// prevent a race condition when the 'x' is clicked really fast repeatedly the event can be queued
|
2797
|
+
// and invoked on an element already removed
|
2798
|
+
return;
|
2799
|
+
}
|
2800
|
+
|
2801
|
+
index = indexOf(this.id(data), val);
|
2802
|
+
|
2803
|
+
if (index >= 0) {
|
2804
|
+
val.splice(index, 1);
|
2805
|
+
this.setVal(val);
|
2806
|
+
if (this.select) this.postprocessResults();
|
2807
|
+
}
|
2808
|
+
selected.remove();
|
2809
|
+
|
2810
|
+
this.opts.element.trigger({ type: "removed", val: this.id(data), choice: data });
|
2811
|
+
this.triggerChange({ removed: data });
|
2812
|
+
},
|
2813
|
+
|
2814
|
+
// multi
|
2815
|
+
postprocessResults: function (data, initial, noHighlightUpdate) {
|
2816
|
+
var val = this.getVal(),
|
2817
|
+
choices = this.results.find(".select2-result"),
|
2818
|
+
compound = this.results.find(".select2-result-with-children"),
|
2819
|
+
self = this;
|
2820
|
+
|
2821
|
+
choices.each2(function (i, choice) {
|
2822
|
+
var id = self.id(choice.data("select2-data"));
|
2823
|
+
if (indexOf(id, val) >= 0) {
|
2824
|
+
choice.addClass("select2-selected");
|
2825
|
+
// mark all children of the selected parent as selected
|
2826
|
+
choice.find(".select2-result-selectable").addClass("select2-selected");
|
2827
|
+
}
|
2828
|
+
});
|
2829
|
+
|
2830
|
+
compound.each2(function(i, choice) {
|
2831
|
+
// hide an optgroup if it doesnt have any selectable children
|
2832
|
+
if (!choice.is('.select2-result-selectable')
|
2833
|
+
&& choice.find(".select2-result-selectable:not(.select2-selected)").length === 0) {
|
2834
|
+
choice.addClass("select2-selected");
|
2835
|
+
}
|
2836
|
+
});
|
2837
|
+
|
2838
|
+
if (this.highlight() == -1 && noHighlightUpdate !== false){
|
2839
|
+
self.highlight(0);
|
2840
|
+
}
|
2841
|
+
|
2842
|
+
//If all results are chosen render formatNoMAtches
|
2843
|
+
if(!this.opts.createSearchChoice && !choices.filter('.select2-result:not(.select2-selected)').length > 0){
|
2844
|
+
if(!data || data && !data.more && this.results.find(".select2-no-results").length === 0) {
|
2845
|
+
if (checkFormatter(self.opts.formatNoMatches, "formatNoMatches")) {
|
2846
|
+
this.results.append("<li class='select2-no-results'>" + self.opts.formatNoMatches(self.search.val()) + "</li>");
|
2847
|
+
}
|
2848
|
+
}
|
2849
|
+
}
|
2850
|
+
|
2851
|
+
},
|
2852
|
+
|
2853
|
+
// multi
|
2854
|
+
getMaxSearchWidth: function() {
|
2855
|
+
return this.selection.width() - getSideBorderPadding(this.search);
|
2856
|
+
},
|
2857
|
+
|
2858
|
+
// multi
|
2859
|
+
resizeSearch: function () {
|
2860
|
+
var minimumWidth, left, maxWidth, containerLeft, searchWidth,
|
2861
|
+
sideBorderPadding = getSideBorderPadding(this.search);
|
2862
|
+
|
2863
|
+
minimumWidth = measureTextWidth(this.search) + 10;
|
2864
|
+
|
2865
|
+
left = this.search.offset().left;
|
2866
|
+
|
2867
|
+
maxWidth = this.selection.width();
|
2868
|
+
containerLeft = this.selection.offset().left;
|
2869
|
+
|
2870
|
+
searchWidth = maxWidth - (left - containerLeft) - sideBorderPadding;
|
2871
|
+
|
2872
|
+
if (searchWidth < minimumWidth) {
|
2873
|
+
searchWidth = maxWidth - sideBorderPadding;
|
2874
|
+
}
|
2875
|
+
|
2876
|
+
if (searchWidth < 40) {
|
2877
|
+
searchWidth = maxWidth - sideBorderPadding;
|
2878
|
+
}
|
2879
|
+
|
2880
|
+
if (searchWidth <= 0) {
|
2881
|
+
searchWidth = minimumWidth;
|
2882
|
+
}
|
2883
|
+
|
2884
|
+
this.search.width(searchWidth);
|
2885
|
+
},
|
2886
|
+
|
2887
|
+
// multi
|
2888
|
+
getVal: function () {
|
2889
|
+
var val;
|
2890
|
+
if (this.select) {
|
2891
|
+
val = this.select.val();
|
2892
|
+
return val === null ? [] : val;
|
2893
|
+
} else {
|
2894
|
+
val = this.opts.element.val();
|
2895
|
+
return splitVal(val, this.opts.separator);
|
2896
|
+
}
|
2897
|
+
},
|
2898
|
+
|
2899
|
+
// multi
|
2900
|
+
setVal: function (val) {
|
2901
|
+
var unique;
|
2902
|
+
if (this.select) {
|
2903
|
+
this.select.val(val);
|
2904
|
+
} else {
|
2905
|
+
unique = [];
|
2906
|
+
// filter out duplicates
|
2907
|
+
$(val).each(function () {
|
2908
|
+
if (indexOf(this, unique) < 0) unique.push(this);
|
2909
|
+
});
|
2910
|
+
this.opts.element.val(unique.length === 0 ? "" : unique.join(this.opts.separator));
|
2911
|
+
}
|
2912
|
+
},
|
2913
|
+
|
2914
|
+
// multi
|
2915
|
+
buildChangeDetails: function (old, current) {
|
2916
|
+
var current = current.slice(0),
|
2917
|
+
old = old.slice(0);
|
2918
|
+
|
2919
|
+
// remove intersection from each array
|
2920
|
+
for (var i = 0; i < current.length; i++) {
|
2921
|
+
for (var j = 0; j < old.length; j++) {
|
2922
|
+
if (equal(this.opts.id(current[i]), this.opts.id(old[j]))) {
|
2923
|
+
current.splice(i, 1);
|
2924
|
+
i--;
|
2925
|
+
old.splice(j, 1);
|
2926
|
+
j--;
|
2927
|
+
}
|
2928
|
+
}
|
2929
|
+
}
|
2930
|
+
|
2931
|
+
return {added: current, removed: old};
|
2932
|
+
},
|
2933
|
+
|
2934
|
+
|
2935
|
+
// multi
|
2936
|
+
val: function (val, triggerChange) {
|
2937
|
+
var oldData, self=this, changeDetails;
|
2938
|
+
|
2939
|
+
if (arguments.length === 0) {
|
2940
|
+
return this.getVal();
|
2941
|
+
}
|
2942
|
+
|
2943
|
+
oldData=this.data();
|
2944
|
+
if (!oldData.length) oldData=[];
|
2945
|
+
|
2946
|
+
// val is an id. !val is true for [undefined,null,'',0] - 0 is legal
|
2947
|
+
if (!val && val !== 0) {
|
2948
|
+
this.opts.element.val("");
|
2949
|
+
this.updateSelection([]);
|
2950
|
+
this.clearSearch();
|
2951
|
+
if (triggerChange) {
|
2952
|
+
this.triggerChange({added: this.data(), removed: oldData});
|
2953
|
+
}
|
2954
|
+
return;
|
2955
|
+
}
|
2956
|
+
|
2957
|
+
// val is a list of ids
|
2958
|
+
this.setVal(val);
|
2959
|
+
|
2960
|
+
if (this.select) {
|
2961
|
+
this.opts.initSelection(this.select, this.bind(this.updateSelection));
|
2962
|
+
if (triggerChange) {
|
2963
|
+
this.triggerChange(this.buildChangeDetails(oldData, this.data()));
|
2964
|
+
}
|
2965
|
+
} else {
|
2966
|
+
if (this.opts.initSelection === undefined) {
|
2967
|
+
throw new Error("val() cannot be called if initSelection() is not defined");
|
2968
|
+
}
|
2969
|
+
|
2970
|
+
this.opts.initSelection(this.opts.element, function(data){
|
2971
|
+
var ids=$.map(data, self.id);
|
2972
|
+
self.setVal(ids);
|
2973
|
+
self.updateSelection(data);
|
2974
|
+
self.clearSearch();
|
2975
|
+
if (triggerChange) {
|
2976
|
+
self.triggerChange(this.buildChangeDetails(oldData, this.data()));
|
2977
|
+
}
|
2978
|
+
});
|
2979
|
+
}
|
2980
|
+
this.clearSearch();
|
2981
|
+
},
|
2982
|
+
|
2983
|
+
// multi
|
2984
|
+
onSortStart: function() {
|
2985
|
+
if (this.select) {
|
2986
|
+
throw new Error("Sorting of elements is not supported when attached to <select>. Attach to <input type='hidden'/> instead.");
|
2987
|
+
}
|
2988
|
+
|
2989
|
+
// collapse search field into 0 width so its container can be collapsed as well
|
2990
|
+
this.search.width(0);
|
2991
|
+
// hide the container
|
2992
|
+
this.searchContainer.hide();
|
2993
|
+
},
|
2994
|
+
|
2995
|
+
// multi
|
2996
|
+
onSortEnd:function() {
|
2997
|
+
|
2998
|
+
var val=[], self=this;
|
2999
|
+
|
3000
|
+
// show search and move it to the end of the list
|
3001
|
+
this.searchContainer.show();
|
3002
|
+
// make sure the search container is the last item in the list
|
3003
|
+
this.searchContainer.appendTo(this.searchContainer.parent());
|
3004
|
+
// since we collapsed the width in dragStarted, we resize it here
|
3005
|
+
this.resizeSearch();
|
3006
|
+
|
3007
|
+
// update selection
|
3008
|
+
this.selection.find(".select2-search-choice").each(function() {
|
3009
|
+
val.push(self.opts.id($(this).data("select2-data")));
|
3010
|
+
});
|
3011
|
+
this.setVal(val);
|
3012
|
+
this.triggerChange();
|
3013
|
+
},
|
3014
|
+
|
3015
|
+
// multi
|
3016
|
+
data: function(values, triggerChange) {
|
3017
|
+
var self=this, ids, old;
|
3018
|
+
if (arguments.length === 0) {
|
3019
|
+
return this.selection
|
3020
|
+
.find(".select2-search-choice")
|
3021
|
+
.map(function() { return $(this).data("select2-data"); })
|
3022
|
+
.get();
|
3023
|
+
} else {
|
3024
|
+
old = this.data();
|
3025
|
+
if (!values) { values = []; }
|
3026
|
+
ids = $.map(values, function(e) { return self.opts.id(e); });
|
3027
|
+
this.setVal(ids);
|
3028
|
+
this.updateSelection(values);
|
3029
|
+
this.clearSearch();
|
3030
|
+
if (triggerChange) {
|
3031
|
+
this.triggerChange(this.buildChangeDetails(old, this.data()));
|
3032
|
+
}
|
3033
|
+
}
|
3034
|
+
}
|
3035
|
+
});
|
3036
|
+
|
3037
|
+
$.fn.select2 = function () {
|
3038
|
+
|
3039
|
+
var args = Array.prototype.slice.call(arguments, 0),
|
3040
|
+
opts,
|
3041
|
+
select2,
|
3042
|
+
method, value, multiple,
|
3043
|
+
allowedMethods = ["val", "destroy", "opened", "open", "close", "focus", "isFocused", "container", "dropdown", "onSortStart", "onSortEnd", "enable", "readonly", "positionDropdown", "data", "search"],
|
3044
|
+
valueMethods = ["opened", "isFocused", "container", "dropdown"],
|
3045
|
+
propertyMethods = ["val", "data"],
|
3046
|
+
methodsMap = { search: "externalSearch" };
|
3047
|
+
|
3048
|
+
this.each(function () {
|
3049
|
+
if (args.length === 0 || typeof(args[0]) === "object") {
|
3050
|
+
opts = args.length === 0 ? {} : $.extend({}, args[0]);
|
3051
|
+
opts.element = $(this);
|
3052
|
+
|
3053
|
+
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
3054
|
+
multiple = opts.element.prop("multiple");
|
3055
|
+
} else {
|
3056
|
+
multiple = opts.multiple || false;
|
3057
|
+
if ("tags" in opts) {opts.multiple = multiple = true;}
|
3058
|
+
}
|
3059
|
+
|
3060
|
+
select2 = multiple ? new MultiSelect2() : new SingleSelect2();
|
3061
|
+
select2.init(opts);
|
3062
|
+
} else if (typeof(args[0]) === "string") {
|
3063
|
+
|
3064
|
+
if (indexOf(args[0], allowedMethods) < 0) {
|
3065
|
+
throw "Unknown method: " + args[0];
|
3066
|
+
}
|
3067
|
+
|
3068
|
+
value = undefined;
|
3069
|
+
select2 = $(this).data("select2");
|
3070
|
+
if (select2 === undefined) return;
|
3071
|
+
|
3072
|
+
method=args[0];
|
3073
|
+
|
3074
|
+
if (method === "container") {
|
3075
|
+
value = select2.container;
|
3076
|
+
} else if (method === "dropdown") {
|
3077
|
+
value = select2.dropdown;
|
3078
|
+
} else {
|
3079
|
+
if (methodsMap[method]) method = methodsMap[method];
|
3080
|
+
|
3081
|
+
value = select2[method].apply(select2, args.slice(1));
|
3082
|
+
}
|
3083
|
+
if (indexOf(args[0], valueMethods) >= 0
|
3084
|
+
|| (indexOf(args[0], propertyMethods) && args.length == 1)) {
|
3085
|
+
return false; // abort the iteration, ready to return first matched value
|
3086
|
+
}
|
3087
|
+
} else {
|
3088
|
+
throw "Invalid arguments to select2 plugin: " + args;
|
3089
|
+
}
|
3090
|
+
});
|
3091
|
+
return (value === undefined) ? this : value;
|
3092
|
+
};
|
3093
|
+
|
3094
|
+
// plugin defaults, accessible to users
|
3095
|
+
$.fn.select2.defaults = {
|
3096
|
+
width: "copy",
|
3097
|
+
loadMorePadding: 0,
|
3098
|
+
closeOnSelect: true,
|
3099
|
+
openOnEnter: true,
|
3100
|
+
containerCss: {},
|
3101
|
+
dropdownCss: {},
|
3102
|
+
containerCssClass: "",
|
3103
|
+
dropdownCssClass: "",
|
3104
|
+
formatResult: function(result, container, query, escapeMarkup) {
|
3105
|
+
var markup=[];
|
3106
|
+
markMatch(result.text, query.term, markup, escapeMarkup);
|
3107
|
+
return markup.join("");
|
3108
|
+
},
|
3109
|
+
formatSelection: function (data, container, escapeMarkup) {
|
3110
|
+
return data ? escapeMarkup(data.text) : undefined;
|
3111
|
+
},
|
3112
|
+
sortResults: function (results, container, query) {
|
3113
|
+
return results;
|
3114
|
+
},
|
3115
|
+
formatResultCssClass: function(data) {return undefined;},
|
3116
|
+
formatSelectionCssClass: function(data, container) {return undefined;},
|
3117
|
+
formatNoMatches: function () { return "No matches found"; },
|
3118
|
+
formatInputTooShort: function (input, min) { var n = min - input.length; return "Please enter " + n + " more character" + (n == 1? "" : "s"); },
|
3119
|
+
formatInputTooLong: function (input, max) { var n = input.length - max; return "Please delete " + n + " character" + (n == 1? "" : "s"); },
|
3120
|
+
formatSelectionTooBig: function (limit) { return "You can only select " + limit + " item" + (limit == 1 ? "" : "s"); },
|
3121
|
+
formatLoadMore: function (pageNumber) { return "Loading more results..."; },
|
3122
|
+
formatSearching: function () { return "Searching..."; },
|
3123
|
+
minimumResultsForSearch: 0,
|
3124
|
+
minimumInputLength: 0,
|
3125
|
+
maximumInputLength: null,
|
3126
|
+
maximumSelectionSize: 0,
|
3127
|
+
id: function (e) { return e.id; },
|
3128
|
+
matcher: function(term, text) {
|
3129
|
+
return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
|
3130
|
+
},
|
3131
|
+
separator: ",",
|
3132
|
+
tokenSeparators: [],
|
3133
|
+
tokenizer: defaultTokenizer,
|
3134
|
+
escapeMarkup: defaultEscapeMarkup,
|
3135
|
+
blurOnChange: false,
|
3136
|
+
selectOnBlur: false,
|
3137
|
+
adaptContainerCssClass: function(c) { return c; },
|
3138
|
+
adaptDropdownCssClass: function(c) { return null; }
|
3139
|
+
};
|
3140
|
+
|
3141
|
+
$.fn.select2.ajaxDefaults = {
|
3142
|
+
transport: $.ajax,
|
3143
|
+
params: {
|
3144
|
+
type: "GET",
|
3145
|
+
cache: false,
|
3146
|
+
dataType: "json"
|
3147
|
+
}
|
3148
|
+
};
|
3149
|
+
|
3150
|
+
// exports
|
3151
|
+
window.Select2 = {
|
3152
|
+
query: {
|
3153
|
+
ajax: ajax,
|
3154
|
+
local: local,
|
3155
|
+
tags: tags
|
3156
|
+
}, util: {
|
3157
|
+
debounce: debounce,
|
3158
|
+
markMatch: markMatch,
|
3159
|
+
escapeMarkup: defaultEscapeMarkup,
|
3160
|
+
stripDiacritics: stripDiacritics
|
3161
|
+
}, "class": {
|
3162
|
+
"abstract": AbstractSelect2,
|
3163
|
+
"single": SingleSelect2,
|
3164
|
+
"multi": MultiSelect2
|
3165
|
+
}
|
3166
|
+
};
|
3167
|
+
|
3168
|
+
}(jQuery));
|