ImagePreview.js 5.4 KB

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