select2.amd.full.js 52 KB

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