select2.amd.js 21 KB

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