select2.amd.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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. // Create items marked as tags
  707. if (data._tag === true) {
  708. // Clear the tag flag from it
  709. delete data._tag;
  710. // Create and add the option
  711. var $option = this.option(data);
  712. this.$element.append($option);
  713. }
  714. if (this.$element.prop('multiple')) {
  715. this.current(function (currentData) {
  716. var val = [];
  717. data = [data];
  718. data.push.apply(data, currentData);
  719. for (var d = 0; d < data.length; d++) {
  720. id = data[d].id;
  721. if (val.indexOf(id) === -1) {
  722. val.push(id);
  723. }
  724. }
  725. self.$element.val(val);
  726. self.$element.trigger('change');
  727. });
  728. } else {
  729. var val = data.id;
  730. this.$element.val(val);
  731. this.$element.trigger('change');
  732. }
  733. };
  734. SelectAdapter.prototype.unselect = function (data) {
  735. var self = this;
  736. if (!this.$element.prop('multiple')) {
  737. return;
  738. }
  739. this.current(function (currentData) {
  740. var val = [];
  741. for (var d = 0; d < currentData.length; d++) {
  742. id = currentData[d].id;
  743. if (id !== data.id && val.indexOf(id) === -1) {
  744. val.push(id);
  745. }
  746. }
  747. self.$element.val(val);
  748. self.$element.trigger('change');
  749. });
  750. };
  751. SelectAdapter.prototype.bind = function (container, $container) {
  752. var self = this;
  753. this.container = container;
  754. container.on('select', function (params) {
  755. self.select(params.data);
  756. });
  757. container.on('unselect', function (params) {
  758. self.unselect(params.data);
  759. });
  760. };
  761. SelectAdapter.prototype.query = function (params, callback) {
  762. var data = [];
  763. var self = this;
  764. var $options = this.$element.children();
  765. $options.each(function () {
  766. var $option = $(this);
  767. if (!$option.is('option') && !$option.is('optgroup')) {
  768. return;
  769. }
  770. var option = self.item($option);
  771. var matches = self.matches(params, option);
  772. if (matches !== null) {
  773. data.push(matches);
  774. }
  775. });
  776. callback(data);
  777. };
  778. SelectAdapter.prototype.option = function (data) {
  779. var $option = $('<option></option>');
  780. $option.text(data.text);
  781. $option.val(data.id);
  782. $option.prop('disabled', data.disabled || false);
  783. // Get any automatically generated data values
  784. var detectedData = this.item($option);
  785. // Merge it with the already present data
  786. var combinedData = $.extend({}, data, detectedData);
  787. // Override the option's data with the combined data
  788. $option.data('data', combinedData);
  789. return $option;
  790. };
  791. SelectAdapter.prototype.item = function ($option) {
  792. var data = $option.data('data');
  793. // If the data has already be generated, use it
  794. if (data == null) {
  795. if ($option.is('option')) {
  796. data = {
  797. id: $option.val(),
  798. text: $option.html(),
  799. disabled: $option.prop('disabled')
  800. };
  801. } else if ($option.is('optgroup')) {
  802. data = {
  803. text: $option.attr('label'),
  804. children: []
  805. };
  806. var $children = $option.children('option');
  807. var children = [];
  808. for (var c = 0; c < $children.length; c++) {
  809. var $child = $($children[c]);
  810. var child = this.item($child);
  811. children.push(child);
  812. }
  813. data.children = children;
  814. }
  815. if (data.id && this.container != null) {
  816. data._resultId = this.generateResultId(this.container, data);
  817. }
  818. $option.data('data', data);
  819. }
  820. return data;
  821. };
  822. SelectAdapter.prototype.matches = function (params, data) {
  823. var match = $.extend(true, {}, data);
  824. if (data.children) {
  825. for (var c = data.children.length - 1; c >= 0; c--) {
  826. var child = data.children[c];
  827. var matches = this.matches(params, child);
  828. // If there wasn't a match, remove the object in the array
  829. if (matches === null) {
  830. match.children.splice(c, 1);
  831. }
  832. }
  833. if (match.children.length > 0) {
  834. return match;
  835. }
  836. }
  837. if ($.trim(params.term) === '') {
  838. return match;
  839. }
  840. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  841. return match;
  842. }
  843. return null;
  844. };
  845. return SelectAdapter;
  846. });
  847. define('select2/data/array',[
  848. './select',
  849. '../utils',
  850. 'jquery'
  851. ], function (SelectAdapter, Utils, $) {
  852. function ArrayAdapter ($element, options) {
  853. this.data = options.get('data');
  854. ArrayAdapter.__super__.constructor.call(this, $element, options);
  855. }
  856. Utils.Extend(ArrayAdapter, SelectAdapter);
  857. ArrayAdapter.prototype.select = function (data) {
  858. var self = this;
  859. this.$element.find('option').each(function () {
  860. var $option = $(this);
  861. var option = self.item($option);
  862. if (option.id == data.id.toString()) {
  863. $option.remove();
  864. }
  865. });
  866. var $option = this.option(data);
  867. this.$element.append($option);
  868. ArrayAdapter.__super__.select.call(this, data);
  869. };
  870. ArrayAdapter.prototype.query = function (params, callback) {
  871. var matches = [];
  872. var self = this;
  873. $.each(this.data, function () {
  874. var option = this;
  875. if (self.matches(params, option)) {
  876. matches.push(option);
  877. }
  878. });
  879. callback(matches);
  880. };
  881. return ArrayAdapter;
  882. });
  883. define('select2/data/ajax',[
  884. './array',
  885. '../utils',
  886. 'jquery'
  887. ], function (ArrayAdapter, Utils, $) {
  888. function AjaxAdapter ($element, options) {
  889. this.ajaxOptions = options.get('ajax');
  890. this.processResults = this.ajaxOptions.processResults ||
  891. function (results) {
  892. return results;
  893. };
  894. ArrayAdapter.__super__.constructor.call(this, $element, options);
  895. }
  896. Utils.Extend(AjaxAdapter, ArrayAdapter);
  897. AjaxAdapter.prototype.query = function (params, callback) {
  898. var matches = [];
  899. var self = this;
  900. var options = $.extend({
  901. type: 'GET'
  902. }, this.ajaxOptions);
  903. if (typeof options.url === 'function') {
  904. options.url = options.url(params);
  905. }
  906. if (typeof options.data === 'function') {
  907. options.data = options.data(params);
  908. }
  909. var $request = $.ajax(options);
  910. $request.success(function (data) {
  911. var results = self.processResults(data);
  912. callback(results);
  913. });
  914. };
  915. return AjaxAdapter;
  916. });
  917. define('select2/data/tags',[
  918. ], function () {
  919. function Tags (decorated, $element, options) {
  920. var tags = options.get('tags');
  921. decorated.call(this, $element, options);
  922. }
  923. Tags.prototype.query = function (decorated, params, callback) {
  924. var self = this;
  925. if (params.term == null || params.term === '' || params.page != null) {
  926. decorated.call(this, params, callback);
  927. return;
  928. }
  929. function wrapper (data, child) {
  930. for (var i = 0; i < data.length; i++) {
  931. var option = data[i];
  932. var checkChildren = (
  933. option.children != null && !wrapper(option.children, true)
  934. );
  935. var checkText = option.text === params.term;
  936. if (checkText || checkChildren) {
  937. if (child) {
  938. return false;
  939. }
  940. callback(data);
  941. return;
  942. }
  943. }
  944. if (child) {
  945. return true;
  946. }
  947. var tag = self.createTag(params);
  948. tag._tag = true;
  949. data.unshift(tag);
  950. callback(data);
  951. }
  952. decorated.call(this, params, wrapper);
  953. };
  954. Tags.prototype.createTag = function (decorated, params) {
  955. return {
  956. id: params.term,
  957. text: params.term
  958. };
  959. };
  960. return Tags;
  961. });
  962. define('select2/dropdown',[
  963. './utils'
  964. ], function (Utils) {
  965. function Dropdown ($element, options) {
  966. this.$element = $element;
  967. }
  968. Utils.Extend(Dropdown, Utils.Observable);
  969. Dropdown.prototype.render = function () {
  970. var $dropdown = $(
  971. '<span class="dropdown">' +
  972. '<span class="results"></span>' +
  973. '</span>'
  974. );
  975. return $dropdown;
  976. };
  977. Dropdown.prototype.bind = function (container, $container) {
  978. // Can be implemented in subclasses
  979. };
  980. return Dropdown;
  981. });
  982. define('select2/dropdown/search',[
  983. ], function () {
  984. function Search () { }
  985. Search.prototype.render = function (decorated) {
  986. var $rendered = decorated.call(this);
  987. var $search = $(
  988. '<span class="search">' +
  989. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  990. '</span>'
  991. );
  992. this.$searchContainer = $search;
  993. this.$search = $search.find('input');
  994. $rendered.prepend($search);
  995. return $rendered;
  996. };
  997. Search.prototype.bind = function (decorated, container, $container) {
  998. var self = this;
  999. decorated.call(this, container, $container);
  1000. this.$search.on('keyup', function () {
  1001. container.trigger('query', {
  1002. term: $(this).val()
  1003. });
  1004. });
  1005. container.on('open', function () {
  1006. self.$search.attr('tabindex', 0);
  1007. });
  1008. container.on('close', function () {
  1009. self.$search.attr('tabindex', -1);
  1010. });
  1011. container.on('results:all', function (params) {
  1012. if (params.query.term == null || params.query.term === '') {
  1013. var showSearch = self.showSearch(params);
  1014. if (showSearch) {
  1015. self.$searchContainer.show();
  1016. } else {
  1017. self.$searchContainer.hide();
  1018. }
  1019. }
  1020. });
  1021. };
  1022. Search.prototype.showSearch = function (params) {
  1023. return true;
  1024. };
  1025. return Search;
  1026. });
  1027. define('select2/dropdown/hidePlaceholder',[
  1028. ], function () {
  1029. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1030. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1031. decorated.call(this, $element, options, dataAdapter);
  1032. }
  1033. HidePlaceholder.prototype.append = function (decorated, data) {
  1034. data = this.removePlaceholder(data);
  1035. decorated.call(this, data);
  1036. };
  1037. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1038. if (typeof placeholder === 'string') {
  1039. placeholder = {
  1040. id: '',
  1041. text: placeholder
  1042. };
  1043. }
  1044. return placeholder;
  1045. };
  1046. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1047. var modifiedData = data.slice(0);
  1048. for (var d = data.length - 1; d >= 0; d--) {
  1049. var item = data[d];
  1050. if (this.placeholder.id === item.id) {
  1051. modifiedData.splice(d, 1);
  1052. }
  1053. }
  1054. return modifiedData;
  1055. };
  1056. return HidePlaceholder;
  1057. });
  1058. define('select2/i18n/en',[],function () {
  1059. return {
  1060. noResults: function () {
  1061. return 'No results found';
  1062. }
  1063. };
  1064. });
  1065. define('select2/defaults',[
  1066. 'jquery',
  1067. './results',
  1068. './selection/single',
  1069. './selection/multiple',
  1070. './selection/placeholder',
  1071. './utils',
  1072. './translation',
  1073. './data/select',
  1074. './data/array',
  1075. './data/ajax',
  1076. './data/tags',
  1077. './dropdown',
  1078. './dropdown/search',
  1079. './dropdown/hidePlaceholder',
  1080. './i18n/en'
  1081. ], function ($, ResultsList,
  1082. SingleSelection, MultipleSelection, Placeholder,
  1083. Utils, Translation,
  1084. SelectData, ArrayData, AjaxData, Tags,
  1085. Dropdown, Search, HidePlaceholder,
  1086. EnglishTranslation) {
  1087. function Defaults () {
  1088. this.reset();
  1089. }
  1090. Defaults.prototype.apply = function (options) {
  1091. options = $.extend({}, this.defaults, options);
  1092. if (options.dataAdapter == null) {
  1093. if (options.ajax) {
  1094. options.dataAdapter = AjaxData;
  1095. } else if (options.data) {
  1096. options.dataAdapter = ArrayData;
  1097. } else {
  1098. options.dataAdapter = SelectData;
  1099. }
  1100. }
  1101. if (options.tags != null) {
  1102. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1103. }
  1104. if (options.resultsAdapter == null) {
  1105. options.resultsAdapter = ResultsList;
  1106. }
  1107. if (options.dropdownAdapter == null) {
  1108. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1109. options.dropdownAdapter = SearchableDropdown;
  1110. }
  1111. if (options.selectionAdapter == null) {
  1112. if (options.multiple) {
  1113. options.selectionAdapter = MultipleSelection;
  1114. } else {
  1115. options.selectionAdapter = SingleSelection;
  1116. }
  1117. // Add the placeholder mixin if a placeholder was specified
  1118. if (options.placeholder != null) {
  1119. options.selectionAdapter = Utils.Decorate(
  1120. options.selectionAdapter,
  1121. Placeholder
  1122. );
  1123. options.resultsAdapter = Utils.Decorate(
  1124. options.resultsAdapter,
  1125. HidePlaceholder
  1126. );
  1127. }
  1128. }
  1129. if (typeof options.language === 'string') {
  1130. options.language = [options.language];
  1131. }
  1132. if ($.isArray(options.language)) {
  1133. var languages = new Translation();
  1134. var languageNames = options.language.concat(this.defaults.language);
  1135. for (var l = 0; l < languageNames.length; l++) {
  1136. var name = languageNames[l];
  1137. var language = {};
  1138. try {
  1139. // Try to load it with the original name
  1140. language = Translation.loadPath(name);
  1141. } catch (e) {
  1142. // If we couldn't load it, check if it wasn't the full path
  1143. name = 'select2/i18n/' + name;
  1144. language = Translation.loadPath(name);
  1145. }
  1146. languages.extend(language);
  1147. }
  1148. options.translations = languages;
  1149. } else {
  1150. options.translations = new Translations(options.language);
  1151. }
  1152. return options;
  1153. };
  1154. Defaults.prototype.reset = function () {
  1155. this.defaults = {
  1156. language: ['select2/i18n/en']
  1157. };
  1158. };
  1159. var defaults = new Defaults();
  1160. return defaults;
  1161. });
  1162. define('select2/options',[
  1163. './defaults'
  1164. ], function (Defaults) {
  1165. function Options (options, $element) {
  1166. this.options = options;
  1167. if ($element != null) {
  1168. this.fromElement($element);
  1169. }
  1170. this.options = Defaults.apply(this.options);
  1171. }
  1172. Options.prototype.fromElement = function ($e) {
  1173. if (this.options.multiple == null) {
  1174. this.options.multiple = $e.prop('multiple');
  1175. }
  1176. return this;
  1177. };
  1178. Options.prototype.get = function (key) {
  1179. return this.options[key];
  1180. };
  1181. Options.prototype.set = function (key, val) {
  1182. this.options[key] = val;
  1183. };
  1184. return Options;
  1185. });
  1186. define('select2/core',[
  1187. 'jquery',
  1188. './options',
  1189. './utils'
  1190. ], function ($, Options, Utils) {
  1191. var Select2 = function ($element, options) {
  1192. this.$element = $element;
  1193. this.id = this._generateId($element);
  1194. options = options || {};
  1195. this.options = new Options(options, $element);
  1196. Select2.__super__.constructor.call(this);
  1197. // Set up containers and adapters
  1198. var DataAdapter = this.options.get('dataAdapter');
  1199. this.data = new DataAdapter($element, this.options);
  1200. var $container = this.render();
  1201. this._placeContainer($container);
  1202. var SelectionAdapter = this.options.get('selectionAdapter');
  1203. this.selection = new SelectionAdapter($element, this.options);
  1204. var $selection = this.selection.render();
  1205. this._placeSelection($selection);
  1206. var DropdownAdapter = this.options.get('dropdownAdapter');
  1207. this.dropdown = new DropdownAdapter($element, this.options);
  1208. var $dropdown = this.dropdown.render();
  1209. this._placeDropdown($dropdown);
  1210. var ResultsAdapter = this.options.get('resultsAdapter');
  1211. this.results = new ResultsAdapter($element, this.options, this.data);
  1212. var $results = this.results.render();
  1213. this._placeResults($results);
  1214. // Bind events
  1215. var self = this;
  1216. this.data.bind(this, $container);
  1217. this.selection.bind(this, $container);
  1218. this.dropdown.bind(this, $container);
  1219. this.results.bind(this, $container);
  1220. this.$element.on('change', function () {
  1221. self.data.current(function (data) {
  1222. self.trigger('selection:update', {
  1223. data: data
  1224. });
  1225. });
  1226. });
  1227. this.selection.on('open', function () {
  1228. self.open();
  1229. });
  1230. this.selection.on('close', function () {
  1231. self.close();
  1232. });
  1233. this.selection.on('toggle', function () {
  1234. self.toggleDropdown();
  1235. });
  1236. this.selection.on('results:select', function () {
  1237. self.trigger('results:select');
  1238. });
  1239. this.selection.on('results:previous', function () {
  1240. self.trigger('results:previous');
  1241. });
  1242. this.selection.on('results:next', function () {
  1243. self.trigger('results:next');
  1244. });
  1245. this.selection.on('unselected', function (params) {
  1246. self.trigger('unselect', params);
  1247. self.close();
  1248. });
  1249. this.results.on('selected', function (params) {
  1250. self.trigger('select', params);
  1251. self.close();
  1252. });
  1253. this.results.on('unselected', function (params) {
  1254. self.trigger('unselect', params);
  1255. self.close();
  1256. });
  1257. this.results.on('results:focus', function (params) {
  1258. self.trigger('results:focus', params);
  1259. });
  1260. this.on('open', function () {
  1261. $container.addClass('open');
  1262. });
  1263. this.on('close', function () {
  1264. $container.removeClass('open');
  1265. });
  1266. // Set the initial state
  1267. this.data.current(function (initialData) {
  1268. self.trigger('selection:update', {
  1269. data: initialData
  1270. });
  1271. });
  1272. this.on('query', function (params) {
  1273. this.data.query(params, function (data) {
  1274. self.trigger('results:all', {
  1275. data: data,
  1276. query: params
  1277. });
  1278. });
  1279. });
  1280. this.trigger('query', {});
  1281. // Hide the original select
  1282. $element.hide();
  1283. $element.attr('tabindex', '-1');
  1284. $element.data('select2', this);
  1285. };
  1286. Utils.Extend(Select2, Utils.Observable);
  1287. Select2.prototype._generateId = function ($element) {
  1288. var id = '';
  1289. if ($element.attr('id') != null) {
  1290. id = $element.attr('id');
  1291. } else if ($element.attr('name') != null) {
  1292. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1293. } else {
  1294. id = Utils.generateChars(4);
  1295. }
  1296. id = 'select2-' + id;
  1297. return id;
  1298. };
  1299. Select2.prototype._placeContainer = function ($container) {
  1300. $container.insertAfter(this.$element);
  1301. $container.width(this.$element.outerWidth(false));
  1302. };
  1303. Select2.prototype._placeSelection = function ($selection) {
  1304. var $selectionContainer = this.$container.find('.selection');
  1305. $selectionContainer.append($selection);
  1306. };
  1307. Select2.prototype._placeDropdown = function ($dropdown) {
  1308. this.$dropdown = $dropdown;
  1309. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1310. $dropdownContainer.append($dropdown);
  1311. };
  1312. Select2.prototype._placeResults = function ($results) {
  1313. var $resultsContainer = this.$dropdown.find('.results');
  1314. $resultsContainer.append($results);
  1315. };
  1316. Select2.prototype.toggleDropdown = function () {
  1317. if (this.isOpen()) {
  1318. this.close();
  1319. } else {
  1320. this.open();
  1321. }
  1322. };
  1323. Select2.prototype.open = function () {
  1324. if (this.isOpen()) {
  1325. return;
  1326. }
  1327. this.trigger('open');
  1328. };
  1329. Select2.prototype.close = function () {
  1330. if (!this.isOpen()) {
  1331. return;
  1332. }
  1333. this.trigger('close');
  1334. };
  1335. Select2.prototype.isOpen = function () {
  1336. return this.$container.hasClass('open');
  1337. };
  1338. Select2.prototype.render = function () {
  1339. var $container = $(
  1340. '<span class="select2 select2-container select2-theme-default">' +
  1341. '<span class="selection"></span>' +
  1342. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1343. '</span>'
  1344. );
  1345. this.$container = $container;
  1346. return $container;
  1347. };
  1348. return Select2;
  1349. });
  1350. define('jquery.select2',[
  1351. 'jquery',
  1352. 'select2/core'
  1353. ], function ($, Select2) {
  1354. if ($.fn.select2 == null) {
  1355. $.fn.select2 = function (options) {
  1356. options = options || {};
  1357. if (typeof options === 'object') {
  1358. this.each(function () {
  1359. var instance = new Select2($(this), options);
  1360. });
  1361. } else if (typeof options === 'string') {
  1362. var instance = this.data('select2');
  1363. var args = Array.prototype.slice.call(arguments, 1);
  1364. instance[options](args);
  1365. } else {
  1366. throw new Error('Invalid arguments for Select2: ' + options);
  1367. }
  1368. };
  1369. }
  1370. return Select2;
  1371. });
  1372. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());