select2.amd.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. if (event in this.listeners) {
  30. this.invoke(this.listeners[event], util.shift(arguments));
  31. }
  32. if ("*" in this.listeners) {
  33. this.invoke(this.listeners["*"], arguments);
  34. }
  35. };
  36. Observable.prototype.invoke = function (listeners, params) {
  37. for (var i = 0, len = listeners.length; i < len; i++) {
  38. listeners[i].apply(this, params);
  39. }
  40. };
  41. Utils.Observable = Observable;
  42. return Utils;
  43. });
  44. define('select2/data/select',[
  45. '../utils'
  46. ], function (Utils) {
  47. function SelectAdapter ($element, options) {
  48. this.$element = $element;
  49. }
  50. Utils.Extend(SelectAdapter, Utils.Observable);
  51. SelectAdapter.prototype.current = function () {
  52. var data = [];
  53. var self = this;
  54. this.$element.find(":selected").each(function () {
  55. var $option = $(this);
  56. var option = self.item($option);
  57. data.push(option);
  58. });
  59. return data;
  60. };
  61. SelectAdapter.prototype.item = function ($option) {
  62. var data = {
  63. id: $option.val(),
  64. text: $option.html()
  65. };
  66. return data;
  67. };
  68. return SelectAdapter;
  69. });
  70. define('select2/results',[
  71. './utils'
  72. ], function (Utils) {
  73. function Results ($element, dataAdapter) {
  74. this.$element = $element;
  75. this.dataAdapter = dataAdapter;
  76. }
  77. Utils.Extend(Results, Utils.Observable);
  78. Results.prototype.render = function () {
  79. var $results = $(
  80. '<ul class="options"></ul>'
  81. );
  82. return $results;
  83. }
  84. return Results;
  85. })
  86. ;
  87. define('select2/dropdown',[
  88. './utils'
  89. ], function (Utils) {
  90. function Dropdown ($element, options) {
  91. this.$element = $element;
  92. }
  93. Utils.Extend(Dropdown, Utils.Observable);
  94. Dropdown.prototype.render = function () {
  95. var $dropdown = $(
  96. '<div class="select2 select2-dropdown">' +
  97. '<div class="results"></div>' +
  98. '</div>'
  99. );
  100. return $dropdown;
  101. }
  102. return Dropdown;
  103. })
  104. ;
  105. define('select2/selection',[
  106. './utils'
  107. ], function (Utils) {
  108. function Selection ($element, options) {
  109. this.$element = $element;
  110. this.options = options;
  111. }
  112. Utils.Extend(Selection, Utils.Observable);
  113. Selection.prototype.render = function () {
  114. var $selection = $(
  115. '<div class="single-select">' +
  116. '<div class="rendered-selection"></div>' +
  117. '</div>'
  118. );
  119. this.$selection = $selection;
  120. return $selection;
  121. }
  122. Selection.prototype.bind = function ($container) {
  123. var self = this;
  124. $container.on('click', function (evt) {
  125. self.trigger("toggle", {
  126. originalEvent: evt
  127. });
  128. });
  129. }
  130. Selection.prototype.clear = function () {
  131. this.$selection.find(".rendered-selection").text("");
  132. }
  133. Selection.prototype.display = function (data) {
  134. return data.text;
  135. }
  136. Selection.prototype.update = function (data) {
  137. if (data.length == 0) {
  138. this.clear();
  139. return;
  140. }
  141. var selection = data[0];
  142. var formatted = this.display(selection);
  143. this.$selection.find(".rendered-selection").html(formatted);
  144. }
  145. return Selection;
  146. });
  147. define('select2/options',[
  148. './data/select',
  149. './results',
  150. './dropdown',
  151. './selection'
  152. ], function (SelectData, ResultsList, Dropdown, Selection) {
  153. function Options (options) {
  154. this.options = options;
  155. this.dataAdapter = SelectData;
  156. this.resultsAdapter = ResultsList;
  157. this.dropdownAdapter = Dropdown;
  158. this.selectionAdapter = Selection;
  159. }
  160. return Options;
  161. })
  162. ;
  163. define('select2/core',[
  164. 'jquery',
  165. './options',
  166. './utils'
  167. ], function ($, Options, Utils) {
  168. var Select2 = function ($element, options) {
  169. this.$element = $element;
  170. this.options = new Options(options);
  171. // Set up containers and adapters
  172. this.data = new this.options.dataAdapter($element, options);
  173. var $container = this.render();
  174. $container.insertAfter(this.$element);
  175. this.selection = new this.options.selectionAdapter($element, options);
  176. var $selectionContainer = $container.find(".selection");
  177. var $selection = this.selection.render();
  178. $selectionContainer.append($selection);
  179. this.dropdown = new this.options.dropdownAdapter($element, options);
  180. var $dropdown = this.dropdown.render();
  181. $dropdown.insertAfter($container);
  182. this.results = new this.options.resultsAdapter($element, options);
  183. var $resultsContainer = $dropdown.find(".results");
  184. var $results = this.results.render();
  185. $resultsContainer.append($results);
  186. // Set the initial state
  187. var initialData = this.data.current();
  188. this.selection.update(initialData);
  189. var self = this;
  190. this.$element.on("change", function () {
  191. self.selection.update(self.data.current());
  192. })
  193. };
  194. Utils.Extend(Select2, Utils.Observable);
  195. Select2.prototype.render = function () {
  196. var $container = $(
  197. '<div class="select2 select2-container">' +
  198. '<div class="selection"></div>' +
  199. '</div>'
  200. );
  201. return $container;
  202. };
  203. return Select2;
  204. });