select2.amd.full.js 55 KB

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