select2.amd.js 50 KB

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