trees.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const errorImg = require('../libs/config.js').errorImg;
  2. Component({
  3. data: {
  4. canIUse: !!wx.chooseMessageFile,
  5. placeholder: "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='300' height='225'/>"
  6. },
  7. properties: {
  8. nodes: Array,
  9. lazyLoad: Boolean,
  10. loading: String
  11. },
  12. methods: {
  13. // 视频播放事件
  14. play(e) {
  15. this.top.group && this.top.group.pause(this.top.i);
  16. if (this.top.videoContexts.length > 1 && this.top.data.autopause)
  17. for (var i = this.top.videoContexts.length; i--;)
  18. if (this.top.videoContexts[i].id != e.currentTarget.id)
  19. this.top.videoContexts[i].pause();
  20. },
  21. // 图片事件
  22. imgtap(e) {
  23. var attrs = e.currentTarget.dataset.attrs;
  24. if (!attrs.ignore) {
  25. var preview = true;
  26. this.top.triggerEvent('imgtap', {
  27. id: e.currentTarget.id,
  28. src: attrs.src,
  29. ignore: () => preview = false
  30. })
  31. if (preview) {
  32. if (this.top.group) return this.top.group.preview(this.top.i, attrs.i);
  33. var urls = this.top.imgList,
  34. current = urls[attrs.i] ? urls[attrs.i] : (urls = [attrs.src], attrs.src);
  35. wx.previewImage({
  36. current,
  37. urls
  38. })
  39. }
  40. }
  41. },
  42. loadImg(e) {
  43. var i = e.target.dataset.i;
  44. if (this.data.lazyLoad && !this.data.nodes[i].load)
  45. this.setData({
  46. [`nodes[${i}].load`]: 1
  47. })
  48. else if (this.data.loading && this.data.nodes[i].load != 2)
  49. this.setData({
  50. [`nodes[${i}].load`]: 2
  51. })
  52. },
  53. // 链接点击事件
  54. linkpress(e) {
  55. var jump = true,
  56. attrs = e.currentTarget.dataset.attrs;
  57. attrs.ignore = () => jump = false;
  58. this.top.triggerEvent('linkpress', attrs);
  59. if (jump) {
  60. if (attrs['app-id'])
  61. wx.navigateToMiniProgram({
  62. appId: attrs['app-id'],
  63. path: attrs.path
  64. })
  65. else if (attrs.href) {
  66. if (attrs.href[0] == '#')
  67. this.top.navigateTo({
  68. id: attrs.href.substring(1)
  69. })
  70. else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0)
  71. wx.setClipboardData({
  72. data: attrs.href,
  73. success: () =>
  74. wx.showToast({
  75. title: '链接已复制'
  76. })
  77. })
  78. else
  79. wx.navigateTo({
  80. url: attrs.href,
  81. fail() {
  82. wx.switchTab({
  83. url: attrs.href,
  84. })
  85. }
  86. })
  87. }
  88. }
  89. },
  90. // 错误事件
  91. error(e) {
  92. var source = e.target.dataset.source,
  93. i = e.target.dataset.i,
  94. node = this.data.nodes[i];
  95. if (source == 'video' || source == 'audio') {
  96. // 加载其他 source
  97. var index = (node.i || 0) + 1;
  98. if (index < node.attrs.source.length)
  99. return this.setData({
  100. [`nodes[${i}].i`]: index
  101. })
  102. } else if (source == 'img' && errorImg) {
  103. this.top.imgList.setItem(e.target.dataset.index, errorImg);
  104. this.setData({
  105. [`nodes[${i}].attrs.src`]: errorImg
  106. })
  107. }
  108. this.top && this.top.triggerEvent('error', {
  109. source,
  110. target: e.target,
  111. errMsg: e.detail.errMsg
  112. })
  113. },
  114. // 加载视频
  115. loadVideo(e) {
  116. var i = e.target.dataset.i;
  117. this.setData({
  118. [`nodes[${i}].attrs.autoplay`]: true
  119. })
  120. }
  121. }
  122. })