select2.amd.js 33 KB

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