select2.amd.js 18 KB

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