select2.amd.js 46 KB

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