select2.amd.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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.query = function (params, callback) {
  140. var data = [];
  141. var self = this;
  142. this.$element.find("option").each(function () {
  143. var $option = $(this);
  144. var option = self.item($option);
  145. if (self.matches(params, option)) {
  146. data.push(option);
  147. }
  148. });
  149. callback(data);
  150. };
  151. SelectAdapter.prototype.item = function ($option) {
  152. var data = {
  153. id: $option.val(),
  154. text: $option.html()
  155. };
  156. return data;
  157. };
  158. SelectAdapter.prototype.matches = function (params, data) {
  159. if ($.trim(params.term) == "") {
  160. return true;
  161. }
  162. if (data.text.indexOf(params.term) > -1) {
  163. return true;
  164. }
  165. return false;
  166. }
  167. return SelectAdapter;
  168. });
  169. define('select2/results',[
  170. './utils'
  171. ], function (Utils) {
  172. function Results ($element, options, dataAdapter) {
  173. this.$element = $element;
  174. this.data = dataAdapter;
  175. Results.__super__.constructor.call(this);
  176. }
  177. Utils.Extend(Results, Utils.Observable);
  178. Results.prototype.render = function () {
  179. var $results = $(
  180. '<ul class="options"></ul>'
  181. );
  182. this.$results = $results;
  183. return $results;
  184. };
  185. Results.prototype.clear = function () {
  186. this.$results.empty();
  187. };
  188. Results.prototype.append = function (data) {
  189. var $options = [];
  190. for (var d = 0; d < data.length; d++) {
  191. var item = data[d];
  192. var $option = this.option(item);
  193. $options.push($option);
  194. }
  195. this.$results.append($options);
  196. };
  197. Results.prototype.setClasses = function () {
  198. var self = this;
  199. this.data.current(function (selected) {
  200. selected = $.map(selected, function (s) { return s.id; });
  201. self.$results.find(".option.selected").removeClass("selected");
  202. var $options = self.$results.find(".option");
  203. $options.each(function () {
  204. var $option = $(this);
  205. var item = $option.data("data");
  206. if (selected.indexOf(item.id) > -1) {
  207. $option.addClass("selected");
  208. }
  209. });
  210. });
  211. };
  212. Results.prototype.option = function (data) {
  213. var $option = $(
  214. '<li class="option"></li>'
  215. );
  216. $option.html(data.text);
  217. $option.data("data", data);
  218. return $option;
  219. }
  220. Results.prototype.bind = function ($container) {
  221. var self = this;
  222. this.on("results:all", function (data) {
  223. self.clear();
  224. self.append(data);
  225. self.setClasses();
  226. });
  227. this.on("results:append", function (data) {
  228. self.append(data);
  229. self.setClasses();
  230. })
  231. this.$results.on("click", ".option", function (evt) {
  232. var data = $(this).data("data");
  233. self.trigger("selected", {
  234. originalEvent: evt,
  235. data: data
  236. });
  237. self.setClasses();
  238. });
  239. this.$results.on("mouseenter", ".option", function (evt) {
  240. self.$results.find(".option.highlighted").removeClass("highlighted");
  241. $(this).addClass("highlighted");
  242. });
  243. this.$results.on("mouseleave", ".option", function (evt) {
  244. $(this).removeClass("highlighted");
  245. });
  246. };
  247. return Results;
  248. })
  249. ;
  250. define('select2/dropdown',[
  251. './utils'
  252. ], function (Utils) {
  253. function Dropdown ($element, options) {
  254. this.$element = $element;
  255. }
  256. Utils.Extend(Dropdown, Utils.Observable);
  257. Dropdown.prototype.render = function () {
  258. var $dropdown = $(
  259. '<span class="">' +
  260. '<span class="results"></span>' +
  261. '</span>'
  262. );
  263. return $dropdown;
  264. }
  265. return Dropdown;
  266. })
  267. ;
  268. define('select2/selection/single',[
  269. '../utils'
  270. ], function (Utils) {
  271. function SingleSelection ($element, options) {
  272. this.$element = $element;
  273. this.options = options;
  274. SingleSelection.__super__.constructor.call(this);
  275. }
  276. Utils.Extend(SingleSelection, Utils.Observable);
  277. SingleSelection.prototype.render = function () {
  278. var $selection = $(
  279. '<span class="single-select">' +
  280. '<span class="rendered-selection"></span>' +
  281. '</span>'
  282. );
  283. this.$selection = $selection;
  284. return $selection;
  285. }
  286. SingleSelection.prototype.bind = function ($container) {
  287. var self = this;
  288. this.$selection.on('click', function (evt) {
  289. self.trigger("toggle", {
  290. originalEvent: evt
  291. });
  292. });
  293. }
  294. SingleSelection.prototype.clear = function () {
  295. this.$selection.find(".rendered-selection").empty();
  296. }
  297. SingleSelection.prototype.display = function (data) {
  298. return data.text;
  299. }
  300. SingleSelection.prototype.update = function (data) {
  301. if (data.length == 0) {
  302. this.clear();
  303. return;
  304. }
  305. var selection = data[0];
  306. var formatted = this.display(selection);
  307. this.$selection.find(".rendered-selection").html(formatted);
  308. }
  309. return SingleSelection;
  310. });
  311. define('select2/selection/multiple',[
  312. '../utils'
  313. ], function (Utils) {
  314. function MultipleSelection ($element, options) {
  315. this.$element = $element;
  316. this.options = options;
  317. MultipleSelection.__super__.constructor.call(this);
  318. }
  319. Utils.Extend(MultipleSelection, Utils.Observable);
  320. MultipleSelection.prototype.render = function () {
  321. var $selection = $(
  322. '<span class="multiple-select">' +
  323. '<ul class="rendered-selection"></ul>' +
  324. '</span>'
  325. );
  326. this.$selection = $selection;
  327. return $selection;
  328. }
  329. MultipleSelection.prototype.bind = function ($container) {
  330. var self = this;
  331. this.$selection.on('click', function (evt) {
  332. self.trigger("toggle", {
  333. originalEvent: evt
  334. });
  335. });
  336. }
  337. MultipleSelection.prototype.clear = function () {
  338. this.$selection.find(".rendered-selection").empty();
  339. }
  340. MultipleSelection.prototype.display = function (data) {
  341. return data.text;
  342. }
  343. MultipleSelection.prototype.update = function (data) {
  344. this.clear();
  345. if (data.length == 0) {
  346. return;
  347. }
  348. var $selections = [];
  349. for (var d = 0; d < data.length; d++) {
  350. var selection = data[d];
  351. var formatted = this.display(selection);
  352. var $selection = $('<ul class="choice"></ul>');
  353. $selection.text(formatted);
  354. $selection.data("data", data);
  355. $selections.push($selection);
  356. }
  357. this.$selection.find(".rendered-selection").append($selections);
  358. }
  359. return MultipleSelection;
  360. });
  361. define('select2/options',[
  362. './data/select',
  363. './results',
  364. './dropdown',
  365. './selection/single',
  366. './selection/multiple'
  367. ], function (SelectData, ResultsList, Dropdown, SingleSelection,
  368. MultipleSelection) {
  369. function Options (options) {
  370. this.options = options;
  371. this.dataAdapter = SelectData;
  372. this.resultsAdapter = ResultsList;
  373. this.dropdownAdapter = Dropdown;
  374. this.selectionAdapter = options.selectionAdapter;
  375. if (this.selectionAdapter == null) {
  376. if (this.options.multiple) {
  377. this.selectionAdapter = MultipleSelection;
  378. } else {
  379. this.selectionAdapter = SingleSelection;
  380. }
  381. }
  382. }
  383. return Options;
  384. })
  385. ;
  386. define('select2/core',[
  387. 'jquery',
  388. './options',
  389. './utils'
  390. ], function ($, Options, Utils) {
  391. var Select2 = function ($element, options) {
  392. this.$element = $element;
  393. options = options || {};
  394. options.multiple = options.multiple || $element.prop("multiple");
  395. this.options = new Options(options);
  396. Select2.__super__.constructor.call(this);
  397. // Set up containers and adapters
  398. this.data = new this.options.dataAdapter($element, this.options);
  399. var $container = this.render();
  400. $container.insertAfter(this.$element);
  401. $container.width($element.width());
  402. this.selection = new this.options.selectionAdapter($element, this.options);
  403. var $selectionContainer = $container.find(".selection");
  404. var $selection = this.selection.render();
  405. $selectionContainer.append($selection);
  406. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  407. var $dropdownContainer = $container.find(".dropdown");
  408. var $dropdown = this.dropdown.render();
  409. $dropdownContainer.append($dropdown);
  410. this.results = new this.options.resultsAdapter($element, this.options, this.data);
  411. var $resultsContainer = $dropdown.find(".results");
  412. var $results = this.results.render();
  413. $resultsContainer.append($results);
  414. // Bind events
  415. var self = this;
  416. this.selection.bind($container);
  417. this.results.bind($container);
  418. this.$element.on("change", function () {
  419. self.data.current(function (data) {
  420. self.selection.update(data);
  421. });
  422. });
  423. this.selection.on("toggle", function () {
  424. $container.toggleClass("open");
  425. });
  426. this.results.on("selected", function (params) {
  427. self.data.select(params.data);
  428. $container.removeClass("open");
  429. });
  430. // Set the initial state
  431. this.data.current(function (initialData) {
  432. self.selection.update(initialData);
  433. });
  434. this.data.query({}, function (data) {
  435. self.results.trigger("results:all", data);
  436. });
  437. };
  438. Utils.Extend(Select2, Utils.Observable);
  439. Select2.prototype.render = function () {
  440. var $container = $(
  441. '<span class="select2 select2-container select2-theme-default">' +
  442. '<span class="selection"></span>' +
  443. '<span class="dropdown"></span>' +
  444. '</span>'
  445. );
  446. return $container;
  447. };
  448. return Select2;
  449. });