ImagePreview.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _shared = require("./shared");
  6. var _popup = require("../mixins/popup");
  7. var _touch = require("../mixins/touch");
  8. var _bindEvent = require("../mixins/bind-event");
  9. var _icon = _interopRequireDefault(require("../icon"));
  10. var _swipe = _interopRequireDefault(require("../swipe"));
  11. var _ImagePreviewItem = _interopRequireDefault(require("./ImagePreviewItem"));
  12. // Utils
  13. // Mixins
  14. // Components
  15. var _default2 = (0, _shared.createComponent)({
  16. mixins: [_touch.TouchMixin, (0, _popup.PopupMixin)({
  17. skipToggleEvent: true
  18. }), (0, _bindEvent.BindEventMixin)(function (bind) {
  19. bind(window, 'resize', this.resize, true);
  20. bind(window, 'orientationchange', this.resize, true);
  21. })],
  22. props: {
  23. className: null,
  24. closeable: Boolean,
  25. asyncClose: Boolean,
  26. overlayStyle: Object,
  27. showIndicators: Boolean,
  28. images: {
  29. type: Array,
  30. default: function _default() {
  31. return [];
  32. }
  33. },
  34. loop: {
  35. type: Boolean,
  36. default: true
  37. },
  38. overlay: {
  39. type: Boolean,
  40. default: true
  41. },
  42. minZoom: {
  43. type: [Number, String],
  44. default: 1 / 3
  45. },
  46. maxZoom: {
  47. type: [Number, String],
  48. default: 3
  49. },
  50. transition: {
  51. type: String,
  52. default: 'van-fade'
  53. },
  54. showIndex: {
  55. type: Boolean,
  56. default: true
  57. },
  58. swipeDuration: {
  59. type: [Number, String],
  60. default: 300
  61. },
  62. startPosition: {
  63. type: [Number, String],
  64. default: 0
  65. },
  66. overlayClass: {
  67. type: String,
  68. default: (0, _shared.bem)('overlay')
  69. },
  70. closeIcon: {
  71. type: String,
  72. default: 'clear'
  73. },
  74. closeOnPopstate: {
  75. type: Boolean,
  76. default: true
  77. },
  78. closeIconPosition: {
  79. type: String,
  80. default: 'top-right'
  81. }
  82. },
  83. data: function data() {
  84. return {
  85. active: 0,
  86. rootWidth: 0,
  87. rootHeight: 0,
  88. doubleClickTimer: null
  89. };
  90. },
  91. mounted: function mounted() {
  92. this.resize();
  93. },
  94. watch: {
  95. startPosition: 'setActive',
  96. value: function value(val) {
  97. var _this = this;
  98. if (val) {
  99. this.setActive(+this.startPosition);
  100. this.$nextTick(function () {
  101. _this.resize();
  102. _this.$refs.swipe.swipeTo(+_this.startPosition, {
  103. immediate: true
  104. });
  105. });
  106. } else {
  107. this.$emit('close', {
  108. index: this.active,
  109. url: this.images[this.active]
  110. });
  111. }
  112. }
  113. },
  114. methods: {
  115. resize: function resize() {
  116. if (this.$el && this.$el.getBoundingClientRect) {
  117. var rect = this.$el.getBoundingClientRect();
  118. this.rootWidth = rect.width;
  119. this.rootHeight = rect.height;
  120. }
  121. },
  122. emitClose: function emitClose() {
  123. if (!this.asyncClose) {
  124. this.$emit('input', false);
  125. }
  126. },
  127. emitScale: function emitScale(args) {
  128. this.$emit('scale', args);
  129. },
  130. setActive: function setActive(active) {
  131. if (active !== this.active) {
  132. this.active = active;
  133. this.$emit('change', active);
  134. }
  135. },
  136. genIndex: function genIndex() {
  137. var h = this.$createElement;
  138. if (this.showIndex) {
  139. return h("div", {
  140. "class": (0, _shared.bem)('index')
  141. }, [this.slots('index', {
  142. index: this.active
  143. }) || this.active + 1 + " / " + this.images.length]);
  144. }
  145. },
  146. genCover: function genCover() {
  147. var h = this.$createElement;
  148. var cover = this.slots('cover');
  149. if (cover) {
  150. return h("div", {
  151. "class": (0, _shared.bem)('cover')
  152. }, [cover]);
  153. }
  154. },
  155. genImages: function genImages() {
  156. var _this2 = this;
  157. var h = this.$createElement;
  158. return h(_swipe.default, {
  159. "ref": "swipe",
  160. "attrs": {
  161. "lazyRender": true,
  162. "loop": this.loop,
  163. "duration": this.swipeDuration,
  164. "initialSwipe": this.startPosition,
  165. "showIndicators": this.showIndicators,
  166. "indicatorColor": "white"
  167. },
  168. "class": (0, _shared.bem)('swipe'),
  169. "on": {
  170. "change": this.setActive
  171. }
  172. }, [this.images.map(function (image) {
  173. return h(_ImagePreviewItem.default, {
  174. "attrs": {
  175. "src": image,
  176. "show": _this2.value,
  177. "active": _this2.active,
  178. "maxZoom": _this2.maxZoom,
  179. "minZoom": _this2.minZoom,
  180. "rootWidth": _this2.rootWidth,
  181. "rootHeight": _this2.rootHeight
  182. },
  183. "on": {
  184. "scale": _this2.emitScale,
  185. "close": _this2.emitClose
  186. }
  187. });
  188. })]);
  189. },
  190. genClose: function genClose() {
  191. var h = this.$createElement;
  192. if (this.closeable) {
  193. return h(_icon.default, {
  194. "attrs": {
  195. "role": "button",
  196. "name": this.closeIcon
  197. },
  198. "class": (0, _shared.bem)('close-icon', this.closeIconPosition),
  199. "on": {
  200. "click": this.emitClose
  201. }
  202. });
  203. }
  204. },
  205. onClosed: function onClosed() {
  206. this.$emit('closed');
  207. },
  208. // @exposed-api
  209. swipeTo: function swipeTo(index, options) {
  210. if (this.$refs.swipe) {
  211. this.$refs.swipe.swipeTo(index, options);
  212. }
  213. }
  214. },
  215. render: function render() {
  216. var h = arguments[0];
  217. return h("transition", {
  218. "attrs": {
  219. "name": this.transition
  220. },
  221. "on": {
  222. "afterLeave": this.onClosed
  223. }
  224. }, [this.shouldRender ? h("div", {
  225. "directives": [{
  226. name: "show",
  227. value: this.value
  228. }],
  229. "class": [(0, _shared.bem)(), this.className]
  230. }, [this.genClose(), this.genImages(), this.genIndex(), this.genCover()]) : null]);
  231. }
  232. });
  233. exports.default = _default2;