select2.amd.full.js 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  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.select = function (data) {
  885. var $option = this.$element.find('option[value="' + data.id + '"]');
  886. if ($option.length === 0) {
  887. $option = this.option(data);
  888. this.$element.append($option);
  889. }
  890. ArrayAdapter.__super__.select.call(this, data);
  891. };
  892. ArrayAdapter.prototype.convertToOptions = function (data) {
  893. var self = this;
  894. var $existing = this.$element.find('option');
  895. var existingIds = $existing.map(function () {
  896. return self.item($(this)).id;
  897. }).get();
  898. // Filter out all items except for the one passed in the argument
  899. function onlyItem (item) {
  900. return function () {
  901. return $(this).val() == item.id;
  902. };
  903. }
  904. for (var d = 0; d < data.length; d++) {
  905. var item = data[d];
  906. item.id = item.id.toString();
  907. // Skip items which were pre-loaded, only merge the data
  908. if (existingIds.indexOf(item.id) >= 0) {
  909. var $existingOption = $existing.filter(onlyItem(item));
  910. var existingData = this.item($existingOption);
  911. var newData = $.extend(true, {}, existingData, item);
  912. var $newOption = this.option(existingData);
  913. $existingOption.replaceWith($newOption);
  914. continue;
  915. }
  916. var $option = this.option(item);
  917. this.$element.append($option);
  918. }
  919. };
  920. return ArrayAdapter;
  921. });
  922. define('select2/data/ajax',[
  923. './array',
  924. '../utils',
  925. 'jquery'
  926. ], function (ArrayAdapter, Utils, $) {
  927. function AjaxAdapter ($element, options) {
  928. this.ajaxOptions = options.get('ajax');
  929. if (this.ajaxOptions.processResults != null) {
  930. this.processResults = this.ajaxOptions.processResults;
  931. }
  932. ArrayAdapter.__super__.constructor.call(this, $element, options);
  933. }
  934. Utils.Extend(AjaxAdapter, ArrayAdapter);
  935. AjaxAdapter.prototype.processResults = function (results) {
  936. return results;
  937. };
  938. AjaxAdapter.prototype.query = function (params, callback) {
  939. var matches = [];
  940. var self = this;
  941. var options = $.extend({
  942. type: 'GET'
  943. }, this.ajaxOptions);
  944. if (typeof options.url === 'function') {
  945. options.url = options.url(params);
  946. }
  947. if (typeof options.data === 'function') {
  948. options.data = options.data(params);
  949. }
  950. function request () {
  951. var $request = $.ajax(options);
  952. $request.success(function (data) {
  953. var results = self.processResults(data);
  954. callback(results);
  955. });
  956. }
  957. if (this.ajaxOptions.delay && params.term !== '') {
  958. if (this._queryTimeout) {
  959. window.clearTimeout(this._queryTimeout);
  960. }
  961. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  962. } else {
  963. request();
  964. }
  965. };
  966. return AjaxAdapter;
  967. });
  968. define('select2/data/tags',[
  969. ], function () {
  970. function Tags (decorated, $element, options) {
  971. var tags = options.get('tags');
  972. decorated.call(this, $element, options);
  973. }
  974. Tags.prototype.query = function (decorated, params, callback) {
  975. var self = this;
  976. this._removeOldTags();
  977. if (params.term == null || params.term === '' || params.page != null) {
  978. decorated.call(this, params, callback);
  979. return;
  980. }
  981. function wrapper (data, child) {
  982. for (var i = 0; i < data.length; i++) {
  983. var option = data[i];
  984. var checkChildren = (
  985. option.children != null && !wrapper(option.children, true)
  986. );
  987. var checkText = option.text === params.term;
  988. if (checkText || checkChildren) {
  989. if (child) {
  990. return false;
  991. }
  992. callback(data);
  993. return;
  994. }
  995. }
  996. if (child) {
  997. return true;
  998. }
  999. var tag = self.createTag(params);
  1000. var $option = self.option(tag);
  1001. $option.attr('data-select2-tag', true);
  1002. self.$element.append($option);
  1003. self.insertTag(data, tag);
  1004. callback(data);
  1005. }
  1006. decorated.call(this, params, wrapper);
  1007. };
  1008. Tags.prototype.createTag = function (decorated, params) {
  1009. return {
  1010. id: params.term,
  1011. text: params.term
  1012. };
  1013. };
  1014. Tags.prototype.insertTag = function (_, data, tag) {
  1015. data.unshift(tag);
  1016. };
  1017. Tags.prototype._removeOldTags = function (_) {
  1018. var tag = this._lastTag;
  1019. var $options = this.$element.find('option[data-select2-tag]');
  1020. $options.each(function () {
  1021. if (this.selected) {
  1022. return;
  1023. }
  1024. $(this).remove();
  1025. });
  1026. };
  1027. return Tags;
  1028. });
  1029. define('select2/data/minimumInputLength',[
  1030. ], function () {
  1031. function MinimumInputLength (decorated, $e, options) {
  1032. this.minimumInputLength = options.get('minimumInputLength');
  1033. decorated.call(this, $e, options);
  1034. }
  1035. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1036. params.term = params.term || '';
  1037. if (params.term.length < this.minimumInputLength) {
  1038. this.trigger('results:message', {
  1039. message: 'inputTooShort',
  1040. args: {
  1041. minimum: this.minimumInputLength,
  1042. input: params.term,
  1043. params: params
  1044. }
  1045. });
  1046. return;
  1047. }
  1048. decorated.call(this, params, callback);
  1049. };
  1050. return MinimumInputLength;
  1051. });
  1052. define('select2/dropdown',[
  1053. './utils'
  1054. ], function (Utils) {
  1055. function Dropdown ($element, options) {
  1056. this.$element = $element;
  1057. }
  1058. Utils.Extend(Dropdown, Utils.Observable);
  1059. Dropdown.prototype.render = function () {
  1060. var $dropdown = $(
  1061. '<span class="dropdown">' +
  1062. '<span class="results"></span>' +
  1063. '</span>'
  1064. );
  1065. return $dropdown;
  1066. };
  1067. Dropdown.prototype.bind = function (container, $container) {
  1068. // Can be implemented in subclasses
  1069. };
  1070. return Dropdown;
  1071. });
  1072. define('select2/dropdown/search',[
  1073. '../utils'
  1074. ], function (Utils) {
  1075. function Search () { }
  1076. Search.prototype.render = function (decorated) {
  1077. var $rendered = decorated.call(this);
  1078. var $search = $(
  1079. '<span class="search">' +
  1080. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1081. '</span>'
  1082. );
  1083. this.$searchContainer = $search;
  1084. this.$search = $search.find('input');
  1085. $rendered.prepend($search);
  1086. return $rendered;
  1087. };
  1088. Search.prototype.bind = function (decorated, container, $container) {
  1089. var self = this;
  1090. decorated.call(this, container, $container);
  1091. this.$search.on('keydown', function (evt) {
  1092. self.trigger('keypress', evt);
  1093. self._keyUpPrevented = evt.isDefaultPrevented();
  1094. });
  1095. this.$search.on('keyup', function (evt) {
  1096. self.handleSearch(evt);
  1097. });
  1098. container.on('open', function () {
  1099. self.$search.attr('tabindex', 0);
  1100. });
  1101. container.on('close', function () {
  1102. self.$search.attr('tabindex', -1);
  1103. self.$search.val('');
  1104. });
  1105. container.on('results:all', function (params) {
  1106. if (params.query.term == null || params.query.term === '') {
  1107. var showSearch = self.showSearch(params);
  1108. if (showSearch) {
  1109. self.$searchContainer.show();
  1110. } else {
  1111. self.$searchContainer.hide();
  1112. }
  1113. }
  1114. });
  1115. };
  1116. Search.prototype.handleSearch = function (evt) {
  1117. if (!this._keyUpPrevented) {
  1118. var input = this.$search.val();
  1119. this.trigger('query', {
  1120. term: input
  1121. });
  1122. }
  1123. this._keyUpPrevented = false;
  1124. };
  1125. Search.prototype.showSearch = function (_, params) {
  1126. return true;
  1127. };
  1128. return Search;
  1129. });
  1130. define('select2/dropdown/hidePlaceholder',[
  1131. ], function () {
  1132. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1133. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1134. decorated.call(this, $element, options, dataAdapter);
  1135. }
  1136. HidePlaceholder.prototype.append = function (decorated, data) {
  1137. data = this.removePlaceholder(data);
  1138. decorated.call(this, data);
  1139. };
  1140. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1141. if (typeof placeholder === 'string') {
  1142. placeholder = {
  1143. id: '',
  1144. text: placeholder
  1145. };
  1146. }
  1147. return placeholder;
  1148. };
  1149. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1150. var modifiedData = data.slice(0);
  1151. for (var d = data.length - 1; d >= 0; d--) {
  1152. var item = data[d];
  1153. if (this.placeholder.id === item.id) {
  1154. modifiedData.splice(d, 1);
  1155. }
  1156. }
  1157. return modifiedData;
  1158. };
  1159. return HidePlaceholder;
  1160. });
  1161. define('select2/dropdown/infiniteScroll',[
  1162. 'jquery'
  1163. ], function ($) {
  1164. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  1165. this.lastParams = {};
  1166. decorated.call(this, $element, options, dataAdapter);
  1167. this.$loadingMore = this.createLoadingMore();
  1168. this.loading = false;
  1169. }
  1170. InfiniteScroll.prototype.append = function (decorated, data) {
  1171. this.$loadingMore.remove();
  1172. decorated.call(this, data);
  1173. if (data.length > 0) {
  1174. this.$results.append(this.$loadingMore);
  1175. }
  1176. this.loading = false;
  1177. };
  1178. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  1179. var self = this;
  1180. decorated.call(this, container, $container);
  1181. container.on('query', function (params) {
  1182. self.lastParams = params;
  1183. self.loading = true;
  1184. });
  1185. container.on('query:append', function (params) {
  1186. self.lastParams = params;
  1187. self.loading = true;
  1188. });
  1189. this.$results.on('scroll', function () {
  1190. var loadMoreVisible = $.contains(
  1191. document.documentElement,
  1192. self.$loadingMore[0]
  1193. );
  1194. if (self.loading || !loadMoreVisible) {
  1195. return;
  1196. }
  1197. var currentOffset = self.$results.offset().top +
  1198. self.$results.outerHeight(false);
  1199. var loadingMoreOffset = self.$loadingMore.offset().top +
  1200. self.$loadingMore.outerHeight(false);
  1201. if (currentOffset + 50 >= loadingMoreOffset) {
  1202. self.loadMore();
  1203. }
  1204. });
  1205. };
  1206. InfiniteScroll.prototype.loadMore = function () {
  1207. this.loading = true;
  1208. var params = $.extend({}, {page: 1}, this.lastParams);
  1209. params.page++;
  1210. this.trigger('query:append', params);
  1211. };
  1212. InfiniteScroll.prototype.createLoadingMore = function () {
  1213. var $option = $(
  1214. '<li class="option load-more" role="treeitem"></li>'
  1215. );
  1216. var message = this.options.get('translations').get('loadingMore');
  1217. $option.html(message(this.lastParams));
  1218. return $option;
  1219. };
  1220. return InfiniteScroll;
  1221. });
  1222. define('select2/i18n/en',[],function () {
  1223. return {
  1224. inputTooShort: function (args) {
  1225. var remainingChars = args.minimum - args.input.length;
  1226. var message = 'Please enter ' + remainingChars + ' or more character';
  1227. if (remainingChars != 1) {
  1228. message += 's';
  1229. }
  1230. return message;
  1231. },
  1232. loadingMore: function () {
  1233. return 'Loading more results…';
  1234. },
  1235. noResults: function () {
  1236. return 'No results found';
  1237. }
  1238. };
  1239. });
  1240. define('select2/defaults',[
  1241. 'jquery',
  1242. './results',
  1243. './selection/single',
  1244. './selection/multiple',
  1245. './selection/placeholder',
  1246. './utils',
  1247. './translation',
  1248. './data/select',
  1249. './data/array',
  1250. './data/ajax',
  1251. './data/tags',
  1252. './data/minimumInputLength',
  1253. './dropdown',
  1254. './dropdown/search',
  1255. './dropdown/hidePlaceholder',
  1256. './dropdown/infiniteScroll',
  1257. './i18n/en'
  1258. ], function ($, ResultsList,
  1259. SingleSelection, MultipleSelection, Placeholder,
  1260. Utils, Translation,
  1261. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1262. Dropdown, Search, HidePlaceholder, InfiniteScroll,
  1263. EnglishTranslation) {
  1264. function Defaults () {
  1265. this.reset();
  1266. }
  1267. Defaults.prototype.apply = function (options) {
  1268. options = $.extend({}, this.defaults, options);
  1269. if (options.dataAdapter == null) {
  1270. if (options.ajax != null) {
  1271. options.dataAdapter = AjaxData;
  1272. } else if (options.data != null) {
  1273. options.dataAdapter = ArrayData;
  1274. } else {
  1275. options.dataAdapter = SelectData;
  1276. }
  1277. }
  1278. if (options.minimumInputLength > 0) {
  1279. options.dataAdapter = Utils.Decorate(
  1280. options.dataAdapter,
  1281. MinimumInputLength
  1282. );
  1283. }
  1284. if (options.tags != null) {
  1285. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1286. }
  1287. if (options.resultsAdapter == null) {
  1288. options.resultsAdapter = ResultsList;
  1289. if (options.ajax != null) {
  1290. options.resultsAdapter = Utils.Decorate(
  1291. options.resultsAdapter,
  1292. InfiniteScroll
  1293. );
  1294. }
  1295. if (options.placeholder != null) {
  1296. options.resultsAdapter = Utils.Decorate(
  1297. options.resultsAdapter,
  1298. HidePlaceholder
  1299. );
  1300. }
  1301. }
  1302. if (options.dropdownAdapter == null) {
  1303. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1304. options.dropdownAdapter = SearchableDropdown;
  1305. }
  1306. if (options.selectionAdapter == null) {
  1307. if (options.multiple) {
  1308. options.selectionAdapter = MultipleSelection;
  1309. } else {
  1310. options.selectionAdapter = SingleSelection;
  1311. }
  1312. // Add the placeholder mixin if a placeholder was specified
  1313. if (options.placeholder != null) {
  1314. options.selectionAdapter = Utils.Decorate(
  1315. options.selectionAdapter,
  1316. Placeholder
  1317. );
  1318. }
  1319. }
  1320. if (typeof options.language === 'string') {
  1321. options.language = [options.language];
  1322. }
  1323. if ($.isArray(options.language)) {
  1324. var languages = new Translation();
  1325. var languageNames = options.language.concat(this.defaults.language);
  1326. for (var l = 0; l < languageNames.length; l++) {
  1327. var name = languageNames[l];
  1328. var language = {};
  1329. try {
  1330. // Try to load it with the original name
  1331. language = Translation.loadPath(name);
  1332. } catch (e) {
  1333. // If we couldn't load it, check if it wasn't the full path
  1334. name = 'select2/i18n/' + name;
  1335. language = Translation.loadPath(name);
  1336. }
  1337. languages.extend(language);
  1338. }
  1339. options.translations = languages;
  1340. } else {
  1341. options.translations = new Translations(options.language);
  1342. }
  1343. return options;
  1344. };
  1345. Defaults.prototype.reset = function () {
  1346. function matcher (params, data) {
  1347. // Always return the object if there is nothing to compare
  1348. if ($.trim(params.term) === '') {
  1349. return data;
  1350. }
  1351. // Do a recursive check for options with children
  1352. if (data.children && data.children.length > 0) {
  1353. // Clone the data object if there are children
  1354. // This is required as we modify the object to remove any non-matches
  1355. var match = $.extend(true, {}, data);
  1356. // Check each child of the option
  1357. for (var c = data.children.length - 1; c >= 0; c--) {
  1358. var child = data.children[c];
  1359. var matches = matcher(params, child);
  1360. // If there wasn't a match, remove the object in the array
  1361. if (matches == null) {
  1362. match.children.splice(c, 1);
  1363. }
  1364. }
  1365. // If any children matched, return the new object
  1366. if (match.children.length > 0) {
  1367. return match;
  1368. }
  1369. // If there were no matching children, check just the plain object
  1370. return matcher(params, match);
  1371. }
  1372. // Check if the text contains the term
  1373. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1374. return data;
  1375. }
  1376. // If it doesn't contain the term, don't return anything
  1377. return null;
  1378. }
  1379. this.defaults = {
  1380. language: ['select2/i18n/en'],
  1381. matcher: matcher,
  1382. minimumInputLength: 0,
  1383. templateResult: function (result) {
  1384. return result.text;
  1385. },
  1386. templateSelection: function (selection) {
  1387. return selection.text;
  1388. }
  1389. };
  1390. };
  1391. var defaults = new Defaults();
  1392. return defaults;
  1393. });
  1394. define('select2/options',[
  1395. './defaults'
  1396. ], function (Defaults) {
  1397. function Options (options, $element) {
  1398. this.options = options;
  1399. if ($element != null) {
  1400. this.fromElement($element);
  1401. }
  1402. this.options = Defaults.apply(this.options);
  1403. }
  1404. Options.prototype.fromElement = function ($e) {
  1405. if (this.options.multiple == null) {
  1406. this.options.multiple = $e.prop('multiple');
  1407. }
  1408. return this;
  1409. };
  1410. Options.prototype.get = function (key) {
  1411. return this.options[key];
  1412. };
  1413. Options.prototype.set = function (key, val) {
  1414. this.options[key] = val;
  1415. };
  1416. return Options;
  1417. });
  1418. define('select2/core',[
  1419. 'jquery',
  1420. './options',
  1421. './utils',
  1422. './keys'
  1423. ], function ($, Options, Utils, KEYS) {
  1424. var Select2 = function ($element, options) {
  1425. this.$element = $element;
  1426. this.id = this._generateId($element);
  1427. options = options || {};
  1428. this.options = new Options(options, $element);
  1429. Select2.__super__.constructor.call(this);
  1430. // Set up containers and adapters
  1431. var DataAdapter = this.options.get('dataAdapter');
  1432. this.data = new DataAdapter($element, this.options);
  1433. var $container = this.render();
  1434. this._placeContainer($container);
  1435. var SelectionAdapter = this.options.get('selectionAdapter');
  1436. this.selection = new SelectionAdapter($element, this.options);
  1437. var $selection = this.selection.render();
  1438. this._placeSelection($selection);
  1439. var DropdownAdapter = this.options.get('dropdownAdapter');
  1440. this.dropdown = new DropdownAdapter($element, this.options);
  1441. var $dropdown = this.dropdown.render();
  1442. this._placeDropdown($dropdown);
  1443. var ResultsAdapter = this.options.get('resultsAdapter');
  1444. this.results = new ResultsAdapter($element, this.options, this.data);
  1445. var $results = this.results.render();
  1446. this._placeResults($results);
  1447. // Bind events
  1448. var self = this;
  1449. // Bind the container to all of the adapters
  1450. this._bindAdapters();
  1451. // Register any DOM event handlers
  1452. this._registerDomEvents();
  1453. // Register any internal event handlers
  1454. this._registerDataEvents();
  1455. this._registerSelectionEvents();
  1456. this._registerDropdownEvents();
  1457. this._registerResultsEvents();
  1458. this._registerEvents();
  1459. // Set the initial state
  1460. this.data.current(function (initialData) {
  1461. self.trigger('selection:update', {
  1462. data: initialData
  1463. });
  1464. });
  1465. // Hide the original select
  1466. $element.hide();
  1467. $element.attr('tabindex', '-1');
  1468. $element.data('select2', this);
  1469. };
  1470. Utils.Extend(Select2, Utils.Observable);
  1471. Select2.prototype._generateId = function ($element) {
  1472. var id = '';
  1473. if ($element.attr('id') != null) {
  1474. id = $element.attr('id');
  1475. } else if ($element.attr('name') != null) {
  1476. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1477. } else {
  1478. id = Utils.generateChars(4);
  1479. }
  1480. id = 'select2-' + id;
  1481. return id;
  1482. };
  1483. Select2.prototype._placeContainer = function ($container) {
  1484. $container.insertAfter(this.$element);
  1485. $container.width(this.$element.outerWidth(false));
  1486. };
  1487. Select2.prototype._placeSelection = function ($selection) {
  1488. var $selectionContainer = this.$container.find('.selection');
  1489. $selectionContainer.append($selection);
  1490. };
  1491. Select2.prototype._placeDropdown = function ($dropdown) {
  1492. this.$dropdown = $dropdown;
  1493. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1494. $dropdownContainer.append($dropdown);
  1495. };
  1496. Select2.prototype._placeResults = function ($results) {
  1497. var $resultsContainer = this.$dropdown.find('.results');
  1498. $resultsContainer.append($results);
  1499. };
  1500. Select2.prototype._bindAdapters = function () {
  1501. this.data.bind(this, this.$container);
  1502. this.selection.bind(this, this.$container);
  1503. this.dropdown.bind(this, this.$container);
  1504. this.results.bind(this, this.$container);
  1505. };
  1506. Select2.prototype._registerDomEvents = function () {
  1507. var self = this;
  1508. this.$element.on('change', function () {
  1509. self.data.current(function (data) {
  1510. self.trigger('selection:update', {
  1511. data: data
  1512. });
  1513. });
  1514. });
  1515. };
  1516. Select2.prototype._registerDataEvents = function () {
  1517. var self = this;
  1518. this.data.on('results:message', function (params) {
  1519. self.trigger('results:message', params);
  1520. });
  1521. };
  1522. Select2.prototype._registerSelectionEvents = function () {
  1523. var self = this;
  1524. this.selection.on('open', function () {
  1525. self.open();
  1526. });
  1527. this.selection.on('close', function () {
  1528. self.close();
  1529. });
  1530. this.selection.on('toggle', function () {
  1531. self.toggleDropdown();
  1532. });
  1533. this.selection.on('results:select', function () {
  1534. self.trigger('results:select');
  1535. });
  1536. this.selection.on('results:previous', function () {
  1537. self.trigger('results:previous');
  1538. });
  1539. this.selection.on('results:next', function () {
  1540. self.trigger('results:next');
  1541. });
  1542. this.selection.on('unselected', function (params) {
  1543. self.trigger('unselect', params);
  1544. self.close();
  1545. });
  1546. this.selection.on('keypress', function (e) {
  1547. self.trigger('keypress', e);
  1548. });
  1549. };
  1550. Select2.prototype._registerDropdownEvents = function () {
  1551. var self = this;
  1552. this.dropdown.on('query', function (params) {
  1553. self.trigger('query', params);
  1554. });
  1555. this.dropdown.on('keypress', function (e) {
  1556. self.trigger('keypress', e);
  1557. });
  1558. };
  1559. Select2.prototype._registerResultsEvents = function () {
  1560. var self = this;
  1561. this.results.on('query:append', function (params) {
  1562. self.trigger('query:append', params);
  1563. });
  1564. this.results.on('selected', function (params) {
  1565. self.trigger('select', params);
  1566. self.close();
  1567. });
  1568. this.results.on('unselected', function (params) {
  1569. self.trigger('unselect', params);
  1570. self.close();
  1571. });
  1572. this.results.on('results:focus', function (params) {
  1573. self.trigger('results:focus', params);
  1574. });
  1575. };
  1576. Select2.prototype._registerEvents = function () {
  1577. var self = this;
  1578. this.on('open', function () {
  1579. self.$container.addClass('open');
  1580. });
  1581. this.on('close', function () {
  1582. self.$container.removeClass('open');
  1583. });
  1584. this.on('query', function (params) {
  1585. this.data.query(params, function (data) {
  1586. self.trigger('results:all', {
  1587. data: data,
  1588. query: params
  1589. });
  1590. });
  1591. });
  1592. this.on('query:append', function (params) {
  1593. this.data.query(params, function (data) {
  1594. self.trigger('results:append', {
  1595. data: data,
  1596. query: params
  1597. });
  1598. });
  1599. });
  1600. this.on('keypress', function (evt) {
  1601. var key = evt.which;
  1602. if (self.isOpen()) {
  1603. if (key === KEYS.ENTER) {
  1604. self.trigger('results:select');
  1605. evt.preventDefault();
  1606. } else if (key === KEYS.UP) {
  1607. self.trigger('results:previous');
  1608. evt.preventDefault();
  1609. } else if (key === KEYS.DOWN) {
  1610. self.trigger('results:next');
  1611. evt.preventDefault();
  1612. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1613. self.close();
  1614. evt.preventDefault();
  1615. }
  1616. } else {
  1617. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1618. self.open();
  1619. evt.preventDefault();
  1620. }
  1621. }
  1622. });
  1623. };
  1624. Select2.prototype.toggleDropdown = function () {
  1625. if (this.isOpen()) {
  1626. this.close();
  1627. } else {
  1628. this.open();
  1629. }
  1630. };
  1631. Select2.prototype.open = function () {
  1632. if (this.isOpen()) {
  1633. return;
  1634. }
  1635. this.trigger('query', {});
  1636. this.trigger('open');
  1637. };
  1638. Select2.prototype.close = function () {
  1639. if (!this.isOpen()) {
  1640. return;
  1641. }
  1642. this.trigger('close');
  1643. };
  1644. Select2.prototype.isOpen = function () {
  1645. return this.$container.hasClass('open');
  1646. };
  1647. Select2.prototype.render = function () {
  1648. var $container = $(
  1649. '<span class="select2 select2-container select2-theme-default">' +
  1650. '<span class="selection"></span>' +
  1651. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1652. '</span>'
  1653. );
  1654. this.$container = $container;
  1655. $container.data('element', this.$element);
  1656. return $container;
  1657. };
  1658. return Select2;
  1659. });
  1660. define('select2/compat/matcher',[
  1661. ], function () {
  1662. function oldMatcher (matcher) {
  1663. function wrappedMatcher (params, data) {
  1664. var match = $.extend(true, {}, data);
  1665. if (params.term == null || $.trim(params.term) === '') {
  1666. return match;
  1667. }
  1668. if (data.children) {
  1669. for (var c = data.children.length - 1; c >= 0; c--) {
  1670. var child = data.children[c];
  1671. // Check if the child object matches
  1672. // The old matcher returned a boolean true or false
  1673. var doesMatch = matcher(params.term, child.text, child);
  1674. // If the child didn't match, pop it off
  1675. if (!doesMatch) {
  1676. match.children.splice(c, 1);
  1677. }
  1678. }
  1679. if (match.children.length > 0) {
  1680. return match;
  1681. }
  1682. }
  1683. if (matcher(params.term, data.text, data)) {
  1684. return match;
  1685. }
  1686. return null;
  1687. }
  1688. return wrappedMatcher;
  1689. }
  1690. return oldMatcher;
  1691. });
  1692. define('jquery.select2',[
  1693. 'jquery',
  1694. 'select2/core'
  1695. ], function ($, Select2) {
  1696. if ($.fn.select2 == null) {
  1697. $.fn.select2 = function (options) {
  1698. options = options || {};
  1699. if (typeof options === 'object') {
  1700. this.each(function () {
  1701. var instance = new Select2($(this), options);
  1702. });
  1703. } else if (typeof options === 'string') {
  1704. var instance = this.data('select2');
  1705. var args = Array.prototype.slice.call(arguments, 1);
  1706. instance[options](args);
  1707. } else {
  1708. throw new Error('Invalid arguments for Select2: ' + options);
  1709. }
  1710. };
  1711. }
  1712. return Select2;
  1713. });
  1714. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());