select2.js 139 KB

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