select2.amd.js 35 KB

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