select2.amd.js 37 KB

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