select2.amd.js 51 KB

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