select2.amd.full.js 56 KB

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