select2.amd.full.js 52 KB

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