select2.amd.js 18 KB

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