ImagePreview.js 5.8 KB

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