select2.amd.js 52 KB

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