select2.amd.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430
  1. window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
  2. var Utils = {};
  3. Utils.Extend = function (ChildClass, SuperClass) {
  4. var __hasProp = {}.hasOwnProperty;
  5. function BaseConstructor () {
  6. this.constructor = ChildClass;
  7. }
  8. for (var key in SuperClass) {
  9. if (__hasProp.call(SuperClass, key)) {
  10. ChildClass[key] = SuperClass[key];
  11. }
  12. }
  13. BaseConstructor.prototype = SuperClass.prototype;
  14. ChildClass.prototype = new BaseConstructor();
  15. ChildClass.__super__ = SuperClass.prototype;
  16. return ChildClass;
  17. };
  18. function getMethods (theClass) {
  19. var proto = theClass.prototype;
  20. var methods = [];
  21. for (var methodName in proto) {
  22. var m = proto[methodName];
  23. if (typeof m !== 'function') {
  24. continue;
  25. }
  26. if (methodName === 'constructor') {
  27. continue;
  28. }
  29. methods.push(methodName);
  30. }
  31. return methods;
  32. }
  33. Utils.Decorate = function (SuperClass, DecoratorClass) {
  34. var decoratedMethods = getMethods(DecoratorClass);
  35. var superMethods = getMethods(SuperClass);
  36. function DecoratedClass () {
  37. var unshift = Array.prototype.unshift;
  38. var argCount = DecoratorClass.prototype.constructor.length;
  39. var calledConstructor = SuperClass.prototype.constructor;
  40. if (argCount > 0) {
  41. unshift.call(arguments, SuperClass.prototype.constructor);
  42. calledConstructor = DecoratorClass.prototype.constructor;
  43. }
  44. calledConstructor.apply(this, arguments);
  45. }
  46. DecoratorClass.displayName = SuperClass.displayName;
  47. function ctr () {
  48. this.constructor = DecoratedClass;
  49. }
  50. DecoratedClass.prototype = new ctr();
  51. for (var m = 0; m < superMethods.length; m++) {
  52. var superMethod = superMethods[m];
  53. DecoratedClass.prototype[superMethod] =
  54. SuperClass.prototype[superMethod];
  55. }
  56. var calledMethod = function (methodName) {
  57. // Stub out the original method if it's not decorating an actual method
  58. var originalMethod = function () {};
  59. if (methodName in DecoratedClass.prototype) {
  60. originalMethod = DecoratedClass.prototype[methodName];
  61. }
  62. var decoratedMethod = DecoratorClass.prototype[methodName];
  63. return function () {
  64. var unshift = Array.prototype.unshift;
  65. unshift.call(arguments, originalMethod);
  66. return decoratedMethod.apply(this, arguments);
  67. };
  68. };
  69. for (var d = 0; d < decoratedMethods.length; d++) {
  70. var decoratedMethod = decoratedMethods[d];
  71. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  72. }
  73. return DecoratedClass;
  74. };
  75. var Observable = function () {
  76. this.listeners = {};
  77. };
  78. Observable.prototype.on = function (event, callback) {
  79. this.listeners = this.listeners || {};
  80. if (event in this.listeners) {
  81. this.listeners[event].push(callback);
  82. } else {
  83. this.listeners[event] = [callback];
  84. }
  85. };
  86. Observable.prototype.trigger = function (event) {
  87. var slice = Array.prototype.slice;
  88. this.listeners = this.listeners || {};
  89. if (event in this.listeners) {
  90. this.invoke(this.listeners[event], slice.call(arguments, 1));
  91. }
  92. if ('*' in this.listeners) {
  93. this.invoke(this.listeners['*'], arguments);
  94. }
  95. };
  96. Observable.prototype.invoke = function (listeners, params) {
  97. for (var i = 0, len = listeners.length; i < len; i++) {
  98. listeners[i].apply(this, params);
  99. }
  100. };
  101. Utils.Observable = Observable;
  102. Utils.generateChars = function (length) {
  103. var chars = '';
  104. for (var i = 0; i < length; i++) {
  105. var randomChar = Math.floor(Math.random() * 36);
  106. chars += randomChar.toString(36);
  107. }
  108. return chars;
  109. };
  110. return Utils;
  111. });
  112. define('select2/results',[
  113. './utils'
  114. ], function (Utils) {
  115. function Results ($element, options, dataAdapter) {
  116. this.$element = $element;
  117. this.data = dataAdapter;
  118. this.options = options;
  119. Results.__super__.constructor.call(this);
  120. }
  121. Utils.Extend(Results, Utils.Observable);
  122. Results.prototype.render = function () {
  123. var $results = $(
  124. '<ul class="select2-results__options" role="tree"></ul>'
  125. );
  126. if (this.options.get('multiple')) {
  127. $results.attr('aria-multiselectable', 'true');
  128. }
  129. this.$results = $results;
  130. return $results;
  131. };
  132. Results.prototype.clear = function () {
  133. this.$results.empty();
  134. };
  135. Results.prototype.displayMessage = function (params) {
  136. this.clear();
  137. this.hideLoading();
  138. var $message = $(
  139. '<li role="treeitem" class="select2-results__option"></li>'
  140. );
  141. var message = this.options.get('translations').get(params.message);
  142. $message.text(message(params.args));
  143. this.$results.append($message);
  144. };
  145. Results.prototype.append = function (data) {
  146. this.hideLoading();
  147. var $options = [];
  148. if (data.length === 0) {
  149. if (this.$results.children().length === 0) {
  150. this.trigger('results:message', {
  151. message: 'noResults'
  152. });
  153. }
  154. return;
  155. }
  156. data = this.sort(data);
  157. for (var d = 0; d < data.length; d++) {
  158. var item = data[d];
  159. var $option = this.option(item);
  160. $options.push($option);
  161. }
  162. this.$results.append($options);
  163. };
  164. Results.prototype.sort = function (data) {
  165. return data;
  166. };
  167. Results.prototype.setClasses = function () {
  168. var self = this;
  169. this.data.current(function (selected) {
  170. var selectedIds = $.map(selected, function (s) {
  171. return s.id.toString();
  172. });
  173. var $options = self.$results
  174. .find('.select2-results__option[aria-selected]');
  175. $options.each(function () {
  176. var $option = $(this);
  177. var item = $.data(this, 'data');
  178. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  179. $option.attr('aria-selected', 'true');
  180. } else {
  181. $option.attr('aria-selected', 'false');
  182. }
  183. });
  184. var $selected = $options.filter('[aria-selected=true]');
  185. // Check if there are any selected options
  186. if ($selected.length > 0) {
  187. // If there are selected options, highlight the first
  188. $selected.first().trigger('mouseenter');
  189. } else {
  190. // If there are no selected options, highlight the first option
  191. // in the dropdown
  192. $options.first().trigger('mouseenter');
  193. }
  194. });
  195. };
  196. Results.prototype.showLoading = function (params) {
  197. this.hideLoading();
  198. var loadingMore = this.options.get('translations').get('searching');
  199. var loading = {
  200. disabled: true,
  201. loading: true,
  202. text: loadingMore(params)
  203. };
  204. var $loading = this.option(loading);
  205. $loading.className += ' loading-results';
  206. this.$results.prepend($loading);
  207. };
  208. Results.prototype.hideLoading = function () {
  209. this.$results.find('.loading-results').remove();
  210. };
  211. Results.prototype.option = function (data) {
  212. var option = document.createElement('li');
  213. option.className = 'select2-results__option';
  214. var attrs = {
  215. 'role': 'treeitem',
  216. 'aria-selected': 'false'
  217. };
  218. if (data.disabled) {
  219. delete attrs['aria-selected'];
  220. attrs['aria-disabled'] = 'true';
  221. }
  222. if (data.id == null) {
  223. delete attrs['aria-selected'];
  224. }
  225. if (data._resultId != null) {
  226. option.id = data._resultId;
  227. }
  228. if (data.children) {
  229. attrs.role = 'group';
  230. attrs['aria-label'] = data.text;
  231. delete attrs['aria-selected'];
  232. }
  233. for (var attr in attrs) {
  234. var val = attrs[attr];
  235. option.setAttribute(attr, val);
  236. }
  237. if (data.children) {
  238. var $option = $(option);
  239. var label = document.createElement('strong');
  240. label.className = 'select2-results__group';
  241. var $label = $(label);
  242. this.template(data, label);
  243. var $children = [];
  244. for (var c = 0; c < data.children.length; c++) {
  245. var child = data.children[c];
  246. var $child = this.option(child);
  247. $children.push($child);
  248. }
  249. var $childrenContainer = $('<ul></ul>', {
  250. 'class': 'select2-results__options select2-results__options--nested'
  251. });
  252. $childrenContainer.append($children);
  253. $option.append(label);
  254. $option.append($childrenContainer);
  255. } else {
  256. this.template(data, option);
  257. }
  258. $.data(option, 'data', data);
  259. return option;
  260. };
  261. Results.prototype.bind = function (container, $container) {
  262. var self = this;
  263. var id = container.id + '-results';
  264. this.$results.attr('id', id);
  265. container.on('results:all', function (params) {
  266. self.clear();
  267. self.append(params.data);
  268. if (container.isOpen()) {
  269. self.setClasses();
  270. }
  271. });
  272. container.on('results:append', function (params) {
  273. self.append(params.data);
  274. if (container.isOpen()) {
  275. self.setClasses();
  276. }
  277. });
  278. container.on('query', function (params) {
  279. self.showLoading(params);
  280. });
  281. container.on('select', function () {
  282. if (!container.isOpen()) {
  283. return;
  284. }
  285. self.setClasses();
  286. });
  287. container.on('unselect', function () {
  288. if (!container.isOpen()) {
  289. return;
  290. }
  291. self.setClasses();
  292. });
  293. container.on('open', function () {
  294. // When the dropdown is open, aria-expended="true"
  295. self.$results.attr('aria-expanded', 'true');
  296. self.$results.attr('aria-hidden', 'false');
  297. self.setClasses();
  298. self.ensureHighlightVisible();
  299. });
  300. container.on('close', function () {
  301. // When the dropdown is closed, aria-expended="false"
  302. self.$results.attr('aria-expanded', 'false');
  303. self.$results.attr('aria-hidden', 'true');
  304. self.$results.removeAttr('aria-activedescendant');
  305. });
  306. container.on('results:select', function () {
  307. var $highlighted = self.getHighlightedResults();
  308. if ($highlighted.length === 0) {
  309. return;
  310. }
  311. var data = $highlighted.data('data');
  312. if ($highlighted.attr('aria-selected') == 'true') {
  313. self.trigger('unselected', {
  314. data: data
  315. });
  316. } else {
  317. self.trigger('selected', {
  318. data: data
  319. });
  320. }
  321. });
  322. container.on('results:previous', function () {
  323. var $highlighted = self.getHighlightedResults();
  324. var $options = self.$results.find('[aria-selected]');
  325. var currentIndex = $options.index($highlighted);
  326. // If we are already at te top, don't move further
  327. if (currentIndex === 0) {
  328. return;
  329. }
  330. var nextIndex = currentIndex - 1;
  331. // If none are highlighted, highlight the first
  332. if ($highlighted.length === 0) {
  333. nextIndex = 0;
  334. }
  335. var $next = $options.eq(nextIndex);
  336. $next.trigger('mouseenter');
  337. var currentOffset = self.$results.offset().top;
  338. var nextTop = $next.offset().top;
  339. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  340. if (nextIndex === 0) {
  341. self.$results.scrollTop(0);
  342. } else if (nextTop - currentOffset < 0) {
  343. self.$results.scrollTop(nextOffset);
  344. }
  345. });
  346. container.on('results:next', function () {
  347. var $highlighted = self.getHighlightedResults();
  348. var $options = self.$results.find('[aria-selected]');
  349. var currentIndex = $options.index($highlighted);
  350. var nextIndex = currentIndex + 1;
  351. // If we are at the last option, stay there
  352. if (nextIndex >= $options.length) {
  353. return;
  354. }
  355. var $next = $options.eq(nextIndex);
  356. $next.trigger('mouseenter');
  357. var currentOffset = self.$results.offset().top +
  358. self.$results.outerHeight(false);
  359. var nextBottom = $next.offset().top + $next.outerHeight(false);
  360. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  361. if (nextIndex === 0) {
  362. self.$results.scrollTop(0);
  363. } else if (nextBottom > currentOffset) {
  364. self.$results.scrollTop(nextOffset);
  365. }
  366. });
  367. container.on('results:focus', function (params) {
  368. params.element.addClass('select2-results__option--highlighted');
  369. });
  370. container.on('results:message', function (params) {
  371. self.trigger('results:message', params);
  372. });
  373. this.on('results:message', function (params) {
  374. self.displayMessage(params);
  375. });
  376. this.$results.on('mouseup', '.select2-results__option[aria-selected]',
  377. function (evt) {
  378. var $this = $(this);
  379. var data = $this.data('data');
  380. if ($this.attr('aria-selected') === 'true') {
  381. self.trigger('unselected', {
  382. originalEvent: evt,
  383. data: data
  384. });
  385. return;
  386. }
  387. self.trigger('selected', {
  388. originalEvent: evt,
  389. data: data
  390. });
  391. });
  392. this.$results.on('mouseenter', '.select2-results__option[aria-selected]',
  393. function (evt) {
  394. var data = $(this).data('data');
  395. self.getHighlightedResults()
  396. .removeClass('select2-results__option--highlighted');
  397. self.trigger('results:focus', {
  398. data: data,
  399. element: $(this)
  400. });
  401. });
  402. };
  403. Results.prototype.getHighlightedResults = function () {
  404. var $highlighted = this.$results
  405. .find('.select2-results__option--highlighted');
  406. return $highlighted;
  407. };
  408. Results.prototype.destroy = function () {
  409. this.$results.remove();
  410. };
  411. Results.prototype.ensureHighlightVisible = function () {
  412. var $highlighted = this.getHighlightedResults();
  413. if ($highlighted.length === 0) {
  414. return;
  415. }
  416. var $options = this.$results.find('[aria-selected]');
  417. var currentIndex = $options.index($highlighted);
  418. var currentOffset = this.$results.offset().top;
  419. var nextTop = $highlighted.offset().top;
  420. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  421. var offsetDelta = nextTop - currentOffset;
  422. nextOffset -= $highlighted.outerHeight(false) * 2;
  423. if (currentIndex <= 2) {
  424. this.$results.scrollTop(0);
  425. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  426. this.$results.scrollTop(nextOffset);
  427. }
  428. };
  429. Results.prototype.template = function (result, container) {
  430. var template = this.options.get('templateResult');
  431. container.innerHTML = template(result);
  432. };
  433. return Results;
  434. });
  435. define('select2/keys',[
  436. ], function () {
  437. var KEYS = {
  438. BACKSPACE: 8,
  439. TAB: 9,
  440. ENTER: 13,
  441. SHIFT: 16,
  442. CTRL: 17,
  443. ALT: 18,
  444. ESC: 27,
  445. SPACE: 32,
  446. PAGE_UP: 33,
  447. PAGE_DOWN: 34,
  448. END: 35,
  449. HOME: 36,
  450. LEFT: 37,
  451. UP: 38,
  452. RIGHT: 39,
  453. DOWN: 40,
  454. DELETE: 46,
  455. isArrow: function (k) {
  456. k = k.which ? k.which : k;
  457. switch (k) {
  458. case KEY.LEFT:
  459. case KEY.RIGHT:
  460. case KEY.UP:
  461. case KEY.DOWN:
  462. return true;
  463. }
  464. return false;
  465. }
  466. };
  467. return KEYS;
  468. });
  469. define('select2/selection/base',[
  470. '../utils',
  471. '../keys'
  472. ], function (Utils, KEYS) {
  473. function BaseSelection ($element, options) {
  474. this.$element = $element;
  475. this.options = options;
  476. BaseSelection.__super__.constructor.call(this);
  477. }
  478. Utils.Extend(BaseSelection, Utils.Observable);
  479. BaseSelection.prototype.render = function () {
  480. throw new Error('The `render` method must be defined in child classes.');
  481. };
  482. BaseSelection.prototype.bind = function (container, $container) {
  483. var self = this;
  484. var id = container.id + '-container';
  485. var resultsId = container.id + '-results';
  486. this.$selection.attr('aria-owns', resultsId);
  487. this.$selection.on('keydown', function (evt) {
  488. self.trigger('keypress', evt);
  489. if (evt.which === KEYS.SPACE) {
  490. evt.preventDefault();
  491. }
  492. });
  493. container.on('results:focus', function (params) {
  494. self.$selection.attr('aria-activedescendant', params.data._resultId);
  495. });
  496. container.on('selection:update', function (params) {
  497. self.update(params.data);
  498. });
  499. container.on('open', function () {
  500. // When the dropdown is open, aria-expanded="true"
  501. self.$selection.attr('aria-expanded', 'true');
  502. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  503. var $target = $(e.target);
  504. var $select = $target.closest('.select2');
  505. var $all = $('.select2.select2-container--open');
  506. $all.each(function () {
  507. var $this = $(this);
  508. if (this == $select[0]) {
  509. return;
  510. }
  511. var $element = $this.data('element');
  512. $element.select2('close');
  513. });
  514. });
  515. });
  516. container.on('close', function () {
  517. // When the dropdown is closed, aria-expanded="false"
  518. self.$selection.attr('aria-expanded', 'false');
  519. self.$selection.removeAttr('aria-activedescendant');
  520. self.$selection.focus();
  521. $(document.body).off('mousedown.select2.' + container.id);
  522. });
  523. };
  524. BaseSelection.prototype.destroy = function () {
  525. // Unbind the dropdown click handler if it exists
  526. $(document.body).off('.select2.' + container.id);
  527. };
  528. BaseSelection.prototype.update = function (data) {
  529. throw new Error('The `update` method must be defined in child classes.');
  530. };
  531. return BaseSelection;
  532. });
  533. define('select2/selection/single',[
  534. './base',
  535. '../utils',
  536. '../keys'
  537. ], function (BaseSelection, Utils, KEYS) {
  538. function SingleSelection () {
  539. SingleSelection.__super__.constructor.apply(this, arguments);
  540. }
  541. Utils.Extend(SingleSelection, BaseSelection);
  542. SingleSelection.prototype.render = function () {
  543. var $selection = $(
  544. '<span class="select2-selection select2-selection--single" tabindex="0"' +
  545. ' role="combobox" aria-autocomplete="list" aria-haspopup="true"' +
  546. ' aria-expanded="false">' +
  547. '<span class="select2-selection__rendered"></span>' +
  548. '</span>'
  549. );
  550. $selection.attr('title', this.$element.attr('title'));
  551. this.$selection = $selection;
  552. return $selection;
  553. };
  554. SingleSelection.prototype.bind = function (container, $container) {
  555. var self = this;
  556. SingleSelection.__super__.bind.apply(this, arguments);
  557. var id = container.id + '-container';
  558. this.$selection.find('.select2-selection__rendered').attr('id', id);
  559. this.$selection.attr('aria-labelledby', id);
  560. this.$selection.on('mousedown', function (evt) {
  561. // Only respond to left clicks
  562. if (evt.which !== 1) {
  563. return;
  564. }
  565. self.trigger('toggle', {
  566. originalEvent: evt
  567. });
  568. });
  569. this.$selection.on('focus', function (evt) {
  570. // User focuses on the container
  571. });
  572. this.$selection.on('blur', function (evt) {
  573. // User exits the container
  574. });
  575. container.on('selection:update', function (params) {
  576. self.update(params.data);
  577. });
  578. };
  579. SingleSelection.prototype.clear = function () {
  580. this.$selection.find('.select2-selection__rendered').empty();
  581. };
  582. SingleSelection.prototype.display = function (data) {
  583. var template = this.options.get('templateSelection');
  584. return template(data);
  585. };
  586. SingleSelection.prototype.selectionContainer = function () {
  587. return $('<span></span>');
  588. };
  589. SingleSelection.prototype.update = function (data) {
  590. if (data.length === 0) {
  591. this.clear();
  592. return;
  593. }
  594. var selection = data[0];
  595. var formatted = this.display(selection);
  596. this.$selection.find('.select2-selection__rendered').html(formatted);
  597. };
  598. return SingleSelection;
  599. });
  600. define('select2/selection/multiple',[
  601. './base',
  602. '../utils'
  603. ], function (BaseSelection, Utils) {
  604. function MultipleSelection ($element, options) {
  605. MultipleSelection.__super__.constructor.apply(this, arguments);
  606. }
  607. Utils.Extend(MultipleSelection, BaseSelection);
  608. MultipleSelection.prototype.render = function () {
  609. var $selection = $(
  610. '<span class="select2-selection select2-selection--multiple"' +
  611. ' tabindex="0" role="combobox" aria-autocomplete="list"' +
  612. ' aria-haspopup="true" aria-expanded="false">' +
  613. '<ul class="select2-selection__rendered"></ul>' +
  614. '</span>'
  615. );
  616. $selection.attr('title', this.$element.attr('title'));
  617. this.$selection = $selection;
  618. return $selection;
  619. };
  620. MultipleSelection.prototype.bind = function (container, $container) {
  621. var self = this;
  622. MultipleSelection.__super__.bind.apply(this, arguments);
  623. this.$selection.on('click', function (evt) {
  624. self.trigger('toggle', {
  625. originalEvent: evt
  626. });
  627. });
  628. this.$selection.on('click', '.select2-selection__choice__remove',
  629. function (evt) {
  630. var $remove = $(this);
  631. var $selection = $remove.parent();
  632. var data = $selection.data('data');
  633. self.trigger('unselected', {
  634. originalEvent: evt,
  635. data: data
  636. });
  637. });
  638. };
  639. MultipleSelection.prototype.clear = function () {
  640. this.$selection.find('.select2-selection__rendered').empty();
  641. };
  642. MultipleSelection.prototype.display = function (data) {
  643. var template = this.options.get('templateSelection');
  644. return template(data);
  645. };
  646. MultipleSelection.prototype.selectionContainer = function () {
  647. var $container = $(
  648. '<li class="select2-selection__choice">' +
  649. '<span class="select2-selection__choice__remove" role="presentation">' +
  650. '&times;' +
  651. '</span>' +
  652. '</li>'
  653. );
  654. return $container;
  655. };
  656. MultipleSelection.prototype.update = function (data) {
  657. this.clear();
  658. if (data.length === 0) {
  659. return;
  660. }
  661. var $selections = [];
  662. for (var d = 0; d < data.length; d++) {
  663. var selection = data[d];
  664. var formatted = this.display(selection);
  665. var $selection = this.selectionContainer();
  666. $selection.append(formatted);
  667. $selection.data('data', selection);
  668. $selections.push($selection);
  669. }
  670. this.$selection.find('.select2-selection__rendered').append($selections);
  671. };
  672. return MultipleSelection;
  673. });
  674. define('select2/selection/placeholder',[
  675. '../utils'
  676. ], function (Utils) {
  677. function Placeholder (decorated, $element, options) {
  678. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  679. decorated.call(this, $element, options);
  680. }
  681. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  682. if (typeof placeholder === 'string') {
  683. placeholder = {
  684. id: '',
  685. text: placeholder
  686. };
  687. }
  688. return placeholder;
  689. };
  690. Placeholder.prototype.createPlaceholder = function (decorated, placeholder) {
  691. var $placeholder = this.selectionContainer();
  692. $placeholder.html(this.display(placeholder));
  693. $placeholder.addClass('select2-selection__placeholder')
  694. .removeClass('select2-selection__choice');
  695. return $placeholder;
  696. };
  697. Placeholder.prototype.update = function (decorated, data) {
  698. var singlePlaceholder = (
  699. data.length == 1 && data[0].id != this.placeholder.id
  700. );
  701. var multipleSelections = data.length > 1;
  702. if (multipleSelections || singlePlaceholder) {
  703. return decorated.call(this, data);
  704. }
  705. this.clear();
  706. var $placeholder = this.createPlaceholder(this.placeholder);
  707. this.$selection.find('.select2-selection__rendered').append($placeholder);
  708. };
  709. return Placeholder;
  710. });
  711. define('select2/selection/search',[
  712. '../utils'
  713. ], function (Utils) {
  714. function Search (decorated, $element, options) {
  715. decorated.call(this, $element, options);
  716. }
  717. Search.prototype.render = function (decorated) {
  718. var $search = $(
  719. '<li class="select2-search select2-search--inline">' +
  720. '<input class="select2-search__field" type="search" tabindex="-1"' +
  721. ' role="textbox" />' +
  722. '</li>'
  723. );
  724. this.$searchContainer = $search;
  725. this.$search = $search.find('input');
  726. var $rendered = decorated.call(this);
  727. return $rendered;
  728. };
  729. Search.prototype.bind = function (decorated, container, $container) {
  730. var self = this;
  731. decorated.call(this, container, $container);
  732. container.on('open', function () {
  733. self.$search.attr('tabindex', 0);
  734. self.$search.focus();
  735. });
  736. container.on('close', function () {
  737. self.$search.attr('tabindex', -1);
  738. self.$search.val('');
  739. });
  740. this.$selection.on('keydown', '.select2-search--inline', function (evt) {
  741. evt.stopPropagation();
  742. self.trigger('keypress', evt);
  743. self._keyUpPrevented = evt.isDefaultPrevented();
  744. });
  745. this.$selection.on('keyup', '.select2-search--inline', function (evt) {
  746. self.handleSearch(evt);
  747. });
  748. };
  749. Search.prototype.createPlaceholder = function (decorated, placeholder) {
  750. this.$search.attr('placeholder', placeholder.text);
  751. };
  752. Search.prototype.update = function (decorated, data) {
  753. this.$search.attr('placeholder', '');
  754. decorated.call(this, data);
  755. this.$selection.find('.select2-selection__rendered')
  756. .append(this.$searchContainer);
  757. this.resizeSearch();
  758. };
  759. Search.prototype.handleSearch = function (evt) {
  760. this.resizeSearch();
  761. if (!this._keyUpPrevented) {
  762. var input = this.$search.val();
  763. this.trigger('query', {
  764. term: input
  765. });
  766. }
  767. this._keyUpPrevented = false;
  768. };
  769. Search.prototype.resizeSearch = function () {
  770. this.$search.css('width', '25px');
  771. var width = '';
  772. if (this.$search.attr('placeholder') !== '') {
  773. width = this.$selection.innerWidth();
  774. width -= this.$selection.find('.select2-selection__rendered')
  775. .innerWidth();
  776. width = width + 'px';
  777. } else {
  778. var minimumWidth = this.$search.val().length + 1;
  779. width = (minimumWidth * 0.75) + 'em';
  780. }
  781. this.$search.css('width', width);
  782. };
  783. Search.prototype.showSearch = function (_, params) {
  784. return true;
  785. };
  786. return Search;
  787. });
  788. define('select2/translation',[
  789. ], function () {
  790. function Translation (dict) {
  791. this.dict = dict || {};
  792. }
  793. Translation.prototype.all = function () {
  794. return this.dict;
  795. };
  796. Translation.prototype.get = function (key) {
  797. return this.dict[key];
  798. };
  799. Translation.prototype.extend = function (translation) {
  800. this.dict = $.extend({}, translation.all(), this.dict);
  801. };
  802. // Static functions
  803. Translation._cache = {};
  804. Translation.loadPath = function (path) {
  805. if (!(path in Translation._cache)) {
  806. var translations = require(path);
  807. Translation._cache[path] = translations;
  808. }
  809. return new Translation(Translation._cache[path]);
  810. };
  811. return Translation;
  812. });
  813. define('select2/diacritics',[
  814. ], function () {
  815. var diacritics = {
  816. '\u24B6': 'A',
  817. '\uFF21': 'A',
  818. '\u00C0': 'A',
  819. '\u00C1': 'A',
  820. '\u00C2': 'A',
  821. '\u1EA6': 'A',
  822. '\u1EA4': 'A',
  823. '\u1EAA': 'A',
  824. '\u1EA8': 'A',
  825. '\u00C3': 'A',
  826. '\u0100': 'A',
  827. '\u0102': 'A',
  828. '\u1EB0': 'A',
  829. '\u1EAE': 'A',
  830. '\u1EB4': 'A',
  831. '\u1EB2': 'A',
  832. '\u0226': 'A',
  833. '\u01E0': 'A',
  834. '\u00C4': 'A',
  835. '\u01DE': 'A',
  836. '\u1EA2': 'A',
  837. '\u00C5': 'A',
  838. '\u01FA': 'A',
  839. '\u01CD': 'A',
  840. '\u0200': 'A',
  841. '\u0202': 'A',
  842. '\u1EA0': 'A',
  843. '\u1EAC': 'A',
  844. '\u1EB6': 'A',
  845. '\u1E00': 'A',
  846. '\u0104': 'A',
  847. '\u023A': 'A',
  848. '\u2C6F': 'A',
  849. '\uA732': 'AA',
  850. '\u00C6': 'AE',
  851. '\u01FC': 'AE',
  852. '\u01E2': 'AE',
  853. '\uA734': 'AO',
  854. '\uA736': 'AU',
  855. '\uA738': 'AV',
  856. '\uA73A': 'AV',
  857. '\uA73C': 'AY',
  858. '\u24B7': 'B',
  859. '\uFF22': 'B',
  860. '\u1E02': 'B',
  861. '\u1E04': 'B',
  862. '\u1E06': 'B',
  863. '\u0243': 'B',
  864. '\u0182': 'B',
  865. '\u0181': 'B',
  866. '\u24B8': 'C',
  867. '\uFF23': 'C',
  868. '\u0106': 'C',
  869. '\u0108': 'C',
  870. '\u010A': 'C',
  871. '\u010C': 'C',
  872. '\u00C7': 'C',
  873. '\u1E08': 'C',
  874. '\u0187': 'C',
  875. '\u023B': 'C',
  876. '\uA73E': 'C',
  877. '\u24B9': 'D',
  878. '\uFF24': 'D',
  879. '\u1E0A': 'D',
  880. '\u010E': 'D',
  881. '\u1E0C': 'D',
  882. '\u1E10': 'D',
  883. '\u1E12': 'D',
  884. '\u1E0E': 'D',
  885. '\u0110': 'D',
  886. '\u018B': 'D',
  887. '\u018A': 'D',
  888. '\u0189': 'D',
  889. '\uA779': 'D',
  890. '\u01F1': 'DZ',
  891. '\u01C4': 'DZ',
  892. '\u01F2': 'Dz',
  893. '\u01C5': 'Dz',
  894. '\u24BA': 'E',
  895. '\uFF25': 'E',
  896. '\u00C8': 'E',
  897. '\u00C9': 'E',
  898. '\u00CA': 'E',
  899. '\u1EC0': 'E',
  900. '\u1EBE': 'E',
  901. '\u1EC4': 'E',
  902. '\u1EC2': 'E',
  903. '\u1EBC': 'E',
  904. '\u0112': 'E',
  905. '\u1E14': 'E',
  906. '\u1E16': 'E',
  907. '\u0114': 'E',
  908. '\u0116': 'E',
  909. '\u00CB': 'E',
  910. '\u1EBA': 'E',
  911. '\u011A': 'E',
  912. '\u0204': 'E',
  913. '\u0206': 'E',
  914. '\u1EB8': 'E',
  915. '\u1EC6': 'E',
  916. '\u0228': 'E',
  917. '\u1E1C': 'E',
  918. '\u0118': 'E',
  919. '\u1E18': 'E',
  920. '\u1E1A': 'E',
  921. '\u0190': 'E',
  922. '\u018E': 'E',
  923. '\u24BB': 'F',
  924. '\uFF26': 'F',
  925. '\u1E1E': 'F',
  926. '\u0191': 'F',
  927. '\uA77B': 'F',
  928. '\u24BC': 'G',
  929. '\uFF27': 'G',
  930. '\u01F4': 'G',
  931. '\u011C': 'G',
  932. '\u1E20': 'G',
  933. '\u011E': 'G',
  934. '\u0120': 'G',
  935. '\u01E6': 'G',
  936. '\u0122': 'G',
  937. '\u01E4': 'G',
  938. '\u0193': 'G',
  939. '\uA7A0': 'G',
  940. '\uA77D': 'G',
  941. '\uA77E': 'G',
  942. '\u24BD': 'H',
  943. '\uFF28': 'H',
  944. '\u0124': 'H',
  945. '\u1E22': 'H',
  946. '\u1E26': 'H',
  947. '\u021E': 'H',
  948. '\u1E24': 'H',
  949. '\u1E28': 'H',
  950. '\u1E2A': 'H',
  951. '\u0126': 'H',
  952. '\u2C67': 'H',
  953. '\u2C75': 'H',
  954. '\uA78D': 'H',
  955. '\u24BE': 'I',
  956. '\uFF29': 'I',
  957. '\u00CC': 'I',
  958. '\u00CD': 'I',
  959. '\u00CE': 'I',
  960. '\u0128': 'I',
  961. '\u012A': 'I',
  962. '\u012C': 'I',
  963. '\u0130': 'I',
  964. '\u00CF': 'I',
  965. '\u1E2E': 'I',
  966. '\u1EC8': 'I',
  967. '\u01CF': 'I',
  968. '\u0208': 'I',
  969. '\u020A': 'I',
  970. '\u1ECA': 'I',
  971. '\u012E': 'I',
  972. '\u1E2C': 'I',
  973. '\u0197': 'I',
  974. '\u24BF': 'J',
  975. '\uFF2A': 'J',
  976. '\u0134': 'J',
  977. '\u0248': 'J',
  978. '\u24C0': 'K',
  979. '\uFF2B': 'K',
  980. '\u1E30': 'K',
  981. '\u01E8': 'K',
  982. '\u1E32': 'K',
  983. '\u0136': 'K',
  984. '\u1E34': 'K',
  985. '\u0198': 'K',
  986. '\u2C69': 'K',
  987. '\uA740': 'K',
  988. '\uA742': 'K',
  989. '\uA744': 'K',
  990. '\uA7A2': 'K',
  991. '\u24C1': 'L',
  992. '\uFF2C': 'L',
  993. '\u013F': 'L',
  994. '\u0139': 'L',
  995. '\u013D': 'L',
  996. '\u1E36': 'L',
  997. '\u1E38': 'L',
  998. '\u013B': 'L',
  999. '\u1E3C': 'L',
  1000. '\u1E3A': 'L',
  1001. '\u0141': 'L',
  1002. '\u023D': 'L',
  1003. '\u2C62': 'L',
  1004. '\u2C60': 'L',
  1005. '\uA748': 'L',
  1006. '\uA746': 'L',
  1007. '\uA780': 'L',
  1008. '\u01C7': 'LJ',
  1009. '\u01C8': 'Lj',
  1010. '\u24C2': 'M',
  1011. '\uFF2D': 'M',
  1012. '\u1E3E': 'M',
  1013. '\u1E40': 'M',
  1014. '\u1E42': 'M',
  1015. '\u2C6E': 'M',
  1016. '\u019C': 'M',
  1017. '\u24C3': 'N',
  1018. '\uFF2E': 'N',
  1019. '\u01F8': 'N',
  1020. '\u0143': 'N',
  1021. '\u00D1': 'N',
  1022. '\u1E44': 'N',
  1023. '\u0147': 'N',
  1024. '\u1E46': 'N',
  1025. '\u0145': 'N',
  1026. '\u1E4A': 'N',
  1027. '\u1E48': 'N',
  1028. '\u0220': 'N',
  1029. '\u019D': 'N',
  1030. '\uA790': 'N',
  1031. '\uA7A4': 'N',
  1032. '\u01CA': 'NJ',
  1033. '\u01CB': 'Nj',
  1034. '\u24C4': 'O',
  1035. '\uFF2F': 'O',
  1036. '\u00D2': 'O',
  1037. '\u00D3': 'O',
  1038. '\u00D4': 'O',
  1039. '\u1ED2': 'O',
  1040. '\u1ED0': 'O',
  1041. '\u1ED6': 'O',
  1042. '\u1ED4': 'O',
  1043. '\u00D5': 'O',
  1044. '\u1E4C': 'O',
  1045. '\u022C': 'O',
  1046. '\u1E4E': 'O',
  1047. '\u014C': 'O',
  1048. '\u1E50': 'O',
  1049. '\u1E52': 'O',
  1050. '\u014E': 'O',
  1051. '\u022E': 'O',
  1052. '\u0230': 'O',
  1053. '\u00D6': 'O',
  1054. '\u022A': 'O',
  1055. '\u1ECE': 'O',
  1056. '\u0150': 'O',
  1057. '\u01D1': 'O',
  1058. '\u020C': 'O',
  1059. '\u020E': 'O',
  1060. '\u01A0': 'O',
  1061. '\u1EDC': 'O',
  1062. '\u1EDA': 'O',
  1063. '\u1EE0': 'O',
  1064. '\u1EDE': 'O',
  1065. '\u1EE2': 'O',
  1066. '\u1ECC': 'O',
  1067. '\u1ED8': 'O',
  1068. '\u01EA': 'O',
  1069. '\u01EC': 'O',
  1070. '\u00D8': 'O',
  1071. '\u01FE': 'O',
  1072. '\u0186': 'O',
  1073. '\u019F': 'O',
  1074. '\uA74A': 'O',
  1075. '\uA74C': 'O',
  1076. '\u01A2': 'OI',
  1077. '\uA74E': 'OO',
  1078. '\u0222': 'OU',
  1079. '\u24C5': 'P',
  1080. '\uFF30': 'P',
  1081. '\u1E54': 'P',
  1082. '\u1E56': 'P',
  1083. '\u01A4': 'P',
  1084. '\u2C63': 'P',
  1085. '\uA750': 'P',
  1086. '\uA752': 'P',
  1087. '\uA754': 'P',
  1088. '\u24C6': 'Q',
  1089. '\uFF31': 'Q',
  1090. '\uA756': 'Q',
  1091. '\uA758': 'Q',
  1092. '\u024A': 'Q',
  1093. '\u24C7': 'R',
  1094. '\uFF32': 'R',
  1095. '\u0154': 'R',
  1096. '\u1E58': 'R',
  1097. '\u0158': 'R',
  1098. '\u0210': 'R',
  1099. '\u0212': 'R',
  1100. '\u1E5A': 'R',
  1101. '\u1E5C': 'R',
  1102. '\u0156': 'R',
  1103. '\u1E5E': 'R',
  1104. '\u024C': 'R',
  1105. '\u2C64': 'R',
  1106. '\uA75A': 'R',
  1107. '\uA7A6': 'R',
  1108. '\uA782': 'R',
  1109. '\u24C8': 'S',
  1110. '\uFF33': 'S',
  1111. '\u1E9E': 'S',
  1112. '\u015A': 'S',
  1113. '\u1E64': 'S',
  1114. '\u015C': 'S',
  1115. '\u1E60': 'S',
  1116. '\u0160': 'S',
  1117. '\u1E66': 'S',
  1118. '\u1E62': 'S',
  1119. '\u1E68': 'S',
  1120. '\u0218': 'S',
  1121. '\u015E': 'S',
  1122. '\u2C7E': 'S',
  1123. '\uA7A8': 'S',
  1124. '\uA784': 'S',
  1125. '\u24C9': 'T',
  1126. '\uFF34': 'T',
  1127. '\u1E6A': 'T',
  1128. '\u0164': 'T',
  1129. '\u1E6C': 'T',
  1130. '\u021A': 'T',
  1131. '\u0162': 'T',
  1132. '\u1E70': 'T',
  1133. '\u1E6E': 'T',
  1134. '\u0166': 'T',
  1135. '\u01AC': 'T',
  1136. '\u01AE': 'T',
  1137. '\u023E': 'T',
  1138. '\uA786': 'T',
  1139. '\uA728': 'TZ',
  1140. '\u24CA': 'U',
  1141. '\uFF35': 'U',
  1142. '\u00D9': 'U',
  1143. '\u00DA': 'U',
  1144. '\u00DB': 'U',
  1145. '\u0168': 'U',
  1146. '\u1E78': 'U',
  1147. '\u016A': 'U',
  1148. '\u1E7A': 'U',
  1149. '\u016C': 'U',
  1150. '\u00DC': 'U',
  1151. '\u01DB': 'U',
  1152. '\u01D7': 'U',
  1153. '\u01D5': 'U',
  1154. '\u01D9': 'U',
  1155. '\u1EE6': 'U',
  1156. '\u016E': 'U',
  1157. '\u0170': 'U',
  1158. '\u01D3': 'U',
  1159. '\u0214': 'U',
  1160. '\u0216': 'U',
  1161. '\u01AF': 'U',
  1162. '\u1EEA': 'U',
  1163. '\u1EE8': 'U',
  1164. '\u1EEE': 'U',
  1165. '\u1EEC': 'U',
  1166. '\u1EF0': 'U',
  1167. '\u1EE4': 'U',
  1168. '\u1E72': 'U',
  1169. '\u0172': 'U',
  1170. '\u1E76': 'U',
  1171. '\u1E74': 'U',
  1172. '\u0244': 'U',
  1173. '\u24CB': 'V',
  1174. '\uFF36': 'V',
  1175. '\u1E7C': 'V',
  1176. '\u1E7E': 'V',
  1177. '\u01B2': 'V',
  1178. '\uA75E': 'V',
  1179. '\u0245': 'V',
  1180. '\uA760': 'VY',
  1181. '\u24CC': 'W',
  1182. '\uFF37': 'W',
  1183. '\u1E80': 'W',
  1184. '\u1E82': 'W',
  1185. '\u0174': 'W',
  1186. '\u1E86': 'W',
  1187. '\u1E84': 'W',
  1188. '\u1E88': 'W',
  1189. '\u2C72': 'W',
  1190. '\u24CD': 'X',
  1191. '\uFF38': 'X',
  1192. '\u1E8A': 'X',
  1193. '\u1E8C': 'X',
  1194. '\u24CE': 'Y',
  1195. '\uFF39': 'Y',
  1196. '\u1EF2': 'Y',
  1197. '\u00DD': 'Y',
  1198. '\u0176': 'Y',
  1199. '\u1EF8': 'Y',
  1200. '\u0232': 'Y',
  1201. '\u1E8E': 'Y',
  1202. '\u0178': 'Y',
  1203. '\u1EF6': 'Y',
  1204. '\u1EF4': 'Y',
  1205. '\u01B3': 'Y',
  1206. '\u024E': 'Y',
  1207. '\u1EFE': 'Y',
  1208. '\u24CF': 'Z',
  1209. '\uFF3A': 'Z',
  1210. '\u0179': 'Z',
  1211. '\u1E90': 'Z',
  1212. '\u017B': 'Z',
  1213. '\u017D': 'Z',
  1214. '\u1E92': 'Z',
  1215. '\u1E94': 'Z',
  1216. '\u01B5': 'Z',
  1217. '\u0224': 'Z',
  1218. '\u2C7F': 'Z',
  1219. '\u2C6B': 'Z',
  1220. '\uA762': 'Z',
  1221. '\u24D0': 'a',
  1222. '\uFF41': 'a',
  1223. '\u1E9A': 'a',
  1224. '\u00E0': 'a',
  1225. '\u00E1': 'a',
  1226. '\u00E2': 'a',
  1227. '\u1EA7': 'a',
  1228. '\u1EA5': 'a',
  1229. '\u1EAB': 'a',
  1230. '\u1EA9': 'a',
  1231. '\u00E3': 'a',
  1232. '\u0101': 'a',
  1233. '\u0103': 'a',
  1234. '\u1EB1': 'a',
  1235. '\u1EAF': 'a',
  1236. '\u1EB5': 'a',
  1237. '\u1EB3': 'a',
  1238. '\u0227': 'a',
  1239. '\u01E1': 'a',
  1240. '\u00E4': 'a',
  1241. '\u01DF': 'a',
  1242. '\u1EA3': 'a',
  1243. '\u00E5': 'a',
  1244. '\u01FB': 'a',
  1245. '\u01CE': 'a',
  1246. '\u0201': 'a',
  1247. '\u0203': 'a',
  1248. '\u1EA1': 'a',
  1249. '\u1EAD': 'a',
  1250. '\u1EB7': 'a',
  1251. '\u1E01': 'a',
  1252. '\u0105': 'a',
  1253. '\u2C65': 'a',
  1254. '\u0250': 'a',
  1255. '\uA733': 'aa',
  1256. '\u00E6': 'ae',
  1257. '\u01FD': 'ae',
  1258. '\u01E3': 'ae',
  1259. '\uA735': 'ao',
  1260. '\uA737': 'au',
  1261. '\uA739': 'av',
  1262. '\uA73B': 'av',
  1263. '\uA73D': 'ay',
  1264. '\u24D1': 'b',
  1265. '\uFF42': 'b',
  1266. '\u1E03': 'b',
  1267. '\u1E05': 'b',
  1268. '\u1E07': 'b',
  1269. '\u0180': 'b',
  1270. '\u0183': 'b',
  1271. '\u0253': 'b',
  1272. '\u24D2': 'c',
  1273. '\uFF43': 'c',
  1274. '\u0107': 'c',
  1275. '\u0109': 'c',
  1276. '\u010B': 'c',
  1277. '\u010D': 'c',
  1278. '\u00E7': 'c',
  1279. '\u1E09': 'c',
  1280. '\u0188': 'c',
  1281. '\u023C': 'c',
  1282. '\uA73F': 'c',
  1283. '\u2184': 'c',
  1284. '\u24D3': 'd',
  1285. '\uFF44': 'd',
  1286. '\u1E0B': 'd',
  1287. '\u010F': 'd',
  1288. '\u1E0D': 'd',
  1289. '\u1E11': 'd',
  1290. '\u1E13': 'd',
  1291. '\u1E0F': 'd',
  1292. '\u0111': 'd',
  1293. '\u018C': 'd',
  1294. '\u0256': 'd',
  1295. '\u0257': 'd',
  1296. '\uA77A': 'd',
  1297. '\u01F3': 'dz',
  1298. '\u01C6': 'dz',
  1299. '\u24D4': 'e',
  1300. '\uFF45': 'e',
  1301. '\u00E8': 'e',
  1302. '\u00E9': 'e',
  1303. '\u00EA': 'e',
  1304. '\u1EC1': 'e',
  1305. '\u1EBF': 'e',
  1306. '\u1EC5': 'e',
  1307. '\u1EC3': 'e',
  1308. '\u1EBD': 'e',
  1309. '\u0113': 'e',
  1310. '\u1E15': 'e',
  1311. '\u1E17': 'e',
  1312. '\u0115': 'e',
  1313. '\u0117': 'e',
  1314. '\u00EB': 'e',
  1315. '\u1EBB': 'e',
  1316. '\u011B': 'e',
  1317. '\u0205': 'e',
  1318. '\u0207': 'e',
  1319. '\u1EB9': 'e',
  1320. '\u1EC7': 'e',
  1321. '\u0229': 'e',
  1322. '\u1E1D': 'e',
  1323. '\u0119': 'e',
  1324. '\u1E19': 'e',
  1325. '\u1E1B': 'e',
  1326. '\u0247': 'e',
  1327. '\u025B': 'e',
  1328. '\u01DD': 'e',
  1329. '\u24D5': 'f',
  1330. '\uFF46': 'f',
  1331. '\u1E1F': 'f',
  1332. '\u0192': 'f',
  1333. '\uA77C': 'f',
  1334. '\u24D6': 'g',
  1335. '\uFF47': 'g',
  1336. '\u01F5': 'g',
  1337. '\u011D': 'g',
  1338. '\u1E21': 'g',
  1339. '\u011F': 'g',
  1340. '\u0121': 'g',
  1341. '\u01E7': 'g',
  1342. '\u0123': 'g',
  1343. '\u01E5': 'g',
  1344. '\u0260': 'g',
  1345. '\uA7A1': 'g',
  1346. '\u1D79': 'g',
  1347. '\uA77F': 'g',
  1348. '\u24D7': 'h',
  1349. '\uFF48': 'h',
  1350. '\u0125': 'h',
  1351. '\u1E23': 'h',
  1352. '\u1E27': 'h',
  1353. '\u021F': 'h',
  1354. '\u1E25': 'h',
  1355. '\u1E29': 'h',
  1356. '\u1E2B': 'h',
  1357. '\u1E96': 'h',
  1358. '\u0127': 'h',
  1359. '\u2C68': 'h',
  1360. '\u2C76': 'h',
  1361. '\u0265': 'h',
  1362. '\u0195': 'hv',
  1363. '\u24D8': 'i',
  1364. '\uFF49': 'i',
  1365. '\u00EC': 'i',
  1366. '\u00ED': 'i',
  1367. '\u00EE': 'i',
  1368. '\u0129': 'i',
  1369. '\u012B': 'i',
  1370. '\u012D': 'i',
  1371. '\u00EF': 'i',
  1372. '\u1E2F': 'i',
  1373. '\u1EC9': 'i',
  1374. '\u01D0': 'i',
  1375. '\u0209': 'i',
  1376. '\u020B': 'i',
  1377. '\u1ECB': 'i',
  1378. '\u012F': 'i',
  1379. '\u1E2D': 'i',
  1380. '\u0268': 'i',
  1381. '\u0131': 'i',
  1382. '\u24D9': 'j',
  1383. '\uFF4A': 'j',
  1384. '\u0135': 'j',
  1385. '\u01F0': 'j',
  1386. '\u0249': 'j',
  1387. '\u24DA': 'k',
  1388. '\uFF4B': 'k',
  1389. '\u1E31': 'k',
  1390. '\u01E9': 'k',
  1391. '\u1E33': 'k',
  1392. '\u0137': 'k',
  1393. '\u1E35': 'k',
  1394. '\u0199': 'k',
  1395. '\u2C6A': 'k',
  1396. '\uA741': 'k',
  1397. '\uA743': 'k',
  1398. '\uA745': 'k',
  1399. '\uA7A3': 'k',
  1400. '\u24DB': 'l',
  1401. '\uFF4C': 'l',
  1402. '\u0140': 'l',
  1403. '\u013A': 'l',
  1404. '\u013E': 'l',
  1405. '\u1E37': 'l',
  1406. '\u1E39': 'l',
  1407. '\u013C': 'l',
  1408. '\u1E3D': 'l',
  1409. '\u1E3B': 'l',
  1410. '\u017F': 'l',
  1411. '\u0142': 'l',
  1412. '\u019A': 'l',
  1413. '\u026B': 'l',
  1414. '\u2C61': 'l',
  1415. '\uA749': 'l',
  1416. '\uA781': 'l',
  1417. '\uA747': 'l',
  1418. '\u01C9': 'lj',
  1419. '\u24DC': 'm',
  1420. '\uFF4D': 'm',
  1421. '\u1E3F': 'm',
  1422. '\u1E41': 'm',
  1423. '\u1E43': 'm',
  1424. '\u0271': 'm',
  1425. '\u026F': 'm',
  1426. '\u24DD': 'n',
  1427. '\uFF4E': 'n',
  1428. '\u01F9': 'n',
  1429. '\u0144': 'n',
  1430. '\u00F1': 'n',
  1431. '\u1E45': 'n',
  1432. '\u0148': 'n',
  1433. '\u1E47': 'n',
  1434. '\u0146': 'n',
  1435. '\u1E4B': 'n',
  1436. '\u1E49': 'n',
  1437. '\u019E': 'n',
  1438. '\u0272': 'n',
  1439. '\u0149': 'n',
  1440. '\uA791': 'n',
  1441. '\uA7A5': 'n',
  1442. '\u01CC': 'nj',
  1443. '\u24DE': 'o',
  1444. '\uFF4F': 'o',
  1445. '\u00F2': 'o',
  1446. '\u00F3': 'o',
  1447. '\u00F4': 'o',
  1448. '\u1ED3': 'o',
  1449. '\u1ED1': 'o',
  1450. '\u1ED7': 'o',
  1451. '\u1ED5': 'o',
  1452. '\u00F5': 'o',
  1453. '\u1E4D': 'o',
  1454. '\u022D': 'o',
  1455. '\u1E4F': 'o',
  1456. '\u014D': 'o',
  1457. '\u1E51': 'o',
  1458. '\u1E53': 'o',
  1459. '\u014F': 'o',
  1460. '\u022F': 'o',
  1461. '\u0231': 'o',
  1462. '\u00F6': 'o',
  1463. '\u022B': 'o',
  1464. '\u1ECF': 'o',
  1465. '\u0151': 'o',
  1466. '\u01D2': 'o',
  1467. '\u020D': 'o',
  1468. '\u020F': 'o',
  1469. '\u01A1': 'o',
  1470. '\u1EDD': 'o',
  1471. '\u1EDB': 'o',
  1472. '\u1EE1': 'o',
  1473. '\u1EDF': 'o',
  1474. '\u1EE3': 'o',
  1475. '\u1ECD': 'o',
  1476. '\u1ED9': 'o',
  1477. '\u01EB': 'o',
  1478. '\u01ED': 'o',
  1479. '\u00F8': 'o',
  1480. '\u01FF': 'o',
  1481. '\u0254': 'o',
  1482. '\uA74B': 'o',
  1483. '\uA74D': 'o',
  1484. '\u0275': 'o',
  1485. '\u01A3': 'oi',
  1486. '\u0223': 'ou',
  1487. '\uA74F': 'oo',
  1488. '\u24DF': 'p',
  1489. '\uFF50': 'p',
  1490. '\u1E55': 'p',
  1491. '\u1E57': 'p',
  1492. '\u01A5': 'p',
  1493. '\u1D7D': 'p',
  1494. '\uA751': 'p',
  1495. '\uA753': 'p',
  1496. '\uA755': 'p',
  1497. '\u24E0': 'q',
  1498. '\uFF51': 'q',
  1499. '\u024B': 'q',
  1500. '\uA757': 'q',
  1501. '\uA759': 'q',
  1502. '\u24E1': 'r',
  1503. '\uFF52': 'r',
  1504. '\u0155': 'r',
  1505. '\u1E59': 'r',
  1506. '\u0159': 'r',
  1507. '\u0211': 'r',
  1508. '\u0213': 'r',
  1509. '\u1E5B': 'r',
  1510. '\u1E5D': 'r',
  1511. '\u0157': 'r',
  1512. '\u1E5F': 'r',
  1513. '\u024D': 'r',
  1514. '\u027D': 'r',
  1515. '\uA75B': 'r',
  1516. '\uA7A7': 'r',
  1517. '\uA783': 'r',
  1518. '\u24E2': 's',
  1519. '\uFF53': 's',
  1520. '\u00DF': 's',
  1521. '\u015B': 's',
  1522. '\u1E65': 's',
  1523. '\u015D': 's',
  1524. '\u1E61': 's',
  1525. '\u0161': 's',
  1526. '\u1E67': 's',
  1527. '\u1E63': 's',
  1528. '\u1E69': 's',
  1529. '\u0219': 's',
  1530. '\u015F': 's',
  1531. '\u023F': 's',
  1532. '\uA7A9': 's',
  1533. '\uA785': 's',
  1534. '\u1E9B': 's',
  1535. '\u24E3': 't',
  1536. '\uFF54': 't',
  1537. '\u1E6B': 't',
  1538. '\u1E97': 't',
  1539. '\u0165': 't',
  1540. '\u1E6D': 't',
  1541. '\u021B': 't',
  1542. '\u0163': 't',
  1543. '\u1E71': 't',
  1544. '\u1E6F': 't',
  1545. '\u0167': 't',
  1546. '\u01AD': 't',
  1547. '\u0288': 't',
  1548. '\u2C66': 't',
  1549. '\uA787': 't',
  1550. '\uA729': 'tz',
  1551. '\u24E4': 'u',
  1552. '\uFF55': 'u',
  1553. '\u00F9': 'u',
  1554. '\u00FA': 'u',
  1555. '\u00FB': 'u',
  1556. '\u0169': 'u',
  1557. '\u1E79': 'u',
  1558. '\u016B': 'u',
  1559. '\u1E7B': 'u',
  1560. '\u016D': 'u',
  1561. '\u00FC': 'u',
  1562. '\u01DC': 'u',
  1563. '\u01D8': 'u',
  1564. '\u01D6': 'u',
  1565. '\u01DA': 'u',
  1566. '\u1EE7': 'u',
  1567. '\u016F': 'u',
  1568. '\u0171': 'u',
  1569. '\u01D4': 'u',
  1570. '\u0215': 'u',
  1571. '\u0217': 'u',
  1572. '\u01B0': 'u',
  1573. '\u1EEB': 'u',
  1574. '\u1EE9': 'u',
  1575. '\u1EEF': 'u',
  1576. '\u1EED': 'u',
  1577. '\u1EF1': 'u',
  1578. '\u1EE5': 'u',
  1579. '\u1E73': 'u',
  1580. '\u0173': 'u',
  1581. '\u1E77': 'u',
  1582. '\u1E75': 'u',
  1583. '\u0289': 'u',
  1584. '\u24E5': 'v',
  1585. '\uFF56': 'v',
  1586. '\u1E7D': 'v',
  1587. '\u1E7F': 'v',
  1588. '\u028B': 'v',
  1589. '\uA75F': 'v',
  1590. '\u028C': 'v',
  1591. '\uA761': 'vy',
  1592. '\u24E6': 'w',
  1593. '\uFF57': 'w',
  1594. '\u1E81': 'w',
  1595. '\u1E83': 'w',
  1596. '\u0175': 'w',
  1597. '\u1E87': 'w',
  1598. '\u1E85': 'w',
  1599. '\u1E98': 'w',
  1600. '\u1E89': 'w',
  1601. '\u2C73': 'w',
  1602. '\u24E7': 'x',
  1603. '\uFF58': 'x',
  1604. '\u1E8B': 'x',
  1605. '\u1E8D': 'x',
  1606. '\u24E8': 'y',
  1607. '\uFF59': 'y',
  1608. '\u1EF3': 'y',
  1609. '\u00FD': 'y',
  1610. '\u0177': 'y',
  1611. '\u1EF9': 'y',
  1612. '\u0233': 'y',
  1613. '\u1E8F': 'y',
  1614. '\u00FF': 'y',
  1615. '\u1EF7': 'y',
  1616. '\u1E99': 'y',
  1617. '\u1EF5': 'y',
  1618. '\u01B4': 'y',
  1619. '\u024F': 'y',
  1620. '\u1EFF': 'y',
  1621. '\u24E9': 'z',
  1622. '\uFF5A': 'z',
  1623. '\u017A': 'z',
  1624. '\u1E91': 'z',
  1625. '\u017C': 'z',
  1626. '\u017E': 'z',
  1627. '\u1E93': 'z',
  1628. '\u1E95': 'z',
  1629. '\u01B6': 'z',
  1630. '\u0225': 'z',
  1631. '\u0240': 'z',
  1632. '\u2C6C': 'z',
  1633. '\uA763': 'z',
  1634. '\u0386': '\u0391',
  1635. '\u0388': '\u0395',
  1636. '\u0389': '\u0397',
  1637. '\u038A': '\u0399',
  1638. '\u03AA': '\u0399',
  1639. '\u038C': '\u039F',
  1640. '\u038E': '\u03A5',
  1641. '\u03AB': '\u03A5',
  1642. '\u038F': '\u03A9',
  1643. '\u03AC': '\u03B1',
  1644. '\u03AD': '\u03B5',
  1645. '\u03AE': '\u03B7',
  1646. '\u03AF': '\u03B9',
  1647. '\u03CA': '\u03B9',
  1648. '\u0390': '\u03B9',
  1649. '\u03CC': '\u03BF',
  1650. '\u03CD': '\u03C5',
  1651. '\u03CB': '\u03C5',
  1652. '\u03B0': '\u03C5',
  1653. '\u03C9': '\u03C9',
  1654. '\u03C2': '\u03C3'
  1655. };
  1656. return diacritics;
  1657. });
  1658. define('select2/data/base',[
  1659. '../utils'
  1660. ], function (Utils) {
  1661. function BaseAdapter ($element, options) {
  1662. BaseAdapter.__super__.constructor.call(this);
  1663. }
  1664. Utils.Extend(BaseAdapter, Utils.Observable);
  1665. BaseAdapter.prototype.current = function (callback) {
  1666. throw new Error('The `current` method must be defined in child classes.');
  1667. };
  1668. BaseAdapter.prototype.query = function (params, callback) {
  1669. throw new Error('The `query` method must be defined in child classes.');
  1670. };
  1671. BaseAdapter.prototype.bind = function (container, $container) {
  1672. // Can be implemented in subclasses
  1673. };
  1674. BaseAdapter.prototype.destroy = function () {
  1675. // Can be implemented in subclasses
  1676. };
  1677. BaseAdapter.prototype.generateResultId = function (container, data) {
  1678. var id = container.id + '-result-';
  1679. id += Utils.generateChars(4);
  1680. if (data.id != null) {
  1681. id += '-' + data.id.toString();
  1682. } else {
  1683. id += '-' + Utils.generateChars(4);
  1684. }
  1685. return id;
  1686. };
  1687. return BaseAdapter;
  1688. });
  1689. define('select2/data/select',[
  1690. './base',
  1691. '../utils',
  1692. 'jquery'
  1693. ], function (BaseAdapter, Utils, $) {
  1694. function SelectAdapter ($element, options) {
  1695. this.$element = $element;
  1696. this.options = options;
  1697. SelectAdapter.__super__.constructor.call(this);
  1698. }
  1699. Utils.Extend(SelectAdapter, BaseAdapter);
  1700. SelectAdapter.prototype.current = function (callback) {
  1701. var data = [];
  1702. var self = this;
  1703. this.$element.find(':selected').each(function () {
  1704. var $option = $(this);
  1705. var option = self.item($option);
  1706. data.push(option);
  1707. });
  1708. callback(data);
  1709. };
  1710. SelectAdapter.prototype.select = function (data) {
  1711. var self = this;
  1712. if (this.$element.prop('multiple')) {
  1713. this.current(function (currentData) {
  1714. var val = [];
  1715. data = [data];
  1716. data.push.apply(data, currentData);
  1717. for (var d = 0; d < data.length; d++) {
  1718. id = data[d].id;
  1719. if (val.indexOf(id) === -1) {
  1720. val.push(id);
  1721. }
  1722. }
  1723. self.$element.val(val);
  1724. self.$element.trigger('change');
  1725. });
  1726. } else {
  1727. var val = data.id;
  1728. this.$element.val(val);
  1729. this.$element.trigger('change');
  1730. }
  1731. };
  1732. SelectAdapter.prototype.unselect = function (data) {
  1733. var self = this;
  1734. if (!this.$element.prop('multiple')) {
  1735. return;
  1736. }
  1737. this.current(function (currentData) {
  1738. var val = [];
  1739. for (var d = 0; d < currentData.length; d++) {
  1740. id = currentData[d].id;
  1741. if (id !== data.id && val.indexOf(id) === -1) {
  1742. val.push(id);
  1743. }
  1744. }
  1745. self.$element.val(val);
  1746. self.$element.trigger('change');
  1747. });
  1748. };
  1749. SelectAdapter.prototype.bind = function (container, $container) {
  1750. var self = this;
  1751. this.container = container;
  1752. container.on('select', function (params) {
  1753. self.select(params.data);
  1754. });
  1755. container.on('unselect', function (params) {
  1756. self.unselect(params.data);
  1757. });
  1758. };
  1759. SelectAdapter.prototype.destroy = function () {
  1760. // Remove anything added to child elements
  1761. this.$element.find('*').each(function () {
  1762. // Remove any custom data set by Select2
  1763. $.removeData(this, 'data');
  1764. });
  1765. };
  1766. SelectAdapter.prototype.query = function (params, callback) {
  1767. var data = [];
  1768. var self = this;
  1769. var $options = this.$element.children();
  1770. $options.each(function () {
  1771. var $option = $(this);
  1772. if (!$option.is('option') && !$option.is('optgroup')) {
  1773. return;
  1774. }
  1775. var option = self.item($option);
  1776. var matches = self.matches(params, option);
  1777. if (matches !== null) {
  1778. data.push(matches);
  1779. }
  1780. });
  1781. callback(data);
  1782. };
  1783. SelectAdapter.prototype.option = function (data) {
  1784. var option = document.createElement('option');
  1785. option.value = data.id;
  1786. if (data.disabled) {
  1787. option.disabled = true;
  1788. }
  1789. if (data.selected) {
  1790. option.selected = true;
  1791. }
  1792. option.innerText = data.text;
  1793. var $option = $(option);
  1794. var normalizedData = this._normalizeItem(data);
  1795. // Override the option's data with the combined data
  1796. $.data(option, 'data', normalizedData);
  1797. return $option;
  1798. };
  1799. SelectAdapter.prototype.item = function ($option) {
  1800. var data = {};
  1801. data = $.data($option[0], 'data');
  1802. if (data != null) {
  1803. return data;
  1804. }
  1805. if ($option.is('option')) {
  1806. data = {
  1807. id: $option.val(),
  1808. text: $option.html(),
  1809. disabled: $option.prop('disabled'),
  1810. selected: $option.prop('selected')
  1811. };
  1812. } else if ($option.is('optgroup')) {
  1813. data = {
  1814. text: $option.attr('label'),
  1815. children: []
  1816. };
  1817. var $children = $option.children('option');
  1818. var children = [];
  1819. for (var c = 0; c < $children.length; c++) {
  1820. var $child = $($children[c]);
  1821. var child = this.item($child);
  1822. children.push(child);
  1823. }
  1824. data.children = children;
  1825. }
  1826. data = this._normalizeItem(data);
  1827. $.data($option[0], 'data', data);
  1828. return data;
  1829. };
  1830. SelectAdapter.prototype._normalizeItem = function (item) {
  1831. if (item.text == null) {
  1832. item = {
  1833. id: item,
  1834. text: item
  1835. };
  1836. }
  1837. var defaults = {
  1838. selected: false,
  1839. disabled: false
  1840. };
  1841. if (item.id != null) {
  1842. item.id = item.id.toString();
  1843. }
  1844. if (item.text != null) {
  1845. item.text = item.text.toString();
  1846. }
  1847. if (item._resultId == null && item.id && this.container != null) {
  1848. item._resultId = this.generateResultId(this.container, item);
  1849. }
  1850. return $.extend({}, defaults, item);
  1851. };
  1852. SelectAdapter.prototype.matches = function (params, data) {
  1853. var matcher = this.options.get('matcher');
  1854. return matcher(params, data);
  1855. };
  1856. return SelectAdapter;
  1857. });
  1858. define('select2/data/array',[
  1859. './select',
  1860. '../utils',
  1861. 'jquery'
  1862. ], function (SelectAdapter, Utils, $) {
  1863. function ArrayAdapter ($element, options) {
  1864. var data = options.get('data');
  1865. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1866. this.convertToOptions(data);
  1867. }
  1868. Utils.Extend(ArrayAdapter, SelectAdapter);
  1869. ArrayAdapter.prototype.select = function (data) {
  1870. var $option = this.$element.find('option[value="' + data.id + '"]');
  1871. if ($option.length === 0) {
  1872. $option = this.option(data);
  1873. this.$element.append($option);
  1874. }
  1875. ArrayAdapter.__super__.select.call(this, data);
  1876. };
  1877. ArrayAdapter.prototype.convertToOptions = function (data) {
  1878. var self = this;
  1879. var $existing = this.$element.find('option');
  1880. var existingIds = $existing.map(function () {
  1881. return self.item($(this)).id;
  1882. }).get();
  1883. // Filter out all items except for the one passed in the argument
  1884. function onlyItem (item) {
  1885. return function () {
  1886. return $(this).val() == item.id;
  1887. };
  1888. }
  1889. for (var d = 0; d < data.length; d++) {
  1890. var item = data[d];
  1891. item.id = item.id.toString();
  1892. // Skip items which were pre-loaded, only merge the data
  1893. if (existingIds.indexOf(item.id) >= 0) {
  1894. var $existingOption = $existing.filter(onlyItem(item));
  1895. var existingData = this.item($existingOption);
  1896. var newData = $.extend(true, {}, existingData, item);
  1897. var $newOption = this.option(existingData);
  1898. $existingOption.replaceWith($newOption);
  1899. continue;
  1900. }
  1901. var $option = this.option(item);
  1902. this.$element.append($option);
  1903. }
  1904. };
  1905. return ArrayAdapter;
  1906. });
  1907. define('select2/data/ajax',[
  1908. './array',
  1909. '../utils',
  1910. 'jquery'
  1911. ], function (ArrayAdapter, Utils, $) {
  1912. function AjaxAdapter ($element, options) {
  1913. this.ajaxOptions = options.get('ajax');
  1914. if (this.ajaxOptions.processResults != null) {
  1915. this.processResults = this.ajaxOptions.processResults;
  1916. }
  1917. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1918. }
  1919. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1920. AjaxAdapter.prototype.processResults = function (results) {
  1921. return results;
  1922. };
  1923. AjaxAdapter.prototype.query = function (params, callback) {
  1924. var matches = [];
  1925. var self = this;
  1926. if (this._request) {
  1927. this._request.abort();
  1928. this._request = null;
  1929. }
  1930. var options = $.extend({
  1931. type: 'GET'
  1932. }, this.ajaxOptions);
  1933. if (typeof options.url === 'function') {
  1934. options.url = options.url(params);
  1935. }
  1936. if (typeof options.data === 'function') {
  1937. options.data = options.data(params);
  1938. }
  1939. function request () {
  1940. var $request = $.ajax(options);
  1941. $request.success(function (data) {
  1942. var results = self.processResults(data);
  1943. callback(results);
  1944. });
  1945. self._request = $request;
  1946. }
  1947. if (this.ajaxOptions.delay && params.term !== '') {
  1948. if (this._queryTimeout) {
  1949. window.clearTimeout(this._queryTimeout);
  1950. }
  1951. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  1952. } else {
  1953. request();
  1954. }
  1955. };
  1956. return AjaxAdapter;
  1957. });
  1958. define('select2/data/tags',[
  1959. ], function () {
  1960. function Tags (decorated, $element, options) {
  1961. var tags = options.get('tags');
  1962. decorated.call(this, $element, options);
  1963. if ($.isArray(tags)) {
  1964. for (var t = 0; t < tags.length; t++) {
  1965. var tag = tags[t];
  1966. var item = this._normalizeItem(tag);
  1967. var $option = this.option(item);
  1968. this.$element.append($option);
  1969. }
  1970. }
  1971. }
  1972. Tags.prototype.query = function (decorated, params, callback) {
  1973. var self = this;
  1974. this._removeOldTags();
  1975. if (params.term == null || params.term === '' || params.page != null) {
  1976. decorated.call(this, params, callback);
  1977. return;
  1978. }
  1979. function wrapper (data, child) {
  1980. for (var i = 0; i < data.length; i++) {
  1981. var option = data[i];
  1982. var checkChildren = (
  1983. option.children != null && !wrapper(option.children, true)
  1984. );
  1985. var checkText = option.text === params.term;
  1986. if (checkText || checkChildren) {
  1987. if (child) {
  1988. return false;
  1989. }
  1990. callback(data);
  1991. return;
  1992. }
  1993. }
  1994. if (child) {
  1995. return true;
  1996. }
  1997. var tag = self.createTag(params);
  1998. var $option = self.option(tag);
  1999. $option.attr('data-select2-tag', true);
  2000. self.$element.append($option);
  2001. self.insertTag(data, tag);
  2002. callback(data);
  2003. }
  2004. decorated.call(this, params, wrapper);
  2005. };
  2006. Tags.prototype.createTag = function (decorated, params) {
  2007. return {
  2008. id: params.term,
  2009. text: params.term
  2010. };
  2011. };
  2012. Tags.prototype.insertTag = function (_, data, tag) {
  2013. data.unshift(tag);
  2014. };
  2015. Tags.prototype._removeOldTags = function (_) {
  2016. var tag = this._lastTag;
  2017. var $options = this.$element.find('option[data-select2-tag]');
  2018. $options.each(function () {
  2019. if (this.selected) {
  2020. return;
  2021. }
  2022. $(this).remove();
  2023. });
  2024. };
  2025. return Tags;
  2026. });
  2027. define('select2/data/minimumInputLength',[
  2028. ], function () {
  2029. function MinimumInputLength (decorated, $e, options) {
  2030. this.minimumInputLength = options.get('minimumInputLength');
  2031. decorated.call(this, $e, options);
  2032. }
  2033. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  2034. params.term = params.term || '';
  2035. if (params.term.length < this.minimumInputLength) {
  2036. this.trigger('results:message', {
  2037. message: 'inputTooShort',
  2038. args: {
  2039. minimum: this.minimumInputLength,
  2040. input: params.term,
  2041. params: params
  2042. }
  2043. });
  2044. return;
  2045. }
  2046. decorated.call(this, params, callback);
  2047. };
  2048. return MinimumInputLength;
  2049. });
  2050. define('select2/dropdown',[
  2051. './utils'
  2052. ], function (Utils) {
  2053. function Dropdown ($element, options) {
  2054. this.$element = $element;
  2055. }
  2056. Utils.Extend(Dropdown, Utils.Observable);
  2057. Dropdown.prototype.render = function () {
  2058. var $dropdown = $(
  2059. '<span class="select2-dropdown">' +
  2060. '<span class="select2-results"></span>' +
  2061. '</span>'
  2062. );
  2063. this.$dropdown = $dropdown;
  2064. return $dropdown;
  2065. };
  2066. Dropdown.prototype.destroy = function () {
  2067. // Remove the dropdown from the DOM
  2068. this.$dropdown.remove();
  2069. };
  2070. Dropdown.prototype.bind = function (container, $container) {
  2071. // Can be implemented in subclasses
  2072. };
  2073. return Dropdown;
  2074. });
  2075. define('select2/dropdown/search',[
  2076. '../utils'
  2077. ], function (Utils) {
  2078. function Search () { }
  2079. Search.prototype.render = function (decorated) {
  2080. var $rendered = decorated.call(this);
  2081. var $search = $(
  2082. '<span class="select2-search select2-search--dropdown">' +
  2083. '<input class="select2-search__field" type="search" tabindex="-1"' +
  2084. ' role="textbox" />' +
  2085. '</span>'
  2086. );
  2087. this.$searchContainer = $search;
  2088. this.$search = $search.find('input');
  2089. $rendered.prepend($search);
  2090. return $rendered;
  2091. };
  2092. Search.prototype.bind = function (decorated, container, $container) {
  2093. var self = this;
  2094. decorated.call(this, container, $container);
  2095. this.$search.on('keydown', function (evt) {
  2096. self.trigger('keypress', evt);
  2097. self._keyUpPrevented = evt.isDefaultPrevented();
  2098. });
  2099. this.$search.on('keyup', function (evt) {
  2100. self.handleSearch(evt);
  2101. });
  2102. container.on('open', function () {
  2103. self.$search.attr('tabindex', 0);
  2104. self.$search.focus();
  2105. });
  2106. container.on('close', function () {
  2107. self.$search.attr('tabindex', -1);
  2108. self.$search.val('');
  2109. });
  2110. container.on('results:all', function (params) {
  2111. if (params.query.term == null || params.query.term === '') {
  2112. var showSearch = self.showSearch(params);
  2113. if (showSearch) {
  2114. self.$searchContainer.show();
  2115. } else {
  2116. self.$searchContainer.hide();
  2117. }
  2118. }
  2119. });
  2120. };
  2121. Search.prototype.handleSearch = function (evt) {
  2122. if (!this._keyUpPrevented) {
  2123. var input = this.$search.val();
  2124. this.trigger('query', {
  2125. term: input
  2126. });
  2127. }
  2128. this._keyUpPrevented = false;
  2129. };
  2130. Search.prototype.showSearch = function (_, params) {
  2131. return true;
  2132. };
  2133. return Search;
  2134. });
  2135. define('select2/dropdown/hidePlaceholder',[
  2136. ], function () {
  2137. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  2138. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  2139. decorated.call(this, $element, options, dataAdapter);
  2140. }
  2141. HidePlaceholder.prototype.append = function (decorated, data) {
  2142. data = this.removePlaceholder(data);
  2143. decorated.call(this, data);
  2144. };
  2145. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  2146. if (typeof placeholder === 'string') {
  2147. placeholder = {
  2148. id: '',
  2149. text: placeholder
  2150. };
  2151. }
  2152. return placeholder;
  2153. };
  2154. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  2155. var modifiedData = data.slice(0);
  2156. for (var d = data.length - 1; d >= 0; d--) {
  2157. var item = data[d];
  2158. if (this.placeholder.id === item.id) {
  2159. modifiedData.splice(d, 1);
  2160. }
  2161. }
  2162. return modifiedData;
  2163. };
  2164. return HidePlaceholder;
  2165. });
  2166. define('select2/dropdown/infiniteScroll',[
  2167. 'jquery'
  2168. ], function ($) {
  2169. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  2170. this.lastParams = {};
  2171. decorated.call(this, $element, options, dataAdapter);
  2172. this.$loadingMore = this.createLoadingMore();
  2173. this.loading = false;
  2174. }
  2175. InfiniteScroll.prototype.append = function (decorated, data) {
  2176. this.$loadingMore.remove();
  2177. decorated.call(this, data);
  2178. if (data.length > 0) {
  2179. this.$results.append(this.$loadingMore);
  2180. }
  2181. this.loading = false;
  2182. };
  2183. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  2184. var self = this;
  2185. decorated.call(this, container, $container);
  2186. container.on('query', function (params) {
  2187. self.lastParams = params;
  2188. self.loading = true;
  2189. });
  2190. container.on('query:append', function (params) {
  2191. self.lastParams = params;
  2192. self.loading = true;
  2193. });
  2194. this.$results.on('scroll', function () {
  2195. var loadMoreVisible = $.contains(
  2196. document.documentElement,
  2197. self.$loadingMore[0]
  2198. );
  2199. if (self.loading || !loadMoreVisible) {
  2200. return;
  2201. }
  2202. var currentOffset = self.$results.offset().top +
  2203. self.$results.outerHeight(false);
  2204. var loadingMoreOffset = self.$loadingMore.offset().top +
  2205. self.$loadingMore.outerHeight(false);
  2206. if (currentOffset + 50 >= loadingMoreOffset) {
  2207. self.loadMore();
  2208. }
  2209. });
  2210. };
  2211. InfiniteScroll.prototype.loadMore = function () {
  2212. this.loading = true;
  2213. var params = $.extend({}, {page: 1}, this.lastParams);
  2214. params.page++;
  2215. this.trigger('query:append', params);
  2216. };
  2217. InfiniteScroll.prototype.createLoadingMore = function () {
  2218. var $option = $(
  2219. '<li class="option load-more" role="treeitem"></li>'
  2220. );
  2221. var message = this.options.get('translations').get('loadingMore');
  2222. $option.html(message(this.lastParams));
  2223. return $option;
  2224. };
  2225. return InfiniteScroll;
  2226. });
  2227. define('select2/i18n/en',[],function () {
  2228. return {
  2229. errorLoading: function () {
  2230. return 'The results could not be loaded.';
  2231. },
  2232. inputTooLong: function (args) {
  2233. var overChars = args.input.length - args.maximum;
  2234. var message = 'Please delete ' + overChars + ' character';
  2235. if (overChars != 1) {
  2236. message += 's';
  2237. }
  2238. return message;
  2239. },
  2240. inputTooShort: function (args) {
  2241. var remainingChars = args.minimum - args.input.length;
  2242. var message = 'Please enter ' + remainingChars + ' or more character';
  2243. if (remainingChars != 1) {
  2244. message += 's';
  2245. }
  2246. return message;
  2247. },
  2248. searching: function () {
  2249. return 'Searching…';
  2250. },
  2251. loadingMore: function () {
  2252. return 'Loading more results…';
  2253. },
  2254. maximumSelected: function (args) {
  2255. var message = 'You can only select' + args.maximum + ' item';
  2256. if (args.maximum != 1) {
  2257. message += 's';
  2258. }
  2259. return message;
  2260. },
  2261. noResults: function () {
  2262. return 'No results found';
  2263. }
  2264. };
  2265. });
  2266. define('select2/defaults',[
  2267. 'jquery',
  2268. './results',
  2269. './selection/single',
  2270. './selection/multiple',
  2271. './selection/placeholder',
  2272. './selection/search',
  2273. './utils',
  2274. './translation',
  2275. './diacritics',
  2276. './data/select',
  2277. './data/array',
  2278. './data/ajax',
  2279. './data/tags',
  2280. './data/minimumInputLength',
  2281. './dropdown',
  2282. './dropdown/search',
  2283. './dropdown/hidePlaceholder',
  2284. './dropdown/infiniteScroll',
  2285. './i18n/en'
  2286. ], function ($, ResultsList,
  2287. SingleSelection, MultipleSelection, Placeholder, SelectionSearch,
  2288. Utils, Translation, DIACRITICS,
  2289. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  2290. Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
  2291. EnglishTranslation) {
  2292. function Defaults () {
  2293. this.reset();
  2294. }
  2295. Defaults.prototype.apply = function (options) {
  2296. options = $.extend({}, this.defaults, options);
  2297. if (options.dataAdapter == null) {
  2298. if (options.ajax != null) {
  2299. options.dataAdapter = AjaxData;
  2300. } else if (options.data != null) {
  2301. options.dataAdapter = ArrayData;
  2302. } else {
  2303. options.dataAdapter = SelectData;
  2304. }
  2305. }
  2306. if (options.minimumInputLength > 0) {
  2307. options.dataAdapter = Utils.Decorate(
  2308. options.dataAdapter,
  2309. MinimumInputLength
  2310. );
  2311. }
  2312. if (options.tags != null) {
  2313. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  2314. }
  2315. if (options.resultsAdapter == null) {
  2316. options.resultsAdapter = ResultsList;
  2317. if (options.ajax != null) {
  2318. options.resultsAdapter = Utils.Decorate(
  2319. options.resultsAdapter,
  2320. InfiniteScroll
  2321. );
  2322. }
  2323. if (options.placeholder != null) {
  2324. options.resultsAdapter = Utils.Decorate(
  2325. options.resultsAdapter,
  2326. HidePlaceholder
  2327. );
  2328. }
  2329. }
  2330. if (options.dropdownAdapter == null) {
  2331. if (options.multiple) {
  2332. options.dropdownAdapter = Dropdown;
  2333. } else {
  2334. var SearchableDropdown = Utils.Decorate(Dropdown, DropdownSearch);
  2335. options.dropdownAdapter = SearchableDropdown;
  2336. }
  2337. }
  2338. if (options.selectionAdapter == null) {
  2339. if (options.multiple) {
  2340. options.selectionAdapter = MultipleSelection;
  2341. } else {
  2342. options.selectionAdapter = SingleSelection;
  2343. }
  2344. // Add the placeholder mixin if a placeholder was specified
  2345. if (options.placeholder != null) {
  2346. options.selectionAdapter = Utils.Decorate(
  2347. options.selectionAdapter,
  2348. Placeholder
  2349. );
  2350. }
  2351. if (options.multiple) {
  2352. options.selectionAdapter = Utils.Decorate(
  2353. options.selectionAdapter,
  2354. SelectionSearch
  2355. );
  2356. }
  2357. }
  2358. if (typeof options.language === 'string') {
  2359. options.language = [options.language];
  2360. }
  2361. if ($.isArray(options.language)) {
  2362. var languages = new Translation();
  2363. var languageNames = options.language.concat(this.defaults.language);
  2364. for (var l = 0; l < languageNames.length; l++) {
  2365. var name = languageNames[l];
  2366. var language = {};
  2367. try {
  2368. // Try to load it with the original name
  2369. language = Translation.loadPath(name);
  2370. } catch (e) {
  2371. // If we couldn't load it, check if it wasn't the full path
  2372. name = 'select2/i18n/' + name;
  2373. language = Translation.loadPath(name);
  2374. }
  2375. languages.extend(language);
  2376. }
  2377. options.translations = languages;
  2378. } else {
  2379. options.translations = new Translations(options.language);
  2380. }
  2381. return options;
  2382. };
  2383. Defaults.prototype.reset = function () {
  2384. function stripDiacritics (text) {
  2385. // Used 'uni range + named function' from http://jsperf.com/diacritics/18
  2386. function match(a) {
  2387. return DIACRITICS[a] || a;
  2388. }
  2389. return text.replace(/[^\u0000-\u007E]/g, match);
  2390. }
  2391. function matcher (params, data) {
  2392. // Always return the object if there is nothing to compare
  2393. if ($.trim(params.term) === '') {
  2394. return data;
  2395. }
  2396. // Do a recursive check for options with children
  2397. if (data.children && data.children.length > 0) {
  2398. // Clone the data object if there are children
  2399. // This is required as we modify the object to remove any non-matches
  2400. var match = $.extend(true, {}, data);
  2401. // Check each child of the option
  2402. for (var c = data.children.length - 1; c >= 0; c--) {
  2403. var child = data.children[c];
  2404. var matches = matcher(params, child);
  2405. // If there wasn't a match, remove the object in the array
  2406. if (matches == null) {
  2407. match.children.splice(c, 1);
  2408. }
  2409. }
  2410. // If any children matched, return the new object
  2411. if (match.children.length > 0) {
  2412. return match;
  2413. }
  2414. // If there were no matching children, check just the plain object
  2415. return matcher(params, match);
  2416. }
  2417. var original = stripDiacritics(data.text).toUpperCase();
  2418. var term = stripDiacritics(params.term).toUpperCase();
  2419. // Check if the text contains the term
  2420. if (original.indexOf(term) > -1) {
  2421. return data;
  2422. }
  2423. // If it doesn't contain the term, don't return anything
  2424. return null;
  2425. }
  2426. this.defaults = {
  2427. language: ['select2/i18n/en'],
  2428. matcher: matcher,
  2429. minimumInputLength: 0,
  2430. theme: 'default',
  2431. templateResult: function (result) {
  2432. return result.text;
  2433. },
  2434. templateSelection: function (selection) {
  2435. return selection.text;
  2436. }
  2437. };
  2438. };
  2439. var defaults = new Defaults();
  2440. return defaults;
  2441. });
  2442. define('select2/options',[
  2443. './defaults'
  2444. ], function (Defaults) {
  2445. function Options (options, $element) {
  2446. this.options = options;
  2447. if ($element != null) {
  2448. this.fromElement($element);
  2449. }
  2450. this.options = Defaults.apply(this.options);
  2451. }
  2452. Options.prototype.fromElement = function ($e) {
  2453. if (this.options.multiple == null) {
  2454. this.options.multiple = $e.prop('multiple');
  2455. }
  2456. return this;
  2457. };
  2458. Options.prototype.get = function (key) {
  2459. return this.options[key];
  2460. };
  2461. Options.prototype.set = function (key, val) {
  2462. this.options[key] = val;
  2463. };
  2464. return Options;
  2465. });
  2466. define('select2/core',[
  2467. 'jquery',
  2468. './options',
  2469. './utils',
  2470. './keys'
  2471. ], function ($, Options, Utils, KEYS) {
  2472. var Select2 = function ($element, options) {
  2473. if ($element.data('select2') != null) {
  2474. return;
  2475. }
  2476. this.$element = $element;
  2477. this.id = this._generateId($element);
  2478. options = options || {};
  2479. this.options = new Options(options, $element);
  2480. Select2.__super__.constructor.call(this);
  2481. // Set up containers and adapters
  2482. var DataAdapter = this.options.get('dataAdapter');
  2483. this.data = new DataAdapter($element, this.options);
  2484. var $container = this.render();
  2485. this._placeContainer($container);
  2486. var SelectionAdapter = this.options.get('selectionAdapter');
  2487. this.selection = new SelectionAdapter($element, this.options);
  2488. var $selection = this.selection.render();
  2489. this._placeSelection($selection);
  2490. var DropdownAdapter = this.options.get('dropdownAdapter');
  2491. this.dropdown = new DropdownAdapter($element, this.options);
  2492. var $dropdown = this.dropdown.render();
  2493. this._placeDropdown($dropdown);
  2494. var ResultsAdapter = this.options.get('resultsAdapter');
  2495. this.results = new ResultsAdapter($element, this.options, this.data);
  2496. var $results = this.results.render();
  2497. this._placeResults($results);
  2498. // Bind events
  2499. var self = this;
  2500. // Bind the container to all of the adapters
  2501. this._bindAdapters();
  2502. // Register any DOM event handlers
  2503. this._registerDomEvents();
  2504. // Register any internal event handlers
  2505. this._registerDataEvents();
  2506. this._registerSelectionEvents();
  2507. this._registerDropdownEvents();
  2508. this._registerResultsEvents();
  2509. this._registerEvents();
  2510. // Set the initial state
  2511. this.data.current(function (initialData) {
  2512. self.trigger('selection:update', {
  2513. data: initialData
  2514. });
  2515. });
  2516. // Hide the original select
  2517. $element.hide();
  2518. this._tabindex = $element.attr('tabindex') || 0;
  2519. $element.attr('tabindex', '-1');
  2520. $element.data('select2', this);
  2521. };
  2522. Utils.Extend(Select2, Utils.Observable);
  2523. Select2.prototype._generateId = function ($element) {
  2524. var id = '';
  2525. if ($element.attr('id') != null) {
  2526. id = $element.attr('id');
  2527. } else if ($element.attr('name') != null) {
  2528. id = $element.attr('name') + '-' + Utils.generateChars(2);
  2529. } else {
  2530. id = Utils.generateChars(4);
  2531. }
  2532. id = 'select2-' + id;
  2533. return id;
  2534. };
  2535. Select2.prototype._placeContainer = function ($container) {
  2536. $container.insertAfter(this.$element);
  2537. $container.width(this.$element.outerWidth(false));
  2538. };
  2539. Select2.prototype._placeSelection = function ($selection) {
  2540. var $selectionContainer = this.$container.find('.selection');
  2541. $selectionContainer.append($selection);
  2542. };
  2543. Select2.prototype._placeDropdown = function ($dropdown) {
  2544. this.$dropdown = $dropdown;
  2545. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  2546. $dropdownContainer.append($dropdown);
  2547. };
  2548. Select2.prototype._placeResults = function ($results) {
  2549. var $resultsContainer = this.$dropdown.find('.select2-results');
  2550. $resultsContainer.append($results);
  2551. };
  2552. Select2.prototype._bindAdapters = function () {
  2553. this.data.bind(this, this.$container);
  2554. this.selection.bind(this, this.$container);
  2555. this.dropdown.bind(this, this.$container);
  2556. this.results.bind(this, this.$container);
  2557. };
  2558. Select2.prototype._registerDomEvents = function () {
  2559. var self = this;
  2560. this.$element.on('change.select2', function () {
  2561. self.data.current(function (data) {
  2562. self.trigger('selection:update', {
  2563. data: data
  2564. });
  2565. });
  2566. });
  2567. };
  2568. Select2.prototype._registerDataEvents = function () {
  2569. var self = this;
  2570. this.data.on('results:message', function (params) {
  2571. self.trigger('results:message', params);
  2572. });
  2573. };
  2574. Select2.prototype._registerSelectionEvents = function () {
  2575. var self = this;
  2576. this.selection.on('open', function () {
  2577. self.open();
  2578. });
  2579. this.selection.on('close', function () {
  2580. self.close();
  2581. });
  2582. this.selection.on('toggle', function () {
  2583. self.toggleDropdown();
  2584. });
  2585. this.selection.on('results:select', function () {
  2586. self.trigger('results:select');
  2587. });
  2588. this.selection.on('results:previous', function () {
  2589. self.trigger('results:previous');
  2590. });
  2591. this.selection.on('results:next', function () {
  2592. self.trigger('results:next');
  2593. });
  2594. this.selection.on('unselected', function (params) {
  2595. self.trigger('unselect', params);
  2596. self.close();
  2597. });
  2598. this.selection.on('query', function (params) {
  2599. self.trigger('query', params);
  2600. });
  2601. this.selection.on('keypress', function (e) {
  2602. self.trigger('keypress', e);
  2603. });
  2604. };
  2605. Select2.prototype._registerDropdownEvents = function () {
  2606. var self = this;
  2607. this.dropdown.on('query', function (params) {
  2608. self.trigger('query', params);
  2609. });
  2610. this.dropdown.on('keypress', function (e) {
  2611. self.trigger('keypress', e);
  2612. });
  2613. };
  2614. Select2.prototype._registerResultsEvents = function () {
  2615. var self = this;
  2616. this.results.on('query:append', function (params) {
  2617. self.trigger('query:append', params);
  2618. });
  2619. this.results.on('selected', function (params) {
  2620. self.trigger('select', params);
  2621. self.close();
  2622. });
  2623. this.results.on('unselected', function (params) {
  2624. self.trigger('unselect', params);
  2625. self.close();
  2626. });
  2627. this.results.on('results:focus', function (params) {
  2628. self.trigger('results:focus', params);
  2629. });
  2630. };
  2631. Select2.prototype._registerEvents = function () {
  2632. var self = this;
  2633. this.on('open', function () {
  2634. self.$container.addClass('select2-container--open');
  2635. });
  2636. this.on('close', function () {
  2637. self.$container.removeClass('select2-container--open');
  2638. });
  2639. this.on('query', function (params) {
  2640. this.data.query(params, function (data) {
  2641. self.trigger('results:all', {
  2642. data: data,
  2643. query: params
  2644. });
  2645. });
  2646. });
  2647. this.on('query:append', function (params) {
  2648. this.data.query(params, function (data) {
  2649. self.trigger('results:append', {
  2650. data: data,
  2651. query: params
  2652. });
  2653. });
  2654. });
  2655. this.on('keypress', function (evt) {
  2656. var key = evt.which;
  2657. if (self.isOpen()) {
  2658. if (key === KEYS.ENTER) {
  2659. self.trigger('results:select');
  2660. evt.preventDefault();
  2661. } else if (key === KEYS.UP) {
  2662. self.trigger('results:previous');
  2663. evt.preventDefault();
  2664. } else if (key === KEYS.DOWN) {
  2665. self.trigger('results:next');
  2666. evt.preventDefault();
  2667. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  2668. self.close();
  2669. evt.preventDefault();
  2670. }
  2671. } else {
  2672. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  2673. self.open();
  2674. evt.preventDefault();
  2675. }
  2676. }
  2677. });
  2678. };
  2679. Select2.prototype.toggleDropdown = function () {
  2680. if (this.isOpen()) {
  2681. this.close();
  2682. } else {
  2683. this.open();
  2684. }
  2685. };
  2686. Select2.prototype.open = function () {
  2687. if (this.isOpen()) {
  2688. return;
  2689. }
  2690. this.trigger('query', {});
  2691. this.trigger('open');
  2692. };
  2693. Select2.prototype.close = function () {
  2694. if (!this.isOpen()) {
  2695. return;
  2696. }
  2697. this.trigger('close');
  2698. };
  2699. Select2.prototype.isOpen = function () {
  2700. return this.$container.hasClass('select2-container--open');
  2701. };
  2702. Select2.prototype.destroy = function () {
  2703. this.$container.remove();
  2704. this.$element.off('.select2');
  2705. this.$element.attr('tabindex', this._tabindex);
  2706. this.$element.show();
  2707. this.$element.removeData('select2');
  2708. this.data.destroy();
  2709. this.selection.destroy();
  2710. this.dropdown.destroy();
  2711. this.results.destroy();
  2712. this.data = null;
  2713. this.selection = null;
  2714. this.dropdown = null;
  2715. this.results = null;
  2716. };
  2717. Select2.prototype.render = function () {
  2718. var $container = $(
  2719. '<span class="select2 select2-container">' +
  2720. '<span class="selection"></span>' +
  2721. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  2722. '</span>'
  2723. );
  2724. this.$container = $container;
  2725. this.$container.addClass('select2-container--' + this.options.get('theme'));
  2726. $container.data('element', this.$element);
  2727. return $container;
  2728. };
  2729. return Select2;
  2730. });
  2731. define('jquery.select2',[
  2732. 'jquery',
  2733. 'select2/core'
  2734. ], function ($, Select2) {
  2735. if ($.fn.select2 == null) {
  2736. $.fn.select2 = function (options) {
  2737. options = options || {};
  2738. if (typeof options === 'object') {
  2739. this.each(function () {
  2740. var instance = new Select2($(this), options);
  2741. });
  2742. } else if (typeof options === 'string') {
  2743. var instance = this.data('select2');
  2744. var args = Array.prototype.slice.call(arguments, 1);
  2745. instance[options](args);
  2746. } else {
  2747. throw new Error('Invalid arguments for Select2: ' + options);
  2748. }
  2749. };
  2750. }
  2751. return Select2;
  2752. });
  2753. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());