select2.amd.full.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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.empty = function () {
  133. var $empty = $('<li role="treeitem" class="option"></li>');
  134. $empty.text(this.options.get('translations').get('noResults'));
  135. this.$results.append($empty);
  136. };
  137. Results.prototype.append = function (data) {
  138. var $options = [];
  139. if (data.length === 0) {
  140. this.empty();
  141. return;
  142. }
  143. data = this.sort(data);
  144. for (var d = 0; d < data.length; d++) {
  145. var item = data[d];
  146. var $option = this.option(item);
  147. $options.push($option);
  148. }
  149. this.$results.append($options);
  150. };
  151. Results.prototype.sort = function (data) {
  152. return data;
  153. };
  154. Results.prototype.setClasses = function () {
  155. var self = this;
  156. this.data.current(function (selected) {
  157. var selectedIds = $.map(selected, function (s) {
  158. return s.id.toString();
  159. });
  160. var $options = self.$results.find('.option[aria-selected]');
  161. $options.each(function () {
  162. var $option = $(this);
  163. var item = $option.data('data');
  164. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  165. $option.attr('aria-selected', 'true');
  166. } else {
  167. $option.attr('aria-selected', 'false');
  168. }
  169. });
  170. var $selected = $options.filter('[aria-selected=true]');
  171. // Check if there are any selected options
  172. if ($selected.length > 0) {
  173. // If there are selected options, highlight the first
  174. $selected.first().trigger('mouseenter');
  175. } else {
  176. // If there are no selected options, highlight the first option
  177. // in the dropdown
  178. $options.first().trigger('mouseenter');
  179. }
  180. });
  181. };
  182. Results.prototype.option = function (data) {
  183. var $option = $(
  184. '<li class="option" role="treeitem" aria-selected="false"></li>'
  185. );
  186. if (data.children) {
  187. $option
  188. .attr('role', 'group')
  189. .attr('aria-label', data.text)
  190. .removeAttr('aria-selected');
  191. var $label = $('<strong class="group-label"></strong>');
  192. $label.html(data.text);
  193. var $children = [];
  194. for (var c = 0; c < data.children.length; c++) {
  195. var child = data.children[c];
  196. var $child = this.option(child);
  197. $children.push($child);
  198. }
  199. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  200. $childrenContainer.append($children);
  201. $option.append($label);
  202. $option.append($childrenContainer);
  203. } else {
  204. $option.html(data.text);
  205. }
  206. if (data.disabled) {
  207. $option
  208. .removeAttr('aria-selected')
  209. .attr('aria-disabled', 'true');
  210. }
  211. if (data.id == null) {
  212. $option.removeAttr('aria-selected');
  213. }
  214. if (data._resultId != null) {
  215. $option.attr('id', data._resultId);
  216. }
  217. $option.data('data', data);
  218. return $option;
  219. };
  220. Results.prototype.bind = function (container, $container) {
  221. var self = this;
  222. var id = container.id + '-results';
  223. this.$results.attr('id', id);
  224. container.on('results:all', function (params) {
  225. self.clear();
  226. self.append(params.data);
  227. if (container.isOpen()) {
  228. self.setClasses();
  229. }
  230. });
  231. container.on('results:append', function (params) {
  232. self.append(params.data);
  233. if (container.isOpen()) {
  234. self.setClasses();
  235. }
  236. });
  237. container.on('select', function () {
  238. if (!container.isOpen()) {
  239. return;
  240. }
  241. self.setClasses();
  242. });
  243. container.on('unselect', function () {
  244. if (!container.isOpen()) {
  245. return;
  246. }
  247. self.setClasses();
  248. });
  249. container.on('open', function () {
  250. // When the dropdown is open, aria-expended="true"
  251. self.$results.attr('aria-expanded', 'true');
  252. self.$results.attr('aria-hidden', 'false');
  253. self.setClasses();
  254. self.ensureHighlightVisible();
  255. });
  256. container.on('close', function () {
  257. // When the dropdown is closed, aria-expended="false"
  258. self.$results.attr('aria-expanded', 'false');
  259. self.$results.attr('aria-hidden', 'true');
  260. self.$results.removeAttr('aria-activedescendant');
  261. });
  262. container.on('results:select', function () {
  263. var $highlighted = self.$results.find('.highlighted');
  264. if ($highlighted.length === 0) {
  265. return;
  266. }
  267. var data = $highlighted.data('data');
  268. if ($highlighted.attr('aria-selected') == 'true') {
  269. self.trigger('unselected', {
  270. data: data
  271. });
  272. } else {
  273. self.trigger('selected', {
  274. data: data
  275. });
  276. }
  277. });
  278. container.on('results:previous', function () {
  279. var $highlighted = self.$results.find('.highlighted');
  280. var $options = self.$results.find('[aria-selected]');
  281. var currentIndex = $options.index($highlighted);
  282. // If we are already at te top, don't move further
  283. if (currentIndex === 0) {
  284. return;
  285. }
  286. var nextIndex = currentIndex - 1;
  287. // If none are highlighted, highlight the first
  288. if ($highlighted.length === 0) {
  289. nextIndex = 0;
  290. }
  291. var $next = $options.eq(nextIndex);
  292. $next.trigger('mouseenter');
  293. var currentOffset = self.$results.offset().top;
  294. var nextTop = $next.offset().top;
  295. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  296. if (nextIndex === 0) {
  297. self.$results.scrollTop(0);
  298. } else if (nextTop - currentOffset < 0) {
  299. self.$results.scrollTop(nextOffset);
  300. }
  301. });
  302. container.on('results:next', function () {
  303. var $highlighted = self.$results.find('.highlighted');
  304. var $options = self.$results.find('[aria-selected]');
  305. var currentIndex = $options.index($highlighted);
  306. var nextIndex = currentIndex + 1;
  307. // If we are at the last option, stay there
  308. if (nextIndex >= $options.length) {
  309. return;
  310. }
  311. var $next = $options.eq(nextIndex);
  312. $next.trigger('mouseenter');
  313. var currentOffset = self.$results.offset().top +
  314. self.$results.outerHeight(false);
  315. var nextBottom = $next.offset().top + $next.outerHeight(false);
  316. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  317. if (nextIndex === 0) {
  318. self.$results.scrollTop(0);
  319. } else if (nextBottom > currentOffset) {
  320. self.$results.scrollTop(nextOffset);
  321. }
  322. });
  323. container.on('results:focus', function (params) {
  324. params.element.addClass('highlighted');
  325. });
  326. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  327. var $this = $(this);
  328. var data = $this.data('data');
  329. if ($this.attr('aria-selected') === 'true') {
  330. self.trigger('unselected', {
  331. originalEvent: evt,
  332. data: data
  333. });
  334. return;
  335. }
  336. self.trigger('selected', {
  337. originalEvent: evt,
  338. data: data
  339. });
  340. });
  341. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  342. var data = $(this).data('data');
  343. self.$results.find('.option.highlighted').removeClass('highlighted');
  344. self.trigger('results:focus', {
  345. data: data,
  346. element: $(this)
  347. });
  348. });
  349. this.$results.on('mouseleave', '.option', function (evt) {
  350. if ($(this).hasClass('highlighted')) {
  351. $(this).removeClass('highlighted');
  352. self.$results.removeAttr('aria-activedescendant');
  353. }
  354. });
  355. };
  356. Results.prototype.ensureHighlightVisible = function () {
  357. var $highlighted = this.$results.find('.highlighted');
  358. if ($highlighted.length === 0) {
  359. return;
  360. }
  361. var $options = this.$results.find('[aria-selected]');
  362. var currentIndex = $options.index($highlighted);
  363. var currentOffset = this.$results.offset().top;
  364. var nextTop = $highlighted.offset().top;
  365. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  366. var offsetDelta = nextTop - currentOffset;
  367. nextOffset -= $highlighted.outerHeight(false) * 2;
  368. if (currentIndex <= 2) {
  369. this.$results.scrollTop(0);
  370. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  371. this.$results.scrollTop(nextOffset);
  372. }
  373. };
  374. return Results;
  375. });
  376. define('select2/selection/base',[
  377. '../utils'
  378. ], function (Utils) {
  379. function BaseSelection ($element, options) {
  380. this.$element = $element;
  381. this.options = options;
  382. BaseSelection.__super__.constructor.call(this);
  383. }
  384. Utils.Extend(BaseSelection, Utils.Observable);
  385. BaseSelection.prototype.render = function () {
  386. throw new Error('The `render` method must be defined in child classes.');
  387. };
  388. BaseSelection.prototype.bind = function (container, $container) {
  389. var self = this;
  390. container.on('selection:update', function (params) {
  391. self.update(params.data);
  392. });
  393. container.on('open', function () {
  394. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  395. var $target = $(e.target);
  396. var $select = $target.closest('.select2');
  397. var $all = $('.select2.open');
  398. $all.each(function () {
  399. var $this = $(this);
  400. if (this == $select[0]) {
  401. return;
  402. }
  403. var $element = $this.data('element');
  404. $element.select2('close');
  405. });
  406. });
  407. container.on('close', function () {
  408. $(document.body).off('mousedown.select2.' + container.id);
  409. });
  410. });
  411. };
  412. BaseSelection.prototype.update = function (data) {
  413. throw new Error('The `update` method must be defined in child classes.');
  414. };
  415. return BaseSelection;
  416. });
  417. define('select2/selection/single',[
  418. './base',
  419. '../utils'
  420. ], function (BaseSelection, Utils) {
  421. function SingleSelection () {
  422. SingleSelection.__super__.constructor.apply(this, arguments);
  423. }
  424. Utils.Extend(SingleSelection, BaseSelection);
  425. SingleSelection.prototype.render = function () {
  426. var $selection = $(
  427. '<span class="single-select" tabindex="0" role="combobox" ' +
  428. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  429. '<span class="rendered-selection"></span>' +
  430. '</span>'
  431. );
  432. $selection.attr('title', this.$element.attr('title'));
  433. this.$selection = $selection;
  434. return $selection;
  435. };
  436. SingleSelection.prototype.bind = function (container, $container) {
  437. var self = this;
  438. SingleSelection.__super__.bind.apply(this, arguments);
  439. var id = container.id + '-container';
  440. var resultsId = container.id + '-results';
  441. this.$selection.find('.rendered-selection').attr('id', id);
  442. this.$selection.attr('aria-labelledby', id);
  443. this.$selection.attr('aria-owns', resultsId);
  444. this.$selection.on('mousedown', function (evt) {
  445. // Only respond to left clicks
  446. if (evt.which !== 1) {
  447. return;
  448. }
  449. self.trigger('toggle', {
  450. originalEvent: evt
  451. });
  452. });
  453. container.on('open', function () {
  454. // When the dropdown is open, aria-expanded="true"
  455. self.$selection.attr('aria-expanded', 'true');
  456. });
  457. container.on('close', function () {
  458. // When the dropdown is closed, aria-expanded="false"
  459. self.$selection.attr('aria-expanded', 'false');
  460. self.$selection.removeAttr('aria-activedescendant');
  461. self.$selection.focus();
  462. });
  463. this.$selection.on('focus', function (evt) {
  464. // User focuses on the container
  465. });
  466. this.$selection.on('blur', function (evt) {
  467. // User exits the container
  468. });
  469. this.$selection.on('keydown', function (evt) {
  470. self.trigger('keypress', evt);
  471. });
  472. container.on('results:focus', function (params) {
  473. self.$selection.attr('aria-activedescendant', params.data._resultId);
  474. });
  475. container.on('selection:update', function (params) {
  476. self.update(params.data);
  477. });
  478. };
  479. SingleSelection.prototype.clear = function () {
  480. this.$selection.find('.rendered-selection').empty();
  481. };
  482. SingleSelection.prototype.display = function (data) {
  483. return data.text;
  484. };
  485. SingleSelection.prototype.selectionContainer = function () {
  486. return $('<span></span>');
  487. };
  488. SingleSelection.prototype.update = function (data) {
  489. if (data.length === 0) {
  490. this.clear();
  491. return;
  492. }
  493. var selection = data[0];
  494. var formatted = this.display(selection);
  495. this.$selection.find('.rendered-selection').html(formatted);
  496. };
  497. return SingleSelection;
  498. });
  499. define('select2/selection/multiple',[
  500. './base',
  501. '../utils'
  502. ], function (BaseSelection, Utils) {
  503. function MultipleSelection ($element, options) {
  504. this.$element = $element;
  505. this.options = options;
  506. MultipleSelection.__super__.constructor.call(this);
  507. }
  508. Utils.Extend(MultipleSelection, BaseSelection);
  509. MultipleSelection.prototype.render = function () {
  510. var $selection = $(
  511. '<span class="multiple-select">' +
  512. '<ul class="rendered-selection"></ul>' +
  513. '</span>'
  514. );
  515. this.$selection = $selection;
  516. return $selection;
  517. };
  518. MultipleSelection.prototype.bind = function (container, $container) {
  519. var self = this;
  520. MultipleSelection.__super__.bind.apply(this, arguments);
  521. this.$selection.on('click', function (evt) {
  522. self.trigger('toggle', {
  523. originalEvent: evt
  524. });
  525. });
  526. this.$selection.on('click', '.remove', function (evt) {
  527. var $remove = $(this);
  528. var $selection = $remove.parent();
  529. var data = $selection.data('data');
  530. self.trigger('unselected', {
  531. originalEvent: evt,
  532. data: data
  533. });
  534. });
  535. };
  536. MultipleSelection.prototype.clear = function () {
  537. this.$selection.find('.rendered-selection').empty();
  538. };
  539. MultipleSelection.prototype.display = function (data) {
  540. return data.text;
  541. };
  542. MultipleSelection.prototype.selectionContainer = function () {
  543. var $container = $(
  544. '<li class="choice">' +
  545. '<span class="remove" role="presentation">&times;</span>' +
  546. '</li>'
  547. );
  548. return $container;
  549. };
  550. MultipleSelection.prototype.update = function (data) {
  551. this.clear();
  552. if (data.length === 0) {
  553. return;
  554. }
  555. var $selections = [];
  556. for (var d = 0; d < data.length; d++) {
  557. var selection = data[d];
  558. var formatted = this.display(selection);
  559. var $selection = this.selectionContainer();
  560. $selection.append(formatted);
  561. $selection.data('data', selection);
  562. $selections.push($selection);
  563. }
  564. this.$selection.find('.rendered-selection').append($selections);
  565. };
  566. return MultipleSelection;
  567. });
  568. define('select2/selection/placeholder',[
  569. '../utils'
  570. ], function (Utils) {
  571. function Placeholder (decorated, $element, options) {
  572. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  573. decorated.call(this, $element, options);
  574. }
  575. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  576. if (typeof placeholder === 'string') {
  577. placeholder = {
  578. id: '',
  579. text: placeholder
  580. };
  581. }
  582. return placeholder;
  583. };
  584. Placeholder.prototype.update = function (decorated, data) {
  585. var singlePlaceholder = (
  586. data.length == 1 && data[0].id != this.placeholder.id
  587. );
  588. var multipleSelections = data.length > 1;
  589. if (multipleSelections || singlePlaceholder) {
  590. return decorated.call(this, data);
  591. }
  592. this.clear();
  593. var $placeholder = this.selectionContainer();
  594. $placeholder.html(this.display(this.placeholder));
  595. $placeholder.addClass('placeholder').removeClass('choice');
  596. this.$selection.find('.rendered-selection').append($placeholder);
  597. };
  598. return Placeholder;
  599. });
  600. define('select2/translation',[
  601. ], function () {
  602. function Translation (dict) {
  603. this.dict = dict || {};
  604. }
  605. Translation.prototype.all = function () {
  606. return this.dict;
  607. };
  608. Translation.prototype.get = function (key) {
  609. return this.dict[key];
  610. };
  611. Translation.prototype.extend = function (translation) {
  612. this.dict = $.extend({}, translation.all(), this.dict);
  613. };
  614. // Static functions
  615. Translation._cache = {};
  616. Translation.loadPath = function (path) {
  617. if (!(path in Translation._cache)) {
  618. var translations = require(path);
  619. Translation._cache[path] = translations;
  620. }
  621. return new Translation(Translation._cache[path]);
  622. };
  623. return Translation;
  624. });
  625. define('select2/data/base',[
  626. '../utils'
  627. ], function (Utils) {
  628. function BaseAdapter ($element, options) {
  629. BaseAdapter.__super__.constructor.call(this);
  630. }
  631. Utils.Extend(BaseAdapter, Utils.Observable);
  632. BaseAdapter.prototype.current = function (callback) {
  633. throw new Error('The `current` method must be defined in child classes.');
  634. };
  635. BaseAdapter.prototype.query = function (params, callback) {
  636. throw new Error('The `query` method must be defined in child classes.');
  637. };
  638. BaseAdapter.prototype.bind = function (container, $container) {
  639. // Can be implemented in subclasses
  640. };
  641. BaseAdapter.prototype.generateResultId = function (container, data) {
  642. var id = container.id + '-result-';
  643. id += Utils.generateChars(4);
  644. if (data.id != null) {
  645. id += '-' + data.id.toString();
  646. } else {
  647. id += '-' + Utils.generateChars(4);
  648. }
  649. return id;
  650. };
  651. return BaseAdapter;
  652. });
  653. define('select2/data/select',[
  654. './base',
  655. '../utils',
  656. 'jquery'
  657. ], function (BaseAdapter, Utils, $) {
  658. function SelectAdapter ($element, options) {
  659. this.$element = $element;
  660. SelectAdapter.__super__.constructor.call(this);
  661. }
  662. Utils.Extend(SelectAdapter, BaseAdapter);
  663. SelectAdapter.prototype.current = function (callback) {
  664. var data = [];
  665. var self = this;
  666. this.$element.find(':selected').each(function () {
  667. var $option = $(this);
  668. var option = self.item($option);
  669. data.push(option);
  670. });
  671. callback(data);
  672. };
  673. SelectAdapter.prototype.select = function (data) {
  674. var self = this;
  675. if (this.$element.prop('multiple')) {
  676. this.current(function (currentData) {
  677. var val = [];
  678. data = [data];
  679. data.push.apply(data, currentData);
  680. for (var d = 0; d < data.length; d++) {
  681. id = data[d].id;
  682. if (val.indexOf(id) === -1) {
  683. val.push(id);
  684. }
  685. }
  686. self.$element.val(val);
  687. self.$element.trigger('change');
  688. });
  689. } else {
  690. var val = data.id;
  691. this.$element.val(val);
  692. this.$element.trigger('change');
  693. }
  694. };
  695. SelectAdapter.prototype.unselect = function (data) {
  696. var self = this;
  697. if (!this.$element.prop('multiple')) {
  698. return;
  699. }
  700. this.current(function (currentData) {
  701. var val = [];
  702. for (var d = 0; d < currentData.length; d++) {
  703. id = currentData[d].id;
  704. if (id !== data.id && val.indexOf(id) === -1) {
  705. val.push(id);
  706. }
  707. }
  708. self.$element.val(val);
  709. self.$element.trigger('change');
  710. });
  711. };
  712. SelectAdapter.prototype.bind = function (container, $container) {
  713. var self = this;
  714. this.container = container;
  715. container.on('select', function (params) {
  716. self.select(params.data);
  717. });
  718. container.on('unselect', function (params) {
  719. self.unselect(params.data);
  720. });
  721. };
  722. SelectAdapter.prototype.query = function (params, callback) {
  723. var data = [];
  724. var self = this;
  725. var $options = this.$element.children();
  726. $options.each(function () {
  727. var $option = $(this);
  728. if (!$option.is('option') && !$option.is('optgroup')) {
  729. return;
  730. }
  731. var option = self.item($option);
  732. var matches = self.matches(params, option);
  733. if (matches !== null) {
  734. data.push(matches);
  735. }
  736. });
  737. callback(data);
  738. };
  739. SelectAdapter.prototype.option = function (data) {
  740. var $option = $('<option></option>');
  741. $option.text(data.text);
  742. $option.val(data.id);
  743. $option.prop('disabled', data.disabled || false);
  744. // Get any automatically generated data values
  745. var detectedData = this.item($option);
  746. // Merge it with the already present data
  747. var combinedData = $.extend({}, data, detectedData);
  748. // Override the option's data with the combined data
  749. $option.data('data', combinedData);
  750. return $option;
  751. };
  752. SelectAdapter.prototype.item = function ($option) {
  753. var data = $option.data('data');
  754. // If the data has already be generated, use it
  755. if (data == null) {
  756. if ($option.is('option')) {
  757. data = {
  758. id: $option.val(),
  759. text: $option.html(),
  760. disabled: $option.prop('disabled')
  761. };
  762. } else if ($option.is('optgroup')) {
  763. data = {
  764. text: $option.attr('label'),
  765. children: []
  766. };
  767. var $children = $option.children('option');
  768. var children = [];
  769. for (var c = 0; c < $children.length; c++) {
  770. var $child = $($children[c]);
  771. var child = this.item($child);
  772. children.push(child);
  773. }
  774. data.children = children;
  775. }
  776. if (data.id && this.container != null) {
  777. data._resultId = this.generateResultId(this.container, data);
  778. }
  779. $option.data('data', data);
  780. }
  781. return data;
  782. };
  783. SelectAdapter.prototype.matches = function (params, data) {
  784. var match = $.extend(true, {}, data);
  785. if (data.children) {
  786. for (var c = data.children.length - 1; c >= 0; c--) {
  787. var child = data.children[c];
  788. var matches = this.matches(params, child);
  789. // If there wasn't a match, remove the object in the array
  790. if (matches === null) {
  791. match.children.splice(c, 1);
  792. }
  793. }
  794. if (match.children.length > 0) {
  795. return match;
  796. }
  797. }
  798. if ($.trim(params.term) === '') {
  799. return match;
  800. }
  801. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  802. return match;
  803. }
  804. return null;
  805. };
  806. return SelectAdapter;
  807. });
  808. define('select2/data/array',[
  809. './select',
  810. '../utils',
  811. 'jquery'
  812. ], function (SelectAdapter, Utils, $) {
  813. function ArrayAdapter ($element, options) {
  814. this.data = options.get('data');
  815. ArrayAdapter.__super__.constructor.call(this, $element, options);
  816. }
  817. Utils.Extend(ArrayAdapter, SelectAdapter);
  818. ArrayAdapter.prototype.select = function (data) {
  819. var self = this;
  820. this.$element.find('option').each(function () {
  821. var $option = $(this);
  822. var option = self.item($option);
  823. if (option.id == data.id.toString()) {
  824. $option.remove();
  825. }
  826. });
  827. var $option = this.option(data);
  828. this.$element.append($option);
  829. ArrayAdapter.__super__.select.call(this, data);
  830. };
  831. ArrayAdapter.prototype.query = function (params, callback) {
  832. var matches = [];
  833. var self = this;
  834. $.each(this.data, function () {
  835. var option = this;
  836. if (self.matches(params, option)) {
  837. matches.push(option);
  838. }
  839. });
  840. callback(matches);
  841. };
  842. return ArrayAdapter;
  843. });
  844. define('select2/data/ajax',[
  845. './array',
  846. '../utils',
  847. 'jquery'
  848. ], function (ArrayAdapter, Utils, $) {
  849. function AjaxAdapter ($element, options) {
  850. this.ajaxOptions = options.get('ajax');
  851. this.processResults = this.ajaxOptions.processResults ||
  852. function (results) {
  853. return results;
  854. };
  855. ArrayAdapter.__super__.constructor.call(this, $element, options);
  856. }
  857. Utils.Extend(AjaxAdapter, ArrayAdapter);
  858. AjaxAdapter.prototype.query = function (params, callback) {
  859. var matches = [];
  860. var self = this;
  861. var options = $.extend({
  862. type: 'GET'
  863. }, this.ajaxOptions);
  864. if (typeof options.url === 'function') {
  865. options.url = options.url(params);
  866. }
  867. if (typeof options.data === 'function') {
  868. options.data = options.data(params);
  869. }
  870. var $request = $.ajax(options);
  871. $request.success(function (data) {
  872. var results = self.processResults(data);
  873. callback(results);
  874. });
  875. };
  876. return AjaxAdapter;
  877. });
  878. define('select2/data/tags',[
  879. ], function () {
  880. function Tags (decorated, $element, options) {
  881. var tags = options.get('tags');
  882. decorated.call(this, $element, options);
  883. }
  884. Tags.prototype.query = function (decorated, params, callback) {
  885. var self = this;
  886. this._removeOldTags();
  887. if (params.term == null || params.term === '' || params.page != null) {
  888. decorated.call(this, params, callback);
  889. return;
  890. }
  891. function wrapper (data, child) {
  892. for (var i = 0; i < data.length; i++) {
  893. var option = data[i];
  894. var checkChildren = (
  895. option.children != null && !wrapper(option.children, true)
  896. );
  897. var checkText = option.text === params.term;
  898. if (checkText || checkChildren) {
  899. if (child) {
  900. return false;
  901. }
  902. callback(data);
  903. return;
  904. }
  905. }
  906. if (child) {
  907. return true;
  908. }
  909. var tag = self.createTag(params);
  910. var $option = self.option(tag);
  911. $option.attr('data-select2-tag', true);
  912. self.$element.append($option);
  913. self.insertTag(data, tag);
  914. callback(data);
  915. }
  916. decorated.call(this, params, wrapper);
  917. };
  918. Tags.prototype.createTag = function (decorated, params) {
  919. return {
  920. id: params.term,
  921. text: params.term
  922. };
  923. };
  924. Tags.prototype.insertTag = function (_, data, tag) {
  925. data.unshift(tag);
  926. };
  927. Tags.prototype._removeOldTags = function (_) {
  928. var tag = this._lastTag;
  929. var $options = this.$element.find('option[data-select2-tag]');
  930. $options.each(function () {
  931. if (this.selected) {
  932. return;
  933. }
  934. $(this).remove();
  935. });
  936. };
  937. return Tags;
  938. });
  939. define('select2/dropdown',[
  940. './utils'
  941. ], function (Utils) {
  942. function Dropdown ($element, options) {
  943. this.$element = $element;
  944. }
  945. Utils.Extend(Dropdown, Utils.Observable);
  946. Dropdown.prototype.render = function () {
  947. var $dropdown = $(
  948. '<span class="dropdown">' +
  949. '<span class="results"></span>' +
  950. '</span>'
  951. );
  952. return $dropdown;
  953. };
  954. Dropdown.prototype.bind = function (container, $container) {
  955. // Can be implemented in subclasses
  956. };
  957. return Dropdown;
  958. });
  959. define('select2/dropdown/search',[
  960. '../utils'
  961. ], function (Utils) {
  962. function Search () { }
  963. Search.prototype.render = function (decorated) {
  964. var $rendered = decorated.call(this);
  965. var $search = $(
  966. '<span class="search">' +
  967. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  968. '</span>'
  969. );
  970. this.$searchContainer = $search;
  971. this.$search = $search.find('input');
  972. $rendered.prepend($search);
  973. return $rendered;
  974. };
  975. Search.prototype.bind = function (decorated, container, $container) {
  976. var self = this;
  977. decorated.call(this, container, $container);
  978. this.$search.on('keyup', function (evt) {
  979. self.trigger('keypress', evt);
  980. if (evt.isDefaultPrevented()) {
  981. return;
  982. }
  983. self.trigger('query', {
  984. term: $(this).val()
  985. });
  986. });
  987. container.on('open', function () {
  988. self.$search.attr('tabindex', 0);
  989. });
  990. container.on('close', function () {
  991. self.$search.attr('tabindex', -1);
  992. self.$search.val('');
  993. });
  994. container.on('results:all', function (params) {
  995. if (params.query.term == null || params.query.term === '') {
  996. var showSearch = self.showSearch(params);
  997. if (showSearch) {
  998. self.$searchContainer.show();
  999. } else {
  1000. self.$searchContainer.hide();
  1001. }
  1002. }
  1003. });
  1004. };
  1005. Search.prototype.showSearch = function (_, params) {
  1006. return true;
  1007. };
  1008. return Search;
  1009. });
  1010. define('select2/dropdown/hidePlaceholder',[
  1011. ], function () {
  1012. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1013. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1014. decorated.call(this, $element, options, dataAdapter);
  1015. }
  1016. HidePlaceholder.prototype.append = function (decorated, data) {
  1017. data = this.removePlaceholder(data);
  1018. decorated.call(this, data);
  1019. };
  1020. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1021. if (typeof placeholder === 'string') {
  1022. placeholder = {
  1023. id: '',
  1024. text: placeholder
  1025. };
  1026. }
  1027. return placeholder;
  1028. };
  1029. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1030. var modifiedData = data.slice(0);
  1031. for (var d = data.length - 1; d >= 0; d--) {
  1032. var item = data[d];
  1033. if (this.placeholder.id === item.id) {
  1034. modifiedData.splice(d, 1);
  1035. }
  1036. }
  1037. return modifiedData;
  1038. };
  1039. return HidePlaceholder;
  1040. });
  1041. define('select2/i18n/en',[],function () {
  1042. return {
  1043. noResults: function () {
  1044. return 'No results found';
  1045. }
  1046. };
  1047. });
  1048. define('select2/defaults',[
  1049. 'jquery',
  1050. './results',
  1051. './selection/single',
  1052. './selection/multiple',
  1053. './selection/placeholder',
  1054. './utils',
  1055. './translation',
  1056. './data/select',
  1057. './data/array',
  1058. './data/ajax',
  1059. './data/tags',
  1060. './dropdown',
  1061. './dropdown/search',
  1062. './dropdown/hidePlaceholder',
  1063. './i18n/en'
  1064. ], function ($, ResultsList,
  1065. SingleSelection, MultipleSelection, Placeholder,
  1066. Utils, Translation,
  1067. SelectData, ArrayData, AjaxData, Tags,
  1068. Dropdown, Search, HidePlaceholder,
  1069. EnglishTranslation) {
  1070. function Defaults () {
  1071. this.reset();
  1072. }
  1073. Defaults.prototype.apply = function (options) {
  1074. options = $.extend({}, this.defaults, options);
  1075. if (options.dataAdapter == null) {
  1076. if (options.ajax) {
  1077. options.dataAdapter = AjaxData;
  1078. } else if (options.data) {
  1079. options.dataAdapter = ArrayData;
  1080. } else {
  1081. options.dataAdapter = SelectData;
  1082. }
  1083. }
  1084. if (options.tags != null) {
  1085. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1086. }
  1087. if (options.resultsAdapter == null) {
  1088. options.resultsAdapter = ResultsList;
  1089. if (options.placeholder != null) {
  1090. options.resultsAdapter = Utils.Decorate(
  1091. options.resultsAdapter,
  1092. HidePlaceholder
  1093. );
  1094. }
  1095. }
  1096. if (options.dropdownAdapter == null) {
  1097. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1098. options.dropdownAdapter = SearchableDropdown;
  1099. }
  1100. if (options.selectionAdapter == null) {
  1101. if (options.multiple) {
  1102. options.selectionAdapter = MultipleSelection;
  1103. } else {
  1104. options.selectionAdapter = SingleSelection;
  1105. }
  1106. // Add the placeholder mixin if a placeholder was specified
  1107. if (options.placeholder != null) {
  1108. options.selectionAdapter = Utils.Decorate(
  1109. options.selectionAdapter,
  1110. Placeholder
  1111. );
  1112. }
  1113. }
  1114. if (typeof options.language === 'string') {
  1115. options.language = [options.language];
  1116. }
  1117. if ($.isArray(options.language)) {
  1118. var languages = new Translation();
  1119. var languageNames = options.language.concat(this.defaults.language);
  1120. for (var l = 0; l < languageNames.length; l++) {
  1121. var name = languageNames[l];
  1122. var language = {};
  1123. try {
  1124. // Try to load it with the original name
  1125. language = Translation.loadPath(name);
  1126. } catch (e) {
  1127. // If we couldn't load it, check if it wasn't the full path
  1128. name = 'select2/i18n/' + name;
  1129. language = Translation.loadPath(name);
  1130. }
  1131. languages.extend(language);
  1132. }
  1133. options.translations = languages;
  1134. } else {
  1135. options.translations = new Translations(options.language);
  1136. }
  1137. return options;
  1138. };
  1139. Defaults.prototype.reset = function () {
  1140. this.defaults = {
  1141. language: ['select2/i18n/en']
  1142. };
  1143. };
  1144. var defaults = new Defaults();
  1145. return defaults;
  1146. });
  1147. define('select2/options',[
  1148. './defaults'
  1149. ], function (Defaults) {
  1150. function Options (options, $element) {
  1151. this.options = options;
  1152. if ($element != null) {
  1153. this.fromElement($element);
  1154. }
  1155. this.options = Defaults.apply(this.options);
  1156. }
  1157. Options.prototype.fromElement = function ($e) {
  1158. if (this.options.multiple == null) {
  1159. this.options.multiple = $e.prop('multiple');
  1160. }
  1161. return this;
  1162. };
  1163. Options.prototype.get = function (key) {
  1164. return this.options[key];
  1165. };
  1166. Options.prototype.set = function (key, val) {
  1167. this.options[key] = val;
  1168. };
  1169. return Options;
  1170. });
  1171. define('select2/keys',[
  1172. ], function () {
  1173. var KEYS = {
  1174. BACKSPACE: 8,
  1175. TAB: 9,
  1176. ENTER: 13,
  1177. SHIFT: 16,
  1178. CTRL: 17,
  1179. ALT: 18,
  1180. ESC: 27,
  1181. SPACE: 32,
  1182. PAGE_UP: 33,
  1183. PAGE_DOWN: 34,
  1184. END: 35,
  1185. HOME: 36,
  1186. LEFT: 37,
  1187. UP: 38,
  1188. RIGHT: 39,
  1189. DOWN: 40,
  1190. DELETE: 46,
  1191. isArrow: function (k) {
  1192. k = k.which ? k.which : k;
  1193. switch (k) {
  1194. case KEY.LEFT:
  1195. case KEY.RIGHT:
  1196. case KEY.UP:
  1197. case KEY.DOWN:
  1198. return true;
  1199. }
  1200. return false;
  1201. }
  1202. };
  1203. return KEYS;
  1204. });
  1205. define('select2/core',[
  1206. 'jquery',
  1207. './options',
  1208. './utils',
  1209. './keys'
  1210. ], function ($, Options, Utils, KEYS) {
  1211. var Select2 = function ($element, options) {
  1212. this.$element = $element;
  1213. this.id = this._generateId($element);
  1214. options = options || {};
  1215. this.options = new Options(options, $element);
  1216. Select2.__super__.constructor.call(this);
  1217. // Set up containers and adapters
  1218. var DataAdapter = this.options.get('dataAdapter');
  1219. this.data = new DataAdapter($element, this.options);
  1220. var $container = this.render();
  1221. this._placeContainer($container);
  1222. var SelectionAdapter = this.options.get('selectionAdapter');
  1223. this.selection = new SelectionAdapter($element, this.options);
  1224. var $selection = this.selection.render();
  1225. this._placeSelection($selection);
  1226. var DropdownAdapter = this.options.get('dropdownAdapter');
  1227. this.dropdown = new DropdownAdapter($element, this.options);
  1228. var $dropdown = this.dropdown.render();
  1229. this._placeDropdown($dropdown);
  1230. var ResultsAdapter = this.options.get('resultsAdapter');
  1231. this.results = new ResultsAdapter($element, this.options, this.data);
  1232. var $results = this.results.render();
  1233. this._placeResults($results);
  1234. // Bind events
  1235. var self = this;
  1236. // Bind the container to all of the adapters
  1237. this._bindAdapters();
  1238. // Register any DOM event handlers
  1239. this._registerDomEvents();
  1240. // Register any internal event handlers
  1241. this._registerSelectionEvents();
  1242. this._registerDropdownEvents();
  1243. this._registerResultsEvents();
  1244. this._registerEvents();
  1245. // Set the initial state
  1246. this.data.current(function (initialData) {
  1247. self.trigger('selection:update', {
  1248. data: initialData
  1249. });
  1250. });
  1251. // Hide the original select
  1252. $element.hide();
  1253. $element.attr('tabindex', '-1');
  1254. $element.data('select2', this);
  1255. };
  1256. Utils.Extend(Select2, Utils.Observable);
  1257. Select2.prototype._generateId = function ($element) {
  1258. var id = '';
  1259. if ($element.attr('id') != null) {
  1260. id = $element.attr('id');
  1261. } else if ($element.attr('name') != null) {
  1262. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1263. } else {
  1264. id = Utils.generateChars(4);
  1265. }
  1266. id = 'select2-' + id;
  1267. return id;
  1268. };
  1269. Select2.prototype._placeContainer = function ($container) {
  1270. $container.insertAfter(this.$element);
  1271. $container.width(this.$element.outerWidth(false));
  1272. };
  1273. Select2.prototype._placeSelection = function ($selection) {
  1274. var $selectionContainer = this.$container.find('.selection');
  1275. $selectionContainer.append($selection);
  1276. };
  1277. Select2.prototype._placeDropdown = function ($dropdown) {
  1278. this.$dropdown = $dropdown;
  1279. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1280. $dropdownContainer.append($dropdown);
  1281. };
  1282. Select2.prototype._placeResults = function ($results) {
  1283. var $resultsContainer = this.$dropdown.find('.results');
  1284. $resultsContainer.append($results);
  1285. };
  1286. Select2.prototype._bindAdapters = function () {
  1287. this.data.bind(this, this.$container);
  1288. this.selection.bind(this, this.$container);
  1289. this.dropdown.bind(this, this.$container);
  1290. this.results.bind(this, this.$container);
  1291. };
  1292. Select2.prototype._registerDomEvents = function () {
  1293. var self = this;
  1294. this.$element.on('change', function () {
  1295. self.data.current(function (data) {
  1296. self.trigger('selection:update', {
  1297. data: data
  1298. });
  1299. });
  1300. });
  1301. };
  1302. Select2.prototype._registerSelectionEvents = function () {
  1303. var self = this;
  1304. this.selection.on('open', function () {
  1305. self.open();
  1306. });
  1307. this.selection.on('close', function () {
  1308. self.close();
  1309. });
  1310. this.selection.on('toggle', function () {
  1311. self.toggleDropdown();
  1312. });
  1313. this.selection.on('results:select', function () {
  1314. self.trigger('results:select');
  1315. });
  1316. this.selection.on('results:previous', function () {
  1317. self.trigger('results:previous');
  1318. });
  1319. this.selection.on('results:next', function () {
  1320. self.trigger('results:next');
  1321. });
  1322. this.selection.on('unselected', function (params) {
  1323. self.trigger('unselect', params);
  1324. self.close();
  1325. });
  1326. this.selection.on('keypress', function (e) {
  1327. self.trigger('keypress', e);
  1328. });
  1329. };
  1330. Select2.prototype._registerDropdownEvents = function () {
  1331. var self = this;
  1332. this.dropdown.on('query', function (params) {
  1333. self.trigger('query', params);
  1334. });
  1335. this.dropdown.on('keypress', function (e) {
  1336. self.trigger('keypress', e);
  1337. });
  1338. };
  1339. Select2.prototype._registerResultsEvents = function () {
  1340. var self = this;
  1341. this.results.on('selected', function (params) {
  1342. self.trigger('select', params);
  1343. self.close();
  1344. });
  1345. this.results.on('unselected', function (params) {
  1346. self.trigger('unselect', params);
  1347. self.close();
  1348. });
  1349. this.results.on('results:focus', function (params) {
  1350. self.trigger('results:focus', params);
  1351. });
  1352. };
  1353. Select2.prototype._registerEvents = function () {
  1354. var self = this;
  1355. this.on('open', function () {
  1356. self.$container.addClass('open');
  1357. });
  1358. this.on('close', function () {
  1359. self.$container.removeClass('open');
  1360. });
  1361. this.on('query', function (params) {
  1362. this.data.query(params, function (data) {
  1363. self.trigger('results:all', {
  1364. data: data,
  1365. query: params
  1366. });
  1367. });
  1368. });
  1369. this.on('keypress', function (evt) {
  1370. var key = evt.which;
  1371. if (self.isOpen()) {
  1372. if (key == KEYS.ENTER) {
  1373. self.trigger('results:select');
  1374. evt.preventDefault();
  1375. } else if (key == KEYS.UP) {
  1376. self.trigger('results:previous');
  1377. evt.preventDefault();
  1378. } else if (key == KEYS.DOWN) {
  1379. self.trigger('results:next');
  1380. evt.preventDefault();
  1381. }
  1382. } else {
  1383. if (key == KEYS.ENTER || key == KEYS.SPACE) {
  1384. self.trigger('open');
  1385. evt.preventDefault();
  1386. }
  1387. }
  1388. });
  1389. };
  1390. Select2.prototype.toggleDropdown = function () {
  1391. if (this.isOpen()) {
  1392. this.close();
  1393. } else {
  1394. this.open();
  1395. }
  1396. };
  1397. Select2.prototype.open = function () {
  1398. if (this.isOpen()) {
  1399. return;
  1400. }
  1401. this.trigger('query', {});
  1402. this.trigger('open');
  1403. };
  1404. Select2.prototype.close = function () {
  1405. if (!this.isOpen()) {
  1406. return;
  1407. }
  1408. this.trigger('close');
  1409. };
  1410. Select2.prototype.isOpen = function () {
  1411. return this.$container.hasClass('open');
  1412. };
  1413. Select2.prototype.render = function () {
  1414. var $container = $(
  1415. '<span class="select2 select2-container select2-theme-default">' +
  1416. '<span class="selection"></span>' +
  1417. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1418. '</span>'
  1419. );
  1420. this.$container = $container;
  1421. $container.data('element', this.$element);
  1422. return $container;
  1423. };
  1424. return Select2;
  1425. });
  1426. define('jquery.select2',[
  1427. 'jquery',
  1428. 'select2/core'
  1429. ], function ($, Select2) {
  1430. if ($.fn.select2 == null) {
  1431. $.fn.select2 = function (options) {
  1432. options = options || {};
  1433. if (typeof options === 'object') {
  1434. this.each(function () {
  1435. var instance = new Select2($(this), options);
  1436. });
  1437. } else if (typeof options === 'string') {
  1438. var instance = this.data('select2');
  1439. var args = Array.prototype.slice.call(arguments, 1);
  1440. instance[options](args);
  1441. } else {
  1442. throw new Error('Invalid arguments for Select2: ' + options);
  1443. }
  1444. };
  1445. }
  1446. return Select2;
  1447. });
  1448. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());