select2.amd.full.js 55 KB

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