select2.amd.js 52 KB

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