select2.amd.full.js 34 KB

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