select2.amd.full.js 52 KB

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