select2.amd.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  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. SelectAdapter.__super__.constructor.call(this);
  715. }
  716. Utils.Extend(SelectAdapter, BaseAdapter);
  717. SelectAdapter.prototype.current = function (callback) {
  718. var data = [];
  719. var self = this;
  720. this.$element.find(':selected').each(function () {
  721. var $option = $(this);
  722. var option = self.item($option);
  723. data.push(option);
  724. });
  725. callback(data);
  726. };
  727. SelectAdapter.prototype.select = function (data) {
  728. var self = this;
  729. if (this.$element.prop('multiple')) {
  730. this.current(function (currentData) {
  731. var val = [];
  732. data = [data];
  733. data.push.apply(data, currentData);
  734. for (var d = 0; d < data.length; d++) {
  735. id = data[d].id;
  736. if (val.indexOf(id) === -1) {
  737. val.push(id);
  738. }
  739. }
  740. self.$element.val(val);
  741. self.$element.trigger('change');
  742. });
  743. } else {
  744. var val = data.id;
  745. this.$element.val(val);
  746. this.$element.trigger('change');
  747. }
  748. };
  749. SelectAdapter.prototype.unselect = function (data) {
  750. var self = this;
  751. if (!this.$element.prop('multiple')) {
  752. return;
  753. }
  754. this.current(function (currentData) {
  755. var val = [];
  756. for (var d = 0; d < currentData.length; d++) {
  757. id = currentData[d].id;
  758. if (id !== data.id && val.indexOf(id) === -1) {
  759. val.push(id);
  760. }
  761. }
  762. self.$element.val(val);
  763. self.$element.trigger('change');
  764. });
  765. };
  766. SelectAdapter.prototype.bind = function (container, $container) {
  767. var self = this;
  768. this.container = container;
  769. container.on('select', function (params) {
  770. self.select(params.data);
  771. });
  772. container.on('unselect', function (params) {
  773. self.unselect(params.data);
  774. });
  775. };
  776. SelectAdapter.prototype.query = function (params, callback) {
  777. var data = [];
  778. var self = this;
  779. var $options = this.$element.children();
  780. $options.each(function () {
  781. var $option = $(this);
  782. if (!$option.is('option') && !$option.is('optgroup')) {
  783. return;
  784. }
  785. var option = self.item($option);
  786. var matches = self.matches(params, option);
  787. if (matches !== null) {
  788. data.push(matches);
  789. }
  790. });
  791. callback(data);
  792. };
  793. SelectAdapter.prototype.option = function (data) {
  794. var $option = $('<option></option>');
  795. $option.text(data.text);
  796. $option.val(data.id);
  797. $option.prop('disabled', data.disabled || false);
  798. // Get any automatically generated data values
  799. var detectedData = this.item($option);
  800. // Merge it with the already present data
  801. var combinedData = $.extend({}, data, detectedData);
  802. // Override the option's data with the combined data
  803. $option.data('data', combinedData);
  804. return $option;
  805. };
  806. SelectAdapter.prototype.item = function ($option) {
  807. var data = $option.data('data');
  808. // If the data has already be generated, use it
  809. if (data == null) {
  810. if ($option.is('option')) {
  811. data = {
  812. id: $option.val(),
  813. text: $option.html(),
  814. disabled: $option.prop('disabled')
  815. };
  816. } else if ($option.is('optgroup')) {
  817. data = {
  818. text: $option.attr('label'),
  819. children: []
  820. };
  821. var $children = $option.children('option');
  822. var children = [];
  823. for (var c = 0; c < $children.length; c++) {
  824. var $child = $($children[c]);
  825. var child = this.item($child);
  826. children.push(child);
  827. }
  828. data.children = children;
  829. }
  830. if (data.id && this.container != null) {
  831. data._resultId = this.generateResultId(this.container, data);
  832. }
  833. $option.data('data', data);
  834. }
  835. return data;
  836. };
  837. SelectAdapter.prototype.matches = function (params, data) {
  838. var match = $.extend(true, {}, data);
  839. if (data.children) {
  840. for (var c = data.children.length - 1; c >= 0; c--) {
  841. var child = data.children[c];
  842. var matches = this.matches(params, child);
  843. // If there wasn't a match, remove the object in the array
  844. if (matches === null) {
  845. match.children.splice(c, 1);
  846. }
  847. }
  848. if (match.children.length > 0) {
  849. return match;
  850. }
  851. }
  852. if ($.trim(params.term) === '') {
  853. return match;
  854. }
  855. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  856. return match;
  857. }
  858. return null;
  859. };
  860. return SelectAdapter;
  861. });
  862. define('select2/data/array',[
  863. './select',
  864. '../utils',
  865. 'jquery'
  866. ], function (SelectAdapter, Utils, $) {
  867. function ArrayAdapter ($element, options) {
  868. this.data = options.get('data');
  869. ArrayAdapter.__super__.constructor.call(this, $element, options);
  870. }
  871. Utils.Extend(ArrayAdapter, SelectAdapter);
  872. ArrayAdapter.prototype.select = function (data) {
  873. var self = this;
  874. this.$element.find('option').each(function () {
  875. var $option = $(this);
  876. var option = self.item($option);
  877. if (option.id == data.id.toString()) {
  878. $option.remove();
  879. }
  880. });
  881. var $option = this.option(data);
  882. this.$element.append($option);
  883. ArrayAdapter.__super__.select.call(this, data);
  884. };
  885. ArrayAdapter.prototype.query = function (params, callback) {
  886. var matches = [];
  887. var self = this;
  888. $.each(this.data, function () {
  889. var option = this;
  890. if (self.matches(params, option)) {
  891. matches.push(option);
  892. }
  893. });
  894. callback(matches);
  895. };
  896. return ArrayAdapter;
  897. });
  898. define('select2/data/ajax',[
  899. './array',
  900. '../utils',
  901. 'jquery'
  902. ], function (ArrayAdapter, Utils, $) {
  903. function AjaxAdapter ($element, options) {
  904. this.ajaxOptions = options.get('ajax');
  905. if (this.ajaxOptions.processResults != null) {
  906. this.processResults = this.ajaxOptions.processResults;
  907. }
  908. ArrayAdapter.__super__.constructor.call(this, $element, options);
  909. }
  910. Utils.Extend(AjaxAdapter, ArrayAdapter);
  911. AjaxAdapter.prototype.processResults = function (results) {
  912. return results;
  913. };
  914. AjaxAdapter.prototype.query = function (params, callback) {
  915. var matches = [];
  916. var self = this;
  917. var options = $.extend({
  918. type: 'GET'
  919. }, this.ajaxOptions);
  920. if (typeof options.url === 'function') {
  921. options.url = options.url(params);
  922. }
  923. if (typeof options.data === 'function') {
  924. options.data = options.data(params);
  925. }
  926. function request () {
  927. var $request = $.ajax(options);
  928. $request.success(function (data) {
  929. var results = self.processResults(data);
  930. callback(results);
  931. });
  932. }
  933. if (this.ajaxOptions.delay && params.term !== '') {
  934. if (this._queryTimeout) {
  935. window.clearTimeout(this._queryTimeout);
  936. }
  937. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  938. } else {
  939. request();
  940. }
  941. };
  942. return AjaxAdapter;
  943. });
  944. define('select2/data/tags',[
  945. ], function () {
  946. function Tags (decorated, $element, options) {
  947. var tags = options.get('tags');
  948. decorated.call(this, $element, options);
  949. }
  950. Tags.prototype.query = function (decorated, params, callback) {
  951. var self = this;
  952. this._removeOldTags();
  953. if (params.term == null || params.term === '' || params.page != null) {
  954. decorated.call(this, params, callback);
  955. return;
  956. }
  957. function wrapper (data, child) {
  958. for (var i = 0; i < data.length; i++) {
  959. var option = data[i];
  960. var checkChildren = (
  961. option.children != null && !wrapper(option.children, true)
  962. );
  963. var checkText = option.text === params.term;
  964. if (checkText || checkChildren) {
  965. if (child) {
  966. return false;
  967. }
  968. callback(data);
  969. return;
  970. }
  971. }
  972. if (child) {
  973. return true;
  974. }
  975. var tag = self.createTag(params);
  976. var $option = self.option(tag);
  977. $option.attr('data-select2-tag', true);
  978. self.$element.append($option);
  979. self.insertTag(data, tag);
  980. callback(data);
  981. }
  982. decorated.call(this, params, wrapper);
  983. };
  984. Tags.prototype.createTag = function (decorated, params) {
  985. return {
  986. id: params.term,
  987. text: params.term
  988. };
  989. };
  990. Tags.prototype.insertTag = function (_, data, tag) {
  991. data.unshift(tag);
  992. };
  993. Tags.prototype._removeOldTags = function (_) {
  994. var tag = this._lastTag;
  995. var $options = this.$element.find('option[data-select2-tag]');
  996. $options.each(function () {
  997. if (this.selected) {
  998. return;
  999. }
  1000. $(this).remove();
  1001. });
  1002. };
  1003. return Tags;
  1004. });
  1005. define('select2/data/minimumInputLength',[
  1006. ], function () {
  1007. function MinimumInputLength (decorated, $e, options) {
  1008. this.minimumInputLength = options.get('minimumInputLength');
  1009. decorated.call(this, $e, options);
  1010. }
  1011. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1012. params.term = params.term || '';
  1013. if (params.term.length < this.minimumInputLength) {
  1014. this.trigger('results:message', {
  1015. message: 'inputTooShort',
  1016. args: {
  1017. minimum: this.minimumInputLength,
  1018. input: params.term,
  1019. params: params
  1020. }
  1021. });
  1022. return;
  1023. }
  1024. decorated.call(this, params, callback);
  1025. };
  1026. return MinimumInputLength;
  1027. });
  1028. define('select2/dropdown',[
  1029. './utils'
  1030. ], function (Utils) {
  1031. function Dropdown ($element, options) {
  1032. this.$element = $element;
  1033. }
  1034. Utils.Extend(Dropdown, Utils.Observable);
  1035. Dropdown.prototype.render = function () {
  1036. var $dropdown = $(
  1037. '<span class="dropdown">' +
  1038. '<span class="results"></span>' +
  1039. '</span>'
  1040. );
  1041. return $dropdown;
  1042. };
  1043. Dropdown.prototype.bind = function (container, $container) {
  1044. // Can be implemented in subclasses
  1045. };
  1046. return Dropdown;
  1047. });
  1048. define('select2/dropdown/search',[
  1049. '../utils'
  1050. ], function (Utils) {
  1051. function Search () { }
  1052. Search.prototype.render = function (decorated) {
  1053. var $rendered = decorated.call(this);
  1054. var $search = $(
  1055. '<span class="search">' +
  1056. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1057. '</span>'
  1058. );
  1059. this.$searchContainer = $search;
  1060. this.$search = $search.find('input');
  1061. $rendered.prepend($search);
  1062. return $rendered;
  1063. };
  1064. Search.prototype.bind = function (decorated, container, $container) {
  1065. var self = this;
  1066. decorated.call(this, container, $container);
  1067. this.$search.on('keydown', function (evt) {
  1068. self.trigger('keypress', evt);
  1069. self._keyUpPrevented = evt.isDefaultPrevented();
  1070. });
  1071. this.$search.on('keyup', function (evt) {
  1072. self.handleSearch(evt);
  1073. });
  1074. container.on('open', function () {
  1075. self.$search.attr('tabindex', 0);
  1076. });
  1077. container.on('close', function () {
  1078. self.$search.attr('tabindex', -1);
  1079. self.$search.val('');
  1080. });
  1081. container.on('results:all', function (params) {
  1082. if (params.query.term == null || params.query.term === '') {
  1083. var showSearch = self.showSearch(params);
  1084. if (showSearch) {
  1085. self.$searchContainer.show();
  1086. } else {
  1087. self.$searchContainer.hide();
  1088. }
  1089. }
  1090. });
  1091. };
  1092. Search.prototype.handleSearch = function (evt) {
  1093. if (!this._keyUpPrevented) {
  1094. var input = this.$search.val();
  1095. this.trigger('query', {
  1096. term: input
  1097. });
  1098. }
  1099. this._keyUpPrevented = false;
  1100. };
  1101. Search.prototype.showSearch = function (_, params) {
  1102. return true;
  1103. };
  1104. return Search;
  1105. });
  1106. define('select2/dropdown/hidePlaceholder',[
  1107. ], function () {
  1108. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1109. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1110. decorated.call(this, $element, options, dataAdapter);
  1111. }
  1112. HidePlaceholder.prototype.append = function (decorated, data) {
  1113. data = this.removePlaceholder(data);
  1114. decorated.call(this, data);
  1115. };
  1116. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1117. if (typeof placeholder === 'string') {
  1118. placeholder = {
  1119. id: '',
  1120. text: placeholder
  1121. };
  1122. }
  1123. return placeholder;
  1124. };
  1125. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1126. var modifiedData = data.slice(0);
  1127. for (var d = data.length - 1; d >= 0; d--) {
  1128. var item = data[d];
  1129. if (this.placeholder.id === item.id) {
  1130. modifiedData.splice(d, 1);
  1131. }
  1132. }
  1133. return modifiedData;
  1134. };
  1135. return HidePlaceholder;
  1136. });
  1137. define('select2/dropdown/infiniteScroll',[
  1138. 'jquery'
  1139. ], function ($) {
  1140. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  1141. this.lastParams = {};
  1142. decorated.call(this, $element, options, dataAdapter);
  1143. this.$loadingMore = this.createLoadingMore();
  1144. this.loading = false;
  1145. }
  1146. InfiniteScroll.prototype.append = function (decorated, data) {
  1147. this.$loadingMore.remove();
  1148. decorated.call(this, data);
  1149. if (data.length > 0) {
  1150. this.$results.append(this.$loadingMore);
  1151. }
  1152. this.loading = false;
  1153. };
  1154. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  1155. var self = this;
  1156. decorated.call(this, container, $container);
  1157. container.on('query', function (params) {
  1158. self.lastParams = params;
  1159. self.loading = true;
  1160. });
  1161. container.on('query:append', function (params) {
  1162. self.lastParams = params;
  1163. self.loading = true;
  1164. });
  1165. this.$results.on('scroll', function () {
  1166. var loadMoreVisible = $.contains(
  1167. document.documentElement,
  1168. self.$loadingMore[0]
  1169. );
  1170. if (self.loading || !loadMoreVisible) {
  1171. return;
  1172. }
  1173. var currentOffset = self.$results.offset().top +
  1174. self.$results.outerHeight(false);
  1175. var loadingMoreOffset = self.$loadingMore.offset().top +
  1176. self.$loadingMore.outerHeight(false);
  1177. if (currentOffset + 50 >= loadingMoreOffset) {
  1178. self.loadMore();
  1179. }
  1180. });
  1181. };
  1182. InfiniteScroll.prototype.loadMore = function () {
  1183. this.loading = true;
  1184. var params = $.extend({}, {page: 1}, this.lastParams);
  1185. params.page++;
  1186. this.trigger('query:append', params);
  1187. };
  1188. InfiniteScroll.prototype.createLoadingMore = function () {
  1189. var $option = $(
  1190. '<li class="option load-more" role="treeitem"></li>'
  1191. );
  1192. var message = this.options.get('translations').get('loadingMore');
  1193. $option.html(message(this.lastParams));
  1194. return $option;
  1195. };
  1196. return InfiniteScroll;
  1197. });
  1198. define('select2/i18n/en',[],function () {
  1199. return {
  1200. inputTooShort: function (args) {
  1201. var remainingChars = args.minimum - args.input.length;
  1202. var message = 'Please enter ' + remainingChars + ' or more character';
  1203. if (remainingChars != 1) {
  1204. message += 's';
  1205. }
  1206. return message;
  1207. },
  1208. loadingMore: function () {
  1209. return 'Loading more results…';
  1210. },
  1211. noResults: function () {
  1212. return 'No results found';
  1213. }
  1214. };
  1215. });
  1216. define('select2/defaults',[
  1217. 'jquery',
  1218. './results',
  1219. './selection/single',
  1220. './selection/multiple',
  1221. './selection/placeholder',
  1222. './utils',
  1223. './translation',
  1224. './data/select',
  1225. './data/array',
  1226. './data/ajax',
  1227. './data/tags',
  1228. './data/minimumInputLength',
  1229. './dropdown',
  1230. './dropdown/search',
  1231. './dropdown/hidePlaceholder',
  1232. './dropdown/infiniteScroll',
  1233. './i18n/en'
  1234. ], function ($, ResultsList,
  1235. SingleSelection, MultipleSelection, Placeholder,
  1236. Utils, Translation,
  1237. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1238. Dropdown, Search, HidePlaceholder, InfiniteScroll,
  1239. EnglishTranslation) {
  1240. function Defaults () {
  1241. this.reset();
  1242. }
  1243. Defaults.prototype.apply = function (options) {
  1244. options = $.extend({}, this.defaults, options);
  1245. if (options.dataAdapter == null) {
  1246. if (options.ajax != null) {
  1247. options.dataAdapter = AjaxData;
  1248. } else if (options.data != null) {
  1249. options.dataAdapter = ArrayData;
  1250. } else {
  1251. options.dataAdapter = SelectData;
  1252. }
  1253. }
  1254. if (options.minimumInputLength > 0) {
  1255. options.dataAdapter = Utils.Decorate(
  1256. options.dataAdapter,
  1257. MinimumInputLength
  1258. );
  1259. }
  1260. if (options.tags != null) {
  1261. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1262. }
  1263. if (options.resultsAdapter == null) {
  1264. options.resultsAdapter = ResultsList;
  1265. if (options.ajax != null) {
  1266. options.resultsAdapter = Utils.Decorate(
  1267. options.resultsAdapter,
  1268. InfiniteScroll
  1269. );
  1270. }
  1271. if (options.placeholder != null) {
  1272. options.resultsAdapter = Utils.Decorate(
  1273. options.resultsAdapter,
  1274. HidePlaceholder
  1275. );
  1276. }
  1277. }
  1278. if (options.dropdownAdapter == null) {
  1279. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1280. options.dropdownAdapter = SearchableDropdown;
  1281. }
  1282. if (options.selectionAdapter == null) {
  1283. if (options.multiple) {
  1284. options.selectionAdapter = MultipleSelection;
  1285. } else {
  1286. options.selectionAdapter = SingleSelection;
  1287. }
  1288. // Add the placeholder mixin if a placeholder was specified
  1289. if (options.placeholder != null) {
  1290. options.selectionAdapter = Utils.Decorate(
  1291. options.selectionAdapter,
  1292. Placeholder
  1293. );
  1294. }
  1295. }
  1296. if (typeof options.language === 'string') {
  1297. options.language = [options.language];
  1298. }
  1299. if ($.isArray(options.language)) {
  1300. var languages = new Translation();
  1301. var languageNames = options.language.concat(this.defaults.language);
  1302. for (var l = 0; l < languageNames.length; l++) {
  1303. var name = languageNames[l];
  1304. var language = {};
  1305. try {
  1306. // Try to load it with the original name
  1307. language = Translation.loadPath(name);
  1308. } catch (e) {
  1309. // If we couldn't load it, check if it wasn't the full path
  1310. name = 'select2/i18n/' + name;
  1311. language = Translation.loadPath(name);
  1312. }
  1313. languages.extend(language);
  1314. }
  1315. options.translations = languages;
  1316. } else {
  1317. options.translations = new Translations(options.language);
  1318. }
  1319. return options;
  1320. };
  1321. Defaults.prototype.reset = function () {
  1322. this.defaults = {
  1323. language: ['select2/i18n/en'],
  1324. minimumInputLength: 0,
  1325. templateResult: function (result) {
  1326. return result.text;
  1327. },
  1328. templateSelection: function (selection) {
  1329. return selection.text;
  1330. }
  1331. };
  1332. };
  1333. var defaults = new Defaults();
  1334. return defaults;
  1335. });
  1336. define('select2/options',[
  1337. './defaults'
  1338. ], function (Defaults) {
  1339. function Options (options, $element) {
  1340. this.options = options;
  1341. if ($element != null) {
  1342. this.fromElement($element);
  1343. }
  1344. this.options = Defaults.apply(this.options);
  1345. }
  1346. Options.prototype.fromElement = function ($e) {
  1347. if (this.options.multiple == null) {
  1348. this.options.multiple = $e.prop('multiple');
  1349. }
  1350. return this;
  1351. };
  1352. Options.prototype.get = function (key) {
  1353. return this.options[key];
  1354. };
  1355. Options.prototype.set = function (key, val) {
  1356. this.options[key] = val;
  1357. };
  1358. return Options;
  1359. });
  1360. define('select2/core',[
  1361. 'jquery',
  1362. './options',
  1363. './utils',
  1364. './keys'
  1365. ], function ($, Options, Utils, KEYS) {
  1366. var Select2 = function ($element, options) {
  1367. this.$element = $element;
  1368. this.id = this._generateId($element);
  1369. options = options || {};
  1370. this.options = new Options(options, $element);
  1371. Select2.__super__.constructor.call(this);
  1372. // Set up containers and adapters
  1373. var DataAdapter = this.options.get('dataAdapter');
  1374. this.data = new DataAdapter($element, this.options);
  1375. var $container = this.render();
  1376. this._placeContainer($container);
  1377. var SelectionAdapter = this.options.get('selectionAdapter');
  1378. this.selection = new SelectionAdapter($element, this.options);
  1379. var $selection = this.selection.render();
  1380. this._placeSelection($selection);
  1381. var DropdownAdapter = this.options.get('dropdownAdapter');
  1382. this.dropdown = new DropdownAdapter($element, this.options);
  1383. var $dropdown = this.dropdown.render();
  1384. this._placeDropdown($dropdown);
  1385. var ResultsAdapter = this.options.get('resultsAdapter');
  1386. this.results = new ResultsAdapter($element, this.options, this.data);
  1387. var $results = this.results.render();
  1388. this._placeResults($results);
  1389. // Bind events
  1390. var self = this;
  1391. // Bind the container to all of the adapters
  1392. this._bindAdapters();
  1393. // Register any DOM event handlers
  1394. this._registerDomEvents();
  1395. // Register any internal event handlers
  1396. this._registerDataEvents();
  1397. this._registerSelectionEvents();
  1398. this._registerDropdownEvents();
  1399. this._registerResultsEvents();
  1400. this._registerEvents();
  1401. // Set the initial state
  1402. this.data.current(function (initialData) {
  1403. self.trigger('selection:update', {
  1404. data: initialData
  1405. });
  1406. });
  1407. // Hide the original select
  1408. $element.hide();
  1409. $element.attr('tabindex', '-1');
  1410. $element.data('select2', this);
  1411. };
  1412. Utils.Extend(Select2, Utils.Observable);
  1413. Select2.prototype._generateId = function ($element) {
  1414. var id = '';
  1415. if ($element.attr('id') != null) {
  1416. id = $element.attr('id');
  1417. } else if ($element.attr('name') != null) {
  1418. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1419. } else {
  1420. id = Utils.generateChars(4);
  1421. }
  1422. id = 'select2-' + id;
  1423. return id;
  1424. };
  1425. Select2.prototype._placeContainer = function ($container) {
  1426. $container.insertAfter(this.$element);
  1427. $container.width(this.$element.outerWidth(false));
  1428. };
  1429. Select2.prototype._placeSelection = function ($selection) {
  1430. var $selectionContainer = this.$container.find('.selection');
  1431. $selectionContainer.append($selection);
  1432. };
  1433. Select2.prototype._placeDropdown = function ($dropdown) {
  1434. this.$dropdown = $dropdown;
  1435. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1436. $dropdownContainer.append($dropdown);
  1437. };
  1438. Select2.prototype._placeResults = function ($results) {
  1439. var $resultsContainer = this.$dropdown.find('.results');
  1440. $resultsContainer.append($results);
  1441. };
  1442. Select2.prototype._bindAdapters = function () {
  1443. this.data.bind(this, this.$container);
  1444. this.selection.bind(this, this.$container);
  1445. this.dropdown.bind(this, this.$container);
  1446. this.results.bind(this, this.$container);
  1447. };
  1448. Select2.prototype._registerDomEvents = function () {
  1449. var self = this;
  1450. this.$element.on('change', function () {
  1451. self.data.current(function (data) {
  1452. self.trigger('selection:update', {
  1453. data: data
  1454. });
  1455. });
  1456. });
  1457. };
  1458. Select2.prototype._registerDataEvents = function () {
  1459. var self = this;
  1460. this.data.on('results:message', function (params) {
  1461. self.trigger('results:message', params);
  1462. });
  1463. };
  1464. Select2.prototype._registerSelectionEvents = function () {
  1465. var self = this;
  1466. this.selection.on('open', function () {
  1467. self.open();
  1468. });
  1469. this.selection.on('close', function () {
  1470. self.close();
  1471. });
  1472. this.selection.on('toggle', function () {
  1473. self.toggleDropdown();
  1474. });
  1475. this.selection.on('results:select', function () {
  1476. self.trigger('results:select');
  1477. });
  1478. this.selection.on('results:previous', function () {
  1479. self.trigger('results:previous');
  1480. });
  1481. this.selection.on('results:next', function () {
  1482. self.trigger('results:next');
  1483. });
  1484. this.selection.on('unselected', function (params) {
  1485. self.trigger('unselect', params);
  1486. self.close();
  1487. });
  1488. this.selection.on('keypress', function (e) {
  1489. self.trigger('keypress', e);
  1490. });
  1491. };
  1492. Select2.prototype._registerDropdownEvents = function () {
  1493. var self = this;
  1494. this.dropdown.on('query', function (params) {
  1495. self.trigger('query', params);
  1496. });
  1497. this.dropdown.on('keypress', function (e) {
  1498. self.trigger('keypress', e);
  1499. });
  1500. };
  1501. Select2.prototype._registerResultsEvents = function () {
  1502. var self = this;
  1503. this.results.on('query:append', function (params) {
  1504. self.trigger('query:append', params);
  1505. });
  1506. this.results.on('selected', function (params) {
  1507. self.trigger('select', params);
  1508. self.close();
  1509. });
  1510. this.results.on('unselected', function (params) {
  1511. self.trigger('unselect', params);
  1512. self.close();
  1513. });
  1514. this.results.on('results:focus', function (params) {
  1515. self.trigger('results:focus', params);
  1516. });
  1517. };
  1518. Select2.prototype._registerEvents = function () {
  1519. var self = this;
  1520. this.on('open', function () {
  1521. self.$container.addClass('open');
  1522. });
  1523. this.on('close', function () {
  1524. self.$container.removeClass('open');
  1525. });
  1526. this.on('query', function (params) {
  1527. this.data.query(params, function (data) {
  1528. self.trigger('results:all', {
  1529. data: data,
  1530. query: params
  1531. });
  1532. });
  1533. });
  1534. this.on('query:append', function (params) {
  1535. this.data.query(params, function (data) {
  1536. self.trigger('results:append', {
  1537. data: data,
  1538. query: params
  1539. });
  1540. });
  1541. });
  1542. this.on('keypress', function (evt) {
  1543. var key = evt.which;
  1544. if (self.isOpen()) {
  1545. if (key === KEYS.ENTER) {
  1546. self.trigger('results:select');
  1547. evt.preventDefault();
  1548. } else if (key === KEYS.UP) {
  1549. self.trigger('results:previous');
  1550. evt.preventDefault();
  1551. } else if (key === KEYS.DOWN) {
  1552. self.trigger('results:next');
  1553. evt.preventDefault();
  1554. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1555. self.close();
  1556. evt.preventDefault();
  1557. }
  1558. } else {
  1559. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1560. self.open();
  1561. evt.preventDefault();
  1562. }
  1563. }
  1564. });
  1565. };
  1566. Select2.prototype.toggleDropdown = function () {
  1567. if (this.isOpen()) {
  1568. this.close();
  1569. } else {
  1570. this.open();
  1571. }
  1572. };
  1573. Select2.prototype.open = function () {
  1574. if (this.isOpen()) {
  1575. return;
  1576. }
  1577. this.trigger('query', {});
  1578. this.trigger('open');
  1579. };
  1580. Select2.prototype.close = function () {
  1581. if (!this.isOpen()) {
  1582. return;
  1583. }
  1584. this.trigger('close');
  1585. };
  1586. Select2.prototype.isOpen = function () {
  1587. return this.$container.hasClass('open');
  1588. };
  1589. Select2.prototype.render = function () {
  1590. var $container = $(
  1591. '<span class="select2 select2-container select2-theme-default">' +
  1592. '<span class="selection"></span>' +
  1593. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1594. '</span>'
  1595. );
  1596. this.$container = $container;
  1597. $container.data('element', this.$element);
  1598. return $container;
  1599. };
  1600. return Select2;
  1601. });
  1602. define('jquery.select2',[
  1603. 'jquery',
  1604. 'select2/core'
  1605. ], function ($, Select2) {
  1606. if ($.fn.select2 == null) {
  1607. $.fn.select2 = function (options) {
  1608. options = options || {};
  1609. if (typeof options === 'object') {
  1610. this.each(function () {
  1611. var instance = new Select2($(this), options);
  1612. });
  1613. } else if (typeof options === 'string') {
  1614. var instance = this.data('select2');
  1615. var args = Array.prototype.slice.call(arguments, 1);
  1616. instance[options](args);
  1617. } else {
  1618. throw new Error('Invalid arguments for Select2: ' + options);
  1619. }
  1620. };
  1621. }
  1622. return Select2;
  1623. });
  1624. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());