select2.amd.js 71 KB

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