select2.amd.full.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. 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.append = function (data) {
  131. var $options = [];
  132. data = this.sort(data);
  133. for (var d = 0; d < data.length; d++) {
  134. var item = data[d];
  135. var $option = this.option(item);
  136. $options.push($option);
  137. }
  138. this.$results.append($options);
  139. };
  140. Results.prototype.sort = function (data) {
  141. return data;
  142. };
  143. Results.prototype.setClasses = function () {
  144. var self = this;
  145. this.data.current(function (selected) {
  146. var selectedIds = $.map(selected, function (s) {
  147. return s.id.toString();
  148. });
  149. var $options = self.$results.find('.option[aria-selected]');
  150. $options.each(function () {
  151. var $option = $(this);
  152. var item = $option.data('data');
  153. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  154. $option.attr('aria-selected', 'true');
  155. } else {
  156. $option.attr('aria-selected', 'false');
  157. }
  158. });
  159. var $selected = $options.filter('[aria-selected=true]');
  160. // Check if there are any selected options
  161. if ($selected.length > 0) {
  162. // If there are selected options, highlight the first
  163. $selected.first().trigger('mouseenter');
  164. } else {
  165. // If there are no selected options, highlight the first option
  166. // in the dropdown
  167. $options.first().trigger('mouseenter');
  168. }
  169. });
  170. };
  171. Results.prototype.option = function (data) {
  172. var $option = $(
  173. '<li class="option" role="treeitem" aria-selected="false"></li>'
  174. );
  175. if (data.children) {
  176. $option
  177. .attr('role', 'group')
  178. .attr('aria-label', data.text)
  179. .removeAttr('aria-selected');
  180. var $label = $('<strong class="group-label"></strong>');
  181. $label.html(data.text);
  182. var $children = [];
  183. for (var c = 0; c < data.children.length; c++) {
  184. var child = data.children[c];
  185. var $child = this.option(child);
  186. $children.push($child);
  187. }
  188. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  189. $childrenContainer.append($children);
  190. $option.append($label);
  191. $option.append($childrenContainer);
  192. } else {
  193. $option.html(data.text);
  194. }
  195. if (data.disabled) {
  196. $option
  197. .removeAttr('aria-selected')
  198. .attr('aria-disabled', 'true');
  199. }
  200. if (data.id == null) {
  201. $option.removeAttr('aria-selected');
  202. }
  203. if (data._resultId != null) {
  204. $option.attr('id', data._resultId);
  205. }
  206. $option.data('data', data);
  207. return $option;
  208. };
  209. Results.prototype.bind = function (container, $container) {
  210. var self = this;
  211. var id = container.id + '-results';
  212. this.$results.attr('id', id);
  213. container.on('results:all', function (params) {
  214. self.clear();
  215. self.append(params.data);
  216. if (container.isOpen()) {
  217. self.setClasses();
  218. }
  219. });
  220. container.on('results:append', function (params) {
  221. self.append(params.data);
  222. if (container.isOpen()) {
  223. self.setClasses();
  224. }
  225. });
  226. container.on('select', function () {
  227. if (!container.isOpen()) {
  228. return;
  229. }
  230. self.setClasses();
  231. });
  232. container.on('unselect', function () {
  233. if (!container.isOpen()) {
  234. return;
  235. }
  236. self.setClasses();
  237. });
  238. container.on('open', function () {
  239. // When the dropdown is open, aria-expended="true"
  240. self.$results.attr('aria-expanded', 'true');
  241. self.$results.attr('aria-hidden', 'false');
  242. self.setClasses();
  243. });
  244. container.on('close', function () {
  245. // When the dropdown is closed, aria-expended="false"
  246. self.$results.attr('aria-expanded', 'false');
  247. self.$results.attr('aria-hidden', 'true');
  248. self.$results.removeAttr('aria-activedescendant');
  249. });
  250. container.on('results:select', function () {
  251. var $highlighted = self.$results.find('.highlighted');
  252. if ($highlighted.length === 0) {
  253. return;
  254. }
  255. var data = $highlighted.data('data');
  256. if ($highlighted.attr('aria-selected') == 'true') {
  257. self.trigger('unselected', {
  258. data: data
  259. });
  260. } else {
  261. self.trigger('selected', {
  262. data: data
  263. });
  264. }
  265. });
  266. container.on('results:previous', function () {
  267. var $highlighted = self.$results.find('.highlighted');
  268. var $options = self.$results.find('[aria-selected]');
  269. var currentIndex = $options.index($highlighted);
  270. // If we are already at te top, don't move further
  271. if (currentIndex === 0) {
  272. return;
  273. }
  274. var nextIndex = currentIndex - 1;
  275. // If none are highlighted, highlight the first
  276. if ($highlighted.length === 0) {
  277. nextIndex = 0;
  278. }
  279. var $next = $options.eq(nextIndex);
  280. $next.trigger('mouseenter');
  281. var currentOffset = self.$results.offset().top;
  282. var nextTop = $next.offset().top;
  283. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  284. if (nextIndex === 0) {
  285. self.$results.scrollTop(0);
  286. } else if (nextTop - currentOffset < 0) {
  287. self.$results.scrollTop(nextOffset);
  288. }
  289. });
  290. container.on('results:next', function () {
  291. var $highlighted = self.$results.find('.highlighted');
  292. var $options = self.$results.find('[aria-selected]');
  293. var currentIndex = $options.index($highlighted);
  294. var nextIndex = currentIndex + 1;
  295. // If we are at the last option, stay there
  296. if (nextIndex >= $options.length) {
  297. return;
  298. }
  299. var $next = $options.eq(nextIndex);
  300. $next.trigger('mouseenter');
  301. var currentOffset = self.$results.offset().top +
  302. self.$results.outerHeight(false);
  303. var nextBottom = $next.offset().top + $next.outerHeight(false);
  304. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  305. if (nextIndex === 0) {
  306. self.$results.scrollTop(0);
  307. } else if (nextBottom > currentOffset) {
  308. self.$results.scrollTop(nextOffset);
  309. }
  310. });
  311. container.on('results:focus', function (params) {
  312. params.element.addClass('highlighted');
  313. });
  314. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  315. var $this = $(this);
  316. var data = $this.data('data');
  317. if ($this.attr('aria-selected') === 'true') {
  318. self.trigger('unselected', {
  319. originalEvent: evt,
  320. data: data
  321. });
  322. return;
  323. }
  324. self.trigger('selected', {
  325. originalEvent: evt,
  326. data: data
  327. });
  328. });
  329. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  330. var data = $(this).data('data');
  331. self.$results.find('.option.highlighted').removeClass('highlighted');
  332. self.trigger('results:focus', {
  333. data: data,
  334. element: $(this)
  335. });
  336. });
  337. this.$results.on('mouseleave', '.option', function (evt) {
  338. if ($(this).hasClass('highlighted')) {
  339. $(this).removeClass('highlighted');
  340. self.$results.removeAttr('aria-activedescendant');
  341. }
  342. });
  343. };
  344. return Results;
  345. });
  346. define('select2/selection/base',[
  347. '../utils'
  348. ], function (Utils) {
  349. function BaseSelection ($element, options) {
  350. this.$element = $element;
  351. this.options = options;
  352. BaseSelection.__super__.constructor.call(this);
  353. }
  354. Utils.Extend(BaseSelection, Utils.Observable);
  355. BaseSelection.prototype.render = function () {
  356. throw new Error('The `render` method must be defined in child classes.');
  357. };
  358. BaseSelection.prototype.bind = function (container, $container) {
  359. var self = this;
  360. container.on('selection:update', function (params) {
  361. self.update(params.data);
  362. });
  363. };
  364. BaseSelection.prototype.update = function (data) {
  365. throw new Error('The `update` method must be defined in child classes.');
  366. };
  367. return BaseSelection;
  368. });
  369. define('select2/keys',[
  370. ], function () {
  371. var KEYS = {
  372. BACKSPACE: 8,
  373. TAB: 9,
  374. ENTER: 13,
  375. SHIFT: 16,
  376. CTRL: 17,
  377. ALT: 18,
  378. ESC: 27,
  379. SPACE: 32,
  380. PAGE_UP: 33,
  381. PAGE_DOWN: 34,
  382. END: 35,
  383. HOME: 36,
  384. LEFT: 37,
  385. UP: 38,
  386. RIGHT: 39,
  387. DOWN: 40,
  388. DELETE: 46,
  389. isArrow: function (k) {
  390. k = k.which ? k.which : k;
  391. switch (k) {
  392. case KEY.LEFT:
  393. case KEY.RIGHT:
  394. case KEY.UP:
  395. case KEY.DOWN:
  396. return true;
  397. }
  398. return false;
  399. }
  400. };
  401. return KEYS;
  402. });
  403. define('select2/selection/single',[
  404. './base',
  405. '../utils',
  406. '../keys'
  407. ], function (BaseSelection, Utils, KEYS) {
  408. function SingleSelection () {
  409. SingleSelection.__super__.constructor.apply(this, arguments);
  410. }
  411. Utils.Extend(SingleSelection, BaseSelection);
  412. SingleSelection.prototype.render = function () {
  413. var $selection = $(
  414. '<span class="single-select" tabindex="0" role="combobox" ' +
  415. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  416. '<span class="rendered-selection"></span>' +
  417. '</span>'
  418. );
  419. $selection.attr('title', this.$element.attr('title'));
  420. this.$selection = $selection;
  421. return $selection;
  422. };
  423. SingleSelection.prototype.bind = function (container, $container) {
  424. var self = this;
  425. SingleSelection.__super__.bind.apply(this, arguments);
  426. var id = container.id + '-container';
  427. var resultsId = container.id + '-results';
  428. this.$selection.find('.rendered-selection').attr('id', id);
  429. this.$selection.attr('aria-labelledby', id);
  430. this.$selection.attr('aria-owns', resultsId);
  431. this.$selection.on('mousedown', function (evt) {
  432. // Only respond to left clicks
  433. if (evt.which !== 1) {
  434. return;
  435. }
  436. self.trigger('toggle', {
  437. originalEvent: evt
  438. });
  439. });
  440. container.on('open', function () {
  441. // When the dropdown is open, aria-expanded="true"
  442. self.$selection.attr('aria-expanded', 'true');
  443. });
  444. container.on('close', function () {
  445. // When the dropdown is closed, aria-expanded="false"
  446. self.$selection.attr('aria-expanded', 'false');
  447. self.$selection.removeAttr('aria-activedescendant');
  448. });
  449. this.$selection.on('focus', function (evt) {
  450. // User focuses on the container
  451. });
  452. this.$selection.on('blur', function (evt) {
  453. // User exits the container
  454. });
  455. this.$selection.on('keydown', function (evt) {
  456. var key = evt.which;
  457. if (container.isOpen()) {
  458. if (key == KEYS.ENTER) {
  459. self.trigger('results:select');
  460. evt.preventDefault();
  461. } else if (key == KEYS.UP) {
  462. self.trigger('results:previous');
  463. evt.preventDefault();
  464. } else if (key == KEYS.DOWN) {
  465. self.trigger('results:next');
  466. evt.preventDefault();
  467. }
  468. } else {
  469. if (key == KEYS.ENTER || key == KEYS.SPACE) {
  470. self.trigger('open');
  471. evt.preventDefault();
  472. }
  473. }
  474. });
  475. container.on('results:focus', function (params) {
  476. self.$selection.attr('aria-activedescendant', params.data._resultId);
  477. });
  478. container.on('selection:update', function (params) {
  479. self.update(params.data);
  480. });
  481. };
  482. SingleSelection.prototype.clear = function () {
  483. this.$selection.find('.rendered-selection').empty();
  484. };
  485. SingleSelection.prototype.display = function (data) {
  486. return data.text;
  487. };
  488. SingleSelection.prototype.selectionContainer = function () {
  489. return $('<span></span>');
  490. };
  491. SingleSelection.prototype.update = function (data) {
  492. if (data.length === 0) {
  493. this.clear();
  494. return;
  495. }
  496. var selection = data[0];
  497. var formatted = this.display(selection);
  498. this.$selection.find('.rendered-selection').html(formatted);
  499. };
  500. return SingleSelection;
  501. });
  502. define('select2/selection/multiple',[
  503. './base',
  504. '../utils'
  505. ], function (BaseSelection, Utils) {
  506. function MultipleSelection ($element, options) {
  507. this.$element = $element;
  508. this.options = options;
  509. MultipleSelection.__super__.constructor.call(this);
  510. }
  511. Utils.Extend(MultipleSelection, BaseSelection);
  512. MultipleSelection.prototype.render = function () {
  513. var $selection = $(
  514. '<span class="multiple-select">' +
  515. '<ul class="rendered-selection"></ul>' +
  516. '</span>'
  517. );
  518. this.$selection = $selection;
  519. return $selection;
  520. };
  521. MultipleSelection.prototype.bind = function (container, $container) {
  522. var self = this;
  523. MultipleSelection.__super__.bind.apply(this, arguments);
  524. this.$selection.on('click', function (evt) {
  525. self.trigger('toggle', {
  526. originalEvent: evt
  527. });
  528. });
  529. this.$selection.on('click', '.remove', function (evt) {
  530. var $remove = $(this);
  531. var $selection = $remove.parent();
  532. var data = $selection.data('data');
  533. self.trigger('unselected', {
  534. originalEvent: evt,
  535. data: data
  536. });
  537. });
  538. };
  539. MultipleSelection.prototype.clear = function () {
  540. this.$selection.find('.rendered-selection').empty();
  541. };
  542. MultipleSelection.prototype.display = function (data) {
  543. return data.text;
  544. };
  545. MultipleSelection.prototype.selectionContainer = function () {
  546. var $container = $(
  547. '<li class="choice">' +
  548. '<span class="remove" role="presentation">&times;</span>' +
  549. '</li>'
  550. );
  551. return $container;
  552. };
  553. MultipleSelection.prototype.update = function (data) {
  554. this.clear();
  555. if (data.length === 0) {
  556. return;
  557. }
  558. var $selections = [];
  559. for (var d = 0; d < data.length; d++) {
  560. var selection = data[d];
  561. var formatted = this.display(selection);
  562. var $selection = this.selectionContainer();
  563. $selection.append(formatted);
  564. $selection.data('data', selection);
  565. $selections.push($selection);
  566. }
  567. this.$selection.find('.rendered-selection').append($selections);
  568. };
  569. return MultipleSelection;
  570. });
  571. define('select2/selection/placeholder',[
  572. '../utils'
  573. ], function (Utils) {
  574. function Placeholder (decorated, $element, options) {
  575. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  576. decorated.call(this, $element, options);
  577. }
  578. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  579. if (typeof placeholder === 'string') {
  580. placeholder = {
  581. id: '',
  582. text: placeholder
  583. };
  584. }
  585. return placeholder;
  586. };
  587. Placeholder.prototype.update = function (decorated, data) {
  588. var singlePlaceholder = (
  589. data.length == 1 && data[0].id != this.placeholder.id
  590. );
  591. var multipleSelections = data.length > 1;
  592. if (multipleSelections || singlePlaceholder) {
  593. return decorated.call(this, data);
  594. }
  595. this.clear();
  596. var $placeholder = this.selectionContainer();
  597. $placeholder.html(this.display(this.placeholder));
  598. $placeholder.addClass('placeholder').removeClass('choice');
  599. this.$selection.find('.rendered-selection').append($placeholder);
  600. };
  601. return Placeholder;
  602. });
  603. define('select2/data/base',[
  604. '../utils'
  605. ], function (Utils) {
  606. function BaseAdapter ($element, options) {
  607. BaseAdapter.__super__.constructor.call(this);
  608. }
  609. Utils.Extend(BaseAdapter, Utils.Observable);
  610. BaseAdapter.prototype.current = function (callback) {
  611. throw new Error('The `current` method must be defined in child classes.');
  612. };
  613. BaseAdapter.prototype.query = function (params, callback) {
  614. throw new Error('The `query` method must be defined in child classes.');
  615. };
  616. BaseAdapter.prototype.bind = function (container, $container) {
  617. // Can be implemented in subclasses
  618. };
  619. BaseAdapter.prototype.generateResultId = function (container, data) {
  620. var id = container.id + '-result-';
  621. id += Utils.generateChars(4);
  622. if (data.id != null) {
  623. id += '-' + data.id.toString();
  624. } else {
  625. id += '-' + Utils.generateChars(4);
  626. }
  627. return id;
  628. };
  629. return BaseAdapter;
  630. });
  631. define('select2/data/select',[
  632. './base',
  633. '../utils',
  634. 'jquery'
  635. ], function (BaseAdapter, Utils, $) {
  636. function SelectAdapter ($element, options) {
  637. this.$element = $element;
  638. SelectAdapter.__super__.constructor.call(this);
  639. }
  640. Utils.Extend(SelectAdapter, BaseAdapter);
  641. SelectAdapter.prototype.current = function (callback) {
  642. var data = [];
  643. var self = this;
  644. this.$element.find(':selected').each(function () {
  645. var $option = $(this);
  646. var option = self.item($option);
  647. data.push(option);
  648. });
  649. callback(data);
  650. };
  651. SelectAdapter.prototype.select = function (data) {
  652. var self = this;
  653. if (this.$element.prop('multiple')) {
  654. this.current(function (currentData) {
  655. var val = [];
  656. data = [data];
  657. data.push.apply(data, currentData);
  658. for (var d = 0; d < data.length; d++) {
  659. id = data[d].id;
  660. if (val.indexOf(id) === -1) {
  661. val.push(id);
  662. }
  663. }
  664. self.$element.val(val);
  665. self.$element.trigger('change');
  666. });
  667. } else {
  668. var val = data.id;
  669. this.$element.val(val);
  670. this.$element.trigger('change');
  671. }
  672. };
  673. SelectAdapter.prototype.unselect = function (data) {
  674. var self = this;
  675. if (!this.$element.prop('multiple')) {
  676. return;
  677. }
  678. this.current(function (currentData) {
  679. var val = [];
  680. for (var d = 0; d < currentData.length; d++) {
  681. id = currentData[d].id;
  682. if (id !== data.id && val.indexOf(id) === -1) {
  683. val.push(id);
  684. }
  685. }
  686. self.$element.val(val);
  687. self.$element.trigger('change');
  688. });
  689. };
  690. SelectAdapter.prototype.bind = function (container, $container) {
  691. var self = this;
  692. this.container = container;
  693. container.on('select', function (params) {
  694. self.select(params.data);
  695. });
  696. container.on('unselect', function (params) {
  697. self.unselect(params.data);
  698. });
  699. };
  700. SelectAdapter.prototype.query = function (params, callback) {
  701. var data = [];
  702. var self = this;
  703. var $options = this.$element.children();
  704. $options.each(function () {
  705. var $option = $(this);
  706. if (!$option.is('option') && !$option.is('optgroup')) {
  707. return;
  708. }
  709. var option = self.item($option);
  710. var matches = self.matches(params, option);
  711. if (matches !== null) {
  712. data.push(matches);
  713. }
  714. });
  715. callback(data);
  716. };
  717. SelectAdapter.prototype.item = function ($option) {
  718. var data = $option.data('data');
  719. // If the data has already be generated, use it
  720. if (data == null) {
  721. if ($option.is('option')) {
  722. data = {
  723. id: $option.val(),
  724. text: $option.html(),
  725. disabled: $option.prop('disabled')
  726. };
  727. } else if ($option.is('optgroup')) {
  728. data = {
  729. text: $option.attr('label'),
  730. children: []
  731. };
  732. var $children = $option.children('option');
  733. var children = [];
  734. for (var c = 0; c < $children.length; c++) {
  735. var $child = $($children[c]);
  736. var child = this.item($child);
  737. children.push(child);
  738. }
  739. data.children = children;
  740. }
  741. if (data.id && this.container != null) {
  742. data._resultId = this.generateResultId(this.container, data);
  743. }
  744. $option.data('data', data);
  745. }
  746. return data;
  747. };
  748. SelectAdapter.prototype.matches = function (params, data) {
  749. var match = $.extend(true, {}, data);
  750. if (data.children) {
  751. for (var c = data.children.length - 1; c >= 0; c--) {
  752. var child = data.children[c];
  753. var matches = this.matches(params, child);
  754. // If there wasn't a match, remove the object in the array
  755. if (matches === null) {
  756. match.children.splice(c, 1);
  757. }
  758. }
  759. if (match.children.length > 0) {
  760. return match;
  761. }
  762. }
  763. if ($.trim(params.term) === '') {
  764. return match;
  765. }
  766. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  767. return match;
  768. }
  769. return null;
  770. };
  771. return SelectAdapter;
  772. });
  773. define('select2/data/array',[
  774. './select',
  775. '../utils',
  776. 'jquery'
  777. ], function (SelectAdapter, Utils, $) {
  778. function ArrayAdapter ($element, options) {
  779. this.data = options.get('data');
  780. ArrayAdapter.__super__.constructor.call(this, $element, options);
  781. }
  782. Utils.Extend(ArrayAdapter, SelectAdapter);
  783. ArrayAdapter.prototype.select = function (data) {
  784. var self = this;
  785. this.$element.find('option').each(function () {
  786. var $option = $(this);
  787. var option = self.item($option);
  788. if (option.id == data.id.toString()) {
  789. $option.remove();
  790. }
  791. });
  792. var $option = this.option(data);
  793. this.$element.append($option);
  794. ArrayAdapter.__super__.select.call(this, data);
  795. };
  796. ArrayAdapter.prototype.option = function (data) {
  797. var $option = $('<option></option>');
  798. $option.text(data.text);
  799. $option.val(data.id);
  800. $option.prop('disabled', data.disabled || false);
  801. // Get any automatically generated data values
  802. var detectedData = this.item($option);
  803. // Merge it with the already present data
  804. var combinedData = $.extend({}, data, detectedData);
  805. // Override the option's data with the combined data
  806. $option.data('data', combinedData);
  807. return $option;
  808. };
  809. ArrayAdapter.prototype.query = function (params, callback) {
  810. var matches = [];
  811. var self = this;
  812. $.each(this.data, function () {
  813. var option = this;
  814. if (self.matches(params, option)) {
  815. matches.push(option);
  816. }
  817. });
  818. callback(matches);
  819. };
  820. return ArrayAdapter;
  821. });
  822. define('select2/data/ajax',[
  823. './array',
  824. '../utils',
  825. 'jquery'
  826. ], function (ArrayAdapter, Utils, $) {
  827. function AjaxAdapter ($element, options) {
  828. this.ajaxOptions = options.get('ajax');
  829. this.processResults = this.ajaxOptions.processResults ||
  830. function (results) {
  831. return results;
  832. };
  833. ArrayAdapter.__super__.constructor.call(this, $element, options);
  834. }
  835. Utils.Extend(AjaxAdapter, ArrayAdapter);
  836. AjaxAdapter.prototype.query = function (params, callback) {
  837. var matches = [];
  838. var self = this;
  839. var options = $.extend({
  840. type: 'GET'
  841. }, this.ajaxOptions);
  842. if (typeof options.url === 'function') {
  843. options.url = options.url(params);
  844. }
  845. if (typeof options.data === 'function') {
  846. options.data = options.data(params);
  847. }
  848. var $request = $.ajax(options);
  849. $request.success(function (data) {
  850. var results = self.processResults(data);
  851. callback(results);
  852. });
  853. };
  854. return AjaxAdapter;
  855. });
  856. define('select2/dropdown',[
  857. './utils'
  858. ], function (Utils) {
  859. function Dropdown ($element, options) {
  860. this.$element = $element;
  861. }
  862. Utils.Extend(Dropdown, Utils.Observable);
  863. Dropdown.prototype.render = function () {
  864. var $dropdown = $(
  865. '<span class="dropdown">' +
  866. '<span class="results"></span>' +
  867. '</span>'
  868. );
  869. return $dropdown;
  870. };
  871. Dropdown.prototype.bind = function (container, $container) {
  872. // Can be implemented in subclasses
  873. };
  874. return Dropdown;
  875. });
  876. define('select2/dropdown/search',[
  877. ], function () {
  878. function Search () { }
  879. Search.prototype.render = function (decorated) {
  880. var $rendered = decorated.call(this);
  881. var $search = $(
  882. '<span class="search">' +
  883. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  884. '</span>'
  885. );
  886. this.$searchContainer = $search;
  887. this.$search = $search.find('input');
  888. $rendered.prepend($search);
  889. return $rendered;
  890. };
  891. Search.prototype.bind = function (decorated, container, $container) {
  892. var self = this;
  893. decorated.call(this, container, $container);
  894. this.$search.on('keyup', function () {
  895. container.trigger('query', {
  896. term: $(this).val()
  897. });
  898. });
  899. container.on('open', function () {
  900. self.$search.attr('tabindex', 0);
  901. });
  902. container.on('close', function () {
  903. self.$search.attr('tabindex', -1);
  904. });
  905. container.on('results:all', function (params) {
  906. if (params.query.term == null || params.query.term === '') {
  907. var showSearch = self.showSearch(params);
  908. if (showSearch) {
  909. self.$searchContainer.show();
  910. } else {
  911. self.$searchContainer.hide();
  912. }
  913. }
  914. });
  915. };
  916. Search.prototype.showSearch = function (params) {
  917. return true;
  918. };
  919. return Search;
  920. });
  921. define('select2/defaults',[
  922. './results',
  923. './selection/single',
  924. './selection/multiple',
  925. './selection/placeholder',
  926. './utils',
  927. './data/select',
  928. './data/array',
  929. './data/ajax',
  930. './dropdown',
  931. './dropdown/search'
  932. ], function (ResultsList,
  933. SingleSelection, MultipleSelection, Placeholder,
  934. Utils,
  935. SelectData, ArrayData, AjaxData,
  936. Dropdown, Search) {
  937. function Defaults () {
  938. this.reset();
  939. }
  940. Defaults.prototype.apply = function (options) {
  941. options = $.extend({}, options, this.defaults);
  942. if (options.dataAdapter == null) {
  943. if (options.ajax) {
  944. options.dataAdapter = AjaxData;
  945. } else if (options.data) {
  946. options.dataAdapter = ArrayData;
  947. } else {
  948. options.dataAdapter = SelectData;
  949. }
  950. }
  951. if (options.resultsAdapter == null) {
  952. options.resultsAdapter = ResultsList;
  953. }
  954. if (options.dropdownAdapter == null) {
  955. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  956. options.dropdownAdapter = SearchableDropdown;
  957. }
  958. if (options.selectionAdapter == null) {
  959. if (options.multiple) {
  960. options.selectionAdapter = MultipleSelection;
  961. } else {
  962. options.selectionAdapter = SingleSelection;
  963. }
  964. // Add the placeholder mixin if a placeholder was specified
  965. if (options.placeholder != null) {
  966. options.selectionAdapter = Utils.Decorate(
  967. options.selectionAdapter,
  968. Placeholder
  969. );
  970. }
  971. }
  972. return options;
  973. };
  974. Defaults.prototype.reset = function () {
  975. this.defaults = { };
  976. };
  977. var defaults = new Defaults();
  978. return defaults;
  979. });
  980. define('select2/options',[
  981. './defaults'
  982. ], function (Defaults) {
  983. function Options (options) {
  984. this.options = Defaults.apply(options);
  985. }
  986. Options.prototype.fromElement = function ($e) {
  987. return this;
  988. };
  989. Options.prototype.get = function (key) {
  990. return this.options[key];
  991. };
  992. Options.prototype.set = function (key, val) {
  993. this.options[key] = val;
  994. };
  995. return Options;
  996. });
  997. define('select2/core',[
  998. 'jquery',
  999. './options',
  1000. './utils'
  1001. ], function ($, Options, Utils) {
  1002. var Select2 = function ($element, options) {
  1003. this.$element = $element;
  1004. if ($element.attr('id') != null) {
  1005. this.id = $element.attr('id');
  1006. } else if ($element.attr('name') != null) {
  1007. this.id = $element.attr('name') + '-' + Utils.generateChars(2);
  1008. } else {
  1009. this.id = Utils.generateChars(4);
  1010. }
  1011. this.id = 'select2-' + this.id;
  1012. options = options || {};
  1013. options.multiple = options.multiple || $element.prop('multiple');
  1014. this.options = new Options(options);
  1015. Select2.__super__.constructor.call(this);
  1016. // Set up containers and adapters
  1017. var DataAdapter = this.options.get('dataAdapter');
  1018. this.data = new DataAdapter($element, this.options);
  1019. var $container = this.render();
  1020. this.$container = $container;
  1021. $container.insertAfter(this.$element);
  1022. $container.width($element.outerWidth(false));
  1023. var SelectionAdapter = this.options.get('selectionAdapter');
  1024. this.selection = new SelectionAdapter($element, this.options);
  1025. var $selectionContainer = $container.find('.selection');
  1026. var $selection = this.selection.render();
  1027. $selectionContainer.append($selection);
  1028. var DropdownAdapter = this.options.get('dropdownAdapter');
  1029. this.dropdown = new DropdownAdapter($element, this.options);
  1030. var $dropdownContainer = $container.find('.dropdown-wrapper');
  1031. var $dropdown = this.dropdown.render();
  1032. $dropdownContainer.append($dropdown);
  1033. var ResultsAdapter = this.options.get('resultsAdapter');
  1034. this.results = new ResultsAdapter($element, this.options, this.data);
  1035. var $resultsContainer = $dropdown.find('.results');
  1036. var $results = this.results.render();
  1037. $resultsContainer.append($results);
  1038. // Bind events
  1039. var self = this;
  1040. this.data.bind(this, $container);
  1041. this.selection.bind(this, $container);
  1042. this.dropdown.bind(this, $container);
  1043. this.results.bind(this, $container);
  1044. this.$element.on('change', function () {
  1045. self.data.current(function (data) {
  1046. self.trigger('selection:update', {
  1047. data: data
  1048. });
  1049. });
  1050. });
  1051. this.selection.on('open', function () {
  1052. self.trigger('open');
  1053. });
  1054. this.selection.on('close', function () {
  1055. self.trigger('close');
  1056. });
  1057. this.selection.on('toggle', function () {
  1058. self.toggleDropdown();
  1059. });
  1060. this.selection.on('results:select', function () {
  1061. self.trigger('results:select');
  1062. });
  1063. this.selection.on('results:previous', function () {
  1064. self.trigger('results:previous');
  1065. });
  1066. this.selection.on('results:next', function () {
  1067. self.trigger('results:next');
  1068. });
  1069. this.selection.on('unselected', function (params) {
  1070. self.trigger('unselect', params);
  1071. self.trigger('close');
  1072. });
  1073. this.results.on('selected', function (params) {
  1074. self.trigger('select', params);
  1075. self.trigger('close');
  1076. });
  1077. this.results.on('unselected', function (params) {
  1078. self.trigger('unselect', params);
  1079. self.trigger('close');
  1080. });
  1081. this.results.on('results:focus', function (params) {
  1082. self.trigger('results:focus', params);
  1083. });
  1084. this.on('open', function () {
  1085. $container.addClass('open');
  1086. });
  1087. this.on('close', function () {
  1088. $container.removeClass('open');
  1089. });
  1090. // Set the initial state
  1091. this.data.current(function (initialData) {
  1092. self.trigger('selection:update', {
  1093. data: initialData
  1094. });
  1095. });
  1096. this.on('query', function (params) {
  1097. this.data.query(params, function (data) {
  1098. self.trigger('results:all', {
  1099. data: data,
  1100. query: params
  1101. });
  1102. });
  1103. });
  1104. this.trigger('query', {});
  1105. // Hide the original select
  1106. $element.hide();
  1107. $element.attr('tabindex', '-1');
  1108. };
  1109. Utils.Extend(Select2, Utils.Observable);
  1110. Select2.prototype.toggleDropdown = function () {
  1111. if (this.isOpen()) {
  1112. this.trigger('close');
  1113. } else {
  1114. this.trigger('open');
  1115. }
  1116. };
  1117. Select2.prototype.isOpen = function () {
  1118. return this.$container.hasClass('open');
  1119. };
  1120. Select2.prototype.render = function () {
  1121. var $container = $(
  1122. '<span class="select2 select2-container select2-theme-default">' +
  1123. '<span class="selection"></span>' +
  1124. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1125. '</span>'
  1126. );
  1127. return $container;
  1128. };
  1129. return Select2;
  1130. });
  1131. define('jquery.select2',[
  1132. 'jquery',
  1133. 'select2/core'
  1134. ], function ($, Select2) {
  1135. if ($.fn.select2 == null) {
  1136. $.fn.select2 = function (options) {
  1137. options = options || {};
  1138. if (typeof options === 'object') {
  1139. this.each(function () {
  1140. var instance = new Select2($(this), options);
  1141. });
  1142. } else if (typeof options === 'string') {
  1143. var instance = this.data('select2');
  1144. instance[options](arguments.slice(1));
  1145. } else {
  1146. throw new Error('Invalid arguments for Select2: ' + options);
  1147. }
  1148. };
  1149. }
  1150. return Select2;
  1151. });