select2.amd.full.js 38 KB

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