select2.amd.js 55 KB

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