select2.amd.js 54 KB

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