select2.amd.js 45 KB

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