select2.amd.js 42 KB

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