select2.js 145 KB

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