index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import Vue from 'vue';
  3. import VueImagePreview from './ImagePreview';
  4. import { isServer } from '../utils';
  5. var instance;
  6. var defaultConfig = {
  7. loop: true,
  8. value: true,
  9. images: [],
  10. maxZoom: 3,
  11. minZoom: 1 / 3,
  12. onClose: null,
  13. onChange: null,
  14. className: '',
  15. showIndex: true,
  16. closeable: false,
  17. closeIcon: 'clear',
  18. asyncClose: false,
  19. transition: 'van-fade',
  20. getContainer: 'body',
  21. overlayStyle: null,
  22. startPosition: 0,
  23. swipeDuration: 300,
  24. showIndicators: false,
  25. closeOnPopstate: true,
  26. closeIconPosition: 'top-right'
  27. };
  28. var initInstance = function initInstance() {
  29. instance = new (Vue.extend(VueImagePreview))({
  30. el: document.createElement('div')
  31. });
  32. document.body.appendChild(instance.$el);
  33. instance.$on('change', function (index) {
  34. if (instance.onChange) {
  35. instance.onChange(index);
  36. }
  37. });
  38. instance.$on('scale', function (data) {
  39. if (instance.onScale) {
  40. instance.onScale(data);
  41. }
  42. });
  43. };
  44. var ImagePreview = function ImagePreview(images, startPosition) {
  45. if (startPosition === void 0) {
  46. startPosition = 0;
  47. }
  48. /* istanbul ignore if */
  49. if (isServer) {
  50. return;
  51. }
  52. if (!instance) {
  53. initInstance();
  54. }
  55. var options = Array.isArray(images) ? {
  56. images: images,
  57. startPosition: startPosition
  58. } : images;
  59. _extends(instance, defaultConfig, options);
  60. instance.$once('input', function (show) {
  61. instance.value = show;
  62. });
  63. instance.$once('closed', function () {
  64. instance.images = [];
  65. });
  66. if (options.onClose) {
  67. instance.$off('close');
  68. instance.$once('close', options.onClose);
  69. }
  70. return instance;
  71. };
  72. ImagePreview.Component = VueImagePreview;
  73. ImagePreview.install = function () {
  74. Vue.use(VueImagePreview);
  75. };
  76. export default ImagePreview;