Keine Beschreibung

selectWoo.js 145KB

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