Nessuna descrizione

select2.js 129KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419
  1. /*
  2. Copyright 2012 Igor Vaynberg
  3. Version: 3.5.2 Timestamp: Sat Nov 1 14:43:36 EDT 2014
  4. This software is licensed under the Apache License, Version 2.0 (the "Apache License") or the GNU
  5. General Public License version 2 (the "GPL License"). You may choose either license to govern your
  6. use of this software only upon the condition that you accept all of the terms of either the Apache
  7. License or the GPL License.
  8. You may obtain a copy of the Apache License and the GPL License at:
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. http://www.gnu.org/licenses/gpl-2.0.html
  11. Unless required by applicable law or agreed to in writing, software distributed under the
  12. Apache License or the GPL License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. CONDITIONS OF ANY KIND, either express or implied. See the Apache License and the GPL License for
  14. the specific language governing permissions and limitations under the Apache License and the GPL License.
  15. */
  16. ( function ( $ ) {
  17. if ( typeof $.fn.each2 == 'undefined' ) {
  18. $.extend( $.fn, {
  19. /*
  20. * 4-10 times faster .each replacement
  21. * use it carefully, as it overrides jQuery context of element on each iteration
  22. */
  23. each2: function ( c ) {
  24. var j = $( [ 0 ] ),
  25. i = -1,
  26. l = this.length;
  27. while (
  28. ++i < l &&
  29. ( j.context = j[ 0 ] = this[ i ] ) &&
  30. c.call( j[ 0 ], i, j ) !== false //"this"=DOM, i=index, j=jQuery object
  31. );
  32. return this;
  33. },
  34. } );
  35. }
  36. } )( jQuery );
  37. ( function ( $, undefined ) {
  38. 'use strict';
  39. /*global document, window, jQuery, console */
  40. if ( window.Select2 !== undefined ) {
  41. return;
  42. }
  43. var AbstractSelect2,
  44. SingleSelect2,
  45. MultiSelect2,
  46. nextUid,
  47. sizer,
  48. lastMousePosition = { x: 0, y: 0 },
  49. $document,
  50. scrollBarDimensions,
  51. KEY = {
  52. TAB: 9,
  53. ENTER: 13,
  54. ESC: 27,
  55. SPACE: 32,
  56. LEFT: 37,
  57. UP: 38,
  58. RIGHT: 39,
  59. DOWN: 40,
  60. SHIFT: 16,
  61. CTRL: 17,
  62. ALT: 18,
  63. PAGE_UP: 33,
  64. PAGE_DOWN: 34,
  65. HOME: 36,
  66. END: 35,
  67. BACKSPACE: 8,
  68. DELETE: 46,
  69. isArrow: function ( k ) {
  70. k = k.which ? k.which : k;
  71. switch ( k ) {
  72. case KEY.LEFT:
  73. case KEY.RIGHT:
  74. case KEY.UP:
  75. case KEY.DOWN:
  76. return true;
  77. }
  78. return false;
  79. },
  80. isControl: function ( e ) {
  81. var k = e.which;
  82. switch ( k ) {
  83. case KEY.SHIFT:
  84. case KEY.CTRL:
  85. case KEY.ALT:
  86. return true;
  87. }
  88. if ( e.metaKey ) return true;
  89. return false;
  90. },
  91. isFunctionKey: function ( k ) {
  92. k = k.which ? k.which : k;
  93. return k >= 112 && k <= 123;
  94. },
  95. },
  96. MEASURE_SCROLLBAR_TEMPLATE =
  97. "<div class='select2-measure-scrollbar'></div>",
  98. DIACRITICS = {
  99. '\u24B6': 'A',
  100. '\uFF21': 'A',
  101. '\u00C0': 'A',
  102. '\u00C1': 'A',
  103. '\u00C2': 'A',
  104. '\u1EA6': 'A',
  105. '\u1EA4': 'A',
  106. '\u1EAA': 'A',
  107. '\u1EA8': 'A',
  108. '\u00C3': 'A',
  109. '\u0100': 'A',
  110. '\u0102': 'A',
  111. '\u1EB0': 'A',
  112. '\u1EAE': 'A',
  113. '\u1EB4': 'A',
  114. '\u1EB2': 'A',
  115. '\u0226': 'A',
  116. '\u01E0': 'A',
  117. '\u00C4': 'A',
  118. '\u01DE': 'A',
  119. '\u1EA2': 'A',
  120. '\u00C5': 'A',
  121. '\u01FA': 'A',
  122. '\u01CD': 'A',
  123. '\u0200': 'A',
  124. '\u0202': 'A',
  125. '\u1EA0': 'A',
  126. '\u1EAC': 'A',
  127. '\u1EB6': 'A',
  128. '\u1E00': 'A',
  129. '\u0104': 'A',
  130. '\u023A': 'A',
  131. '\u2C6F': 'A',
  132. '\uA732': 'AA',
  133. '\u00C6': 'AE',
  134. '\u01FC': 'AE',
  135. '\u01E2': 'AE',
  136. '\uA734': 'AO',
  137. '\uA736': 'AU',
  138. '\uA738': 'AV',
  139. '\uA73A': 'AV',
  140. '\uA73C': 'AY',
  141. '\u24B7': 'B',
  142. '\uFF22': 'B',
  143. '\u1E02': 'B',
  144. '\u1E04': 'B',
  145. '\u1E06': 'B',
  146. '\u0243': 'B',
  147. '\u0182': 'B',
  148. '\u0181': 'B',
  149. '\u24B8': 'C',
  150. '\uFF23': 'C',
  151. '\u0106': 'C',
  152. '\u0108': 'C',
  153. '\u010A': 'C',
  154. '\u010C': 'C',
  155. '\u00C7': 'C',
  156. '\u1E08': 'C',
  157. '\u0187': 'C',
  158. '\u023B': 'C',
  159. '\uA73E': 'C',
  160. '\u24B9': 'D',
  161. '\uFF24': 'D',
  162. '\u1E0A': 'D',
  163. '\u010E': 'D',
  164. '\u1E0C': 'D',
  165. '\u1E10': 'D',
  166. '\u1E12': 'D',
  167. '\u1E0E': 'D',
  168. '\u0110': 'D',
  169. '\u018B': 'D',
  170. '\u018A': 'D',
  171. '\u0189': 'D',
  172. '\uA779': 'D',
  173. '\u01F1': 'DZ',
  174. '\u01C4': 'DZ',
  175. '\u01F2': 'Dz',
  176. '\u01C5': 'Dz',
  177. '\u24BA': 'E',
  178. '\uFF25': 'E',
  179. '\u00C8': 'E',
  180. '\u00C9': 'E',
  181. '\u00CA': 'E',
  182. '\u1EC0': 'E',
  183. '\u1EBE': 'E',
  184. '\u1EC4': 'E',
  185. '\u1EC2': 'E',
  186. '\u1EBC': 'E',
  187. '\u0112': 'E',
  188. '\u1E14': 'E',
  189. '\u1E16': 'E',
  190. '\u0114': 'E',
  191. '\u0116': 'E',
  192. '\u00CB': 'E',
  193. '\u1EBA': 'E',
  194. '\u011A': 'E',
  195. '\u0204': 'E',
  196. '\u0206': 'E',
  197. '\u1EB8': 'E',
  198. '\u1EC6': 'E',
  199. '\u0228': 'E',
  200. '\u1E1C': 'E',
  201. '\u0118': 'E',
  202. '\u1E18': 'E',
  203. '\u1E1A': 'E',
  204. '\u0190': 'E',
  205. '\u018E': 'E',
  206. '\u24BB': 'F',
  207. '\uFF26': 'F',
  208. '\u1E1E': 'F',
  209. '\u0191': 'F',
  210. '\uA77B': 'F',
  211. '\u24BC': 'G',
  212. '\uFF27': 'G',
  213. '\u01F4': 'G',
  214. '\u011C': 'G',
  215. '\u1E20': 'G',
  216. '\u011E': 'G',
  217. '\u0120': 'G',
  218. '\u01E6': 'G',
  219. '\u0122': 'G',
  220. '\u01E4': 'G',
  221. '\u0193': 'G',
  222. '\uA7A0': 'G',
  223. '\uA77D': 'G',
  224. '\uA77E': 'G',
  225. '\u24BD': 'H',
  226. '\uFF28': 'H',
  227. '\u0124': 'H',
  228. '\u1E22': 'H',
  229. '\u1E26': 'H',
  230. '\u021E': 'H',
  231. '\u1E24': 'H',
  232. '\u1E28': 'H',
  233. '\u1E2A': 'H',
  234. '\u0126': 'H',
  235. '\u2C67': 'H',
  236. '\u2C75': 'H',
  237. '\uA78D': 'H',
  238. '\u24BE': 'I',
  239. '\uFF29': 'I',
  240. '\u00CC': 'I',
  241. '\u00CD': 'I',
  242. '\u00CE': 'I',
  243. '\u0128': 'I',
  244. '\u012A': 'I',
  245. '\u012C': 'I',
  246. '\u0130': 'I',
  247. '\u00CF': 'I',
  248. '\u1E2E': 'I',
  249. '\u1EC8': 'I',
  250. '\u01CF': 'I',
  251. '\u0208': 'I',
  252. '\u020A': 'I',
  253. '\u1ECA': 'I',
  254. '\u012E': 'I',
  255. '\u1E2C': 'I',
  256. '\u0197': 'I',
  257. '\u24BF': 'J',
  258. '\uFF2A': 'J',
  259. '\u0134': 'J',
  260. '\u0248': 'J',
  261. '\u24C0': 'K',
  262. '\uFF2B': 'K',
  263. '\u1E30': 'K',
  264. '\u01E8': 'K',
  265. '\u1E32': 'K',
  266. '\u0136': 'K',
  267. '\u1E34': 'K',
  268. '\u0198': 'K',
  269. '\u2C69': 'K',
  270. '\uA740': 'K',
  271. '\uA742': 'K',
  272. '\uA744': 'K',
  273. '\uA7A2': 'K',
  274. '\u24C1': 'L',
  275. '\uFF2C': 'L',
  276. '\u013F': 'L',
  277. '\u0139': 'L',
  278. '\u013D': 'L',
  279. '\u1E36': 'L',
  280. '\u1E38': 'L',
  281. '\u013B': 'L',
  282. '\u1E3C': 'L',
  283. '\u1E3A': 'L',
  284. '\u0141': 'L',
  285. '\u023D': 'L',
  286. '\u2C62': 'L',
  287. '\u2C60': 'L',
  288. '\uA748': 'L',
  289. '\uA746': 'L',
  290. '\uA780': 'L',
  291. '\u01C7': 'LJ',
  292. '\u01C8': 'Lj',
  293. '\u24C2': 'M',
  294. '\uFF2D': 'M',
  295. '\u1E3E': 'M',
  296. '\u1E40': 'M',
  297. '\u1E42': 'M',
  298. '\u2C6E': 'M',
  299. '\u019C': 'M',
  300. '\u24C3': 'N',
  301. '\uFF2E': 'N',
  302. '\u01F8': 'N',
  303. '\u0143': 'N',
  304. '\u00D1': 'N',
  305. '\u1E44': 'N',
  306. '\u0147': 'N',
  307. '\u1E46': 'N',
  308. '\u0145': 'N',
  309. '\u1E4A': 'N',
  310. '\u1E48': 'N',
  311. '\u0220': 'N',
  312. '\u019D': 'N',
  313. '\uA790': 'N',
  314. '\uA7A4': 'N',
  315. '\u01CA': 'NJ',
  316. '\u01CB': 'Nj',
  317. '\u24C4': 'O',
  318. '\uFF2F': 'O',
  319. '\u00D2': 'O',
  320. '\u00D3': 'O',
  321. '\u00D4': 'O',
  322. '\u1ED2': 'O',
  323. '\u1ED0': 'O',
  324. '\u1ED6': 'O',
  325. '\u1ED4': 'O',
  326. '\u00D5': 'O',
  327. '\u1E4C': 'O',
  328. '\u022C': 'O',
  329. '\u1E4E': 'O',
  330. '\u014C': 'O',
  331. '\u1E50': 'O',
  332. '\u1E52': 'O',
  333. '\u014E': 'O',
  334. '\u022E': 'O',
  335. '\u0230': 'O',
  336. '\u00D6': 'O',
  337. '\u022A': 'O',
  338. '\u1ECE': 'O',
  339. '\u0150': 'O',
  340. '\u01D1': 'O',
  341. '\u020C': 'O',
  342. '\u020E': 'O',
  343. '\u01A0': 'O',
  344. '\u1EDC': 'O',
  345. '\u1EDA': 'O',
  346. '\u1EE0': 'O',
  347. '\u1EDE': 'O',
  348. '\u1EE2': 'O',
  349. '\u1ECC': 'O',
  350. '\u1ED8': 'O',
  351. '\u01EA': 'O',
  352. '\u01EC': 'O',
  353. '\u00D8': 'O',
  354. '\u01FE': 'O',
  355. '\u0186': 'O',
  356. '\u019F': 'O',
  357. '\uA74A': 'O',
  358. '\uA74C': 'O',
  359. '\u01A2': 'OI',
  360. '\uA74E': 'OO',
  361. '\u0222': 'OU',
  362. '\u24C5': 'P',
  363. '\uFF30': 'P',
  364. '\u1E54': 'P',
  365. '\u1E56': 'P',
  366. '\u01A4': 'P',
  367. '\u2C63': 'P',
  368. '\uA750': 'P',
  369. '\uA752': 'P',
  370. '\uA754': 'P',
  371. '\u24C6': 'Q',
  372. '\uFF31': 'Q',
  373. '\uA756': 'Q',
  374. '\uA758': 'Q',
  375. '\u024A': 'Q',
  376. '\u24C7': 'R',
  377. '\uFF32': 'R',
  378. '\u0154': 'R',
  379. '\u1E58': 'R',
  380. '\u0158': 'R',
  381. '\u0210': 'R',
  382. '\u0212': 'R',
  383. '\u1E5A': 'R',
  384. '\u1E5C': 'R',
  385. '\u0156': 'R',
  386. '\u1E5E': 'R',
  387. '\u024C': 'R',
  388. '\u2C64': 'R',
  389. '\uA75A': 'R',
  390. '\uA7A6': 'R',
  391. '\uA782': 'R',
  392. '\u24C8': 'S',
  393. '\uFF33': 'S',
  394. '\u1E9E': 'S',
  395. '\u015A': 'S',
  396. '\u1E64': 'S',
  397. '\u015C': 'S',
  398. '\u1E60': 'S',
  399. '\u0160': 'S',
  400. '\u1E66': 'S',
  401. '\u1E62': 'S',
  402. '\u1E68': 'S',
  403. '\u0218': 'S',
  404. '\u015E': 'S',
  405. '\u2C7E': 'S',
  406. '\uA7A8': 'S',
  407. '\uA784': 'S',
  408. '\u24C9': 'T',
  409. '\uFF34': 'T',
  410. '\u1E6A': 'T',
  411. '\u0164': 'T',
  412. '\u1E6C': 'T',
  413. '\u021A': 'T',
  414. '\u0162': 'T',
  415. '\u1E70': 'T',
  416. '\u1E6E': 'T',
  417. '\u0166': 'T',
  418. '\u01AC': 'T',
  419. '\u01AE': 'T',
  420. '\u023E': 'T',
  421. '\uA786': 'T',
  422. '\uA728': 'TZ',
  423. '\u24CA': 'U',
  424. '\uFF35': 'U',
  425. '\u00D9': 'U',
  426. '\u00DA': 'U',
  427. '\u00DB': 'U',
  428. '\u0168': 'U',
  429. '\u1E78': 'U',
  430. '\u016A': 'U',
  431. '\u1E7A': 'U',
  432. '\u016C': 'U',
  433. '\u00DC': 'U',
  434. '\u01DB': 'U',
  435. '\u01D7': 'U',
  436. '\u01D5': 'U',
  437. '\u01D9': 'U',
  438. '\u1EE6': 'U',
  439. '\u016E': 'U',
  440. '\u0170': 'U',
  441. '\u01D3': 'U',
  442. '\u0214': 'U',
  443. '\u0216': 'U',
  444. '\u01AF': 'U',
  445. '\u1EEA': 'U',
  446. '\u1EE8': 'U',
  447. '\u1EEE': 'U',
  448. '\u1EEC': 'U',
  449. '\u1EF0': 'U',
  450. '\u1EE4': 'U',
  451. '\u1E72': 'U',
  452. '\u0172': 'U',
  453. '\u1E76': 'U',
  454. '\u1E74': 'U',
  455. '\u0244': 'U',
  456. '\u24CB': 'V',
  457. '\uFF36': 'V',
  458. '\u1E7C': 'V',
  459. '\u1E7E': 'V',
  460. '\u01B2': 'V',
  461. '\uA75E': 'V',
  462. '\u0245': 'V',
  463. '\uA760': 'VY',
  464. '\u24CC': 'W',
  465. '\uFF37': 'W',
  466. '\u1E80': 'W',
  467. '\u1E82': 'W',
  468. '\u0174': 'W',
  469. '\u1E86': 'W',
  470. '\u1E84': 'W',
  471. '\u1E88': 'W',
  472. '\u2C72': 'W',
  473. '\u24CD': 'X',
  474. '\uFF38': 'X',
  475. '\u1E8A': 'X',
  476. '\u1E8C': 'X',
  477. '\u24CE': 'Y',
  478. '\uFF39': 'Y',
  479. '\u1EF2': 'Y',
  480. '\u00DD': 'Y',
  481. '\u0176': 'Y',
  482. '\u1EF8': 'Y',
  483. '\u0232': 'Y',
  484. '\u1E8E': 'Y',
  485. '\u0178': 'Y',
  486. '\u1EF6': 'Y',
  487. '\u1EF4': 'Y',
  488. '\u01B3': 'Y',
  489. '\u024E': 'Y',
  490. '\u1EFE': 'Y',
  491. '\u24CF': 'Z',
  492. '\uFF3A': 'Z',
  493. '\u0179': 'Z',
  494. '\u1E90': 'Z',
  495. '\u017B': 'Z',
  496. '\u017D': 'Z',
  497. '\u1E92': 'Z',
  498. '\u1E94': 'Z',
  499. '\u01B5': 'Z',
  500. '\u0224': 'Z',
  501. '\u2C7F': 'Z',
  502. '\u2C6B': 'Z',
  503. '\uA762': 'Z',
  504. '\u24D0': 'a',
  505. '\uFF41': 'a',
  506. '\u1E9A': 'a',
  507. '\u00E0': 'a',
  508. '\u00E1': 'a',
  509. '\u00E2': 'a',
  510. '\u1EA7': 'a',
  511. '\u1EA5': 'a',
  512. '\u1EAB': 'a',
  513. '\u1EA9': 'a',
  514. '\u00E3': 'a',
  515. '\u0101': 'a',
  516. '\u0103': 'a',
  517. '\u1EB1': 'a',
  518. '\u1EAF': 'a',
  519. '\u1EB5': 'a',
  520. '\u1EB3': 'a',
  521. '\u0227': 'a',
  522. '\u01E1': 'a',
  523. '\u00E4': 'a',
  524. '\u01DF': 'a',
  525. '\u1EA3': 'a',
  526. '\u00E5': 'a',
  527. '\u01FB': 'a',
  528. '\u01CE': 'a',
  529. '\u0201': 'a',
  530. '\u0203': 'a',
  531. '\u1EA1': 'a',
  532. '\u1EAD': 'a',
  533. '\u1EB7': 'a',
  534. '\u1E01': 'a',
  535. '\u0105': 'a',
  536. '\u2C65': 'a',
  537. '\u0250': 'a',
  538. '\uA733': 'aa',
  539. '\u00E6': 'ae',
  540. '\u01FD': 'ae',
  541. '\u01E3': 'ae',
  542. '\uA735': 'ao',
  543. '\uA737': 'au',
  544. '\uA739': 'av',
  545. '\uA73B': 'av',
  546. '\uA73D': 'ay',
  547. '\u24D1': 'b',
  548. '\uFF42': 'b',
  549. '\u1E03': 'b',
  550. '\u1E05': 'b',
  551. '\u1E07': 'b',
  552. '\u0180': 'b',
  553. '\u0183': 'b',
  554. '\u0253': 'b',
  555. '\u24D2': 'c',
  556. '\uFF43': 'c',
  557. '\u0107': 'c',
  558. '\u0109': 'c',
  559. '\u010B': 'c',
  560. '\u010D': 'c',
  561. '\u00E7': 'c',
  562. '\u1E09': 'c',
  563. '\u0188': 'c',
  564. '\u023C': 'c',
  565. '\uA73F': 'c',
  566. '\u2184': 'c',
  567. '\u24D3': 'd',
  568. '\uFF44': 'd',
  569. '\u1E0B': 'd',
  570. '\u010F': 'd',
  571. '\u1E0D': 'd',
  572. '\u1E11': 'd',
  573. '\u1E13': 'd',
  574. '\u1E0F': 'd',
  575. '\u0111': 'd',
  576. '\u018C': 'd',
  577. '\u0256': 'd',
  578. '\u0257': 'd',
  579. '\uA77A': 'd',
  580. '\u01F3': 'dz',
  581. '\u01C6': 'dz',
  582. '\u24D4': 'e',
  583. '\uFF45': 'e',
  584. '\u00E8': 'e',
  585. '\u00E9': 'e',
  586. '\u00EA': 'e',
  587. '\u1EC1': 'e',
  588. '\u1EBF': 'e',
  589. '\u1EC5': 'e',
  590. '\u1EC3': 'e',
  591. '\u1EBD': 'e',
  592. '\u0113': 'e',
  593. '\u1E15': 'e',
  594. '\u1E17': 'e',
  595. '\u0115': 'e',
  596. '\u0117': 'e',
  597. '\u00EB': 'e',
  598. '\u1EBB': 'e',
  599. '\u011B': 'e',
  600. '\u0205': 'e',
  601. '\u0207': 'e',
  602. '\u1EB9': 'e',
  603. '\u1EC7': 'e',
  604. '\u0229': 'e',
  605. '\u1E1D': 'e',
  606. '\u0119': 'e',
  607. '\u1E19': 'e',
  608. '\u1E1B': 'e',
  609. '\u0247': 'e',
  610. '\u025B': 'e',
  611. '\u01DD': 'e',
  612. '\u24D5': 'f',
  613. '\uFF46': 'f',
  614. '\u1E1F': 'f',
  615. '\u0192': 'f',
  616. '\uA77C': 'f',
  617. '\u24D6': 'g',
  618. '\uFF47': 'g',
  619. '\u01F5': 'g',
  620. '\u011D': 'g',
  621. '\u1E21': 'g',
  622. '\u011F': 'g',
  623. '\u0121': 'g',
  624. '\u01E7': 'g',
  625. '\u0123': 'g',
  626. '\u01E5': 'g',
  627. '\u0260': 'g',
  628. '\uA7A1': 'g',
  629. '\u1D79': 'g',
  630. '\uA77F': 'g',
  631. '\u24D7': 'h',
  632. '\uFF48': 'h',
  633. '\u0125': 'h',
  634. '\u1E23': 'h',
  635. '\u1E27': 'h',
  636. '\u021F': 'h',
  637. '\u1E25': 'h',
  638. '\u1E29': 'h',
  639. '\u1E2B': 'h',
  640. '\u1E96': 'h',
  641. '\u0127': 'h',
  642. '\u2C68': 'h',
  643. '\u2C76': 'h',
  644. '\u0265': 'h',
  645. '\u0195': 'hv',
  646. '\u24D8': 'i',
  647. '\uFF49': 'i',
  648. '\u00EC': 'i',
  649. '\u00ED': 'i',
  650. '\u00EE': 'i',
  651. '\u0129': 'i',
  652. '\u012B': 'i',
  653. '\u012D': 'i',
  654. '\u00EF': 'i',
  655. '\u1E2F': 'i',
  656. '\u1EC9': 'i',
  657. '\u01D0': 'i',
  658. '\u0209': 'i',
  659. '\u020B': 'i',
  660. '\u1ECB': 'i',
  661. '\u012F': 'i',
  662. '\u1E2D': 'i',
  663. '\u0268': 'i',
  664. '\u0131': 'i',
  665. '\u24D9': 'j',
  666. '\uFF4A': 'j',
  667. '\u0135': 'j',
  668. '\u01F0': 'j',
  669. '\u0249': 'j',
  670. '\u24DA': 'k',
  671. '\uFF4B': 'k',
  672. '\u1E31': 'k',
  673. '\u01E9': 'k',
  674. '\u1E33': 'k',
  675. '\u0137': 'k',
  676. '\u1E35': 'k',
  677. '\u0199': 'k',
  678. '\u2C6A': 'k',
  679. '\uA741': 'k',
  680. '\uA743': 'k',
  681. '\uA745': 'k',
  682. '\uA7A3': 'k',
  683. '\u24DB': 'l',
  684. '\uFF4C': 'l',
  685. '\u0140': 'l',
  686. '\u013A': 'l',
  687. '\u013E': 'l',
  688. '\u1E37': 'l',
  689. '\u1E39': 'l',
  690. '\u013C': 'l',
  691. '\u1E3D': 'l',
  692. '\u1E3B': 'l',
  693. '\u017F': 'l',
  694. '\u0142': 'l',
  695. '\u019A': 'l',
  696. '\u026B': 'l',
  697. '\u2C61': 'l',
  698. '\uA749': 'l',
  699. '\uA781': 'l',
  700. '\uA747': 'l',
  701. '\u01C9': 'lj',
  702. '\u24DC': 'm',
  703. '\uFF4D': 'm',
  704. '\u1E3F': 'm',
  705. '\u1E41': 'm',
  706. '\u1E43': 'm',
  707. '\u0271': 'm',
  708. '\u026F': 'm',
  709. '\u24DD': 'n',
  710. '\uFF4E': 'n',
  711. '\u01F9': 'n',
  712. '\u0144': 'n',
  713. '\u00F1': 'n',
  714. '\u1E45': 'n',
  715. '\u0148': 'n',
  716. '\u1E47': 'n',
  717. '\u0146': 'n',
  718. '\u1E4B': 'n',
  719. '\u1E49': 'n',
  720. '\u019E': 'n',
  721. '\u0272': 'n',
  722. '\u0149': 'n',
  723. '\uA791': 'n',
  724. '\uA7A5': 'n',
  725. '\u01CC': 'nj',
  726. '\u24DE': 'o',
  727. '\uFF4F': 'o',
  728. '\u00F2': 'o',
  729. '\u00F3': 'o',
  730. '\u00F4': 'o',
  731. '\u1ED3': 'o',
  732. '\u1ED1': 'o',
  733. '\u1ED7': 'o',
  734. '\u1ED5': 'o',
  735. '\u00F5': 'o',
  736. '\u1E4D': 'o',
  737. '\u022D': 'o',
  738. '\u1E4F': 'o',
  739. '\u014D': 'o',
  740. '\u1E51': 'o',
  741. '\u1E53': 'o',
  742. '\u014F': 'o',
  743. '\u022F': 'o',
  744. '\u0231': 'o',
  745. '\u00F6': 'o',
  746. '\u022B': 'o',
  747. '\u1ECF': 'o',
  748. '\u0151': 'o',
  749. '\u01D2': 'o',
  750. '\u020D': 'o',
  751. '\u020F': 'o',
  752. '\u01A1': 'o',
  753. '\u1EDD': 'o',
  754. '\u1EDB': 'o',
  755. '\u1EE1': 'o',
  756. '\u1EDF': 'o',
  757. '\u1EE3': 'o',
  758. '\u1ECD': 'o',
  759. '\u1ED9': 'o',
  760. '\u01EB': 'o',
  761. '\u01ED': 'o',
  762. '\u00F8': 'o',
  763. '\u01FF': 'o',
  764. '\u0254': 'o',
  765. '\uA74B': 'o',
  766. '\uA74D': 'o',
  767. '\u0275': 'o',
  768. '\u01A3': 'oi',
  769. '\u0223': 'ou',
  770. '\uA74F': 'oo',
  771. '\u24DF': 'p',
  772. '\uFF50': 'p',
  773. '\u1E55': 'p',
  774. '\u1E57': 'p',
  775. '\u01A5': 'p',
  776. '\u1D7D': 'p',
  777. '\uA751': 'p',
  778. '\uA753': 'p',
  779. '\uA755': 'p',
  780. '\u24E0': 'q',
  781. '\uFF51': 'q',
  782. '\u024B': 'q',
  783. '\uA757': 'q',
  784. '\uA759': 'q',
  785. '\u24E1': 'r',
  786. '\uFF52': 'r',
  787. '\u0155': 'r',
  788. '\u1E59': 'r',
  789. '\u0159': 'r',
  790. '\u0211': 'r',
  791. '\u0213': 'r',
  792. '\u1E5B': 'r',
  793. '\u1E5D': 'r',
  794. '\u0157': 'r',
  795. '\u1E5F': 'r',
  796. '\u024D': 'r',
  797. '\u027D': 'r',
  798. '\uA75B': 'r',
  799. '\uA7A7': 'r',
  800. '\uA783': 'r',
  801. '\u24E2': 's',
  802. '\uFF53': 's',
  803. '\u00DF': 's',
  804. '\u015B': 's',
  805. '\u1E65': 's',
  806. '\u015D': 's',
  807. '\u1E61': 's',
  808. '\u0161': 's',
  809. '\u1E67': 's',
  810. '\u1E63': 's',
  811. '\u1E69': 's',
  812. '\u0219': 's',
  813. '\u015F': 's',
  814. '\u023F': 's',
  815. '\uA7A9': 's',
  816. '\uA785': 's',
  817. '\u1E9B': 's',
  818. '\u24E3': 't',
  819. '\uFF54': 't',
  820. '\u1E6B': 't',
  821. '\u1E97': 't',
  822. '\u0165': 't',
  823. '\u1E6D': 't',
  824. '\u021B': 't',
  825. '\u0163': 't',
  826. '\u1E71': 't',
  827. '\u1E6F': 't',
  828. '\u0167': 't',
  829. '\u01AD': 't',
  830. '\u0288': 't',
  831. '\u2C66': 't',
  832. '\uA787': 't',
  833. '\uA729': 'tz',
  834. '\u24E4': 'u',
  835. '\uFF55': 'u',
  836. '\u00F9': 'u',
  837. '\u00FA': 'u',
  838. '\u00FB': 'u',
  839. '\u0169': 'u',
  840. '\u1E79': 'u',
  841. '\u016B': 'u',
  842. '\u1E7B': 'u',
  843. '\u016D': 'u',
  844. '\u00FC': 'u',
  845. '\u01DC': 'u',
  846. '\u01D8': 'u',
  847. '\u01D6': 'u',
  848. '\u01DA': 'u',
  849. '\u1EE7': 'u',
  850. '\u016F': 'u',
  851. '\u0171': 'u',
  852. '\u01D4': 'u',
  853. '\u0215': 'u',
  854. '\u0217': 'u',
  855. '\u01B0': 'u',
  856. '\u1EEB': 'u',
  857. '\u1EE9': 'u',
  858. '\u1EEF': 'u',
  859. '\u1EED': 'u',
  860. '\u1EF1': 'u',
  861. '\u1EE5': 'u',
  862. '\u1E73': 'u',
  863. '\u0173': 'u',
  864. '\u1E77': 'u',
  865. '\u1E75': 'u',
  866. '\u0289': 'u',
  867. '\u24E5': 'v',
  868. '\uFF56': 'v',
  869. '\u1E7D': 'v',
  870. '\u1E7F': 'v',
  871. '\u028B': 'v',
  872. '\uA75F': 'v',
  873. '\u028C': 'v',
  874. '\uA761': 'vy',
  875. '\u24E6': 'w',
  876. '\uFF57': 'w',
  877. '\u1E81': 'w',
  878. '\u1E83': 'w',
  879. '\u0175': 'w',
  880. '\u1E87': 'w',
  881. '\u1E85': 'w',
  882. '\u1E98': 'w',
  883. '\u1E89': 'w',
  884. '\u2C73': 'w',
  885. '\u24E7': 'x',
  886. '\uFF58': 'x',
  887. '\u1E8B': 'x',
  888. '\u1E8D': 'x',
  889. '\u24E8': 'y',
  890. '\uFF59': 'y',
  891. '\u1EF3': 'y',
  892. '\u00FD': 'y',
  893. '\u0177': 'y',
  894. '\u1EF9': 'y',
  895. '\u0233': 'y',
  896. '\u1E8F': 'y',
  897. '\u00FF': 'y',
  898. '\u1EF7': 'y',
  899. '\u1E99': 'y',
  900. '\u1EF5': 'y',
  901. '\u01B4': 'y',
  902. '\u024F': 'y',
  903. '\u1EFF': 'y',
  904. '\u24E9': 'z',
  905. '\uFF5A': 'z',
  906. '\u017A': 'z',
  907. '\u1E91': 'z',
  908. '\u017C': 'z',
  909. '\u017E': 'z',
  910. '\u1E93': 'z',
  911. '\u1E95': 'z',
  912. '\u01B6': 'z',
  913. '\u0225': 'z',
  914. '\u0240': 'z',
  915. '\u2C6C': 'z',
  916. '\uA763': 'z',
  917. '\u0386': '\u0391',
  918. '\u0388': '\u0395',
  919. '\u0389': '\u0397',
  920. '\u038A': '\u0399',
  921. '\u03AA': '\u0399',
  922. '\u038C': '\u039F',
  923. '\u038E': '\u03A5',
  924. '\u03AB': '\u03A5',
  925. '\u038F': '\u03A9',
  926. '\u03AC': '\u03B1',
  927. '\u03AD': '\u03B5',
  928. '\u03AE': '\u03B7',
  929. '\u03AF': '\u03B9',
  930. '\u03CA': '\u03B9',
  931. '\u0390': '\u03B9',
  932. '\u03CC': '\u03BF',
  933. '\u03CD': '\u03C5',
  934. '\u03CB': '\u03C5',
  935. '\u03B0': '\u03C5',
  936. '\u03C9': '\u03C9',
  937. '\u03C2': '\u03C3',
  938. };
  939. $document = $( document );
  940. nextUid = ( function () {
  941. var counter = 1;
  942. return function () {
  943. return counter++;
  944. };
  945. } )();
  946. function reinsertElement( element ) {
  947. var placeholder = $( document.createTextNode( '' ) );
  948. element.before( placeholder );
  949. placeholder.before( element );
  950. placeholder.remove();
  951. }
  952. function stripDiacritics( str ) {
  953. // Used 'uni range + named function' from http://jsperf.com/diacritics/18
  954. function match( a ) {
  955. return DIACRITICS[ a ] || a;
  956. }
  957. return str.replace( /[^\u0000-\u007E]/g, match );
  958. }
  959. function indexOf( value, array ) {
  960. var i = 0,
  961. l = array.length;
  962. for ( ; i < l; i = i + 1 ) {
  963. if ( equal( value, array[ i ] ) ) return i;
  964. }
  965. return -1;
  966. }
  967. function measureScrollbar() {
  968. var $template = $( MEASURE_SCROLLBAR_TEMPLATE );
  969. $template.appendTo( document.body );
  970. var dim = {
  971. width: $template.width() - $template[ 0 ].clientWidth,
  972. height: $template.height() - $template[ 0 ].clientHeight,
  973. };
  974. $template.remove();
  975. return dim;
  976. }
  977. /**
  978. * Compares equality of a and b
  979. * @param a
  980. * @param b
  981. */
  982. function equal( a, b ) {
  983. if ( a === b ) return true;
  984. if ( a === undefined || b === undefined ) return false;
  985. if ( a === null || b === null ) return false;
  986. // Check whether 'a' or 'b' is a string (primitive or object).
  987. // The concatenation of an empty string (+'') converts its argument to a string's primitive.
  988. if ( a.constructor === String ) return a + '' === b + ''; // a+'' - in case 'a' is a String object
  989. if ( b.constructor === String ) return b + '' === a + ''; // b+'' - in case 'b' is a String object
  990. return false;
  991. }
  992. /**
  993. * Splits the string into an array of values, transforming each value. An empty array is returned for nulls or empty
  994. * strings
  995. * @param string
  996. * @param separator
  997. */
  998. function splitVal( string, separator, transform ) {
  999. var val, i, l;
  1000. if ( string === null || string.length < 1 ) return [];
  1001. val = string.split( separator );
  1002. for ( i = 0, l = val.length; i < l; i = i + 1 )
  1003. val[ i ] = transform( val[ i ] );
  1004. return val;
  1005. }
  1006. function getSideBorderPadding( element ) {
  1007. return element.outerWidth( false ) - element.width();
  1008. }
  1009. function installKeyUpChangeEvent( element ) {
  1010. var key = 'keyup-change-value';
  1011. element.on( 'keydown', function () {
  1012. if ( $.data( element, key ) === undefined ) {
  1013. $.data( element, key, element.val() );
  1014. }
  1015. } );
  1016. element.on( 'keyup', function () {
  1017. var val = $.data( element, key );
  1018. if ( val !== undefined && element.val() !== val ) {
  1019. $.removeData( element, key );
  1020. element.trigger( 'keyup-change' );
  1021. }
  1022. } );
  1023. }
  1024. /**
  1025. * filters mouse events so an event is fired only if the mouse moved.
  1026. *
  1027. * filters out mouse events that occur when mouse is stationary but
  1028. * the elements under the pointer are scrolled.
  1029. */
  1030. function installFilteredMouseMove( element ) {
  1031. element.on( 'mousemove', function ( e ) {
  1032. var lastpos = lastMousePosition;
  1033. if (
  1034. lastpos === undefined ||
  1035. lastpos.x !== e.pageX ||
  1036. lastpos.y !== e.pageY
  1037. ) {
  1038. $( e.target ).trigger( 'mousemove-filtered', e );
  1039. }
  1040. } );
  1041. }
  1042. /**
  1043. * Debounces a function. Returns a function that calls the original fn function only if no invocations have been made
  1044. * within the last quietMillis milliseconds.
  1045. *
  1046. * @param quietMillis number of milliseconds to wait before invoking fn
  1047. * @param fn function to be debounced
  1048. * @param ctx object to be used as this reference within fn
  1049. * @return debounced version of fn
  1050. */
  1051. function debounce( quietMillis, fn, ctx ) {
  1052. ctx = ctx || undefined;
  1053. var timeout;
  1054. return function () {
  1055. var args = arguments;
  1056. window.clearTimeout( timeout );
  1057. timeout = window.setTimeout( function () {
  1058. fn.apply( ctx, args );
  1059. }, quietMillis );
  1060. };
  1061. }
  1062. function installDebouncedScroll( threshold, element ) {
  1063. var notify = debounce( threshold, function ( e ) {
  1064. element.trigger( 'scroll-debounced', e );
  1065. } );
  1066. element.on( 'scroll', function ( e ) {
  1067. if ( indexOf( e.target, element.get() ) >= 0 ) notify( e );
  1068. } );
  1069. }
  1070. function focus( $el ) {
  1071. if ( $el[ 0 ] === document.activeElement ) return;
  1072. /* set the focus in a 0 timeout - that way the focus is set after the processing
  1073. of the current event has finished - which seems like the only reliable way
  1074. to set focus */
  1075. window.setTimeout( function () {
  1076. var el = $el[ 0 ],
  1077. pos = $el.val().length,
  1078. range;
  1079. $el.focus();
  1080. /* make sure el received focus so we do not error out when trying to manipulate the caret.
  1081. sometimes modals or others listeners may steal it after its set */
  1082. var isVisible = el.offsetWidth > 0 || el.offsetHeight > 0;
  1083. if ( isVisible && el === document.activeElement ) {
  1084. /* after the focus is set move the caret to the end, necessary when we val()
  1085. just before setting focus */
  1086. if ( el.setSelectionRange ) {
  1087. el.setSelectionRange( pos, pos );
  1088. } else if ( el.createTextRange ) {
  1089. range = el.createTextRange();
  1090. range.collapse( false );
  1091. range.select();
  1092. }
  1093. }
  1094. }, 0 );
  1095. }
  1096. function getCursorInfo( el ) {
  1097. el = $( el )[ 0 ];
  1098. var offset = 0;
  1099. var length = 0;
  1100. if ( 'selectionStart' in el ) {
  1101. offset = el.selectionStart;
  1102. length = el.selectionEnd - offset;
  1103. } else if ( 'selection' in document ) {
  1104. el.focus();
  1105. var sel = document.selection.createRange();
  1106. length = document.selection.createRange().text.length;
  1107. sel.moveStart( 'character', -el.value.length );
  1108. offset = sel.text.length - length;
  1109. }
  1110. return { offset: offset, length: length };
  1111. }
  1112. function killEvent( event ) {
  1113. event.preventDefault();
  1114. event.stopPropagation();
  1115. }
  1116. function killEventImmediately( event ) {
  1117. event.preventDefault();
  1118. event.stopImmediatePropagation();
  1119. }
  1120. function measureTextWidth( e ) {
  1121. if ( ! sizer ) {
  1122. var style =
  1123. e[ 0 ].currentStyle || window.getComputedStyle( e[ 0 ], null );
  1124. sizer = $( document.createElement( 'div' ) ).css( {
  1125. position: 'absolute',
  1126. left: '-10000px',
  1127. top: '-10000px',
  1128. display: 'none',
  1129. fontSize: style.fontSize,
  1130. fontFamily: style.fontFamily,
  1131. fontStyle: style.fontStyle,
  1132. fontWeight: style.fontWeight,
  1133. letterSpacing: style.letterSpacing,
  1134. textTransform: style.textTransform,
  1135. whiteSpace: 'nowrap',
  1136. } );
  1137. sizer.attr( 'class', 'select2-sizer' );
  1138. $( document.body ).append( sizer );
  1139. }
  1140. sizer.text( e.val() );
  1141. return sizer.width();
  1142. }
  1143. function syncCssClasses( dest, src, adapter ) {
  1144. var classes,
  1145. replacements = [],
  1146. adapted;
  1147. classes = $.trim( dest.attr( 'class' ) );
  1148. if ( classes ) {
  1149. classes = '' + classes; // for IE which returns object
  1150. $( classes.split( /\s+/ ) ).each2( function () {
  1151. if ( this.indexOf( 'select2-' ) === 0 ) {
  1152. replacements.push( this );
  1153. }
  1154. } );
  1155. }
  1156. classes = $.trim( src.attr( 'class' ) );
  1157. if ( classes ) {
  1158. classes = '' + classes; // for IE which returns object
  1159. $( classes.split( /\s+/ ) ).each2( function () {
  1160. if ( this.indexOf( 'select2-' ) !== 0 ) {
  1161. adapted = adapter( this );
  1162. if ( adapted ) {
  1163. replacements.push( adapted );
  1164. }
  1165. }
  1166. } );
  1167. }
  1168. dest.attr( 'class', replacements.join( ' ' ) );
  1169. }
  1170. function markMatch( text, term, markup, escapeMarkup ) {
  1171. var match = stripDiacritics( text.toUpperCase() ).indexOf(
  1172. stripDiacritics( term.toUpperCase() )
  1173. ),
  1174. tl = term.length;
  1175. if ( match < 0 ) {
  1176. markup.push( escapeMarkup( text ) );
  1177. return;
  1178. }
  1179. markup.push( escapeMarkup( text.substring( 0, match ) ) );
  1180. markup.push( "<span class='select2-match'>" );
  1181. markup.push( escapeMarkup( text.substring( match, match + tl ) ) );
  1182. markup.push( '</span>' );
  1183. markup.push(
  1184. escapeMarkup( text.substring( match + tl, text.length ) )
  1185. );
  1186. }
  1187. function defaultEscapeMarkup( markup ) {
  1188. var replace_map = {
  1189. '\\': '&#92;',
  1190. '&': '&amp;',
  1191. '<': '&lt;',
  1192. '>': '&gt;',
  1193. '"': '&quot;',
  1194. "'": '&#39;',
  1195. '/': '&#47;',
  1196. };
  1197. return String( markup ).replace( /[&<>"'\/\\]/g, function ( match ) {
  1198. return replace_map[ match ];
  1199. } );
  1200. }
  1201. /**
  1202. * Produces an ajax-based query function
  1203. *
  1204. * @param options object containing configuration parameters
  1205. * @param options.params parameter map for the transport ajax call, can contain such options as cache, jsonpCallback, etc. see $.ajax
  1206. * @param options.transport function that will be used to execute the ajax request. must be compatible with parameters supported by $.ajax
  1207. * @param options.url url for the data
  1208. * @param options.data a function(searchTerm, pageNumber, context) that should return an object containing query string parameters for the above url.
  1209. * @param options.dataType request data type: ajax, jsonp, other datatypes supported by jQuery's $.ajax function or the transport function if specified
  1210. * @param options.quietMillis (optional) milliseconds to wait before making the ajaxRequest, helps debounce the ajax function if invoked too often
  1211. * @param options.results a function(remoteData, pageNumber, query) that converts data returned form the remote request to the format expected by Select2.
  1212. * The expected format is an object containing the following keys:
  1213. * results array of objects that will be used as choices
  1214. * more (optional) boolean indicating whether there are more results available
  1215. * Example: {results:[{id:1, text:'Red'},{id:2, text:'Blue'}], more:true}
  1216. */
  1217. function ajax( options ) {
  1218. var timeout, // current scheduled but not yet executed request
  1219. handler = null,
  1220. quietMillis = options.quietMillis || 100,
  1221. ajaxUrl = options.url,
  1222. self = this;
  1223. return function ( query ) {
  1224. window.clearTimeout( timeout );
  1225. timeout = window.setTimeout( function () {
  1226. var data = options.data, // ajax data function
  1227. url = ajaxUrl, // ajax url string or function
  1228. transport =
  1229. options.transport ||
  1230. $.fn.select2.ajaxDefaults.transport,
  1231. // deprecated - to be removed in 4.0 - use params instead
  1232. deprecated = {
  1233. type: options.type || 'GET', // set type of request (GET or POST)
  1234. cache: options.cache || false,
  1235. jsonpCallback: options.jsonpCallback || undefined,
  1236. dataType: options.dataType || 'json',
  1237. },
  1238. params = $.extend(
  1239. {},
  1240. $.fn.select2.ajaxDefaults.params,
  1241. deprecated
  1242. );
  1243. data = data
  1244. ? data.call( self, query.term, query.page, query.context )
  1245. : null;
  1246. url =
  1247. typeof url === 'function'
  1248. ? url.call(
  1249. self,
  1250. query.term,
  1251. query.page,
  1252. query.context
  1253. )
  1254. : url;
  1255. if ( handler && typeof handler.abort === 'function' ) {
  1256. handler.abort();
  1257. }
  1258. if ( options.params ) {
  1259. if ( $.isFunction( options.params ) ) {
  1260. $.extend( params, options.params.call( self ) );
  1261. } else {
  1262. $.extend( params, options.params );
  1263. }
  1264. }
  1265. $.extend( params, {
  1266. url: url,
  1267. dataType: options.dataType,
  1268. data: data,
  1269. success: function ( data ) {
  1270. // TODO - replace query.page with query so users have access to term, page, etc.
  1271. // added query as third paramter to keep backwards compatibility
  1272. var results = options.results(
  1273. data,
  1274. query.page,
  1275. query
  1276. );
  1277. query.callback( results );
  1278. },
  1279. error: function ( jqXHR, textStatus, errorThrown ) {
  1280. var results = {
  1281. hasError: true,
  1282. jqXHR: jqXHR,
  1283. textStatus: textStatus,
  1284. errorThrown: errorThrown,
  1285. };
  1286. query.callback( results );
  1287. },
  1288. } );
  1289. handler = transport.call( self, params );
  1290. }, quietMillis );
  1291. };
  1292. }
  1293. /**
  1294. * Produces a query function that works with a local array
  1295. *
  1296. * @param options object containing configuration parameters. The options parameter can either be an array or an
  1297. * object.
  1298. *
  1299. * If the array form is used it is assumed that it contains objects with 'id' and 'text' keys.
  1300. *
  1301. * If the object form is used it is assumed that it contains 'data' and 'text' keys. The 'data' key should contain
  1302. * an array of objects that will be used as choices. These objects must contain at least an 'id' key. The 'text'
  1303. * key can either be a String in which case it is expected that each element in the 'data' array has a key with the
  1304. * value of 'text' which will be used to match choices. Alternatively, text can be a function(item) that can extract
  1305. * the text.
  1306. */
  1307. function local( options ) {
  1308. var data = options, // data elements
  1309. dataText,
  1310. tmp,
  1311. text = function ( item ) {
  1312. return '' + item.text;
  1313. }; // function used to retrieve the text portion of a data item that is matched against the search
  1314. if ( $.isArray( data ) ) {
  1315. tmp = data;
  1316. data = { results: tmp };
  1317. }
  1318. if ( $.isFunction( data ) === false ) {
  1319. tmp = data;
  1320. data = function () {
  1321. return tmp;
  1322. };
  1323. }
  1324. var dataItem = data();
  1325. if ( dataItem.text ) {
  1326. text = dataItem.text;
  1327. // if text is not a function we assume it to be a key name
  1328. if ( ! $.isFunction( text ) ) {
  1329. dataText = dataItem.text; // we need to store this in a separate variable because in the next step data gets reset and data.text is no longer available
  1330. text = function ( item ) {
  1331. return item[ dataText ];
  1332. };
  1333. }
  1334. }
  1335. return function ( query ) {
  1336. var t = query.term,
  1337. filtered = { results: [] },
  1338. process;
  1339. if ( t === '' ) {
  1340. query.callback( data() );
  1341. return;
  1342. }
  1343. process = function ( datum, collection ) {
  1344. var group, attr;
  1345. datum = datum[ 0 ];
  1346. if ( datum.children ) {
  1347. group = {};
  1348. for ( attr in datum ) {
  1349. if ( datum.hasOwnProperty( attr ) )
  1350. group[ attr ] = datum[ attr ];
  1351. }
  1352. group.children = [];
  1353. $( datum.children ).each2( function ( i, childDatum ) {
  1354. process( childDatum, group.children );
  1355. } );
  1356. if (
  1357. group.children.length ||
  1358. query.matcher( t, text( group ), datum )
  1359. ) {
  1360. collection.push( group );
  1361. }
  1362. } else {
  1363. if ( query.matcher( t, text( datum ), datum ) ) {
  1364. collection.push( datum );
  1365. }
  1366. }
  1367. };
  1368. $( data().results ).each2( function ( i, datum ) {
  1369. process( datum, filtered.results );
  1370. } );
  1371. query.callback( filtered );
  1372. };
  1373. }
  1374. // TODO javadoc
  1375. function tags( data ) {
  1376. var isFunc = $.isFunction( data );
  1377. return function ( query ) {
  1378. var t = query.term,
  1379. filtered = { results: [] };
  1380. var result = isFunc ? data( query ) : data;
  1381. if ( $.isArray( result ) ) {
  1382. $( result ).each( function () {
  1383. var isObject = this.text !== undefined,
  1384. text = isObject ? this.text : this;
  1385. if ( t === '' || query.matcher( t, text ) ) {
  1386. filtered.results.push(
  1387. isObject ? this : { id: this, text: this }
  1388. );
  1389. }
  1390. } );
  1391. query.callback( filtered );
  1392. }
  1393. };
  1394. }
  1395. /**
  1396. * Checks if the formatter function should be used.
  1397. *
  1398. * Throws an error if it is not a function. Returns true if it should be used,
  1399. * false if no formatting should be performed.
  1400. *
  1401. * @param formatter
  1402. */
  1403. function checkFormatter( formatter, formatterName ) {
  1404. if ( $.isFunction( formatter ) ) return true;
  1405. if ( ! formatter ) return false;
  1406. if ( typeof formatter === 'string' ) return true;
  1407. throw new Error(
  1408. formatterName + ' must be a string, function, or falsy value'
  1409. );
  1410. }
  1411. /**
  1412. * Returns a given value
  1413. * If given a function, returns its output
  1414. *
  1415. * @param val string|function
  1416. * @param context value of "this" to be passed to function
  1417. * @returns {*}
  1418. */
  1419. function evaluate( val, context ) {
  1420. if ( $.isFunction( val ) ) {
  1421. var args = Array.prototype.slice.call( arguments, 2 );
  1422. return val.apply( context, args );
  1423. }
  1424. return val;
  1425. }
  1426. function countResults( results ) {
  1427. var count = 0;
  1428. $.each( results, function ( i, item ) {
  1429. if ( item.children ) {
  1430. count += countResults( item.children );
  1431. } else {
  1432. count++;
  1433. }
  1434. } );
  1435. return count;
  1436. }
  1437. /**
  1438. * Default tokenizer. This function uses breaks the input on substring match of any string from the
  1439. * opts.tokenSeparators array and uses opts.createSearchChoice to create the choice object. Both of those
  1440. * two options have to be defined in order for the tokenizer to work.
  1441. *
  1442. * @param input text user has typed so far or pasted into the search field
  1443. * @param selection currently selected choices
  1444. * @param selectCallback function(choice) callback tho add the choice to selection
  1445. * @param opts select2's opts
  1446. * @return undefined/null to leave the current input unchanged, or a string to change the input to the returned value
  1447. */
  1448. function defaultTokenizer( input, selection, selectCallback, opts ) {
  1449. var original = input, // store the original so we can compare and know if we need to tell the search to update its text
  1450. dupe = false, // check for whether a token we extracted represents a duplicate selected choice
  1451. token, // token
  1452. index, // position at which the separator was found
  1453. i,
  1454. l, // looping variables
  1455. separator; // the matched separator
  1456. if (
  1457. ! opts.createSearchChoice ||
  1458. ! opts.tokenSeparators ||
  1459. opts.tokenSeparators.length < 1
  1460. )
  1461. return undefined;
  1462. while ( true ) {
  1463. index = -1;
  1464. for ( i = 0, l = opts.tokenSeparators.length; i < l; i++ ) {
  1465. separator = opts.tokenSeparators[ i ];
  1466. index = input.indexOf( separator );
  1467. if ( index >= 0 ) break;
  1468. }
  1469. if ( index < 0 ) break; // did not find any token separator in the input string, bail
  1470. token = input.substring( 0, index );
  1471. input = input.substring( index + separator.length );
  1472. if ( token.length > 0 ) {
  1473. token = opts.createSearchChoice.call( this, token, selection );
  1474. if (
  1475. token !== undefined &&
  1476. token !== null &&
  1477. opts.id( token ) !== undefined &&
  1478. opts.id( token ) !== null
  1479. ) {
  1480. dupe = false;
  1481. for ( i = 0, l = selection.length; i < l; i++ ) {
  1482. if (
  1483. equal( opts.id( token ), opts.id( selection[ i ] ) )
  1484. ) {
  1485. dupe = true;
  1486. break;
  1487. }
  1488. }
  1489. if ( ! dupe ) selectCallback( token );
  1490. }
  1491. }
  1492. }
  1493. if ( original !== input ) return input;
  1494. }
  1495. function cleanupJQueryElements() {
  1496. var self = this;
  1497. $.each( arguments, function ( i, element ) {
  1498. self[ element ].remove();
  1499. self[ element ] = null;
  1500. } );
  1501. }
  1502. /**
  1503. * Creates a new class
  1504. *
  1505. * @param superClass
  1506. * @param methods
  1507. */
  1508. function clazz( SuperClass, methods ) {
  1509. var constructor = function () {};
  1510. constructor.prototype = new SuperClass();
  1511. constructor.prototype.constructor = constructor;
  1512. constructor.prototype.parent = SuperClass.prototype;
  1513. constructor.prototype = $.extend( constructor.prototype, methods );
  1514. return constructor;
  1515. }
  1516. AbstractSelect2 = clazz( Object, {
  1517. // abstract
  1518. bind: function ( func ) {
  1519. var self = this;
  1520. return function () {
  1521. func.apply( self, arguments );
  1522. };
  1523. },
  1524. // abstract
  1525. init: function ( opts ) {
  1526. var results,
  1527. search,
  1528. resultsSelector = '.select2-results';
  1529. // prepare options
  1530. this.opts = opts = this.prepareOpts( opts );
  1531. this.id = opts.id;
  1532. // destroy if called on an existing component
  1533. if (
  1534. opts.element.data( 'select2' ) !== undefined &&
  1535. opts.element.data( 'select2' ) !== null
  1536. ) {
  1537. opts.element.data( 'select2' ).destroy();
  1538. }
  1539. this.container = this.createContainer();
  1540. this.liveRegion = $( '.select2-hidden-accessible' );
  1541. if ( this.liveRegion.length == 0 ) {
  1542. this.liveRegion = $( '<span>', {
  1543. role: 'status',
  1544. 'aria-live': 'polite',
  1545. } )
  1546. .addClass( 'select2-hidden-accessible' )
  1547. .appendTo( document.body );
  1548. }
  1549. this.containerId =
  1550. 's2id_' +
  1551. ( opts.element.attr( 'id' ) || 'autogen' + nextUid() );
  1552. this.containerEventName = this.containerId
  1553. .replace( /([.])/g, '_' )
  1554. .replace( /([;&,\-\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1' );
  1555. this.container.attr( 'id', this.containerId );
  1556. this.container.attr( 'title', opts.element.attr( 'title' ) );
  1557. this.body = $( document.body );
  1558. syncCssClasses(
  1559. this.container,
  1560. this.opts.element,
  1561. this.opts.adaptContainerCssClass
  1562. );
  1563. this.container.attr( 'style', opts.element.attr( 'style' ) );
  1564. this.container.css(
  1565. evaluate( opts.containerCss, this.opts.element )
  1566. );
  1567. this.container.addClass(
  1568. evaluate( opts.containerCssClass, this.opts.element )
  1569. );
  1570. this.elementTabIndex = this.opts.element.attr( 'tabindex' );
  1571. // swap container for the element
  1572. this.opts.element
  1573. .data( 'select2', this )
  1574. .attr( 'tabindex', '-1' )
  1575. .before( this.container )
  1576. .on( 'click.select2', killEvent ); // do not leak click events
  1577. this.container.data( 'select2', this );
  1578. this.dropdown = this.container.find( '.select2-drop' );
  1579. syncCssClasses(
  1580. this.dropdown,
  1581. this.opts.element,
  1582. this.opts.adaptDropdownCssClass
  1583. );
  1584. this.dropdown.addClass(
  1585. evaluate( opts.dropdownCssClass, this.opts.element )
  1586. );
  1587. this.dropdown.data( 'select2', this );
  1588. this.dropdown.on( 'click', killEvent );
  1589. this.results = results = this.container.find( resultsSelector );
  1590. this.search = search = this.container.find( 'input.select2-input' );
  1591. this.queryCount = 0;
  1592. this.resultsPage = 0;
  1593. this.context = null;
  1594. // initialize the container
  1595. this.initContainer();
  1596. this.container.on( 'click', killEvent );
  1597. installFilteredMouseMove( this.results );
  1598. this.dropdown.on(
  1599. 'mousemove-filtered',
  1600. resultsSelector,
  1601. this.bind( this.highlightUnderEvent )
  1602. );
  1603. this.dropdown.on(
  1604. 'touchstart touchmove touchend',
  1605. resultsSelector,
  1606. this.bind( function ( event ) {
  1607. this._touchEvent = true;
  1608. this.highlightUnderEvent( event );
  1609. } )
  1610. );
  1611. this.dropdown.on(
  1612. 'touchmove',
  1613. resultsSelector,
  1614. this.bind( this.touchMoved )
  1615. );
  1616. this.dropdown.on(
  1617. 'touchstart touchend',
  1618. resultsSelector,
  1619. this.bind( this.clearTouchMoved )
  1620. );
  1621. // Waiting for a click event on touch devices to select option and hide dropdown
  1622. // otherwise click will be triggered on an underlying element
  1623. this.dropdown.on(
  1624. 'click',
  1625. this.bind( function ( event ) {
  1626. if ( this._touchEvent ) {
  1627. this._touchEvent = false;
  1628. this.selectHighlighted();
  1629. }
  1630. } )
  1631. );
  1632. installDebouncedScroll( 80, this.results );
  1633. this.dropdown.on(
  1634. 'scroll-debounced',
  1635. resultsSelector,
  1636. this.bind( this.loadMoreIfNeeded )
  1637. );
  1638. // do not propagate change event from the search field out of the component
  1639. $( this.container ).on( 'change', '.select2-input', function ( e ) {
  1640. e.stopPropagation();
  1641. } );
  1642. $( this.dropdown ).on( 'change', '.select2-input', function ( e ) {
  1643. e.stopPropagation();
  1644. } );
  1645. // if jquery.mousewheel plugin is installed we can prevent out-of-bounds scrolling of results via mousewheel
  1646. if ( $.fn.mousewheel ) {
  1647. results.mousewheel( function ( e, delta, deltaX, deltaY ) {
  1648. var top = results.scrollTop();
  1649. if ( deltaY > 0 && top - deltaY <= 0 ) {
  1650. results.scrollTop( 0 );
  1651. killEvent( e );
  1652. } else if (
  1653. deltaY < 0 &&
  1654. results.get( 0 ).scrollHeight -
  1655. results.scrollTop() +
  1656. deltaY <=
  1657. results.height()
  1658. ) {
  1659. results.scrollTop(
  1660. results.get( 0 ).scrollHeight - results.height()
  1661. );
  1662. killEvent( e );
  1663. }
  1664. } );
  1665. }
  1666. installKeyUpChangeEvent( search );
  1667. search.on(
  1668. 'keyup-change input paste',
  1669. this.bind( this.updateResults )
  1670. );
  1671. search.on( 'focus', function () {
  1672. search.addClass( 'select2-focused' );
  1673. } );
  1674. search.on( 'blur', function () {
  1675. search.removeClass( 'select2-focused' );
  1676. } );
  1677. this.dropdown.on(
  1678. 'mouseup',
  1679. resultsSelector,
  1680. this.bind( function ( e ) {
  1681. if (
  1682. $( e.target ).closest( '.select2-result-selectable' )
  1683. .length > 0
  1684. ) {
  1685. this.highlightUnderEvent( e );
  1686. this.selectHighlighted( e );
  1687. }
  1688. } )
  1689. );
  1690. // trap all mouse events from leaving the dropdown. sometimes there may be a modal that is listening
  1691. // for mouse events outside of itself so it can close itself. since the dropdown is now outside the select2's
  1692. // dom it will trigger the popup close, which is not what we want
  1693. // focusin can cause focus wars between modals and select2 since the dropdown is outside the modal.
  1694. this.dropdown.on(
  1695. 'click mouseup mousedown touchstart touchend focusin',
  1696. function ( e ) {
  1697. e.stopPropagation();
  1698. }
  1699. );
  1700. this.nextSearchTerm = undefined;
  1701. if ( $.isFunction( this.opts.initSelection ) ) {
  1702. // initialize selection based on the current value of the source element
  1703. this.initSelection();
  1704. // if the user has provided a function that can set selection based on the value of the source element
  1705. // we monitor the change event on the element and trigger it, allowing for two way synchronization
  1706. this.monitorSource();
  1707. }
  1708. if ( opts.maximumInputLength !== null ) {
  1709. this.search.attr( 'maxlength', opts.maximumInputLength );
  1710. }
  1711. var disabled = opts.element.prop( 'disabled' );
  1712. if ( disabled === undefined ) disabled = false;
  1713. this.enable( ! disabled );
  1714. var readonly = opts.element.prop( 'readonly' );
  1715. if ( readonly === undefined ) readonly = false;
  1716. this.readonly( readonly );
  1717. // Calculate size of scrollbar
  1718. scrollBarDimensions = scrollBarDimensions || measureScrollbar();
  1719. this.autofocus = opts.element.prop( 'autofocus' );
  1720. opts.element.prop( 'autofocus', false );
  1721. if ( this.autofocus ) this.focus();
  1722. this.search.attr( 'placeholder', opts.searchInputPlaceholder );
  1723. },
  1724. // abstract
  1725. destroy: function () {
  1726. var element = this.opts.element,
  1727. select2 = element.data( 'select2' ),
  1728. self = this;
  1729. this.close();
  1730. if ( element.length && element[ 0 ].detachEvent && self._sync ) {
  1731. element.each( function () {
  1732. if ( self._sync ) {
  1733. this.detachEvent( 'onpropertychange', self._sync );
  1734. }
  1735. } );
  1736. }
  1737. if ( this.propertyObserver ) {
  1738. this.propertyObserver.disconnect();
  1739. this.propertyObserver = null;
  1740. }
  1741. this._sync = null;
  1742. if ( select2 !== undefined ) {
  1743. select2.container.remove();
  1744. select2.liveRegion.remove();
  1745. select2.dropdown.remove();
  1746. element
  1747. .show()
  1748. .removeData( 'select2' )
  1749. .off( '.select2' )
  1750. .prop( 'autofocus', this.autofocus || false );
  1751. if ( this.elementTabIndex ) {
  1752. element.attr( { tabindex: this.elementTabIndex } );
  1753. } else {
  1754. element.removeAttr( 'tabindex' );
  1755. }
  1756. element.show();
  1757. }
  1758. cleanupJQueryElements.call(
  1759. this,
  1760. 'container',
  1761. 'liveRegion',
  1762. 'dropdown',
  1763. 'results',
  1764. 'search'
  1765. );
  1766. },
  1767. // abstract
  1768. optionToData: function ( element ) {
  1769. if ( element.is( 'option' ) ) {
  1770. return {
  1771. id: element.prop( 'value' ),
  1772. text: element.text(),
  1773. element: element.get(),
  1774. css: element.attr( 'class' ),
  1775. disabled: element.prop( 'disabled' ),
  1776. locked:
  1777. equal( element.attr( 'locked' ), 'locked' ) ||
  1778. equal( element.data( 'locked' ), true ),
  1779. };
  1780. } else if ( element.is( 'optgroup' ) ) {
  1781. return {
  1782. text: element.attr( 'label' ),
  1783. children: [],
  1784. element: element.get(),
  1785. css: element.attr( 'class' ),
  1786. };
  1787. }
  1788. },
  1789. // abstract
  1790. prepareOpts: function ( opts ) {
  1791. var element,
  1792. select,
  1793. idKey,
  1794. ajaxUrl,
  1795. self = this;
  1796. element = opts.element;
  1797. if ( element.get( 0 ).tagName.toLowerCase() === 'select' ) {
  1798. this.select = select = opts.element;
  1799. }
  1800. if ( select ) {
  1801. // these options are not allowed when attached to a select because they are picked up off the element itself
  1802. $.each(
  1803. [
  1804. 'id',
  1805. 'multiple',
  1806. 'ajax',
  1807. 'query',
  1808. 'createSearchChoice',
  1809. 'initSelection',
  1810. 'data',
  1811. 'tags',
  1812. ],
  1813. function () {
  1814. if ( this in opts ) {
  1815. throw new Error(
  1816. "Option '" +
  1817. this +
  1818. "' is not allowed for Select2 when attached to a <select> element."
  1819. );
  1820. }
  1821. }
  1822. );
  1823. }
  1824. opts = $.extend(
  1825. {},
  1826. {
  1827. populateResults: function ( container, results, query ) {
  1828. var populate,
  1829. id = this.opts.id,
  1830. liveRegion = this.liveRegion;
  1831. populate = function ( results, container, depth ) {
  1832. var i,
  1833. l,
  1834. result,
  1835. selectable,
  1836. disabled,
  1837. compound,
  1838. node,
  1839. label,
  1840. innerContainer,
  1841. formatted;
  1842. results = opts.sortResults(
  1843. results,
  1844. container,
  1845. query
  1846. );
  1847. // collect the created nodes for bulk append
  1848. var nodes = [];
  1849. for (
  1850. i = 0, l = results.length;
  1851. i < l;
  1852. i = i + 1
  1853. ) {
  1854. result = results[ i ];
  1855. disabled = result.disabled === true;
  1856. selectable =
  1857. ! disabled && id( result ) !== undefined;
  1858. compound =
  1859. result.children &&
  1860. result.children.length > 0;
  1861. node = $( '<li></li>' );
  1862. node.addClass(
  1863. 'select2-results-dept-' + depth
  1864. );
  1865. node.addClass( 'select2-result' );
  1866. node.addClass(
  1867. selectable
  1868. ? 'select2-result-selectable'
  1869. : 'select2-result-unselectable'
  1870. );
  1871. if ( disabled ) {
  1872. node.addClass( 'select2-disabled' );
  1873. }
  1874. if ( compound ) {
  1875. node.addClass(
  1876. 'select2-result-with-children'
  1877. );
  1878. }
  1879. node.addClass(
  1880. self.opts.formatResultCssClass( result )
  1881. );
  1882. node.attr( 'role', 'presentation' );
  1883. label = $( document.createElement( 'div' ) );
  1884. label.addClass( 'select2-result-label' );
  1885. label.attr(
  1886. 'id',
  1887. 'select2-result-label-' + nextUid()
  1888. );
  1889. label.attr( 'role', 'option' );
  1890. formatted = opts.formatResult(
  1891. result,
  1892. label,
  1893. query,
  1894. self.opts.escapeMarkup
  1895. );
  1896. if ( formatted !== undefined ) {
  1897. label.html( formatted );
  1898. node.append( label );
  1899. }
  1900. if ( compound ) {
  1901. innerContainer = $( '<ul></ul>' );
  1902. innerContainer.addClass(
  1903. 'select2-result-sub'
  1904. );
  1905. populate(
  1906. result.children,
  1907. innerContainer,
  1908. depth + 1
  1909. );
  1910. node.append( innerContainer );
  1911. }
  1912. node.data( 'select2-data', result );
  1913. nodes.push( node[ 0 ] );
  1914. }
  1915. // bulk append the created nodes
  1916. container.append( nodes );
  1917. liveRegion.text(
  1918. opts.formatMatches( results.length )
  1919. );
  1920. };
  1921. populate( results, container, 0 );
  1922. },
  1923. },
  1924. $.fn.select2.defaults,
  1925. opts
  1926. );
  1927. if ( typeof opts.id !== 'function' ) {
  1928. idKey = opts.id;
  1929. opts.id = function ( e ) {
  1930. return e[ idKey ];
  1931. };
  1932. }
  1933. if ( $.isArray( opts.element.data( 'select2Tags' ) ) ) {
  1934. if ( 'tags' in opts ) {
  1935. throw (
  1936. "tags specified as both an attribute 'data-select2-tags' and in options of Select2 " +
  1937. opts.element.attr( 'id' )
  1938. );
  1939. }
  1940. opts.tags = opts.element.data( 'select2Tags' );
  1941. }
  1942. if ( select ) {
  1943. opts.query = this.bind( function ( query ) {
  1944. var data = { results: [], more: false },
  1945. term = query.term,
  1946. children,
  1947. placeholderOption,
  1948. process;
  1949. process = function ( element, collection ) {
  1950. var group;
  1951. if ( element.is( 'option' ) ) {
  1952. if (
  1953. query.matcher( term, element.text(), element )
  1954. ) {
  1955. collection.push( self.optionToData( element ) );
  1956. }
  1957. } else if ( element.is( 'optgroup' ) ) {
  1958. group = self.optionToData( element );
  1959. element.children().each2( function ( i, elm ) {
  1960. process( elm, group.children );
  1961. } );
  1962. if ( group.children.length > 0 ) {
  1963. collection.push( group );
  1964. }
  1965. }
  1966. };
  1967. children = element.children();
  1968. // ignore the placeholder option if there is one
  1969. if (
  1970. this.getPlaceholder() !== undefined &&
  1971. children.length > 0
  1972. ) {
  1973. placeholderOption = this.getPlaceholderOption();
  1974. if ( placeholderOption ) {
  1975. children = children.not( placeholderOption );
  1976. }
  1977. }
  1978. children.each2( function ( i, elm ) {
  1979. process( elm, data.results );
  1980. } );
  1981. query.callback( data );
  1982. } );
  1983. // this is needed because inside val() we construct choices from options and their id is hardcoded
  1984. opts.id = function ( e ) {
  1985. return e.id;
  1986. };
  1987. } else {
  1988. if ( ! ( 'query' in opts ) ) {
  1989. if ( 'ajax' in opts ) {
  1990. ajaxUrl = opts.element.data( 'ajax-url' );
  1991. if ( ajaxUrl && ajaxUrl.length > 0 ) {
  1992. opts.ajax.url = ajaxUrl;
  1993. }
  1994. opts.query = ajax.call( opts.element, opts.ajax );
  1995. } else if ( 'data' in opts ) {
  1996. opts.query = local( opts.data );
  1997. } else if ( 'tags' in opts ) {
  1998. opts.query = tags( opts.tags );
  1999. if ( opts.createSearchChoice === undefined ) {
  2000. opts.createSearchChoice = function ( term ) {
  2001. return {
  2002. id: $.trim( term ),
  2003. text: $.trim( term ),
  2004. };
  2005. };
  2006. }
  2007. if ( opts.initSelection === undefined ) {
  2008. opts.initSelection = function (
  2009. element,
  2010. callback
  2011. ) {
  2012. var data = [];
  2013. $(
  2014. splitVal(
  2015. element.val(),
  2016. opts.separator,
  2017. opts.transformVal
  2018. )
  2019. ).each( function () {
  2020. var obj = { id: this, text: this },
  2021. tags = opts.tags;
  2022. if ( $.isFunction( tags ) ) tags = tags();
  2023. $( tags ).each( function () {
  2024. if ( equal( this.id, obj.id ) ) {
  2025. obj = this;
  2026. return false;
  2027. }
  2028. } );
  2029. data.push( obj );
  2030. } );
  2031. callback( data );
  2032. };
  2033. }
  2034. }
  2035. }
  2036. }
  2037. if ( typeof opts.query !== 'function' ) {
  2038. throw (
  2039. 'query function not defined for Select2 ' +
  2040. opts.element.attr( 'id' )
  2041. );
  2042. }
  2043. if ( opts.createSearchChoicePosition === 'top' ) {
  2044. opts.createSearchChoicePosition = function ( list, item ) {
  2045. list.unshift( item );
  2046. };
  2047. } else if ( opts.createSearchChoicePosition === 'bottom' ) {
  2048. opts.createSearchChoicePosition = function ( list, item ) {
  2049. list.push( item );
  2050. };
  2051. } else if (
  2052. typeof opts.createSearchChoicePosition !== 'function'
  2053. ) {
  2054. throw "invalid createSearchChoicePosition option must be 'top', 'bottom' or a custom function";
  2055. }
  2056. return opts;
  2057. },
  2058. /**
  2059. * Monitor the original element for changes and update select2 accordingly
  2060. */
  2061. // abstract
  2062. monitorSource: function () {
  2063. var el = this.opts.element,
  2064. observer,
  2065. self = this;
  2066. el.on(
  2067. 'change.select2',
  2068. this.bind( function ( e ) {
  2069. if (
  2070. this.opts.element.data( 'select2-change-triggered' ) !==
  2071. true
  2072. ) {
  2073. this.initSelection();
  2074. }
  2075. } )
  2076. );
  2077. this._sync = this.bind( function () {
  2078. // sync enabled state
  2079. var disabled = el.prop( 'disabled' );
  2080. if ( disabled === undefined ) disabled = false;
  2081. this.enable( ! disabled );
  2082. var readonly = el.prop( 'readonly' );
  2083. if ( readonly === undefined ) readonly = false;
  2084. this.readonly( readonly );
  2085. if ( this.container ) {
  2086. syncCssClasses(
  2087. this.container,
  2088. this.opts.element,
  2089. this.opts.adaptContainerCssClass
  2090. );
  2091. this.container.addClass(
  2092. evaluate(
  2093. this.opts.containerCssClass,
  2094. this.opts.element
  2095. )
  2096. );
  2097. }
  2098. if ( this.dropdown ) {
  2099. syncCssClasses(
  2100. this.dropdown,
  2101. this.opts.element,
  2102. this.opts.adaptDropdownCssClass
  2103. );
  2104. this.dropdown.addClass(
  2105. evaluate(
  2106. this.opts.dropdownCssClass,
  2107. this.opts.element
  2108. )
  2109. );
  2110. }
  2111. } );
  2112. // IE8-10 (IE9/10 won't fire propertyChange via attachEventListener)
  2113. if ( el.length && el[ 0 ].attachEvent ) {
  2114. el.each( function () {
  2115. this.attachEvent( 'onpropertychange', self._sync );
  2116. } );
  2117. }
  2118. // safari, chrome, firefox, IE11
  2119. observer =
  2120. window.MutationObserver ||
  2121. window.WebKitMutationObserver ||
  2122. window.MozMutationObserver;
  2123. if ( observer !== undefined ) {
  2124. if ( this.propertyObserver ) {
  2125. delete this.propertyObserver;
  2126. this.propertyObserver = null;
  2127. }
  2128. this.propertyObserver = new observer( function ( mutations ) {
  2129. $.each( mutations, self._sync );
  2130. } );
  2131. this.propertyObserver.observe( el.get( 0 ), {
  2132. attributes: true,
  2133. subtree: false,
  2134. } );
  2135. }
  2136. },
  2137. // abstract
  2138. triggerSelect: function ( data ) {
  2139. var evt = $.Event( 'select2-selecting', {
  2140. val: this.id( data ),
  2141. object: data,
  2142. choice: data,
  2143. } );
  2144. this.opts.element.trigger( evt );
  2145. return ! evt.isDefaultPrevented();
  2146. },
  2147. /**
  2148. * Triggers the change event on the source element
  2149. */
  2150. // abstract
  2151. triggerChange: function ( details ) {
  2152. details = details || {};
  2153. details = $.extend( {}, details, {
  2154. type: 'change',
  2155. val: this.val(),
  2156. } );
  2157. // prevents recursive triggering
  2158. this.opts.element.data( 'select2-change-triggered', true );
  2159. this.opts.element.trigger( details );
  2160. this.opts.element.data( 'select2-change-triggered', false );
  2161. // some validation frameworks ignore the change event and listen instead to keyup, click for selects
  2162. // so here we trigger the click event manually
  2163. this.opts.element.click();
  2164. // ValidationEngine ignores the change event and listens instead to blur
  2165. // so here we trigger the blur event manually if so desired
  2166. if ( this.opts.blurOnChange ) this.opts.element.blur();
  2167. },
  2168. //abstract
  2169. isInterfaceEnabled: function () {
  2170. return this.enabledInterface === true;
  2171. },
  2172. // abstract
  2173. enableInterface: function () {
  2174. var enabled = this._enabled && ! this._readonly,
  2175. disabled = ! enabled;
  2176. if ( enabled === this.enabledInterface ) return false;
  2177. this.container.toggleClass(
  2178. 'select2-container-disabled',
  2179. disabled
  2180. );
  2181. this.close();
  2182. this.enabledInterface = enabled;
  2183. return true;
  2184. },
  2185. // abstract
  2186. enable: function ( enabled ) {
  2187. if ( enabled === undefined ) enabled = true;
  2188. if ( this._enabled === enabled ) return;
  2189. this._enabled = enabled;
  2190. this.opts.element.prop( 'disabled', ! enabled );
  2191. this.enableInterface();
  2192. },
  2193. // abstract
  2194. disable: function () {
  2195. this.enable( false );
  2196. },
  2197. // abstract
  2198. readonly: function ( enabled ) {
  2199. if ( enabled === undefined ) enabled = false;
  2200. if ( this._readonly === enabled ) return;
  2201. this._readonly = enabled;
  2202. this.opts.element.prop( 'readonly', enabled );
  2203. this.enableInterface();
  2204. },
  2205. // abstract
  2206. opened: function () {
  2207. return this.container
  2208. ? this.container.hasClass( 'select2-dropdown-open' )
  2209. : false;
  2210. },
  2211. // abstract
  2212. positionDropdown: function () {
  2213. var $dropdown = this.dropdown,
  2214. container = this.container,
  2215. offset = container.offset(),
  2216. height = container.outerHeight( false ),
  2217. width = container.outerWidth( false ),
  2218. dropHeight = $dropdown.outerHeight( false ),
  2219. $window = $( window ),
  2220. windowWidth = $window.width(),
  2221. windowHeight = $window.height(),
  2222. viewPortRight = $window.scrollLeft() + windowWidth,
  2223. viewportBottom = $window.scrollTop() + windowHeight,
  2224. dropTop = offset.top + height,
  2225. dropLeft = offset.left,
  2226. enoughRoomBelow = dropTop + dropHeight <= viewportBottom,
  2227. enoughRoomAbove =
  2228. offset.top - dropHeight >= $window.scrollTop(),
  2229. dropWidth = $dropdown.outerWidth( false ),
  2230. enoughRoomOnRight = function () {
  2231. return dropLeft + dropWidth <= viewPortRight;
  2232. },
  2233. enoughRoomOnLeft = function () {
  2234. return (
  2235. offset.left +
  2236. viewPortRight +
  2237. container.outerWidth( false ) >
  2238. dropWidth
  2239. );
  2240. },
  2241. aboveNow = $dropdown.hasClass( 'select2-drop-above' ),
  2242. bodyOffset,
  2243. above,
  2244. changeDirection,
  2245. css,
  2246. resultsListNode;
  2247. // always prefer the current above/below alignment, unless there is not enough room
  2248. if ( aboveNow ) {
  2249. above = true;
  2250. if ( ! enoughRoomAbove && enoughRoomBelow ) {
  2251. changeDirection = true;
  2252. above = false;
  2253. }
  2254. } else {
  2255. above = false;
  2256. if ( ! enoughRoomBelow && enoughRoomAbove ) {
  2257. changeDirection = true;
  2258. above = true;
  2259. }
  2260. }
  2261. //if we are changing direction we need to get positions when dropdown is hidden;
  2262. if ( changeDirection ) {
  2263. $dropdown.hide();
  2264. offset = this.container.offset();
  2265. height = this.container.outerHeight( false );
  2266. width = this.container.outerWidth( false );
  2267. dropHeight = $dropdown.outerHeight( false );
  2268. viewPortRight = $window.scrollLeft() + windowWidth;
  2269. viewportBottom = $window.scrollTop() + windowHeight;
  2270. dropTop = offset.top + height;
  2271. dropLeft = offset.left;
  2272. dropWidth = $dropdown.outerWidth( false );
  2273. $dropdown.show();
  2274. // fix so the cursor does not move to the left within the search-textbox in IE
  2275. this.focusSearch();
  2276. }
  2277. if ( this.opts.dropdownAutoWidth ) {
  2278. resultsListNode = $( '.select2-results', $dropdown )[ 0 ];
  2279. $dropdown.addClass( 'select2-drop-auto-width' );
  2280. $dropdown.css( 'width', '' );
  2281. // Add scrollbar width to dropdown if vertical scrollbar is present
  2282. dropWidth =
  2283. $dropdown.outerWidth( false ) +
  2284. ( resultsListNode.scrollHeight ===
  2285. resultsListNode.clientHeight
  2286. ? 0
  2287. : scrollBarDimensions.width );
  2288. dropWidth > width
  2289. ? ( width = dropWidth )
  2290. : ( dropWidth = width );
  2291. dropHeight = $dropdown.outerHeight( false );
  2292. } else {
  2293. this.container.removeClass( 'select2-drop-auto-width' );
  2294. }
  2295. //console.log("below/ droptop:", dropTop, "dropHeight", dropHeight, "sum", (dropTop+dropHeight)+" viewport bottom", viewportBottom, "enough?", enoughRoomBelow);
  2296. //console.log("above/ offset.top", offset.top, "dropHeight", dropHeight, "top", (offset.top-dropHeight), "scrollTop", this.body.scrollTop(), "enough?", enoughRoomAbove);
  2297. // fix positioning when body has an offset and is not position: static
  2298. if ( this.body.css( 'position' ) !== 'static' ) {
  2299. bodyOffset = this.body.offset();
  2300. dropTop -= bodyOffset.top;
  2301. dropLeft -= bodyOffset.left;
  2302. }
  2303. if ( ! enoughRoomOnRight() && enoughRoomOnLeft() ) {
  2304. dropLeft =
  2305. offset.left +
  2306. this.container.outerWidth( false ) -
  2307. dropWidth;
  2308. }
  2309. css = {
  2310. left: dropLeft,
  2311. width: width,
  2312. };
  2313. if ( above ) {
  2314. css.top = offset.top - dropHeight;
  2315. css.bottom = 'auto';
  2316. this.container.addClass( 'select2-drop-above' );
  2317. $dropdown.addClass( 'select2-drop-above' );
  2318. } else {
  2319. css.top = dropTop;
  2320. css.bottom = 'auto';
  2321. this.container.removeClass( 'select2-drop-above' );
  2322. $dropdown.removeClass( 'select2-drop-above' );
  2323. }
  2324. css = $.extend(
  2325. css,
  2326. evaluate( this.opts.dropdownCss, this.opts.element )
  2327. );
  2328. $dropdown.css( css );
  2329. },
  2330. // abstract
  2331. shouldOpen: function () {
  2332. var event;
  2333. if ( this.opened() ) return false;
  2334. if ( this._enabled === false || this._readonly === true )
  2335. return false;
  2336. event = $.Event( 'select2-opening' );
  2337. this.opts.element.trigger( event );
  2338. return ! event.isDefaultPrevented();
  2339. },
  2340. // abstract
  2341. clearDropdownAlignmentPreference: function () {
  2342. // clear the classes used to figure out the preference of where the dropdown should be opened
  2343. this.container.removeClass( 'select2-drop-above' );
  2344. this.dropdown.removeClass( 'select2-drop-above' );
  2345. },
  2346. /**
  2347. * Opens the dropdown
  2348. *
  2349. * @return {Boolean} whether or not dropdown was opened. This method will return false if, for example,
  2350. * the dropdown is already open, or if the 'open' event listener on the element called preventDefault().
  2351. */
  2352. // abstract
  2353. open: function () {
  2354. if ( ! this.shouldOpen() ) return false;
  2355. this.opening();
  2356. // Only bind the document mousemove when the dropdown is visible
  2357. $document.on( 'mousemove.select2Event', function ( e ) {
  2358. lastMousePosition.x = e.pageX;
  2359. lastMousePosition.y = e.pageY;
  2360. } );
  2361. return true;
  2362. },
  2363. /**
  2364. * Performs the opening of the dropdown
  2365. */
  2366. // abstract
  2367. opening: function () {
  2368. var cid = this.containerEventName,
  2369. scroll = 'scroll.' + cid,
  2370. resize = 'resize.' + cid,
  2371. orient = 'orientationchange.' + cid,
  2372. mask;
  2373. this.container
  2374. .addClass( 'select2-dropdown-open' )
  2375. .addClass( 'select2-container-active' );
  2376. this.clearDropdownAlignmentPreference();
  2377. if ( this.dropdown[ 0 ] !== this.body.children().last()[ 0 ] ) {
  2378. this.dropdown.detach().appendTo( this.body );
  2379. }
  2380. // create the dropdown mask if doesn't already exist
  2381. mask = $( '#select2-drop-mask' );
  2382. if ( mask.length === 0 ) {
  2383. mask = $( document.createElement( 'div' ) );
  2384. mask.attr( 'id', 'select2-drop-mask' ).attr(
  2385. 'class',
  2386. 'select2-drop-mask'
  2387. );
  2388. mask.hide();
  2389. mask.appendTo( this.body );
  2390. mask.on( 'mousedown touchstart click', function ( e ) {
  2391. // Prevent IE from generating a click event on the body
  2392. reinsertElement( mask );
  2393. var dropdown = $( '#select2-drop' ),
  2394. self;
  2395. if ( dropdown.length > 0 ) {
  2396. self = dropdown.data( 'select2' );
  2397. if ( self.opts.selectOnBlur ) {
  2398. self.selectHighlighted( { noFocus: true } );
  2399. }
  2400. self.close();
  2401. e.preventDefault();
  2402. e.stopPropagation();
  2403. }
  2404. } );
  2405. }
  2406. // ensure the mask is always right before the dropdown
  2407. if ( this.dropdown.prev()[ 0 ] !== mask[ 0 ] ) {
  2408. this.dropdown.before( mask );
  2409. }
  2410. // move the global id to the correct dropdown
  2411. $( '#select2-drop' ).removeAttr( 'id' );
  2412. this.dropdown.attr( 'id', 'select2-drop' );
  2413. // show the elements
  2414. mask.show();
  2415. this.positionDropdown();
  2416. this.dropdown.show();
  2417. this.positionDropdown();
  2418. this.dropdown.addClass( 'select2-drop-active' );
  2419. // attach listeners to events that can change the position of the container and thus require
  2420. // the position of the dropdown to be updated as well so it does not come unglued from the container
  2421. var that = this;
  2422. this.container
  2423. .parents()
  2424. .add( window )
  2425. .each( function () {
  2426. $( this ).on(
  2427. resize + ' ' + scroll + ' ' + orient,
  2428. function ( e ) {
  2429. if ( that.opened() ) that.positionDropdown();
  2430. }
  2431. );
  2432. } );
  2433. },
  2434. // abstract
  2435. close: function () {
  2436. if ( ! this.opened() ) return;
  2437. var cid = this.containerEventName,
  2438. scroll = 'scroll.' + cid,
  2439. resize = 'resize.' + cid,
  2440. orient = 'orientationchange.' + cid;
  2441. // unbind event listeners
  2442. this.container
  2443. .parents()
  2444. .add( window )
  2445. .each( function () {
  2446. $( this ).off( scroll ).off( resize ).off( orient );
  2447. } );
  2448. this.clearDropdownAlignmentPreference();
  2449. $( '#select2-drop-mask' ).hide();
  2450. this.dropdown.removeAttr( 'id' ); // only the active dropdown has the select2-drop id
  2451. this.dropdown.hide();
  2452. this.container
  2453. .removeClass( 'select2-dropdown-open' )
  2454. .removeClass( 'select2-container-active' );
  2455. this.results.empty();
  2456. // Now that the dropdown is closed, unbind the global document mousemove event
  2457. $document.off( 'mousemove.select2Event' );
  2458. this.clearSearch();
  2459. this.search.removeClass( 'select2-active' );
  2460. this.opts.element.trigger( $.Event( 'select2-close' ) );
  2461. },
  2462. /**
  2463. * Opens control, sets input value, and updates results.
  2464. */
  2465. // abstract
  2466. externalSearch: function ( term ) {
  2467. this.open();
  2468. this.search.val( term );
  2469. this.updateResults( false );
  2470. },
  2471. // abstract
  2472. clearSearch: function () {},
  2473. //abstract
  2474. getMaximumSelectionSize: function () {
  2475. return evaluate(
  2476. this.opts.maximumSelectionSize,
  2477. this.opts.element
  2478. );
  2479. },
  2480. // abstract
  2481. ensureHighlightVisible: function () {
  2482. var results = this.results,
  2483. children,
  2484. index,
  2485. child,
  2486. hb,
  2487. rb,
  2488. y,
  2489. more,
  2490. topOffset;
  2491. index = this.highlight();
  2492. if ( index < 0 ) return;
  2493. if ( index == 0 ) {
  2494. // if the first element is highlighted scroll all the way to the top,
  2495. // that way any unselectable headers above it will also be scrolled
  2496. // into view
  2497. results.scrollTop( 0 );
  2498. return;
  2499. }
  2500. children = this.findHighlightableChoices().find(
  2501. '.select2-result-label'
  2502. );
  2503. child = $( children[ index ] );
  2504. topOffset = ( child.offset() || {} ).top || 0;
  2505. hb = topOffset + child.outerHeight( true );
  2506. // if this is the last child lets also make sure select2-more-results is visible
  2507. if ( index === children.length - 1 ) {
  2508. more = results.find( 'li.select2-more-results' );
  2509. if ( more.length > 0 ) {
  2510. hb = more.offset().top + more.outerHeight( true );
  2511. }
  2512. }
  2513. rb = results.offset().top + results.outerHeight( false );
  2514. if ( hb > rb ) {
  2515. results.scrollTop( results.scrollTop() + ( hb - rb ) );
  2516. }
  2517. y = topOffset - results.offset().top;
  2518. // make sure the top of the element is visible
  2519. if ( y < 0 && child.css( 'display' ) != 'none' ) {
  2520. results.scrollTop( results.scrollTop() + y ); // y is negative
  2521. }
  2522. },
  2523. // abstract
  2524. findHighlightableChoices: function () {
  2525. return this.results.find(
  2526. '.select2-result-selectable:not(.select2-disabled):not(.select2-selected)'
  2527. );
  2528. },
  2529. // abstract
  2530. moveHighlight: function ( delta ) {
  2531. var choices = this.findHighlightableChoices(),
  2532. index = this.highlight();
  2533. while ( index > -1 && index < choices.length ) {
  2534. index += delta;
  2535. var choice = $( choices[ index ] );
  2536. if (
  2537. choice.hasClass( 'select2-result-selectable' ) &&
  2538. ! choice.hasClass( 'select2-disabled' ) &&
  2539. ! choice.hasClass( 'select2-selected' )
  2540. ) {
  2541. this.highlight( index );
  2542. break;
  2543. }
  2544. }
  2545. },
  2546. // abstract
  2547. highlight: function ( index ) {
  2548. var choices = this.findHighlightableChoices(),
  2549. choice,
  2550. data;
  2551. if ( arguments.length === 0 ) {
  2552. return indexOf(
  2553. choices.filter( '.select2-highlighted' )[ 0 ],
  2554. choices.get()
  2555. );
  2556. }
  2557. if ( index >= choices.length ) index = choices.length - 1;
  2558. if ( index < 0 ) index = 0;
  2559. this.removeHighlight();
  2560. choice = $( choices[ index ] );
  2561. choice.addClass( 'select2-highlighted' );
  2562. // ensure assistive technology can determine the active choice
  2563. this.search.attr(
  2564. 'aria-activedescendant',
  2565. choice.find( '.select2-result-label' ).attr( 'id' )
  2566. );
  2567. this.ensureHighlightVisible();
  2568. this.liveRegion.text( choice.text() );
  2569. data = choice.data( 'select2-data' );
  2570. if ( data ) {
  2571. this.opts.element.trigger( {
  2572. type: 'select2-highlight',
  2573. val: this.id( data ),
  2574. choice: data,
  2575. } );
  2576. }
  2577. },
  2578. removeHighlight: function () {
  2579. this.results
  2580. .find( '.select2-highlighted' )
  2581. .removeClass( 'select2-highlighted' );
  2582. },
  2583. touchMoved: function () {
  2584. this._touchMoved = true;
  2585. },
  2586. clearTouchMoved: function () {
  2587. this._touchMoved = false;
  2588. },
  2589. // abstract
  2590. countSelectableResults: function () {
  2591. return this.findHighlightableChoices().length;
  2592. },
  2593. // abstract
  2594. highlightUnderEvent: function ( event ) {
  2595. var el = $( event.target ).closest( '.select2-result-selectable' );
  2596. if ( el.length > 0 && ! el.is( '.select2-highlighted' ) ) {
  2597. var choices = this.findHighlightableChoices();
  2598. this.highlight( choices.index( el ) );
  2599. } else if ( el.length == 0 ) {
  2600. // if we are over an unselectable item remove all highlights
  2601. this.removeHighlight();
  2602. }
  2603. },
  2604. // abstract
  2605. loadMoreIfNeeded: function () {
  2606. var results = this.results,
  2607. more = results.find( 'li.select2-more-results' ),
  2608. below, // pixels the element is below the scroll fold, below==0 is when the element is starting to be visible
  2609. page = this.resultsPage + 1,
  2610. self = this,
  2611. term = this.search.val(),
  2612. context = this.context;
  2613. if ( more.length === 0 ) return;
  2614. below = more.offset().top - results.offset().top - results.height();
  2615. if ( below <= this.opts.loadMorePadding ) {
  2616. more.addClass( 'select2-active' );
  2617. this.opts.query( {
  2618. element: this.opts.element,
  2619. term: term,
  2620. page: page,
  2621. context: context,
  2622. matcher: this.opts.matcher,
  2623. callback: this.bind( function ( data ) {
  2624. // ignore a response if the select2 has been closed before it was received
  2625. if ( ! self.opened() ) return;
  2626. self.opts.populateResults.call(
  2627. this,
  2628. results,
  2629. data.results,
  2630. { term: term, page: page, context: context }
  2631. );
  2632. self.postprocessResults( data, false, false );
  2633. if ( data.more === true ) {
  2634. more.detach()
  2635. .appendTo( results )
  2636. .html(
  2637. self.opts.escapeMarkup(
  2638. evaluate(
  2639. self.opts.formatLoadMore,
  2640. self.opts.element,
  2641. page + 1
  2642. )
  2643. )
  2644. );
  2645. window.setTimeout( function () {
  2646. self.loadMoreIfNeeded();
  2647. }, 10 );
  2648. } else {
  2649. more.remove();
  2650. }
  2651. self.positionDropdown();
  2652. self.resultsPage = page;
  2653. self.context = data.context;
  2654. this.opts.element.trigger( {
  2655. type: 'select2-loaded',
  2656. items: data,
  2657. } );
  2658. } ),
  2659. } );
  2660. }
  2661. },
  2662. /**
  2663. * Default tokenizer function which does nothing
  2664. */
  2665. tokenize: function () {},
  2666. /**
  2667. * @param initial whether or not this is the call to this method right after the dropdown has been opened
  2668. */
  2669. // abstract
  2670. updateResults: function ( initial ) {
  2671. var search = this.search,
  2672. results = this.results,
  2673. opts = this.opts,
  2674. data,
  2675. self = this,
  2676. input,
  2677. term = search.val(),
  2678. lastTerm = $.data( this.container, 'select2-last-term' ),
  2679. // sequence number used to drop out-of-order responses
  2680. queryNumber;
  2681. // prevent duplicate queries against the same term
  2682. if ( initial !== true && lastTerm && equal( term, lastTerm ) )
  2683. return;
  2684. $.data( this.container, 'select2-last-term', term );
  2685. // if the search is currently hidden we do not alter the results
  2686. if (
  2687. initial !== true &&
  2688. ( this.showSearchInput === false || ! this.opened() )
  2689. ) {
  2690. return;
  2691. }
  2692. function postRender() {
  2693. search.removeClass( 'select2-active' );
  2694. self.positionDropdown();
  2695. if (
  2696. results.find(
  2697. '.select2-no-results,.select2-selection-limit,.select2-searching'
  2698. ).length
  2699. ) {
  2700. self.liveRegion.text( results.text() );
  2701. } else {
  2702. self.liveRegion.text(
  2703. self.opts.formatMatches(
  2704. results.find(
  2705. '.select2-result-selectable:not(".select2-selected")'
  2706. ).length
  2707. )
  2708. );
  2709. }
  2710. }
  2711. function render( html ) {
  2712. results.html( html );
  2713. postRender();
  2714. }
  2715. queryNumber = ++this.queryCount;
  2716. var maxSelSize = this.getMaximumSelectionSize();
  2717. if ( maxSelSize >= 1 ) {
  2718. data = this.data();
  2719. if (
  2720. $.isArray( data ) &&
  2721. data.length >= maxSelSize &&
  2722. checkFormatter(
  2723. opts.formatSelectionTooBig,
  2724. 'formatSelectionTooBig'
  2725. )
  2726. ) {
  2727. render(
  2728. "<li class='select2-selection-limit'>" +
  2729. evaluate(
  2730. opts.formatSelectionTooBig,
  2731. opts.element,
  2732. maxSelSize
  2733. ) +
  2734. '</li>'
  2735. );
  2736. return;
  2737. }
  2738. }
  2739. if ( search.val().length < opts.minimumInputLength ) {
  2740. if (
  2741. checkFormatter(
  2742. opts.formatInputTooShort,
  2743. 'formatInputTooShort'
  2744. )
  2745. ) {
  2746. render(
  2747. "<li class='select2-no-results'>" +
  2748. evaluate(
  2749. opts.formatInputTooShort,
  2750. opts.element,
  2751. search.val(),
  2752. opts.minimumInputLength
  2753. ) +
  2754. '</li>'
  2755. );
  2756. } else {
  2757. render( '' );
  2758. }
  2759. if ( initial && this.showSearch ) this.showSearch( true );
  2760. return;
  2761. }
  2762. if (
  2763. opts.maximumInputLength &&
  2764. search.val().length > opts.maximumInputLength
  2765. ) {
  2766. if (
  2767. checkFormatter(
  2768. opts.formatInputTooLong,
  2769. 'formatInputTooLong'
  2770. )
  2771. ) {
  2772. render(
  2773. "<li class='select2-no-results'>" +
  2774. evaluate(
  2775. opts.formatInputTooLong,
  2776. opts.element,
  2777. search.val(),
  2778. opts.maximumInputLength
  2779. ) +
  2780. '</li>'
  2781. );
  2782. } else {
  2783. render( '' );
  2784. }
  2785. return;
  2786. }
  2787. if (
  2788. opts.formatSearching &&
  2789. this.findHighlightableChoices().length === 0
  2790. ) {
  2791. render(
  2792. "<li class='select2-searching'>" +
  2793. evaluate( opts.formatSearching, opts.element ) +
  2794. '</li>'
  2795. );
  2796. }
  2797. search.addClass( 'select2-active' );
  2798. this.removeHighlight();
  2799. // give the tokenizer a chance to pre-process the input
  2800. input = this.tokenize();
  2801. if ( input != undefined && input != null ) {
  2802. search.val( input );
  2803. }
  2804. this.resultsPage = 1;
  2805. opts.query( {
  2806. element: opts.element,
  2807. term: search.val(),
  2808. page: this.resultsPage,
  2809. context: null,
  2810. matcher: opts.matcher,
  2811. callback: this.bind( function ( data ) {
  2812. var def; // default choice
  2813. // ignore old responses
  2814. if ( queryNumber != this.queryCount ) {
  2815. return;
  2816. }
  2817. // ignore a response if the select2 has been closed before it was received
  2818. if ( ! this.opened() ) {
  2819. this.search.removeClass( 'select2-active' );
  2820. return;
  2821. }
  2822. // handle ajax error
  2823. if (
  2824. data.hasError !== undefined &&
  2825. checkFormatter(
  2826. opts.formatAjaxError,
  2827. 'formatAjaxError'
  2828. )
  2829. ) {
  2830. render(
  2831. "<li class='select2-ajax-error'>" +
  2832. evaluate(
  2833. opts.formatAjaxError,
  2834. opts.element,
  2835. data.jqXHR,
  2836. data.textStatus,
  2837. data.errorThrown
  2838. ) +
  2839. '</li>'
  2840. );
  2841. return;
  2842. }
  2843. // save context, if any
  2844. this.context =
  2845. data.context === undefined ? null : data.context;
  2846. // create a default choice and prepend it to the list
  2847. if ( this.opts.createSearchChoice && search.val() !== '' ) {
  2848. def = this.opts.createSearchChoice.call(
  2849. self,
  2850. search.val(),
  2851. data.results
  2852. );
  2853. if (
  2854. def !== undefined &&
  2855. def !== null &&
  2856. self.id( def ) !== undefined &&
  2857. self.id( def ) !== null
  2858. ) {
  2859. if (
  2860. $( data.results ).filter( function () {
  2861. return equal(
  2862. self.id( this ),
  2863. self.id( def )
  2864. );
  2865. } ).length === 0
  2866. ) {
  2867. this.opts.createSearchChoicePosition(
  2868. data.results,
  2869. def
  2870. );
  2871. }
  2872. }
  2873. }
  2874. if (
  2875. data.results.length === 0 &&
  2876. checkFormatter(
  2877. opts.formatNoMatches,
  2878. 'formatNoMatches'
  2879. )
  2880. ) {
  2881. render(
  2882. "<li class='select2-no-results'>" +
  2883. evaluate(
  2884. opts.formatNoMatches,
  2885. opts.element,
  2886. search.val()
  2887. ) +
  2888. '</li>'
  2889. );
  2890. return;
  2891. }
  2892. results.empty();
  2893. self.opts.populateResults.call(
  2894. this,
  2895. results,
  2896. data.results,
  2897. {
  2898. term: search.val(),
  2899. page: this.resultsPage,
  2900. context: null,
  2901. }
  2902. );
  2903. if (
  2904. data.more === true &&
  2905. checkFormatter( opts.formatLoadMore, 'formatLoadMore' )
  2906. ) {
  2907. results.append(
  2908. "<li class='select2-more-results'>" +
  2909. opts.escapeMarkup(
  2910. evaluate(
  2911. opts.formatLoadMore,
  2912. opts.element,
  2913. this.resultsPage
  2914. )
  2915. ) +
  2916. '</li>'
  2917. );
  2918. window.setTimeout( function () {
  2919. self.loadMoreIfNeeded();
  2920. }, 10 );
  2921. }
  2922. this.postprocessResults( data, initial );
  2923. postRender();
  2924. this.opts.element.trigger( {
  2925. type: 'select2-loaded',
  2926. items: data,
  2927. } );
  2928. } ),
  2929. } );
  2930. },
  2931. // abstract
  2932. cancel: function () {
  2933. this.close();
  2934. },
  2935. // abstract
  2936. blur: function () {
  2937. // if selectOnBlur == true, select the currently highlighted option
  2938. if ( this.opts.selectOnBlur )
  2939. this.selectHighlighted( { noFocus: true } );
  2940. this.close();
  2941. this.container.removeClass( 'select2-container-active' );
  2942. // synonymous to .is(':focus'), which is available in jquery >= 1.6
  2943. if ( this.search[ 0 ] === document.activeElement ) {
  2944. this.search.blur();
  2945. }
  2946. this.clearSearch();
  2947. this.selection
  2948. .find( '.select2-search-choice-focus' )
  2949. .removeClass( 'select2-search-choice-focus' );
  2950. },
  2951. // abstract
  2952. focusSearch: function () {
  2953. focus( this.search );
  2954. },
  2955. // abstract
  2956. selectHighlighted: function ( options ) {
  2957. if ( this._touchMoved ) {
  2958. this.clearTouchMoved();
  2959. return;
  2960. }
  2961. var index = this.highlight(),
  2962. highlighted = this.results.find( '.select2-highlighted' ),
  2963. data = highlighted
  2964. .closest( '.select2-result' )
  2965. .data( 'select2-data' );
  2966. if ( data ) {
  2967. this.highlight( index );
  2968. this.onSelect( data, options );
  2969. } else if ( options && options.noFocus ) {
  2970. this.close();
  2971. }
  2972. },
  2973. // abstract
  2974. getPlaceholder: function () {
  2975. var placeholderOption;
  2976. return (
  2977. this.opts.element.attr( 'placeholder' ) ||
  2978. this.opts.element.attr( 'data-placeholder' ) || // jquery 1.4 compat
  2979. this.opts.element.data( 'placeholder' ) ||
  2980. this.opts.placeholder ||
  2981. ( ( placeholderOption = this.getPlaceholderOption() ) !==
  2982. undefined
  2983. ? placeholderOption.text()
  2984. : undefined )
  2985. );
  2986. },
  2987. // abstract
  2988. getPlaceholderOption: function () {
  2989. if ( this.select ) {
  2990. var firstOption = this.select.children( 'option' ).first();
  2991. if ( this.opts.placeholderOption !== undefined ) {
  2992. //Determine the placeholder option based on the specified placeholderOption setting
  2993. return (
  2994. ( this.opts.placeholderOption === 'first' &&
  2995. firstOption ) ||
  2996. ( typeof this.opts.placeholderOption === 'function' &&
  2997. this.opts.placeholderOption( this.select ) )
  2998. );
  2999. } else if (
  3000. $.trim( firstOption.text() ) === '' &&
  3001. firstOption.val() === ''
  3002. ) {
  3003. //No explicit placeholder option specified, use the first if it's blank
  3004. return firstOption;
  3005. }
  3006. }
  3007. },
  3008. /**
  3009. * Get the desired width for the container element. This is
  3010. * derived first from option `width` passed to select2, then
  3011. * the inline 'style' on the original element, and finally
  3012. * falls back to the jQuery calculated element width.
  3013. */
  3014. // abstract
  3015. initContainerWidth: function () {
  3016. function resolveContainerWidth() {
  3017. var style, attrs, matches, i, l, attr;
  3018. if ( this.opts.width === 'off' ) {
  3019. return null;
  3020. } else if ( this.opts.width === 'element' ) {
  3021. return this.opts.element.outerWidth( false ) === 0
  3022. ? 'auto'
  3023. : this.opts.element.outerWidth( false ) + 'px';
  3024. } else if (
  3025. this.opts.width === 'copy' ||
  3026. this.opts.width === 'resolve'
  3027. ) {
  3028. // check if there is inline style on the element that contains width
  3029. style = this.opts.element.attr( 'style' );
  3030. if ( style !== undefined ) {
  3031. attrs = style.split( ';' );
  3032. for ( i = 0, l = attrs.length; i < l; i = i + 1 ) {
  3033. attr = attrs[ i ].replace( /\s/g, '' );
  3034. matches = attr.match(
  3035. /^width:(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc))/i
  3036. );
  3037. if ( matches !== null && matches.length >= 1 )
  3038. return matches[ 1 ];
  3039. }
  3040. }
  3041. if ( this.opts.width === 'resolve' ) {
  3042. // next check if css('width') can resolve a width that is percent based, this is sometimes possible
  3043. // when attached to input type=hidden or elements hidden via css
  3044. style = this.opts.element.css( 'width' );
  3045. if ( style.indexOf( '%' ) > 0 ) return style;
  3046. // finally, fallback on the calculated width of the element
  3047. return this.opts.element.outerWidth( false ) === 0
  3048. ? 'auto'
  3049. : this.opts.element.outerWidth( false ) + 'px';
  3050. }
  3051. return null;
  3052. } else if ( $.isFunction( this.opts.width ) ) {
  3053. return this.opts.width();
  3054. } else {
  3055. return this.opts.width;
  3056. }
  3057. }
  3058. var width = resolveContainerWidth.call( this );
  3059. if ( width !== null ) {
  3060. this.container.css( 'width', width );
  3061. }
  3062. },
  3063. } );
  3064. SingleSelect2 = clazz( AbstractSelect2, {
  3065. // single
  3066. createContainer: function () {
  3067. var container = $( document.createElement( 'div' ) )
  3068. .attr( {
  3069. class: 'select2-container',
  3070. } )
  3071. .html(
  3072. [
  3073. "<a href='javascript:void(0)' class='select2-choice' tabindex='-1'>",
  3074. " <span class='select2-chosen'>&#160;</span><abbr class='select2-search-choice-close'></abbr>",
  3075. " <span class='select2-arrow' role='presentation'><b role='presentation'></b></span>",
  3076. '</a>',
  3077. "<label for='' class='select2-offscreen'></label>",
  3078. "<input class='select2-focusser select2-offscreen' type='text' aria-haspopup='true' role='button' />",
  3079. "<div class='select2-drop select2-display-none'>",
  3080. " <div class='select2-search'>",
  3081. " <label for='' class='select2-offscreen'></label>",
  3082. " <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input' role='combobox' aria-expanded='true'",
  3083. " aria-autocomplete='list' />",
  3084. ' </div>',
  3085. " <ul class='select2-results' role='listbox'>",
  3086. ' </ul>',
  3087. '</div>',
  3088. ].join( '' )
  3089. );
  3090. return container;
  3091. },
  3092. // single
  3093. enableInterface: function () {
  3094. if ( this.parent.enableInterface.apply( this, arguments ) ) {
  3095. this.focusser.prop( 'disabled', ! this.isInterfaceEnabled() );
  3096. }
  3097. },
  3098. // single
  3099. opening: function () {
  3100. var el, range, len;
  3101. if ( this.opts.minimumResultsForSearch >= 0 ) {
  3102. this.showSearch( true );
  3103. }
  3104. this.parent.opening.apply( this, arguments );
  3105. if ( this.showSearchInput !== false ) {
  3106. // IE appends focusser.val() at the end of field :/ so we manually insert it at the beginning using a range
  3107. // all other browsers handle this just fine
  3108. this.search.val( this.focusser.val() );
  3109. }
  3110. if ( this.opts.shouldFocusInput( this ) ) {
  3111. this.search.focus();
  3112. // move the cursor to the end after focussing, otherwise it will be at the beginning and
  3113. // new text will appear *before* focusser.val()
  3114. el = this.search.get( 0 );
  3115. if ( el.createTextRange ) {
  3116. range = el.createTextRange();
  3117. range.collapse( false );
  3118. range.select();
  3119. } else if ( el.setSelectionRange ) {
  3120. len = this.search.val().length;
  3121. el.setSelectionRange( len, len );
  3122. }
  3123. }
  3124. // initializes search's value with nextSearchTerm (if defined by user)
  3125. // ignore nextSearchTerm if the dropdown is opened by the user pressing a letter
  3126. if ( this.search.val() === '' ) {
  3127. if ( this.nextSearchTerm != undefined ) {
  3128. this.search.val( this.nextSearchTerm );
  3129. this.search.select();
  3130. }
  3131. }
  3132. this.focusser.prop( 'disabled', true ).val( '' );
  3133. this.updateResults( true );
  3134. this.opts.element.trigger( $.Event( 'select2-open' ) );
  3135. },
  3136. // single
  3137. close: function () {
  3138. if ( ! this.opened() ) return;
  3139. this.parent.close.apply( this, arguments );
  3140. this.focusser.prop( 'disabled', false );
  3141. if ( this.opts.shouldFocusInput( this ) ) {
  3142. this.focusser.focus();
  3143. }
  3144. },
  3145. // single
  3146. focus: function () {
  3147. if ( this.opened() ) {
  3148. this.close();
  3149. } else {
  3150. this.focusser.prop( 'disabled', false );
  3151. if ( this.opts.shouldFocusInput( this ) ) {
  3152. this.focusser.focus();
  3153. }
  3154. }
  3155. },
  3156. // single
  3157. isFocused: function () {
  3158. return this.container.hasClass( 'select2-container-active' );
  3159. },
  3160. // single
  3161. cancel: function () {
  3162. this.parent.cancel.apply( this, arguments );
  3163. this.focusser.prop( 'disabled', false );
  3164. if ( this.opts.shouldFocusInput( this ) ) {
  3165. this.focusser.focus();
  3166. }
  3167. },
  3168. // single
  3169. destroy: function () {
  3170. $( "label[for='" + this.focusser.attr( 'id' ) + "']" ).attr(
  3171. 'for',
  3172. this.opts.element.attr( 'id' )
  3173. );
  3174. this.parent.destroy.apply( this, arguments );
  3175. cleanupJQueryElements.call( this, 'selection', 'focusser' );
  3176. },
  3177. // single
  3178. initContainer: function () {
  3179. var selection,
  3180. container = this.container,
  3181. dropdown = this.dropdown,
  3182. idSuffix = nextUid(),
  3183. elementLabel;
  3184. if ( this.opts.minimumResultsForSearch < 0 ) {
  3185. this.showSearch( false );
  3186. } else {
  3187. this.showSearch( true );
  3188. }
  3189. this.selection = selection = container.find( '.select2-choice' );
  3190. this.focusser = container.find( '.select2-focusser' );
  3191. // add aria associations
  3192. selection
  3193. .find( '.select2-chosen' )
  3194. .attr( 'id', 'select2-chosen-' + idSuffix );
  3195. this.focusser.attr(
  3196. 'aria-labelledby',
  3197. 'select2-chosen-' + idSuffix
  3198. );
  3199. this.results.attr( 'id', 'select2-results-' + idSuffix );
  3200. this.search.attr( 'aria-owns', 'select2-results-' + idSuffix );
  3201. // rewrite labels from original element to focusser
  3202. this.focusser.attr( 'id', 's2id_autogen' + idSuffix );
  3203. elementLabel = $(
  3204. "label[for='" + this.opts.element.attr( 'id' ) + "']"
  3205. );
  3206. this.opts.element.focus(
  3207. this.bind( function () {
  3208. this.focus();
  3209. } )
  3210. );
  3211. this.focusser
  3212. .prev()
  3213. .text( elementLabel.text() )
  3214. .attr( 'for', this.focusser.attr( 'id' ) );
  3215. // Ensure the original element retains an accessible name
  3216. var originalTitle = this.opts.element.attr( 'title' );
  3217. this.opts.element.attr(
  3218. 'title',
  3219. originalTitle || elementLabel.text()
  3220. );
  3221. this.focusser.attr( 'tabindex', this.elementTabIndex );
  3222. // write label for search field using the label from the focusser element
  3223. this.search.attr( 'id', this.focusser.attr( 'id' ) + '_search' );
  3224. this.search
  3225. .prev()
  3226. .text(
  3227. $(
  3228. "label[for='" + this.focusser.attr( 'id' ) + "']"
  3229. ).text()
  3230. )
  3231. .attr( 'for', this.search.attr( 'id' ) );
  3232. this.search.on(
  3233. 'keydown',
  3234. this.bind( function ( e ) {
  3235. if ( ! this.isInterfaceEnabled() ) return;
  3236. // filter 229 keyCodes (input method editor is processing key input)
  3237. if ( 229 == e.keyCode ) return;
  3238. if (
  3239. e.which === KEY.PAGE_UP ||
  3240. e.which === KEY.PAGE_DOWN
  3241. ) {
  3242. // prevent the page from scrolling
  3243. killEvent( e );
  3244. return;
  3245. }
  3246. switch ( e.which ) {
  3247. case KEY.UP:
  3248. case KEY.DOWN:
  3249. this.moveHighlight( e.which === KEY.UP ? -1 : 1 );
  3250. killEvent( e );
  3251. return;
  3252. case KEY.ENTER:
  3253. this.selectHighlighted();
  3254. killEvent( e );
  3255. return;
  3256. case KEY.TAB:
  3257. this.selectHighlighted( { noFocus: true } );
  3258. return;
  3259. case KEY.ESC:
  3260. this.cancel( e );
  3261. killEvent( e );
  3262. return;
  3263. }
  3264. } )
  3265. );
  3266. this.search.on(
  3267. 'blur',
  3268. this.bind( function ( e ) {
  3269. // a workaround for chrome to keep the search field focussed when the scroll bar is used to scroll the dropdown.
  3270. // without this the search field loses focus which is annoying
  3271. if ( document.activeElement === this.body.get( 0 ) ) {
  3272. window.setTimeout(
  3273. this.bind( function () {
  3274. if ( this.opened() ) {
  3275. this.search.focus();
  3276. }
  3277. } ),
  3278. 0
  3279. );
  3280. }
  3281. } )
  3282. );
  3283. this.focusser.on(
  3284. 'keydown',
  3285. this.bind( function ( e ) {
  3286. if ( ! this.isInterfaceEnabled() ) return;
  3287. if (
  3288. e.which === KEY.TAB ||
  3289. KEY.isControl( e ) ||
  3290. KEY.isFunctionKey( e ) ||
  3291. e.which === KEY.ESC
  3292. ) {
  3293. return;
  3294. }
  3295. if (
  3296. this.opts.openOnEnter === false &&
  3297. e.which === KEY.ENTER
  3298. ) {
  3299. killEvent( e );
  3300. return;
  3301. }
  3302. if (
  3303. e.which == KEY.DOWN ||
  3304. e.which == KEY.UP ||
  3305. ( e.which == KEY.ENTER && this.opts.openOnEnter )
  3306. ) {
  3307. if ( e.altKey || e.ctrlKey || e.shiftKey || e.metaKey )
  3308. return;
  3309. this.open();
  3310. killEvent( e );
  3311. return;
  3312. }
  3313. if ( e.which == KEY.DELETE || e.which == KEY.BACKSPACE ) {
  3314. if ( this.opts.allowClear ) {
  3315. this.clear();
  3316. }
  3317. killEvent( e );
  3318. return;
  3319. }
  3320. } )
  3321. );
  3322. installKeyUpChangeEvent( this.focusser );
  3323. this.focusser.on(
  3324. 'keyup-change input',
  3325. this.bind( function ( e ) {
  3326. if ( this.opts.minimumResultsForSearch >= 0 ) {
  3327. e.stopPropagation();
  3328. if ( this.opened() ) return;
  3329. this.open();
  3330. }
  3331. } )
  3332. );
  3333. selection.on(
  3334. 'mousedown touchstart',
  3335. 'abbr',
  3336. this.bind( function ( e ) {
  3337. if ( ! this.isInterfaceEnabled() ) {
  3338. return;
  3339. }
  3340. this.clear();
  3341. killEventImmediately( e );
  3342. this.close();
  3343. if ( this.selection ) {
  3344. this.selection.focus();
  3345. }
  3346. } )
  3347. );
  3348. selection.on(
  3349. 'mousedown touchstart',
  3350. this.bind( function ( e ) {
  3351. // Prevent IE from generating a click event on the body
  3352. reinsertElement( selection );
  3353. if (
  3354. ! this.container.hasClass( 'select2-container-active' )
  3355. ) {
  3356. this.opts.element.trigger( $.Event( 'select2-focus' ) );
  3357. }
  3358. if ( this.opened() ) {
  3359. this.close();
  3360. } else if ( this.isInterfaceEnabled() ) {
  3361. this.open();
  3362. }
  3363. killEvent( e );
  3364. } )
  3365. );
  3366. dropdown.on(
  3367. 'mousedown touchstart',
  3368. this.bind( function () {
  3369. if ( this.opts.shouldFocusInput( this ) ) {
  3370. this.search.focus();
  3371. }
  3372. } )
  3373. );
  3374. selection.on(
  3375. 'focus',
  3376. this.bind( function ( e ) {
  3377. killEvent( e );
  3378. } )
  3379. );
  3380. this.focusser
  3381. .on(
  3382. 'focus',
  3383. this.bind( function () {
  3384. if (
  3385. ! this.container.hasClass(
  3386. 'select2-container-active'
  3387. )
  3388. ) {
  3389. this.opts.element.trigger(
  3390. $.Event( 'select2-focus' )
  3391. );
  3392. }
  3393. this.container.addClass( 'select2-container-active' );
  3394. } )
  3395. )
  3396. .on(
  3397. 'blur',
  3398. this.bind( function () {
  3399. if ( ! this.opened() ) {
  3400. this.container.removeClass(
  3401. 'select2-container-active'
  3402. );
  3403. this.opts.element.trigger(
  3404. $.Event( 'select2-blur' )
  3405. );
  3406. }
  3407. } )
  3408. );
  3409. this.search.on(
  3410. 'focus',
  3411. this.bind( function () {
  3412. if (
  3413. ! this.container.hasClass( 'select2-container-active' )
  3414. ) {
  3415. this.opts.element.trigger( $.Event( 'select2-focus' ) );
  3416. }
  3417. this.container.addClass( 'select2-container-active' );
  3418. } )
  3419. );
  3420. this.initContainerWidth();
  3421. this.opts.element.hide();
  3422. this.setPlaceholder();
  3423. },
  3424. // single
  3425. clear: function ( triggerChange ) {
  3426. var data = this.selection.data( 'select2-data' );
  3427. if ( data ) {
  3428. // guard against queued quick consecutive clicks
  3429. var evt = $.Event( 'select2-clearing' );
  3430. this.opts.element.trigger( evt );
  3431. if ( evt.isDefaultPrevented() ) {
  3432. return;
  3433. }
  3434. var placeholderOption = this.getPlaceholderOption();
  3435. this.opts.element.val(
  3436. placeholderOption ? placeholderOption.val() : ''
  3437. );
  3438. this.selection.find( '.select2-chosen' ).empty();
  3439. this.selection.removeData( 'select2-data' );
  3440. this.setPlaceholder();
  3441. if ( triggerChange !== false ) {
  3442. this.opts.element.trigger( {
  3443. type: 'select2-removed',
  3444. val: this.id( data ),
  3445. choice: data,
  3446. } );
  3447. this.triggerChange( { removed: data } );
  3448. }
  3449. }
  3450. },
  3451. /**
  3452. * Sets selection based on source element's value
  3453. */
  3454. // single
  3455. initSelection: function () {
  3456. var selected;
  3457. if ( this.isPlaceholderOptionSelected() ) {
  3458. this.updateSelection( null );
  3459. this.close();
  3460. this.setPlaceholder();
  3461. } else {
  3462. var self = this;
  3463. this.opts.initSelection.call(
  3464. null,
  3465. this.opts.element,
  3466. function ( selected ) {
  3467. if ( selected !== undefined && selected !== null ) {
  3468. self.updateSelection( selected );
  3469. self.close();
  3470. self.setPlaceholder();
  3471. self.nextSearchTerm = self.opts.nextSearchTerm(
  3472. selected,
  3473. self.search.val()
  3474. );
  3475. }
  3476. }
  3477. );
  3478. }
  3479. },
  3480. isPlaceholderOptionSelected: function () {
  3481. var placeholderOption;
  3482. if ( this.getPlaceholder() === undefined ) return false; // no placeholder specified so no option should be considered
  3483. return (
  3484. ( ( placeholderOption = this.getPlaceholderOption() ) !==
  3485. undefined &&
  3486. placeholderOption.prop( 'selected' ) ) ||
  3487. this.opts.element.val() === '' ||
  3488. this.opts.element.val() === undefined ||
  3489. this.opts.element.val() === null
  3490. );
  3491. },
  3492. // single
  3493. prepareOpts: function () {
  3494. var opts = this.parent.prepareOpts.apply( this, arguments ),
  3495. self = this;
  3496. if ( opts.element.get( 0 ).tagName.toLowerCase() === 'select' ) {
  3497. // install the selection initializer
  3498. opts.initSelection = function ( element, callback ) {
  3499. var selected = element
  3500. .find( 'option' )
  3501. .filter( function () {
  3502. return this.selected && ! this.disabled;
  3503. } );
  3504. // a single select box always has a value, no need to null check 'selected'
  3505. callback( self.optionToData( selected ) );
  3506. };
  3507. } else if ( 'data' in opts ) {
  3508. // install default initSelection when applied to hidden input and data is local
  3509. opts.initSelection =
  3510. opts.initSelection ||
  3511. function ( element, callback ) {
  3512. var id = element.val();
  3513. //search in data by id, storing the actual matching item
  3514. var match = null;
  3515. opts.query( {
  3516. matcher: function ( term, text, el ) {
  3517. var is_match = equal( id, opts.id( el ) );
  3518. if ( is_match ) {
  3519. match = el;
  3520. }
  3521. return is_match;
  3522. },
  3523. callback: ! $.isFunction( callback )
  3524. ? $.noop
  3525. : function () {
  3526. callback( match );
  3527. },
  3528. } );
  3529. };
  3530. }
  3531. return opts;
  3532. },
  3533. // single
  3534. getPlaceholder: function () {
  3535. // if a placeholder is specified on a single select without a valid placeholder option ignore it
  3536. if ( this.select ) {
  3537. if ( this.getPlaceholderOption() === undefined ) {
  3538. return undefined;
  3539. }
  3540. }
  3541. return this.parent.getPlaceholder.apply( this, arguments );
  3542. },
  3543. // single
  3544. setPlaceholder: function () {
  3545. var placeholder = this.getPlaceholder();
  3546. if (
  3547. this.isPlaceholderOptionSelected() &&
  3548. placeholder !== undefined
  3549. ) {
  3550. // check for a placeholder option if attached to a select
  3551. if ( this.select && this.getPlaceholderOption() === undefined )
  3552. return;
  3553. this.selection
  3554. .find( '.select2-chosen' )
  3555. .html( this.opts.escapeMarkup( placeholder ) );
  3556. this.selection.addClass( 'select2-default' );
  3557. this.container.removeClass( 'select2-allowclear' );
  3558. }
  3559. },
  3560. // single
  3561. postprocessResults: function ( data, initial, noHighlightUpdate ) {
  3562. var selected = 0,
  3563. self = this,
  3564. showSearchInput = true;
  3565. // find the selected element in the result list
  3566. this.findHighlightableChoices().each2( function ( i, elm ) {
  3567. if (
  3568. equal(
  3569. self.id( elm.data( 'select2-data' ) ),
  3570. self.opts.element.val()
  3571. )
  3572. ) {
  3573. selected = i;
  3574. return false;
  3575. }
  3576. } );
  3577. // and highlight it
  3578. if ( noHighlightUpdate !== false ) {
  3579. if ( initial === true && selected >= 0 ) {
  3580. this.highlight( selected );
  3581. } else {
  3582. this.highlight( 0 );
  3583. }
  3584. }
  3585. // hide the search box if this is the first we got the results and there are enough of them for search
  3586. if ( initial === true ) {
  3587. var min = this.opts.minimumResultsForSearch;
  3588. if ( min >= 0 ) {
  3589. this.showSearch( countResults( data.results ) >= min );
  3590. }
  3591. }
  3592. },
  3593. // single
  3594. showSearch: function ( showSearchInput ) {
  3595. if ( this.showSearchInput === showSearchInput ) return;
  3596. this.showSearchInput = showSearchInput;
  3597. this.dropdown
  3598. .find( '.select2-search' )
  3599. .toggleClass( 'select2-search-hidden', ! showSearchInput );
  3600. this.dropdown
  3601. .find( '.select2-search' )
  3602. .toggleClass( 'select2-offscreen', ! showSearchInput );
  3603. //add "select2-with-searchbox" to the container if search box is shown
  3604. $( this.dropdown, this.container ).toggleClass(
  3605. 'select2-with-searchbox',
  3606. showSearchInput
  3607. );
  3608. },
  3609. // single
  3610. onSelect: function ( data, options ) {
  3611. if ( ! this.triggerSelect( data ) ) {
  3612. return;
  3613. }
  3614. var old = this.opts.element.val(),
  3615. oldData = this.data();
  3616. this.opts.element.val( this.id( data ) );
  3617. this.updateSelection( data );
  3618. this.opts.element.trigger( {
  3619. type: 'select2-selected',
  3620. val: this.id( data ),
  3621. choice: data,
  3622. } );
  3623. this.nextSearchTerm = this.opts.nextSearchTerm(
  3624. data,
  3625. this.search.val()
  3626. );
  3627. this.close();
  3628. if (
  3629. ( ! options || ! options.noFocus ) &&
  3630. this.opts.shouldFocusInput( this )
  3631. ) {
  3632. this.focusser.focus();
  3633. }
  3634. if ( ! equal( old, this.id( data ) ) ) {
  3635. this.triggerChange( { added: data, removed: oldData } );
  3636. }
  3637. },
  3638. // single
  3639. updateSelection: function ( data ) {
  3640. var container = this.selection.find( '.select2-chosen' ),
  3641. formatted,
  3642. cssClass;
  3643. this.selection.data( 'select2-data', data );
  3644. container.empty();
  3645. if ( data !== null ) {
  3646. formatted = this.opts.formatSelection(
  3647. data,
  3648. container,
  3649. this.opts.escapeMarkup
  3650. );
  3651. }
  3652. if ( formatted !== undefined ) {
  3653. container.append( formatted );
  3654. }
  3655. cssClass = this.opts.formatSelectionCssClass( data, container );
  3656. if ( cssClass !== undefined ) {
  3657. container.addClass( cssClass );
  3658. }
  3659. this.selection.removeClass( 'select2-default' );
  3660. if ( this.opts.allowClear && this.getPlaceholder() !== undefined ) {
  3661. this.container.addClass( 'select2-allowclear' );
  3662. }
  3663. },
  3664. // single
  3665. val: function () {
  3666. var val,
  3667. triggerChange = false,
  3668. data = null,
  3669. self = this,
  3670. oldData = this.data();
  3671. if ( arguments.length === 0 ) {
  3672. return this.opts.element.val();
  3673. }
  3674. val = arguments[ 0 ];
  3675. if ( arguments.length > 1 ) {
  3676. triggerChange = arguments[ 1 ];
  3677. }
  3678. if ( this.select ) {
  3679. this.select
  3680. .val( val )
  3681. .find( 'option' )
  3682. .filter( function () {
  3683. return this.selected;
  3684. } )
  3685. .each2( function ( i, elm ) {
  3686. data = self.optionToData( elm );
  3687. return false;
  3688. } );
  3689. this.updateSelection( data );
  3690. this.setPlaceholder();
  3691. if ( triggerChange ) {
  3692. this.triggerChange( { added: data, removed: oldData } );
  3693. }
  3694. } else {
  3695. // val is an id. !val is true for [undefined,null,'',0] - 0 is legal
  3696. if ( ! val && val !== 0 ) {
  3697. this.clear( triggerChange );
  3698. return;
  3699. }
  3700. if ( this.opts.initSelection === undefined ) {
  3701. throw new Error(
  3702. 'cannot call val() if initSelection() is not defined'
  3703. );
  3704. }
  3705. this.opts.element.val( val );
  3706. this.opts.initSelection( this.opts.element, function ( data ) {
  3707. self.opts.element.val( ! data ? '' : self.id( data ) );
  3708. self.updateSelection( data );
  3709. self.setPlaceholder();
  3710. if ( triggerChange ) {
  3711. self.triggerChange( { added: data, removed: oldData } );
  3712. }
  3713. } );
  3714. }
  3715. },
  3716. // single
  3717. clearSearch: function () {
  3718. this.search.val( '' );
  3719. this.focusser.val( '' );
  3720. },
  3721. // single
  3722. data: function ( value ) {
  3723. var data,
  3724. triggerChange = false;
  3725. if ( arguments.length === 0 ) {
  3726. data = this.selection.data( 'select2-data' );
  3727. if ( data == undefined ) data = null;
  3728. return data;
  3729. } else {
  3730. if ( arguments.length > 1 ) {
  3731. triggerChange = arguments[ 1 ];
  3732. }
  3733. if ( ! value ) {
  3734. this.clear( triggerChange );
  3735. } else {
  3736. data = this.data();
  3737. this.opts.element.val( ! value ? '' : this.id( value ) );
  3738. this.updateSelection( value );
  3739. if ( triggerChange ) {
  3740. this.triggerChange( { added: value, removed: data } );
  3741. }
  3742. }
  3743. }
  3744. },
  3745. } );
  3746. MultiSelect2 = clazz( AbstractSelect2, {
  3747. // multi
  3748. createContainer: function () {
  3749. var container = $( document.createElement( 'div' ) )
  3750. .attr( {
  3751. class: 'select2-container select2-container-multi',
  3752. } )
  3753. .html(
  3754. [
  3755. "<ul class='select2-choices'>",
  3756. " <li class='select2-search-field'>",
  3757. " <label for='' class='select2-offscreen'></label>",
  3758. " <input type='text' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' class='select2-input'>",
  3759. ' </li>',
  3760. '</ul>',
  3761. "<div class='select2-drop select2-drop-multi select2-display-none'>",
  3762. " <ul class='select2-results'>",
  3763. ' </ul>',
  3764. '</div>',
  3765. ].join( '' )
  3766. );
  3767. return container;
  3768. },
  3769. // multi
  3770. prepareOpts: function () {
  3771. var opts = this.parent.prepareOpts.apply( this, arguments ),
  3772. self = this;
  3773. // TODO validate placeholder is a string if specified
  3774. if ( opts.element.get( 0 ).tagName.toLowerCase() === 'select' ) {
  3775. // install the selection initializer
  3776. opts.initSelection = function ( element, callback ) {
  3777. var data = [];
  3778. element
  3779. .find( 'option' )
  3780. .filter( function () {
  3781. return this.selected && ! this.disabled;
  3782. } )
  3783. .each2( function ( i, elm ) {
  3784. data.push( self.optionToData( elm ) );
  3785. } );
  3786. callback( data );
  3787. };
  3788. } else if ( 'data' in opts ) {
  3789. // install default initSelection when applied to hidden input and data is local
  3790. opts.initSelection =
  3791. opts.initSelection ||
  3792. function ( element, callback ) {
  3793. var ids = splitVal(
  3794. element.val(),
  3795. opts.separator,
  3796. opts.transformVal
  3797. );
  3798. //search in data by array of ids, storing matching items in a list
  3799. var matches = [];
  3800. opts.query( {
  3801. matcher: function ( term, text, el ) {
  3802. var is_match = $.grep( ids, function ( id ) {
  3803. return equal( id, opts.id( el ) );
  3804. } ).length;
  3805. if ( is_match ) {
  3806. matches.push( el );
  3807. }
  3808. return is_match;
  3809. },
  3810. callback: ! $.isFunction( callback )
  3811. ? $.noop
  3812. : function () {
  3813. // reorder matches based on the order they appear in the ids array because right now
  3814. // they are in the order in which they appear in data array
  3815. var ordered = [];
  3816. for ( var i = 0; i < ids.length; i++ ) {
  3817. var id = ids[ i ];
  3818. for (
  3819. var j = 0;
  3820. j < matches.length;
  3821. j++
  3822. ) {
  3823. var match = matches[ j ];
  3824. if (
  3825. equal(
  3826. id,
  3827. opts.id( match )
  3828. )
  3829. ) {
  3830. ordered.push( match );
  3831. matches.splice( j, 1 );
  3832. break;
  3833. }
  3834. }
  3835. }
  3836. callback( ordered );
  3837. },
  3838. } );
  3839. };
  3840. }
  3841. return opts;
  3842. },
  3843. // multi
  3844. selectChoice: function ( choice ) {
  3845. var selected = this.container.find(
  3846. '.select2-search-choice-focus'
  3847. );
  3848. if ( selected.length && choice && choice[ 0 ] == selected[ 0 ] ) {
  3849. } else {
  3850. if ( selected.length ) {
  3851. this.opts.element.trigger( 'choice-deselected', selected );
  3852. }
  3853. selected.removeClass( 'select2-search-choice-focus' );
  3854. if ( choice && choice.length ) {
  3855. this.close();
  3856. choice.addClass( 'select2-search-choice-focus' );
  3857. this.opts.element.trigger( 'choice-selected', choice );
  3858. }
  3859. }
  3860. },
  3861. // multi
  3862. destroy: function () {
  3863. $( "label[for='" + this.search.attr( 'id' ) + "']" ).attr(
  3864. 'for',
  3865. this.opts.element.attr( 'id' )
  3866. );
  3867. this.parent.destroy.apply( this, arguments );
  3868. cleanupJQueryElements.call( this, 'searchContainer', 'selection' );
  3869. },
  3870. // multi
  3871. initContainer: function () {
  3872. var selector = '.select2-choices',
  3873. selection;
  3874. this.searchContainer = this.container.find(
  3875. '.select2-search-field'
  3876. );
  3877. this.selection = selection = this.container.find( selector );
  3878. var _this = this;
  3879. this.selection.on(
  3880. 'click',
  3881. '.select2-container:not(.select2-container-disabled) .select2-search-choice:not(.select2-locked)',
  3882. function ( e ) {
  3883. _this.search[ 0 ].focus();
  3884. _this.selectChoice( $( this ) );
  3885. }
  3886. );
  3887. // rewrite labels from original element to focusser
  3888. this.search.attr( 'id', 's2id_autogen' + nextUid() );
  3889. this.search
  3890. .prev()
  3891. .text(
  3892. $(
  3893. "label[for='" + this.opts.element.attr( 'id' ) + "']"
  3894. ).text()
  3895. )
  3896. .attr( 'for', this.search.attr( 'id' ) );
  3897. this.opts.element.focus(
  3898. this.bind( function () {
  3899. this.focus();
  3900. } )
  3901. );
  3902. this.search.on(
  3903. 'input paste',
  3904. this.bind( function () {
  3905. if (
  3906. this.search.attr( 'placeholder' ) &&
  3907. this.search.val().length == 0
  3908. )
  3909. return;
  3910. if ( ! this.isInterfaceEnabled() ) return;
  3911. if ( ! this.opened() ) {
  3912. this.open();
  3913. }
  3914. } )
  3915. );
  3916. this.search.attr( 'tabindex', this.elementTabIndex );
  3917. this.keydowns = 0;
  3918. this.search.on(
  3919. 'keydown',
  3920. this.bind( function ( e ) {
  3921. if ( ! this.isInterfaceEnabled() ) return;
  3922. ++this.keydowns;
  3923. var selected = selection.find(
  3924. '.select2-search-choice-focus'
  3925. );
  3926. var prev = selected.prev(
  3927. '.select2-search-choice:not(.select2-locked)'
  3928. );
  3929. var next = selected.next(
  3930. '.select2-search-choice:not(.select2-locked)'
  3931. );
  3932. var pos = getCursorInfo( this.search );
  3933. if (
  3934. selected.length &&
  3935. ( e.which == KEY.LEFT ||
  3936. e.which == KEY.RIGHT ||
  3937. e.which == KEY.BACKSPACE ||
  3938. e.which == KEY.DELETE ||
  3939. e.which == KEY.ENTER )
  3940. ) {
  3941. var selectedChoice = selected;
  3942. if ( e.which == KEY.LEFT && prev.length ) {
  3943. selectedChoice = prev;
  3944. } else if ( e.which == KEY.RIGHT ) {
  3945. selectedChoice = next.length ? next : null;
  3946. } else if ( e.which === KEY.BACKSPACE ) {
  3947. if ( this.unselect( selected.first() ) ) {
  3948. this.search.width( 10 );
  3949. selectedChoice = prev.length ? prev : next;
  3950. }
  3951. } else if ( e.which == KEY.DELETE ) {
  3952. if ( this.unselect( selected.first() ) ) {
  3953. this.search.width( 10 );
  3954. selectedChoice = next.length ? next : null;
  3955. }
  3956. } else if ( e.which == KEY.ENTER ) {
  3957. selectedChoice = null;
  3958. }
  3959. this.selectChoice( selectedChoice );
  3960. killEvent( e );
  3961. if ( ! selectedChoice || ! selectedChoice.length ) {
  3962. this.open();
  3963. }
  3964. return;
  3965. } else if (
  3966. ( ( e.which === KEY.BACKSPACE && this.keydowns == 1 ) ||
  3967. e.which == KEY.LEFT ) &&
  3968. pos.offset == 0 &&
  3969. ! pos.length
  3970. ) {
  3971. this.selectChoice(
  3972. selection
  3973. .find(
  3974. '.select2-search-choice:not(.select2-locked)'
  3975. )
  3976. .last()
  3977. );
  3978. killEvent( e );
  3979. return;
  3980. } else {
  3981. this.selectChoice( null );
  3982. }
  3983. if ( this.opened() ) {
  3984. switch ( e.which ) {
  3985. case KEY.UP:
  3986. case KEY.DOWN:
  3987. this.moveHighlight(
  3988. e.which === KEY.UP ? -1 : 1
  3989. );
  3990. killEvent( e );
  3991. return;
  3992. case KEY.ENTER:
  3993. this.selectHighlighted();
  3994. killEvent( e );
  3995. return;
  3996. case KEY.TAB:
  3997. this.selectHighlighted( { noFocus: true } );
  3998. this.close();
  3999. return;
  4000. case KEY.ESC:
  4001. this.cancel( e );
  4002. killEvent( e );
  4003. return;
  4004. }
  4005. }
  4006. if (
  4007. e.which === KEY.TAB ||
  4008. KEY.isControl( e ) ||
  4009. KEY.isFunctionKey( e ) ||
  4010. e.which === KEY.BACKSPACE ||
  4011. e.which === KEY.ESC
  4012. ) {
  4013. return;
  4014. }
  4015. if ( e.which === KEY.ENTER ) {
  4016. if ( this.opts.openOnEnter === false ) {
  4017. return;
  4018. } else if (
  4019. e.altKey ||
  4020. e.ctrlKey ||
  4021. e.shiftKey ||
  4022. e.metaKey
  4023. ) {
  4024. return;
  4025. }
  4026. }
  4027. this.open();
  4028. if (
  4029. e.which === KEY.PAGE_UP ||
  4030. e.which === KEY.PAGE_DOWN
  4031. ) {
  4032. // prevent the page from scrolling
  4033. killEvent( e );
  4034. }
  4035. if ( e.which === KEY.ENTER ) {
  4036. // prevent form from being submitted
  4037. killEvent( e );
  4038. }
  4039. } )
  4040. );
  4041. this.search.on(
  4042. 'keyup',
  4043. this.bind( function ( e ) {
  4044. this.keydowns = 0;
  4045. this.resizeSearch();
  4046. } )
  4047. );
  4048. this.search.on(
  4049. 'blur',
  4050. this.bind( function ( e ) {
  4051. this.container.removeClass( 'select2-container-active' );
  4052. this.search.removeClass( 'select2-focused' );
  4053. this.selectChoice( null );
  4054. if ( ! this.opened() ) this.clearSearch();
  4055. e.stopImmediatePropagation();
  4056. this.opts.element.trigger( $.Event( 'select2-blur' ) );
  4057. } )
  4058. );
  4059. this.container.on(
  4060. 'click',
  4061. selector,
  4062. this.bind( function ( e ) {
  4063. if ( ! this.isInterfaceEnabled() ) return;
  4064. if (
  4065. $( e.target ).closest( '.select2-search-choice' )
  4066. .length > 0
  4067. ) {
  4068. // clicked inside a select2 search choice, do not open
  4069. return;
  4070. }
  4071. this.selectChoice( null );
  4072. this.clearPlaceholder();
  4073. if (
  4074. ! this.container.hasClass( 'select2-container-active' )
  4075. ) {
  4076. this.opts.element.trigger( $.Event( 'select2-focus' ) );
  4077. }
  4078. this.open();
  4079. this.focusSearch();
  4080. e.preventDefault();
  4081. } )
  4082. );
  4083. this.container.on(
  4084. 'focus',
  4085. selector,
  4086. this.bind( function () {
  4087. if ( ! this.isInterfaceEnabled() ) return;
  4088. if (
  4089. ! this.container.hasClass( 'select2-container-active' )
  4090. ) {
  4091. this.opts.element.trigger( $.Event( 'select2-focus' ) );
  4092. }
  4093. this.container.addClass( 'select2-container-active' );
  4094. this.dropdown.addClass( 'select2-drop-active' );
  4095. this.clearPlaceholder();
  4096. } )
  4097. );
  4098. this.initContainerWidth();
  4099. this.opts.element.hide();
  4100. // set the placeholder if necessary
  4101. this.clearSearch();
  4102. },
  4103. // multi
  4104. enableInterface: function () {
  4105. if ( this.parent.enableInterface.apply( this, arguments ) ) {
  4106. this.search.prop( 'disabled', ! this.isInterfaceEnabled() );
  4107. }
  4108. },
  4109. // multi
  4110. initSelection: function () {
  4111. var data;
  4112. if (
  4113. this.opts.element.val() === '' &&
  4114. this.opts.element.text() === ''
  4115. ) {
  4116. this.updateSelection( [] );
  4117. this.close();
  4118. // set the placeholder if necessary
  4119. this.clearSearch();
  4120. }
  4121. if ( this.select || this.opts.element.val() !== '' ) {
  4122. var self = this;
  4123. this.opts.initSelection.call(
  4124. null,
  4125. this.opts.element,
  4126. function ( data ) {
  4127. if ( data !== undefined && data !== null ) {
  4128. self.updateSelection( data );
  4129. self.close();
  4130. // set the placeholder if necessary
  4131. self.clearSearch();
  4132. }
  4133. }
  4134. );
  4135. }
  4136. },
  4137. // multi
  4138. clearSearch: function () {
  4139. var placeholder = this.getPlaceholder(),
  4140. maxWidth = this.getMaxSearchWidth();
  4141. if (
  4142. placeholder !== undefined &&
  4143. this.getVal().length === 0 &&
  4144. this.search.hasClass( 'select2-focused' ) === false
  4145. ) {
  4146. this.search.val( placeholder ).addClass( 'select2-default' );
  4147. // stretch the search box to full width of the container so as much of the placeholder is visible as possible
  4148. // we could call this.resizeSearch(), but we do not because that requires a sizer and we do not want to create one so early because of a firefox bug, see #944
  4149. this.search.width(
  4150. maxWidth > 0 ? maxWidth : this.container.css( 'width' )
  4151. );
  4152. } else {
  4153. this.search.val( '' ).width( 10 );
  4154. }
  4155. },
  4156. // multi
  4157. clearPlaceholder: function () {
  4158. if ( this.search.hasClass( 'select2-default' ) ) {
  4159. this.search.val( '' ).removeClass( 'select2-default' );
  4160. }
  4161. },
  4162. // multi
  4163. opening: function () {
  4164. this.clearPlaceholder(); // should be done before super so placeholder is not used to search
  4165. this.resizeSearch();
  4166. this.parent.opening.apply( this, arguments );
  4167. this.focusSearch();
  4168. // initializes search's value with nextSearchTerm (if defined by user)
  4169. // ignore nextSearchTerm if the dropdown is opened by the user pressing a letter
  4170. if ( this.search.val() === '' ) {
  4171. if ( this.nextSearchTerm != undefined ) {
  4172. this.search.val( this.nextSearchTerm );
  4173. this.search.select();
  4174. }
  4175. }
  4176. this.updateResults( true );
  4177. if ( this.opts.shouldFocusInput( this ) ) {
  4178. this.search.focus();
  4179. }
  4180. this.opts.element.trigger( $.Event( 'select2-open' ) );
  4181. },
  4182. // multi
  4183. close: function () {
  4184. if ( ! this.opened() ) return;
  4185. this.parent.close.apply( this, arguments );
  4186. },
  4187. // multi
  4188. focus: function () {
  4189. this.close();
  4190. this.search.focus();
  4191. },
  4192. // multi
  4193. isFocused: function () {
  4194. return this.search.hasClass( 'select2-focused' );
  4195. },
  4196. // multi
  4197. updateSelection: function ( data ) {
  4198. var ids = [],
  4199. filtered = [],
  4200. self = this;
  4201. // filter out duplicates
  4202. $( data ).each( function () {
  4203. if ( indexOf( self.id( this ), ids ) < 0 ) {
  4204. ids.push( self.id( this ) );
  4205. filtered.push( this );
  4206. }
  4207. } );
  4208. data = filtered;
  4209. this.selection.find( '.select2-search-choice' ).remove();
  4210. $( data ).each( function () {
  4211. self.addSelectedChoice( this );
  4212. } );
  4213. self.postprocessResults();
  4214. },
  4215. // multi
  4216. tokenize: function () {
  4217. var input = this.search.val();
  4218. input = this.opts.tokenizer.call(
  4219. this,
  4220. input,
  4221. this.data(),
  4222. this.bind( this.onSelect ),
  4223. this.opts
  4224. );
  4225. if ( input != null && input != undefined ) {
  4226. this.search.val( input );
  4227. if ( input.length > 0 ) {
  4228. this.open();
  4229. }
  4230. }
  4231. },
  4232. // multi
  4233. onSelect: function ( data, options ) {
  4234. if ( ! this.triggerSelect( data ) || data.text === '' ) {
  4235. return;
  4236. }
  4237. this.addSelectedChoice( data );
  4238. this.opts.element.trigger( {
  4239. type: 'selected',
  4240. val: this.id( data ),
  4241. choice: data,
  4242. } );
  4243. // keep track of the search's value before it gets cleared
  4244. this.nextSearchTerm = this.opts.nextSearchTerm(
  4245. data,
  4246. this.search.val()
  4247. );
  4248. this.clearSearch();
  4249. this.updateResults();
  4250. if ( this.select || ! this.opts.closeOnSelect )
  4251. this.postprocessResults(
  4252. data,
  4253. false,
  4254. this.opts.closeOnSelect === true
  4255. );
  4256. if ( this.opts.closeOnSelect ) {
  4257. this.close();
  4258. this.search.width( 10 );
  4259. } else {
  4260. if ( this.countSelectableResults() > 0 ) {
  4261. this.search.width( 10 );
  4262. this.resizeSearch();
  4263. if (
  4264. this.getMaximumSelectionSize() > 0 &&
  4265. this.val().length >= this.getMaximumSelectionSize()
  4266. ) {
  4267. // if we reached max selection size repaint the results so choices
  4268. // are replaced with the max selection reached message
  4269. this.updateResults( true );
  4270. } else {
  4271. // initializes search's value with nextSearchTerm and update search result
  4272. if ( this.nextSearchTerm != undefined ) {
  4273. this.search.val( this.nextSearchTerm );
  4274. this.updateResults();
  4275. this.search.select();
  4276. }
  4277. }
  4278. this.positionDropdown();
  4279. } else {
  4280. // if nothing left to select close
  4281. this.close();
  4282. this.search.width( 10 );
  4283. }
  4284. }
  4285. // since its not possible to select an element that has already been
  4286. // added we do not need to check if this is a new element before firing change
  4287. this.triggerChange( { added: data } );
  4288. if ( ! options || ! options.noFocus ) this.focusSearch();
  4289. },
  4290. // multi
  4291. cancel: function () {
  4292. this.close();
  4293. this.focusSearch();
  4294. },
  4295. addSelectedChoice: function ( data ) {
  4296. var enableChoice = ! data.locked,
  4297. enabledItem = $(
  4298. "<li class='select2-search-choice'>" +
  4299. ' <div></div>' +
  4300. " <a href='#' class='select2-search-choice-close' tabindex='-1'></a>" +
  4301. '</li>'
  4302. ),
  4303. disabledItem = $(
  4304. "<li class='select2-search-choice select2-locked'>" +
  4305. '<div></div>' +
  4306. '</li>'
  4307. );
  4308. var choice = enableChoice ? enabledItem : disabledItem,
  4309. id = this.id( data ),
  4310. val = this.getVal(),
  4311. formatted,
  4312. cssClass;
  4313. formatted = this.opts.formatSelection(
  4314. data,
  4315. choice.find( 'div' ),
  4316. this.opts.escapeMarkup
  4317. );
  4318. if ( formatted != undefined ) {
  4319. choice
  4320. .find( 'div' )
  4321. .replaceWith( $( '<div></div>' ).html( formatted ) );
  4322. }
  4323. cssClass = this.opts.formatSelectionCssClass(
  4324. data,
  4325. choice.find( 'div' )
  4326. );
  4327. if ( cssClass != undefined ) {
  4328. choice.addClass( cssClass );
  4329. }
  4330. if ( enableChoice ) {
  4331. choice
  4332. .find( '.select2-search-choice-close' )
  4333. .on( 'mousedown', killEvent )
  4334. .on(
  4335. 'click dblclick',
  4336. this.bind( function ( e ) {
  4337. if ( ! this.isInterfaceEnabled() ) return;
  4338. this.unselect( $( e.target ) );
  4339. this.selection
  4340. .find( '.select2-search-choice-focus' )
  4341. .removeClass( 'select2-search-choice-focus' );
  4342. killEvent( e );
  4343. this.close();
  4344. this.focusSearch();
  4345. } )
  4346. )
  4347. .on(
  4348. 'focus',
  4349. this.bind( function () {
  4350. if ( ! this.isInterfaceEnabled() ) return;
  4351. this.container.addClass(
  4352. 'select2-container-active'
  4353. );
  4354. this.dropdown.addClass( 'select2-drop-active' );
  4355. } )
  4356. );
  4357. }
  4358. choice.data( 'select2-data', data );
  4359. choice.insertBefore( this.searchContainer );
  4360. val.push( id );
  4361. this.setVal( val );
  4362. },
  4363. // multi
  4364. unselect: function ( selected ) {
  4365. var val = this.getVal(),
  4366. data,
  4367. index;
  4368. selected = selected.closest( '.select2-search-choice' );
  4369. if ( selected.length === 0 ) {
  4370. throw (
  4371. 'Invalid argument: ' +
  4372. selected +
  4373. '. Must be .select2-search-choice'
  4374. );
  4375. }
  4376. data = selected.data( 'select2-data' );
  4377. if ( ! data ) {
  4378. // prevent a race condition when the 'x' is clicked really fast repeatedly the event can be queued
  4379. // and invoked on an element already removed
  4380. return;
  4381. }
  4382. var evt = $.Event( 'select2-removing' );
  4383. evt.val = this.id( data );
  4384. evt.choice = data;
  4385. this.opts.element.trigger( evt );
  4386. if ( evt.isDefaultPrevented() ) {
  4387. return false;
  4388. }
  4389. while ( ( index = indexOf( this.id( data ), val ) ) >= 0 ) {
  4390. val.splice( index, 1 );
  4391. this.setVal( val );
  4392. if ( this.select ) this.postprocessResults();
  4393. }
  4394. selected.remove();
  4395. this.opts.element.trigger( {
  4396. type: 'select2-removed',
  4397. val: this.id( data ),
  4398. choice: data,
  4399. } );
  4400. this.triggerChange( { removed: data } );
  4401. return true;
  4402. },
  4403. // multi
  4404. postprocessResults: function ( data, initial, noHighlightUpdate ) {
  4405. var val = this.getVal(),
  4406. choices = this.results.find( '.select2-result' ),
  4407. compound = this.results.find( '.select2-result-with-children' ),
  4408. self = this;
  4409. choices.each2( function ( i, choice ) {
  4410. var id = self.id( choice.data( 'select2-data' ) );
  4411. if ( indexOf( id, val ) >= 0 ) {
  4412. choice.addClass( 'select2-selected' );
  4413. // mark all children of the selected parent as selected
  4414. choice
  4415. .find( '.select2-result-selectable' )
  4416. .addClass( 'select2-selected' );
  4417. }
  4418. } );
  4419. compound.each2( function ( i, choice ) {
  4420. // hide an optgroup if it doesn't have any selectable children
  4421. if (
  4422. ! choice.is( '.select2-result-selectable' ) &&
  4423. choice.find(
  4424. '.select2-result-selectable:not(.select2-selected)'
  4425. ).length === 0
  4426. ) {
  4427. choice.addClass( 'select2-selected' );
  4428. }
  4429. } );
  4430. if (
  4431. this.highlight() == -1 &&
  4432. noHighlightUpdate !== false &&
  4433. this.opts.closeOnSelect === true
  4434. ) {
  4435. self.highlight( 0 );
  4436. }
  4437. //If all results are chosen render formatNoMatches
  4438. if (
  4439. ! this.opts.createSearchChoice &&
  4440. ! choices.filter( '.select2-result:not(.select2-selected)' )
  4441. .length > 0
  4442. ) {
  4443. if (
  4444. ! data ||
  4445. ( data &&
  4446. ! data.more &&
  4447. this.results.find( '.select2-no-results' ).length ===
  4448. 0 )
  4449. ) {
  4450. if (
  4451. checkFormatter(
  4452. self.opts.formatNoMatches,
  4453. 'formatNoMatches'
  4454. )
  4455. ) {
  4456. this.results.append(
  4457. "<li class='select2-no-results'>" +
  4458. evaluate(
  4459. self.opts.formatNoMatches,
  4460. self.opts.element,
  4461. self.search.val()
  4462. ) +
  4463. '</li>'
  4464. );
  4465. }
  4466. }
  4467. }
  4468. },
  4469. // multi
  4470. getMaxSearchWidth: function () {
  4471. return this.selection.width() - getSideBorderPadding( this.search );
  4472. },
  4473. // multi
  4474. resizeSearch: function () {
  4475. var minimumWidth,
  4476. left,
  4477. maxWidth,
  4478. containerLeft,
  4479. searchWidth,
  4480. sideBorderPadding = getSideBorderPadding( this.search );
  4481. minimumWidth = measureTextWidth( this.search ) + 10;
  4482. left = this.search.offset().left;
  4483. maxWidth = this.selection.width();
  4484. containerLeft = this.selection.offset().left;
  4485. searchWidth =
  4486. maxWidth - ( left - containerLeft ) - sideBorderPadding;
  4487. if ( searchWidth < minimumWidth ) {
  4488. searchWidth = maxWidth - sideBorderPadding;
  4489. }
  4490. if ( searchWidth < 40 ) {
  4491. searchWidth = maxWidth - sideBorderPadding;
  4492. }
  4493. if ( searchWidth <= 0 ) {
  4494. searchWidth = minimumWidth;
  4495. }
  4496. this.search.width( Math.floor( searchWidth ) );
  4497. },
  4498. // multi
  4499. getVal: function () {
  4500. var val;
  4501. if ( this.select ) {
  4502. val = this.select.val();
  4503. return val === null ? [] : val;
  4504. } else {
  4505. val = this.opts.element.val();
  4506. return splitVal(
  4507. val,
  4508. this.opts.separator,
  4509. this.opts.transformVal
  4510. );
  4511. }
  4512. },
  4513. // multi
  4514. setVal: function ( val ) {
  4515. var unique;
  4516. if ( this.select ) {
  4517. this.select.val( val );
  4518. } else {
  4519. unique = [];
  4520. // filter out duplicates
  4521. $( val ).each( function () {
  4522. if ( indexOf( this, unique ) < 0 ) unique.push( this );
  4523. } );
  4524. this.opts.element.val(
  4525. unique.length === 0
  4526. ? ''
  4527. : unique.join( this.opts.separator )
  4528. );
  4529. }
  4530. },
  4531. // multi
  4532. buildChangeDetails: function ( old, current ) {
  4533. var current = current.slice( 0 ),
  4534. old = old.slice( 0 );
  4535. // remove intersection from each array
  4536. for ( var i = 0; i < current.length; i++ ) {
  4537. for ( var j = 0; j < old.length; j++ ) {
  4538. if (
  4539. equal(
  4540. this.opts.id( current[ i ] ),
  4541. this.opts.id( old[ j ] )
  4542. )
  4543. ) {
  4544. current.splice( i, 1 );
  4545. if ( i > 0 ) {
  4546. i--;
  4547. }
  4548. old.splice( j, 1 );
  4549. j--;
  4550. }
  4551. }
  4552. }
  4553. return { added: current, removed: old };
  4554. },
  4555. // multi
  4556. val: function ( val, triggerChange ) {
  4557. var oldData,
  4558. self = this;
  4559. if ( arguments.length === 0 ) {
  4560. return this.getVal();
  4561. }
  4562. oldData = this.data();
  4563. if ( ! oldData.length ) oldData = [];
  4564. // val is an id. !val is true for [undefined,null,'',0] - 0 is legal
  4565. if ( ! val && val !== 0 ) {
  4566. this.opts.element.val( '' );
  4567. this.updateSelection( [] );
  4568. this.clearSearch();
  4569. if ( triggerChange ) {
  4570. this.triggerChange( {
  4571. added: this.data(),
  4572. removed: oldData,
  4573. } );
  4574. }
  4575. return;
  4576. }
  4577. // val is a list of ids
  4578. this.setVal( val );
  4579. if ( this.select ) {
  4580. this.opts.initSelection(
  4581. this.select,
  4582. this.bind( this.updateSelection )
  4583. );
  4584. if ( triggerChange ) {
  4585. this.triggerChange(
  4586. this.buildChangeDetails( oldData, this.data() )
  4587. );
  4588. }
  4589. } else {
  4590. if ( this.opts.initSelection === undefined ) {
  4591. throw new Error(
  4592. 'val() cannot be called if initSelection() is not defined'
  4593. );
  4594. }
  4595. this.opts.initSelection( this.opts.element, function ( data ) {
  4596. var ids = $.map( data, self.id );
  4597. self.setVal( ids );
  4598. self.updateSelection( data );
  4599. self.clearSearch();
  4600. if ( triggerChange ) {
  4601. self.triggerChange(
  4602. self.buildChangeDetails( oldData, self.data() )
  4603. );
  4604. }
  4605. } );
  4606. }
  4607. this.clearSearch();
  4608. },
  4609. // multi
  4610. onSortStart: function () {
  4611. if ( this.select ) {
  4612. throw new Error(
  4613. "Sorting of elements is not supported when attached to <select>. Attach to <input type='hidden'/> instead."
  4614. );
  4615. }
  4616. // collapse search field into 0 width so its container can be collapsed as well
  4617. this.search.width( 0 );
  4618. // hide the container
  4619. this.searchContainer.hide();
  4620. },
  4621. // multi
  4622. onSortEnd: function () {
  4623. var val = [],
  4624. self = this;
  4625. // show search and move it to the end of the list
  4626. this.searchContainer.show();
  4627. // make sure the search container is the last item in the list
  4628. this.searchContainer.appendTo( this.searchContainer.parent() );
  4629. // since we collapsed the width in dragStarted, we resize it here
  4630. this.resizeSearch();
  4631. // update selection
  4632. this.selection.find( '.select2-search-choice' ).each( function () {
  4633. val.push( self.opts.id( $( this ).data( 'select2-data' ) ) );
  4634. } );
  4635. this.setVal( val );
  4636. this.triggerChange();
  4637. },
  4638. // multi
  4639. data: function ( values, triggerChange ) {
  4640. var self = this,
  4641. ids,
  4642. old;
  4643. if ( arguments.length === 0 ) {
  4644. return this.selection
  4645. .children( '.select2-search-choice' )
  4646. .map( function () {
  4647. return $( this ).data( 'select2-data' );
  4648. } )
  4649. .get();
  4650. } else {
  4651. old = this.data();
  4652. if ( ! values ) {
  4653. values = [];
  4654. }
  4655. ids = $.map( values, function ( e ) {
  4656. return self.opts.id( e );
  4657. } );
  4658. this.setVal( ids );
  4659. this.updateSelection( values );
  4660. this.clearSearch();
  4661. if ( triggerChange ) {
  4662. this.triggerChange(
  4663. this.buildChangeDetails( old, this.data() )
  4664. );
  4665. }
  4666. }
  4667. },
  4668. } );
  4669. $.fn.select2 = function () {
  4670. var args = Array.prototype.slice.call( arguments, 0 ),
  4671. opts,
  4672. select2,
  4673. method,
  4674. value,
  4675. multiple,
  4676. allowedMethods = [
  4677. 'val',
  4678. 'destroy',
  4679. 'opened',
  4680. 'open',
  4681. 'close',
  4682. 'focus',
  4683. 'isFocused',
  4684. 'container',
  4685. 'dropdown',
  4686. 'onSortStart',
  4687. 'onSortEnd',
  4688. 'enable',
  4689. 'disable',
  4690. 'readonly',
  4691. 'positionDropdown',
  4692. 'data',
  4693. 'search',
  4694. ],
  4695. valueMethods = [ 'opened', 'isFocused', 'container', 'dropdown' ],
  4696. propertyMethods = [ 'val', 'data' ],
  4697. methodsMap = { search: 'externalSearch' };
  4698. this.each( function () {
  4699. if ( args.length === 0 || typeof args[ 0 ] === 'object' ) {
  4700. opts = args.length === 0 ? {} : $.extend( {}, args[ 0 ] );
  4701. opts.element = $( this );
  4702. if (
  4703. opts.element.get( 0 ).tagName.toLowerCase() === 'select'
  4704. ) {
  4705. multiple = opts.element.prop( 'multiple' );
  4706. } else {
  4707. multiple = opts.multiple || false;
  4708. if ( 'tags' in opts ) {
  4709. opts.multiple = multiple = true;
  4710. }
  4711. }
  4712. select2 = multiple
  4713. ? new window.Select2[ 'class' ].multi()
  4714. : new window.Select2[ 'class' ].single();
  4715. select2.init( opts );
  4716. } else if ( typeof args[ 0 ] === 'string' ) {
  4717. if ( indexOf( args[ 0 ], allowedMethods ) < 0 ) {
  4718. throw 'Unknown method: ' + args[ 0 ];
  4719. }
  4720. value = undefined;
  4721. select2 = $( this ).data( 'select2' );
  4722. if ( select2 === undefined ) return;
  4723. method = args[ 0 ];
  4724. if ( method === 'container' ) {
  4725. value = select2.container;
  4726. } else if ( method === 'dropdown' ) {
  4727. value = select2.dropdown;
  4728. } else {
  4729. if ( methodsMap[ method ] ) method = methodsMap[ method ];
  4730. value = select2[ method ].apply( select2, args.slice( 1 ) );
  4731. }
  4732. if (
  4733. indexOf( args[ 0 ], valueMethods ) >= 0 ||
  4734. ( indexOf( args[ 0 ], propertyMethods ) >= 0 &&
  4735. args.length == 1 )
  4736. ) {
  4737. return false; // abort the iteration, ready to return first matched value
  4738. }
  4739. } else {
  4740. throw 'Invalid arguments to select2 plugin: ' + args;
  4741. }
  4742. } );
  4743. return value === undefined ? this : value;
  4744. };
  4745. // plugin defaults, accessible to users
  4746. $.fn.select2.defaults = {
  4747. width: 'copy',
  4748. loadMorePadding: 0,
  4749. closeOnSelect: true,
  4750. openOnEnter: true,
  4751. containerCss: {},
  4752. dropdownCss: {},
  4753. containerCssClass: '',
  4754. dropdownCssClass: '',
  4755. formatResult: function ( result, container, query, escapeMarkup ) {
  4756. var markup = [];
  4757. markMatch( this.text( result ), query.term, markup, escapeMarkup );
  4758. return markup.join( '' );
  4759. },
  4760. transformVal: function ( val ) {
  4761. return $.trim( val );
  4762. },
  4763. formatSelection: function ( data, container, escapeMarkup ) {
  4764. return data ? escapeMarkup( this.text( data ) ) : undefined;
  4765. },
  4766. sortResults: function ( results, container, query ) {
  4767. return results;
  4768. },
  4769. formatResultCssClass: function ( data ) {
  4770. return data.css;
  4771. },
  4772. formatSelectionCssClass: function ( data, container ) {
  4773. return undefined;
  4774. },
  4775. minimumResultsForSearch: 0,
  4776. minimumInputLength: 0,
  4777. maximumInputLength: null,
  4778. maximumSelectionSize: 0,
  4779. id: function ( e ) {
  4780. return e == undefined ? null : e.id;
  4781. },
  4782. text: function ( e ) {
  4783. if ( e && this.data && this.data.text ) {
  4784. if ( $.isFunction( this.data.text ) ) {
  4785. return this.data.text( e );
  4786. } else {
  4787. return e[ this.data.text ];
  4788. }
  4789. } else {
  4790. return e.text;
  4791. }
  4792. },
  4793. matcher: function ( term, text ) {
  4794. return (
  4795. stripDiacritics( '' + text )
  4796. .toUpperCase()
  4797. .indexOf( stripDiacritics( '' + term ).toUpperCase() ) >= 0
  4798. );
  4799. },
  4800. separator: ',',
  4801. tokenSeparators: [],
  4802. tokenizer: defaultTokenizer,
  4803. escapeMarkup: defaultEscapeMarkup,
  4804. blurOnChange: false,
  4805. selectOnBlur: false,
  4806. adaptContainerCssClass: function ( c ) {
  4807. return c;
  4808. },
  4809. adaptDropdownCssClass: function ( c ) {
  4810. return null;
  4811. },
  4812. nextSearchTerm: function ( selectedObject, currentSearchTerm ) {
  4813. return undefined;
  4814. },
  4815. searchInputPlaceholder: '',
  4816. createSearchChoicePosition: 'top',
  4817. shouldFocusInput: function ( instance ) {
  4818. // Attempt to detect touch devices
  4819. var supportsTouchEvents =
  4820. 'ontouchstart' in window || navigator.msMaxTouchPoints > 0;
  4821. // Only devices which support touch events should be special cased
  4822. if ( ! supportsTouchEvents ) {
  4823. return true;
  4824. }
  4825. // Never focus the input if search is disabled
  4826. if ( instance.opts.minimumResultsForSearch < 0 ) {
  4827. return false;
  4828. }
  4829. return true;
  4830. },
  4831. };
  4832. $.fn.select2.locales = [];
  4833. $.fn.select2.locales[ 'en' ] = {
  4834. formatMatches: function ( matches ) {
  4835. if ( matches === 1 ) {
  4836. return 'One result is available, press enter to select it.';
  4837. }
  4838. return (
  4839. matches +
  4840. ' results are available, use up and down arrow keys to navigate.'
  4841. );
  4842. },
  4843. formatNoMatches: function () {
  4844. return 'No matches found';
  4845. },
  4846. formatAjaxError: function ( jqXHR, textStatus, errorThrown ) {
  4847. return 'Loading failed';
  4848. },
  4849. formatInputTooShort: function ( input, min ) {
  4850. var n = min - input.length;
  4851. return (
  4852. 'Please enter ' +
  4853. n +
  4854. ' or more character' +
  4855. ( n == 1 ? '' : 's' )
  4856. );
  4857. },
  4858. formatInputTooLong: function ( input, max ) {
  4859. var n = input.length - max;
  4860. return 'Please delete ' + n + ' character' + ( n == 1 ? '' : 's' );
  4861. },
  4862. formatSelectionTooBig: function ( limit ) {
  4863. return (
  4864. 'You can only select ' +
  4865. limit +
  4866. ' item' +
  4867. ( limit == 1 ? '' : 's' )
  4868. );
  4869. },
  4870. formatLoadMore: function ( pageNumber ) {
  4871. return 'Loading more results…';
  4872. },
  4873. formatSearching: function () {
  4874. return 'Searching…';
  4875. },
  4876. };
  4877. $.extend( $.fn.select2.defaults, $.fn.select2.locales[ 'en' ] );
  4878. $.fn.select2.ajaxDefaults = {
  4879. transport: $.ajax,
  4880. params: {
  4881. type: 'GET',
  4882. cache: false,
  4883. dataType: 'json',
  4884. },
  4885. };
  4886. // exports
  4887. window.Select2 = {
  4888. query: {
  4889. ajax: ajax,
  4890. local: local,
  4891. tags: tags,
  4892. },
  4893. util: {
  4894. debounce: debounce,
  4895. markMatch: markMatch,
  4896. escapeMarkup: defaultEscapeMarkup,
  4897. stripDiacritics: stripDiacritics,
  4898. },
  4899. class: {
  4900. abstract: AbstractSelect2,
  4901. single: SingleSelect2,
  4902. multi: MultiSelect2,
  4903. },
  4904. };
  4905. } )( jQuery );