select2.amd.full.js 53 KB

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