select2.amd.js 43 KB

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