select2.amd.js 30 KB

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