select2.amd.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. ], function (Utils) {
  48. function SelectAdapter ($element, options) {
  49. this.$element = $element;
  50. }
  51. Utils.Extend(SelectAdapter, Utils.Observable);
  52. SelectAdapter.prototype.current = function (callback) {
  53. var data = [];
  54. var self = this;
  55. this.$element.find(":selected").each(function () {
  56. var $option = $(this);
  57. var option = self.item($option);
  58. data.push(option);
  59. });
  60. callback(data);
  61. };
  62. SelectAdapter.prototype.query = function (params, callback) {
  63. var data = [];
  64. var self = this;
  65. this.$element.find("option").each(function () {
  66. var $option = $(this);
  67. var option = self.item($option);
  68. if (self.matches(params, option)) {
  69. data.push(option);
  70. }
  71. });
  72. callback(data);
  73. };
  74. SelectAdapter.prototype.item = function ($option) {
  75. var data = {
  76. id: $option.val(),
  77. text: $option.html()
  78. };
  79. return data;
  80. };
  81. SelectAdapter.prototype.matches = function (params, data) {
  82. if (data.text.indexOf(params.term) > -1) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. return SelectAdapter;
  88. });
  89. define('select2/results',[
  90. './utils'
  91. ], function (Utils) {
  92. function Results ($element, dataAdapter) {
  93. this.$element = $element;
  94. this.dataAdapter = dataAdapter;
  95. }
  96. Utils.Extend(Results, Utils.Observable);
  97. Results.prototype.render = function () {
  98. var $results = $(
  99. '<ul class="options"></ul>'
  100. );
  101. return $results;
  102. }
  103. return Results;
  104. })
  105. ;
  106. define('select2/dropdown',[
  107. './utils'
  108. ], function (Utils) {
  109. function Dropdown ($element, options) {
  110. this.$element = $element;
  111. }
  112. Utils.Extend(Dropdown, Utils.Observable);
  113. Dropdown.prototype.render = function () {
  114. var $dropdown = $(
  115. '<span class="">' +
  116. '<span class="results"></span>' +
  117. '</span>'
  118. );
  119. return $dropdown;
  120. }
  121. return Dropdown;
  122. })
  123. ;
  124. define('select2/selection',[
  125. './utils'
  126. ], function (Utils) {
  127. function Selection ($element, options) {
  128. this.$element = $element;
  129. this.options = options;
  130. Selection.__super__.constructor.call(this);
  131. }
  132. Utils.Extend(Selection, Utils.Observable);
  133. Selection.prototype.render = function () {
  134. var $selection = $(
  135. '<span class="single-select">' +
  136. '<span class="rendered-selection"></span>' +
  137. '</span>'
  138. );
  139. this.$selection = $selection;
  140. return $selection;
  141. }
  142. Selection.prototype.bind = function ($container) {
  143. var self = this;
  144. this.$selection.on('click', function (evt) {
  145. self.trigger("toggle", {
  146. originalEvent: evt
  147. });
  148. });
  149. }
  150. Selection.prototype.clear = function () {
  151. this.$selection.find(".rendered-selection").text("");
  152. }
  153. Selection.prototype.display = function (data) {
  154. return data.text;
  155. }
  156. Selection.prototype.update = function (data) {
  157. if (data.length == 0) {
  158. this.clear();
  159. return;
  160. }
  161. var selection = data[0];
  162. var formatted = this.display(selection);
  163. this.$selection.find(".rendered-selection").html(formatted);
  164. }
  165. return Selection;
  166. });
  167. define('select2/options',[
  168. './data/select',
  169. './results',
  170. './dropdown',
  171. './selection'
  172. ], function (SelectData, ResultsList, Dropdown, Selection) {
  173. function Options (options) {
  174. this.options = options;
  175. this.dataAdapter = SelectData;
  176. this.resultsAdapter = ResultsList;
  177. this.dropdownAdapter = Dropdown;
  178. this.selectionAdapter = Selection;
  179. }
  180. return Options;
  181. })
  182. ;
  183. define('select2/core',[
  184. 'jquery',
  185. './options',
  186. './utils'
  187. ], function ($, Options, Utils) {
  188. var Select2 = function ($element, options) {
  189. this.$element = $element;
  190. this.options = new Options(options);
  191. Select2.__super__.constructor.call(this);
  192. // Set up containers and adapters
  193. this.data = new this.options.dataAdapter($element, options);
  194. var $container = this.render();
  195. $container.insertAfter(this.$element);
  196. $container.width($element.width());
  197. this.selection = new this.options.selectionAdapter($element, options);
  198. var $selectionContainer = $container.find(".selection");
  199. var $selection = this.selection.render();
  200. $selectionContainer.append($selection);
  201. this.dropdown = new this.options.dropdownAdapter($element, options);
  202. var $dropdownContainer = $container.find(".dropdown");
  203. var $dropdown = this.dropdown.render();
  204. $dropdownContainer.append($dropdown);
  205. this.results = new this.options.resultsAdapter($element, options);
  206. var $resultsContainer = $dropdown.find(".results");
  207. var $results = this.results.render();
  208. $resultsContainer.append($results);
  209. // Bind events
  210. this.selection.bind($container);
  211. // Set the initial state
  212. var self = this;
  213. this.data.current(function (initialData) {
  214. self.selection.update(initialData);
  215. });
  216. this.$element.on("change", function () {
  217. self.data.current(function (data) {
  218. self.selection.update(data);
  219. });
  220. });
  221. this.selection.on("toggle", function () {
  222. $container.toggleClass("open");
  223. });
  224. };
  225. Utils.Extend(Select2, Utils.Observable);
  226. Select2.prototype.render = function () {
  227. var $container = $(
  228. '<span class="select2 select2-container">' +
  229. '<span class="selection"></span>' +
  230. '<span class="dropdown"></span>' +
  231. '</span>'
  232. );
  233. return $container;
  234. };
  235. return Select2;
  236. });