index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import $ from 'jquery';
  2. import Cropper from 'cropperjs/src';
  3. if ($.fn) {
  4. const AnotherCropper = $.fn.cropper;
  5. const NAMESPACE = 'cropper';
  6. $.fn.cropper = function jQueryCropper(option, ...args) {
  7. let result;
  8. this.each((i, element) => {
  9. const $element = $(element);
  10. const isDestroy = option === 'destroy';
  11. let cropper = $element.data(NAMESPACE);
  12. if (!cropper) {
  13. if (isDestroy) {
  14. return;
  15. }
  16. const options = $.extend({}, $element.data(), $.isPlainObject(option) && option);
  17. cropper = new Cropper(element, options);
  18. $element.data(NAMESPACE, cropper);
  19. }
  20. if (typeof option === 'string') {
  21. const fn = cropper[option];
  22. if ($.isFunction(fn)) {
  23. result = fn.apply(cropper, args);
  24. if (result === cropper) {
  25. result = undefined;
  26. }
  27. if (isDestroy) {
  28. $element.removeData(NAMESPACE);
  29. }
  30. }
  31. }
  32. });
  33. return result !== undefined ? result : this;
  34. };
  35. $.fn.cropper.Constructor = Cropper;
  36. $.fn.cropper.setDefaults = Cropper.setDefaults;
  37. $.fn.cropper.noConflict = function noConflict() {
  38. $.fn.cropper = AnotherCropper;
  39. return this;
  40. };
  41. }