jquery.select2.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. define([
  2. 'jquery',
  3. 'jquery-mousewheel',
  4. './select2/core',
  5. './select2/defaults',
  6. './select2/utils'
  7. ], function ($, _, Select2, Defaults, Utils) {
  8. if ($.fn.select2 == null) {
  9. // All methods that should return the element
  10. var thisMethods = ['open', 'close', 'destroy'];
  11. $.fn.select2 = function (options) {
  12. options = options || {};
  13. if (typeof options === 'object') {
  14. this.each(function () {
  15. var instanceOptions = $.extend(true, {}, options);
  16. var instance = new Select2($(this), instanceOptions);
  17. });
  18. return this;
  19. } else if (typeof options === 'string') {
  20. var ret;
  21. var args = Array.prototype.slice.call(arguments, 1);
  22. this.each(function () {
  23. var instance = Utils.GetData(this, 'select2');
  24. if (instance == null && window.console && console.error) {
  25. console.error(
  26. 'The select2(\'' + options + '\') method was called on an ' +
  27. 'element that is not using Select2.'
  28. );
  29. }
  30. ret = instance[options].apply(instance, args);
  31. });
  32. // Check if we should be returning `this`
  33. if ($.inArray(options, thisMethods) > -1) {
  34. return this;
  35. }
  36. return ret;
  37. } else {
  38. throw new Error('Invalid arguments for Select2: ' + options);
  39. }
  40. };
  41. }
  42. if ($.fn.select2.defaults == null) {
  43. $.fn.select2.defaults = Defaults;
  44. }
  45. return Select2;
  46. });