select2.amd.js 15 KB

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