select2.amd.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. self.trigger("toggle", {
  330. originalEvent: evt
  331. });
  332. });
  333. container.on("selection:update", function (params) {
  334. self.update(params.data);
  335. })
  336. }
  337. SingleSelection.prototype.clear = function () {
  338. this.$selection.find(".rendered-selection").empty();
  339. }
  340. SingleSelection.prototype.display = function (data) {
  341. return data.text;
  342. }
  343. SingleSelection.prototype.update = function (data) {
  344. if (data.length == 0) {
  345. this.clear();
  346. return;
  347. }
  348. var selection = data[0];
  349. var formatted = this.display(selection);
  350. this.$selection.find(".rendered-selection").html(formatted);
  351. }
  352. return SingleSelection;
  353. });
  354. define('select2/selection/multiple',[
  355. '../utils'
  356. ], function (Utils) {
  357. function MultipleSelection ($element, options) {
  358. this.$element = $element;
  359. this.options = options;
  360. MultipleSelection.__super__.constructor.call(this);
  361. }
  362. Utils.Extend(MultipleSelection, Utils.Observable);
  363. MultipleSelection.prototype.render = function () {
  364. var $selection = $(
  365. '<span class="multiple-select">' +
  366. '<ul class="rendered-selection"></ul>' +
  367. '</span>'
  368. );
  369. this.$selection = $selection;
  370. return $selection;
  371. }
  372. MultipleSelection.prototype.bind = function (container, $container) {
  373. var self = this;
  374. this.$selection.on('click', function (evt) {
  375. self.trigger("toggle", {
  376. originalEvent: evt
  377. });
  378. });
  379. container.on("selection:update", function (params) {
  380. self.update(params.data);
  381. });
  382. }
  383. MultipleSelection.prototype.clear = function () {
  384. this.$selection.find(".rendered-selection").empty();
  385. }
  386. MultipleSelection.prototype.display = function (data) {
  387. return data.text;
  388. }
  389. MultipleSelection.prototype.update = function (data) {
  390. this.clear();
  391. if (data.length == 0) {
  392. return;
  393. }
  394. var $selections = [];
  395. for (var d = 0; d < data.length; d++) {
  396. var selection = data[d];
  397. var formatted = this.display(selection);
  398. var $selection = $('<ul class="choice"></ul>');
  399. $selection.text(formatted);
  400. $selection.data("data", data);
  401. $selections.push($selection);
  402. }
  403. this.$selection.find(".rendered-selection").append($selections);
  404. }
  405. return MultipleSelection;
  406. });
  407. define('select2/options',[
  408. './data/select',
  409. './results',
  410. './dropdown',
  411. './selection/single',
  412. './selection/multiple'
  413. ], function (SelectData, ResultsList, Dropdown, SingleSelection,
  414. MultipleSelection) {
  415. function Options (options) {
  416. this.options = options;
  417. this.dataAdapter = options.dataAdapter || SelectData;
  418. this.resultsAdapter = ResultsList;
  419. this.dropdownAdapter = options.dropdownAdapter || Dropdown;
  420. this.selectionAdapter = options.selectionAdapter;
  421. if (this.selectionAdapter == null) {
  422. if (this.options.multiple) {
  423. this.selectionAdapter = MultipleSelection;
  424. } else {
  425. this.selectionAdapter = SingleSelection;
  426. }
  427. }
  428. }
  429. return Options;
  430. })
  431. ;
  432. define('select2/core',[
  433. 'jquery',
  434. './options',
  435. './utils'
  436. ], function ($, Options, Utils) {
  437. var Select2 = function ($element, options) {
  438. this.$element = $element;
  439. options = options || {};
  440. options.multiple = options.multiple || $element.prop("multiple");
  441. this.options = new Options(options);
  442. Select2.__super__.constructor.call(this);
  443. // Set up containers and adapters
  444. this.data = new this.options.dataAdapter($element, this.options);
  445. var $container = this.render();
  446. $container.insertAfter(this.$element);
  447. $container.width($element.width());
  448. this.selection = new this.options.selectionAdapter($element, this.options);
  449. var $selectionContainer = $container.find(".selection");
  450. var $selection = this.selection.render();
  451. $selectionContainer.append($selection);
  452. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  453. var $dropdownContainer = $container.find(".dropdown");
  454. var $dropdown = this.dropdown.render();
  455. $dropdownContainer.append($dropdown);
  456. this.results = new this.options.resultsAdapter($element, this.options, this.data);
  457. var $resultsContainer = $dropdown.find(".results");
  458. var $results = this.results.render();
  459. $resultsContainer.append($results);
  460. // Bind events
  461. var self = this;
  462. this.data.bind(this, $container);
  463. this.selection.bind(this, $container);
  464. this.results.bind(this, $container);
  465. this.$element.on("change", function () {
  466. self.data.current(function (data) {
  467. self.trigger("selection:update", {
  468. data: data
  469. });
  470. });
  471. });
  472. this.selection.on("toggle", function () {
  473. $container.toggleClass("open");
  474. });
  475. this.results.on("selected", function (params) {
  476. self.trigger("select", params);
  477. $container.removeClass("open");
  478. });
  479. this.results.on("unselected", function (params) {
  480. self.trigger("unselect", params);
  481. $container.removeClass("open");
  482. });
  483. // Set the initial state
  484. this.data.current(function (initialData) {
  485. self.trigger("selection:update", {
  486. data: initialData
  487. });
  488. });
  489. this.data.query({}, function (data) {
  490. self.results.trigger("results:all", data);
  491. });
  492. // Hide the original select
  493. $element.hide();
  494. };
  495. Utils.Extend(Select2, Utils.Observable);
  496. Select2.prototype.render = function () {
  497. var $container = $(
  498. '<span class="select2 select2-container select2-theme-default">' +
  499. '<span class="selection"></span>' +
  500. '<span class="dropdown"></span>' +
  501. '</span>'
  502. );
  503. return $container;
  504. };
  505. return Select2;
  506. });