jquery.select2.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 ret;
  22. this.each(function () {
  23. var instance = $(this).data('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. var args = Array.prototype.slice.call(arguments, 1);
  31. ret = instance[options].apply(instance, args);
  32. });
  33. // Check if we should be returning `this`
  34. if ($.inArray(options, thisMethods) > -1) {
  35. return this;
  36. }
  37. return ret;
  38. } else {
  39. throw new Error('Invalid arguments for Select2: ' + options);
  40. }
  41. };
  42. }
  43. if ($.fn.select2.defaults == null) {
  44. $.fn.select2.defaults = Defaults;
  45. }
  46. return Select2;
  47. });