select2.amd.js 33 KB

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