select2.amd.full.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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. var $message = $('<li role="treeitem" class="option"></li>');
  135. var message = this.options.get('translations').get(params.message);
  136. $message.text(message(params.args));
  137. this.$results.append($message);
  138. };
  139. Results.prototype.append = function (data) {
  140. var $options = [];
  141. if (data.length === 0) {
  142. if (this.$results.children().length === 0) {
  143. this.trigger('results:message', {
  144. message: 'noResults'
  145. });
  146. }
  147. return;
  148. }
  149. data = this.sort(data);
  150. for (var d = 0; d < data.length; d++) {
  151. var item = data[d];
  152. var $option = this.option(item);
  153. $options.push($option);
  154. }
  155. this.$results.append($options);
  156. };
  157. Results.prototype.sort = function (data) {
  158. return data;
  159. };
  160. Results.prototype.setClasses = function () {
  161. var self = this;
  162. this.data.current(function (selected) {
  163. var selectedIds = $.map(selected, function (s) {
  164. return s.id.toString();
  165. });
  166. var $options = self.$results.find('.option[aria-selected]');
  167. $options.each(function () {
  168. var $option = $(this);
  169. var item = $.data(this, 'data');
  170. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  171. $option.attr('aria-selected', 'true');
  172. } else {
  173. $option.attr('aria-selected', 'false');
  174. }
  175. });
  176. var $selected = $options.filter('[aria-selected=true]');
  177. // Check if there are any selected options
  178. if ($selected.length > 0) {
  179. // If there are selected options, highlight the first
  180. $selected.first().trigger('mouseenter');
  181. } else {
  182. // If there are no selected options, highlight the first option
  183. // in the dropdown
  184. $options.first().trigger('mouseenter');
  185. }
  186. });
  187. };
  188. Results.prototype.option = function (data) {
  189. var option = document.createElement('li');
  190. option.className = 'option';
  191. var attrs = {
  192. 'role': 'treeitem',
  193. 'aria-selected': 'false'
  194. };
  195. if (data.disabled) {
  196. delete attrs['aria-selected'];
  197. attrs['aria-disabled'] = 'true';
  198. }
  199. if (data.id == null) {
  200. delete attrs['aria-selected'];
  201. }
  202. if (data._resultId != null) {
  203. option.id = data._resultId;
  204. }
  205. if (data.children) {
  206. attrs.role = 'group';
  207. attrs['aria-label'] = data.text;
  208. delete attrs['aria-selected'];
  209. }
  210. for (var attr in attrs) {
  211. var val = attrs[attr];
  212. option.setAttribute(attr, val);
  213. }
  214. if (data.children) {
  215. var $option = $(option);
  216. var label = document.createElement('strong');
  217. label.className = 'group-label';
  218. var $label = $(label);
  219. this.template(data, label);
  220. var $children = [];
  221. for (var c = 0; c < data.children.length; c++) {
  222. var child = data.children[c];
  223. var $child = this.option(child);
  224. $children.push($child);
  225. }
  226. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  227. $childrenContainer.append($children);
  228. $option.append(label);
  229. $option.append($childrenContainer);
  230. } else {
  231. this.template(data, option);
  232. }
  233. $.data(option, 'data', data);
  234. return option;
  235. };
  236. Results.prototype.bind = function (container, $container) {
  237. var self = this;
  238. var id = container.id + '-results';
  239. this.$results.attr('id', id);
  240. container.on('results:all', function (params) {
  241. self.clear();
  242. self.append(params.data);
  243. if (container.isOpen()) {
  244. self.setClasses();
  245. }
  246. });
  247. container.on('results:append', function (params) {
  248. self.append(params.data);
  249. if (container.isOpen()) {
  250. self.setClasses();
  251. }
  252. });
  253. container.on('select', function () {
  254. if (!container.isOpen()) {
  255. return;
  256. }
  257. self.setClasses();
  258. });
  259. container.on('unselect', function () {
  260. if (!container.isOpen()) {
  261. return;
  262. }
  263. self.setClasses();
  264. });
  265. container.on('open', function () {
  266. // When the dropdown is open, aria-expended="true"
  267. self.$results.attr('aria-expanded', 'true');
  268. self.$results.attr('aria-hidden', 'false');
  269. self.setClasses();
  270. self.ensureHighlightVisible();
  271. });
  272. container.on('close', function () {
  273. // When the dropdown is closed, aria-expended="false"
  274. self.$results.attr('aria-expanded', 'false');
  275. self.$results.attr('aria-hidden', 'true');
  276. self.$results.removeAttr('aria-activedescendant');
  277. });
  278. container.on('results:select', function () {
  279. var $highlighted = self.$results.find('.highlighted');
  280. if ($highlighted.length === 0) {
  281. return;
  282. }
  283. var data = $highlighted.data('data');
  284. if ($highlighted.attr('aria-selected') == 'true') {
  285. self.trigger('unselected', {
  286. data: data
  287. });
  288. } else {
  289. self.trigger('selected', {
  290. data: data
  291. });
  292. }
  293. });
  294. container.on('results:previous', function () {
  295. var $highlighted = self.$results.find('.highlighted');
  296. var $options = self.$results.find('[aria-selected]');
  297. var currentIndex = $options.index($highlighted);
  298. // If we are already at te top, don't move further
  299. if (currentIndex === 0) {
  300. return;
  301. }
  302. var nextIndex = currentIndex - 1;
  303. // If none are highlighted, highlight the first
  304. if ($highlighted.length === 0) {
  305. nextIndex = 0;
  306. }
  307. var $next = $options.eq(nextIndex);
  308. $next.trigger('mouseenter');
  309. var currentOffset = self.$results.offset().top;
  310. var nextTop = $next.offset().top;
  311. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  312. if (nextIndex === 0) {
  313. self.$results.scrollTop(0);
  314. } else if (nextTop - currentOffset < 0) {
  315. self.$results.scrollTop(nextOffset);
  316. }
  317. });
  318. container.on('results:next', function () {
  319. var $highlighted = self.$results.find('.highlighted');
  320. var $options = self.$results.find('[aria-selected]');
  321. var currentIndex = $options.index($highlighted);
  322. var nextIndex = currentIndex + 1;
  323. // If we are at the last option, stay there
  324. if (nextIndex >= $options.length) {
  325. return;
  326. }
  327. var $next = $options.eq(nextIndex);
  328. $next.trigger('mouseenter');
  329. var currentOffset = self.$results.offset().top +
  330. self.$results.outerHeight(false);
  331. var nextBottom = $next.offset().top + $next.outerHeight(false);
  332. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  333. if (nextIndex === 0) {
  334. self.$results.scrollTop(0);
  335. } else if (nextBottom > currentOffset) {
  336. self.$results.scrollTop(nextOffset);
  337. }
  338. });
  339. container.on('results:focus', function (params) {
  340. params.element.addClass('highlighted');
  341. });
  342. container.on('results:message', function (params) {
  343. self.trigger('results:message', params);
  344. });
  345. this.on('results:message', function (params) {
  346. self.displayMessage(params);
  347. });
  348. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  349. var $this = $(this);
  350. var data = $this.data('data');
  351. if ($this.attr('aria-selected') === 'true') {
  352. self.trigger('unselected', {
  353. originalEvent: evt,
  354. data: data
  355. });
  356. return;
  357. }
  358. self.trigger('selected', {
  359. originalEvent: evt,
  360. data: data
  361. });
  362. });
  363. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  364. var data = $(this).data('data');
  365. self.$results.find('.option.highlighted').removeClass('highlighted');
  366. self.trigger('results:focus', {
  367. data: data,
  368. element: $(this)
  369. });
  370. });
  371. };
  372. Results.prototype.destroy = function () {
  373. this.$results.remove();
  374. };
  375. Results.prototype.ensureHighlightVisible = function () {
  376. var $highlighted = this.$results.find('.highlighted');
  377. if ($highlighted.length === 0) {
  378. return;
  379. }
  380. var $options = this.$results.find('[aria-selected]');
  381. var currentIndex = $options.index($highlighted);
  382. var currentOffset = this.$results.offset().top;
  383. var nextTop = $highlighted.offset().top;
  384. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  385. var offsetDelta = nextTop - currentOffset;
  386. nextOffset -= $highlighted.outerHeight(false) * 2;
  387. if (currentIndex <= 2) {
  388. this.$results.scrollTop(0);
  389. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  390. this.$results.scrollTop(nextOffset);
  391. }
  392. };
  393. Results.prototype.template = function (result, container) {
  394. var template = this.options.get('templateResult');
  395. container.innerHTML = template(result);
  396. };
  397. return Results;
  398. });
  399. define('select2/selection/base',[
  400. '../utils'
  401. ], function (Utils) {
  402. function BaseSelection ($element, options) {
  403. this.$element = $element;
  404. this.options = options;
  405. BaseSelection.__super__.constructor.call(this);
  406. }
  407. Utils.Extend(BaseSelection, Utils.Observable);
  408. BaseSelection.prototype.render = function () {
  409. throw new Error('The `render` method must be defined in child classes.');
  410. };
  411. BaseSelection.prototype.bind = function (container, $container) {
  412. var self = this;
  413. var id = container.id + '-container';
  414. var resultsId = container.id + '-results';
  415. this.$selection.attr('aria-owns', resultsId);
  416. this.$selection.on('keydown', function (evt) {
  417. self.trigger('keypress', evt);
  418. if (evt.which === KEYS.SPACE) {
  419. evt.preventDefault();
  420. }
  421. });
  422. container.on('results:focus', function (params) {
  423. self.$selection.attr('aria-activedescendant', params.data._resultId);
  424. });
  425. container.on('selection:update', function (params) {
  426. self.update(params.data);
  427. });
  428. container.on('open', function () {
  429. // When the dropdown is open, aria-expanded="true"
  430. self.$selection.attr('aria-expanded', 'true');
  431. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  432. var $target = $(e.target);
  433. var $select = $target.closest('.select2');
  434. var $all = $('.select2.open');
  435. $all.each(function () {
  436. var $this = $(this);
  437. if (this == $select[0]) {
  438. return;
  439. }
  440. var $element = $this.data('element');
  441. $element.select2('close');
  442. });
  443. });
  444. });
  445. container.on('close', function () {
  446. // When the dropdown is closed, aria-expanded="false"
  447. self.$selection.attr('aria-expanded', 'false');
  448. self.$selection.removeAttr('aria-activedescendant');
  449. self.$selection.focus();
  450. $(document.body).off('mousedown.select2.' + container.id);
  451. });
  452. };
  453. BaseSelection.prototype.destroy = function () {
  454. // Unbind the dropdown click handler if it exists
  455. $(document.body).off('.select2.' + container.id);
  456. };
  457. BaseSelection.prototype.update = function (data) {
  458. throw new Error('The `update` method must be defined in child classes.');
  459. };
  460. return BaseSelection;
  461. });
  462. define('select2/keys',[
  463. ], function () {
  464. var KEYS = {
  465. BACKSPACE: 8,
  466. TAB: 9,
  467. ENTER: 13,
  468. SHIFT: 16,
  469. CTRL: 17,
  470. ALT: 18,
  471. ESC: 27,
  472. SPACE: 32,
  473. PAGE_UP: 33,
  474. PAGE_DOWN: 34,
  475. END: 35,
  476. HOME: 36,
  477. LEFT: 37,
  478. UP: 38,
  479. RIGHT: 39,
  480. DOWN: 40,
  481. DELETE: 46,
  482. isArrow: function (k) {
  483. k = k.which ? k.which : k;
  484. switch (k) {
  485. case KEY.LEFT:
  486. case KEY.RIGHT:
  487. case KEY.UP:
  488. case KEY.DOWN:
  489. return true;
  490. }
  491. return false;
  492. }
  493. };
  494. return KEYS;
  495. });
  496. define('select2/selection/single',[
  497. './base',
  498. '../utils',
  499. '../keys'
  500. ], function (BaseSelection, Utils, KEYS) {
  501. function SingleSelection () {
  502. SingleSelection.__super__.constructor.apply(this, arguments);
  503. }
  504. Utils.Extend(SingleSelection, BaseSelection);
  505. SingleSelection.prototype.render = function () {
  506. var $selection = $(
  507. '<span class="single-select" tabindex="0" role="combobox" ' +
  508. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  509. '<span class="rendered-selection"></span>' +
  510. '</span>'
  511. );
  512. $selection.attr('title', this.$element.attr('title'));
  513. this.$selection = $selection;
  514. return $selection;
  515. };
  516. SingleSelection.prototype.bind = function (container, $container) {
  517. var self = this;
  518. SingleSelection.__super__.bind.apply(this, arguments);
  519. var id = container.id + '-container';
  520. this.$selection.find('.rendered-selection').attr('id', id);
  521. this.$selection.attr('aria-labelledby', id);
  522. this.$selection.on('mousedown', function (evt) {
  523. // Only respond to left clicks
  524. if (evt.which !== 1) {
  525. return;
  526. }
  527. self.trigger('toggle', {
  528. originalEvent: evt
  529. });
  530. });
  531. this.$selection.on('focus', function (evt) {
  532. // User focuses on the container
  533. });
  534. this.$selection.on('blur', function (evt) {
  535. // User exits the container
  536. });
  537. container.on('selection:update', function (params) {
  538. self.update(params.data);
  539. });
  540. };
  541. SingleSelection.prototype.clear = function () {
  542. this.$selection.find('.rendered-selection').empty();
  543. };
  544. SingleSelection.prototype.display = function (data) {
  545. var template = this.options.get('templateSelection');
  546. return template(data);
  547. };
  548. SingleSelection.prototype.selectionContainer = function () {
  549. return $('<span></span>');
  550. };
  551. SingleSelection.prototype.update = function (data) {
  552. if (data.length === 0) {
  553. this.clear();
  554. return;
  555. }
  556. var selection = data[0];
  557. var formatted = this.display(selection);
  558. this.$selection.find('.rendered-selection').html(formatted);
  559. };
  560. return SingleSelection;
  561. });
  562. define('select2/selection/multiple',[
  563. './base',
  564. '../utils'
  565. ], function (BaseSelection, Utils) {
  566. function MultipleSelection ($element, options) {
  567. MultipleSelection.__super__.constructor.apply(this, arguments);
  568. }
  569. Utils.Extend(MultipleSelection, BaseSelection);
  570. MultipleSelection.prototype.render = function () {
  571. var $selection = $(
  572. '<span class="multiple-select" tabindex="0" role="combobox" ' +
  573. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  574. '<ul class="rendered-selection"></ul>' +
  575. '</span>'
  576. );
  577. $selection.attr('title', this.$element.attr('title'));
  578. this.$selection = $selection;
  579. return $selection;
  580. };
  581. MultipleSelection.prototype.bind = function (container, $container) {
  582. var self = this;
  583. MultipleSelection.__super__.bind.apply(this, arguments);
  584. this.$selection.on('click', function (evt) {
  585. self.trigger('toggle', {
  586. originalEvent: evt
  587. });
  588. });
  589. this.$selection.on('click', '.remove', function (evt) {
  590. var $remove = $(this);
  591. var $selection = $remove.parent();
  592. var data = $selection.data('data');
  593. self.trigger('unselected', {
  594. originalEvent: evt,
  595. data: data
  596. });
  597. });
  598. };
  599. MultipleSelection.prototype.clear = function () {
  600. this.$selection.find('.rendered-selection').empty();
  601. };
  602. MultipleSelection.prototype.display = function (data) {
  603. var template = this.options.get('templateSelection');
  604. return template(data);
  605. };
  606. MultipleSelection.prototype.selectionContainer = function () {
  607. var $container = $(
  608. '<li class="choice">' +
  609. '<span class="remove" role="presentation">&times;</span>' +
  610. '</li>'
  611. );
  612. return $container;
  613. };
  614. MultipleSelection.prototype.update = function (data) {
  615. this.clear();
  616. if (data.length === 0) {
  617. return;
  618. }
  619. var $selections = [];
  620. for (var d = 0; d < data.length; d++) {
  621. var selection = data[d];
  622. var formatted = this.display(selection);
  623. var $selection = this.selectionContainer();
  624. $selection.append(formatted);
  625. $selection.data('data', selection);
  626. $selections.push($selection);
  627. }
  628. this.$selection.find('.rendered-selection').append($selections);
  629. };
  630. return MultipleSelection;
  631. });
  632. define('select2/selection/placeholder',[
  633. '../utils'
  634. ], function (Utils) {
  635. function Placeholder (decorated, $element, options) {
  636. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  637. decorated.call(this, $element, options);
  638. }
  639. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  640. if (typeof placeholder === 'string') {
  641. placeholder = {
  642. id: '',
  643. text: placeholder
  644. };
  645. }
  646. return placeholder;
  647. };
  648. Placeholder.prototype.update = function (decorated, data) {
  649. var singlePlaceholder = (
  650. data.length == 1 && data[0].id != this.placeholder.id
  651. );
  652. var multipleSelections = data.length > 1;
  653. if (multipleSelections || singlePlaceholder) {
  654. return decorated.call(this, data);
  655. }
  656. this.clear();
  657. var $placeholder = this.selectionContainer();
  658. $placeholder.html(this.display(this.placeholder));
  659. $placeholder.addClass('placeholder').removeClass('choice');
  660. this.$selection.find('.rendered-selection').append($placeholder);
  661. };
  662. return Placeholder;
  663. });
  664. define('select2/translation',[
  665. ], function () {
  666. function Translation (dict) {
  667. this.dict = dict || {};
  668. }
  669. Translation.prototype.all = function () {
  670. return this.dict;
  671. };
  672. Translation.prototype.get = function (key) {
  673. return this.dict[key];
  674. };
  675. Translation.prototype.extend = function (translation) {
  676. this.dict = $.extend({}, translation.all(), this.dict);
  677. };
  678. // Static functions
  679. Translation._cache = {};
  680. Translation.loadPath = function (path) {
  681. if (!(path in Translation._cache)) {
  682. var translations = require(path);
  683. Translation._cache[path] = translations;
  684. }
  685. return new Translation(Translation._cache[path]);
  686. };
  687. return Translation;
  688. });
  689. define('select2/data/base',[
  690. '../utils'
  691. ], function (Utils) {
  692. function BaseAdapter ($element, options) {
  693. BaseAdapter.__super__.constructor.call(this);
  694. }
  695. Utils.Extend(BaseAdapter, Utils.Observable);
  696. BaseAdapter.prototype.current = function (callback) {
  697. throw new Error('The `current` method must be defined in child classes.');
  698. };
  699. BaseAdapter.prototype.query = function (params, callback) {
  700. throw new Error('The `query` method must be defined in child classes.');
  701. };
  702. BaseAdapter.prototype.bind = function (container, $container) {
  703. // Can be implemented in subclasses
  704. };
  705. BaseAdapter.prototype.destroy = function () {
  706. // Can be implemented in subclasses
  707. };
  708. BaseAdapter.prototype.generateResultId = function (container, data) {
  709. var id = container.id + '-result-';
  710. id += Utils.generateChars(4);
  711. if (data.id != null) {
  712. id += '-' + data.id.toString();
  713. } else {
  714. id += '-' + Utils.generateChars(4);
  715. }
  716. return id;
  717. };
  718. return BaseAdapter;
  719. });
  720. define('select2/data/select',[
  721. './base',
  722. '../utils',
  723. 'jquery'
  724. ], function (BaseAdapter, Utils, $) {
  725. function SelectAdapter ($element, options) {
  726. this.$element = $element;
  727. this.options = options;
  728. SelectAdapter.__super__.constructor.call(this);
  729. }
  730. Utils.Extend(SelectAdapter, BaseAdapter);
  731. SelectAdapter.prototype.current = function (callback) {
  732. var data = [];
  733. var self = this;
  734. this.$element.find(':selected').each(function () {
  735. var $option = $(this);
  736. var option = self.item($option);
  737. data.push(option);
  738. });
  739. callback(data);
  740. };
  741. SelectAdapter.prototype.select = function (data) {
  742. var self = this;
  743. if (this.$element.prop('multiple')) {
  744. this.current(function (currentData) {
  745. var val = [];
  746. data = [data];
  747. data.push.apply(data, currentData);
  748. for (var d = 0; d < data.length; d++) {
  749. id = data[d].id;
  750. if (val.indexOf(id) === -1) {
  751. val.push(id);
  752. }
  753. }
  754. self.$element.val(val);
  755. self.$element.trigger('change');
  756. });
  757. } else {
  758. var val = data.id;
  759. this.$element.val(val);
  760. this.$element.trigger('change');
  761. }
  762. };
  763. SelectAdapter.prototype.unselect = function (data) {
  764. var self = this;
  765. if (!this.$element.prop('multiple')) {
  766. return;
  767. }
  768. this.current(function (currentData) {
  769. var val = [];
  770. for (var d = 0; d < currentData.length; d++) {
  771. id = currentData[d].id;
  772. if (id !== data.id && val.indexOf(id) === -1) {
  773. val.push(id);
  774. }
  775. }
  776. self.$element.val(val);
  777. self.$element.trigger('change');
  778. });
  779. };
  780. SelectAdapter.prototype.bind = function (container, $container) {
  781. var self = this;
  782. this.container = container;
  783. container.on('select', function (params) {
  784. self.select(params.data);
  785. });
  786. container.on('unselect', function (params) {
  787. self.unselect(params.data);
  788. });
  789. };
  790. SelectAdapter.prototype.destroy = function () {
  791. // Remove anything added to child elements
  792. this.$element.find('*').each(function () {
  793. // Remove any custom data set by Select2
  794. $.removeData(this, 'data');
  795. });
  796. };
  797. SelectAdapter.prototype.query = function (params, callback) {
  798. var data = [];
  799. var self = this;
  800. var $options = this.$element.children();
  801. $options.each(function () {
  802. var $option = $(this);
  803. if (!$option.is('option') && !$option.is('optgroup')) {
  804. return;
  805. }
  806. var option = self.item($option);
  807. var matches = self.matches(params, option);
  808. if (matches !== null) {
  809. data.push(matches);
  810. }
  811. });
  812. callback(data);
  813. };
  814. SelectAdapter.prototype.option = function (data) {
  815. var option = document.createElement('option');
  816. option.value = data.id;
  817. if (data.disabled) {
  818. option.disabled = true;
  819. }
  820. if (data.selected) {
  821. option.selected = true;
  822. }
  823. option.innerText = data.text;
  824. var $option = $(option);
  825. var normalizedData = this._normalizeItem(data);
  826. // Override the option's data with the combined data
  827. $.data(option, 'data', normalizedData);
  828. return $option;
  829. };
  830. SelectAdapter.prototype.item = function ($option) {
  831. var data = {};
  832. data = $.data($option[0], 'data');
  833. if (data != null) {
  834. return data;
  835. }
  836. if ($option.is('option')) {
  837. data = {
  838. id: $option.val(),
  839. text: $option.html(),
  840. disabled: $option.prop('disabled'),
  841. selected: $option.prop('selected')
  842. };
  843. } else if ($option.is('optgroup')) {
  844. data = {
  845. text: $option.attr('label'),
  846. children: []
  847. };
  848. var $children = $option.children('option');
  849. var children = [];
  850. for (var c = 0; c < $children.length; c++) {
  851. var $child = $($children[c]);
  852. var child = this.item($child);
  853. children.push(child);
  854. }
  855. data.children = children;
  856. }
  857. data = this._normalizeItem(data);
  858. $.data($option[0], 'data', data);
  859. return data;
  860. };
  861. SelectAdapter.prototype._normalizeItem = function (item) {
  862. if (item.text == null) {
  863. item = {
  864. id: item,
  865. text: item
  866. };
  867. }
  868. var defaults = {
  869. selected: false,
  870. disabled: false
  871. };
  872. if (item.id != null) {
  873. item.id = item.id.toString();
  874. }
  875. if (item.text != null) {
  876. item.text = item.text.toString();
  877. }
  878. if (item._resultId == null && item.id && this.container != null) {
  879. item._resultId = this.generateResultId(this.container, item);
  880. }
  881. return $.extend({}, defaults, item);
  882. };
  883. SelectAdapter.prototype.matches = function (params, data) {
  884. var matcher = this.options.get('matcher');
  885. return matcher(params, data);
  886. };
  887. return SelectAdapter;
  888. });
  889. define('select2/data/array',[
  890. './select',
  891. '../utils',
  892. 'jquery'
  893. ], function (SelectAdapter, Utils, $) {
  894. function ArrayAdapter ($element, options) {
  895. var data = options.get('data');
  896. ArrayAdapter.__super__.constructor.call(this, $element, options);
  897. this.convertToOptions(data);
  898. }
  899. Utils.Extend(ArrayAdapter, SelectAdapter);
  900. ArrayAdapter.prototype.select = function (data) {
  901. var $option = this.$element.find('option[value="' + data.id + '"]');
  902. if ($option.length === 0) {
  903. $option = this.option(data);
  904. this.$element.append($option);
  905. }
  906. ArrayAdapter.__super__.select.call(this, data);
  907. };
  908. ArrayAdapter.prototype.convertToOptions = function (data) {
  909. var self = this;
  910. var $existing = this.$element.find('option');
  911. var existingIds = $existing.map(function () {
  912. return self.item($(this)).id;
  913. }).get();
  914. // Filter out all items except for the one passed in the argument
  915. function onlyItem (item) {
  916. return function () {
  917. return $(this).val() == item.id;
  918. };
  919. }
  920. for (var d = 0; d < data.length; d++) {
  921. var item = data[d];
  922. item.id = item.id.toString();
  923. // Skip items which were pre-loaded, only merge the data
  924. if (existingIds.indexOf(item.id) >= 0) {
  925. var $existingOption = $existing.filter(onlyItem(item));
  926. var existingData = this.item($existingOption);
  927. var newData = $.extend(true, {}, existingData, item);
  928. var $newOption = this.option(existingData);
  929. $existingOption.replaceWith($newOption);
  930. continue;
  931. }
  932. var $option = this.option(item);
  933. this.$element.append($option);
  934. }
  935. };
  936. return ArrayAdapter;
  937. });
  938. define('select2/data/ajax',[
  939. './array',
  940. '../utils',
  941. 'jquery'
  942. ], function (ArrayAdapter, Utils, $) {
  943. function AjaxAdapter ($element, options) {
  944. this.ajaxOptions = options.get('ajax');
  945. if (this.ajaxOptions.processResults != null) {
  946. this.processResults = this.ajaxOptions.processResults;
  947. }
  948. ArrayAdapter.__super__.constructor.call(this, $element, options);
  949. }
  950. Utils.Extend(AjaxAdapter, ArrayAdapter);
  951. AjaxAdapter.prototype.processResults = function (results) {
  952. return results;
  953. };
  954. AjaxAdapter.prototype.query = function (params, callback) {
  955. var matches = [];
  956. var self = this;
  957. var options = $.extend({
  958. type: 'GET'
  959. }, this.ajaxOptions);
  960. if (typeof options.url === 'function') {
  961. options.url = options.url(params);
  962. }
  963. if (typeof options.data === 'function') {
  964. options.data = options.data(params);
  965. }
  966. function request () {
  967. var $request = $.ajax(options);
  968. $request.success(function (data) {
  969. var results = self.processResults(data);
  970. callback(results);
  971. });
  972. }
  973. if (this.ajaxOptions.delay && params.term !== '') {
  974. if (this._queryTimeout) {
  975. window.clearTimeout(this._queryTimeout);
  976. }
  977. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  978. } else {
  979. request();
  980. }
  981. };
  982. return AjaxAdapter;
  983. });
  984. define('select2/data/tags',[
  985. ], function () {
  986. function Tags (decorated, $element, options) {
  987. var tags = options.get('tags');
  988. decorated.call(this, $element, options);
  989. if ($.isArray(tags)) {
  990. for (var t = 0; t < tags.length; t++) {
  991. var tag = tags[t];
  992. var item = this._normalizeItem(tag);
  993. var $option = this.option(item);
  994. this.$element.append($option);
  995. }
  996. }
  997. }
  998. Tags.prototype.query = function (decorated, params, callback) {
  999. var self = this;
  1000. this._removeOldTags();
  1001. if (params.term == null || params.term === '' || params.page != null) {
  1002. decorated.call(this, params, callback);
  1003. return;
  1004. }
  1005. function wrapper (data, child) {
  1006. for (var i = 0; i < data.length; i++) {
  1007. var option = data[i];
  1008. var checkChildren = (
  1009. option.children != null && !wrapper(option.children, true)
  1010. );
  1011. var checkText = option.text === params.term;
  1012. if (checkText || checkChildren) {
  1013. if (child) {
  1014. return false;
  1015. }
  1016. callback(data);
  1017. return;
  1018. }
  1019. }
  1020. if (child) {
  1021. return true;
  1022. }
  1023. var tag = self.createTag(params);
  1024. var $option = self.option(tag);
  1025. $option.attr('data-select2-tag', true);
  1026. self.$element.append($option);
  1027. self.insertTag(data, tag);
  1028. callback(data);
  1029. }
  1030. decorated.call(this, params, wrapper);
  1031. };
  1032. Tags.prototype.createTag = function (decorated, params) {
  1033. return {
  1034. id: params.term,
  1035. text: params.term
  1036. };
  1037. };
  1038. Tags.prototype.insertTag = function (_, data, tag) {
  1039. data.unshift(tag);
  1040. };
  1041. Tags.prototype._removeOldTags = function (_) {
  1042. var tag = this._lastTag;
  1043. var $options = this.$element.find('option[data-select2-tag]');
  1044. $options.each(function () {
  1045. if (this.selected) {
  1046. return;
  1047. }
  1048. $(this).remove();
  1049. });
  1050. };
  1051. return Tags;
  1052. });
  1053. define('select2/data/minimumInputLength',[
  1054. ], function () {
  1055. function MinimumInputLength (decorated, $e, options) {
  1056. this.minimumInputLength = options.get('minimumInputLength');
  1057. decorated.call(this, $e, options);
  1058. }
  1059. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1060. params.term = params.term || '';
  1061. if (params.term.length < this.minimumInputLength) {
  1062. this.trigger('results:message', {
  1063. message: 'inputTooShort',
  1064. args: {
  1065. minimum: this.minimumInputLength,
  1066. input: params.term,
  1067. params: params
  1068. }
  1069. });
  1070. return;
  1071. }
  1072. decorated.call(this, params, callback);
  1073. };
  1074. return MinimumInputLength;
  1075. });
  1076. define('select2/dropdown',[
  1077. './utils'
  1078. ], function (Utils) {
  1079. function Dropdown ($element, options) {
  1080. this.$element = $element;
  1081. }
  1082. Utils.Extend(Dropdown, Utils.Observable);
  1083. Dropdown.prototype.render = function () {
  1084. var $dropdown = $(
  1085. '<span class="dropdown">' +
  1086. '<span class="results"></span>' +
  1087. '</span>'
  1088. );
  1089. this.$dropdown = $dropdown;
  1090. return $dropdown;
  1091. };
  1092. Dropdown.prototype.destroy = function () {
  1093. // Remove the dropdown from the DOM
  1094. this.$dropdown.remove();
  1095. };
  1096. Dropdown.prototype.bind = function (container, $container) {
  1097. // Can be implemented in subclasses
  1098. };
  1099. return Dropdown;
  1100. });
  1101. define('select2/dropdown/search',[
  1102. '../utils'
  1103. ], function (Utils) {
  1104. function Search () { }
  1105. Search.prototype.render = function (decorated) {
  1106. var $rendered = decorated.call(this);
  1107. var $search = $(
  1108. '<span class="search">' +
  1109. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1110. '</span>'
  1111. );
  1112. this.$searchContainer = $search;
  1113. this.$search = $search.find('input');
  1114. $rendered.prepend($search);
  1115. return $rendered;
  1116. };
  1117. Search.prototype.bind = function (decorated, container, $container) {
  1118. var self = this;
  1119. decorated.call(this, container, $container);
  1120. this.$search.on('keydown', function (evt) {
  1121. self.trigger('keypress', evt);
  1122. self._keyUpPrevented = evt.isDefaultPrevented();
  1123. });
  1124. this.$search.on('keyup', function (evt) {
  1125. self.handleSearch(evt);
  1126. });
  1127. container.on('open', function () {
  1128. self.$search.attr('tabindex', 0);
  1129. self.$search.focus();
  1130. });
  1131. container.on('close', function () {
  1132. self.$search.attr('tabindex', -1);
  1133. self.$search.val('');
  1134. });
  1135. container.on('results:all', function (params) {
  1136. if (params.query.term == null || params.query.term === '') {
  1137. var showSearch = self.showSearch(params);
  1138. if (showSearch) {
  1139. self.$searchContainer.show();
  1140. } else {
  1141. self.$searchContainer.hide();
  1142. }
  1143. }
  1144. });
  1145. };
  1146. Search.prototype.handleSearch = function (evt) {
  1147. if (!this._keyUpPrevented) {
  1148. var input = this.$search.val();
  1149. this.trigger('query', {
  1150. term: input
  1151. });
  1152. }
  1153. this._keyUpPrevented = false;
  1154. };
  1155. Search.prototype.showSearch = function (_, params) {
  1156. return true;
  1157. };
  1158. return Search;
  1159. });
  1160. define('select2/dropdown/hidePlaceholder',[
  1161. ], function () {
  1162. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1163. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1164. decorated.call(this, $element, options, dataAdapter);
  1165. }
  1166. HidePlaceholder.prototype.append = function (decorated, data) {
  1167. data = this.removePlaceholder(data);
  1168. decorated.call(this, data);
  1169. };
  1170. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1171. if (typeof placeholder === 'string') {
  1172. placeholder = {
  1173. id: '',
  1174. text: placeholder
  1175. };
  1176. }
  1177. return placeholder;
  1178. };
  1179. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1180. var modifiedData = data.slice(0);
  1181. for (var d = data.length - 1; d >= 0; d--) {
  1182. var item = data[d];
  1183. if (this.placeholder.id === item.id) {
  1184. modifiedData.splice(d, 1);
  1185. }
  1186. }
  1187. return modifiedData;
  1188. };
  1189. return HidePlaceholder;
  1190. });
  1191. define('select2/dropdown/infiniteScroll',[
  1192. 'jquery'
  1193. ], function ($) {
  1194. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  1195. this.lastParams = {};
  1196. decorated.call(this, $element, options, dataAdapter);
  1197. this.$loadingMore = this.createLoadingMore();
  1198. this.loading = false;
  1199. }
  1200. InfiniteScroll.prototype.append = function (decorated, data) {
  1201. this.$loadingMore.remove();
  1202. decorated.call(this, data);
  1203. if (data.length > 0) {
  1204. this.$results.append(this.$loadingMore);
  1205. }
  1206. this.loading = false;
  1207. };
  1208. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  1209. var self = this;
  1210. decorated.call(this, container, $container);
  1211. container.on('query', function (params) {
  1212. self.lastParams = params;
  1213. self.loading = true;
  1214. });
  1215. container.on('query:append', function (params) {
  1216. self.lastParams = params;
  1217. self.loading = true;
  1218. });
  1219. this.$results.on('scroll', function () {
  1220. var loadMoreVisible = $.contains(
  1221. document.documentElement,
  1222. self.$loadingMore[0]
  1223. );
  1224. if (self.loading || !loadMoreVisible) {
  1225. return;
  1226. }
  1227. var currentOffset = self.$results.offset().top +
  1228. self.$results.outerHeight(false);
  1229. var loadingMoreOffset = self.$loadingMore.offset().top +
  1230. self.$loadingMore.outerHeight(false);
  1231. if (currentOffset + 50 >= loadingMoreOffset) {
  1232. self.loadMore();
  1233. }
  1234. });
  1235. };
  1236. InfiniteScroll.prototype.loadMore = function () {
  1237. this.loading = true;
  1238. var params = $.extend({}, {page: 1}, this.lastParams);
  1239. params.page++;
  1240. this.trigger('query:append', params);
  1241. };
  1242. InfiniteScroll.prototype.createLoadingMore = function () {
  1243. var $option = $(
  1244. '<li class="option load-more" role="treeitem"></li>'
  1245. );
  1246. var message = this.options.get('translations').get('loadingMore');
  1247. $option.html(message(this.lastParams));
  1248. return $option;
  1249. };
  1250. return InfiniteScroll;
  1251. });
  1252. define('select2/i18n/en',[],function () {
  1253. return {
  1254. inputTooShort: function (args) {
  1255. var remainingChars = args.minimum - args.input.length;
  1256. var message = 'Please enter ' + remainingChars + ' or more character';
  1257. if (remainingChars != 1) {
  1258. message += 's';
  1259. }
  1260. return message;
  1261. },
  1262. loadingMore: function () {
  1263. return 'Loading more results…';
  1264. },
  1265. noResults: function () {
  1266. return 'No results found';
  1267. }
  1268. };
  1269. });
  1270. define('select2/defaults',[
  1271. 'jquery',
  1272. './results',
  1273. './selection/single',
  1274. './selection/multiple',
  1275. './selection/placeholder',
  1276. './utils',
  1277. './translation',
  1278. './data/select',
  1279. './data/array',
  1280. './data/ajax',
  1281. './data/tags',
  1282. './data/minimumInputLength',
  1283. './dropdown',
  1284. './dropdown/search',
  1285. './dropdown/hidePlaceholder',
  1286. './dropdown/infiniteScroll',
  1287. './i18n/en'
  1288. ], function ($, ResultsList,
  1289. SingleSelection, MultipleSelection, Placeholder,
  1290. Utils, Translation,
  1291. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1292. Dropdown, Search, HidePlaceholder, InfiniteScroll,
  1293. EnglishTranslation) {
  1294. function Defaults () {
  1295. this.reset();
  1296. }
  1297. Defaults.prototype.apply = function (options) {
  1298. options = $.extend({}, this.defaults, options);
  1299. if (options.dataAdapter == null) {
  1300. if (options.ajax != null) {
  1301. options.dataAdapter = AjaxData;
  1302. } else if (options.data != null) {
  1303. options.dataAdapter = ArrayData;
  1304. } else {
  1305. options.dataAdapter = SelectData;
  1306. }
  1307. }
  1308. if (options.minimumInputLength > 0) {
  1309. options.dataAdapter = Utils.Decorate(
  1310. options.dataAdapter,
  1311. MinimumInputLength
  1312. );
  1313. }
  1314. if (options.tags != null) {
  1315. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1316. }
  1317. if (options.resultsAdapter == null) {
  1318. options.resultsAdapter = ResultsList;
  1319. if (options.ajax != null) {
  1320. options.resultsAdapter = Utils.Decorate(
  1321. options.resultsAdapter,
  1322. InfiniteScroll
  1323. );
  1324. }
  1325. if (options.placeholder != null) {
  1326. options.resultsAdapter = Utils.Decorate(
  1327. options.resultsAdapter,
  1328. HidePlaceholder
  1329. );
  1330. }
  1331. }
  1332. if (options.dropdownAdapter == null) {
  1333. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1334. options.dropdownAdapter = SearchableDropdown;
  1335. }
  1336. if (options.selectionAdapter == null) {
  1337. if (options.multiple) {
  1338. options.selectionAdapter = MultipleSelection;
  1339. } else {
  1340. options.selectionAdapter = SingleSelection;
  1341. }
  1342. // Add the placeholder mixin if a placeholder was specified
  1343. if (options.placeholder != null) {
  1344. options.selectionAdapter = Utils.Decorate(
  1345. options.selectionAdapter,
  1346. Placeholder
  1347. );
  1348. }
  1349. }
  1350. if (typeof options.language === 'string') {
  1351. options.language = [options.language];
  1352. }
  1353. if ($.isArray(options.language)) {
  1354. var languages = new Translation();
  1355. var languageNames = options.language.concat(this.defaults.language);
  1356. for (var l = 0; l < languageNames.length; l++) {
  1357. var name = languageNames[l];
  1358. var language = {};
  1359. try {
  1360. // Try to load it with the original name
  1361. language = Translation.loadPath(name);
  1362. } catch (e) {
  1363. // If we couldn't load it, check if it wasn't the full path
  1364. name = 'select2/i18n/' + name;
  1365. language = Translation.loadPath(name);
  1366. }
  1367. languages.extend(language);
  1368. }
  1369. options.translations = languages;
  1370. } else {
  1371. options.translations = new Translations(options.language);
  1372. }
  1373. return options;
  1374. };
  1375. Defaults.prototype.reset = function () {
  1376. function matcher (params, data) {
  1377. // Always return the object if there is nothing to compare
  1378. if ($.trim(params.term) === '') {
  1379. return data;
  1380. }
  1381. // Do a recursive check for options with children
  1382. if (data.children && data.children.length > 0) {
  1383. // Clone the data object if there are children
  1384. // This is required as we modify the object to remove any non-matches
  1385. var match = $.extend(true, {}, data);
  1386. // Check each child of the option
  1387. for (var c = data.children.length - 1; c >= 0; c--) {
  1388. var child = data.children[c];
  1389. var matches = matcher(params, child);
  1390. // If there wasn't a match, remove the object in the array
  1391. if (matches == null) {
  1392. match.children.splice(c, 1);
  1393. }
  1394. }
  1395. // If any children matched, return the new object
  1396. if (match.children.length > 0) {
  1397. return match;
  1398. }
  1399. // If there were no matching children, check just the plain object
  1400. return matcher(params, match);
  1401. }
  1402. // Check if the text contains the term
  1403. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1404. return data;
  1405. }
  1406. // If it doesn't contain the term, don't return anything
  1407. return null;
  1408. }
  1409. this.defaults = {
  1410. language: ['select2/i18n/en'],
  1411. matcher: matcher,
  1412. minimumInputLength: 0,
  1413. theme: 'default',
  1414. templateResult: function (result) {
  1415. return result.text;
  1416. },
  1417. templateSelection: function (selection) {
  1418. return selection.text;
  1419. }
  1420. };
  1421. };
  1422. var defaults = new Defaults();
  1423. return defaults;
  1424. });
  1425. define('select2/options',[
  1426. './defaults'
  1427. ], function (Defaults) {
  1428. function Options (options, $element) {
  1429. this.options = options;
  1430. if ($element != null) {
  1431. this.fromElement($element);
  1432. }
  1433. this.options = Defaults.apply(this.options);
  1434. }
  1435. Options.prototype.fromElement = function ($e) {
  1436. if (this.options.multiple == null) {
  1437. this.options.multiple = $e.prop('multiple');
  1438. }
  1439. return this;
  1440. };
  1441. Options.prototype.get = function (key) {
  1442. return this.options[key];
  1443. };
  1444. Options.prototype.set = function (key, val) {
  1445. this.options[key] = val;
  1446. };
  1447. return Options;
  1448. });
  1449. define('select2/core',[
  1450. 'jquery',
  1451. './options',
  1452. './utils',
  1453. './keys'
  1454. ], function ($, Options, Utils, KEYS) {
  1455. var Select2 = function ($element, options) {
  1456. if ($element.data('select2') != null) {
  1457. return;
  1458. }
  1459. this.$element = $element;
  1460. this.id = this._generateId($element);
  1461. options = options || {};
  1462. this.options = new Options(options, $element);
  1463. Select2.__super__.constructor.call(this);
  1464. // Set up containers and adapters
  1465. var DataAdapter = this.options.get('dataAdapter');
  1466. this.data = new DataAdapter($element, this.options);
  1467. var $container = this.render();
  1468. this._placeContainer($container);
  1469. var SelectionAdapter = this.options.get('selectionAdapter');
  1470. this.selection = new SelectionAdapter($element, this.options);
  1471. var $selection = this.selection.render();
  1472. this._placeSelection($selection);
  1473. var DropdownAdapter = this.options.get('dropdownAdapter');
  1474. this.dropdown = new DropdownAdapter($element, this.options);
  1475. var $dropdown = this.dropdown.render();
  1476. this._placeDropdown($dropdown);
  1477. var ResultsAdapter = this.options.get('resultsAdapter');
  1478. this.results = new ResultsAdapter($element, this.options, this.data);
  1479. var $results = this.results.render();
  1480. this._placeResults($results);
  1481. // Bind events
  1482. var self = this;
  1483. // Bind the container to all of the adapters
  1484. this._bindAdapters();
  1485. // Register any DOM event handlers
  1486. this._registerDomEvents();
  1487. // Register any internal event handlers
  1488. this._registerDataEvents();
  1489. this._registerSelectionEvents();
  1490. this._registerDropdownEvents();
  1491. this._registerResultsEvents();
  1492. this._registerEvents();
  1493. // Set the initial state
  1494. this.data.current(function (initialData) {
  1495. self.trigger('selection:update', {
  1496. data: initialData
  1497. });
  1498. });
  1499. // Hide the original select
  1500. $element.hide();
  1501. this._tabindex = $element.attr('tabindex') || 0;
  1502. $element.attr('tabindex', '-1');
  1503. $element.data('select2', this);
  1504. };
  1505. Utils.Extend(Select2, Utils.Observable);
  1506. Select2.prototype._generateId = function ($element) {
  1507. var id = '';
  1508. if ($element.attr('id') != null) {
  1509. id = $element.attr('id');
  1510. } else if ($element.attr('name') != null) {
  1511. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1512. } else {
  1513. id = Utils.generateChars(4);
  1514. }
  1515. id = 'select2-' + id;
  1516. return id;
  1517. };
  1518. Select2.prototype._placeContainer = function ($container) {
  1519. $container.insertAfter(this.$element);
  1520. $container.width(this.$element.outerWidth(false));
  1521. };
  1522. Select2.prototype._placeSelection = function ($selection) {
  1523. var $selectionContainer = this.$container.find('.selection');
  1524. $selectionContainer.append($selection);
  1525. };
  1526. Select2.prototype._placeDropdown = function ($dropdown) {
  1527. this.$dropdown = $dropdown;
  1528. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1529. $dropdownContainer.append($dropdown);
  1530. };
  1531. Select2.prototype._placeResults = function ($results) {
  1532. var $resultsContainer = this.$dropdown.find('.results');
  1533. $resultsContainer.append($results);
  1534. };
  1535. Select2.prototype._bindAdapters = function () {
  1536. this.data.bind(this, this.$container);
  1537. this.selection.bind(this, this.$container);
  1538. this.dropdown.bind(this, this.$container);
  1539. this.results.bind(this, this.$container);
  1540. };
  1541. Select2.prototype._registerDomEvents = function () {
  1542. var self = this;
  1543. this.$element.on('change.select2', function () {
  1544. self.data.current(function (data) {
  1545. self.trigger('selection:update', {
  1546. data: data
  1547. });
  1548. });
  1549. });
  1550. };
  1551. Select2.prototype._registerDataEvents = function () {
  1552. var self = this;
  1553. this.data.on('results:message', function (params) {
  1554. self.trigger('results:message', params);
  1555. });
  1556. };
  1557. Select2.prototype._registerSelectionEvents = function () {
  1558. var self = this;
  1559. this.selection.on('open', function () {
  1560. self.open();
  1561. });
  1562. this.selection.on('close', function () {
  1563. self.close();
  1564. });
  1565. this.selection.on('toggle', function () {
  1566. self.toggleDropdown();
  1567. });
  1568. this.selection.on('results:select', function () {
  1569. self.trigger('results:select');
  1570. });
  1571. this.selection.on('results:previous', function () {
  1572. self.trigger('results:previous');
  1573. });
  1574. this.selection.on('results:next', function () {
  1575. self.trigger('results:next');
  1576. });
  1577. this.selection.on('unselected', function (params) {
  1578. self.trigger('unselect', params);
  1579. self.close();
  1580. });
  1581. this.selection.on('keypress', function (e) {
  1582. self.trigger('keypress', e);
  1583. });
  1584. };
  1585. Select2.prototype._registerDropdownEvents = function () {
  1586. var self = this;
  1587. this.dropdown.on('query', function (params) {
  1588. self.trigger('query', params);
  1589. });
  1590. this.dropdown.on('keypress', function (e) {
  1591. self.trigger('keypress', e);
  1592. });
  1593. };
  1594. Select2.prototype._registerResultsEvents = function () {
  1595. var self = this;
  1596. this.results.on('query:append', function (params) {
  1597. self.trigger('query:append', params);
  1598. });
  1599. this.results.on('selected', function (params) {
  1600. self.trigger('select', params);
  1601. self.close();
  1602. });
  1603. this.results.on('unselected', function (params) {
  1604. self.trigger('unselect', params);
  1605. self.close();
  1606. });
  1607. this.results.on('results:focus', function (params) {
  1608. self.trigger('results:focus', params);
  1609. });
  1610. };
  1611. Select2.prototype._registerEvents = function () {
  1612. var self = this;
  1613. this.on('open', function () {
  1614. self.$container.addClass('open');
  1615. });
  1616. this.on('close', function () {
  1617. self.$container.removeClass('open');
  1618. });
  1619. this.on('query', function (params) {
  1620. this.data.query(params, function (data) {
  1621. self.trigger('results:all', {
  1622. data: data,
  1623. query: params
  1624. });
  1625. });
  1626. });
  1627. this.on('query:append', function (params) {
  1628. this.data.query(params, function (data) {
  1629. self.trigger('results:append', {
  1630. data: data,
  1631. query: params
  1632. });
  1633. });
  1634. });
  1635. this.on('keypress', function (evt) {
  1636. var key = evt.which;
  1637. if (self.isOpen()) {
  1638. if (key === KEYS.ENTER) {
  1639. self.trigger('results:select');
  1640. evt.preventDefault();
  1641. } else if (key === KEYS.UP) {
  1642. self.trigger('results:previous');
  1643. evt.preventDefault();
  1644. } else if (key === KEYS.DOWN) {
  1645. self.trigger('results:next');
  1646. evt.preventDefault();
  1647. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1648. self.close();
  1649. evt.preventDefault();
  1650. }
  1651. } else {
  1652. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1653. self.open();
  1654. evt.preventDefault();
  1655. }
  1656. }
  1657. });
  1658. };
  1659. Select2.prototype.toggleDropdown = function () {
  1660. if (this.isOpen()) {
  1661. this.close();
  1662. } else {
  1663. this.open();
  1664. }
  1665. };
  1666. Select2.prototype.open = function () {
  1667. if (this.isOpen()) {
  1668. return;
  1669. }
  1670. this.trigger('query', {});
  1671. this.trigger('open');
  1672. };
  1673. Select2.prototype.close = function () {
  1674. if (!this.isOpen()) {
  1675. return;
  1676. }
  1677. this.trigger('close');
  1678. };
  1679. Select2.prototype.isOpen = function () {
  1680. return this.$container.hasClass('open');
  1681. };
  1682. Select2.prototype.destroy = function () {
  1683. this.$container.remove();
  1684. this.$element.off('.select2');
  1685. this.$element.attr('tabindex', this._tabindex);
  1686. this.$element.show();
  1687. this.$element.removeData('select2');
  1688. this.data.destroy();
  1689. this.selection.destroy();
  1690. this.dropdown.destroy();
  1691. this.results.destroy();
  1692. this.data = null;
  1693. this.selection = null;
  1694. this.dropdown = null;
  1695. this.results = null;
  1696. };
  1697. Select2.prototype.render = function () {
  1698. var $container = $(
  1699. '<span class="select2 select2-container">' +
  1700. '<span class="selection"></span>' +
  1701. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1702. '</span>'
  1703. );
  1704. this.$container = $container;
  1705. this.$container.addClass('select2-theme-' + this.options.get('theme'));
  1706. $container.data('element', this.$element);
  1707. return $container;
  1708. };
  1709. return Select2;
  1710. });
  1711. define('select2/compat/matcher',[
  1712. ], function () {
  1713. function oldMatcher (matcher) {
  1714. function wrappedMatcher (params, data) {
  1715. var match = $.extend(true, {}, data);
  1716. if (params.term == null || $.trim(params.term) === '') {
  1717. return match;
  1718. }
  1719. if (data.children) {
  1720. for (var c = data.children.length - 1; c >= 0; c--) {
  1721. var child = data.children[c];
  1722. // Check if the child object matches
  1723. // The old matcher returned a boolean true or false
  1724. var doesMatch = matcher(params.term, child.text, child);
  1725. // If the child didn't match, pop it off
  1726. if (!doesMatch) {
  1727. match.children.splice(c, 1);
  1728. }
  1729. }
  1730. if (match.children.length > 0) {
  1731. return match;
  1732. }
  1733. }
  1734. if (matcher(params.term, data.text, data)) {
  1735. return match;
  1736. }
  1737. return null;
  1738. }
  1739. return wrappedMatcher;
  1740. }
  1741. return oldMatcher;
  1742. });
  1743. define('jquery.select2',[
  1744. 'jquery',
  1745. 'select2/core'
  1746. ], function ($, Select2) {
  1747. if ($.fn.select2 == null) {
  1748. $.fn.select2 = function (options) {
  1749. options = options || {};
  1750. if (typeof options === 'object') {
  1751. this.each(function () {
  1752. var instance = new Select2($(this), options);
  1753. });
  1754. } else if (typeof options === 'string') {
  1755. var instance = this.data('select2');
  1756. var args = Array.prototype.slice.call(arguments, 1);
  1757. instance[options](args);
  1758. } else {
  1759. throw new Error('Invalid arguments for Select2: ' + options);
  1760. }
  1761. };
  1762. }
  1763. return Select2;
  1764. });
  1765. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());