select2.amd.js 45 KB

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