select2.amd.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. if (event in this.listeners) {
  77. this.listeners[event].push(callback);
  78. } else {
  79. this.listeners[event] = [callback];
  80. }
  81. };
  82. Observable.prototype.trigger = function (event) {
  83. var slice = Array.prototype.slice;
  84. if (event in this.listeners) {
  85. this.invoke(this.listeners[event], slice.call(arguments, 1));
  86. }
  87. if ('*' in this.listeners) {
  88. this.invoke(this.listeners['*'], arguments);
  89. }
  90. };
  91. Observable.prototype.invoke = function (listeners, params) {
  92. for (var i = 0, len = listeners.length; i < len; i++) {
  93. listeners[i].apply(this, params);
  94. }
  95. };
  96. Utils.Observable = Observable;
  97. Utils.generateChars = function (length) {
  98. var chars = '';
  99. for (var i = 0; i < length; i++) {
  100. var randomChar = Math.floor(Math.random() * 36);
  101. chars += randomChar.toString(36);
  102. }
  103. return chars;
  104. };
  105. return Utils;
  106. });
  107. define('select2/results',[
  108. './utils'
  109. ], function (Utils) {
  110. function Results ($element, options, dataAdapter) {
  111. this.$element = $element;
  112. this.data = dataAdapter;
  113. this.options = options;
  114. Results.__super__.constructor.call(this);
  115. }
  116. Utils.Extend(Results, Utils.Observable);
  117. Results.prototype.render = function () {
  118. var $results = $(
  119. '<ul class="options" role="tree"></ul>'
  120. );
  121. if (this.options.get('multiple')) {
  122. $results.attr('aria-multiselectable', 'true');
  123. }
  124. this.$results = $results;
  125. return $results;
  126. };
  127. Results.prototype.clear = function () {
  128. this.$results.empty();
  129. };
  130. Results.prototype.append = function (data) {
  131. var $options = [];
  132. data = this.sort(data);
  133. for (var d = 0; d < data.length; d++) {
  134. var item = data[d];
  135. var $option = this.option(item);
  136. $options.push($option);
  137. }
  138. this.$results.append($options);
  139. };
  140. Results.prototype.sort = function (data) {
  141. return data;
  142. };
  143. Results.prototype.setClasses = function () {
  144. var self = this;
  145. this.data.current(function (selected) {
  146. var selectedIds = $.map(selected, function (s) {
  147. return s.id.toString();
  148. });
  149. var $options = self.$results.find('.option[aria-selected]');
  150. $options.each(function () {
  151. var $option = $(this);
  152. var item = $option.data('data');
  153. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  154. $option.attr('aria-selected', 'true');
  155. } else {
  156. $option.attr('aria-selected', 'false');
  157. }
  158. });
  159. var $selected = $options.filter('[aria-selected=true]');
  160. // Check if there are any selected options
  161. if ($selected.length > 0) {
  162. // If there are selected options, highlight the first
  163. $selected.first().trigger('mouseenter');
  164. } else {
  165. // If there are no selected options, highlight the first option
  166. // in the dropdown
  167. $options.first().trigger('mouseenter');
  168. }
  169. });
  170. };
  171. Results.prototype.option = function (data) {
  172. var $option = $(
  173. '<li class="option" role="treeitem" aria-selected="false"></li>'
  174. );
  175. if (data.children) {
  176. $option
  177. .attr('role', 'group')
  178. .attr('aria-label', data.text)
  179. .removeAttr('aria-selected');
  180. var $label = $('<strong class="group-label"></strong>');
  181. $label.html(data.text);
  182. var $children = [];
  183. for (var c = 0; c < data.children.length; c++) {
  184. var child = data.children[c];
  185. var $child = this.option(child);
  186. $children.push($child);
  187. }
  188. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  189. $childrenContainer.append($children);
  190. $option.append($label);
  191. $option.append($childrenContainer);
  192. } else {
  193. $option.html(data.text);
  194. }
  195. if (data.disabled) {
  196. $option
  197. .removeAttr('aria-selected')
  198. .attr('aria-disabled', 'true');
  199. }
  200. if (data.id == null) {
  201. $option.removeAttr('aria-selected');
  202. }
  203. if (data._resultId != null) {
  204. $option.attr('id', data._resultId);
  205. }
  206. $option.data('data', data);
  207. return $option;
  208. };
  209. Results.prototype.bind = function (container, $container) {
  210. var self = this;
  211. var id = container.id + '-results';
  212. this.$results.attr('id', id);
  213. container.on('results:all', function (params) {
  214. self.clear();
  215. self.append(params.data);
  216. if (container.isOpen()) {
  217. self.setClasses();
  218. }
  219. });
  220. container.on('results:append', function (params) {
  221. self.append(params.data);
  222. if (container.isOpen()) {
  223. self.setClasses();
  224. }
  225. });
  226. container.on('select', function () {
  227. if (!container.isOpen()) {
  228. return;
  229. }
  230. self.setClasses();
  231. });
  232. container.on('unselect', function () {
  233. if (!container.isOpen()) {
  234. return;
  235. }
  236. self.setClasses();
  237. });
  238. container.on('open', function () {
  239. // When the dropdown is open, aria-expended="true"
  240. self.$results.attr('aria-expanded', 'true');
  241. self.$results.attr('aria-hidden', 'false');
  242. self.setClasses();
  243. self.ensureHighlightVisible();
  244. });
  245. container.on('close', function () {
  246. // When the dropdown is closed, aria-expended="false"
  247. self.$results.attr('aria-expanded', 'false');
  248. self.$results.attr('aria-hidden', 'true');
  249. self.$results.removeAttr('aria-activedescendant');
  250. });
  251. container.on('results:select', function () {
  252. var $highlighted = self.$results.find('.highlighted');
  253. if ($highlighted.length === 0) {
  254. return;
  255. }
  256. var data = $highlighted.data('data');
  257. if ($highlighted.attr('aria-selected') == 'true') {
  258. self.trigger('unselected', {
  259. data: data
  260. });
  261. } else {
  262. self.trigger('selected', {
  263. data: data
  264. });
  265. }
  266. });
  267. container.on('results:previous', function () {
  268. var $highlighted = self.$results.find('.highlighted');
  269. var $options = self.$results.find('[aria-selected]');
  270. var currentIndex = $options.index($highlighted);
  271. // If we are already at te top, don't move further
  272. if (currentIndex === 0) {
  273. return;
  274. }
  275. var nextIndex = currentIndex - 1;
  276. // If none are highlighted, highlight the first
  277. if ($highlighted.length === 0) {
  278. nextIndex = 0;
  279. }
  280. var $next = $options.eq(nextIndex);
  281. $next.trigger('mouseenter');
  282. var currentOffset = self.$results.offset().top;
  283. var nextTop = $next.offset().top;
  284. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  285. if (nextIndex === 0) {
  286. self.$results.scrollTop(0);
  287. } else if (nextTop - currentOffset < 0) {
  288. self.$results.scrollTop(nextOffset);
  289. }
  290. });
  291. container.on('results:next', function () {
  292. var $highlighted = self.$results.find('.highlighted');
  293. var $options = self.$results.find('[aria-selected]');
  294. var currentIndex = $options.index($highlighted);
  295. var nextIndex = currentIndex + 1;
  296. // If we are at the last option, stay there
  297. if (nextIndex >= $options.length) {
  298. return;
  299. }
  300. var $next = $options.eq(nextIndex);
  301. $next.trigger('mouseenter');
  302. var currentOffset = self.$results.offset().top +
  303. self.$results.outerHeight(false);
  304. var nextBottom = $next.offset().top + $next.outerHeight(false);
  305. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  306. if (nextIndex === 0) {
  307. self.$results.scrollTop(0);
  308. } else if (nextBottom > currentOffset) {
  309. self.$results.scrollTop(nextOffset);
  310. }
  311. });
  312. container.on('results:focus', function (params) {
  313. params.element.addClass('highlighted');
  314. });
  315. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  316. var $this = $(this);
  317. var data = $this.data('data');
  318. if ($this.attr('aria-selected') === 'true') {
  319. self.trigger('unselected', {
  320. originalEvent: evt,
  321. data: data
  322. });
  323. return;
  324. }
  325. self.trigger('selected', {
  326. originalEvent: evt,
  327. data: data
  328. });
  329. });
  330. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  331. var data = $(this).data('data');
  332. self.$results.find('.option.highlighted').removeClass('highlighted');
  333. self.trigger('results:focus', {
  334. data: data,
  335. element: $(this)
  336. });
  337. });
  338. this.$results.on('mouseleave', '.option', function (evt) {
  339. if ($(this).hasClass('highlighted')) {
  340. $(this).removeClass('highlighted');
  341. self.$results.removeAttr('aria-activedescendant');
  342. }
  343. });
  344. };
  345. Results.prototype.ensureHighlightVisible = function () {
  346. var $highlighted = this.$results.find('.highlighted');
  347. if ($highlighted.length === 0) {
  348. return;
  349. }
  350. var $options = this.$results.find('[aria-selected]');
  351. var currentIndex = $options.index($highlighted);
  352. var currentOffset = this.$results.offset().top;
  353. var nextTop = $highlighted.offset().top;
  354. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  355. var offsetDelta = nextTop - currentOffset;
  356. nextOffset -= $highlighted.outerHeight(false) * 2;
  357. if (currentIndex <= 2) {
  358. this.$results.scrollTop(0);
  359. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  360. this.$results.scrollTop(nextOffset);
  361. }
  362. };
  363. return Results;
  364. });
  365. define('select2/selection/base',[
  366. '../utils'
  367. ], function (Utils) {
  368. function BaseSelection ($element, options) {
  369. this.$element = $element;
  370. this.options = options;
  371. BaseSelection.__super__.constructor.call(this);
  372. }
  373. Utils.Extend(BaseSelection, Utils.Observable);
  374. BaseSelection.prototype.render = function () {
  375. throw new Error('The `render` method must be defined in child classes.');
  376. };
  377. BaseSelection.prototype.bind = function (container, $container) {
  378. var self = this;
  379. container.on('selection:update', function (params) {
  380. self.update(params.data);
  381. });
  382. };
  383. BaseSelection.prototype.update = function (data) {
  384. throw new Error('The `update` method must be defined in child classes.');
  385. };
  386. return BaseSelection;
  387. });
  388. define('select2/keys',[
  389. ], function () {
  390. var KEYS = {
  391. BACKSPACE: 8,
  392. TAB: 9,
  393. ENTER: 13,
  394. SHIFT: 16,
  395. CTRL: 17,
  396. ALT: 18,
  397. ESC: 27,
  398. SPACE: 32,
  399. PAGE_UP: 33,
  400. PAGE_DOWN: 34,
  401. END: 35,
  402. HOME: 36,
  403. LEFT: 37,
  404. UP: 38,
  405. RIGHT: 39,
  406. DOWN: 40,
  407. DELETE: 46,
  408. isArrow: function (k) {
  409. k = k.which ? k.which : k;
  410. switch (k) {
  411. case KEY.LEFT:
  412. case KEY.RIGHT:
  413. case KEY.UP:
  414. case KEY.DOWN:
  415. return true;
  416. }
  417. return false;
  418. }
  419. };
  420. return KEYS;
  421. });
  422. define('select2/selection/single',[
  423. './base',
  424. '../utils',
  425. '../keys'
  426. ], function (BaseSelection, Utils, KEYS) {
  427. function SingleSelection () {
  428. SingleSelection.__super__.constructor.apply(this, arguments);
  429. }
  430. Utils.Extend(SingleSelection, BaseSelection);
  431. SingleSelection.prototype.render = function () {
  432. var $selection = $(
  433. '<span class="single-select" tabindex="0" role="combobox" ' +
  434. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  435. '<span class="rendered-selection"></span>' +
  436. '</span>'
  437. );
  438. $selection.attr('title', this.$element.attr('title'));
  439. this.$selection = $selection;
  440. return $selection;
  441. };
  442. SingleSelection.prototype.bind = function (container, $container) {
  443. var self = this;
  444. SingleSelection.__super__.bind.apply(this, arguments);
  445. var id = container.id + '-container';
  446. var resultsId = container.id + '-results';
  447. this.$selection.find('.rendered-selection').attr('id', id);
  448. this.$selection.attr('aria-labelledby', id);
  449. this.$selection.attr('aria-owns', resultsId);
  450. this.$selection.on('mousedown', function (evt) {
  451. // Only respond to left clicks
  452. if (evt.which !== 1) {
  453. return;
  454. }
  455. self.trigger('toggle', {
  456. originalEvent: evt
  457. });
  458. });
  459. container.on('open', function () {
  460. // When the dropdown is open, aria-expanded="true"
  461. self.$selection.attr('aria-expanded', 'true');
  462. });
  463. container.on('close', function () {
  464. // When the dropdown is closed, aria-expanded="false"
  465. self.$selection.attr('aria-expanded', 'false');
  466. self.$selection.removeAttr('aria-activedescendant');
  467. });
  468. this.$selection.on('focus', function (evt) {
  469. // User focuses on the container
  470. });
  471. this.$selection.on('blur', function (evt) {
  472. // User exits the container
  473. });
  474. this.$selection.on('keydown', function (evt) {
  475. var key = evt.which;
  476. if (container.isOpen()) {
  477. if (key == KEYS.ENTER) {
  478. self.trigger('results:select');
  479. evt.preventDefault();
  480. } else if (key == KEYS.UP) {
  481. self.trigger('results:previous');
  482. evt.preventDefault();
  483. } else if (key == KEYS.DOWN) {
  484. self.trigger('results:next');
  485. evt.preventDefault();
  486. }
  487. } else {
  488. if (key == KEYS.ENTER || key == KEYS.SPACE) {
  489. self.trigger('open');
  490. evt.preventDefault();
  491. }
  492. }
  493. });
  494. container.on('results:focus', function (params) {
  495. self.$selection.attr('aria-activedescendant', params.data._resultId);
  496. });
  497. container.on('selection:update', function (params) {
  498. self.update(params.data);
  499. });
  500. };
  501. SingleSelection.prototype.clear = function () {
  502. this.$selection.find('.rendered-selection').empty();
  503. };
  504. SingleSelection.prototype.display = function (data) {
  505. return data.text;
  506. };
  507. SingleSelection.prototype.selectionContainer = function () {
  508. return $('<span></span>');
  509. };
  510. SingleSelection.prototype.update = function (data) {
  511. if (data.length === 0) {
  512. this.clear();
  513. return;
  514. }
  515. var selection = data[0];
  516. var formatted = this.display(selection);
  517. this.$selection.find('.rendered-selection').html(formatted);
  518. };
  519. return SingleSelection;
  520. });
  521. define('select2/selection/multiple',[
  522. './base',
  523. '../utils'
  524. ], function (BaseSelection, Utils) {
  525. function MultipleSelection ($element, options) {
  526. this.$element = $element;
  527. this.options = options;
  528. MultipleSelection.__super__.constructor.call(this);
  529. }
  530. Utils.Extend(MultipleSelection, BaseSelection);
  531. MultipleSelection.prototype.render = function () {
  532. var $selection = $(
  533. '<span class="multiple-select">' +
  534. '<ul class="rendered-selection"></ul>' +
  535. '</span>'
  536. );
  537. this.$selection = $selection;
  538. return $selection;
  539. };
  540. MultipleSelection.prototype.bind = function (container, $container) {
  541. var self = this;
  542. MultipleSelection.__super__.bind.apply(this, arguments);
  543. this.$selection.on('click', function (evt) {
  544. self.trigger('toggle', {
  545. originalEvent: evt
  546. });
  547. });
  548. this.$selection.on('click', '.remove', function (evt) {
  549. var $remove = $(this);
  550. var $selection = $remove.parent();
  551. var data = $selection.data('data');
  552. self.trigger('unselected', {
  553. originalEvent: evt,
  554. data: data
  555. });
  556. });
  557. };
  558. MultipleSelection.prototype.clear = function () {
  559. this.$selection.find('.rendered-selection').empty();
  560. };
  561. MultipleSelection.prototype.display = function (data) {
  562. return data.text;
  563. };
  564. MultipleSelection.prototype.selectionContainer = function () {
  565. var $container = $(
  566. '<li class="choice">' +
  567. '<span class="remove" role="presentation">&times;</span>' +
  568. '</li>'
  569. );
  570. return $container;
  571. };
  572. MultipleSelection.prototype.update = function (data) {
  573. this.clear();
  574. if (data.length === 0) {
  575. return;
  576. }
  577. var $selections = [];
  578. for (var d = 0; d < data.length; d++) {
  579. var selection = data[d];
  580. var formatted = this.display(selection);
  581. var $selection = this.selectionContainer();
  582. $selection.append(formatted);
  583. $selection.data('data', selection);
  584. $selections.push($selection);
  585. }
  586. this.$selection.find('.rendered-selection').append($selections);
  587. };
  588. return MultipleSelection;
  589. });
  590. define('select2/selection/placeholder',[
  591. '../utils'
  592. ], function (Utils) {
  593. function Placeholder (decorated, $element, options) {
  594. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  595. decorated.call(this, $element, options);
  596. }
  597. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  598. if (typeof placeholder === 'string') {
  599. placeholder = {
  600. id: '',
  601. text: placeholder
  602. };
  603. }
  604. return placeholder;
  605. };
  606. Placeholder.prototype.update = function (decorated, data) {
  607. var singlePlaceholder = (
  608. data.length == 1 && data[0].id != this.placeholder.id
  609. );
  610. var multipleSelections = data.length > 1;
  611. if (multipleSelections || singlePlaceholder) {
  612. return decorated.call(this, data);
  613. }
  614. this.clear();
  615. var $placeholder = this.selectionContainer();
  616. $placeholder.html(this.display(this.placeholder));
  617. $placeholder.addClass('placeholder').removeClass('choice');
  618. this.$selection.find('.rendered-selection').append($placeholder);
  619. };
  620. return Placeholder;
  621. });
  622. define('select2/translation',[
  623. ], function () {
  624. function Translation (dict) {
  625. this.dict = dict || {};
  626. }
  627. Translation.prototype.all = function () {
  628. return this.dict;
  629. };
  630. Translation.prototype.get = function (key) {
  631. return this.dict[key];
  632. };
  633. Translation.prototype.extend = function (translation) {
  634. this.dict = $.extend({}, translation.all(), this.dict);
  635. };
  636. // Static functions
  637. Translation._cache = {};
  638. Translation.loadPath = function (path) {
  639. if (!(path in Translation._cache)) {
  640. var translations = require(path);
  641. Translation._cache[path] = translations;
  642. }
  643. return new Translation(Translation._cache[path]);
  644. };
  645. return Translation;
  646. });
  647. define('select2/data/base',[
  648. '../utils'
  649. ], function (Utils) {
  650. function BaseAdapter ($element, options) {
  651. BaseAdapter.__super__.constructor.call(this);
  652. }
  653. Utils.Extend(BaseAdapter, Utils.Observable);
  654. BaseAdapter.prototype.current = function (callback) {
  655. throw new Error('The `current` method must be defined in child classes.');
  656. };
  657. BaseAdapter.prototype.query = function (params, callback) {
  658. throw new Error('The `query` method must be defined in child classes.');
  659. };
  660. BaseAdapter.prototype.bind = function (container, $container) {
  661. // Can be implemented in subclasses
  662. };
  663. BaseAdapter.prototype.generateResultId = function (container, data) {
  664. var id = container.id + '-result-';
  665. id += Utils.generateChars(4);
  666. if (data.id != null) {
  667. id += '-' + data.id.toString();
  668. } else {
  669. id += '-' + Utils.generateChars(4);
  670. }
  671. return id;
  672. };
  673. return BaseAdapter;
  674. });
  675. define('select2/data/select',[
  676. './base',
  677. '../utils',
  678. 'jquery'
  679. ], function (BaseAdapter, Utils, $) {
  680. function SelectAdapter ($element, options) {
  681. this.$element = $element;
  682. SelectAdapter.__super__.constructor.call(this);
  683. }
  684. Utils.Extend(SelectAdapter, BaseAdapter);
  685. SelectAdapter.prototype.current = function (callback) {
  686. var data = [];
  687. var self = this;
  688. this.$element.find(':selected').each(function () {
  689. var $option = $(this);
  690. var option = self.item($option);
  691. data.push(option);
  692. });
  693. callback(data);
  694. };
  695. SelectAdapter.prototype.select = function (data) {
  696. var self = this;
  697. // Create items marked as tags
  698. if (data._tag === true) {
  699. // Clear the tag flag from it
  700. delete data._tag;
  701. // Create and add the option
  702. var $option = this.option(data);
  703. this.$element.append($option);
  704. }
  705. if (this.$element.prop('multiple')) {
  706. this.current(function (currentData) {
  707. var val = [];
  708. data = [data];
  709. data.push.apply(data, currentData);
  710. for (var d = 0; d < data.length; d++) {
  711. id = data[d].id;
  712. if (val.indexOf(id) === -1) {
  713. val.push(id);
  714. }
  715. }
  716. self.$element.val(val);
  717. self.$element.trigger('change');
  718. });
  719. } else {
  720. var val = data.id;
  721. this.$element.val(val);
  722. this.$element.trigger('change');
  723. }
  724. };
  725. SelectAdapter.prototype.unselect = function (data) {
  726. var self = this;
  727. if (!this.$element.prop('multiple')) {
  728. return;
  729. }
  730. this.current(function (currentData) {
  731. var val = [];
  732. for (var d = 0; d < currentData.length; d++) {
  733. id = currentData[d].id;
  734. if (id !== data.id && val.indexOf(id) === -1) {
  735. val.push(id);
  736. }
  737. }
  738. self.$element.val(val);
  739. self.$element.trigger('change');
  740. });
  741. };
  742. SelectAdapter.prototype.bind = function (container, $container) {
  743. var self = this;
  744. this.container = container;
  745. container.on('select', function (params) {
  746. self.select(params.data);
  747. });
  748. container.on('unselect', function (params) {
  749. self.unselect(params.data);
  750. });
  751. };
  752. SelectAdapter.prototype.query = function (params, callback) {
  753. var data = [];
  754. var self = this;
  755. var $options = this.$element.children();
  756. $options.each(function () {
  757. var $option = $(this);
  758. if (!$option.is('option') && !$option.is('optgroup')) {
  759. return;
  760. }
  761. var option = self.item($option);
  762. var matches = self.matches(params, option);
  763. if (matches !== null) {
  764. data.push(matches);
  765. }
  766. });
  767. callback(data);
  768. };
  769. SelectAdapter.prototype.option = function (data) {
  770. var $option = $('<option></option>');
  771. $option.text(data.text);
  772. $option.val(data.id);
  773. $option.prop('disabled', data.disabled || false);
  774. // Get any automatically generated data values
  775. var detectedData = this.item($option);
  776. // Merge it with the already present data
  777. var combinedData = $.extend({}, data, detectedData);
  778. // Override the option's data with the combined data
  779. $option.data('data', combinedData);
  780. return $option;
  781. };
  782. SelectAdapter.prototype.item = function ($option) {
  783. var data = $option.data('data');
  784. // If the data has already be generated, use it
  785. if (data == null) {
  786. if ($option.is('option')) {
  787. data = {
  788. id: $option.val(),
  789. text: $option.html(),
  790. disabled: $option.prop('disabled')
  791. };
  792. } else if ($option.is('optgroup')) {
  793. data = {
  794. text: $option.attr('label'),
  795. children: []
  796. };
  797. var $children = $option.children('option');
  798. var children = [];
  799. for (var c = 0; c < $children.length; c++) {
  800. var $child = $($children[c]);
  801. var child = this.item($child);
  802. children.push(child);
  803. }
  804. data.children = children;
  805. }
  806. if (data.id && this.container != null) {
  807. data._resultId = this.generateResultId(this.container, data);
  808. }
  809. $option.data('data', data);
  810. }
  811. return data;
  812. };
  813. SelectAdapter.prototype.matches = function (params, data) {
  814. var match = $.extend(true, {}, data);
  815. if (data.children) {
  816. for (var c = data.children.length - 1; c >= 0; c--) {
  817. var child = data.children[c];
  818. var matches = this.matches(params, child);
  819. // If there wasn't a match, remove the object in the array
  820. if (matches === null) {
  821. match.children.splice(c, 1);
  822. }
  823. }
  824. if (match.children.length > 0) {
  825. return match;
  826. }
  827. }
  828. if ($.trim(params.term) === '') {
  829. return match;
  830. }
  831. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  832. return match;
  833. }
  834. return null;
  835. };
  836. return SelectAdapter;
  837. });
  838. define('select2/data/array',[
  839. './select',
  840. '../utils',
  841. 'jquery'
  842. ], function (SelectAdapter, Utils, $) {
  843. function ArrayAdapter ($element, options) {
  844. this.data = options.get('data');
  845. ArrayAdapter.__super__.constructor.call(this, $element, options);
  846. }
  847. Utils.Extend(ArrayAdapter, SelectAdapter);
  848. ArrayAdapter.prototype.select = function (data) {
  849. var self = this;
  850. this.$element.find('option').each(function () {
  851. var $option = $(this);
  852. var option = self.item($option);
  853. if (option.id == data.id.toString()) {
  854. $option.remove();
  855. }
  856. });
  857. var $option = this.option(data);
  858. this.$element.append($option);
  859. ArrayAdapter.__super__.select.call(this, data);
  860. };
  861. ArrayAdapter.prototype.query = function (params, callback) {
  862. var matches = [];
  863. var self = this;
  864. $.each(this.data, function () {
  865. var option = this;
  866. if (self.matches(params, option)) {
  867. matches.push(option);
  868. }
  869. });
  870. callback(matches);
  871. };
  872. return ArrayAdapter;
  873. });
  874. define('select2/data/ajax',[
  875. './array',
  876. '../utils',
  877. 'jquery'
  878. ], function (ArrayAdapter, Utils, $) {
  879. function AjaxAdapter ($element, options) {
  880. this.ajaxOptions = options.get('ajax');
  881. this.processResults = this.ajaxOptions.processResults ||
  882. function (results) {
  883. return results;
  884. };
  885. ArrayAdapter.__super__.constructor.call(this, $element, options);
  886. }
  887. Utils.Extend(AjaxAdapter, ArrayAdapter);
  888. AjaxAdapter.prototype.query = function (params, callback) {
  889. var matches = [];
  890. var self = this;
  891. var options = $.extend({
  892. type: 'GET'
  893. }, this.ajaxOptions);
  894. if (typeof options.url === 'function') {
  895. options.url = options.url(params);
  896. }
  897. if (typeof options.data === 'function') {
  898. options.data = options.data(params);
  899. }
  900. var $request = $.ajax(options);
  901. $request.success(function (data) {
  902. var results = self.processResults(data);
  903. callback(results);
  904. });
  905. };
  906. return AjaxAdapter;
  907. });
  908. define('select2/data/tags',[
  909. ], function () {
  910. function Tags (decorated, $element, options) {
  911. var tags = options.get('tags');
  912. decorated.call(this, $element, options);
  913. }
  914. Tags.prototype.query = function (decorated, params, callback) {
  915. var self = this;
  916. if (params.term == null || params.term === '' || params.page != null) {
  917. decorated.call(this, params, callback);
  918. return;
  919. }
  920. function wrapper (data, child) {
  921. for (var i = 0; i < data.length; i++) {
  922. var option = data[i];
  923. var checkChildren = (
  924. option.children != null && !wrapper(option.children, true)
  925. );
  926. var checkText = option.text === params.term;
  927. if (checkText || checkChildren) {
  928. if (child) {
  929. return false;
  930. }
  931. callback(data);
  932. return;
  933. }
  934. }
  935. if (child) {
  936. return true;
  937. }
  938. var tag = self.createTag(params);
  939. tag._tag = true;
  940. data.unshift(tag);
  941. callback(data);
  942. }
  943. decorated.call(this, params, wrapper);
  944. };
  945. Tags.prototype.createTag = function (decorated, params) {
  946. return {
  947. id: params.term,
  948. text: params.term
  949. };
  950. };
  951. return Tags;
  952. });
  953. define('select2/dropdown',[
  954. './utils'
  955. ], function (Utils) {
  956. function Dropdown ($element, options) {
  957. this.$element = $element;
  958. }
  959. Utils.Extend(Dropdown, Utils.Observable);
  960. Dropdown.prototype.render = function () {
  961. var $dropdown = $(
  962. '<span class="dropdown">' +
  963. '<span class="results"></span>' +
  964. '</span>'
  965. );
  966. return $dropdown;
  967. };
  968. Dropdown.prototype.bind = function (container, $container) {
  969. // Can be implemented in subclasses
  970. };
  971. return Dropdown;
  972. });
  973. define('select2/dropdown/search',[
  974. ], function () {
  975. function Search () { }
  976. Search.prototype.render = function (decorated) {
  977. var $rendered = decorated.call(this);
  978. var $search = $(
  979. '<span class="search">' +
  980. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  981. '</span>'
  982. );
  983. this.$searchContainer = $search;
  984. this.$search = $search.find('input');
  985. $rendered.prepend($search);
  986. return $rendered;
  987. };
  988. Search.prototype.bind = function (decorated, container, $container) {
  989. var self = this;
  990. decorated.call(this, container, $container);
  991. this.$search.on('keyup', function () {
  992. container.trigger('query', {
  993. term: $(this).val()
  994. });
  995. });
  996. container.on('open', function () {
  997. self.$search.attr('tabindex', 0);
  998. });
  999. container.on('close', function () {
  1000. self.$search.attr('tabindex', -1);
  1001. });
  1002. container.on('results:all', function (params) {
  1003. if (params.query.term == null || params.query.term === '') {
  1004. var showSearch = self.showSearch(params);
  1005. if (showSearch) {
  1006. self.$searchContainer.show();
  1007. } else {
  1008. self.$searchContainer.hide();
  1009. }
  1010. }
  1011. });
  1012. };
  1013. Search.prototype.showSearch = function (params) {
  1014. return true;
  1015. };
  1016. return Search;
  1017. });
  1018. define('select2/i18n/en',[],function () {
  1019. return {
  1020. 'no_results': function () {
  1021. return 'No results found';
  1022. }
  1023. };
  1024. });
  1025. define('select2/defaults',[
  1026. 'jquery',
  1027. './results',
  1028. './selection/single',
  1029. './selection/multiple',
  1030. './selection/placeholder',
  1031. './utils',
  1032. './translation',
  1033. './data/select',
  1034. './data/array',
  1035. './data/ajax',
  1036. './data/tags',
  1037. './dropdown',
  1038. './dropdown/search',
  1039. './i18n/en'
  1040. ], function ($, ResultsList,
  1041. SingleSelection, MultipleSelection, Placeholder,
  1042. Utils, Translation,
  1043. SelectData, ArrayData, AjaxData, Tags,
  1044. Dropdown, Search, EnglishTranslation) {
  1045. function Defaults () {
  1046. this.reset();
  1047. }
  1048. Defaults.prototype.apply = function (options) {
  1049. options = $.extend({}, this.defaults, options);
  1050. if (options.dataAdapter == null) {
  1051. if (options.ajax) {
  1052. options.dataAdapter = AjaxData;
  1053. } else if (options.data) {
  1054. options.dataAdapter = ArrayData;
  1055. } else {
  1056. options.dataAdapter = SelectData;
  1057. }
  1058. }
  1059. if (options.tags != null) {
  1060. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1061. }
  1062. if (options.resultsAdapter == null) {
  1063. options.resultsAdapter = ResultsList;
  1064. }
  1065. if (options.dropdownAdapter == null) {
  1066. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1067. options.dropdownAdapter = SearchableDropdown;
  1068. }
  1069. if (options.selectionAdapter == null) {
  1070. if (options.multiple) {
  1071. options.selectionAdapter = MultipleSelection;
  1072. } else {
  1073. options.selectionAdapter = SingleSelection;
  1074. }
  1075. // Add the placeholder mixin if a placeholder was specified
  1076. if (options.placeholder != null) {
  1077. options.selectionAdapter = Utils.Decorate(
  1078. options.selectionAdapter,
  1079. Placeholder
  1080. );
  1081. }
  1082. }
  1083. if (typeof options.language === 'string') {
  1084. options.language = [options.language];
  1085. }
  1086. if ($.isArray(options.language)) {
  1087. var languages = new Translation();
  1088. var languageNames = options.language.concat(this.defaults.language);
  1089. for (var l = 0; l < languageNames.length; l++) {
  1090. var name = languageNames[l];
  1091. var language = {};
  1092. try {
  1093. // Try to load it with the original name
  1094. language = Translation.loadPath(name);
  1095. } catch (e) {
  1096. // If we couldn't load it, check if it wasn't the full path
  1097. name = 'select2/i18n/' + name;
  1098. language = Translation.loadPath(name);
  1099. }
  1100. languages.extend(language);
  1101. }
  1102. options.translations = languages;
  1103. } else {
  1104. options.translations = new Translations(options.language);
  1105. }
  1106. return options;
  1107. };
  1108. Defaults.prototype.reset = function () {
  1109. this.defaults = {
  1110. language: ['select2/i18n/en']
  1111. };
  1112. };
  1113. var defaults = new Defaults();
  1114. return defaults;
  1115. });
  1116. define('select2/options',[
  1117. './defaults'
  1118. ], function (Defaults) {
  1119. function Options (options) {
  1120. this.options = Defaults.apply(options);
  1121. }
  1122. Options.prototype.fromElement = function ($e) {
  1123. return this;
  1124. };
  1125. Options.prototype.get = function (key) {
  1126. return this.options[key];
  1127. };
  1128. Options.prototype.set = function (key, val) {
  1129. this.options[key] = val;
  1130. };
  1131. return Options;
  1132. });
  1133. define('select2/core',[
  1134. 'jquery',
  1135. './options',
  1136. './utils'
  1137. ], function ($, Options, Utils) {
  1138. var Select2 = function ($element, options) {
  1139. this.$element = $element;
  1140. if ($element.attr('id') != null) {
  1141. this.id = $element.attr('id');
  1142. } else if ($element.attr('name') != null) {
  1143. this.id = $element.attr('name') + '-' + Utils.generateChars(2);
  1144. } else {
  1145. this.id = Utils.generateChars(4);
  1146. }
  1147. this.id = 'select2-' + this.id;
  1148. options = options || {};
  1149. options.multiple = options.multiple || $element.prop('multiple');
  1150. this.options = new Options(options);
  1151. Select2.__super__.constructor.call(this);
  1152. // Set up containers and adapters
  1153. var DataAdapter = this.options.get('dataAdapter');
  1154. this.data = new DataAdapter($element, this.options);
  1155. var $container = this.render();
  1156. this.$container = $container;
  1157. $container.insertAfter(this.$element);
  1158. $container.width($element.outerWidth(false));
  1159. var SelectionAdapter = this.options.get('selectionAdapter');
  1160. this.selection = new SelectionAdapter($element, this.options);
  1161. var $selectionContainer = $container.find('.selection');
  1162. var $selection = this.selection.render();
  1163. $selectionContainer.append($selection);
  1164. var DropdownAdapter = this.options.get('dropdownAdapter');
  1165. this.dropdown = new DropdownAdapter($element, this.options);
  1166. var $dropdownContainer = $container.find('.dropdown-wrapper');
  1167. var $dropdown = this.dropdown.render();
  1168. $dropdownContainer.append($dropdown);
  1169. var ResultsAdapter = this.options.get('resultsAdapter');
  1170. this.results = new ResultsAdapter($element, this.options, this.data);
  1171. var $resultsContainer = $dropdown.find('.results');
  1172. var $results = this.results.render();
  1173. $resultsContainer.append($results);
  1174. // Bind events
  1175. var self = this;
  1176. this.data.bind(this, $container);
  1177. this.selection.bind(this, $container);
  1178. this.dropdown.bind(this, $container);
  1179. this.results.bind(this, $container);
  1180. this.$element.on('change', function () {
  1181. self.data.current(function (data) {
  1182. self.trigger('selection:update', {
  1183. data: data
  1184. });
  1185. });
  1186. });
  1187. this.selection.on('open', function () {
  1188. self.open();
  1189. });
  1190. this.selection.on('close', function () {
  1191. self.close();
  1192. });
  1193. this.selection.on('toggle', function () {
  1194. self.toggleDropdown();
  1195. });
  1196. this.selection.on('results:select', function () {
  1197. self.trigger('results:select');
  1198. });
  1199. this.selection.on('results:previous', function () {
  1200. self.trigger('results:previous');
  1201. });
  1202. this.selection.on('results:next', function () {
  1203. self.trigger('results:next');
  1204. });
  1205. this.selection.on('unselected', function (params) {
  1206. self.trigger('unselect', params);
  1207. self.close();
  1208. });
  1209. this.results.on('selected', function (params) {
  1210. self.trigger('select', params);
  1211. self.close();
  1212. });
  1213. this.results.on('unselected', function (params) {
  1214. self.trigger('unselect', params);
  1215. self.close();
  1216. });
  1217. this.results.on('results:focus', function (params) {
  1218. self.trigger('results:focus', params);
  1219. });
  1220. this.on('open', function () {
  1221. $container.addClass('open');
  1222. });
  1223. this.on('close', function () {
  1224. $container.removeClass('open');
  1225. });
  1226. // Set the initial state
  1227. this.data.current(function (initialData) {
  1228. self.trigger('selection:update', {
  1229. data: initialData
  1230. });
  1231. });
  1232. this.on('query', function (params) {
  1233. this.data.query(params, function (data) {
  1234. self.trigger('results:all', {
  1235. data: data,
  1236. query: params
  1237. });
  1238. });
  1239. });
  1240. this.trigger('query', {});
  1241. // Hide the original select
  1242. $element.hide();
  1243. $element.attr('tabindex', '-1');
  1244. $element.data('select2', this);
  1245. };
  1246. Utils.Extend(Select2, Utils.Observable);
  1247. Select2.prototype.toggleDropdown = function () {
  1248. if (this.isOpen()) {
  1249. this.close();
  1250. } else {
  1251. this.open();
  1252. }
  1253. };
  1254. Select2.prototype.open = function () {
  1255. if (this.isOpen()) {
  1256. return;
  1257. }
  1258. this.trigger('open');
  1259. };
  1260. Select2.prototype.close = function () {
  1261. if (!this.isOpen()) {
  1262. return;
  1263. }
  1264. this.trigger('close');
  1265. };
  1266. Select2.prototype.isOpen = function () {
  1267. return this.$container.hasClass('open');
  1268. };
  1269. Select2.prototype.render = function () {
  1270. var $container = $(
  1271. '<span class="select2 select2-container select2-theme-default">' +
  1272. '<span class="selection"></span>' +
  1273. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1274. '</span>'
  1275. );
  1276. return $container;
  1277. };
  1278. return Select2;
  1279. });
  1280. define('jquery.select2',[
  1281. 'jquery',
  1282. 'select2/core'
  1283. ], function ($, Select2) {
  1284. if ($.fn.select2 == null) {
  1285. $.fn.select2 = function (options) {
  1286. options = options || {};
  1287. if (typeof options === 'object') {
  1288. this.each(function () {
  1289. var instance = new Select2($(this), options);
  1290. });
  1291. } else if (typeof options === 'string') {
  1292. var instance = this.data('select2');
  1293. var args = Array.prototype.slice.call(arguments, 1);
  1294. instance[options](args);
  1295. } else {
  1296. throw new Error('Invalid arguments for Select2: ' + options);
  1297. }
  1298. };
  1299. }
  1300. return Select2;
  1301. });
  1302. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());