select2.amd.js 14 KB

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