selectWoo.js 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874
  1. /*!
  2. * SelectWoo 1.0.1
  3. * https://github.com/woocommerce/selectWoo
  4. *
  5. * Released under the MIT license
  6. * https://github.com/woocommerce/selectWoo/blob/master/LICENSE.md
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD. Register as an anonymous module.
  11. define(['jquery'], factory);
  12. } else if (typeof module === 'object' && module.exports) {
  13. // Node/CommonJS
  14. module.exports = function (root, jQuery) {
  15. if (jQuery === undefined) {
  16. // require('jQuery') returns a factory that requires window to
  17. // build a jQuery instance, we normalize how we use modules
  18. // that require this pattern but the window provided is a noop
  19. // if it's defined (how jquery works)
  20. if (typeof window !== 'undefined') {
  21. jQuery = require('jquery');
  22. }
  23. else {
  24. jQuery = require('jquery')(root);
  25. }
  26. }
  27. factory(jQuery);
  28. return jQuery;
  29. };
  30. } else {
  31. // Browser globals
  32. factory(jQuery);
  33. }
  34. } (function (jQuery) {
  35. // This is needed so we can catch the AMD loader configuration and use it
  36. // The inner file should be wrapped (by `banner.start.js`) in a function that
  37. // returns the AMD loader references.
  38. var S2 =(function () {
  39. // Restore the Select2 AMD loader so it can be used
  40. // Needed mostly in the language files, where the loader is not inserted
  41. if (jQuery && jQuery.fn && jQuery.fn.select2 && jQuery.fn.select2.amd) {
  42. var S2 = jQuery.fn.select2.amd;
  43. }
  44. var S2;(function () { if (!S2 || !S2.requirejs) {
  45. if (!S2) { S2 = {}; } else { require = S2; }
  46. /**
  47. * @license almond 0.3.3 Copyright jQuery Foundation and other contributors.
  48. * Released under MIT license, http://github.com/requirejs/almond/LICENSE
  49. */
  50. //Going sloppy to avoid 'use strict' string cost, but strict practices should
  51. //be followed.
  52. /*global setTimeout: false */
  53. var requirejs, require, define;
  54. (function (undef) {
  55. var main, req, makeMap, handlers,
  56. defined = {},
  57. waiting = {},
  58. config = {},
  59. defining = {},
  60. hasOwn = Object.prototype.hasOwnProperty,
  61. aps = [].slice,
  62. jsSuffixRegExp = /\.js$/;
  63. function hasProp(obj, prop) {
  64. return hasOwn.call(obj, prop);
  65. }
  66. /**
  67. * Given a relative module name, like ./something, normalize it to
  68. * a real name that can be mapped to a path.
  69. * @param {String} name the relative name
  70. * @param {String} baseName a real name that the name arg is relative
  71. * to.
  72. * @returns {String} normalized name
  73. */
  74. function normalize(name, baseName) {
  75. var nameParts, nameSegment, mapValue, foundMap, lastIndex,
  76. foundI, foundStarMap, starI, i, j, part, normalizedBaseParts,
  77. baseParts = baseName && baseName.split("/"),
  78. map = config.map,
  79. starMap = (map && map['*']) || {};
  80. //Adjust any relative paths.
  81. if (name) {
  82. name = name.split('/');
  83. lastIndex = name.length - 1;
  84. // If wanting node ID compatibility, strip .js from end
  85. // of IDs. Have to do this here, and not in nameToUrl
  86. // because node allows either .js or non .js to map
  87. // to same file.
  88. if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
  89. name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
  90. }
  91. // Starts with a '.' so need the baseName
  92. if (name[0].charAt(0) === '.' && baseParts) {
  93. //Convert baseName to array, and lop off the last part,
  94. //so that . matches that 'directory' and not name of the baseName's
  95. //module. For instance, baseName of 'one/two/three', maps to
  96. //'one/two/three.js', but we want the directory, 'one/two' for
  97. //this normalization.
  98. normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
  99. name = normalizedBaseParts.concat(name);
  100. }
  101. //start trimDots
  102. for (i = 0; i < name.length; i++) {
  103. part = name[i];
  104. if (part === '.') {
  105. name.splice(i, 1);
  106. i -= 1;
  107. } else if (part === '..') {
  108. // If at the start, or previous value is still ..,
  109. // keep them so that when converted to a path it may
  110. // still work when converted to a path, even though
  111. // as an ID it is less than ideal. In larger point
  112. // releases, may be better to just kick out an error.
  113. if (i === 0 || (i === 1 && name[2] === '..') || name[i - 1] === '..') {
  114. continue;
  115. } else if (i > 0) {
  116. name.splice(i - 1, 2);
  117. i -= 2;
  118. }
  119. }
  120. }
  121. //end trimDots
  122. name = name.join('/');
  123. }
  124. //Apply map config if available.
  125. if ((baseParts || starMap) && map) {
  126. nameParts = name.split('/');
  127. for (i = nameParts.length; i > 0; i -= 1) {
  128. nameSegment = nameParts.slice(0, i).join("/");
  129. if (baseParts) {
  130. //Find the longest baseName segment match in the config.
  131. //So, do joins on the biggest to smallest lengths of baseParts.
  132. for (j = baseParts.length; j > 0; j -= 1) {
  133. mapValue = map[baseParts.slice(0, j).join('/')];
  134. //baseName segment has config, find if it has one for
  135. //this name.
  136. if (mapValue) {
  137. mapValue = mapValue[nameSegment];
  138. if (mapValue) {
  139. //Match, update name to the new value.
  140. foundMap = mapValue;
  141. foundI = i;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. if (foundMap) {
  148. break;
  149. }
  150. //Check for a star map match, but just hold on to it,
  151. //if there is a shorter segment match later in a matching
  152. //config, then favor over this star map.
  153. if (!foundStarMap && starMap && starMap[nameSegment]) {
  154. foundStarMap = starMap[nameSegment];
  155. starI = i;
  156. }
  157. }
  158. if (!foundMap && foundStarMap) {
  159. foundMap = foundStarMap;
  160. foundI = starI;
  161. }
  162. if (foundMap) {
  163. nameParts.splice(0, foundI, foundMap);
  164. name = nameParts.join('/');
  165. }
  166. }
  167. return name;
  168. }
  169. function makeRequire(relName, forceSync) {
  170. return function () {
  171. //A version of a require function that passes a moduleName
  172. //value for items that may need to
  173. //look up paths relative to the moduleName
  174. var args = aps.call(arguments, 0);
  175. //If first arg is not require('string'), and there is only
  176. //one arg, it is the array form without a callback. Insert
  177. //a null so that the following concat is correct.
  178. if (typeof args[0] !== 'string' && args.length === 1) {
  179. args.push(null);
  180. }
  181. return req.apply(undef, args.concat([relName, forceSync]));
  182. };
  183. }
  184. function makeNormalize(relName) {
  185. return function (name) {
  186. return normalize(name, relName);
  187. };
  188. }
  189. function makeLoad(depName) {
  190. return function (value) {
  191. defined[depName] = value;
  192. };
  193. }
  194. function callDep(name) {
  195. if (hasProp(waiting, name)) {
  196. var args = waiting[name];
  197. delete waiting[name];
  198. defining[name] = true;
  199. main.apply(undef, args);
  200. }
  201. if (!hasProp(defined, name) && !hasProp(defining, name)) {
  202. throw new Error('No ' + name);
  203. }
  204. return defined[name];
  205. }
  206. //Turns a plugin!resource to [plugin, resource]
  207. //with the plugin being undefined if the name
  208. //did not have a plugin prefix.
  209. function splitPrefix(name) {
  210. var prefix,
  211. index = name ? name.indexOf('!') : -1;
  212. if (index > -1) {
  213. prefix = name.substring(0, index);
  214. name = name.substring(index + 1, name.length);
  215. }
  216. return [prefix, name];
  217. }
  218. //Creates a parts array for a relName where first part is plugin ID,
  219. //second part is resource ID. Assumes relName has already been normalized.
  220. function makeRelParts(relName) {
  221. return relName ? splitPrefix(relName) : [];
  222. }
  223. /**
  224. * Makes a name map, normalizing the name, and using a plugin
  225. * for normalization if necessary. Grabs a ref to plugin
  226. * too, as an optimization.
  227. */
  228. makeMap = function (name, relParts) {
  229. var plugin,
  230. parts = splitPrefix(name),
  231. prefix = parts[0],
  232. relResourceName = relParts[1];
  233. name = parts[1];
  234. if (prefix) {
  235. prefix = normalize(prefix, relResourceName);
  236. plugin = callDep(prefix);
  237. }
  238. //Normalize according
  239. if (prefix) {
  240. if (plugin && plugin.normalize) {
  241. name = plugin.normalize(name, makeNormalize(relResourceName));
  242. } else {
  243. name = normalize(name, relResourceName);
  244. }
  245. } else {
  246. name = normalize(name, relResourceName);
  247. parts = splitPrefix(name);
  248. prefix = parts[0];
  249. name = parts[1];
  250. if (prefix) {
  251. plugin = callDep(prefix);
  252. }
  253. }
  254. //Using ridiculous property names for space reasons
  255. return {
  256. f: prefix ? prefix + '!' + name : name, //fullName
  257. n: name,
  258. pr: prefix,
  259. p: plugin
  260. };
  261. };
  262. function makeConfig(name) {
  263. return function () {
  264. return (config && config.config && config.config[name]) || {};
  265. };
  266. }
  267. handlers = {
  268. require: function (name) {
  269. return makeRequire(name);
  270. },
  271. exports: function (name) {
  272. var e = defined[name];
  273. if (typeof e !== 'undefined') {
  274. return e;
  275. } else {
  276. return (defined[name] = {});
  277. }
  278. },
  279. module: function (name) {
  280. return {
  281. id: name,
  282. uri: '',
  283. exports: defined[name],
  284. config: makeConfig(name)
  285. };
  286. }
  287. };
  288. main = function (name, deps, callback, relName) {
  289. var cjsModule, depName, ret, map, i, relParts,
  290. args = [],
  291. callbackType = typeof callback,
  292. usingExports;
  293. //Use name if no relName
  294. relName = relName || name;
  295. relParts = makeRelParts(relName);
  296. //Call the callback to define the module, if necessary.
  297. if (callbackType === 'undefined' || callbackType === 'function') {
  298. //Pull out the defined dependencies and pass the ordered
  299. //values to the callback.
  300. //Default to [require, exports, module] if no deps
  301. deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
  302. for (i = 0; i < deps.length; i += 1) {
  303. map = makeMap(deps[i], relParts);
  304. depName = map.f;
  305. //Fast path CommonJS standard dependencies.
  306. if (depName === "require") {
  307. args[i] = handlers.require(name);
  308. } else if (depName === "exports") {
  309. //CommonJS module spec 1.1
  310. args[i] = handlers.exports(name);
  311. usingExports = true;
  312. } else if (depName === "module") {
  313. //CommonJS module spec 1.1
  314. cjsModule = args[i] = handlers.module(name);
  315. } else if (hasProp(defined, depName) ||
  316. hasProp(waiting, depName) ||
  317. hasProp(defining, depName)) {
  318. args[i] = callDep(depName);
  319. } else if (map.p) {
  320. map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
  321. args[i] = defined[depName];
  322. } else {
  323. throw new Error(name + ' missing ' + depName);
  324. }
  325. }
  326. ret = callback ? callback.apply(defined[name], args) : undefined;
  327. if (name) {
  328. //If setting exports via "module" is in play,
  329. //favor that over return value and exports. After that,
  330. //favor a non-undefined return value over exports use.
  331. if (cjsModule && cjsModule.exports !== undef &&
  332. cjsModule.exports !== defined[name]) {
  333. defined[name] = cjsModule.exports;
  334. } else if (ret !== undef || !usingExports) {
  335. //Use the return value from the function.
  336. defined[name] = ret;
  337. }
  338. }
  339. } else if (name) {
  340. //May just be an object definition for the module. Only
  341. //worry about defining if have a module name.
  342. defined[name] = callback;
  343. }
  344. };
  345. requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
  346. if (typeof deps === "string") {
  347. if (handlers[deps]) {
  348. //callback in this case is really relName
  349. return handlers[deps](callback);
  350. }
  351. //Just return the module wanted. In this scenario, the
  352. //deps arg is the module name, and second arg (if passed)
  353. //is just the relName.
  354. //Normalize module name, if it contains . or ..
  355. return callDep(makeMap(deps, makeRelParts(callback)).f);
  356. } else if (!deps.splice) {
  357. //deps is a config object, not an array.
  358. config = deps;
  359. if (config.deps) {
  360. req(config.deps, config.callback);
  361. }
  362. if (!callback) {
  363. return;
  364. }
  365. if (callback.splice) {
  366. //callback is an array, which means it is a dependency list.
  367. //Adjust args if there are dependencies
  368. deps = callback;
  369. callback = relName;
  370. relName = null;
  371. } else {
  372. deps = undef;
  373. }
  374. }
  375. //Support require(['a'])
  376. callback = callback || function () {};
  377. //If relName is a function, it is an errback handler,
  378. //so remove it.
  379. if (typeof relName === 'function') {
  380. relName = forceSync;
  381. forceSync = alt;
  382. }
  383. //Simulate async callback;
  384. if (forceSync) {
  385. main(undef, deps, callback, relName);
  386. } else {
  387. //Using a non-zero value because of concern for what old browsers
  388. //do, and latest browsers "upgrade" to 4 if lower value is used:
  389. //http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout:
  390. //If want a value immediately, use require('id') instead -- something
  391. //that works in almond on the global level, but not guaranteed and
  392. //unlikely to work in other AMD implementations.
  393. setTimeout(function () {
  394. main(undef, deps, callback, relName);
  395. }, 4);
  396. }
  397. return req;
  398. };
  399. /**
  400. * Just drops the config on the floor, but returns req in case
  401. * the config return value is used.
  402. */
  403. req.config = function (cfg) {
  404. return req(cfg);
  405. };
  406. /**
  407. * Expose module registry for debugging and tooling
  408. */
  409. requirejs._defined = defined;
  410. define = function (name, deps, callback) {
  411. if (typeof name !== 'string') {
  412. throw new Error('See almond README: incorrect module build, no module name');
  413. }
  414. //This module may not have dependencies
  415. if (!deps.splice) {
  416. //deps is not an array, so probably means
  417. //an object literal or factory function for
  418. //the value. Adjust args.
  419. callback = deps;
  420. deps = [];
  421. }
  422. if (!hasProp(defined, name) && !hasProp(waiting, name)) {
  423. waiting[name] = [name, deps, callback];
  424. }
  425. };
  426. define.amd = {
  427. jQuery: true
  428. };
  429. }());
  430. S2.requirejs = requirejs;S2.require = require;S2.define = define;
  431. }
  432. }());
  433. S2.define("almond", function(){});
  434. /* global jQuery:false, $:false */
  435. S2.define('jquery',[],function () {
  436. var _$ = jQuery || $;
  437. if (_$ == null && console && console.error) {
  438. console.error(
  439. 'Select2: An instance of jQuery or a jQuery-compatible library was not ' +
  440. 'found. Make sure that you are including jQuery before Select2 on your ' +
  441. 'web page.'
  442. );
  443. }
  444. return _$;
  445. });
  446. S2.define('select2/utils',[
  447. 'jquery'
  448. ], function ($) {
  449. var Utils = {};
  450. Utils.Extend = function (ChildClass, SuperClass) {
  451. var __hasProp = {}.hasOwnProperty;
  452. function BaseConstructor () {
  453. this.constructor = ChildClass;
  454. }
  455. for (var key in SuperClass) {
  456. if (__hasProp.call(SuperClass, key)) {
  457. ChildClass[key] = SuperClass[key];
  458. }
  459. }
  460. BaseConstructor.prototype = SuperClass.prototype;
  461. ChildClass.prototype = new BaseConstructor();
  462. ChildClass.__super__ = SuperClass.prototype;
  463. return ChildClass;
  464. };
  465. function getMethods (theClass) {
  466. var proto = theClass.prototype;
  467. var methods = [];
  468. for (var methodName in proto) {
  469. var m = proto[methodName];
  470. if (typeof m !== 'function') {
  471. continue;
  472. }
  473. if (methodName === 'constructor') {
  474. continue;
  475. }
  476. methods.push(methodName);
  477. }
  478. return methods;
  479. }
  480. Utils.Decorate = function (SuperClass, DecoratorClass) {
  481. var decoratedMethods = getMethods(DecoratorClass);
  482. var superMethods = getMethods(SuperClass);
  483. function DecoratedClass () {
  484. var unshift = Array.prototype.unshift;
  485. var argCount = DecoratorClass.prototype.constructor.length;
  486. var calledConstructor = SuperClass.prototype.constructor;
  487. if (argCount > 0) {
  488. unshift.call(arguments, SuperClass.prototype.constructor);
  489. calledConstructor = DecoratorClass.prototype.constructor;
  490. }
  491. calledConstructor.apply(this, arguments);
  492. }
  493. DecoratorClass.displayName = SuperClass.displayName;
  494. function ctr () {
  495. this.constructor = DecoratedClass;
  496. }
  497. DecoratedClass.prototype = new ctr();
  498. for (var m = 0; m < superMethods.length; m++) {
  499. var superMethod = superMethods[m];
  500. DecoratedClass.prototype[superMethod] =
  501. SuperClass.prototype[superMethod];
  502. }
  503. var calledMethod = function (methodName) {
  504. // Stub out the original method if it's not decorating an actual method
  505. var originalMethod = function () {};
  506. if (methodName in DecoratedClass.prototype) {
  507. originalMethod = DecoratedClass.prototype[methodName];
  508. }
  509. var decoratedMethod = DecoratorClass.prototype[methodName];
  510. return function () {
  511. var unshift = Array.prototype.unshift;
  512. unshift.call(arguments, originalMethod);
  513. return decoratedMethod.apply(this, arguments);
  514. };
  515. };
  516. for (var d = 0; d < decoratedMethods.length; d++) {
  517. var decoratedMethod = decoratedMethods[d];
  518. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  519. }
  520. return DecoratedClass;
  521. };
  522. var Observable = function () {
  523. this.listeners = {};
  524. };
  525. Observable.prototype.on = function (event, callback) {
  526. this.listeners = this.listeners || {};
  527. if (event in this.listeners) {
  528. this.listeners[event].push(callback);
  529. } else {
  530. this.listeners[event] = [callback];
  531. }
  532. };
  533. Observable.prototype.trigger = function (event) {
  534. var slice = Array.prototype.slice;
  535. var params = slice.call(arguments, 1);
  536. this.listeners = this.listeners || {};
  537. // Params should always come in as an array
  538. if (params == null) {
  539. params = [];
  540. }
  541. // If there are no arguments to the event, use a temporary object
  542. if (params.length === 0) {
  543. params.push({});
  544. }
  545. // Set the `_type` of the first object to the event
  546. params[0]._type = event;
  547. if (event in this.listeners) {
  548. this.invoke(this.listeners[event], slice.call(arguments, 1));
  549. }
  550. if ('*' in this.listeners) {
  551. this.invoke(this.listeners['*'], arguments);
  552. }
  553. };
  554. Observable.prototype.invoke = function (listeners, params) {
  555. for (var i = 0, len = listeners.length; i < len; i++) {
  556. listeners[i].apply(this, params);
  557. }
  558. };
  559. Utils.Observable = Observable;
  560. Utils.generateChars = function (length) {
  561. var chars = '';
  562. for (var i = 0; i < length; i++) {
  563. var randomChar = Math.floor(Math.random() * 36);
  564. chars += randomChar.toString(36);
  565. }
  566. return chars;
  567. };
  568. Utils.bind = function (func, context) {
  569. return function () {
  570. func.apply(context, arguments);
  571. };
  572. };
  573. Utils._convertData = function (data) {
  574. for (var originalKey in data) {
  575. var keys = originalKey.split('-');
  576. var dataLevel = data;
  577. if (keys.length === 1) {
  578. continue;
  579. }
  580. for (var k = 0; k < keys.length; k++) {
  581. var key = keys[k];
  582. // Lowercase the first letter
  583. // By default, dash-separated becomes camelCase
  584. key = key.substring(0, 1).toLowerCase() + key.substring(1);
  585. if (!(key in dataLevel)) {
  586. dataLevel[key] = {};
  587. }
  588. if (k == keys.length - 1) {
  589. dataLevel[key] = data[originalKey];
  590. }
  591. dataLevel = dataLevel[key];
  592. }
  593. delete data[originalKey];
  594. }
  595. return data;
  596. };
  597. Utils.hasScroll = function (index, el) {
  598. // Adapted from the function created by @ShadowScripter
  599. // and adapted by @BillBarry on the Stack Exchange Code Review website.
  600. // The original code can be found at
  601. // http://codereview.stackexchange.com/q/13338
  602. // and was designed to be used with the Sizzle selector engine.
  603. var $el = $(el);
  604. var overflowX = el.style.overflowX;
  605. var overflowY = el.style.overflowY;
  606. //Check both x and y declarations
  607. if (overflowX === overflowY &&
  608. (overflowY === 'hidden' || overflowY === 'visible')) {
  609. return false;
  610. }
  611. if (overflowX === 'scroll' || overflowY === 'scroll') {
  612. return true;
  613. }
  614. return ($el.innerHeight() < el.scrollHeight ||
  615. $el.innerWidth() < el.scrollWidth);
  616. };
  617. Utils.escapeMarkup = function (markup) {
  618. var replaceMap = {
  619. '\\': '&#92;',
  620. '&': '&amp;',
  621. '<': '&lt;',
  622. '>': '&gt;',
  623. '"': '&quot;',
  624. '\'': '&#39;',
  625. '/': '&#47;'
  626. };
  627. // Do not try to escape the markup if it's not a string
  628. if (typeof markup !== 'string') {
  629. return markup;
  630. }
  631. return String(markup).replace(/[&<>"'\/\\]/g, function (match) {
  632. return replaceMap[match];
  633. });
  634. };
  635. // Append an array of jQuery nodes to a given element.
  636. Utils.appendMany = function ($element, $nodes) {
  637. // jQuery 1.7.x does not support $.fn.append() with an array
  638. // Fall back to a jQuery object collection using $.fn.add()
  639. if ($.fn.jquery.substr(0, 3) === '1.7') {
  640. var $jqNodes = $();
  641. $.map($nodes, function (node) {
  642. $jqNodes = $jqNodes.add(node);
  643. });
  644. $nodes = $jqNodes;
  645. }
  646. $element.append($nodes);
  647. };
  648. // Determine whether the browser is on a touchscreen device.
  649. Utils.isTouchscreen = function() {
  650. if ('undefined' === typeof Utils._isTouchscreenCache) {
  651. Utils._isTouchscreenCache = 'ontouchstart' in document.documentElement;
  652. }
  653. return Utils._isTouchscreenCache;
  654. }
  655. return Utils;
  656. });
  657. S2.define('select2/results',[
  658. 'jquery',
  659. './utils'
  660. ], function ($, Utils) {
  661. function Results ($element, options, dataAdapter) {
  662. this.$element = $element;
  663. this.data = dataAdapter;
  664. this.options = options;
  665. Results.__super__.constructor.call(this);
  666. }
  667. Utils.Extend(Results, Utils.Observable);
  668. Results.prototype.render = function () {
  669. var $results = $(
  670. '<ul class="select2-results__options" role="listbox" tabindex="-1"></ul>'
  671. );
  672. if (this.options.get('multiple')) {
  673. $results.attr('aria-multiselectable', 'true');
  674. }
  675. this.$results = $results;
  676. return $results;
  677. };
  678. Results.prototype.clear = function () {
  679. this.$results.empty();
  680. };
  681. Results.prototype.displayMessage = function (params) {
  682. var escapeMarkup = this.options.get('escapeMarkup');
  683. this.clear();
  684. this.hideLoading();
  685. var $message = $(
  686. '<li role="alert" aria-live="assertive"' +
  687. ' class="select2-results__option"></li>'
  688. );
  689. var message = this.options.get('translations').get(params.message);
  690. $message.append(
  691. escapeMarkup(
  692. message(params.args)
  693. )
  694. );
  695. $message[0].className += ' select2-results__message';
  696. this.$results.append($message);
  697. };
  698. Results.prototype.hideMessages = function () {
  699. this.$results.find('.select2-results__message').remove();
  700. };
  701. Results.prototype.append = function (data) {
  702. this.hideLoading();
  703. var $options = [];
  704. if (data.results == null || data.results.length === 0) {
  705. if (this.$results.children().length === 0) {
  706. this.trigger('results:message', {
  707. message: 'noResults'
  708. });
  709. }
  710. return;
  711. }
  712. data.results = this.sort(data.results);
  713. for (var d = 0; d < data.results.length; d++) {
  714. var item = data.results[d];
  715. var $option = this.option(item);
  716. $options.push($option);
  717. }
  718. this.$results.append($options);
  719. };
  720. Results.prototype.position = function ($results, $dropdown) {
  721. var $resultsContainer = $dropdown.find('.select2-results');
  722. $resultsContainer.append($results);
  723. };
  724. Results.prototype.sort = function (data) {
  725. var sorter = this.options.get('sorter');
  726. return sorter(data);
  727. };
  728. Results.prototype.highlightFirstItem = function () {
  729. var $options = this.$results
  730. .find('.select2-results__option[data-selected]');
  731. var $selected = $options.filter('[data-selected=true]');
  732. // Check if there are any selected options
  733. if ($selected.length > 0) {
  734. // If there are selected options, highlight the first
  735. $selected.first().trigger('mouseenter');
  736. } else {
  737. // If there are no selected options, highlight the first option
  738. // in the dropdown
  739. $options.first().trigger('mouseenter');
  740. }
  741. this.ensureHighlightVisible();
  742. };
  743. Results.prototype.setClasses = function () {
  744. var self = this;
  745. this.data.current(function (selected) {
  746. var selectedIds = $.map(selected, function (s) {
  747. return s.id.toString();
  748. });
  749. var $options = self.$results
  750. .find('.select2-results__option[data-selected]');
  751. $options.each(function () {
  752. var $option = $(this);
  753. var item = $.data(this, 'data');
  754. // id needs to be converted to a string when comparing
  755. var id = '' + item.id;
  756. if ((item.element != null && item.element.selected) ||
  757. (item.element == null && $.inArray(id, selectedIds) > -1)) {
  758. $option.attr('data-selected', 'true');
  759. } else {
  760. $option.attr('data-selected', 'false');
  761. }
  762. });
  763. });
  764. };
  765. Results.prototype.showLoading = function (params) {
  766. this.hideLoading();
  767. var loadingMore = this.options.get('translations').get('searching');
  768. var loading = {
  769. disabled: true,
  770. loading: true,
  771. text: loadingMore(params)
  772. };
  773. var $loading = this.option(loading);
  774. $loading.className += ' loading-results';
  775. this.$results.prepend($loading);
  776. };
  777. Results.prototype.hideLoading = function () {
  778. this.$results.find('.loading-results').remove();
  779. };
  780. Results.prototype.option = function (data) {
  781. var option = document.createElement('li');
  782. option.className = 'select2-results__option';
  783. var attrs = {
  784. 'role': 'option',
  785. 'data-selected': 'false',
  786. 'tabindex': -1
  787. };
  788. if (data.disabled) {
  789. delete attrs['data-selected'];
  790. attrs['aria-disabled'] = 'true';
  791. }
  792. if (data.id == null) {
  793. delete attrs['data-selected'];
  794. }
  795. if (data._resultId != null) {
  796. option.id = data._resultId;
  797. }
  798. if (data.title) {
  799. option.title = data.title;
  800. }
  801. if (data.children) {
  802. attrs['aria-label'] = data.text;
  803. delete attrs['data-selected'];
  804. }
  805. for (var attr in attrs) {
  806. var val = attrs[attr];
  807. option.setAttribute(attr, val);
  808. }
  809. if (data.children) {
  810. var $option = $(option);
  811. var label = document.createElement('strong');
  812. label.className = 'select2-results__group';
  813. var $label = $(label);
  814. this.template(data, label);
  815. $label.attr('role', 'presentation');
  816. var $children = [];
  817. for (var c = 0; c < data.children.length; c++) {
  818. var child = data.children[c];
  819. var $child = this.option(child);
  820. $children.push($child);
  821. }
  822. var $childrenContainer = $('<ul></ul>', {
  823. 'class': 'select2-results__options select2-results__options--nested',
  824. 'role': 'listbox'
  825. });
  826. $childrenContainer.append($children);
  827. $option.attr('role', 'list');
  828. $option.append(label);
  829. $option.append($childrenContainer);
  830. } else {
  831. this.template(data, option);
  832. }
  833. $.data(option, 'data', data);
  834. return option;
  835. };
  836. Results.prototype.bind = function (container, $container) {
  837. var self = this;
  838. var id = container.id + '-results';
  839. this.$results.attr('id', id);
  840. container.on('results:all', function (params) {
  841. self.clear();
  842. self.append(params.data);
  843. if (container.isOpen()) {
  844. self.setClasses();
  845. self.highlightFirstItem();
  846. }
  847. });
  848. container.on('results:append', function (params) {
  849. self.append(params.data);
  850. if (container.isOpen()) {
  851. self.setClasses();
  852. }
  853. });
  854. container.on('query', function (params) {
  855. self.hideMessages();
  856. self.showLoading(params);
  857. });
  858. container.on('select', function () {
  859. if (!container.isOpen()) {
  860. return;
  861. }
  862. self.setClasses();
  863. self.highlightFirstItem();
  864. });
  865. container.on('unselect', function () {
  866. if (!container.isOpen()) {
  867. return;
  868. }
  869. self.setClasses();
  870. self.highlightFirstItem();
  871. });
  872. container.on('open', function () {
  873. // When the dropdown is open, aria-expended="true"
  874. self.$results.attr('aria-expanded', 'true');
  875. self.$results.attr('aria-hidden', 'false');
  876. self.setClasses();
  877. self.ensureHighlightVisible();
  878. });
  879. container.on('close', function () {
  880. // When the dropdown is closed, aria-expended="false"
  881. self.$results.attr('aria-expanded', 'false');
  882. self.$results.attr('aria-hidden', 'true');
  883. self.$results.removeAttr('aria-activedescendant');
  884. });
  885. container.on('results:toggle', function () {
  886. var $highlighted = self.getHighlightedResults();
  887. if ($highlighted.length === 0) {
  888. return;
  889. }
  890. $highlighted.trigger('mouseup');
  891. });
  892. container.on('results:select', function () {
  893. var $highlighted = self.getHighlightedResults();
  894. if ($highlighted.length === 0) {
  895. return;
  896. }
  897. var data = $highlighted.data('data');
  898. if ($highlighted.attr('data-selected') == 'true') {
  899. self.trigger('close', {});
  900. } else {
  901. self.trigger('select', {
  902. data: data
  903. });
  904. }
  905. });
  906. container.on('results:previous', function () {
  907. var $highlighted = self.getHighlightedResults();
  908. var $options = self.$results.find('[data-selected]');
  909. var currentIndex = $options.index($highlighted);
  910. // If we are already at te top, don't move further
  911. if (currentIndex === 0) {
  912. return;
  913. }
  914. var nextIndex = currentIndex - 1;
  915. // If none are highlighted, highlight the first
  916. if ($highlighted.length === 0) {
  917. nextIndex = 0;
  918. }
  919. var $next = $options.eq(nextIndex);
  920. $next.trigger('mouseenter');
  921. var currentOffset = self.$results.offset().top;
  922. var nextTop = $next.offset().top;
  923. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  924. if (nextIndex === 0) {
  925. self.$results.scrollTop(0);
  926. } else if (nextTop - currentOffset < 0) {
  927. self.$results.scrollTop(nextOffset);
  928. }
  929. });
  930. container.on('results:next', function () {
  931. var $highlighted = self.getHighlightedResults();
  932. var $options = self.$results.find('[data-selected]');
  933. var currentIndex = $options.index($highlighted);
  934. var nextIndex = currentIndex + 1;
  935. // If we are at the last option, stay there
  936. if (nextIndex >= $options.length) {
  937. return;
  938. }
  939. var $next = $options.eq(nextIndex);
  940. $next.trigger('mouseenter');
  941. var currentOffset = self.$results.offset().top +
  942. self.$results.outerHeight(false);
  943. var nextBottom = $next.offset().top + $next.outerHeight(false);
  944. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  945. if (nextIndex === 0) {
  946. self.$results.scrollTop(0);
  947. } else if (nextBottom > currentOffset) {
  948. self.$results.scrollTop(nextOffset);
  949. }
  950. });
  951. container.on('results:focus', function (params) {
  952. params.element.addClass('select2-results__option--highlighted').attr('aria-selected', 'true');
  953. self.$results.attr('aria-activedescendant', params.element.attr('id'));
  954. });
  955. container.on('results:message', function (params) {
  956. self.displayMessage(params);
  957. });
  958. if ($.fn.mousewheel) {
  959. this.$results.on('mousewheel', function (e) {
  960. var top = self.$results.scrollTop();
  961. var bottom = self.$results.get(0).scrollHeight - top + e.deltaY;
  962. var isAtTop = e.deltaY > 0 && top - e.deltaY <= 0;
  963. var isAtBottom = e.deltaY < 0 && bottom <= self.$results.height();
  964. if (isAtTop) {
  965. self.$results.scrollTop(0);
  966. e.preventDefault();
  967. e.stopPropagation();
  968. } else if (isAtBottom) {
  969. self.$results.scrollTop(
  970. self.$results.get(0).scrollHeight - self.$results.height()
  971. );
  972. e.preventDefault();
  973. e.stopPropagation();
  974. }
  975. });
  976. }
  977. this.$results.on('mouseup', '.select2-results__option[data-selected]',
  978. function (evt) {
  979. var $this = $(this);
  980. var data = $this.data('data');
  981. if ($this.attr('data-selected') === 'true') {
  982. if (self.options.get('multiple')) {
  983. self.trigger('unselect', {
  984. originalEvent: evt,
  985. data: data
  986. });
  987. } else {
  988. self.trigger('close', {});
  989. }
  990. return;
  991. }
  992. self.trigger('select', {
  993. originalEvent: evt,
  994. data: data
  995. });
  996. });
  997. this.$results.on('mouseenter', '.select2-results__option[data-selected]',
  998. function (evt) {
  999. var data = $(this).data('data');
  1000. self.getHighlightedResults()
  1001. .removeClass('select2-results__option--highlighted')
  1002. .attr('aria-selected', 'false');
  1003. self.trigger('results:focus', {
  1004. data: data,
  1005. element: $(this)
  1006. });
  1007. });
  1008. };
  1009. Results.prototype.getHighlightedResults = function () {
  1010. var $highlighted = this.$results
  1011. .find('.select2-results__option--highlighted');
  1012. return $highlighted;
  1013. };
  1014. Results.prototype.destroy = function () {
  1015. this.$results.remove();
  1016. };
  1017. Results.prototype.ensureHighlightVisible = function () {
  1018. var $highlighted = this.getHighlightedResults();
  1019. if ($highlighted.length === 0) {
  1020. return;
  1021. }
  1022. var $options = this.$results.find('[data-selected]');
  1023. var currentIndex = $options.index($highlighted);
  1024. var currentOffset = this.$results.offset().top;
  1025. var nextTop = $highlighted.offset().top;
  1026. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  1027. var offsetDelta = nextTop - currentOffset;
  1028. nextOffset -= $highlighted.outerHeight(false) * 2;
  1029. if (currentIndex <= 2) {
  1030. this.$results.scrollTop(0);
  1031. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  1032. this.$results.scrollTop(nextOffset);
  1033. }
  1034. };
  1035. Results.prototype.template = function (result, container) {
  1036. var template = this.options.get('templateResult');
  1037. var escapeMarkup = this.options.get('escapeMarkup');
  1038. var content = template(result, container);
  1039. if (content == null) {
  1040. container.style.display = 'none';
  1041. } else if (typeof content === 'string') {
  1042. container.innerHTML = escapeMarkup(content);
  1043. } else {
  1044. $(container).append(content);
  1045. }
  1046. };
  1047. return Results;
  1048. });
  1049. S2.define('select2/keys',[
  1050. ], function () {
  1051. var KEYS = {
  1052. BACKSPACE: 8,
  1053. TAB: 9,
  1054. ENTER: 13,
  1055. SHIFT: 16,
  1056. CTRL: 17,
  1057. ALT: 18,
  1058. ESC: 27,
  1059. SPACE: 32,
  1060. PAGE_UP: 33,
  1061. PAGE_DOWN: 34,
  1062. END: 35,
  1063. HOME: 36,
  1064. LEFT: 37,
  1065. UP: 38,
  1066. RIGHT: 39,
  1067. DOWN: 40,
  1068. DELETE: 46
  1069. };
  1070. return KEYS;
  1071. });
  1072. S2.define('select2/selection/base',[
  1073. 'jquery',
  1074. '../utils',
  1075. '../keys'
  1076. ], function ($, Utils, KEYS) {
  1077. function BaseSelection ($element, options) {
  1078. this.$element = $element;
  1079. this.options = options;
  1080. BaseSelection.__super__.constructor.call(this);
  1081. }
  1082. Utils.Extend(BaseSelection, Utils.Observable);
  1083. BaseSelection.prototype.render = function () {
  1084. var $selection = $(
  1085. '<span class="select2-selection" ' +
  1086. ' aria-haspopup="true" aria-expanded="false">' +
  1087. '</span>'
  1088. );
  1089. this._tabindex = 0;
  1090. if (this.$element.data('old-tabindex') != null) {
  1091. this._tabindex = this.$element.data('old-tabindex');
  1092. } else if (this.$element.attr('tabindex') != null) {
  1093. this._tabindex = this.$element.attr('tabindex');
  1094. }
  1095. $selection.attr('title', this.$element.attr('title'));
  1096. $selection.attr('tabindex', this._tabindex);
  1097. this.$selection = $selection;
  1098. return $selection;
  1099. };
  1100. BaseSelection.prototype.bind = function (container, $container) {
  1101. var self = this;
  1102. var id = container.id + '-container';
  1103. var resultsId = container.id + '-results';
  1104. var searchHidden = this.options.get('minimumResultsForSearch') === Infinity;
  1105. this.container = container;
  1106. this.$selection.on('focus', function (evt) {
  1107. self.trigger('focus', evt);
  1108. });
  1109. this.$selection.on('blur', function (evt) {
  1110. self._handleBlur(evt);
  1111. });
  1112. this.$selection.on('keydown', function (evt) {
  1113. self.trigger('keypress', evt);
  1114. if (evt.which === KEYS.SPACE) {
  1115. evt.preventDefault();
  1116. }
  1117. });
  1118. container.on('results:focus', function (params) {
  1119. self.$selection.attr('aria-activedescendant', params.data._resultId);
  1120. });
  1121. container.on('selection:update', function (params) {
  1122. self.update(params.data);
  1123. });
  1124. container.on('open', function () {
  1125. // When the dropdown is open, aria-expanded="true"
  1126. self.$selection.attr('aria-expanded', 'true');
  1127. self.$selection.attr('aria-owns', resultsId);
  1128. self._attachCloseHandler(container);
  1129. });
  1130. container.on('close', function () {
  1131. // When the dropdown is closed, aria-expanded="false"
  1132. self.$selection.attr('aria-expanded', 'false');
  1133. self.$selection.removeAttr('aria-activedescendant');
  1134. self.$selection.removeAttr('aria-owns');
  1135. // This needs to be delayed as the active element is the body when the
  1136. // key is pressed.
  1137. window.setTimeout(function () {
  1138. self.$selection.focus();
  1139. }, 1);
  1140. self._detachCloseHandler(container);
  1141. });
  1142. container.on('enable', function () {
  1143. self.$selection.attr('tabindex', self._tabindex);
  1144. });
  1145. container.on('disable', function () {
  1146. self.$selection.attr('tabindex', '-1');
  1147. });
  1148. };
  1149. BaseSelection.prototype._handleBlur = function (evt) {
  1150. var self = this;
  1151. // This needs to be delayed as the active element is the body when the tab
  1152. // key is pressed, possibly along with others.
  1153. window.setTimeout(function () {
  1154. // Don't trigger `blur` if the focus is still in the selection
  1155. if (
  1156. (document.activeElement == self.$selection[0]) ||
  1157. ($.contains(self.$selection[0], document.activeElement))
  1158. ) {
  1159. return;
  1160. }
  1161. self.trigger('blur', evt);
  1162. }, 1);
  1163. };
  1164. BaseSelection.prototype._attachCloseHandler = function (container) {
  1165. var self = this;
  1166. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  1167. var $target = $(e.target);
  1168. var $select = $target.closest('.select2');
  1169. var $all = $('.select2.select2-container--open');
  1170. $all.each(function () {
  1171. var $this = $(this);
  1172. if (this == $select[0]) {
  1173. return;
  1174. }
  1175. var $element = $this.data('element');
  1176. $element.select2('close');
  1177. // Remove any focus when dropdown is closed by clicking outside the select area.
  1178. // Timeout of 1 required for close to finish wrapping up.
  1179. setTimeout(function(){
  1180. $this.find('*:focus').blur();
  1181. $target.focus();
  1182. }, 1);
  1183. });
  1184. });
  1185. };
  1186. BaseSelection.prototype._detachCloseHandler = function (container) {
  1187. $(document.body).off('mousedown.select2.' + container.id);
  1188. };
  1189. BaseSelection.prototype.position = function ($selection, $container) {
  1190. var $selectionContainer = $container.find('.selection');
  1191. $selectionContainer.append($selection);
  1192. };
  1193. BaseSelection.prototype.destroy = function () {
  1194. this._detachCloseHandler(this.container);
  1195. };
  1196. BaseSelection.prototype.update = function (data) {
  1197. throw new Error('The `update` method must be defined in child classes.');
  1198. };
  1199. return BaseSelection;
  1200. });
  1201. S2.define('select2/selection/single',[
  1202. 'jquery',
  1203. './base',
  1204. '../utils',
  1205. '../keys'
  1206. ], function ($, BaseSelection, Utils, KEYS) {
  1207. function SingleSelection () {
  1208. SingleSelection.__super__.constructor.apply(this, arguments);
  1209. }
  1210. Utils.Extend(SingleSelection, BaseSelection);
  1211. SingleSelection.prototype.render = function () {
  1212. var $selection = SingleSelection.__super__.render.call(this);
  1213. $selection.addClass('select2-selection--single');
  1214. $selection.html(
  1215. '<span class="select2-selection__rendered"></span>' +
  1216. '<span class="select2-selection__arrow" role="presentation">' +
  1217. '<b role="presentation"></b>' +
  1218. '</span>'
  1219. );
  1220. return $selection;
  1221. };
  1222. SingleSelection.prototype.bind = function (container, $container) {
  1223. var self = this;
  1224. SingleSelection.__super__.bind.apply(this, arguments);
  1225. var id = container.id + '-container';
  1226. this.$selection.find('.select2-selection__rendered')
  1227. .attr('id', id)
  1228. .attr('role', 'textbox')
  1229. .attr('aria-readonly', 'true');
  1230. this.$selection.attr('aria-labelledby', id);
  1231. // This makes single non-search selects work in screen readers. If it causes problems elsewhere, remove.
  1232. this.$selection.attr('role', 'combobox');
  1233. this.$selection.on('mousedown', function (evt) {
  1234. // Only respond to left clicks
  1235. if (evt.which !== 1) {
  1236. return;
  1237. }
  1238. self.trigger('toggle', {
  1239. originalEvent: evt
  1240. });
  1241. });
  1242. this.$selection.on('focus', function (evt) {
  1243. // User focuses on the container
  1244. });
  1245. this.$selection.on('keydown', function (evt) {
  1246. // If user starts typing an alphanumeric key on the keyboard, open if not opened.
  1247. if (!container.isOpen() && evt.which >= 48 && evt.which <= 90) {
  1248. container.open();
  1249. }
  1250. });
  1251. this.$selection.on('blur', function (evt) {
  1252. // User exits the container
  1253. });
  1254. container.on('focus', function (evt) {
  1255. if (!container.isOpen()) {
  1256. self.$selection.focus();
  1257. }
  1258. });
  1259. container.on('selection:update', function (params) {
  1260. self.update(params.data);
  1261. });
  1262. };
  1263. SingleSelection.prototype.clear = function () {
  1264. this.$selection.find('.select2-selection__rendered').empty();
  1265. };
  1266. SingleSelection.prototype.display = function (data, container) {
  1267. var template = this.options.get('templateSelection');
  1268. var escapeMarkup = this.options.get('escapeMarkup');
  1269. return escapeMarkup(template(data, container));
  1270. };
  1271. SingleSelection.prototype.selectionContainer = function () {
  1272. return $('<span></span>');
  1273. };
  1274. SingleSelection.prototype.update = function (data) {
  1275. if (data.length === 0) {
  1276. this.clear();
  1277. return;
  1278. }
  1279. var selection = data[0];
  1280. var $rendered = this.$selection.find('.select2-selection__rendered');
  1281. var formatted = this.display(selection, $rendered);
  1282. $rendered.empty().append(formatted);
  1283. $rendered.prop('title', selection.title || selection.text);
  1284. };
  1285. return SingleSelection;
  1286. });
  1287. S2.define('select2/selection/multiple',[
  1288. 'jquery',
  1289. './base',
  1290. '../utils'
  1291. ], function ($, BaseSelection, Utils) {
  1292. function MultipleSelection ($element, options) {
  1293. MultipleSelection.__super__.constructor.apply(this, arguments);
  1294. }
  1295. Utils.Extend(MultipleSelection, BaseSelection);
  1296. MultipleSelection.prototype.render = function () {
  1297. var $selection = MultipleSelection.__super__.render.call(this);
  1298. $selection.addClass('select2-selection--multiple');
  1299. $selection.html(
  1300. '<ul class="select2-selection__rendered" aria-live="polite" aria-relevant="additions removals" aria-atomic="true"></ul>'
  1301. );
  1302. return $selection;
  1303. };
  1304. MultipleSelection.prototype.bind = function (container, $container) {
  1305. var self = this;
  1306. MultipleSelection.__super__.bind.apply(this, arguments);
  1307. this.$selection.on('click', function (evt) {
  1308. self.trigger('toggle', {
  1309. originalEvent: evt
  1310. });
  1311. });
  1312. this.$selection.on(
  1313. 'click',
  1314. '.select2-selection__choice__remove',
  1315. function (evt) {
  1316. // Ignore the event if it is disabled
  1317. if (self.options.get('disabled')) {
  1318. return;
  1319. }
  1320. var $remove = $(this);
  1321. var $selection = $remove.parent();
  1322. var data = $selection.data('data');
  1323. self.trigger('unselect', {
  1324. originalEvent: evt,
  1325. data: data
  1326. });
  1327. }
  1328. );
  1329. this.$selection.on('keydown', function (evt) {
  1330. // If user starts typing an alphanumeric key on the keyboard, open if not opened.
  1331. if (!container.isOpen() && evt.which >= 48 && evt.which <= 90) {
  1332. container.open();
  1333. }
  1334. });
  1335. // Focus on the search field when the container is focused instead of the main container.
  1336. container.on( 'focus', function(){
  1337. self.focusOnSearch();
  1338. });
  1339. };
  1340. MultipleSelection.prototype.clear = function () {
  1341. this.$selection.find('.select2-selection__rendered').empty();
  1342. };
  1343. MultipleSelection.prototype.display = function (data, container) {
  1344. var template = this.options.get('templateSelection');
  1345. var escapeMarkup = this.options.get('escapeMarkup');
  1346. return escapeMarkup(template(data, container));
  1347. };
  1348. MultipleSelection.prototype.selectionContainer = function () {
  1349. var $container = $(
  1350. '<li class="select2-selection__choice">' +
  1351. '<span class="select2-selection__choice__remove" role="presentation" aria-hidden="true">' +
  1352. '&times;' +
  1353. '</span>' +
  1354. '</li>'
  1355. );
  1356. return $container;
  1357. };
  1358. /**
  1359. * Focus on the search field instead of the main multiselect container.
  1360. */
  1361. MultipleSelection.prototype.focusOnSearch = function() {
  1362. var self = this;
  1363. if ('undefined' !== typeof self.$search) {
  1364. // Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
  1365. setTimeout(function(){
  1366. // Prevent the dropdown opening again when focused from this.
  1367. // This gets reset automatically when focus is triggered.
  1368. self._keyUpPrevented = true;
  1369. self.$search.focus();
  1370. }, 1);
  1371. }
  1372. }
  1373. MultipleSelection.prototype.update = function (data) {
  1374. this.clear();
  1375. if (data.length === 0) {
  1376. return;
  1377. }
  1378. var $selections = [];
  1379. for (var d = 0; d < data.length; d++) {
  1380. var selection = data[d];
  1381. var $selection = this.selectionContainer();
  1382. var formatted = this.display(selection, $selection);
  1383. if ('string' === typeof formatted) {
  1384. formatted = formatted.trim();
  1385. }
  1386. $selection.append(formatted);
  1387. $selection.prop('title', selection.title || selection.text);
  1388. $selection.data('data', selection);
  1389. $selections.push($selection);
  1390. }
  1391. var $rendered = this.$selection.find('.select2-selection__rendered');
  1392. Utils.appendMany($rendered, $selections);
  1393. };
  1394. return MultipleSelection;
  1395. });
  1396. S2.define('select2/selection/placeholder',[
  1397. '../utils'
  1398. ], function (Utils) {
  1399. function Placeholder (decorated, $element, options) {
  1400. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1401. decorated.call(this, $element, options);
  1402. }
  1403. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1404. if (typeof placeholder === 'string') {
  1405. placeholder = {
  1406. id: '',
  1407. text: placeholder
  1408. };
  1409. }
  1410. return placeholder;
  1411. };
  1412. Placeholder.prototype.createPlaceholder = function (decorated, placeholder) {
  1413. var $placeholder = this.selectionContainer();
  1414. $placeholder.html(this.display(placeholder));
  1415. $placeholder.addClass('select2-selection__placeholder')
  1416. .removeClass('select2-selection__choice');
  1417. return $placeholder;
  1418. };
  1419. Placeholder.prototype.update = function (decorated, data) {
  1420. var singlePlaceholder = (
  1421. data.length == 1 && data[0].id != this.placeholder.id
  1422. );
  1423. var multipleSelections = data.length > 1;
  1424. if (multipleSelections || singlePlaceholder) {
  1425. return decorated.call(this, data);
  1426. }
  1427. this.clear();
  1428. var $placeholder = this.createPlaceholder(this.placeholder);
  1429. this.$selection.find('.select2-selection__rendered').append($placeholder);
  1430. };
  1431. return Placeholder;
  1432. });
  1433. S2.define('select2/selection/allowClear',[
  1434. 'jquery',
  1435. '../keys'
  1436. ], function ($, KEYS) {
  1437. function AllowClear () { }
  1438. AllowClear.prototype.bind = function (decorated, container, $container) {
  1439. var self = this;
  1440. decorated.call(this, container, $container);
  1441. if (this.placeholder == null) {
  1442. if (this.options.get('debug') && window.console && console.error) {
  1443. console.error(
  1444. 'Select2: The `allowClear` option should be used in combination ' +
  1445. 'with the `placeholder` option.'
  1446. );
  1447. }
  1448. }
  1449. this.$selection.on('mousedown', '.select2-selection__clear',
  1450. function (evt) {
  1451. self._handleClear(evt);
  1452. });
  1453. container.on('keypress', function (evt) {
  1454. self._handleKeyboardClear(evt, container);
  1455. });
  1456. };
  1457. AllowClear.prototype._handleClear = function (_, evt) {
  1458. // Ignore the event if it is disabled
  1459. if (this.options.get('disabled')) {
  1460. return;
  1461. }
  1462. var $clear = this.$selection.find('.select2-selection__clear');
  1463. // Ignore the event if nothing has been selected
  1464. if ($clear.length === 0) {
  1465. return;
  1466. }
  1467. evt.stopPropagation();
  1468. var data = $clear.data('data');
  1469. for (var d = 0; d < data.length; d++) {
  1470. var unselectData = {
  1471. data: data[d]
  1472. };
  1473. // Trigger the `unselect` event, so people can prevent it from being
  1474. // cleared.
  1475. this.trigger('unselect', unselectData);
  1476. // If the event was prevented, don't clear it out.
  1477. if (unselectData.prevented) {
  1478. return;
  1479. }
  1480. }
  1481. this.$element.val(this.placeholder.id).trigger('change');
  1482. this.trigger('toggle', {});
  1483. };
  1484. AllowClear.prototype._handleKeyboardClear = function (_, evt, container) {
  1485. if (container.isOpen()) {
  1486. return;
  1487. }
  1488. if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
  1489. this._handleClear(evt);
  1490. }
  1491. };
  1492. AllowClear.prototype.update = function (decorated, data) {
  1493. decorated.call(this, data);
  1494. if (this.$selection.find('.select2-selection__placeholder').length > 0 ||
  1495. data.length === 0) {
  1496. return;
  1497. }
  1498. var $remove = $(
  1499. '<span class="select2-selection__clear">' +
  1500. '&times;' +
  1501. '</span>'
  1502. );
  1503. $remove.data('data', data);
  1504. this.$selection.find('.select2-selection__rendered').prepend($remove);
  1505. };
  1506. return AllowClear;
  1507. });
  1508. S2.define('select2/selection/search',[
  1509. 'jquery',
  1510. '../utils',
  1511. '../keys'
  1512. ], function ($, Utils, KEYS) {
  1513. function Search (decorated, $element, options) {
  1514. decorated.call(this, $element, options);
  1515. }
  1516. Search.prototype.render = function (decorated) {
  1517. var $search = $(
  1518. '<li class="select2-search select2-search--inline">' +
  1519. '<input class="select2-search__field" type="text" tabindex="-1"' +
  1520. ' autocomplete="off" autocorrect="off" autocapitalize="off"' +
  1521. ' spellcheck="false" role="textbox" aria-autocomplete="list" />' +
  1522. '</li>'
  1523. );
  1524. this.$searchContainer = $search;
  1525. this.$search = $search.find('input');
  1526. var $rendered = decorated.call(this);
  1527. this._transferTabIndex();
  1528. return $rendered;
  1529. };
  1530. Search.prototype.bind = function (decorated, container, $container) {
  1531. var self = this;
  1532. var resultsId = container.id + '-results';
  1533. decorated.call(this, container, $container);
  1534. container.on('open', function () {
  1535. self.$search.attr('aria-owns', resultsId);
  1536. self.$search.trigger('focus');
  1537. });
  1538. container.on('close', function () {
  1539. self.$search.val('');
  1540. self.$search.removeAttr('aria-activedescendant');
  1541. self.$search.removeAttr('aria-owns');
  1542. self.$search.trigger('focus');
  1543. });
  1544. container.on('enable', function () {
  1545. self.$search.prop('disabled', false);
  1546. self._transferTabIndex();
  1547. });
  1548. container.on('disable', function () {
  1549. self.$search.prop('disabled', true);
  1550. });
  1551. container.on('focus', function (evt) {
  1552. self.$search.trigger('focus');
  1553. });
  1554. container.on('results:focus', function (params) {
  1555. self.$search.attr('aria-activedescendant', params.data._resultId);
  1556. });
  1557. this.$selection.on('focusin', '.select2-search--inline', function (evt) {
  1558. self.trigger('focus', evt);
  1559. });
  1560. this.$selection.on('focusout', '.select2-search--inline', function (evt) {
  1561. self._handleBlur(evt);
  1562. });
  1563. this.$selection.on('keydown', '.select2-search--inline', function (evt) {
  1564. evt.stopPropagation();
  1565. self.trigger('keypress', evt);
  1566. self._keyUpPrevented = evt.isDefaultPrevented();
  1567. var key = evt.which;
  1568. if (key === KEYS.BACKSPACE && self.$search.val() === '') {
  1569. var $previousChoice = self.$searchContainer
  1570. .prev('.select2-selection__choice');
  1571. if ($previousChoice.length > 0) {
  1572. var item = $previousChoice.data('data');
  1573. self.searchRemoveChoice(item);
  1574. evt.preventDefault();
  1575. }
  1576. } else if (evt.which === KEYS.ENTER) {
  1577. container.open();
  1578. evt.preventDefault();
  1579. }
  1580. });
  1581. // Try to detect the IE version should the `documentMode` property that
  1582. // is stored on the document. This is only implemented in IE and is
  1583. // slightly cleaner than doing a user agent check.
  1584. // This property is not available in Edge, but Edge also doesn't have
  1585. // this bug.
  1586. var msie = document.documentMode;
  1587. var disableInputEvents = msie && msie <= 11;
  1588. // Workaround for browsers which do not support the `input` event
  1589. // This will prevent double-triggering of events for browsers which support
  1590. // both the `keyup` and `input` events.
  1591. this.$selection.on(
  1592. 'input.searchcheck',
  1593. '.select2-search--inline',
  1594. function (evt) {
  1595. // IE will trigger the `input` event when a placeholder is used on a
  1596. // search box. To get around this issue, we are forced to ignore all
  1597. // `input` events in IE and keep using `keyup`.
  1598. if (disableInputEvents) {
  1599. self.$selection.off('input.search input.searchcheck');
  1600. return;
  1601. }
  1602. // Unbind the duplicated `keyup` event
  1603. self.$selection.off('keyup.search');
  1604. }
  1605. );
  1606. this.$selection.on(
  1607. 'keyup.search input.search',
  1608. '.select2-search--inline',
  1609. function (evt) {
  1610. // IE will trigger the `input` event when a placeholder is used on a
  1611. // search box. To get around this issue, we are forced to ignore all
  1612. // `input` events in IE and keep using `keyup`.
  1613. if (disableInputEvents && evt.type === 'input') {
  1614. self.$selection.off('input.search input.searchcheck');
  1615. return;
  1616. }
  1617. var key = evt.which;
  1618. // We can freely ignore events from modifier keys
  1619. if (key == KEYS.SHIFT || key == KEYS.CTRL || key == KEYS.ALT) {
  1620. return;
  1621. }
  1622. // Tabbing will be handled during the `keydown` phase
  1623. if (key == KEYS.TAB) {
  1624. return;
  1625. }
  1626. self.handleSearch(evt);
  1627. }
  1628. );
  1629. };
  1630. /**
  1631. * This method will transfer the tabindex attribute from the rendered
  1632. * selection to the search box. This allows for the search box to be used as
  1633. * the primary focus instead of the selection container.
  1634. *
  1635. * @private
  1636. */
  1637. Search.prototype._transferTabIndex = function (decorated) {
  1638. this.$search.attr('tabindex', this.$selection.attr('tabindex'));
  1639. this.$selection.attr('tabindex', '-1');
  1640. };
  1641. Search.prototype.createPlaceholder = function (decorated, placeholder) {
  1642. this.$search.attr('placeholder', placeholder.text);
  1643. };
  1644. Search.prototype.update = function (decorated, data) {
  1645. var searchHadFocus = this.$search[0] == document.activeElement;
  1646. this.$search.attr('placeholder', '');
  1647. decorated.call(this, data);
  1648. this.$selection.find('.select2-selection__rendered')
  1649. .append(this.$searchContainer);
  1650. this.resizeSearch();
  1651. if (searchHadFocus) {
  1652. this.$search.focus();
  1653. }
  1654. };
  1655. Search.prototype.handleSearch = function () {
  1656. this.resizeSearch();
  1657. if (!this._keyUpPrevented) {
  1658. var input = this.$search.val();
  1659. this.trigger('query', {
  1660. term: input
  1661. });
  1662. }
  1663. this._keyUpPrevented = false;
  1664. };
  1665. Search.prototype.searchRemoveChoice = function (decorated, item) {
  1666. this.trigger('unselect', {
  1667. data: item
  1668. });
  1669. this.$search.val(item.text);
  1670. this.handleSearch();
  1671. };
  1672. Search.prototype.resizeSearch = function () {
  1673. this.$search.css('width', '25px');
  1674. var width = '';
  1675. if (this.$search.attr('placeholder') !== '') {
  1676. width = this.$selection.find('.select2-selection__rendered').innerWidth();
  1677. } else {
  1678. var minimumWidth = this.$search.val().length + 1;
  1679. width = (minimumWidth * 0.75) + 'em';
  1680. }
  1681. this.$search.css('width', width);
  1682. };
  1683. return Search;
  1684. });
  1685. S2.define('select2/selection/eventRelay',[
  1686. 'jquery'
  1687. ], function ($) {
  1688. function EventRelay () { }
  1689. EventRelay.prototype.bind = function (decorated, container, $container) {
  1690. var self = this;
  1691. var relayEvents = [
  1692. 'open', 'opening',
  1693. 'close', 'closing',
  1694. 'select', 'selecting',
  1695. 'unselect', 'unselecting'
  1696. ];
  1697. var preventableEvents = ['opening', 'closing', 'selecting', 'unselecting'];
  1698. decorated.call(this, container, $container);
  1699. container.on('*', function (name, params) {
  1700. // Ignore events that should not be relayed
  1701. if ($.inArray(name, relayEvents) === -1) {
  1702. return;
  1703. }
  1704. // The parameters should always be an object
  1705. params = params || {};
  1706. // Generate the jQuery event for the Select2 event
  1707. var evt = $.Event('select2:' + name, {
  1708. params: params
  1709. });
  1710. self.$element.trigger(evt);
  1711. // Only handle preventable events if it was one
  1712. if ($.inArray(name, preventableEvents) === -1) {
  1713. return;
  1714. }
  1715. params.prevented = evt.isDefaultPrevented();
  1716. });
  1717. };
  1718. return EventRelay;
  1719. });
  1720. S2.define('select2/translation',[
  1721. 'jquery',
  1722. 'require'
  1723. ], function ($, require) {
  1724. function Translation (dict) {
  1725. this.dict = dict || {};
  1726. }
  1727. Translation.prototype.all = function () {
  1728. return this.dict;
  1729. };
  1730. Translation.prototype.get = function (key) {
  1731. return this.dict[key];
  1732. };
  1733. Translation.prototype.extend = function (translation) {
  1734. this.dict = $.extend({}, translation.all(), this.dict);
  1735. };
  1736. // Static functions
  1737. Translation._cache = {};
  1738. Translation.loadPath = function (path) {
  1739. if (!(path in Translation._cache)) {
  1740. var translations = require(path);
  1741. Translation._cache[path] = translations;
  1742. }
  1743. return new Translation(Translation._cache[path]);
  1744. };
  1745. return Translation;
  1746. });
  1747. S2.define('select2/diacritics',[
  1748. ], function () {
  1749. var diacritics = {
  1750. '\u24B6': 'A',
  1751. '\uFF21': 'A',
  1752. '\u00C0': 'A',
  1753. '\u00C1': 'A',
  1754. '\u00C2': 'A',
  1755. '\u1EA6': 'A',
  1756. '\u1EA4': 'A',
  1757. '\u1EAA': 'A',
  1758. '\u1EA8': 'A',
  1759. '\u00C3': 'A',
  1760. '\u0100': 'A',
  1761. '\u0102': 'A',
  1762. '\u1EB0': 'A',
  1763. '\u1EAE': 'A',
  1764. '\u1EB4': 'A',
  1765. '\u1EB2': 'A',
  1766. '\u0226': 'A',
  1767. '\u01E0': 'A',
  1768. '\u00C4': 'A',
  1769. '\u01DE': 'A',
  1770. '\u1EA2': 'A',
  1771. '\u00C5': 'A',
  1772. '\u01FA': 'A',
  1773. '\u01CD': 'A',
  1774. '\u0200': 'A',
  1775. '\u0202': 'A',
  1776. '\u1EA0': 'A',
  1777. '\u1EAC': 'A',
  1778. '\u1EB6': 'A',
  1779. '\u1E00': 'A',
  1780. '\u0104': 'A',
  1781. '\u023A': 'A',
  1782. '\u2C6F': 'A',
  1783. '\uA732': 'AA',
  1784. '\u00C6': 'AE',
  1785. '\u01FC': 'AE',
  1786. '\u01E2': 'AE',
  1787. '\uA734': 'AO',
  1788. '\uA736': 'AU',
  1789. '\uA738': 'AV',
  1790. '\uA73A': 'AV',
  1791. '\uA73C': 'AY',
  1792. '\u24B7': 'B',
  1793. '\uFF22': 'B',
  1794. '\u1E02': 'B',
  1795. '\u1E04': 'B',
  1796. '\u1E06': 'B',
  1797. '\u0243': 'B',
  1798. '\u0182': 'B',
  1799. '\u0181': 'B',
  1800. '\u24B8': 'C',
  1801. '\uFF23': 'C',
  1802. '\u0106': 'C',
  1803. '\u0108': 'C',
  1804. '\u010A': 'C',
  1805. '\u010C': 'C',
  1806. '\u00C7': 'C',
  1807. '\u1E08': 'C',
  1808. '\u0187': 'C',
  1809. '\u023B': 'C',
  1810. '\uA73E': 'C',
  1811. '\u24B9': 'D',
  1812. '\uFF24': 'D',
  1813. '\u1E0A': 'D',
  1814. '\u010E': 'D',
  1815. '\u1E0C': 'D',
  1816. '\u1E10': 'D',
  1817. '\u1E12': 'D',
  1818. '\u1E0E': 'D',
  1819. '\u0110': 'D',
  1820. '\u018B': 'D',
  1821. '\u018A': 'D',
  1822. '\u0189': 'D',
  1823. '\uA779': 'D',
  1824. '\u01F1': 'DZ',
  1825. '\u01C4': 'DZ',
  1826. '\u01F2': 'Dz',
  1827. '\u01C5': 'Dz',
  1828. '\u24BA': 'E',
  1829. '\uFF25': 'E',
  1830. '\u00C8': 'E',
  1831. '\u00C9': 'E',
  1832. '\u00CA': 'E',
  1833. '\u1EC0': 'E',
  1834. '\u1EBE': 'E',
  1835. '\u1EC4': 'E',
  1836. '\u1EC2': 'E',
  1837. '\u1EBC': 'E',
  1838. '\u0112': 'E',
  1839. '\u1E14': 'E',
  1840. '\u1E16': 'E',
  1841. '\u0114': 'E',
  1842. '\u0116': 'E',
  1843. '\u00CB': 'E',
  1844. '\u1EBA': 'E',
  1845. '\u011A': 'E',
  1846. '\u0204': 'E',
  1847. '\u0206': 'E',
  1848. '\u1EB8': 'E',
  1849. '\u1EC6': 'E',
  1850. '\u0228': 'E',
  1851. '\u1E1C': 'E',
  1852. '\u0118': 'E',
  1853. '\u1E18': 'E',
  1854. '\u1E1A': 'E',
  1855. '\u0190': 'E',
  1856. '\u018E': 'E',
  1857. '\u24BB': 'F',
  1858. '\uFF26': 'F',
  1859. '\u1E1E': 'F',
  1860. '\u0191': 'F',
  1861. '\uA77B': 'F',
  1862. '\u24BC': 'G',
  1863. '\uFF27': 'G',
  1864. '\u01F4': 'G',
  1865. '\u011C': 'G',
  1866. '\u1E20': 'G',
  1867. '\u011E': 'G',
  1868. '\u0120': 'G',
  1869. '\u01E6': 'G',
  1870. '\u0122': 'G',
  1871. '\u01E4': 'G',
  1872. '\u0193': 'G',
  1873. '\uA7A0': 'G',
  1874. '\uA77D': 'G',
  1875. '\uA77E': 'G',
  1876. '\u24BD': 'H',
  1877. '\uFF28': 'H',
  1878. '\u0124': 'H',
  1879. '\u1E22': 'H',
  1880. '\u1E26': 'H',
  1881. '\u021E': 'H',
  1882. '\u1E24': 'H',
  1883. '\u1E28': 'H',
  1884. '\u1E2A': 'H',
  1885. '\u0126': 'H',
  1886. '\u2C67': 'H',
  1887. '\u2C75': 'H',
  1888. '\uA78D': 'H',
  1889. '\u24BE': 'I',
  1890. '\uFF29': 'I',
  1891. '\u00CC': 'I',
  1892. '\u00CD': 'I',
  1893. '\u00CE': 'I',
  1894. '\u0128': 'I',
  1895. '\u012A': 'I',
  1896. '\u012C': 'I',
  1897. '\u0130': 'I',
  1898. '\u00CF': 'I',
  1899. '\u1E2E': 'I',
  1900. '\u1EC8': 'I',
  1901. '\u01CF': 'I',
  1902. '\u0208': 'I',
  1903. '\u020A': 'I',
  1904. '\u1ECA': 'I',
  1905. '\u012E': 'I',
  1906. '\u1E2C': 'I',
  1907. '\u0197': 'I',
  1908. '\u24BF': 'J',
  1909. '\uFF2A': 'J',
  1910. '\u0134': 'J',
  1911. '\u0248': 'J',
  1912. '\u24C0': 'K',
  1913. '\uFF2B': 'K',
  1914. '\u1E30': 'K',
  1915. '\u01E8': 'K',
  1916. '\u1E32': 'K',
  1917. '\u0136': 'K',
  1918. '\u1E34': 'K',
  1919. '\u0198': 'K',
  1920. '\u2C69': 'K',
  1921. '\uA740': 'K',
  1922. '\uA742': 'K',
  1923. '\uA744': 'K',
  1924. '\uA7A2': 'K',
  1925. '\u24C1': 'L',
  1926. '\uFF2C': 'L',
  1927. '\u013F': 'L',
  1928. '\u0139': 'L',
  1929. '\u013D': 'L',
  1930. '\u1E36': 'L',
  1931. '\u1E38': 'L',
  1932. '\u013B': 'L',
  1933. '\u1E3C': 'L',
  1934. '\u1E3A': 'L',
  1935. '\u0141': 'L',
  1936. '\u023D': 'L',
  1937. '\u2C62': 'L',
  1938. '\u2C60': 'L',
  1939. '\uA748': 'L',
  1940. '\uA746': 'L',
  1941. '\uA780': 'L',
  1942. '\u01C7': 'LJ',
  1943. '\u01C8': 'Lj',
  1944. '\u24C2': 'M',
  1945. '\uFF2D': 'M',
  1946. '\u1E3E': 'M',
  1947. '\u1E40': 'M',
  1948. '\u1E42': 'M',
  1949. '\u2C6E': 'M',
  1950. '\u019C': 'M',
  1951. '\u24C3': 'N',
  1952. '\uFF2E': 'N',
  1953. '\u01F8': 'N',
  1954. '\u0143': 'N',
  1955. '\u00D1': 'N',
  1956. '\u1E44': 'N',
  1957. '\u0147': 'N',
  1958. '\u1E46': 'N',
  1959. '\u0145': 'N',
  1960. '\u1E4A': 'N',
  1961. '\u1E48': 'N',
  1962. '\u0220': 'N',
  1963. '\u019D': 'N',
  1964. '\uA790': 'N',
  1965. '\uA7A4': 'N',
  1966. '\u01CA': 'NJ',
  1967. '\u01CB': 'Nj',
  1968. '\u24C4': 'O',
  1969. '\uFF2F': 'O',
  1970. '\u00D2': 'O',
  1971. '\u00D3': 'O',
  1972. '\u00D4': 'O',
  1973. '\u1ED2': 'O',
  1974. '\u1ED0': 'O',
  1975. '\u1ED6': 'O',
  1976. '\u1ED4': 'O',
  1977. '\u00D5': 'O',
  1978. '\u1E4C': 'O',
  1979. '\u022C': 'O',
  1980. '\u1E4E': 'O',
  1981. '\u014C': 'O',
  1982. '\u1E50': 'O',
  1983. '\u1E52': 'O',
  1984. '\u014E': 'O',
  1985. '\u022E': 'O',
  1986. '\u0230': 'O',
  1987. '\u00D6': 'O',
  1988. '\u022A': 'O',
  1989. '\u1ECE': 'O',
  1990. '\u0150': 'O',
  1991. '\u01D1': 'O',
  1992. '\u020C': 'O',
  1993. '\u020E': 'O',
  1994. '\u01A0': 'O',
  1995. '\u1EDC': 'O',
  1996. '\u1EDA': 'O',
  1997. '\u1EE0': 'O',
  1998. '\u1EDE': 'O',
  1999. '\u1EE2': 'O',
  2000. '\u1ECC': 'O',
  2001. '\u1ED8': 'O',
  2002. '\u01EA': 'O',
  2003. '\u01EC': 'O',
  2004. '\u00D8': 'O',
  2005. '\u01FE': 'O',
  2006. '\u0186': 'O',
  2007. '\u019F': 'O',
  2008. '\uA74A': 'O',
  2009. '\uA74C': 'O',
  2010. '\u01A2': 'OI',
  2011. '\uA74E': 'OO',
  2012. '\u0222': 'OU',
  2013. '\u24C5': 'P',
  2014. '\uFF30': 'P',
  2015. '\u1E54': 'P',
  2016. '\u1E56': 'P',
  2017. '\u01A4': 'P',
  2018. '\u2C63': 'P',
  2019. '\uA750': 'P',
  2020. '\uA752': 'P',
  2021. '\uA754': 'P',
  2022. '\u24C6': 'Q',
  2023. '\uFF31': 'Q',
  2024. '\uA756': 'Q',
  2025. '\uA758': 'Q',
  2026. '\u024A': 'Q',
  2027. '\u24C7': 'R',
  2028. '\uFF32': 'R',
  2029. '\u0154': 'R',
  2030. '\u1E58': 'R',
  2031. '\u0158': 'R',
  2032. '\u0210': 'R',
  2033. '\u0212': 'R',
  2034. '\u1E5A': 'R',
  2035. '\u1E5C': 'R',
  2036. '\u0156': 'R',
  2037. '\u1E5E': 'R',
  2038. '\u024C': 'R',
  2039. '\u2C64': 'R',
  2040. '\uA75A': 'R',
  2041. '\uA7A6': 'R',
  2042. '\uA782': 'R',
  2043. '\u24C8': 'S',
  2044. '\uFF33': 'S',
  2045. '\u1E9E': 'S',
  2046. '\u015A': 'S',
  2047. '\u1E64': 'S',
  2048. '\u015C': 'S',
  2049. '\u1E60': 'S',
  2050. '\u0160': 'S',
  2051. '\u1E66': 'S',
  2052. '\u1E62': 'S',
  2053. '\u1E68': 'S',
  2054. '\u0218': 'S',
  2055. '\u015E': 'S',
  2056. '\u2C7E': 'S',
  2057. '\uA7A8': 'S',
  2058. '\uA784': 'S',
  2059. '\u24C9': 'T',
  2060. '\uFF34': 'T',
  2061. '\u1E6A': 'T',
  2062. '\u0164': 'T',
  2063. '\u1E6C': 'T',
  2064. '\u021A': 'T',
  2065. '\u0162': 'T',
  2066. '\u1E70': 'T',
  2067. '\u1E6E': 'T',
  2068. '\u0166': 'T',
  2069. '\u01AC': 'T',
  2070. '\u01AE': 'T',
  2071. '\u023E': 'T',
  2072. '\uA786': 'T',
  2073. '\uA728': 'TZ',
  2074. '\u24CA': 'U',
  2075. '\uFF35': 'U',
  2076. '\u00D9': 'U',
  2077. '\u00DA': 'U',
  2078. '\u00DB': 'U',
  2079. '\u0168': 'U',
  2080. '\u1E78': 'U',
  2081. '\u016A': 'U',
  2082. '\u1E7A': 'U',
  2083. '\u016C': 'U',
  2084. '\u00DC': 'U',
  2085. '\u01DB': 'U',
  2086. '\u01D7': 'U',
  2087. '\u01D5': 'U',
  2088. '\u01D9': 'U',
  2089. '\u1EE6': 'U',
  2090. '\u016E': 'U',
  2091. '\u0170': 'U',
  2092. '\u01D3': 'U',
  2093. '\u0214': 'U',
  2094. '\u0216': 'U',
  2095. '\u01AF': 'U',
  2096. '\u1EEA': 'U',
  2097. '\u1EE8': 'U',
  2098. '\u1EEE': 'U',
  2099. '\u1EEC': 'U',
  2100. '\u1EF0': 'U',
  2101. '\u1EE4': 'U',
  2102. '\u1E72': 'U',
  2103. '\u0172': 'U',
  2104. '\u1E76': 'U',
  2105. '\u1E74': 'U',
  2106. '\u0244': 'U',
  2107. '\u24CB': 'V',
  2108. '\uFF36': 'V',
  2109. '\u1E7C': 'V',
  2110. '\u1E7E': 'V',
  2111. '\u01B2': 'V',
  2112. '\uA75E': 'V',
  2113. '\u0245': 'V',
  2114. '\uA760': 'VY',
  2115. '\u24CC': 'W',
  2116. '\uFF37': 'W',
  2117. '\u1E80': 'W',
  2118. '\u1E82': 'W',
  2119. '\u0174': 'W',
  2120. '\u1E86': 'W',
  2121. '\u1E84': 'W',
  2122. '\u1E88': 'W',
  2123. '\u2C72': 'W',
  2124. '\u24CD': 'X',
  2125. '\uFF38': 'X',
  2126. '\u1E8A': 'X',
  2127. '\u1E8C': 'X',
  2128. '\u24CE': 'Y',
  2129. '\uFF39': 'Y',
  2130. '\u1EF2': 'Y',
  2131. '\u00DD': 'Y',
  2132. '\u0176': 'Y',
  2133. '\u1EF8': 'Y',
  2134. '\u0232': 'Y',
  2135. '\u1E8E': 'Y',
  2136. '\u0178': 'Y',
  2137. '\u1EF6': 'Y',
  2138. '\u1EF4': 'Y',
  2139. '\u01B3': 'Y',
  2140. '\u024E': 'Y',
  2141. '\u1EFE': 'Y',
  2142. '\u24CF': 'Z',
  2143. '\uFF3A': 'Z',
  2144. '\u0179': 'Z',
  2145. '\u1E90': 'Z',
  2146. '\u017B': 'Z',
  2147. '\u017D': 'Z',
  2148. '\u1E92': 'Z',
  2149. '\u1E94': 'Z',
  2150. '\u01B5': 'Z',
  2151. '\u0224': 'Z',
  2152. '\u2C7F': 'Z',
  2153. '\u2C6B': 'Z',
  2154. '\uA762': 'Z',
  2155. '\u24D0': 'a',
  2156. '\uFF41': 'a',
  2157. '\u1E9A': 'a',
  2158. '\u00E0': 'a',
  2159. '\u00E1': 'a',
  2160. '\u00E2': 'a',
  2161. '\u1EA7': 'a',
  2162. '\u1EA5': 'a',
  2163. '\u1EAB': 'a',
  2164. '\u1EA9': 'a',
  2165. '\u00E3': 'a',
  2166. '\u0101': 'a',
  2167. '\u0103': 'a',
  2168. '\u1EB1': 'a',
  2169. '\u1EAF': 'a',
  2170. '\u1EB5': 'a',
  2171. '\u1EB3': 'a',
  2172. '\u0227': 'a',
  2173. '\u01E1': 'a',
  2174. '\u00E4': 'a',
  2175. '\u01DF': 'a',
  2176. '\u1EA3': 'a',
  2177. '\u00E5': 'a',
  2178. '\u01FB': 'a',
  2179. '\u01CE': 'a',
  2180. '\u0201': 'a',
  2181. '\u0203': 'a',
  2182. '\u1EA1': 'a',
  2183. '\u1EAD': 'a',
  2184. '\u1EB7': 'a',
  2185. '\u1E01': 'a',
  2186. '\u0105': 'a',
  2187. '\u2C65': 'a',
  2188. '\u0250': 'a',
  2189. '\uA733': 'aa',
  2190. '\u00E6': 'ae',
  2191. '\u01FD': 'ae',
  2192. '\u01E3': 'ae',
  2193. '\uA735': 'ao',
  2194. '\uA737': 'au',
  2195. '\uA739': 'av',
  2196. '\uA73B': 'av',
  2197. '\uA73D': 'ay',
  2198. '\u24D1': 'b',
  2199. '\uFF42': 'b',
  2200. '\u1E03': 'b',
  2201. '\u1E05': 'b',
  2202. '\u1E07': 'b',
  2203. '\u0180': 'b',
  2204. '\u0183': 'b',
  2205. '\u0253': 'b',
  2206. '\u24D2': 'c',
  2207. '\uFF43': 'c',
  2208. '\u0107': 'c',
  2209. '\u0109': 'c',
  2210. '\u010B': 'c',
  2211. '\u010D': 'c',
  2212. '\u00E7': 'c',
  2213. '\u1E09': 'c',
  2214. '\u0188': 'c',
  2215. '\u023C': 'c',
  2216. '\uA73F': 'c',
  2217. '\u2184': 'c',
  2218. '\u24D3': 'd',
  2219. '\uFF44': 'd',
  2220. '\u1E0B': 'd',
  2221. '\u010F': 'd',
  2222. '\u1E0D': 'd',
  2223. '\u1E11': 'd',
  2224. '\u1E13': 'd',
  2225. '\u1E0F': 'd',
  2226. '\u0111': 'd',
  2227. '\u018C': 'd',
  2228. '\u0256': 'd',
  2229. '\u0257': 'd',
  2230. '\uA77A': 'd',
  2231. '\u01F3': 'dz',
  2232. '\u01C6': 'dz',
  2233. '\u24D4': 'e',
  2234. '\uFF45': 'e',
  2235. '\u00E8': 'e',
  2236. '\u00E9': 'e',
  2237. '\u00EA': 'e',
  2238. '\u1EC1': 'e',
  2239. '\u1EBF': 'e',
  2240. '\u1EC5': 'e',
  2241. '\u1EC3': 'e',
  2242. '\u1EBD': 'e',
  2243. '\u0113': 'e',
  2244. '\u1E15': 'e',
  2245. '\u1E17': 'e',
  2246. '\u0115': 'e',
  2247. '\u0117': 'e',
  2248. '\u00EB': 'e',
  2249. '\u1EBB': 'e',
  2250. '\u011B': 'e',
  2251. '\u0205': 'e',
  2252. '\u0207': 'e',
  2253. '\u1EB9': 'e',
  2254. '\u1EC7': 'e',
  2255. '\u0229': 'e',
  2256. '\u1E1D': 'e',
  2257. '\u0119': 'e',
  2258. '\u1E19': 'e',
  2259. '\u1E1B': 'e',
  2260. '\u0247': 'e',
  2261. '\u025B': 'e',
  2262. '\u01DD': 'e',
  2263. '\u24D5': 'f',
  2264. '\uFF46': 'f',
  2265. '\u1E1F': 'f',
  2266. '\u0192': 'f',
  2267. '\uA77C': 'f',
  2268. '\u24D6': 'g',
  2269. '\uFF47': 'g',
  2270. '\u01F5': 'g',
  2271. '\u011D': 'g',
  2272. '\u1E21': 'g',
  2273. '\u011F': 'g',
  2274. '\u0121': 'g',
  2275. '\u01E7': 'g',
  2276. '\u0123': 'g',
  2277. '\u01E5': 'g',
  2278. '\u0260': 'g',
  2279. '\uA7A1': 'g',
  2280. '\u1D79': 'g',
  2281. '\uA77F': 'g',
  2282. '\u24D7': 'h',
  2283. '\uFF48': 'h',
  2284. '\u0125': 'h',
  2285. '\u1E23': 'h',
  2286. '\u1E27': 'h',
  2287. '\u021F': 'h',
  2288. '\u1E25': 'h',
  2289. '\u1E29': 'h',
  2290. '\u1E2B': 'h',
  2291. '\u1E96': 'h',
  2292. '\u0127': 'h',
  2293. '\u2C68': 'h',
  2294. '\u2C76': 'h',
  2295. '\u0265': 'h',
  2296. '\u0195': 'hv',
  2297. '\u24D8': 'i',
  2298. '\uFF49': 'i',
  2299. '\u00EC': 'i',
  2300. '\u00ED': 'i',
  2301. '\u00EE': 'i',
  2302. '\u0129': 'i',
  2303. '\u012B': 'i',
  2304. '\u012D': 'i',
  2305. '\u00EF': 'i',
  2306. '\u1E2F': 'i',
  2307. '\u1EC9': 'i',
  2308. '\u01D0': 'i',
  2309. '\u0209': 'i',
  2310. '\u020B': 'i',
  2311. '\u1ECB': 'i',
  2312. '\u012F': 'i',
  2313. '\u1E2D': 'i',
  2314. '\u0268': 'i',
  2315. '\u0131': 'i',
  2316. '\u24D9': 'j',
  2317. '\uFF4A': 'j',
  2318. '\u0135': 'j',
  2319. '\u01F0': 'j',
  2320. '\u0249': 'j',
  2321. '\u24DA': 'k',
  2322. '\uFF4B': 'k',
  2323. '\u1E31': 'k',
  2324. '\u01E9': 'k',
  2325. '\u1E33': 'k',
  2326. '\u0137': 'k',
  2327. '\u1E35': 'k',
  2328. '\u0199': 'k',
  2329. '\u2C6A': 'k',
  2330. '\uA741': 'k',
  2331. '\uA743': 'k',
  2332. '\uA745': 'k',
  2333. '\uA7A3': 'k',
  2334. '\u24DB': 'l',
  2335. '\uFF4C': 'l',
  2336. '\u0140': 'l',
  2337. '\u013A': 'l',
  2338. '\u013E': 'l',
  2339. '\u1E37': 'l',
  2340. '\u1E39': 'l',
  2341. '\u013C': 'l',
  2342. '\u1E3D': 'l',
  2343. '\u1E3B': 'l',
  2344. '\u017F': 'l',
  2345. '\u0142': 'l',
  2346. '\u019A': 'l',
  2347. '\u026B': 'l',
  2348. '\u2C61': 'l',
  2349. '\uA749': 'l',
  2350. '\uA781': 'l',
  2351. '\uA747': 'l',
  2352. '\u01C9': 'lj',
  2353. '\u24DC': 'm',
  2354. '\uFF4D': 'm',
  2355. '\u1E3F': 'm',
  2356. '\u1E41': 'm',
  2357. '\u1E43': 'm',
  2358. '\u0271': 'm',
  2359. '\u026F': 'm',
  2360. '\u24DD': 'n',
  2361. '\uFF4E': 'n',
  2362. '\u01F9': 'n',
  2363. '\u0144': 'n',
  2364. '\u00F1': 'n',
  2365. '\u1E45': 'n',
  2366. '\u0148': 'n',
  2367. '\u1E47': 'n',
  2368. '\u0146': 'n',
  2369. '\u1E4B': 'n',
  2370. '\u1E49': 'n',
  2371. '\u019E': 'n',
  2372. '\u0272': 'n',
  2373. '\u0149': 'n',
  2374. '\uA791': 'n',
  2375. '\uA7A5': 'n',
  2376. '\u01CC': 'nj',
  2377. '\u24DE': 'o',
  2378. '\uFF4F': 'o',
  2379. '\u00F2': 'o',
  2380. '\u00F3': 'o',
  2381. '\u00F4': 'o',
  2382. '\u1ED3': 'o',
  2383. '\u1ED1': 'o',
  2384. '\u1ED7': 'o',
  2385. '\u1ED5': 'o',
  2386. '\u00F5': 'o',
  2387. '\u1E4D': 'o',
  2388. '\u022D': 'o',
  2389. '\u1E4F': 'o',
  2390. '\u014D': 'o',
  2391. '\u1E51': 'o',
  2392. '\u1E53': 'o',
  2393. '\u014F': 'o',
  2394. '\u022F': 'o',
  2395. '\u0231': 'o',
  2396. '\u00F6': 'o',
  2397. '\u022B': 'o',
  2398. '\u1ECF': 'o',
  2399. '\u0151': 'o',
  2400. '\u01D2': 'o',
  2401. '\u020D': 'o',
  2402. '\u020F': 'o',
  2403. '\u01A1': 'o',
  2404. '\u1EDD': 'o',
  2405. '\u1EDB': 'o',
  2406. '\u1EE1': 'o',
  2407. '\u1EDF': 'o',
  2408. '\u1EE3': 'o',
  2409. '\u1ECD': 'o',
  2410. '\u1ED9': 'o',
  2411. '\u01EB': 'o',
  2412. '\u01ED': 'o',
  2413. '\u00F8': 'o',
  2414. '\u01FF': 'o',
  2415. '\u0254': 'o',
  2416. '\uA74B': 'o',
  2417. '\uA74D': 'o',
  2418. '\u0275': 'o',
  2419. '\u01A3': 'oi',
  2420. '\u0223': 'ou',
  2421. '\uA74F': 'oo',
  2422. '\u24DF': 'p',
  2423. '\uFF50': 'p',
  2424. '\u1E55': 'p',
  2425. '\u1E57': 'p',
  2426. '\u01A5': 'p',
  2427. '\u1D7D': 'p',
  2428. '\uA751': 'p',
  2429. '\uA753': 'p',
  2430. '\uA755': 'p',
  2431. '\u24E0': 'q',
  2432. '\uFF51': 'q',
  2433. '\u024B': 'q',
  2434. '\uA757': 'q',
  2435. '\uA759': 'q',
  2436. '\u24E1': 'r',
  2437. '\uFF52': 'r',
  2438. '\u0155': 'r',
  2439. '\u1E59': 'r',
  2440. '\u0159': 'r',
  2441. '\u0211': 'r',
  2442. '\u0213': 'r',
  2443. '\u1E5B': 'r',
  2444. '\u1E5D': 'r',
  2445. '\u0157': 'r',
  2446. '\u1E5F': 'r',
  2447. '\u024D': 'r',
  2448. '\u027D': 'r',
  2449. '\uA75B': 'r',
  2450. '\uA7A7': 'r',
  2451. '\uA783': 'r',
  2452. '\u24E2': 's',
  2453. '\uFF53': 's',
  2454. '\u00DF': 's',
  2455. '\u015B': 's',
  2456. '\u1E65': 's',
  2457. '\u015D': 's',
  2458. '\u1E61': 's',
  2459. '\u0161': 's',
  2460. '\u1E67': 's',
  2461. '\u1E63': 's',
  2462. '\u1E69': 's',
  2463. '\u0219': 's',
  2464. '\u015F': 's',
  2465. '\u023F': 's',
  2466. '\uA7A9': 's',
  2467. '\uA785': 's',
  2468. '\u1E9B': 's',
  2469. '\u24E3': 't',
  2470. '\uFF54': 't',
  2471. '\u1E6B': 't',
  2472. '\u1E97': 't',
  2473. '\u0165': 't',
  2474. '\u1E6D': 't',
  2475. '\u021B': 't',
  2476. '\u0163': 't',
  2477. '\u1E71': 't',
  2478. '\u1E6F': 't',
  2479. '\u0167': 't',
  2480. '\u01AD': 't',
  2481. '\u0288': 't',
  2482. '\u2C66': 't',
  2483. '\uA787': 't',
  2484. '\uA729': 'tz',
  2485. '\u24E4': 'u',
  2486. '\uFF55': 'u',
  2487. '\u00F9': 'u',
  2488. '\u00FA': 'u',
  2489. '\u00FB': 'u',
  2490. '\u0169': 'u',
  2491. '\u1E79': 'u',
  2492. '\u016B': 'u',
  2493. '\u1E7B': 'u',
  2494. '\u016D': 'u',
  2495. '\u00FC': 'u',
  2496. '\u01DC': 'u',
  2497. '\u01D8': 'u',
  2498. '\u01D6': 'u',
  2499. '\u01DA': 'u',
  2500. '\u1EE7': 'u',
  2501. '\u016F': 'u',
  2502. '\u0171': 'u',
  2503. '\u01D4': 'u',
  2504. '\u0215': 'u',
  2505. '\u0217': 'u',
  2506. '\u01B0': 'u',
  2507. '\u1EEB': 'u',
  2508. '\u1EE9': 'u',
  2509. '\u1EEF': 'u',
  2510. '\u1EED': 'u',
  2511. '\u1EF1': 'u',
  2512. '\u1EE5': 'u',
  2513. '\u1E73': 'u',
  2514. '\u0173': 'u',
  2515. '\u1E77': 'u',
  2516. '\u1E75': 'u',
  2517. '\u0289': 'u',
  2518. '\u24E5': 'v',
  2519. '\uFF56': 'v',
  2520. '\u1E7D': 'v',
  2521. '\u1E7F': 'v',
  2522. '\u028B': 'v',
  2523. '\uA75F': 'v',
  2524. '\u028C': 'v',
  2525. '\uA761': 'vy',
  2526. '\u24E6': 'w',
  2527. '\uFF57': 'w',
  2528. '\u1E81': 'w',
  2529. '\u1E83': 'w',
  2530. '\u0175': 'w',
  2531. '\u1E87': 'w',
  2532. '\u1E85': 'w',
  2533. '\u1E98': 'w',
  2534. '\u1E89': 'w',
  2535. '\u2C73': 'w',
  2536. '\u24E7': 'x',
  2537. '\uFF58': 'x',
  2538. '\u1E8B': 'x',
  2539. '\u1E8D': 'x',
  2540. '\u24E8': 'y',
  2541. '\uFF59': 'y',
  2542. '\u1EF3': 'y',
  2543. '\u00FD': 'y',
  2544. '\u0177': 'y',
  2545. '\u1EF9': 'y',
  2546. '\u0233': 'y',
  2547. '\u1E8F': 'y',
  2548. '\u00FF': 'y',
  2549. '\u1EF7': 'y',
  2550. '\u1E99': 'y',
  2551. '\u1EF5': 'y',
  2552. '\u01B4': 'y',
  2553. '\u024F': 'y',
  2554. '\u1EFF': 'y',
  2555. '\u24E9': 'z',
  2556. '\uFF5A': 'z',
  2557. '\u017A': 'z',
  2558. '\u1E91': 'z',
  2559. '\u017C': 'z',
  2560. '\u017E': 'z',
  2561. '\u1E93': 'z',
  2562. '\u1E95': 'z',
  2563. '\u01B6': 'z',
  2564. '\u0225': 'z',
  2565. '\u0240': 'z',
  2566. '\u2C6C': 'z',
  2567. '\uA763': 'z',
  2568. '\u0386': '\u0391',
  2569. '\u0388': '\u0395',
  2570. '\u0389': '\u0397',
  2571. '\u038A': '\u0399',
  2572. '\u03AA': '\u0399',
  2573. '\u038C': '\u039F',
  2574. '\u038E': '\u03A5',
  2575. '\u03AB': '\u03A5',
  2576. '\u038F': '\u03A9',
  2577. '\u03AC': '\u03B1',
  2578. '\u03AD': '\u03B5',
  2579. '\u03AE': '\u03B7',
  2580. '\u03AF': '\u03B9',
  2581. '\u03CA': '\u03B9',
  2582. '\u0390': '\u03B9',
  2583. '\u03CC': '\u03BF',
  2584. '\u03CD': '\u03C5',
  2585. '\u03CB': '\u03C5',
  2586. '\u03B0': '\u03C5',
  2587. '\u03C9': '\u03C9',
  2588. '\u03C2': '\u03C3'
  2589. };
  2590. return diacritics;
  2591. });
  2592. S2.define('select2/data/base',[
  2593. '../utils'
  2594. ], function (Utils) {
  2595. function BaseAdapter ($element, options) {
  2596. BaseAdapter.__super__.constructor.call(this);
  2597. }
  2598. Utils.Extend(BaseAdapter, Utils.Observable);
  2599. BaseAdapter.prototype.current = function (callback) {
  2600. throw new Error('The `current` method must be defined in child classes.');
  2601. };
  2602. BaseAdapter.prototype.query = function (params, callback) {
  2603. throw new Error('The `query` method must be defined in child classes.');
  2604. };
  2605. BaseAdapter.prototype.bind = function (container, $container) {
  2606. // Can be implemented in subclasses
  2607. };
  2608. BaseAdapter.prototype.destroy = function () {
  2609. // Can be implemented in subclasses
  2610. };
  2611. BaseAdapter.prototype.generateResultId = function (container, data) {
  2612. var id = '';
  2613. if (container != null) {
  2614. id += container.id
  2615. } else {
  2616. id += Utils.generateChars(4);
  2617. }
  2618. id += '-result-';
  2619. id += Utils.generateChars(4);
  2620. if (data.id != null) {
  2621. id += '-' + data.id.toString();
  2622. } else {
  2623. id += '-' + Utils.generateChars(4);
  2624. }
  2625. return id;
  2626. };
  2627. return BaseAdapter;
  2628. });
  2629. S2.define('select2/data/select',[
  2630. './base',
  2631. '../utils',
  2632. 'jquery'
  2633. ], function (BaseAdapter, Utils, $) {
  2634. function SelectAdapter ($element, options) {
  2635. this.$element = $element;
  2636. this.options = options;
  2637. SelectAdapter.__super__.constructor.call(this);
  2638. }
  2639. Utils.Extend(SelectAdapter, BaseAdapter);
  2640. SelectAdapter.prototype.current = function (callback) {
  2641. var data = [];
  2642. var self = this;
  2643. this.$element.find(':selected').each(function () {
  2644. var $option = $(this);
  2645. var option = self.item($option);
  2646. data.push(option);
  2647. });
  2648. callback(data);
  2649. };
  2650. SelectAdapter.prototype.select = function (data) {
  2651. var self = this;
  2652. data.selected = true;
  2653. // If data.element is a DOM node, use it instead
  2654. if ($(data.element).is('option')) {
  2655. data.element.selected = true;
  2656. this.$element.trigger('change');
  2657. return;
  2658. }
  2659. if (this.$element.prop('multiple')) {
  2660. this.current(function (currentData) {
  2661. var val = [];
  2662. data = [data];
  2663. data.push.apply(data, currentData);
  2664. for (var d = 0; d < data.length; d++) {
  2665. var id = data[d].id;
  2666. if ($.inArray(id, val) === -1) {
  2667. val.push(id);
  2668. }
  2669. }
  2670. self.$element.val(val);
  2671. self.$element.trigger('change');
  2672. });
  2673. } else {
  2674. var val = data.id;
  2675. this.$element.val(val);
  2676. this.$element.trigger('change');
  2677. }
  2678. };
  2679. SelectAdapter.prototype.unselect = function (data) {
  2680. var self = this;
  2681. if (!this.$element.prop('multiple')) {
  2682. return;
  2683. }
  2684. data.selected = false;
  2685. if ($(data.element).is('option')) {
  2686. data.element.selected = false;
  2687. this.$element.trigger('change');
  2688. return;
  2689. }
  2690. this.current(function (currentData) {
  2691. var val = [];
  2692. for (var d = 0; d < currentData.length; d++) {
  2693. var id = currentData[d].id;
  2694. if (id !== data.id && $.inArray(id, val) === -1) {
  2695. val.push(id);
  2696. }
  2697. }
  2698. self.$element.val(val);
  2699. self.$element.trigger('change');
  2700. });
  2701. };
  2702. SelectAdapter.prototype.bind = function (container, $container) {
  2703. var self = this;
  2704. this.container = container;
  2705. container.on('select', function (params) {
  2706. self.select(params.data);
  2707. });
  2708. container.on('unselect', function (params) {
  2709. self.unselect(params.data);
  2710. });
  2711. };
  2712. SelectAdapter.prototype.destroy = function () {
  2713. // Remove anything added to child elements
  2714. this.$element.find('*').each(function () {
  2715. // Remove any custom data set by Select2
  2716. $.removeData(this, 'data');
  2717. });
  2718. };
  2719. SelectAdapter.prototype.query = function (params, callback) {
  2720. var data = [];
  2721. var self = this;
  2722. var $options = this.$element.children();
  2723. $options.each(function () {
  2724. var $option = $(this);
  2725. if (!$option.is('option') && !$option.is('optgroup')) {
  2726. return;
  2727. }
  2728. var option = self.item($option);
  2729. var matches = self.matches(params, option);
  2730. if (matches !== null) {
  2731. data.push(matches);
  2732. }
  2733. });
  2734. callback({
  2735. results: data
  2736. });
  2737. };
  2738. SelectAdapter.prototype.addOptions = function ($options) {
  2739. Utils.appendMany(this.$element, $options);
  2740. };
  2741. SelectAdapter.prototype.option = function (data) {
  2742. var option;
  2743. if (data.children) {
  2744. option = document.createElement('optgroup');
  2745. option.label = data.text;
  2746. } else {
  2747. option = document.createElement('option');
  2748. if (option.textContent !== undefined) {
  2749. option.textContent = data.text;
  2750. } else {
  2751. option.innerText = data.text;
  2752. }
  2753. }
  2754. if (data.id !== undefined) {
  2755. option.value = data.id;
  2756. }
  2757. if (data.disabled) {
  2758. option.disabled = true;
  2759. }
  2760. if (data.selected) {
  2761. option.selected = true;
  2762. }
  2763. if (data.title) {
  2764. option.title = data.title;
  2765. }
  2766. var $option = $(option);
  2767. var normalizedData = this._normalizeItem(data);
  2768. normalizedData.element = option;
  2769. // Override the option's data with the combined data
  2770. $.data(option, 'data', normalizedData);
  2771. return $option;
  2772. };
  2773. SelectAdapter.prototype.item = function ($option) {
  2774. var data = {};
  2775. data = $.data($option[0], 'data');
  2776. if (data != null) {
  2777. return data;
  2778. }
  2779. if ($option.is('option')) {
  2780. data = {
  2781. id: $option.val(),
  2782. text: $option.text(),
  2783. disabled: $option.prop('disabled'),
  2784. selected: $option.prop('selected'),
  2785. title: $option.prop('title')
  2786. };
  2787. } else if ($option.is('optgroup')) {
  2788. data = {
  2789. text: $option.prop('label'),
  2790. children: [],
  2791. title: $option.prop('title')
  2792. };
  2793. var $children = $option.children('option');
  2794. var children = [];
  2795. for (var c = 0; c < $children.length; c++) {
  2796. var $child = $($children[c]);
  2797. var child = this.item($child);
  2798. children.push(child);
  2799. }
  2800. data.children = children;
  2801. }
  2802. data = this._normalizeItem(data);
  2803. data.element = $option[0];
  2804. $.data($option[0], 'data', data);
  2805. return data;
  2806. };
  2807. SelectAdapter.prototype._normalizeItem = function (item) {
  2808. if (!$.isPlainObject(item)) {
  2809. item = {
  2810. id: item,
  2811. text: item
  2812. };
  2813. }
  2814. item = $.extend({}, {
  2815. text: ''
  2816. }, item);
  2817. var defaults = {
  2818. selected: false,
  2819. disabled: false
  2820. };
  2821. if (item.id != null) {
  2822. item.id = item.id.toString();
  2823. }
  2824. if (item.text != null) {
  2825. item.text = item.text.toString();
  2826. }
  2827. if (item._resultId == null && item.id) {
  2828. item._resultId = this.generateResultId(this.container, item);
  2829. }
  2830. return $.extend({}, defaults, item);
  2831. };
  2832. SelectAdapter.prototype.matches = function (params, data) {
  2833. var matcher = this.options.get('matcher');
  2834. return matcher(params, data);
  2835. };
  2836. return SelectAdapter;
  2837. });
  2838. S2.define('select2/data/array',[
  2839. './select',
  2840. '../utils',
  2841. 'jquery'
  2842. ], function (SelectAdapter, Utils, $) {
  2843. function ArrayAdapter ($element, options) {
  2844. var data = options.get('data') || [];
  2845. ArrayAdapter.__super__.constructor.call(this, $element, options);
  2846. this.addOptions(this.convertToOptions(data));
  2847. }
  2848. Utils.Extend(ArrayAdapter, SelectAdapter);
  2849. ArrayAdapter.prototype.select = function (data) {
  2850. var $option = this.$element.find('option').filter(function (i, elm) {
  2851. return elm.value == data.id.toString();
  2852. });
  2853. if ($option.length === 0) {
  2854. $option = this.option(data);
  2855. this.addOptions($option);
  2856. }
  2857. ArrayAdapter.__super__.select.call(this, data);
  2858. };
  2859. ArrayAdapter.prototype.convertToOptions = function (data) {
  2860. var self = this;
  2861. var $existing = this.$element.find('option');
  2862. var existingIds = $existing.map(function () {
  2863. return self.item($(this)).id;
  2864. }).get();
  2865. var $options = [];
  2866. // Filter out all items except for the one passed in the argument
  2867. function onlyItem (item) {
  2868. return function () {
  2869. return $(this).val() == item.id;
  2870. };
  2871. }
  2872. for (var d = 0; d < data.length; d++) {
  2873. var item = this._normalizeItem(data[d]);
  2874. // Skip items which were pre-loaded, only merge the data
  2875. if ($.inArray(item.id, existingIds) >= 0) {
  2876. var $existingOption = $existing.filter(onlyItem(item));
  2877. var existingData = this.item($existingOption);
  2878. var newData = $.extend(true, {}, item, existingData);
  2879. var $newOption = this.option(newData);
  2880. $existingOption.replaceWith($newOption);
  2881. continue;
  2882. }
  2883. var $option = this.option(item);
  2884. if (item.children) {
  2885. var $children = this.convertToOptions(item.children);
  2886. Utils.appendMany($option, $children);
  2887. }
  2888. $options.push($option);
  2889. }
  2890. return $options;
  2891. };
  2892. return ArrayAdapter;
  2893. });
  2894. S2.define('select2/data/ajax',[
  2895. './array',
  2896. '../utils',
  2897. 'jquery'
  2898. ], function (ArrayAdapter, Utils, $) {
  2899. function AjaxAdapter ($element, options) {
  2900. this.ajaxOptions = this._applyDefaults(options.get('ajax'));
  2901. if (this.ajaxOptions.processResults != null) {
  2902. this.processResults = this.ajaxOptions.processResults;
  2903. }
  2904. AjaxAdapter.__super__.constructor.call(this, $element, options);
  2905. }
  2906. Utils.Extend(AjaxAdapter, ArrayAdapter);
  2907. AjaxAdapter.prototype._applyDefaults = function (options) {
  2908. var defaults = {
  2909. data: function (params) {
  2910. return $.extend({}, params, {
  2911. q: params.term
  2912. });
  2913. },
  2914. transport: function (params, success, failure) {
  2915. var $request = $.ajax(params);
  2916. $request.then(success);
  2917. $request.fail(failure);
  2918. return $request;
  2919. }
  2920. };
  2921. return $.extend({}, defaults, options, true);
  2922. };
  2923. AjaxAdapter.prototype.processResults = function (results) {
  2924. return results;
  2925. };
  2926. AjaxAdapter.prototype.query = function (params, callback) {
  2927. var matches = [];
  2928. var self = this;
  2929. if (this._request != null) {
  2930. // JSONP requests cannot always be aborted
  2931. if ($.isFunction(this._request.abort)) {
  2932. this._request.abort();
  2933. }
  2934. this._request = null;
  2935. }
  2936. var options = $.extend({
  2937. type: 'GET'
  2938. }, this.ajaxOptions);
  2939. if (typeof options.url === 'function') {
  2940. options.url = options.url.call(this.$element, params);
  2941. }
  2942. if (typeof options.data === 'function') {
  2943. options.data = options.data.call(this.$element, params);
  2944. }
  2945. function request () {
  2946. var $request = options.transport(options, function (data) {
  2947. var results = self.processResults(data, params);
  2948. if (self.options.get('debug') && window.console && console.error) {
  2949. // Check to make sure that the response included a `results` key.
  2950. if (!results || !results.results || !$.isArray(results.results)) {
  2951. console.error(
  2952. 'Select2: The AJAX results did not return an array in the ' +
  2953. '`results` key of the response.'
  2954. );
  2955. }
  2956. }
  2957. callback(results);
  2958. self.container.focusOnActiveElement();
  2959. }, function () {
  2960. // Attempt to detect if a request was aborted
  2961. // Only works if the transport exposes a status property
  2962. if ($request.status && $request.status === '0') {
  2963. return;
  2964. }
  2965. self.trigger('results:message', {
  2966. message: 'errorLoading'
  2967. });
  2968. });
  2969. self._request = $request;
  2970. }
  2971. if (this.ajaxOptions.delay && params.term != null) {
  2972. if (this._queryTimeout) {
  2973. window.clearTimeout(this._queryTimeout);
  2974. }
  2975. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  2976. } else {
  2977. request();
  2978. }
  2979. };
  2980. return AjaxAdapter;
  2981. });
  2982. S2.define('select2/data/tags',[
  2983. 'jquery'
  2984. ], function ($) {
  2985. function Tags (decorated, $element, options) {
  2986. var tags = options.get('tags');
  2987. var createTag = options.get('createTag');
  2988. if (createTag !== undefined) {
  2989. this.createTag = createTag;
  2990. }
  2991. var insertTag = options.get('insertTag');
  2992. if (insertTag !== undefined) {
  2993. this.insertTag = insertTag;
  2994. }
  2995. decorated.call(this, $element, options);
  2996. if ($.isArray(tags)) {
  2997. for (var t = 0; t < tags.length; t++) {
  2998. var tag = tags[t];
  2999. var item = this._normalizeItem(tag);
  3000. var $option = this.option(item);
  3001. this.$element.append($option);
  3002. }
  3003. }
  3004. }
  3005. Tags.prototype.query = function (decorated, params, callback) {
  3006. var self = this;
  3007. this._removeOldTags();
  3008. if (params.term == null || params.page != null) {
  3009. decorated.call(this, params, callback);
  3010. return;
  3011. }
  3012. function wrapper (obj, child) {
  3013. var data = obj.results;
  3014. for (var i = 0; i < data.length; i++) {
  3015. var option = data[i];
  3016. var checkChildren = (
  3017. option.children != null &&
  3018. !wrapper({
  3019. results: option.children
  3020. }, true)
  3021. );
  3022. var optionText = (option.text || '').toUpperCase();
  3023. var paramsTerm = (params.term || '').toUpperCase();
  3024. var checkText = optionText === paramsTerm;
  3025. if (checkText || checkChildren) {
  3026. if (child) {
  3027. return false;
  3028. }
  3029. obj.data = data;
  3030. callback(obj);
  3031. return;
  3032. }
  3033. }
  3034. if (child) {
  3035. return true;
  3036. }
  3037. var tag = self.createTag(params);
  3038. if (tag != null) {
  3039. var $option = self.option(tag);
  3040. $option.attr('data-select2-tag', true);
  3041. self.addOptions([$option]);
  3042. self.insertTag(data, tag);
  3043. }
  3044. obj.results = data;
  3045. callback(obj);
  3046. }
  3047. decorated.call(this, params, wrapper);
  3048. };
  3049. Tags.prototype.createTag = function (decorated, params) {
  3050. var term = $.trim(params.term);
  3051. if (term === '') {
  3052. return null;
  3053. }
  3054. return {
  3055. id: term,
  3056. text: term
  3057. };
  3058. };
  3059. Tags.prototype.insertTag = function (_, data, tag) {
  3060. data.unshift(tag);
  3061. };
  3062. Tags.prototype._removeOldTags = function (_) {
  3063. var tag = this._lastTag;
  3064. var $options = this.$element.find('option[data-select2-tag]');
  3065. $options.each(function () {
  3066. if (this.selected) {
  3067. return;
  3068. }
  3069. $(this).remove();
  3070. });
  3071. };
  3072. return Tags;
  3073. });
  3074. S2.define('select2/data/tokenizer',[
  3075. 'jquery'
  3076. ], function ($) {
  3077. function Tokenizer (decorated, $element, options) {
  3078. var tokenizer = options.get('tokenizer');
  3079. if (tokenizer !== undefined) {
  3080. this.tokenizer = tokenizer;
  3081. }
  3082. decorated.call(this, $element, options);
  3083. }
  3084. Tokenizer.prototype.bind = function (decorated, container, $container) {
  3085. decorated.call(this, container, $container);
  3086. this.$search = container.dropdown.$search || container.selection.$search ||
  3087. $container.find('.select2-search__field');
  3088. };
  3089. Tokenizer.prototype.query = function (decorated, params, callback) {
  3090. var self = this;
  3091. function createAndSelect (data) {
  3092. // Normalize the data object so we can use it for checks
  3093. var item = self._normalizeItem(data);
  3094. // Check if the data object already exists as a tag
  3095. // Select it if it doesn't
  3096. var $existingOptions = self.$element.find('option').filter(function () {
  3097. return $(this).val() === item.id;
  3098. });
  3099. // If an existing option wasn't found for it, create the option
  3100. if (!$existingOptions.length) {
  3101. var $option = self.option(item);
  3102. $option.attr('data-select2-tag', true);
  3103. self._removeOldTags();
  3104. self.addOptions([$option]);
  3105. }
  3106. // Select the item, now that we know there is an option for it
  3107. select(item);
  3108. }
  3109. function select (data) {
  3110. self.trigger('select', {
  3111. data: data
  3112. });
  3113. }
  3114. params.term = params.term || '';
  3115. var tokenData = this.tokenizer(params, this.options, createAndSelect);
  3116. if (tokenData.term !== params.term) {
  3117. // Replace the search term if we have the search box
  3118. if (this.$search.length) {
  3119. this.$search.val(tokenData.term);
  3120. this.$search.focus();
  3121. }
  3122. params.term = tokenData.term;
  3123. }
  3124. decorated.call(this, params, callback);
  3125. };
  3126. Tokenizer.prototype.tokenizer = function (_, params, options, callback) {
  3127. var separators = options.get('tokenSeparators') || [];
  3128. var term = params.term;
  3129. var i = 0;
  3130. var createTag = this.createTag || function (params) {
  3131. return {
  3132. id: params.term,
  3133. text: params.term
  3134. };
  3135. };
  3136. while (i < term.length) {
  3137. var termChar = term[i];
  3138. if ($.inArray(termChar, separators) === -1) {
  3139. i++;
  3140. continue;
  3141. }
  3142. var part = term.substr(0, i);
  3143. var partParams = $.extend({}, params, {
  3144. term: part
  3145. });
  3146. var data = createTag(partParams);
  3147. if (data == null) {
  3148. i++;
  3149. continue;
  3150. }
  3151. callback(data);
  3152. // Reset the term to not include the tokenized portion
  3153. term = term.substr(i + 1) || '';
  3154. i = 0;
  3155. }
  3156. return {
  3157. term: term
  3158. };
  3159. };
  3160. return Tokenizer;
  3161. });
  3162. S2.define('select2/data/minimumInputLength',[
  3163. ], function () {
  3164. function MinimumInputLength (decorated, $e, options) {
  3165. this.minimumInputLength = options.get('minimumInputLength');
  3166. decorated.call(this, $e, options);
  3167. }
  3168. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  3169. params.term = params.term || '';
  3170. if (params.term.length < this.minimumInputLength) {
  3171. this.trigger('results:message', {
  3172. message: 'inputTooShort',
  3173. args: {
  3174. minimum: this.minimumInputLength,
  3175. input: params.term,
  3176. params: params
  3177. }
  3178. });
  3179. return;
  3180. }
  3181. decorated.call(this, params, callback);
  3182. };
  3183. return MinimumInputLength;
  3184. });
  3185. S2.define('select2/data/maximumInputLength',[
  3186. ], function () {
  3187. function MaximumInputLength (decorated, $e, options) {
  3188. this.maximumInputLength = options.get('maximumInputLength');
  3189. decorated.call(this, $e, options);
  3190. }
  3191. MaximumInputLength.prototype.query = function (decorated, params, callback) {
  3192. params.term = params.term || '';
  3193. if (this.maximumInputLength > 0 &&
  3194. params.term.length > this.maximumInputLength) {
  3195. this.trigger('results:message', {
  3196. message: 'inputTooLong',
  3197. args: {
  3198. maximum: this.maximumInputLength,
  3199. input: params.term,
  3200. params: params
  3201. }
  3202. });
  3203. return;
  3204. }
  3205. decorated.call(this, params, callback);
  3206. };
  3207. return MaximumInputLength;
  3208. });
  3209. S2.define('select2/data/maximumSelectionLength',[
  3210. ], function (){
  3211. function MaximumSelectionLength (decorated, $e, options) {
  3212. this.maximumSelectionLength = options.get('maximumSelectionLength');
  3213. decorated.call(this, $e, options);
  3214. }
  3215. MaximumSelectionLength.prototype.query =
  3216. function (decorated, params, callback) {
  3217. var self = this;
  3218. this.current(function (currentData) {
  3219. var count = currentData != null ? currentData.length : 0;
  3220. if (self.maximumSelectionLength > 0 &&
  3221. count >= self.maximumSelectionLength) {
  3222. self.trigger('results:message', {
  3223. message: 'maximumSelected',
  3224. args: {
  3225. maximum: self.maximumSelectionLength
  3226. }
  3227. });
  3228. return;
  3229. }
  3230. decorated.call(self, params, callback);
  3231. });
  3232. };
  3233. return MaximumSelectionLength;
  3234. });
  3235. S2.define('select2/dropdown',[
  3236. 'jquery',
  3237. './utils'
  3238. ], function ($, Utils) {
  3239. function Dropdown ($element, options) {
  3240. this.$element = $element;
  3241. this.options = options;
  3242. Dropdown.__super__.constructor.call(this);
  3243. }
  3244. Utils.Extend(Dropdown, Utils.Observable);
  3245. Dropdown.prototype.render = function () {
  3246. var $dropdown = $(
  3247. '<span class="select2-dropdown">' +
  3248. '<span class="select2-results"></span>' +
  3249. '</span>'
  3250. );
  3251. $dropdown.attr('dir', this.options.get('dir'));
  3252. this.$dropdown = $dropdown;
  3253. return $dropdown;
  3254. };
  3255. Dropdown.prototype.bind = function () {
  3256. // Should be implemented in subclasses
  3257. };
  3258. Dropdown.prototype.position = function ($dropdown, $container) {
  3259. // Should be implmented in subclasses
  3260. };
  3261. Dropdown.prototype.destroy = function () {
  3262. // Remove the dropdown from the DOM
  3263. this.$dropdown.remove();
  3264. };
  3265. return Dropdown;
  3266. });
  3267. S2.define('select2/dropdown/search',[
  3268. 'jquery',
  3269. '../utils'
  3270. ], function ($, Utils) {
  3271. function Search () { }
  3272. Search.prototype.render = function (decorated) {
  3273. var $rendered = decorated.call(this);
  3274. var $search = $(
  3275. '<span class="select2-search select2-search--dropdown">' +
  3276. '<input class="select2-search__field" type="text" tabindex="-1"' +
  3277. ' autocomplete="off" autocorrect="off" autocapitalize="off"' +
  3278. ' spellcheck="false" role="combobox" aria-autocomplete="list" aria-expanded="true" />' +
  3279. '</span>'
  3280. );
  3281. this.$searchContainer = $search;
  3282. this.$search = $search.find('input');
  3283. $rendered.prepend($search);
  3284. return $rendered;
  3285. };
  3286. Search.prototype.bind = function (decorated, container, $container) {
  3287. var self = this;
  3288. var resultsId = container.id + '-results';
  3289. decorated.call(this, container, $container);
  3290. this.$search.on('keydown', function (evt) {
  3291. self.trigger('keypress', evt);
  3292. self._keyUpPrevented = evt.isDefaultPrevented();
  3293. });
  3294. // Workaround for browsers which do not support the `input` event
  3295. // This will prevent double-triggering of events for browsers which support
  3296. // both the `keyup` and `input` events.
  3297. this.$search.on('input', function (evt) {
  3298. // Unbind the duplicated `keyup` event
  3299. $(this).off('keyup');
  3300. });
  3301. this.$search.on('keyup input', function (evt) {
  3302. self.handleSearch(evt);
  3303. });
  3304. container.on('open', function () {
  3305. self.$search.attr('tabindex', 0);
  3306. self.$search.attr('aria-owns', resultsId);
  3307. self.$search.focus();
  3308. window.setTimeout(function () {
  3309. self.$search.focus();
  3310. }, 0);
  3311. });
  3312. container.on('close', function () {
  3313. self.$search.attr('tabindex', -1);
  3314. self.$search.removeAttr('aria-activedescendant');
  3315. self.$search.removeAttr('aria-owns');
  3316. self.$search.val('');
  3317. });
  3318. container.on('focus', function () {
  3319. if (container.isOpen()) {
  3320. self.$search.focus();
  3321. }
  3322. });
  3323. container.on('results:all', function (params) {
  3324. if (params.query.term == null || params.query.term === '') {
  3325. var showSearch = self.showSearch(params);
  3326. if (showSearch) {
  3327. self.$searchContainer.removeClass('select2-search--hide');
  3328. } else {
  3329. self.$searchContainer.addClass('select2-search--hide');
  3330. }
  3331. }
  3332. });
  3333. container.on('results:focus', function (params) {
  3334. self.$search.attr('aria-activedescendant', params.data._resultId);
  3335. });
  3336. };
  3337. Search.prototype.handleSearch = function (evt) {
  3338. if (!this._keyUpPrevented) {
  3339. var input = this.$search.val();
  3340. this.trigger('query', {
  3341. term: input
  3342. });
  3343. }
  3344. this._keyUpPrevented = false;
  3345. };
  3346. Search.prototype.showSearch = function (_, params) {
  3347. return true;
  3348. };
  3349. return Search;
  3350. });
  3351. S2.define('select2/dropdown/hidePlaceholder',[
  3352. ], function () {
  3353. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  3354. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  3355. decorated.call(this, $element, options, dataAdapter);
  3356. }
  3357. HidePlaceholder.prototype.append = function (decorated, data) {
  3358. data.results = this.removePlaceholder(data.results);
  3359. decorated.call(this, data);
  3360. };
  3361. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  3362. if (typeof placeholder === 'string') {
  3363. placeholder = {
  3364. id: '',
  3365. text: placeholder
  3366. };
  3367. }
  3368. return placeholder;
  3369. };
  3370. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  3371. var modifiedData = data.slice(0);
  3372. for (var d = data.length - 1; d >= 0; d--) {
  3373. var item = data[d];
  3374. if (this.placeholder.id === item.id) {
  3375. modifiedData.splice(d, 1);
  3376. }
  3377. }
  3378. return modifiedData;
  3379. };
  3380. return HidePlaceholder;
  3381. });
  3382. S2.define('select2/dropdown/infiniteScroll',[
  3383. 'jquery'
  3384. ], function ($) {
  3385. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  3386. this.lastParams = {};
  3387. decorated.call(this, $element, options, dataAdapter);
  3388. this.$loadingMore = this.createLoadingMore();
  3389. this.loading = false;
  3390. }
  3391. InfiniteScroll.prototype.append = function (decorated, data) {
  3392. this.$loadingMore.remove();
  3393. this.loading = false;
  3394. decorated.call(this, data);
  3395. if (this.showLoadingMore(data)) {
  3396. this.$results.append(this.$loadingMore);
  3397. }
  3398. };
  3399. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  3400. var self = this;
  3401. decorated.call(this, container, $container);
  3402. container.on('query', function (params) {
  3403. self.lastParams = params;
  3404. self.loading = true;
  3405. });
  3406. container.on('query:append', function (params) {
  3407. self.lastParams = params;
  3408. self.loading = true;
  3409. });
  3410. this.$results.on('scroll', function () {
  3411. var isLoadMoreVisible = $.contains(
  3412. document.documentElement,
  3413. self.$loadingMore[0]
  3414. );
  3415. if (self.loading || !isLoadMoreVisible) {
  3416. return;
  3417. }
  3418. var currentOffset = self.$results.offset().top +
  3419. self.$results.outerHeight(false);
  3420. var loadingMoreOffset = self.$loadingMore.offset().top +
  3421. self.$loadingMore.outerHeight(false);
  3422. if (currentOffset + 50 >= loadingMoreOffset) {
  3423. self.loadMore();
  3424. }
  3425. });
  3426. };
  3427. InfiniteScroll.prototype.loadMore = function () {
  3428. this.loading = true;
  3429. var params = $.extend({}, {page: 1}, this.lastParams);
  3430. params.page++;
  3431. this.trigger('query:append', params);
  3432. };
  3433. InfiniteScroll.prototype.showLoadingMore = function (_, data) {
  3434. return data.pagination && data.pagination.more;
  3435. };
  3436. InfiniteScroll.prototype.createLoadingMore = function () {
  3437. var $option = $(
  3438. '<li ' +
  3439. 'class="select2-results__option select2-results__option--load-more"' +
  3440. 'role="option" aria-disabled="true"></li>'
  3441. );
  3442. var message = this.options.get('translations').get('loadingMore');
  3443. $option.html(message(this.lastParams));
  3444. return $option;
  3445. };
  3446. return InfiniteScroll;
  3447. });
  3448. S2.define('select2/dropdown/attachBody',[
  3449. 'jquery',
  3450. '../utils'
  3451. ], function ($, Utils) {
  3452. function AttachBody (decorated, $element, options) {
  3453. this.$dropdownParent = options.get('dropdownParent') || $(document.body);
  3454. decorated.call(this, $element, options);
  3455. }
  3456. AttachBody.prototype.bind = function (decorated, container, $container) {
  3457. var self = this;
  3458. var setupResultsEvents = false;
  3459. decorated.call(this, container, $container);
  3460. container.on('open', function () {
  3461. self._showDropdown();
  3462. self._attachPositioningHandler(container);
  3463. if (!setupResultsEvents) {
  3464. setupResultsEvents = true;
  3465. container.on('results:all', function () {
  3466. self._positionDropdown();
  3467. self._resizeDropdown();
  3468. });
  3469. container.on('results:append', function () {
  3470. self._positionDropdown();
  3471. self._resizeDropdown();
  3472. });
  3473. }
  3474. });
  3475. container.on('close', function () {
  3476. self._hideDropdown();
  3477. self._detachPositioningHandler(container);
  3478. });
  3479. this.$dropdownContainer.on('mousedown', function (evt) {
  3480. evt.stopPropagation();
  3481. });
  3482. };
  3483. AttachBody.prototype.destroy = function (decorated) {
  3484. decorated.call(this);
  3485. this.$dropdownContainer.remove();
  3486. };
  3487. AttachBody.prototype.position = function (decorated, $dropdown, $container) {
  3488. // Clone all of the container classes
  3489. $dropdown.attr('class', $container.attr('class'));
  3490. $dropdown.removeClass('select2');
  3491. $dropdown.addClass('select2-container--open');
  3492. $dropdown.css({
  3493. position: 'absolute',
  3494. top: -999999
  3495. });
  3496. this.$container = $container;
  3497. };
  3498. AttachBody.prototype.render = function (decorated) {
  3499. var $container = $('<span></span>');
  3500. var $dropdown = decorated.call(this);
  3501. $container.append($dropdown);
  3502. this.$dropdownContainer = $container;
  3503. return $container;
  3504. };
  3505. AttachBody.prototype._hideDropdown = function (decorated) {
  3506. this.$dropdownContainer.detach();
  3507. };
  3508. AttachBody.prototype._attachPositioningHandler =
  3509. function (decorated, container) {
  3510. var self = this;
  3511. var scrollEvent = 'scroll.select2.' + container.id;
  3512. var resizeEvent = 'resize.select2.' + container.id;
  3513. var orientationEvent = 'orientationchange.select2.' + container.id;
  3514. var $watchers = this.$container.parents().filter(Utils.hasScroll);
  3515. $watchers.each(function () {
  3516. $(this).data('select2-scroll-position', {
  3517. x: $(this).scrollLeft(),
  3518. y: $(this).scrollTop()
  3519. });
  3520. });
  3521. $watchers.on(scrollEvent, function (ev) {
  3522. var position = $(this).data('select2-scroll-position');
  3523. $(this).scrollTop(position.y);
  3524. });
  3525. $(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
  3526. function (e) {
  3527. self._positionDropdown();
  3528. self._resizeDropdown();
  3529. });
  3530. };
  3531. AttachBody.prototype._detachPositioningHandler =
  3532. function (decorated, container) {
  3533. var scrollEvent = 'scroll.select2.' + container.id;
  3534. var resizeEvent = 'resize.select2.' + container.id;
  3535. var orientationEvent = 'orientationchange.select2.' + container.id;
  3536. var $watchers = this.$container.parents().filter(Utils.hasScroll);
  3537. $watchers.off(scrollEvent);
  3538. $(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
  3539. };
  3540. AttachBody.prototype._positionDropdown = function () {
  3541. var $window = $(window);
  3542. var isCurrentlyAbove = this.$dropdown.hasClass('select2-dropdown--above');
  3543. var isCurrentlyBelow = this.$dropdown.hasClass('select2-dropdown--below');
  3544. var newDirection = null;
  3545. var offset = this.$container.offset();
  3546. offset.bottom = offset.top + this.$container.outerHeight(false);
  3547. var container = {
  3548. height: this.$container.outerHeight(false)
  3549. };
  3550. container.top = offset.top;
  3551. container.bottom = offset.top + container.height;
  3552. var dropdown = {
  3553. height: this.$dropdown.outerHeight(false)
  3554. };
  3555. var viewport = {
  3556. top: $window.scrollTop(),
  3557. bottom: $window.scrollTop() + $window.height()
  3558. };
  3559. var enoughRoomAbove = viewport.top < (offset.top - dropdown.height);
  3560. var enoughRoomBelow = viewport.bottom > (offset.bottom + dropdown.height);
  3561. var css = {
  3562. left: offset.left,
  3563. top: container.bottom
  3564. };
  3565. // Determine what the parent element is to use for calciulating the offset
  3566. var $offsetParent = this.$dropdownParent;
  3567. // For statically positoned elements, we need to get the element
  3568. // that is determining the offset
  3569. if ($offsetParent.css('position') === 'static') {
  3570. $offsetParent = $offsetParent.offsetParent();
  3571. }
  3572. var parentOffset = $offsetParent.offset();
  3573. css.top -= parentOffset.top;
  3574. css.left -= parentOffset.left;
  3575. if (!isCurrentlyAbove && !isCurrentlyBelow) {
  3576. newDirection = 'below';
  3577. }
  3578. if (!enoughRoomBelow && enoughRoomAbove && !isCurrentlyAbove) {
  3579. newDirection = 'above';
  3580. } else if (!enoughRoomAbove && enoughRoomBelow && isCurrentlyAbove) {
  3581. newDirection = 'below';
  3582. }
  3583. if (newDirection == 'above' ||
  3584. (isCurrentlyAbove && newDirection !== 'below')) {
  3585. css.top = container.top - parentOffset.top - dropdown.height;
  3586. }
  3587. if (newDirection != null) {
  3588. this.$dropdown
  3589. .removeClass('select2-dropdown--below select2-dropdown--above')
  3590. .addClass('select2-dropdown--' + newDirection);
  3591. this.$container
  3592. .removeClass('select2-container--below select2-container--above')
  3593. .addClass('select2-container--' + newDirection);
  3594. }
  3595. this.$dropdownContainer.css(css);
  3596. };
  3597. AttachBody.prototype._resizeDropdown = function () {
  3598. var css = {
  3599. width: this.$container.outerWidth(false) + 'px'
  3600. };
  3601. if (this.options.get('dropdownAutoWidth')) {
  3602. css.minWidth = css.width;
  3603. css.position = 'relative';
  3604. css.width = 'auto';
  3605. }
  3606. this.$dropdown.css(css);
  3607. };
  3608. AttachBody.prototype._showDropdown = function (decorated) {
  3609. this.$dropdownContainer.appendTo(this.$dropdownParent);
  3610. this._positionDropdown();
  3611. this._resizeDropdown();
  3612. };
  3613. return AttachBody;
  3614. });
  3615. S2.define('select2/dropdown/minimumResultsForSearch',[
  3616. ], function () {
  3617. function countResults (data) {
  3618. var count = 0;
  3619. for (var d = 0; d < data.length; d++) {
  3620. var item = data[d];
  3621. if (item.children) {
  3622. count += countResults(item.children);
  3623. } else {
  3624. count++;
  3625. }
  3626. }
  3627. return count;
  3628. }
  3629. function MinimumResultsForSearch (decorated, $element, options, dataAdapter) {
  3630. this.minimumResultsForSearch = options.get('minimumResultsForSearch');
  3631. if (this.minimumResultsForSearch < 0) {
  3632. this.minimumResultsForSearch = Infinity;
  3633. }
  3634. decorated.call(this, $element, options, dataAdapter);
  3635. }
  3636. MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
  3637. if (countResults(params.data.results) < this.minimumResultsForSearch) {
  3638. return false;
  3639. }
  3640. return decorated.call(this, params);
  3641. };
  3642. return MinimumResultsForSearch;
  3643. });
  3644. S2.define('select2/dropdown/selectOnClose',[
  3645. ], function () {
  3646. function SelectOnClose () { }
  3647. SelectOnClose.prototype.bind = function (decorated, container, $container) {
  3648. var self = this;
  3649. decorated.call(this, container, $container);
  3650. container.on('close', function (params) {
  3651. self._handleSelectOnClose(params);
  3652. });
  3653. };
  3654. SelectOnClose.prototype._handleSelectOnClose = function (_, params) {
  3655. if (params && params.originalSelect2Event != null) {
  3656. var event = params.originalSelect2Event;
  3657. // Don't select an item if the close event was triggered from a select or
  3658. // unselect event
  3659. if (event._type === 'select' || event._type === 'unselect') {
  3660. return;
  3661. }
  3662. }
  3663. var $highlightedResults = this.getHighlightedResults();
  3664. // Only select highlighted results
  3665. if ($highlightedResults.length < 1) {
  3666. return;
  3667. }
  3668. var data = $highlightedResults.data('data');
  3669. // Don't re-select already selected resulte
  3670. if (
  3671. (data.element != null && data.element.selected) ||
  3672. (data.element == null && data.selected)
  3673. ) {
  3674. return;
  3675. }
  3676. this.trigger('select', {
  3677. data: data
  3678. });
  3679. };
  3680. return SelectOnClose;
  3681. });
  3682. S2.define('select2/dropdown/closeOnSelect',[
  3683. ], function () {
  3684. function CloseOnSelect () { }
  3685. CloseOnSelect.prototype.bind = function (decorated, container, $container) {
  3686. var self = this;
  3687. decorated.call(this, container, $container);
  3688. container.on('select', function (evt) {
  3689. self._selectTriggered(evt);
  3690. });
  3691. container.on('unselect', function (evt) {
  3692. self._selectTriggered(evt);
  3693. });
  3694. };
  3695. CloseOnSelect.prototype._selectTriggered = function (_, evt) {
  3696. var originalEvent = evt.originalEvent;
  3697. // Don't close if the control key is being held
  3698. if (originalEvent && originalEvent.ctrlKey) {
  3699. return;
  3700. }
  3701. this.trigger('close', {
  3702. originalEvent: originalEvent,
  3703. originalSelect2Event: evt
  3704. });
  3705. };
  3706. return CloseOnSelect;
  3707. });
  3708. S2.define('select2/i18n/en',[],function () {
  3709. // English
  3710. return {
  3711. errorLoading: function () {
  3712. return 'The results could not be loaded.';
  3713. },
  3714. inputTooLong: function (args) {
  3715. var overChars = args.input.length - args.maximum;
  3716. var message = 'Please delete ' + overChars + ' character';
  3717. if (overChars != 1) {
  3718. message += 's';
  3719. }
  3720. return message;
  3721. },
  3722. inputTooShort: function (args) {
  3723. var remainingChars = args.minimum - args.input.length;
  3724. var message = 'Please enter ' + remainingChars + ' or more characters';
  3725. return message;
  3726. },
  3727. loadingMore: function () {
  3728. return 'Loading more results…';
  3729. },
  3730. maximumSelected: function (args) {
  3731. var message = 'You can only select ' + args.maximum + ' item';
  3732. if (args.maximum != 1) {
  3733. message += 's';
  3734. }
  3735. return message;
  3736. },
  3737. noResults: function () {
  3738. return 'No results found';
  3739. },
  3740. searching: function () {
  3741. return 'Searching…';
  3742. }
  3743. };
  3744. });
  3745. S2.define('select2/defaults',[
  3746. 'jquery',
  3747. 'require',
  3748. './results',
  3749. './selection/single',
  3750. './selection/multiple',
  3751. './selection/placeholder',
  3752. './selection/allowClear',
  3753. './selection/search',
  3754. './selection/eventRelay',
  3755. './utils',
  3756. './translation',
  3757. './diacritics',
  3758. './data/select',
  3759. './data/array',
  3760. './data/ajax',
  3761. './data/tags',
  3762. './data/tokenizer',
  3763. './data/minimumInputLength',
  3764. './data/maximumInputLength',
  3765. './data/maximumSelectionLength',
  3766. './dropdown',
  3767. './dropdown/search',
  3768. './dropdown/hidePlaceholder',
  3769. './dropdown/infiniteScroll',
  3770. './dropdown/attachBody',
  3771. './dropdown/minimumResultsForSearch',
  3772. './dropdown/selectOnClose',
  3773. './dropdown/closeOnSelect',
  3774. './i18n/en'
  3775. ], function ($, require,
  3776. ResultsList,
  3777. SingleSelection, MultipleSelection, Placeholder, AllowClear,
  3778. SelectionSearch, EventRelay,
  3779. Utils, Translation, DIACRITICS,
  3780. SelectData, ArrayData, AjaxData, Tags, Tokenizer,
  3781. MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
  3782. Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
  3783. AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
  3784. EnglishTranslation) {
  3785. function Defaults () {
  3786. this.reset();
  3787. }
  3788. Defaults.prototype.apply = function (options) {
  3789. options = $.extend(true, {}, this.defaults, options);
  3790. if (options.dataAdapter == null) {
  3791. if (options.ajax != null) {
  3792. options.dataAdapter = AjaxData;
  3793. } else if (options.data != null) {
  3794. options.dataAdapter = ArrayData;
  3795. } else {
  3796. options.dataAdapter = SelectData;
  3797. }
  3798. if (options.minimumInputLength > 0) {
  3799. options.dataAdapter = Utils.Decorate(
  3800. options.dataAdapter,
  3801. MinimumInputLength
  3802. );
  3803. }
  3804. if (options.maximumInputLength > 0) {
  3805. options.dataAdapter = Utils.Decorate(
  3806. options.dataAdapter,
  3807. MaximumInputLength
  3808. );
  3809. }
  3810. if (options.maximumSelectionLength > 0) {
  3811. options.dataAdapter = Utils.Decorate(
  3812. options.dataAdapter,
  3813. MaximumSelectionLength
  3814. );
  3815. }
  3816. if (options.tags) {
  3817. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  3818. }
  3819. if (options.tokenSeparators != null || options.tokenizer != null) {
  3820. options.dataAdapter = Utils.Decorate(
  3821. options.dataAdapter,
  3822. Tokenizer
  3823. );
  3824. }
  3825. if (options.query != null) {
  3826. var Query = require(options.amdBase + 'compat/query');
  3827. options.dataAdapter = Utils.Decorate(
  3828. options.dataAdapter,
  3829. Query
  3830. );
  3831. }
  3832. if (options.initSelection != null) {
  3833. var InitSelection = require(options.amdBase + 'compat/initSelection');
  3834. options.dataAdapter = Utils.Decorate(
  3835. options.dataAdapter,
  3836. InitSelection
  3837. );
  3838. }
  3839. }
  3840. if (options.resultsAdapter == null) {
  3841. options.resultsAdapter = ResultsList;
  3842. if (options.ajax != null) {
  3843. options.resultsAdapter = Utils.Decorate(
  3844. options.resultsAdapter,
  3845. InfiniteScroll
  3846. );
  3847. }
  3848. if (options.placeholder != null) {
  3849. options.resultsAdapter = Utils.Decorate(
  3850. options.resultsAdapter,
  3851. HidePlaceholder
  3852. );
  3853. }
  3854. if (options.selectOnClose) {
  3855. options.resultsAdapter = Utils.Decorate(
  3856. options.resultsAdapter,
  3857. SelectOnClose
  3858. );
  3859. }
  3860. }
  3861. if (options.dropdownAdapter == null) {
  3862. if (options.multiple) {
  3863. options.dropdownAdapter = Dropdown;
  3864. } else {
  3865. var SearchableDropdown = Utils.Decorate(Dropdown, DropdownSearch);
  3866. options.dropdownAdapter = SearchableDropdown;
  3867. }
  3868. if (options.minimumResultsForSearch !== 0) {
  3869. options.dropdownAdapter = Utils.Decorate(
  3870. options.dropdownAdapter,
  3871. MinimumResultsForSearch
  3872. );
  3873. }
  3874. if (options.closeOnSelect) {
  3875. options.dropdownAdapter = Utils.Decorate(
  3876. options.dropdownAdapter,
  3877. CloseOnSelect
  3878. );
  3879. }
  3880. if (
  3881. options.dropdownCssClass != null ||
  3882. options.dropdownCss != null ||
  3883. options.adaptDropdownCssClass != null
  3884. ) {
  3885. var DropdownCSS = require(options.amdBase + 'compat/dropdownCss');
  3886. options.dropdownAdapter = Utils.Decorate(
  3887. options.dropdownAdapter,
  3888. DropdownCSS
  3889. );
  3890. }
  3891. options.dropdownAdapter = Utils.Decorate(
  3892. options.dropdownAdapter,
  3893. AttachBody
  3894. );
  3895. }
  3896. if (options.selectionAdapter == null) {
  3897. if (options.multiple) {
  3898. options.selectionAdapter = MultipleSelection;
  3899. } else {
  3900. options.selectionAdapter = SingleSelection;
  3901. }
  3902. // Add the placeholder mixin if a placeholder was specified
  3903. if (options.placeholder != null) {
  3904. options.selectionAdapter = Utils.Decorate(
  3905. options.selectionAdapter,
  3906. Placeholder
  3907. );
  3908. }
  3909. if (options.allowClear) {
  3910. options.selectionAdapter = Utils.Decorate(
  3911. options.selectionAdapter,
  3912. AllowClear
  3913. );
  3914. }
  3915. if (options.multiple) {
  3916. options.selectionAdapter = Utils.Decorate(
  3917. options.selectionAdapter,
  3918. SelectionSearch
  3919. );
  3920. }
  3921. if (
  3922. options.containerCssClass != null ||
  3923. options.containerCss != null ||
  3924. options.adaptContainerCssClass != null
  3925. ) {
  3926. var ContainerCSS = require(options.amdBase + 'compat/containerCss');
  3927. options.selectionAdapter = Utils.Decorate(
  3928. options.selectionAdapter,
  3929. ContainerCSS
  3930. );
  3931. }
  3932. options.selectionAdapter = Utils.Decorate(
  3933. options.selectionAdapter,
  3934. EventRelay
  3935. );
  3936. }
  3937. if (typeof options.language === 'string') {
  3938. // Check if the language is specified with a region
  3939. if (options.language.indexOf('-') > 0) {
  3940. // Extract the region information if it is included
  3941. var languageParts = options.language.split('-');
  3942. var baseLanguage = languageParts[0];
  3943. options.language = [options.language, baseLanguage];
  3944. } else {
  3945. options.language = [options.language];
  3946. }
  3947. }
  3948. if ($.isArray(options.language)) {
  3949. var languages = new Translation();
  3950. options.language.push('en');
  3951. var languageNames = options.language;
  3952. for (var l = 0; l < languageNames.length; l++) {
  3953. var name = languageNames[l];
  3954. var language = {};
  3955. try {
  3956. // Try to load it with the original name
  3957. language = Translation.loadPath(name);
  3958. } catch (e) {
  3959. try {
  3960. // If we couldn't load it, check if it wasn't the full path
  3961. name = this.defaults.amdLanguageBase + name;
  3962. language = Translation.loadPath(name);
  3963. } catch (ex) {
  3964. // The translation could not be loaded at all. Sometimes this is
  3965. // because of a configuration problem, other times this can be
  3966. // because of how Select2 helps load all possible translation files.
  3967. if (options.debug && window.console && console.warn) {
  3968. console.warn(
  3969. 'Select2: The language file for "' + name + '" could not be ' +
  3970. 'automatically loaded. A fallback will be used instead.'
  3971. );
  3972. }
  3973. continue;
  3974. }
  3975. }
  3976. languages.extend(language);
  3977. }
  3978. options.translations = languages;
  3979. } else {
  3980. var baseTranslation = Translation.loadPath(
  3981. this.defaults.amdLanguageBase + 'en'
  3982. );
  3983. var customTranslation = new Translation(options.language);
  3984. customTranslation.extend(baseTranslation);
  3985. options.translations = customTranslation;
  3986. }
  3987. return options;
  3988. };
  3989. Defaults.prototype.reset = function () {
  3990. function stripDiacritics (text) {
  3991. // Used 'uni range + named function' from http://jsperf.com/diacritics/18
  3992. function match(a) {
  3993. return DIACRITICS[a] || a;
  3994. }
  3995. return text.replace(/[^\u0000-\u007E]/g, match);
  3996. }
  3997. function matcher (params, data) {
  3998. // Always return the object if there is nothing to compare
  3999. if ($.trim(params.term) === '') {
  4000. return data;
  4001. }
  4002. // Do a recursive check for options with children
  4003. if (data.children && data.children.length > 0) {
  4004. // Clone the data object if there are children
  4005. // This is required as we modify the object to remove any non-matches
  4006. var match = $.extend(true, {}, data);
  4007. // Check each child of the option
  4008. for (var c = data.children.length - 1; c >= 0; c--) {
  4009. var child = data.children[c];
  4010. var matches = matcher(params, child);
  4011. // If there wasn't a match, remove the object in the array
  4012. if (matches == null) {
  4013. match.children.splice(c, 1);
  4014. }
  4015. }
  4016. // If any children matched, return the new object
  4017. if (match.children.length > 0) {
  4018. return match;
  4019. }
  4020. // If there were no matching children, check just the plain object
  4021. return matcher(params, match);
  4022. }
  4023. var original = stripDiacritics(data.text).toUpperCase();
  4024. var term = stripDiacritics(params.term).toUpperCase();
  4025. // Check if the text contains the term
  4026. if (original.indexOf(term) > -1) {
  4027. return data;
  4028. }
  4029. // If it doesn't contain the term, don't return anything
  4030. return null;
  4031. }
  4032. this.defaults = {
  4033. amdBase: './',
  4034. amdLanguageBase: './i18n/',
  4035. closeOnSelect: true,
  4036. debug: false,
  4037. dropdownAutoWidth: false,
  4038. escapeMarkup: Utils.escapeMarkup,
  4039. language: EnglishTranslation,
  4040. matcher: matcher,
  4041. minimumInputLength: 0,
  4042. maximumInputLength: 0,
  4043. maximumSelectionLength: 0,
  4044. minimumResultsForSearch: 0,
  4045. selectOnClose: false,
  4046. sorter: function (data) {
  4047. return data;
  4048. },
  4049. templateResult: function (result) {
  4050. return result.text;
  4051. },
  4052. templateSelection: function (selection) {
  4053. return selection.text;
  4054. },
  4055. theme: 'default',
  4056. width: 'resolve'
  4057. };
  4058. };
  4059. Defaults.prototype.set = function (key, value) {
  4060. var camelKey = $.camelCase(key);
  4061. var data = {};
  4062. data[camelKey] = value;
  4063. var convertedData = Utils._convertData(data);
  4064. $.extend(this.defaults, convertedData);
  4065. };
  4066. var defaults = new Defaults();
  4067. return defaults;
  4068. });
  4069. S2.define('select2/options',[
  4070. 'require',
  4071. 'jquery',
  4072. './defaults',
  4073. './utils'
  4074. ], function (require, $, Defaults, Utils) {
  4075. function Options (options, $element) {
  4076. this.options = options;
  4077. if ($element != null) {
  4078. this.fromElement($element);
  4079. }
  4080. this.options = Defaults.apply(this.options);
  4081. if ($element && $element.is('input')) {
  4082. var InputCompat = require(this.get('amdBase') + 'compat/inputData');
  4083. this.options.dataAdapter = Utils.Decorate(
  4084. this.options.dataAdapter,
  4085. InputCompat
  4086. );
  4087. }
  4088. }
  4089. Options.prototype.fromElement = function ($e) {
  4090. var excludedData = ['select2'];
  4091. if (this.options.multiple == null) {
  4092. this.options.multiple = $e.prop('multiple');
  4093. }
  4094. if (this.options.disabled == null) {
  4095. this.options.disabled = $e.prop('disabled');
  4096. }
  4097. if (this.options.language == null) {
  4098. if ($e.prop('lang')) {
  4099. this.options.language = $e.prop('lang').toLowerCase();
  4100. } else if ($e.closest('[lang]').prop('lang')) {
  4101. this.options.language = $e.closest('[lang]').prop('lang');
  4102. }
  4103. }
  4104. if (this.options.dir == null) {
  4105. if ($e.prop('dir')) {
  4106. this.options.dir = $e.prop('dir');
  4107. } else if ($e.closest('[dir]').prop('dir')) {
  4108. this.options.dir = $e.closest('[dir]').prop('dir');
  4109. } else {
  4110. this.options.dir = 'ltr';
  4111. }
  4112. }
  4113. $e.prop('disabled', this.options.disabled);
  4114. $e.prop('multiple', this.options.multiple);
  4115. if ($e.data('select2Tags')) {
  4116. if (this.options.debug && window.console && console.warn) {
  4117. console.warn(
  4118. 'Select2: The `data-select2-tags` attribute has been changed to ' +
  4119. 'use the `data-data` and `data-tags="true"` attributes and will be ' +
  4120. 'removed in future versions of Select2.'
  4121. );
  4122. }
  4123. $e.data('data', $e.data('select2Tags'));
  4124. $e.data('tags', true);
  4125. }
  4126. if ($e.data('ajaxUrl')) {
  4127. if (this.options.debug && window.console && console.warn) {
  4128. console.warn(
  4129. 'Select2: The `data-ajax-url` attribute has been changed to ' +
  4130. '`data-ajax--url` and support for the old attribute will be removed' +
  4131. ' in future versions of Select2.'
  4132. );
  4133. }
  4134. $e.attr('ajax--url', $e.data('ajaxUrl'));
  4135. $e.data('ajax--url', $e.data('ajaxUrl'));
  4136. }
  4137. var dataset = {};
  4138. // Prefer the element's `dataset` attribute if it exists
  4139. // jQuery 1.x does not correctly handle data attributes with multiple dashes
  4140. if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
  4141. dataset = $.extend(true, {}, $e[0].dataset, $e.data());
  4142. } else {
  4143. dataset = $e.data();
  4144. }
  4145. var data = $.extend(true, {}, dataset);
  4146. data = Utils._convertData(data);
  4147. for (var key in data) {
  4148. if ($.inArray(key, excludedData) > -1) {
  4149. continue;
  4150. }
  4151. if ($.isPlainObject(this.options[key])) {
  4152. $.extend(this.options[key], data[key]);
  4153. } else {
  4154. this.options[key] = data[key];
  4155. }
  4156. }
  4157. return this;
  4158. };
  4159. Options.prototype.get = function (key) {
  4160. return this.options[key];
  4161. };
  4162. Options.prototype.set = function (key, val) {
  4163. this.options[key] = val;
  4164. };
  4165. return Options;
  4166. });
  4167. S2.define('select2/core',[
  4168. 'jquery',
  4169. './options',
  4170. './utils',
  4171. './keys'
  4172. ], function ($, Options, Utils, KEYS) {
  4173. var Select2 = function ($element, options) {
  4174. if ($element.data('select2') != null) {
  4175. $element.data('select2').destroy();
  4176. }
  4177. this.$element = $element;
  4178. this.id = this._generateId($element);
  4179. options = options || {};
  4180. this.options = new Options(options, $element);
  4181. Select2.__super__.constructor.call(this);
  4182. // Set up the tabindex
  4183. var tabindex = $element.attr('tabindex') || 0;
  4184. $element.data('old-tabindex', tabindex);
  4185. $element.attr('tabindex', '-1');
  4186. // Set up containers and adapters
  4187. var DataAdapter = this.options.get('dataAdapter');
  4188. this.dataAdapter = new DataAdapter($element, this.options);
  4189. var $container = this.render();
  4190. this._placeContainer($container);
  4191. var SelectionAdapter = this.options.get('selectionAdapter');
  4192. this.selection = new SelectionAdapter($element, this.options);
  4193. this.$selection = this.selection.render();
  4194. this.selection.position(this.$selection, $container);
  4195. var DropdownAdapter = this.options.get('dropdownAdapter');
  4196. this.dropdown = new DropdownAdapter($element, this.options);
  4197. this.$dropdown = this.dropdown.render();
  4198. this.dropdown.position(this.$dropdown, $container);
  4199. var ResultsAdapter = this.options.get('resultsAdapter');
  4200. this.results = new ResultsAdapter($element, this.options, this.dataAdapter);
  4201. this.$results = this.results.render();
  4202. this.results.position(this.$results, this.$dropdown);
  4203. // Bind events
  4204. var self = this;
  4205. // Bind the container to all of the adapters
  4206. this._bindAdapters();
  4207. // Register any DOM event handlers
  4208. this._registerDomEvents();
  4209. // Register any internal event handlers
  4210. this._registerDataEvents();
  4211. this._registerSelectionEvents();
  4212. this._registerDropdownEvents();
  4213. this._registerResultsEvents();
  4214. this._registerEvents();
  4215. // Set the initial state
  4216. this.dataAdapter.current(function (initialData) {
  4217. self.trigger('selection:update', {
  4218. data: initialData
  4219. });
  4220. });
  4221. // Hide the original select
  4222. $element.addClass('select2-hidden-accessible');
  4223. $element.attr('aria-hidden', 'true');
  4224. // Synchronize any monitored attributes
  4225. this._syncAttributes();
  4226. $element.data('select2', this);
  4227. };
  4228. Utils.Extend(Select2, Utils.Observable);
  4229. Select2.prototype._generateId = function ($element) {
  4230. var id = '';
  4231. if ($element.attr('id') != null) {
  4232. id = $element.attr('id');
  4233. } else if ($element.attr('name') != null) {
  4234. id = $element.attr('name') + '-' + Utils.generateChars(2);
  4235. } else {
  4236. id = Utils.generateChars(4);
  4237. }
  4238. id = id.replace(/(:|\.|\[|\]|,)/g, '');
  4239. id = 'select2-' + id;
  4240. return id;
  4241. };
  4242. Select2.prototype._placeContainer = function ($container) {
  4243. $container.insertAfter(this.$element);
  4244. var width = this._resolveWidth(this.$element, this.options.get('width'));
  4245. if (width != null) {
  4246. $container.css('width', width);
  4247. }
  4248. };
  4249. Select2.prototype._resolveWidth = function ($element, method) {
  4250. var WIDTH = /^width:(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc))/i;
  4251. if (method == 'resolve') {
  4252. var styleWidth = this._resolveWidth($element, 'style');
  4253. if (styleWidth != null) {
  4254. return styleWidth;
  4255. }
  4256. return this._resolveWidth($element, 'element');
  4257. }
  4258. if (method == 'element') {
  4259. var elementWidth = $element.outerWidth(false);
  4260. if (elementWidth <= 0) {
  4261. return 'auto';
  4262. }
  4263. return elementWidth + 'px';
  4264. }
  4265. if (method == 'style') {
  4266. var style = $element.attr('style');
  4267. if (typeof(style) !== 'string') {
  4268. return null;
  4269. }
  4270. var attrs = style.split(';');
  4271. for (var i = 0, l = attrs.length; i < l; i = i + 1) {
  4272. var attr = attrs[i].replace(/\s/g, '');
  4273. var matches = attr.match(WIDTH);
  4274. if (matches !== null && matches.length >= 1) {
  4275. return matches[1];
  4276. }
  4277. }
  4278. return null;
  4279. }
  4280. return method;
  4281. };
  4282. Select2.prototype._bindAdapters = function () {
  4283. this.dataAdapter.bind(this, this.$container);
  4284. this.selection.bind(this, this.$container);
  4285. this.dropdown.bind(this, this.$container);
  4286. this.results.bind(this, this.$container);
  4287. };
  4288. Select2.prototype._registerDomEvents = function () {
  4289. var self = this;
  4290. this.$element.on('change.select2', function () {
  4291. self.dataAdapter.current(function (data) {
  4292. self.trigger('selection:update', {
  4293. data: data
  4294. });
  4295. });
  4296. });
  4297. this.$element.on('focus.select2', function (evt) {
  4298. self.trigger('focus', evt);
  4299. });
  4300. this._syncA = Utils.bind(this._syncAttributes, this);
  4301. this._syncS = Utils.bind(this._syncSubtree, this);
  4302. if (this.$element[0].attachEvent) {
  4303. this.$element[0].attachEvent('onpropertychange', this._syncA);
  4304. }
  4305. var observer = window.MutationObserver ||
  4306. window.WebKitMutationObserver ||
  4307. window.MozMutationObserver
  4308. ;
  4309. if (observer != null) {
  4310. this._observer = new observer(function (mutations) {
  4311. $.each(mutations, self._syncA);
  4312. $.each(mutations, self._syncS);
  4313. });
  4314. this._observer.observe(this.$element[0], {
  4315. attributes: true,
  4316. childList: true,
  4317. subtree: false
  4318. });
  4319. } else if (this.$element[0].addEventListener) {
  4320. this.$element[0].addEventListener(
  4321. 'DOMAttrModified',
  4322. self._syncA,
  4323. false
  4324. );
  4325. this.$element[0].addEventListener(
  4326. 'DOMNodeInserted',
  4327. self._syncS,
  4328. false
  4329. );
  4330. this.$element[0].addEventListener(
  4331. 'DOMNodeRemoved',
  4332. self._syncS,
  4333. false
  4334. );
  4335. }
  4336. };
  4337. Select2.prototype._registerDataEvents = function () {
  4338. var self = this;
  4339. this.dataAdapter.on('*', function (name, params) {
  4340. self.trigger(name, params);
  4341. });
  4342. };
  4343. Select2.prototype._registerSelectionEvents = function () {
  4344. var self = this;
  4345. var nonRelayEvents = ['toggle', 'focus'];
  4346. this.selection.on('toggle', function () {
  4347. self.toggleDropdown();
  4348. });
  4349. this.selection.on('focus', function (params) {
  4350. self.focus(params);
  4351. });
  4352. this.selection.on('*', function (name, params) {
  4353. if ($.inArray(name, nonRelayEvents) !== -1) {
  4354. return;
  4355. }
  4356. self.trigger(name, params);
  4357. });
  4358. };
  4359. Select2.prototype._registerDropdownEvents = function () {
  4360. var self = this;
  4361. this.dropdown.on('*', function (name, params) {
  4362. self.trigger(name, params);
  4363. });
  4364. };
  4365. Select2.prototype._registerResultsEvents = function () {
  4366. var self = this;
  4367. this.results.on('*', function (name, params) {
  4368. self.trigger(name, params);
  4369. });
  4370. };
  4371. Select2.prototype._registerEvents = function () {
  4372. var self = this;
  4373. this.on('open', function () {
  4374. self.$container.addClass('select2-container--open');
  4375. });
  4376. this.on('close', function () {
  4377. self.$container.removeClass('select2-container--open');
  4378. });
  4379. this.on('enable', function () {
  4380. self.$container.removeClass('select2-container--disabled');
  4381. });
  4382. this.on('disable', function () {
  4383. self.$container.addClass('select2-container--disabled');
  4384. });
  4385. this.on('blur', function () {
  4386. self.$container.removeClass('select2-container--focus');
  4387. });
  4388. this.on('query', function (params) {
  4389. if (!self.isOpen()) {
  4390. self.trigger('open', {});
  4391. }
  4392. this.dataAdapter.query(params, function (data) {
  4393. self.trigger('results:all', {
  4394. data: data,
  4395. query: params
  4396. });
  4397. });
  4398. });
  4399. this.on('query:append', function (params) {
  4400. this.dataAdapter.query(params, function (data) {
  4401. self.trigger('results:append', {
  4402. data: data,
  4403. query: params
  4404. });
  4405. });
  4406. });
  4407. this.on('open', function(){
  4408. // Focus on the active element when opening dropdown.
  4409. // Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
  4410. setTimeout(function(){
  4411. self.focusOnActiveElement();
  4412. }, 1);
  4413. });
  4414. $(document).on('keydown', function (evt) {
  4415. var key = evt.which;
  4416. if (self.isOpen()) {
  4417. if (key === KEYS.ESC || (key === KEYS.UP && evt.altKey)) {
  4418. self.close();
  4419. evt.preventDefault();
  4420. } else if (key === KEYS.ENTER || key === KEYS.TAB) {
  4421. self.trigger('results:select', {});
  4422. evt.preventDefault();
  4423. } else if ((key === KEYS.SPACE && evt.ctrlKey)) {
  4424. self.trigger('results:toggle', {});
  4425. evt.preventDefault();
  4426. } else if (key === KEYS.UP) {
  4427. self.trigger('results:previous', {});
  4428. evt.preventDefault();
  4429. } else if (key === KEYS.DOWN) {
  4430. self.trigger('results:next', {});
  4431. evt.preventDefault();
  4432. }
  4433. var $searchField = self.$dropdown.find('.select2-search__field');
  4434. if (! $searchField.length) {
  4435. $searchField = self.$container.find('.select2-search__field');
  4436. }
  4437. // Move the focus to the selected element on keyboard navigation.
  4438. // Required for screen readers to work properly.
  4439. if (key === KEYS.DOWN || key === KEYS.UP) {
  4440. self.focusOnActiveElement();
  4441. } else {
  4442. // Focus on the search if user starts typing.
  4443. $searchField.focus();
  4444. // Focus back to active selection when finished typing.
  4445. // Small delay so typed character can be read by screen reader.
  4446. setTimeout(function(){
  4447. self.focusOnActiveElement();
  4448. }, 1000);
  4449. }
  4450. } else if (self.hasFocus()) {
  4451. if (key === KEYS.ENTER || key === KEYS.SPACE ||
  4452. key === KEYS.DOWN) {
  4453. self.open();
  4454. evt.preventDefault();
  4455. }
  4456. }
  4457. });
  4458. };
  4459. Select2.prototype.focusOnActiveElement = function () {
  4460. // Don't mess with the focus on touchscreens because it causes havoc with on-screen keyboards.
  4461. if (this.isOpen() && ! Utils.isTouchscreen()) {
  4462. this.$results.find('li.select2-results__option--highlighted').focus();
  4463. }
  4464. };
  4465. Select2.prototype._syncAttributes = function () {
  4466. this.options.set('disabled', this.$element.prop('disabled'));
  4467. if (this.options.get('disabled')) {
  4468. if (this.isOpen()) {
  4469. this.close();
  4470. }
  4471. this.trigger('disable', {});
  4472. } else {
  4473. this.trigger('enable', {});
  4474. }
  4475. };
  4476. Select2.prototype._syncSubtree = function (evt, mutations) {
  4477. var changed = false;
  4478. var self = this;
  4479. // Ignore any mutation events raised for elements that aren't options or
  4480. // optgroups. This handles the case when the select element is destroyed
  4481. if (
  4482. evt && evt.target && (
  4483. evt.target.nodeName !== 'OPTION' && evt.target.nodeName !== 'OPTGROUP'
  4484. )
  4485. ) {
  4486. return;
  4487. }
  4488. if (!mutations) {
  4489. // If mutation events aren't supported, then we can only assume that the
  4490. // change affected the selections
  4491. changed = true;
  4492. } else if (mutations.addedNodes && mutations.addedNodes.length > 0) {
  4493. for (var n = 0; n < mutations.addedNodes.length; n++) {
  4494. var node = mutations.addedNodes[n];
  4495. if (node.selected) {
  4496. changed = true;
  4497. }
  4498. }
  4499. } else if (mutations.removedNodes && mutations.removedNodes.length > 0) {
  4500. changed = true;
  4501. }
  4502. // Only re-pull the data if we think there is a change
  4503. if (changed) {
  4504. this.dataAdapter.current(function (currentData) {
  4505. self.trigger('selection:update', {
  4506. data: currentData
  4507. });
  4508. });
  4509. }
  4510. };
  4511. /**
  4512. * Override the trigger method to automatically trigger pre-events when
  4513. * there are events that can be prevented.
  4514. */
  4515. Select2.prototype.trigger = function (name, args) {
  4516. var actualTrigger = Select2.__super__.trigger;
  4517. var preTriggerMap = {
  4518. 'open': 'opening',
  4519. 'close': 'closing',
  4520. 'select': 'selecting',
  4521. 'unselect': 'unselecting'
  4522. };
  4523. if (args === undefined) {
  4524. args = {};
  4525. }
  4526. if (name in preTriggerMap) {
  4527. var preTriggerName = preTriggerMap[name];
  4528. var preTriggerArgs = {
  4529. prevented: false,
  4530. name: name,
  4531. args: args
  4532. };
  4533. actualTrigger.call(this, preTriggerName, preTriggerArgs);
  4534. if (preTriggerArgs.prevented) {
  4535. args.prevented = true;
  4536. return;
  4537. }
  4538. }
  4539. actualTrigger.call(this, name, args);
  4540. };
  4541. Select2.prototype.toggleDropdown = function () {
  4542. if (this.options.get('disabled')) {
  4543. return;
  4544. }
  4545. if (this.isOpen()) {
  4546. this.close();
  4547. } else {
  4548. this.open();
  4549. }
  4550. };
  4551. Select2.prototype.open = function () {
  4552. if (this.isOpen()) {
  4553. return;
  4554. }
  4555. this.trigger('query', {});
  4556. };
  4557. Select2.prototype.close = function () {
  4558. if (!this.isOpen()) {
  4559. return;
  4560. }
  4561. this.trigger('close', {});
  4562. };
  4563. Select2.prototype.isOpen = function () {
  4564. return this.$container.hasClass('select2-container--open');
  4565. };
  4566. Select2.prototype.hasFocus = function () {
  4567. return this.$container.hasClass('select2-container--focus');
  4568. };
  4569. Select2.prototype.focus = function (data) {
  4570. // No need to re-trigger focus events if we are already focused
  4571. if (this.hasFocus()) {
  4572. return;
  4573. }
  4574. this.$container.addClass('select2-container--focus');
  4575. this.trigger('focus', {});
  4576. };
  4577. Select2.prototype.enable = function (args) {
  4578. if (this.options.get('debug') && window.console && console.warn) {
  4579. console.warn(
  4580. 'Select2: The `select2("enable")` method has been deprecated and will' +
  4581. ' be removed in later Select2 versions. Use $element.prop("disabled")' +
  4582. ' instead.'
  4583. );
  4584. }
  4585. if (args == null || args.length === 0) {
  4586. args = [true];
  4587. }
  4588. var disabled = !args[0];
  4589. this.$element.prop('disabled', disabled);
  4590. };
  4591. Select2.prototype.data = function () {
  4592. if (this.options.get('debug') &&
  4593. arguments.length > 0 && window.console && console.warn) {
  4594. console.warn(
  4595. 'Select2: Data can no longer be set using `select2("data")`. You ' +
  4596. 'should consider setting the value instead using `$element.val()`.'
  4597. );
  4598. }
  4599. var data = [];
  4600. this.dataAdapter.current(function (currentData) {
  4601. data = currentData;
  4602. });
  4603. return data;
  4604. };
  4605. Select2.prototype.val = function (args) {
  4606. if (this.options.get('debug') && window.console && console.warn) {
  4607. console.warn(
  4608. 'Select2: The `select2("val")` method has been deprecated and will be' +
  4609. ' removed in later Select2 versions. Use $element.val() instead.'
  4610. );
  4611. }
  4612. if (args == null || args.length === 0) {
  4613. return this.$element.val();
  4614. }
  4615. var newVal = args[0];
  4616. if ($.isArray(newVal)) {
  4617. newVal = $.map(newVal, function (obj) {
  4618. return obj.toString();
  4619. });
  4620. }
  4621. this.$element.val(newVal).trigger('change');
  4622. };
  4623. Select2.prototype.destroy = function () {
  4624. this.$container.remove();
  4625. if (this.$element[0].detachEvent) {
  4626. this.$element[0].detachEvent('onpropertychange', this._syncA);
  4627. }
  4628. if (this._observer != null) {
  4629. this._observer.disconnect();
  4630. this._observer = null;
  4631. } else if (this.$element[0].removeEventListener) {
  4632. this.$element[0]
  4633. .removeEventListener('DOMAttrModified', this._syncA, false);
  4634. this.$element[0]
  4635. .removeEventListener('DOMNodeInserted', this._syncS, false);
  4636. this.$element[0]
  4637. .removeEventListener('DOMNodeRemoved', this._syncS, false);
  4638. }
  4639. this._syncA = null;
  4640. this._syncS = null;
  4641. this.$element.off('.select2');
  4642. this.$element.attr('tabindex', this.$element.data('old-tabindex'));
  4643. this.$element.removeClass('select2-hidden-accessible');
  4644. this.$element.attr('aria-hidden', 'false');
  4645. this.$element.removeData('select2');
  4646. this.dataAdapter.destroy();
  4647. this.selection.destroy();
  4648. this.dropdown.destroy();
  4649. this.results.destroy();
  4650. this.dataAdapter = null;
  4651. this.selection = null;
  4652. this.dropdown = null;
  4653. this.results = null;
  4654. };
  4655. Select2.prototype.render = function () {
  4656. var $container = $(
  4657. '<span class="select2 select2-container">' +
  4658. '<span class="selection"></span>' +
  4659. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  4660. '</span>'
  4661. );
  4662. $container.attr('dir', this.options.get('dir'));
  4663. this.$container = $container;
  4664. this.$container.addClass('select2-container--' + this.options.get('theme'));
  4665. $container.data('element', this.$element);
  4666. return $container;
  4667. };
  4668. return Select2;
  4669. });
  4670. S2.define('jquery-mousewheel',[
  4671. 'jquery'
  4672. ], function ($) {
  4673. // Used to shim jQuery.mousewheel for non-full builds.
  4674. return $;
  4675. });
  4676. S2.define('jquery.select2',[
  4677. 'jquery',
  4678. 'jquery-mousewheel',
  4679. './select2/core',
  4680. './select2/defaults'
  4681. ], function ($, _, Select2, Defaults) {
  4682. if ($.fn.selectWoo == null) {
  4683. // All methods that should return the element
  4684. var thisMethods = ['open', 'close', 'destroy'];
  4685. $.fn.selectWoo = function (options) {
  4686. options = options || {};
  4687. if (typeof options === 'object') {
  4688. this.each(function () {
  4689. var instanceOptions = $.extend(true, {}, options);
  4690. var instance = new Select2($(this), instanceOptions);
  4691. });
  4692. return this;
  4693. } else if (typeof options === 'string') {
  4694. var ret;
  4695. var args = Array.prototype.slice.call(arguments, 1);
  4696. this.each(function () {
  4697. var instance = $(this).data('select2');
  4698. if (instance == null && window.console && console.error) {
  4699. console.error(
  4700. 'The select2(\'' + options + '\') method was called on an ' +
  4701. 'element that is not using Select2.'
  4702. );
  4703. }
  4704. ret = instance[options].apply(instance, args);
  4705. });
  4706. // Check if we should be returning `this`
  4707. if ($.inArray(options, thisMethods) > -1) {
  4708. return this;
  4709. }
  4710. return ret;
  4711. } else {
  4712. throw new Error('Invalid arguments for Select2: ' + options);
  4713. }
  4714. };
  4715. }
  4716. if ($.fn.select2 != null && $.fn.select2.defaults != null) {
  4717. $.fn.selectWoo.defaults = $.fn.select2.defaults;
  4718. }
  4719. if ($.fn.selectWoo.defaults == null) {
  4720. $.fn.selectWoo.defaults = Defaults;
  4721. }
  4722. // Also register selectWoo under select2 if select2 is not already present.
  4723. $.fn.select2 = $.fn.select2 || $.fn.selectWoo;
  4724. return Select2;
  4725. });
  4726. // Return the AMD loader configuration so it can be used outside of this file
  4727. return {
  4728. define: S2.define,
  4729. require: S2.require
  4730. };
  4731. }());
  4732. // Autoload the jQuery bindings
  4733. // We know that all of the modules exist above this, so we're safe
  4734. var select2 = S2.require('jquery.select2');
  4735. // Hold the AMD module references on the jQuery function that was just loaded
  4736. // This allows Select2 to use the internal loader outside of this file, such
  4737. // as in the language files.
  4738. jQuery.fn.select2.amd = S2;
  4739. jQuery.fn.selectWoo.amd = S2;
  4740. // Return the Select2 instance for anyone who is importing it.
  4741. return select2;
  4742. }));