jquery.select2.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. define([
  2. 'jquery',
  3. 'require',
  4. './select2/core',
  5. './select2/defaults'
  6. ], function ($, require, Select2, Defaults) {
  7. // Force jQuery.mousewheel to be loaded if it hasn't already
  8. require('jquery.mousewheel');
  9. if ($.fn.select2 == null) {
  10. // All methods that should return the element
  11. var thisMethods = ['open', 'close', 'destroy'];
  12. $.fn.select2 = function (options) {
  13. options = options || {};
  14. if (typeof options === 'object') {
  15. this.each(function () {
  16. var instanceOptions = $.extend({}, options, true);
  17. var instance = new Select2($(this), instanceOptions);
  18. });
  19. return this;
  20. } else if (typeof options === 'string') {
  21. var instance = this.data('select2');
  22. var args = Array.prototype.slice.call(arguments, 1);
  23. var ret = instance[options](args);
  24. // Check if we should be returning `this`
  25. if ($.inArray(options, thisMethods) > -1) {
  26. return this;
  27. }
  28. return ret;
  29. } else {
  30. throw new Error('Invalid arguments for Select2: ' + options);
  31. }
  32. };
  33. }
  34. if ($.fn.select2.defaults == null) {
  35. $.fn.select2.defaults = Defaults;
  36. }
  37. return Select2;
  38. });