select2.amd.full.js 51 KB

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