select2.amd.js 54 KB

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