select2.amd.js 19 KB

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