select2.amd.js 51 KB

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