select2.amd.js 44 KB

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