select2.amd.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }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. this.listeners = this.listeners || {};
  77. if (event in this.listeners) {
  78. this.listeners[event].push(callback);
  79. } else {
  80. this.listeners[event] = [callback];
  81. }
  82. };
  83. Observable.prototype.trigger = function (event) {
  84. var slice = Array.prototype.slice;
  85. this.listeners = this.listeners || {};
  86. if (event in this.listeners) {
  87. this.invoke(this.listeners[event], slice.call(arguments, 1));
  88. }
  89. if ('*' in this.listeners) {
  90. this.invoke(this.listeners['*'], arguments);
  91. }
  92. };
  93. Observable.prototype.invoke = function (listeners, params) {
  94. for (var i = 0, len = listeners.length; i < len; i++) {
  95. listeners[i].apply(this, params);
  96. }
  97. };
  98. Utils.Observable = Observable;
  99. Utils.generateChars = function (length) {
  100. var chars = '';
  101. for (var i = 0; i < length; i++) {
  102. var randomChar = Math.floor(Math.random() * 36);
  103. chars += randomChar.toString(36);
  104. }
  105. return chars;
  106. };
  107. return Utils;
  108. });
  109. define('select2/results',[
  110. './utils'
  111. ], function (Utils) {
  112. function Results ($element, options, dataAdapter) {
  113. this.$element = $element;
  114. this.data = dataAdapter;
  115. this.options = options;
  116. Results.__super__.constructor.call(this);
  117. }
  118. Utils.Extend(Results, Utils.Observable);
  119. Results.prototype.render = function () {
  120. var $results = $(
  121. '<ul class="options" role="tree"></ul>'
  122. );
  123. if (this.options.get('multiple')) {
  124. $results.attr('aria-multiselectable', 'true');
  125. }
  126. this.$results = $results;
  127. return $results;
  128. };
  129. Results.prototype.clear = function () {
  130. this.$results.empty();
  131. };
  132. Results.prototype.displayMessage = function (params) {
  133. this.clear();
  134. var $message = $('<li role="treeitem" class="option"></li>');
  135. var message = this.options.get('translations').get(params.message);
  136. $message.text(message(params.args));
  137. this.$results.append($message);
  138. };
  139. Results.prototype.append = function (data) {
  140. var $options = [];
  141. if (data.length === 0) {
  142. this.trigger('results:message', {
  143. message: 'noResults'
  144. });
  145. return;
  146. }
  147. data = this.sort(data);
  148. for (var d = 0; d < data.length; d++) {
  149. var item = data[d];
  150. var $option = this.option(item);
  151. $options.push($option);
  152. }
  153. this.$results.append($options);
  154. };
  155. Results.prototype.sort = function (data) {
  156. return data;
  157. };
  158. Results.prototype.setClasses = function () {
  159. var self = this;
  160. this.data.current(function (selected) {
  161. var selectedIds = $.map(selected, function (s) {
  162. return s.id.toString();
  163. });
  164. var $options = self.$results.find('.option[aria-selected]');
  165. $options.each(function () {
  166. var $option = $(this);
  167. var item = $option.data('data');
  168. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  169. $option.attr('aria-selected', 'true');
  170. } else {
  171. $option.attr('aria-selected', 'false');
  172. }
  173. });
  174. var $selected = $options.filter('[aria-selected=true]');
  175. // Check if there are any selected options
  176. if ($selected.length > 0) {
  177. // If there are selected options, highlight the first
  178. $selected.first().trigger('mouseenter');
  179. } else {
  180. // If there are no selected options, highlight the first option
  181. // in the dropdown
  182. $options.first().trigger('mouseenter');
  183. }
  184. });
  185. };
  186. Results.prototype.option = function (data) {
  187. var $option = $(
  188. '<li class="option" role="treeitem" aria-selected="false"></li>'
  189. );
  190. if (data.children) {
  191. $option
  192. .attr('role', 'group')
  193. .attr('aria-label', data.text)
  194. .removeAttr('aria-selected');
  195. var $label = $('<strong class="group-label"></strong>');
  196. this.template(data, $label);
  197. var $children = [];
  198. for (var c = 0; c < data.children.length; c++) {
  199. var child = data.children[c];
  200. var $child = this.option(child);
  201. $children.push($child);
  202. }
  203. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  204. $childrenContainer.append($children);
  205. $option.append($label);
  206. $option.append($childrenContainer);
  207. } else {
  208. this.template(data, $option);
  209. }
  210. if (data.disabled) {
  211. $option
  212. .removeAttr('aria-selected')
  213. .attr('aria-disabled', 'true');
  214. }
  215. if (data.id == null) {
  216. $option.removeAttr('aria-selected');
  217. }
  218. if (data._resultId != null) {
  219. $option.attr('id', data._resultId);
  220. }
  221. $option.data('data', data);
  222. return $option;
  223. };
  224. Results.prototype.bind = function (container, $container) {
  225. var self = this;
  226. var id = container.id + '-results';
  227. this.$results.attr('id', id);
  228. container.on('results:all', function (params) {
  229. self.clear();
  230. self.append(params.data);
  231. if (container.isOpen()) {
  232. self.setClasses();
  233. }
  234. });
  235. container.on('results:append', function (params) {
  236. self.append(params.data);
  237. if (container.isOpen()) {
  238. self.setClasses();
  239. }
  240. });
  241. container.on('select', function () {
  242. if (!container.isOpen()) {
  243. return;
  244. }
  245. self.setClasses();
  246. });
  247. container.on('unselect', function () {
  248. if (!container.isOpen()) {
  249. return;
  250. }
  251. self.setClasses();
  252. });
  253. container.on('open', function () {
  254. // When the dropdown is open, aria-expended="true"
  255. self.$results.attr('aria-expanded', 'true');
  256. self.$results.attr('aria-hidden', 'false');
  257. self.setClasses();
  258. self.ensureHighlightVisible();
  259. });
  260. container.on('close', function () {
  261. // When the dropdown is closed, aria-expended="false"
  262. self.$results.attr('aria-expanded', 'false');
  263. self.$results.attr('aria-hidden', 'true');
  264. self.$results.removeAttr('aria-activedescendant');
  265. });
  266. container.on('results:select', function () {
  267. var $highlighted = self.$results.find('.highlighted');
  268. if ($highlighted.length === 0) {
  269. return;
  270. }
  271. var data = $highlighted.data('data');
  272. if ($highlighted.attr('aria-selected') == 'true') {
  273. self.trigger('unselected', {
  274. data: data
  275. });
  276. } else {
  277. self.trigger('selected', {
  278. data: data
  279. });
  280. }
  281. });
  282. container.on('results:previous', function () {
  283. var $highlighted = self.$results.find('.highlighted');
  284. var $options = self.$results.find('[aria-selected]');
  285. var currentIndex = $options.index($highlighted);
  286. // If we are already at te top, don't move further
  287. if (currentIndex === 0) {
  288. return;
  289. }
  290. var nextIndex = currentIndex - 1;
  291. // If none are highlighted, highlight the first
  292. if ($highlighted.length === 0) {
  293. nextIndex = 0;
  294. }
  295. var $next = $options.eq(nextIndex);
  296. $next.trigger('mouseenter');
  297. var currentOffset = self.$results.offset().top;
  298. var nextTop = $next.offset().top;
  299. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  300. if (nextIndex === 0) {
  301. self.$results.scrollTop(0);
  302. } else if (nextTop - currentOffset < 0) {
  303. self.$results.scrollTop(nextOffset);
  304. }
  305. });
  306. container.on('results:next', function () {
  307. var $highlighted = self.$results.find('.highlighted');
  308. var $options = self.$results.find('[aria-selected]');
  309. var currentIndex = $options.index($highlighted);
  310. var nextIndex = currentIndex + 1;
  311. // If we are at the last option, stay there
  312. if (nextIndex >= $options.length) {
  313. return;
  314. }
  315. var $next = $options.eq(nextIndex);
  316. $next.trigger('mouseenter');
  317. var currentOffset = self.$results.offset().top +
  318. self.$results.outerHeight(false);
  319. var nextBottom = $next.offset().top + $next.outerHeight(false);
  320. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  321. if (nextIndex === 0) {
  322. self.$results.scrollTop(0);
  323. } else if (nextBottom > currentOffset) {
  324. self.$results.scrollTop(nextOffset);
  325. }
  326. });
  327. container.on('results:focus', function (params) {
  328. params.element.addClass('highlighted');
  329. });
  330. container.on('results:message', function (params) {
  331. self.trigger('results:message', params);
  332. });
  333. this.on('results:message', function (params) {
  334. self.displayMessage(params);
  335. });
  336. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  337. var $this = $(this);
  338. var data = $this.data('data');
  339. if ($this.attr('aria-selected') === 'true') {
  340. self.trigger('unselected', {
  341. originalEvent: evt,
  342. data: data
  343. });
  344. return;
  345. }
  346. self.trigger('selected', {
  347. originalEvent: evt,
  348. data: data
  349. });
  350. });
  351. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  352. var data = $(this).data('data');
  353. self.$results.find('.option.highlighted').removeClass('highlighted');
  354. self.trigger('results:focus', {
  355. data: data,
  356. element: $(this)
  357. });
  358. });
  359. this.$results.on('mouseleave', '.option', function (evt) {
  360. if ($(this).hasClass('highlighted')) {
  361. $(this).removeClass('highlighted');
  362. self.$results.removeAttr('aria-activedescendant');
  363. }
  364. });
  365. };
  366. Results.prototype.ensureHighlightVisible = function () {
  367. var $highlighted = this.$results.find('.highlighted');
  368. if ($highlighted.length === 0) {
  369. return;
  370. }
  371. var $options = this.$results.find('[aria-selected]');
  372. var currentIndex = $options.index($highlighted);
  373. var currentOffset = this.$results.offset().top;
  374. var nextTop = $highlighted.offset().top;
  375. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  376. var offsetDelta = nextTop - currentOffset;
  377. nextOffset -= $highlighted.outerHeight(false) * 2;
  378. if (currentIndex <= 2) {
  379. this.$results.scrollTop(0);
  380. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  381. this.$results.scrollTop(nextOffset);
  382. }
  383. };
  384. Results.prototype.template = function (result, $container) {
  385. var template = this.options.get('templateResult');
  386. $container.html(template(result));
  387. };
  388. return Results;
  389. });
  390. define('select2/selection/base',[
  391. '../utils'
  392. ], function (Utils) {
  393. function BaseSelection ($element, options) {
  394. this.$element = $element;
  395. this.options = options;
  396. BaseSelection.__super__.constructor.call(this);
  397. }
  398. Utils.Extend(BaseSelection, Utils.Observable);
  399. BaseSelection.prototype.render = function () {
  400. throw new Error('The `render` method must be defined in child classes.');
  401. };
  402. BaseSelection.prototype.bind = function (container, $container) {
  403. var self = this;
  404. container.on('selection:update', function (params) {
  405. self.update(params.data);
  406. });
  407. container.on('open', function () {
  408. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  409. var $target = $(e.target);
  410. var $select = $target.closest('.select2');
  411. var $all = $('.select2.open');
  412. $all.each(function () {
  413. var $this = $(this);
  414. if (this == $select[0]) {
  415. return;
  416. }
  417. var $element = $this.data('element');
  418. $element.select2('close');
  419. });
  420. });
  421. container.on('close', function () {
  422. $(document.body).off('mousedown.select2.' + container.id);
  423. });
  424. });
  425. };
  426. BaseSelection.prototype.update = function (data) {
  427. throw new Error('The `update` method must be defined in child classes.');
  428. };
  429. return BaseSelection;
  430. });
  431. define('select2/keys',[
  432. ], function () {
  433. var KEYS = {
  434. BACKSPACE: 8,
  435. TAB: 9,
  436. ENTER: 13,
  437. SHIFT: 16,
  438. CTRL: 17,
  439. ALT: 18,
  440. ESC: 27,
  441. SPACE: 32,
  442. PAGE_UP: 33,
  443. PAGE_DOWN: 34,
  444. END: 35,
  445. HOME: 36,
  446. LEFT: 37,
  447. UP: 38,
  448. RIGHT: 39,
  449. DOWN: 40,
  450. DELETE: 46,
  451. isArrow: function (k) {
  452. k = k.which ? k.which : k;
  453. switch (k) {
  454. case KEY.LEFT:
  455. case KEY.RIGHT:
  456. case KEY.UP:
  457. case KEY.DOWN:
  458. return true;
  459. }
  460. return false;
  461. }
  462. };
  463. return KEYS;
  464. });
  465. define('select2/selection/single',[
  466. './base',
  467. '../utils',
  468. '../keys'
  469. ], function (BaseSelection, Utils, KEYS) {
  470. function SingleSelection () {
  471. SingleSelection.__super__.constructor.apply(this, arguments);
  472. }
  473. Utils.Extend(SingleSelection, BaseSelection);
  474. SingleSelection.prototype.render = function () {
  475. var $selection = $(
  476. '<span class="single-select" tabindex="0" role="combobox" ' +
  477. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  478. '<span class="rendered-selection"></span>' +
  479. '</span>'
  480. );
  481. $selection.attr('title', this.$element.attr('title'));
  482. this.$selection = $selection;
  483. return $selection;
  484. };
  485. SingleSelection.prototype.bind = function (container, $container) {
  486. var self = this;
  487. SingleSelection.__super__.bind.apply(this, arguments);
  488. var id = container.id + '-container';
  489. var resultsId = container.id + '-results';
  490. this.$selection.find('.rendered-selection').attr('id', id);
  491. this.$selection.attr('aria-labelledby', id);
  492. this.$selection.attr('aria-owns', resultsId);
  493. this.$selection.on('mousedown', function (evt) {
  494. // Only respond to left clicks
  495. if (evt.which !== 1) {
  496. return;
  497. }
  498. self.trigger('toggle', {
  499. originalEvent: evt
  500. });
  501. });
  502. container.on('open', function () {
  503. // When the dropdown is open, aria-expanded="true"
  504. self.$selection.attr('aria-expanded', 'true');
  505. });
  506. container.on('close', function () {
  507. // When the dropdown is closed, aria-expanded="false"
  508. self.$selection.attr('aria-expanded', 'false');
  509. self.$selection.removeAttr('aria-activedescendant');
  510. self.$selection.focus();
  511. });
  512. this.$selection.on('focus', function (evt) {
  513. // User focuses on the container
  514. });
  515. this.$selection.on('blur', function (evt) {
  516. // User exits the container
  517. });
  518. this.$selection.on('keydown', function (evt) {
  519. self.trigger('keypress', evt);
  520. if (evt.which === KEYS.SPACE) {
  521. evt.preventDefault();
  522. }
  523. });
  524. container.on('results:focus', function (params) {
  525. self.$selection.attr('aria-activedescendant', params.data._resultId);
  526. });
  527. container.on('selection:update', function (params) {
  528. self.update(params.data);
  529. });
  530. };
  531. SingleSelection.prototype.clear = function () {
  532. this.$selection.find('.rendered-selection').empty();
  533. };
  534. SingleSelection.prototype.display = function (data) {
  535. var template = this.options.get('templateSelection');
  536. return template(data);
  537. };
  538. SingleSelection.prototype.selectionContainer = function () {
  539. return $('<span></span>');
  540. };
  541. SingleSelection.prototype.update = function (data) {
  542. if (data.length === 0) {
  543. this.clear();
  544. return;
  545. }
  546. var selection = data[0];
  547. var formatted = this.display(selection);
  548. this.$selection.find('.rendered-selection').html(formatted);
  549. };
  550. return SingleSelection;
  551. });
  552. define('select2/selection/multiple',[
  553. './base',
  554. '../utils'
  555. ], function (BaseSelection, Utils) {
  556. function MultipleSelection ($element, options) {
  557. MultipleSelection.__super__.constructor.apply(this, arguments);
  558. }
  559. Utils.Extend(MultipleSelection, BaseSelection);
  560. MultipleSelection.prototype.render = function () {
  561. var $selection = $(
  562. '<span class="multiple-select">' +
  563. '<ul class="rendered-selection"></ul>' +
  564. '</span>'
  565. );
  566. this.$selection = $selection;
  567. return $selection;
  568. };
  569. MultipleSelection.prototype.bind = function (container, $container) {
  570. var self = this;
  571. MultipleSelection.__super__.bind.apply(this, arguments);
  572. this.$selection.on('click', function (evt) {
  573. self.trigger('toggle', {
  574. originalEvent: evt
  575. });
  576. });
  577. this.$selection.on('click', '.remove', function (evt) {
  578. var $remove = $(this);
  579. var $selection = $remove.parent();
  580. var data = $selection.data('data');
  581. self.trigger('unselected', {
  582. originalEvent: evt,
  583. data: data
  584. });
  585. });
  586. };
  587. MultipleSelection.prototype.clear = function () {
  588. this.$selection.find('.rendered-selection').empty();
  589. };
  590. MultipleSelection.prototype.display = function (data) {
  591. var template = this.options.get('templateSelection');
  592. return template(data);
  593. };
  594. MultipleSelection.prototype.selectionContainer = function () {
  595. var $container = $(
  596. '<li class="choice">' +
  597. '<span class="remove" role="presentation">&times;</span>' +
  598. '</li>'
  599. );
  600. return $container;
  601. };
  602. MultipleSelection.prototype.update = function (data) {
  603. this.clear();
  604. if (data.length === 0) {
  605. return;
  606. }
  607. var $selections = [];
  608. for (var d = 0; d < data.length; d++) {
  609. var selection = data[d];
  610. var formatted = this.display(selection);
  611. var $selection = this.selectionContainer();
  612. $selection.append(formatted);
  613. $selection.data('data', selection);
  614. $selections.push($selection);
  615. }
  616. this.$selection.find('.rendered-selection').append($selections);
  617. };
  618. return MultipleSelection;
  619. });
  620. define('select2/selection/placeholder',[
  621. '../utils'
  622. ], function (Utils) {
  623. function Placeholder (decorated, $element, options) {
  624. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  625. decorated.call(this, $element, options);
  626. }
  627. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  628. if (typeof placeholder === 'string') {
  629. placeholder = {
  630. id: '',
  631. text: placeholder
  632. };
  633. }
  634. return placeholder;
  635. };
  636. Placeholder.prototype.update = function (decorated, data) {
  637. var singlePlaceholder = (
  638. data.length == 1 && data[0].id != this.placeholder.id
  639. );
  640. var multipleSelections = data.length > 1;
  641. if (multipleSelections || singlePlaceholder) {
  642. return decorated.call(this, data);
  643. }
  644. this.clear();
  645. var $placeholder = this.selectionContainer();
  646. $placeholder.html(this.display(this.placeholder));
  647. $placeholder.addClass('placeholder').removeClass('choice');
  648. this.$selection.find('.rendered-selection').append($placeholder);
  649. };
  650. return Placeholder;
  651. });
  652. define('select2/translation',[
  653. ], function () {
  654. function Translation (dict) {
  655. this.dict = dict || {};
  656. }
  657. Translation.prototype.all = function () {
  658. return this.dict;
  659. };
  660. Translation.prototype.get = function (key) {
  661. return this.dict[key];
  662. };
  663. Translation.prototype.extend = function (translation) {
  664. this.dict = $.extend({}, translation.all(), this.dict);
  665. };
  666. // Static functions
  667. Translation._cache = {};
  668. Translation.loadPath = function (path) {
  669. if (!(path in Translation._cache)) {
  670. var translations = require(path);
  671. Translation._cache[path] = translations;
  672. }
  673. return new Translation(Translation._cache[path]);
  674. };
  675. return Translation;
  676. });
  677. define('select2/data/base',[
  678. '../utils'
  679. ], function (Utils) {
  680. function BaseAdapter ($element, options) {
  681. BaseAdapter.__super__.constructor.call(this);
  682. }
  683. Utils.Extend(BaseAdapter, Utils.Observable);
  684. BaseAdapter.prototype.current = function (callback) {
  685. throw new Error('The `current` method must be defined in child classes.');
  686. };
  687. BaseAdapter.prototype.query = function (params, callback) {
  688. throw new Error('The `query` method must be defined in child classes.');
  689. };
  690. BaseAdapter.prototype.bind = function (container, $container) {
  691. // Can be implemented in subclasses
  692. };
  693. BaseAdapter.prototype.generateResultId = function (container, data) {
  694. var id = container.id + '-result-';
  695. id += Utils.generateChars(4);
  696. if (data.id != null) {
  697. id += '-' + data.id.toString();
  698. } else {
  699. id += '-' + Utils.generateChars(4);
  700. }
  701. return id;
  702. };
  703. return BaseAdapter;
  704. });
  705. define('select2/data/select',[
  706. './base',
  707. '../utils',
  708. 'jquery'
  709. ], function (BaseAdapter, Utils, $) {
  710. function SelectAdapter ($element, options) {
  711. this.$element = $element;
  712. SelectAdapter.__super__.constructor.call(this);
  713. }
  714. Utils.Extend(SelectAdapter, BaseAdapter);
  715. SelectAdapter.prototype.current = function (callback) {
  716. var data = [];
  717. var self = this;
  718. this.$element.find(':selected').each(function () {
  719. var $option = $(this);
  720. var option = self.item($option);
  721. data.push(option);
  722. });
  723. callback(data);
  724. };
  725. SelectAdapter.prototype.select = function (data) {
  726. var self = this;
  727. if (this.$element.prop('multiple')) {
  728. this.current(function (currentData) {
  729. var val = [];
  730. data = [data];
  731. data.push.apply(data, currentData);
  732. for (var d = 0; d < data.length; d++) {
  733. id = data[d].id;
  734. if (val.indexOf(id) === -1) {
  735. val.push(id);
  736. }
  737. }
  738. self.$element.val(val);
  739. self.$element.trigger('change');
  740. });
  741. } else {
  742. var val = data.id;
  743. this.$element.val(val);
  744. this.$element.trigger('change');
  745. }
  746. };
  747. SelectAdapter.prototype.unselect = function (data) {
  748. var self = this;
  749. if (!this.$element.prop('multiple')) {
  750. return;
  751. }
  752. this.current(function (currentData) {
  753. var val = [];
  754. for (var d = 0; d < currentData.length; d++) {
  755. id = currentData[d].id;
  756. if (id !== data.id && val.indexOf(id) === -1) {
  757. val.push(id);
  758. }
  759. }
  760. self.$element.val(val);
  761. self.$element.trigger('change');
  762. });
  763. };
  764. SelectAdapter.prototype.bind = function (container, $container) {
  765. var self = this;
  766. this.container = container;
  767. container.on('select', function (params) {
  768. self.select(params.data);
  769. });
  770. container.on('unselect', function (params) {
  771. self.unselect(params.data);
  772. });
  773. };
  774. SelectAdapter.prototype.query = function (params, callback) {
  775. var data = [];
  776. var self = this;
  777. var $options = this.$element.children();
  778. $options.each(function () {
  779. var $option = $(this);
  780. if (!$option.is('option') && !$option.is('optgroup')) {
  781. return;
  782. }
  783. var option = self.item($option);
  784. var matches = self.matches(params, option);
  785. if (matches !== null) {
  786. data.push(matches);
  787. }
  788. });
  789. callback(data);
  790. };
  791. SelectAdapter.prototype.option = function (data) {
  792. var $option = $('<option></option>');
  793. $option.text(data.text);
  794. $option.val(data.id);
  795. $option.prop('disabled', data.disabled || false);
  796. // Get any automatically generated data values
  797. var detectedData = this.item($option);
  798. // Merge it with the already present data
  799. var combinedData = $.extend({}, data, detectedData);
  800. // Override the option's data with the combined data
  801. $option.data('data', combinedData);
  802. return $option;
  803. };
  804. SelectAdapter.prototype.item = function ($option) {
  805. var data = $option.data('data');
  806. // If the data has already be generated, use it
  807. if (data == null) {
  808. if ($option.is('option')) {
  809. data = {
  810. id: $option.val(),
  811. text: $option.html(),
  812. disabled: $option.prop('disabled')
  813. };
  814. } else if ($option.is('optgroup')) {
  815. data = {
  816. text: $option.attr('label'),
  817. children: []
  818. };
  819. var $children = $option.children('option');
  820. var children = [];
  821. for (var c = 0; c < $children.length; c++) {
  822. var $child = $($children[c]);
  823. var child = this.item($child);
  824. children.push(child);
  825. }
  826. data.children = children;
  827. }
  828. if (data.id && this.container != null) {
  829. data._resultId = this.generateResultId(this.container, data);
  830. }
  831. $option.data('data', data);
  832. }
  833. return data;
  834. };
  835. SelectAdapter.prototype.matches = function (params, data) {
  836. var match = $.extend(true, {}, data);
  837. if (data.children) {
  838. for (var c = data.children.length - 1; c >= 0; c--) {
  839. var child = data.children[c];
  840. var matches = this.matches(params, child);
  841. // If there wasn't a match, remove the object in the array
  842. if (matches === null) {
  843. match.children.splice(c, 1);
  844. }
  845. }
  846. if (match.children.length > 0) {
  847. return match;
  848. }
  849. }
  850. if ($.trim(params.term) === '') {
  851. return match;
  852. }
  853. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  854. return match;
  855. }
  856. return null;
  857. };
  858. return SelectAdapter;
  859. });
  860. define('select2/data/array',[
  861. './select',
  862. '../utils',
  863. 'jquery'
  864. ], function (SelectAdapter, Utils, $) {
  865. function ArrayAdapter ($element, options) {
  866. this.data = options.get('data');
  867. ArrayAdapter.__super__.constructor.call(this, $element, options);
  868. }
  869. Utils.Extend(ArrayAdapter, SelectAdapter);
  870. ArrayAdapter.prototype.select = function (data) {
  871. var self = this;
  872. this.$element.find('option').each(function () {
  873. var $option = $(this);
  874. var option = self.item($option);
  875. if (option.id == data.id.toString()) {
  876. $option.remove();
  877. }
  878. });
  879. var $option = this.option(data);
  880. this.$element.append($option);
  881. ArrayAdapter.__super__.select.call(this, data);
  882. };
  883. ArrayAdapter.prototype.query = function (params, callback) {
  884. var matches = [];
  885. var self = this;
  886. $.each(this.data, function () {
  887. var option = this;
  888. if (self.matches(params, option)) {
  889. matches.push(option);
  890. }
  891. });
  892. callback(matches);
  893. };
  894. return ArrayAdapter;
  895. });
  896. define('select2/data/ajax',[
  897. './array',
  898. '../utils',
  899. 'jquery'
  900. ], function (ArrayAdapter, Utils, $) {
  901. function AjaxAdapter ($element, options) {
  902. this.ajaxOptions = options.get('ajax');
  903. if (this.ajaxOptions.processResults != null) {
  904. this.processResults = this.ajaxOptions.processResults;
  905. }
  906. ArrayAdapter.__super__.constructor.call(this, $element, options);
  907. }
  908. Utils.Extend(AjaxAdapter, ArrayAdapter);
  909. AjaxAdapter.prototype.processResults = function (results) {
  910. return results;
  911. };
  912. AjaxAdapter.prototype.query = function (params, callback) {
  913. var matches = [];
  914. var self = this;
  915. var options = $.extend({
  916. type: 'GET'
  917. }, this.ajaxOptions);
  918. if (typeof options.url === 'function') {
  919. options.url = options.url(params);
  920. }
  921. if (typeof options.data === 'function') {
  922. options.data = options.data(params);
  923. }
  924. function request () {
  925. var $request = $.ajax(options);
  926. $request.success(function (data) {
  927. var results = self.processResults(data);
  928. callback(results);
  929. });
  930. }
  931. if (this.ajaxOptions.delay && params.term !== '') {
  932. if (this._queryTimeout) {
  933. window.clearTimeout(this._queryTimeout);
  934. }
  935. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  936. } else {
  937. request();
  938. }
  939. };
  940. return AjaxAdapter;
  941. });
  942. define('select2/data/tags',[
  943. ], function () {
  944. function Tags (decorated, $element, options) {
  945. var tags = options.get('tags');
  946. decorated.call(this, $element, options);
  947. }
  948. Tags.prototype.query = function (decorated, params, callback) {
  949. var self = this;
  950. this._removeOldTags();
  951. if (params.term == null || params.term === '' || params.page != null) {
  952. decorated.call(this, params, callback);
  953. return;
  954. }
  955. function wrapper (data, child) {
  956. for (var i = 0; i < data.length; i++) {
  957. var option = data[i];
  958. var checkChildren = (
  959. option.children != null && !wrapper(option.children, true)
  960. );
  961. var checkText = option.text === params.term;
  962. if (checkText || checkChildren) {
  963. if (child) {
  964. return false;
  965. }
  966. callback(data);
  967. return;
  968. }
  969. }
  970. if (child) {
  971. return true;
  972. }
  973. var tag = self.createTag(params);
  974. var $option = self.option(tag);
  975. $option.attr('data-select2-tag', true);
  976. self.$element.append($option);
  977. self.insertTag(data, tag);
  978. callback(data);
  979. }
  980. decorated.call(this, params, wrapper);
  981. };
  982. Tags.prototype.createTag = function (decorated, params) {
  983. return {
  984. id: params.term,
  985. text: params.term
  986. };
  987. };
  988. Tags.prototype.insertTag = function (_, data, tag) {
  989. data.unshift(tag);
  990. };
  991. Tags.prototype._removeOldTags = function (_) {
  992. var tag = this._lastTag;
  993. var $options = this.$element.find('option[data-select2-tag]');
  994. $options.each(function () {
  995. if (this.selected) {
  996. return;
  997. }
  998. $(this).remove();
  999. });
  1000. };
  1001. return Tags;
  1002. });
  1003. define('select2/data/minimumInputLength',[
  1004. ], function () {
  1005. function MinimumInputLength (decorated, $e, options) {
  1006. this.minimumInputLength = options.get('minimumInputLength');
  1007. decorated.call(this, $e, options);
  1008. }
  1009. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1010. params.term = params.term || '';
  1011. if (params.term.length < this.minimumInputLength) {
  1012. this.trigger('results:message', {
  1013. message: 'inputTooShort',
  1014. args: {
  1015. minimum: this.minimumInputLength,
  1016. input: params.term,
  1017. params: params
  1018. }
  1019. });
  1020. return;
  1021. }
  1022. decorated.call(this, params, callback);
  1023. };
  1024. return MinimumInputLength;
  1025. });
  1026. define('select2/dropdown',[
  1027. './utils'
  1028. ], function (Utils) {
  1029. function Dropdown ($element, options) {
  1030. this.$element = $element;
  1031. }
  1032. Utils.Extend(Dropdown, Utils.Observable);
  1033. Dropdown.prototype.render = function () {
  1034. var $dropdown = $(
  1035. '<span class="dropdown">' +
  1036. '<span class="results"></span>' +
  1037. '</span>'
  1038. );
  1039. return $dropdown;
  1040. };
  1041. Dropdown.prototype.bind = function (container, $container) {
  1042. // Can be implemented in subclasses
  1043. };
  1044. return Dropdown;
  1045. });
  1046. define('select2/dropdown/search',[
  1047. '../utils'
  1048. ], function (Utils) {
  1049. function Search () { }
  1050. Search.prototype.render = function (decorated) {
  1051. var $rendered = decorated.call(this);
  1052. var $search = $(
  1053. '<span class="search">' +
  1054. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1055. '</span>'
  1056. );
  1057. this.$searchContainer = $search;
  1058. this.$search = $search.find('input');
  1059. $rendered.prepend($search);
  1060. return $rendered;
  1061. };
  1062. Search.prototype.bind = function (decorated, container, $container) {
  1063. var self = this;
  1064. decorated.call(this, container, $container);
  1065. this.$search.on('keydown', function (evt) {
  1066. self.trigger('keypress', evt);
  1067. self._keyUpPrevented = evt.isDefaultPrevented();
  1068. });
  1069. this.$search.on('keyup', function (evt) {
  1070. self.handleSearch(evt);
  1071. });
  1072. container.on('open', function () {
  1073. self.$search.attr('tabindex', 0);
  1074. });
  1075. container.on('close', function () {
  1076. self.$search.attr('tabindex', -1);
  1077. self.$search.val('');
  1078. });
  1079. container.on('results:all', function (params) {
  1080. if (params.query.term == null || params.query.term === '') {
  1081. var showSearch = self.showSearch(params);
  1082. if (showSearch) {
  1083. self.$searchContainer.show();
  1084. } else {
  1085. self.$searchContainer.hide();
  1086. }
  1087. }
  1088. });
  1089. };
  1090. Search.prototype.handleSearch = function (evt) {
  1091. if (!this._keyUpPrevented) {
  1092. var input = this.$search.val();
  1093. this.trigger('query', {
  1094. term: input
  1095. });
  1096. }
  1097. this._keyUpPrevented = false;
  1098. };
  1099. Search.prototype.showSearch = function (_, params) {
  1100. return true;
  1101. };
  1102. return Search;
  1103. });
  1104. define('select2/dropdown/hidePlaceholder',[
  1105. ], function () {
  1106. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1107. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1108. decorated.call(this, $element, options, dataAdapter);
  1109. }
  1110. HidePlaceholder.prototype.append = function (decorated, data) {
  1111. data = this.removePlaceholder(data);
  1112. decorated.call(this, data);
  1113. };
  1114. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1115. if (typeof placeholder === 'string') {
  1116. placeholder = {
  1117. id: '',
  1118. text: placeholder
  1119. };
  1120. }
  1121. return placeholder;
  1122. };
  1123. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1124. var modifiedData = data.slice(0);
  1125. for (var d = data.length - 1; d >= 0; d--) {
  1126. var item = data[d];
  1127. if (this.placeholder.id === item.id) {
  1128. modifiedData.splice(d, 1);
  1129. }
  1130. }
  1131. return modifiedData;
  1132. };
  1133. return HidePlaceholder;
  1134. });
  1135. define('select2/i18n/en',[],function () {
  1136. return {
  1137. inputTooShort: function (args) {
  1138. var remainingChars = args.minimum - args.input.length;
  1139. var message = 'Please enter ' + remainingChars + ' or more character';
  1140. if (remainingChars != 1) {
  1141. message += 's';
  1142. }
  1143. return message;
  1144. },
  1145. noResults: function () {
  1146. return 'No results found';
  1147. }
  1148. };
  1149. });
  1150. define('select2/defaults',[
  1151. 'jquery',
  1152. './results',
  1153. './selection/single',
  1154. './selection/multiple',
  1155. './selection/placeholder',
  1156. './utils',
  1157. './translation',
  1158. './data/select',
  1159. './data/array',
  1160. './data/ajax',
  1161. './data/tags',
  1162. './data/minimumInputLength',
  1163. './dropdown',
  1164. './dropdown/search',
  1165. './dropdown/hidePlaceholder',
  1166. './i18n/en'
  1167. ], function ($, ResultsList,
  1168. SingleSelection, MultipleSelection, Placeholder,
  1169. Utils, Translation,
  1170. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1171. Dropdown, Search, HidePlaceholder,
  1172. EnglishTranslation) {
  1173. function Defaults () {
  1174. this.reset();
  1175. }
  1176. Defaults.prototype.apply = function (options) {
  1177. options = $.extend({}, this.defaults, options);
  1178. if (options.dataAdapter == null) {
  1179. if (options.ajax) {
  1180. options.dataAdapter = AjaxData;
  1181. } else if (options.data) {
  1182. options.dataAdapter = ArrayData;
  1183. } else {
  1184. options.dataAdapter = SelectData;
  1185. }
  1186. }
  1187. if (options.minimumInputLength > 0) {
  1188. options.dataAdapter = Utils.Decorate(
  1189. options.dataAdapter,
  1190. MinimumInputLength
  1191. );
  1192. }
  1193. if (options.tags != null) {
  1194. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1195. }
  1196. if (options.resultsAdapter == null) {
  1197. options.resultsAdapter = ResultsList;
  1198. if (options.placeholder != null) {
  1199. options.resultsAdapter = Utils.Decorate(
  1200. options.resultsAdapter,
  1201. HidePlaceholder
  1202. );
  1203. }
  1204. }
  1205. if (options.dropdownAdapter == null) {
  1206. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1207. options.dropdownAdapter = SearchableDropdown;
  1208. }
  1209. if (options.selectionAdapter == null) {
  1210. if (options.multiple) {
  1211. options.selectionAdapter = MultipleSelection;
  1212. } else {
  1213. options.selectionAdapter = SingleSelection;
  1214. }
  1215. // Add the placeholder mixin if a placeholder was specified
  1216. if (options.placeholder != null) {
  1217. options.selectionAdapter = Utils.Decorate(
  1218. options.selectionAdapter,
  1219. Placeholder
  1220. );
  1221. }
  1222. }
  1223. if (typeof options.language === 'string') {
  1224. options.language = [options.language];
  1225. }
  1226. if ($.isArray(options.language)) {
  1227. var languages = new Translation();
  1228. var languageNames = options.language.concat(this.defaults.language);
  1229. for (var l = 0; l < languageNames.length; l++) {
  1230. var name = languageNames[l];
  1231. var language = {};
  1232. try {
  1233. // Try to load it with the original name
  1234. language = Translation.loadPath(name);
  1235. } catch (e) {
  1236. // If we couldn't load it, check if it wasn't the full path
  1237. name = 'select2/i18n/' + name;
  1238. language = Translation.loadPath(name);
  1239. }
  1240. languages.extend(language);
  1241. }
  1242. options.translations = languages;
  1243. } else {
  1244. options.translations = new Translations(options.language);
  1245. }
  1246. return options;
  1247. };
  1248. Defaults.prototype.reset = function () {
  1249. this.defaults = {
  1250. language: ['select2/i18n/en'],
  1251. minimumInputLength: 0,
  1252. templateResult: function (result) {
  1253. return result.text;
  1254. },
  1255. templateSelection: function (selection) {
  1256. return selection.text;
  1257. }
  1258. };
  1259. };
  1260. var defaults = new Defaults();
  1261. return defaults;
  1262. });
  1263. define('select2/options',[
  1264. './defaults'
  1265. ], function (Defaults) {
  1266. function Options (options, $element) {
  1267. this.options = options;
  1268. if ($element != null) {
  1269. this.fromElement($element);
  1270. }
  1271. this.options = Defaults.apply(this.options);
  1272. }
  1273. Options.prototype.fromElement = function ($e) {
  1274. if (this.options.multiple == null) {
  1275. this.options.multiple = $e.prop('multiple');
  1276. }
  1277. return this;
  1278. };
  1279. Options.prototype.get = function (key) {
  1280. return this.options[key];
  1281. };
  1282. Options.prototype.set = function (key, val) {
  1283. this.options[key] = val;
  1284. };
  1285. return Options;
  1286. });
  1287. define('select2/core',[
  1288. 'jquery',
  1289. './options',
  1290. './utils',
  1291. './keys'
  1292. ], function ($, Options, Utils, KEYS) {
  1293. var Select2 = function ($element, options) {
  1294. this.$element = $element;
  1295. this.id = this._generateId($element);
  1296. options = options || {};
  1297. this.options = new Options(options, $element);
  1298. Select2.__super__.constructor.call(this);
  1299. // Set up containers and adapters
  1300. var DataAdapter = this.options.get('dataAdapter');
  1301. this.data = new DataAdapter($element, this.options);
  1302. var $container = this.render();
  1303. this._placeContainer($container);
  1304. var SelectionAdapter = this.options.get('selectionAdapter');
  1305. this.selection = new SelectionAdapter($element, this.options);
  1306. var $selection = this.selection.render();
  1307. this._placeSelection($selection);
  1308. var DropdownAdapter = this.options.get('dropdownAdapter');
  1309. this.dropdown = new DropdownAdapter($element, this.options);
  1310. var $dropdown = this.dropdown.render();
  1311. this._placeDropdown($dropdown);
  1312. var ResultsAdapter = this.options.get('resultsAdapter');
  1313. this.results = new ResultsAdapter($element, this.options, this.data);
  1314. var $results = this.results.render();
  1315. this._placeResults($results);
  1316. // Bind events
  1317. var self = this;
  1318. // Bind the container to all of the adapters
  1319. this._bindAdapters();
  1320. // Register any DOM event handlers
  1321. this._registerDomEvents();
  1322. // Register any internal event handlers
  1323. this._registerDataEvents();
  1324. this._registerSelectionEvents();
  1325. this._registerDropdownEvents();
  1326. this._registerResultsEvents();
  1327. this._registerEvents();
  1328. // Set the initial state
  1329. this.data.current(function (initialData) {
  1330. self.trigger('selection:update', {
  1331. data: initialData
  1332. });
  1333. });
  1334. // Hide the original select
  1335. $element.hide();
  1336. $element.attr('tabindex', '-1');
  1337. $element.data('select2', this);
  1338. };
  1339. Utils.Extend(Select2, Utils.Observable);
  1340. Select2.prototype._generateId = function ($element) {
  1341. var id = '';
  1342. if ($element.attr('id') != null) {
  1343. id = $element.attr('id');
  1344. } else if ($element.attr('name') != null) {
  1345. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1346. } else {
  1347. id = Utils.generateChars(4);
  1348. }
  1349. id = 'select2-' + id;
  1350. return id;
  1351. };
  1352. Select2.prototype._placeContainer = function ($container) {
  1353. $container.insertAfter(this.$element);
  1354. $container.width(this.$element.outerWidth(false));
  1355. };
  1356. Select2.prototype._placeSelection = function ($selection) {
  1357. var $selectionContainer = this.$container.find('.selection');
  1358. $selectionContainer.append($selection);
  1359. };
  1360. Select2.prototype._placeDropdown = function ($dropdown) {
  1361. this.$dropdown = $dropdown;
  1362. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1363. $dropdownContainer.append($dropdown);
  1364. };
  1365. Select2.prototype._placeResults = function ($results) {
  1366. var $resultsContainer = this.$dropdown.find('.results');
  1367. $resultsContainer.append($results);
  1368. };
  1369. Select2.prototype._bindAdapters = function () {
  1370. this.data.bind(this, this.$container);
  1371. this.selection.bind(this, this.$container);
  1372. this.dropdown.bind(this, this.$container);
  1373. this.results.bind(this, this.$container);
  1374. };
  1375. Select2.prototype._registerDomEvents = function () {
  1376. var self = this;
  1377. this.$element.on('change', function () {
  1378. self.data.current(function (data) {
  1379. self.trigger('selection:update', {
  1380. data: data
  1381. });
  1382. });
  1383. });
  1384. };
  1385. Select2.prototype._registerDataEvents = function () {
  1386. var self = this;
  1387. this.data.on('results:message', function (params) {
  1388. self.trigger('results:message', params);
  1389. });
  1390. };
  1391. Select2.prototype._registerSelectionEvents = function () {
  1392. var self = this;
  1393. this.selection.on('open', function () {
  1394. self.open();
  1395. });
  1396. this.selection.on('close', function () {
  1397. self.close();
  1398. });
  1399. this.selection.on('toggle', function () {
  1400. self.toggleDropdown();
  1401. });
  1402. this.selection.on('results:select', function () {
  1403. self.trigger('results:select');
  1404. });
  1405. this.selection.on('results:previous', function () {
  1406. self.trigger('results:previous');
  1407. });
  1408. this.selection.on('results:next', function () {
  1409. self.trigger('results:next');
  1410. });
  1411. this.selection.on('unselected', function (params) {
  1412. self.trigger('unselect', params);
  1413. self.close();
  1414. });
  1415. this.selection.on('keypress', function (e) {
  1416. self.trigger('keypress', e);
  1417. });
  1418. };
  1419. Select2.prototype._registerDropdownEvents = function () {
  1420. var self = this;
  1421. this.dropdown.on('query', function (params) {
  1422. self.trigger('query', params);
  1423. });
  1424. this.dropdown.on('keypress', function (e) {
  1425. self.trigger('keypress', e);
  1426. });
  1427. };
  1428. Select2.prototype._registerResultsEvents = function () {
  1429. var self = this;
  1430. this.results.on('selected', function (params) {
  1431. self.trigger('select', params);
  1432. self.close();
  1433. });
  1434. this.results.on('unselected', function (params) {
  1435. self.trigger('unselect', params);
  1436. self.close();
  1437. });
  1438. this.results.on('results:focus', function (params) {
  1439. self.trigger('results:focus', params);
  1440. });
  1441. };
  1442. Select2.prototype._registerEvents = function () {
  1443. var self = this;
  1444. this.on('open', function () {
  1445. self.$container.addClass('open');
  1446. });
  1447. this.on('close', function () {
  1448. self.$container.removeClass('open');
  1449. });
  1450. this.on('query', function (params) {
  1451. this.data.query(params, function (data) {
  1452. self.trigger('results:all', {
  1453. data: data,
  1454. query: params
  1455. });
  1456. });
  1457. });
  1458. this.on('keypress', function (evt) {
  1459. var key = evt.which;
  1460. if (self.isOpen()) {
  1461. if (key === KEYS.ENTER) {
  1462. self.trigger('results:select');
  1463. evt.preventDefault();
  1464. } else if (key === KEYS.UP) {
  1465. self.trigger('results:previous');
  1466. evt.preventDefault();
  1467. } else if (key === KEYS.DOWN) {
  1468. self.trigger('results:next');
  1469. evt.preventDefault();
  1470. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1471. self.close();
  1472. evt.preventDefault();
  1473. }
  1474. } else {
  1475. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1476. self.open();
  1477. evt.preventDefault();
  1478. }
  1479. }
  1480. });
  1481. };
  1482. Select2.prototype.toggleDropdown = function () {
  1483. if (this.isOpen()) {
  1484. this.close();
  1485. } else {
  1486. this.open();
  1487. }
  1488. };
  1489. Select2.prototype.open = function () {
  1490. if (this.isOpen()) {
  1491. return;
  1492. }
  1493. this.trigger('query', {});
  1494. this.trigger('open');
  1495. };
  1496. Select2.prototype.close = function () {
  1497. if (!this.isOpen()) {
  1498. return;
  1499. }
  1500. this.trigger('close');
  1501. };
  1502. Select2.prototype.isOpen = function () {
  1503. return this.$container.hasClass('open');
  1504. };
  1505. Select2.prototype.render = function () {
  1506. var $container = $(
  1507. '<span class="select2 select2-container select2-theme-default">' +
  1508. '<span class="selection"></span>' +
  1509. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1510. '</span>'
  1511. );
  1512. this.$container = $container;
  1513. $container.data('element', this.$element);
  1514. return $container;
  1515. };
  1516. return Select2;
  1517. });
  1518. define('jquery.select2',[
  1519. 'jquery',
  1520. 'select2/core'
  1521. ], function ($, Select2) {
  1522. if ($.fn.select2 == null) {
  1523. $.fn.select2 = function (options) {
  1524. options = options || {};
  1525. if (typeof options === 'object') {
  1526. this.each(function () {
  1527. var instance = new Select2($(this), options);
  1528. });
  1529. } else if (typeof options === 'string') {
  1530. var instance = this.data('select2');
  1531. var args = Array.prototype.slice.call(arguments, 1);
  1532. instance[options](args);
  1533. } else {
  1534. throw new Error('Invalid arguments for Select2: ' + options);
  1535. }
  1536. };
  1537. }
  1538. return Select2;
  1539. });
  1540. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());