select2.amd.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. var Observable = function () {
  19. this.listeners = {};
  20. };
  21. Observable.prototype.on = function (event, callback) {
  22. if (event in this.listeners) {
  23. this.listeners[event].push(callback);
  24. } else {
  25. this.listeners[event] = [callback];
  26. }
  27. };
  28. Observable.prototype.trigger = function (event) {
  29. var slice = Array.prototype.slice;
  30. if (event in this.listeners) {
  31. this.invoke(this.listeners[event], slice.call(arguments, 1));
  32. }
  33. if ("*" in this.listeners) {
  34. this.invoke(this.listeners["*"], arguments);
  35. }
  36. };
  37. Observable.prototype.invoke = function (listeners, params) {
  38. for (var i = 0, len = listeners.length; i < len; i++) {
  39. listeners[i].apply(this, params);
  40. }
  41. };
  42. Utils.Observable = Observable;
  43. return Utils;
  44. });
  45. define('select2/data/select',[
  46. '../utils',
  47. 'jquery'
  48. ], function (Utils, $) {
  49. function SelectAdapter ($element, options) {
  50. this.$element = $element;
  51. SelectAdapter.__super__.constructor.call(this);
  52. }
  53. Utils.Extend(SelectAdapter, Utils.Observable);
  54. SelectAdapter.prototype.current = function (callback) {
  55. var data = [];
  56. var self = this;
  57. this.$element.find(":selected").each(function () {
  58. var $option = $(this);
  59. var option = self.item($option);
  60. data.push(option);
  61. });
  62. callback(data);
  63. };
  64. SelectAdapter.prototype.select = function (data) {
  65. var val;
  66. if (this.$element.prop("multiple")) {
  67. var currentData = this.current();
  68. data = [data];
  69. data.push(currentData);
  70. val = [];
  71. for (var d = 0; d < data.length; d++) {
  72. id = data[d].id;
  73. if (ids.indexOf(id) === -1) {
  74. val.push(id);
  75. }
  76. }
  77. } else {
  78. val = data.id;
  79. }
  80. this.$element.val(val);
  81. this.$element.trigger("change");
  82. }
  83. SelectAdapter.prototype.query = function (params, callback) {
  84. var data = [];
  85. var self = this;
  86. this.$element.find("option").each(function () {
  87. var $option = $(this);
  88. var option = self.item($option);
  89. if (self.matches(params, option)) {
  90. data.push(option);
  91. }
  92. });
  93. callback(data);
  94. };
  95. SelectAdapter.prototype.item = function ($option) {
  96. var data = {
  97. id: $option.val(),
  98. text: $option.html()
  99. };
  100. return data;
  101. };
  102. SelectAdapter.prototype.matches = function (params, data) {
  103. if ($.trim(params.term) == "") {
  104. return true;
  105. }
  106. if (data.text.indexOf(params.term) > -1) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. return SelectAdapter;
  112. });
  113. define('select2/results',[
  114. './utils'
  115. ], function (Utils) {
  116. function Results ($element, options, dataAdapter) {
  117. this.$element = $element;
  118. this.data = dataAdapter;
  119. Results.__super__.constructor.call(this);
  120. }
  121. Utils.Extend(Results, Utils.Observable);
  122. Results.prototype.render = function () {
  123. var $results = $(
  124. '<ul class="options"></ul>'
  125. );
  126. this.$results = $results;
  127. return $results;
  128. };
  129. Results.prototype.clear = function () {
  130. this.$results.empty();
  131. };
  132. Results.prototype.append = function (data) {
  133. var $options = [];
  134. for (var d = 0; d < data.length; d++) {
  135. var item = data[d];
  136. var $option = this.option(item);
  137. $options.push($option);
  138. }
  139. this.$results.append($options);
  140. };
  141. Results.prototype.setClasses = function () {
  142. var self = this;
  143. this.data.current(function (selected) {
  144. selected = $.map(selected, function (s) { return s.id; });
  145. self.$results.find(".option.selected").removeClass("selected");
  146. var $options = self.$results.find(".option");
  147. console.log($options);
  148. $options.each(function () {
  149. var $option = $(this);
  150. var item = $option.data("data");
  151. if (selected.indexOf(item.id) > -1) {
  152. $option.addClass("selected");
  153. }
  154. });
  155. });
  156. };
  157. Results.prototype.option = function (data) {
  158. var $option = $(
  159. '<li class="option"></li>'
  160. );
  161. $option.html(data.text);
  162. $option.data("data", data);
  163. return $option;
  164. }
  165. Results.prototype.bind = function ($container) {
  166. var self = this;
  167. this.on("results:all", function (data) {
  168. self.clear();
  169. self.append(data);
  170. self.setClasses();
  171. });
  172. this.on("results:append", function (data) {
  173. self.append(data);
  174. self.setClasses();
  175. })
  176. this.$results.on("click", ".option", function (evt) {
  177. var data = $(this).data("data");
  178. self.trigger("selected", {
  179. originalEvent: evt,
  180. data: data
  181. });
  182. self.setClasses();
  183. });
  184. this.$results.on("mouseenter", ".option", function (evt) {
  185. self.$results.find(".option.hovered").removeClass("hovered");
  186. $(this).addClass("hovered");
  187. });
  188. this.$results.on("mouseleave", ".option", function (evt) {
  189. $(this).removeClass("hovered");
  190. });
  191. };
  192. return Results;
  193. })
  194. ;
  195. define('select2/dropdown',[
  196. './utils'
  197. ], function (Utils) {
  198. function Dropdown ($element, options) {
  199. this.$element = $element;
  200. }
  201. Utils.Extend(Dropdown, Utils.Observable);
  202. Dropdown.prototype.render = function () {
  203. var $dropdown = $(
  204. '<span class="">' +
  205. '<span class="results"></span>' +
  206. '</span>'
  207. );
  208. return $dropdown;
  209. }
  210. return Dropdown;
  211. })
  212. ;
  213. define('select2/selection',[
  214. './utils'
  215. ], function (Utils) {
  216. function Selection ($element, options) {
  217. this.$element = $element;
  218. this.options = options;
  219. Selection.__super__.constructor.call(this);
  220. }
  221. Utils.Extend(Selection, Utils.Observable);
  222. Selection.prototype.render = function () {
  223. var $selection = $(
  224. '<span class="single-select">' +
  225. '<span class="rendered-selection"></span>' +
  226. '</span>'
  227. );
  228. this.$selection = $selection;
  229. return $selection;
  230. }
  231. Selection.prototype.bind = function ($container) {
  232. var self = this;
  233. this.$selection.on('click', function (evt) {
  234. self.trigger("toggle", {
  235. originalEvent: evt
  236. });
  237. });
  238. }
  239. Selection.prototype.clear = function () {
  240. this.$selection.find(".rendered-selection").text("");
  241. }
  242. Selection.prototype.display = function (data) {
  243. return data.text;
  244. }
  245. Selection.prototype.update = function (data) {
  246. if (data.length == 0) {
  247. this.clear();
  248. return;
  249. }
  250. var selection = data[0];
  251. var formatted = this.display(selection);
  252. this.$selection.find(".rendered-selection").html(formatted);
  253. }
  254. return Selection;
  255. });
  256. define('select2/options',[
  257. './data/select',
  258. './results',
  259. './dropdown',
  260. './selection'
  261. ], function (SelectData, ResultsList, Dropdown, Selection) {
  262. function Options (options) {
  263. this.options = options;
  264. this.dataAdapter = SelectData;
  265. this.resultsAdapter = ResultsList;
  266. this.dropdownAdapter = Dropdown;
  267. this.selectionAdapter = Selection;
  268. }
  269. return Options;
  270. })
  271. ;
  272. define('select2/core',[
  273. 'jquery',
  274. './options',
  275. './utils'
  276. ], function ($, Options, Utils) {
  277. var Select2 = function ($element, options) {
  278. this.$element = $element;
  279. this.options = new Options(options);
  280. Select2.__super__.constructor.call(this);
  281. // Set up containers and adapters
  282. this.data = new this.options.dataAdapter($element, this.options);
  283. var $container = this.render();
  284. $container.insertAfter(this.$element);
  285. $container.width($element.width());
  286. this.selection = new this.options.selectionAdapter($element, this.options);
  287. var $selectionContainer = $container.find(".selection");
  288. var $selection = this.selection.render();
  289. $selectionContainer.append($selection);
  290. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  291. var $dropdownContainer = $container.find(".dropdown");
  292. var $dropdown = this.dropdown.render();
  293. $dropdownContainer.append($dropdown);
  294. this.results = new this.options.resultsAdapter($element, this.options, this.data);
  295. var $resultsContainer = $dropdown.find(".results");
  296. var $results = this.results.render();
  297. $resultsContainer.append($results);
  298. // Bind events
  299. var self = this;
  300. this.selection.bind($container);
  301. this.results.bind($container);
  302. this.$element.on("change", function () {
  303. self.data.current(function (data) {
  304. self.selection.update(data);
  305. });
  306. });
  307. this.selection.on("toggle", function () {
  308. $container.toggleClass("open");
  309. });
  310. this.results.on("selected", function (params) {
  311. self.data.select(params.data);
  312. $container.removeClass("open");
  313. });
  314. // Set the initial state
  315. this.data.current(function (initialData) {
  316. self.selection.update(initialData);
  317. });
  318. this.data.query({}, function (data) {
  319. self.results.trigger("results:all", data);
  320. });
  321. };
  322. Utils.Extend(Select2, Utils.Observable);
  323. Select2.prototype.render = function () {
  324. var $container = $(
  325. '<span class="select2 select2-container">' +
  326. '<span class="selection"></span>' +
  327. '<span class="dropdown"></span>' +
  328. '</span>'
  329. );
  330. return $container;
  331. };
  332. return Select2;
  333. });