select2.amd.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. 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. if (event in this.listeners) {
  77. this.listeners[event].push(callback);
  78. } else {
  79. this.listeners[event] = [callback];
  80. }
  81. };
  82. Observable.prototype.trigger = function (event) {
  83. var slice = Array.prototype.slice;
  84. if (event in this.listeners) {
  85. this.invoke(this.listeners[event], slice.call(arguments, 1));
  86. }
  87. if ('*' in this.listeners) {
  88. this.invoke(this.listeners['*'], arguments);
  89. }
  90. };
  91. Observable.prototype.invoke = function (listeners, params) {
  92. for (var i = 0, len = listeners.length; i < len; i++) {
  93. listeners[i].apply(this, params);
  94. }
  95. };
  96. Utils.Observable = Observable;
  97. return Utils;
  98. });
  99. define('select2/data/base',[
  100. '../utils'
  101. ], function (Utils) {
  102. function BaseAdapter ($element, options) {
  103. BaseAdapter.__super__.constructor.call(this);
  104. }
  105. Utils.Extend(BaseAdapter, Utils.Observable);
  106. BaseAdapter.prototype.current = function (callback) {
  107. throw new Error('The `current` method must be defined in child classes.');
  108. };
  109. BaseAdapter.prototype.query = function (params, callback) {
  110. throw new Error('The `query` method must be defined in child classes.');
  111. };
  112. return BaseAdapter;
  113. });
  114. define('select2/data/select',[
  115. './base',
  116. '../utils',
  117. 'jquery'
  118. ], function (BaseAdapter, Utils, $) {
  119. function SelectAdapter ($element, options) {
  120. this.$element = $element;
  121. SelectAdapter.__super__.constructor.call(this);
  122. }
  123. Utils.Extend(SelectAdapter, BaseAdapter);
  124. SelectAdapter.prototype.current = function (callback) {
  125. var data = [];
  126. var self = this;
  127. this.$element.find(':selected').each(function () {
  128. var $option = $(this);
  129. var option = self.item($option);
  130. data.push(option);
  131. });
  132. callback(data);
  133. };
  134. SelectAdapter.prototype.select = function (data) {
  135. var self = this;
  136. if (this.$element.prop('multiple')) {
  137. this.current(function (currentData) {
  138. var val = [];
  139. data = [data];
  140. data.push.apply(data, currentData);
  141. for (var d = 0; d < data.length; d++) {
  142. id = data[d].id;
  143. if (val.indexOf(id) === -1) {
  144. val.push(id);
  145. }
  146. }
  147. self.$element.val(val);
  148. self.$element.trigger('change');
  149. });
  150. } else {
  151. var val = data.id;
  152. this.$element.val(val);
  153. this.$element.trigger('change');
  154. }
  155. };
  156. SelectAdapter.prototype.unselect = function (data) {
  157. var self = this;
  158. if (!this.$element.prop('multiple')) {
  159. return;
  160. }
  161. this.current(function (currentData) {
  162. var val = [];
  163. for (var d = 0; d < currentData.length; d++) {
  164. id = currentData[d].id;
  165. if (id !== data.id && val.indexOf(id) === -1) {
  166. val.push(id);
  167. }
  168. }
  169. self.$element.val(val);
  170. self.$element.trigger('change');
  171. });
  172. };
  173. SelectAdapter.prototype.bind = function (container, $container) {
  174. var self = this;
  175. container.on('select', function (params) {
  176. self.select(params.data);
  177. });
  178. container.on('unselect', function (params) {
  179. self.unselect(params.data);
  180. });
  181. };
  182. SelectAdapter.prototype.query = function (params, callback) {
  183. var data = [];
  184. var self = this;
  185. this.$element.find('option').each(function () {
  186. var $option = $(this);
  187. var option = self.item($option);
  188. if (self.matches(params, option)) {
  189. data.push(option);
  190. }
  191. });
  192. callback(data);
  193. };
  194. SelectAdapter.prototype.item = function ($option) {
  195. var data = $option.data('data');
  196. // If the data has already be generated, use it
  197. if (data == null) {
  198. data = {
  199. id: $option.val(),
  200. text: $option.html()
  201. };
  202. $option.data('data', data);
  203. }
  204. return data;
  205. };
  206. SelectAdapter.prototype.matches = function (params, data) {
  207. if ($.trim(params.term) === '') {
  208. return true;
  209. }
  210. if (data.text.indexOf(params.term) > -1) {
  211. return true;
  212. }
  213. return false;
  214. };
  215. return SelectAdapter;
  216. });
  217. define('select2/results',[
  218. './utils'
  219. ], function (Utils) {
  220. function Results ($element, options, dataAdapter) {
  221. this.$element = $element;
  222. this.data = dataAdapter;
  223. Results.__super__.constructor.call(this);
  224. }
  225. Utils.Extend(Results, Utils.Observable);
  226. Results.prototype.render = function () {
  227. var $results = $(
  228. '<ul class="options"></ul>'
  229. );
  230. this.$results = $results;
  231. return $results;
  232. };
  233. Results.prototype.clear = function () {
  234. this.$results.empty();
  235. };
  236. Results.prototype.append = function (data) {
  237. var $options = [];
  238. for (var d = 0; d < data.length; d++) {
  239. var item = data[d];
  240. var $option = this.option(item);
  241. $options.push($option);
  242. }
  243. this.$results.append($options);
  244. };
  245. Results.prototype.setClasses = function () {
  246. var self = this;
  247. this.data.current(function (selected) {
  248. selected = $.map(selected, function (s) { return s.id; });
  249. self.$results.find('.option.selected').removeClass('selected');
  250. var $options = self.$results.find('.option');
  251. $options.each(function () {
  252. var $option = $(this);
  253. var item = $option.data('data');
  254. if (selected.indexOf(item.id) > -1) {
  255. $option.addClass('selected');
  256. }
  257. });
  258. });
  259. };
  260. Results.prototype.option = function (data) {
  261. var $option = $(
  262. '<li class="option"></li>'
  263. );
  264. $option.html(data.text);
  265. $option.data('data', data);
  266. return $option;
  267. };
  268. Results.prototype.bind = function (container, $container) {
  269. var self = this;
  270. this.on('results:all', function (data) {
  271. self.clear();
  272. self.append(data);
  273. self.setClasses();
  274. });
  275. this.on('results:append', function (data) {
  276. self.append(data);
  277. self.setClasses();
  278. });
  279. this.$results.on('mouseup', '.option', function (evt) {
  280. var $this = $(this);
  281. var data = $this.data('data');
  282. if ($this.hasClass('selected')) {
  283. self.trigger('unselected', {
  284. originalEvent: evt,
  285. data: data
  286. });
  287. self.setClasses();
  288. return;
  289. }
  290. self.trigger('selected', {
  291. originalEvent: evt,
  292. data: data
  293. });
  294. self.setClasses();
  295. });
  296. this.$results.on('mouseenter', '.option', function (evt) {
  297. self.$results.find('.option.highlighted').removeClass('highlighted');
  298. $(this).addClass('highlighted');
  299. });
  300. this.$results.on('mouseleave', '.option', function (evt) {
  301. $(this).removeClass('highlighted');
  302. });
  303. };
  304. return Results;
  305. });
  306. define('select2/dropdown',[
  307. './utils'
  308. ], function (Utils) {
  309. function Dropdown ($element, options) {
  310. this.$element = $element;
  311. }
  312. Utils.Extend(Dropdown, Utils.Observable);
  313. Dropdown.prototype.render = function () {
  314. var $dropdown = $(
  315. '<span class="dropdown">' +
  316. '<span class="results"></span>' +
  317. '</span>'
  318. );
  319. return $dropdown;
  320. };
  321. return Dropdown;
  322. });
  323. define('select2/selection/single',[
  324. '../utils'
  325. ], function (Utils) {
  326. function SingleSelection ($element, options) {
  327. this.$element = $element;
  328. this.options = options;
  329. SingleSelection.__super__.constructor.call(this);
  330. }
  331. Utils.Extend(SingleSelection, Utils.Observable);
  332. SingleSelection.prototype.render = function () {
  333. var $selection = $(
  334. '<span class="single-select">' +
  335. '<span class="rendered-selection"></span>' +
  336. '</span>'
  337. );
  338. this.$selection = $selection;
  339. return $selection;
  340. };
  341. SingleSelection.prototype.bind = function (container, $container) {
  342. var self = this;
  343. this.$selection.on('mousedown', function (evt) {
  344. // Only respond to left clicks
  345. if (evt.which !== 1) {
  346. return;
  347. }
  348. self.trigger('toggle', {
  349. originalEvent: evt
  350. });
  351. });
  352. container.on('selection:update', function (params) {
  353. self.update(params.data);
  354. });
  355. };
  356. SingleSelection.prototype.clear = function () {
  357. this.$selection.find('.rendered-selection').empty();
  358. };
  359. SingleSelection.prototype.display = function (data) {
  360. return data.text;
  361. };
  362. SingleSelection.prototype.update = function (data) {
  363. if (data.length === 0) {
  364. this.clear();
  365. return;
  366. }
  367. var selection = data[0];
  368. var formatted = this.display(selection);
  369. this.$selection.find('.rendered-selection').html(formatted);
  370. };
  371. return SingleSelection;
  372. });
  373. define('select2/selection/multiple',[
  374. '../utils'
  375. ], function (Utils) {
  376. function MultipleSelection ($element, options) {
  377. this.$element = $element;
  378. this.options = options;
  379. MultipleSelection.__super__.constructor.call(this);
  380. }
  381. Utils.Extend(MultipleSelection, Utils.Observable);
  382. MultipleSelection.prototype.render = function () {
  383. var $selection = $(
  384. '<span class="multiple-select">' +
  385. '<ul class="rendered-selection"></ul>' +
  386. '</span>'
  387. );
  388. this.$selection = $selection;
  389. return $selection;
  390. };
  391. MultipleSelection.prototype.bind = function (container, $container) {
  392. var self = this;
  393. this.$selection.on('click', function (evt) {
  394. self.trigger('toggle', {
  395. originalEvent: evt
  396. });
  397. });
  398. container.on('selection:update', function (params) {
  399. self.update(params.data);
  400. });
  401. };
  402. MultipleSelection.prototype.clear = function () {
  403. this.$selection.find('.rendered-selection').empty();
  404. };
  405. MultipleSelection.prototype.display = function (data) {
  406. return data.text;
  407. };
  408. MultipleSelection.prototype.update = function (data) {
  409. this.clear();
  410. if (data.length === 0) {
  411. return;
  412. }
  413. var $selections = [];
  414. for (var d = 0; d < data.length; d++) {
  415. var selection = data[d];
  416. var formatted = this.display(selection);
  417. var $selection = $('<ul class="choice"></ul>');
  418. $selection.text(formatted);
  419. $selection.data('data', data);
  420. $selections.push($selection);
  421. }
  422. this.$selection.find('.rendered-selection').append($selections);
  423. };
  424. return MultipleSelection;
  425. });
  426. define('select2/data/array',[
  427. './select',
  428. '../utils'
  429. ], function (SelectAdapter, Utils) {
  430. function ArrayAdapter ($element, options) {
  431. this.data = options.options.data;
  432. ArrayAdapter.__super__.constructor.call(this, $element, options);
  433. }
  434. Utils.Extend(ArrayAdapter, SelectAdapter);
  435. ArrayAdapter.prototype.select = function (data) {
  436. var self = this;
  437. this.$element.find('option').each(function () {
  438. var $option = $(this);
  439. var option = self.item($option);
  440. if (option.id == data.id) {
  441. $option.remove();
  442. }
  443. });
  444. var $option = this.option(data);
  445. this.$element.append($option);
  446. ArrayAdapter.__super__.select.call(this, data);
  447. };
  448. ArrayAdapter.prototype.option = function (data) {
  449. var $option = $('<option></option>');
  450. $option.text(data.text);
  451. $option.val(data.id);
  452. $option.data('data', data);
  453. return $option;
  454. };
  455. ArrayAdapter.prototype.query = function (params, callback) {
  456. var matches = [];
  457. var self = this;
  458. $.each(this.data, function () {
  459. var option = this;
  460. if (self.matches(params, option)) {
  461. matches.push(option);
  462. }
  463. });
  464. callback(matches);
  465. };
  466. return ArrayAdapter;
  467. });
  468. define('select2/data/ajax',[
  469. './array',
  470. '../utils',
  471. 'jquery'
  472. ], function (ArrayAdapter, Utils, $) {
  473. function AjaxAdapter ($element, options) {
  474. this.ajaxOptions = options.options.ajax;
  475. this.processResults = this.ajaxOptions.processResults ||
  476. function (results) {
  477. return results;
  478. };
  479. ArrayAdapter.__super__.constructor.call(this, $element, options);
  480. }
  481. Utils.Extend(AjaxAdapter, ArrayAdapter);
  482. AjaxAdapter.prototype.query = function (params, callback) {
  483. var matches = [];
  484. var self = this;
  485. var options = $.extend({
  486. type: 'GET'
  487. }, this.ajaxOptions);
  488. if (typeof options.url === 'function') {
  489. options.url = options.url(params);
  490. }
  491. if (typeof options.data === 'function') {
  492. options.data = options.data(params);
  493. }
  494. var $request = $.ajax(options);
  495. $request.success(function (data) {
  496. var results = self.processResults(data);
  497. callback(results);
  498. });
  499. };
  500. return AjaxAdapter;
  501. });
  502. define('select2/options',[
  503. './data/select',
  504. './results',
  505. './dropdown',
  506. './selection/single',
  507. './selection/multiple',
  508. './data/array',
  509. './data/ajax'
  510. ], function (SelectData, ResultsList, Dropdown, SingleSelection,
  511. MultipleSelection) {
  512. function Options (options) {
  513. this.options = options;
  514. this.dataAdapter = options.dataAdapter || SelectData;
  515. this.resultsAdapter = ResultsList;
  516. this.dropdownAdapter = options.dropdownAdapter || Dropdown;
  517. this.selectionAdapter = options.selectionAdapter;
  518. if (this.selectionAdapter == null) {
  519. if (this.options.multiple) {
  520. this.selectionAdapter = MultipleSelection;
  521. } else {
  522. this.selectionAdapter = SingleSelection;
  523. }
  524. }
  525. }
  526. return Options;
  527. });
  528. define('select2/core',[
  529. 'jquery',
  530. './options',
  531. './utils'
  532. ], function ($, Options, Utils) {
  533. var Select2 = function ($element, options) {
  534. this.$element = $element;
  535. options = options || {};
  536. options.multiple = options.multiple || $element.prop('multiple');
  537. this.options = new Options(options);
  538. Select2.__super__.constructor.call(this);
  539. // Set up containers and adapters
  540. this.data = new this.options.dataAdapter($element, this.options);
  541. var $container = this.render();
  542. this.$container = $container;
  543. $container.insertAfter(this.$element);
  544. $container.width($element.width());
  545. this.selection = new this.options.selectionAdapter($element, this.options);
  546. var $selectionContainer = $container.find('.selection');
  547. var $selection = this.selection.render();
  548. $selectionContainer.append($selection);
  549. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  550. var $dropdownContainer = $container.find('.dropdown-wrapper');
  551. var $dropdown = this.dropdown.render();
  552. $dropdownContainer.append($dropdown);
  553. this.results = new this.options.resultsAdapter(
  554. $element, this.options, this.data);
  555. var $resultsContainer = $dropdown.find('.results');
  556. var $results = this.results.render();
  557. $resultsContainer.append($results);
  558. // Bind events
  559. var self = this;
  560. this.data.bind(this, $container);
  561. this.selection.bind(this, $container);
  562. this.results.bind(this, $container);
  563. this.$element.on('change', function () {
  564. self.data.current(function (data) {
  565. self.trigger('selection:update', {
  566. data: data
  567. });
  568. });
  569. });
  570. this.selection.on('toggle', function () {
  571. self.toggleDropdown();
  572. });
  573. this.results.on('selected', function (params) {
  574. self.trigger('select', params);
  575. self.trigger('close');
  576. });
  577. this.results.on('unselected', function (params) {
  578. self.trigger('unselect', params);
  579. self.trigger('close');
  580. });
  581. this.on('open', function () {
  582. $container.addClass('open');
  583. });
  584. this.on('close', function () {
  585. $container.removeClass('open');
  586. });
  587. // Set the initial state
  588. this.data.current(function (initialData) {
  589. self.trigger('selection:update', {
  590. data: initialData
  591. });
  592. });
  593. this.data.query({}, function (data) {
  594. self.results.trigger('results:all', data);
  595. });
  596. // Hide the original select
  597. $element.hide();
  598. };
  599. Utils.Extend(Select2, Utils.Observable);
  600. Select2.prototype.toggleDropdown = function () {
  601. if (this.$container.hasClass('open')) {
  602. this.trigger('close');
  603. } else {
  604. this.trigger('open');
  605. }
  606. };
  607. Select2.prototype.render = function () {
  608. var $container = $(
  609. '<span class="select2 select2-container select2-theme-default">' +
  610. '<span class="selection"></span>' +
  611. '<span class="dropdown-wrapper"></span>' +
  612. '</span>'
  613. );
  614. return $container;
  615. };
  616. return Select2;
  617. });