select2.amd.js 51 KB

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