select2.amd.js 52 KB

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