select2.amd.js 51 KB

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