u-trees.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <template>
  2. <view :class="'interlayer '+(c||'')" :style="s">
  3. <block v-for="(n, i) in nodes" v-bind:key="i">
  4. <!--图片-->
  5. <view v-if="n.name=='img'" :class="'_img '+n.attrs.class" :style="n.attrs.style" :data-attrs="n.attrs" @tap.stop="imgtap">
  6. <rich-text v-if="ctrl[i]!=0" :nodes="[{attrs:{src:loading&&(ctrl[i]||0)<2?loading:(lazyLoad&&!ctrl[i]?placeholder:(ctrl[i]==3?errorImg:n.attrs.src||'')),alt:n.attrs.alt||'',width:n.attrs.width||'',style:'-webkit-touch-callout:none;max-width:100%;display:block'+(n.attrs.height?';height:'+n.attrs.height:'')},name:'img'}]" />
  7. <image class="_image" :src="lazyLoad&&!ctrl[i]?placeholder:n.attrs.src" :lazy-load="lazyLoad"
  8. :show-menu-by-longpress="!n.attrs.ignore" :data-i="i" :data-index="n.attrs.i" data-source="img" @load="loadImg"
  9. @error="error" />
  10. </view>
  11. <!--文本-->
  12. <text v-else-if="n.type=='text'" decode>{{n.text}}</text>
  13. <!--#ifndef MP-BAIDU-->
  14. <text v-else-if="n.name=='br'">\n</text>
  15. <!--#endif-->
  16. <!--视频-->
  17. <view v-else-if="((n.lazyLoad&&!n.attrs.autoplay)||(n.name=='video'&&!loadVideo))&&ctrl[i]==undefined" :id="n.attrs.id"
  18. :class="'_video '+(n.attrs.class||'')" :style="n.attrs.style" :data-i="i" @tap.stop="_loadVideo" />
  19. <video v-else-if="n.name=='video'" :id="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :autoplay="n.attrs.autoplay||ctrl[i]==0"
  20. :controls="n.attrs.controls" :loop="n.attrs.loop" :muted="n.attrs.muted" :poster="n.attrs.poster" :src="n.attrs.source[ctrl[i]||0]"
  21. :unit-id="n.attrs['unit-id']" :data-id="n.attrs.id" :data-i="i" data-source="video" @error="error" @play="play" />
  22. <!--音频-->
  23. <audio v-else-if="n.name=='audio'" :ref="n.attrs.id" :class="n.attrs.class" :style="n.attrs.style" :author="n.attrs.author"
  24. :autoplay="n.attrs.autoplay" :controls="n.attrs.controls" :loop="n.attrs.loop" :name="n.attrs.name" :poster="n.attrs.poster"
  25. :src="n.attrs.source[ctrl[i]||0]" :data-i="i" :data-id="n.attrs.id" data-source="audio" @error.native="error"
  26. @play.native="play" />
  27. <!--链接-->
  28. <view v-else-if="n.name=='a'" :id="n.attrs.id" :class="'_a '+(n.attrs.class||'')" hover-class="_hover" :style="n.attrs.style"
  29. :data-attrs="n.attrs" @tap.stop="linkpress">
  30. <u-trees class="_span" c="_span" :nodes="n.children" />
  31. </view>
  32. <!--广告-->
  33. <!--<ad v-else-if="n.name=='ad'" :class="n.attrs.class" :style="n.attrs.style" :unit-id="n.attrs['unit-id']" :appid="n.attrs.appid" :apid="n.attrs.apid" :type="n.attrs.type" :adpid="n.attrs.adpid" data-source="ad" @error="error" />-->
  34. <!--列表-->
  35. <view v-else-if="n.name=='li'" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:flex;flex-direction:row'">
  36. <view v-if="n.type=='ol'" class="_ol-bef">{{n.num}}</view>
  37. <view v-else class="_ul-bef">
  38. <view v-if="n.floor%3==0" class="_ul-p1">█</view>
  39. <view v-else-if="n.floor%3==2" class="_ul-p2" />
  40. <view v-else class="_ul-p1" style="border-radius:50%">█</view>
  41. </view>
  42. <u-trees class="_li" c="_li" :nodes="n.children" :lazyLoad="lazyLoad" :loading="loading" />
  43. </view>
  44. <!--表格-->
  45. <view v-else-if="n.name=='table'&&n.c&&n.flag" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:grid'">
  46. <u-trees v-for="(cell,n) in n.children" v-bind:key="n" :class="cell.attrs.class" :c="cell.attrs.class" :style="cell.attrs.style"
  47. :s="cell.attrs.style" :nodes="cell.children" />
  48. </view>
  49. <view v-else-if="n.name=='table'&&n.c" :id="n.attrs.id" :class="n.attrs.class" :style="(n.attrs.style||'')+';display:table'">
  50. <view v-for="(tbody, o) in n.children" v-bind:key="o" :class="tbody.attrs.class" :style="(tbody.attrs.style||'')+(tbody.name[0]=='t'?';display:table-'+(tbody.name=='tr'?'row':'row-group'):'')">
  51. <view v-for="(tr, p) in tbody.children" v-bind:key="p" :class="tr.attrs.class" :style="(tr.attrs.style||'')+(tr.name[0]=='t'?';display:table-'+(tr.name=='tr'?'row':'cell'):'')">
  52. <u-trees v-if="tr.name=='td'" :nodes="tr.children" />
  53. <u-trees v-else v-for="(td, q) in tr.children" v-bind:key="q" :class="td.attrs.class" :c="td.attrs.class" :style="(td.attrs.style||'')+(td.name[0]=='t'?';display:table-'+(td.name=='tr'?'row':'cell'):'')"
  54. :s="(td.attrs.style||'')+(td.name[0]=='t'?';display:table-'+(td.name=='tr'?'row':'cell'):'')" :nodes="td.children" />
  55. </view>
  56. </view>
  57. </view>
  58. <!--#ifdef APP-PLUS-->
  59. <iframe v-else-if="n.name=='iframe'" :style="n.attrs.style" :allowfullscreen="n.attrs.allowfullscreen" :frameborder="n.attrs.frameborder"
  60. :width="n.attrs.width" :height="n.attrs.height" :src="n.attrs.src" />
  61. <embed v-else-if="n.name=='embed'" :style="n.attrs.style" :width="n.attrs.width" :height="n.attrs.height" :src="n.attrs.src" />
  62. <!--#endif-->
  63. <!--富文本-->
  64. <!--#ifdef MP-WEIXIN || MP-QQ || APP-PLUS-->
  65. <rich-text v-else-if="use(n)" :id="n.attrs.id" :class="'_p __'+n.name" :nodes="[n]" />
  66. <!--#endif-->
  67. <!--#ifndef MP-WEIXIN || MP-QQ || APP-PLUS-->
  68. <rich-text v-else-if="!n.c" :id="n.attrs.id" :nodes="[n]" style="display:inline" />
  69. <!--#endif-->
  70. <u-trees v-else :class="(n.attrs.id||'')+' _'+n.name+' '+(n.attrs.class||'')" :c="(n.attrs.id||'')+' _'+n.name+' '+(n.attrs.class||'')"
  71. :style="n.attrs.style" :s="n.attrs.style" :nodes="n.children" :lazyLoad="lazyLoad" :loading="loading" />
  72. </block>
  73. </view>
  74. </template>
  75. <script>
  76. var inline = {
  77. abbr: 1,
  78. b: 1,
  79. big: 1,
  80. code: 1,
  81. del: 1,
  82. em: 1,
  83. i: 1,
  84. ins: 1,
  85. label: 1,
  86. q: 1,
  87. small: 1,
  88. span: 1,
  89. strong: 1,
  90. sub: 1,
  91. sup: 1
  92. }
  93. let globalData = {};
  94. globalData.Parser = {};
  95. import cfg from './config.js'
  96. const errorImg = cfg.errorImg;
  97. export default {
  98. name: 'u-trees',
  99. data() {
  100. return {
  101. ctrl: [],
  102. placeholder: 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="300" height="225"/>',
  103. errorImg,
  104. loadVideo: typeof plus == 'undefined',
  105. // #ifndef MP-ALIPAY
  106. c: '',
  107. s: ''
  108. // #endif
  109. }
  110. },
  111. props: {
  112. nodes: Array,
  113. lazyLoad: Boolean,
  114. loading: String,
  115. preview: Boolean,
  116. // #ifdef MP-ALIPAY
  117. c: String,
  118. s: String
  119. // #endif
  120. },
  121. mounted() {
  122. for (this.top = this.$parent; this.top.$options.name != 'parser'; this.top = this.top.$parent);
  123. this.init();
  124. },
  125. // #ifdef APP-PLUS
  126. // #ifdef VUE2
  127. // 组件销毁前,将实例从u-form的缓存中移除
  128. beforeDestroy() {
  129. this.observer && this.observer.disconnect();
  130. },
  131. // #endif
  132. // #ifdef VUE3
  133. beforeUnmount() {
  134. this.observer && this.observer.disconnect();
  135. },
  136. // #endif
  137. // #endif
  138. methods: {
  139. init() {
  140. for (var i = this.nodes.length, n; n = this.nodes[--i];) {
  141. if (n.name == 'img') {
  142. this.top.imgList.setItem(n.attrs.i, n.attrs['original-src'] || n.attrs.src);
  143. // #ifdef APP-PLUS
  144. if (this.lazyLoad && !this.observer) {
  145. this.observer = uni.createIntersectionObserver(this).relativeToViewport({
  146. top: 500,
  147. bottom: 500
  148. });
  149. setTimeout(() => {
  150. this.observer.observe('._img', res => {
  151. if (res.intersectionRatio) {
  152. for (var j = this.nodes.length; j--;)
  153. if (this.nodes[j].name == 'img')
  154. this.$set(this.ctrl, j, 1);
  155. this.observer.disconnect();
  156. }
  157. })
  158. }, 0)
  159. }
  160. // #endif
  161. } else if (n.name == 'video' || n.name == 'audio') {
  162. var ctx;
  163. if (n.name == 'video') {
  164. ctx = uni.createVideoContext(n.attrs.id
  165. // #ifndef MP-BAIDU
  166. , this
  167. // #endif
  168. );
  169. } else if (this.$refs[n.attrs.id])
  170. ctx = this.$refs[n.attrs.id][0];
  171. if (ctx) {
  172. ctx.id = n.attrs.id;
  173. this.top.videoContexts.push(ctx);
  174. }
  175. }
  176. }
  177. // #ifdef APP-PLUS
  178. // APP 上避免 video 错位需要延时渲染
  179. setTimeout(() => {
  180. this.loadVideo = true;
  181. }, 1000)
  182. // #endif
  183. },
  184. play(e) {
  185. var contexts = this.top.videoContexts;
  186. if (contexts.length > 1 && this.top.autopause)
  187. for (var i = contexts.length; i--;)
  188. if (contexts[i].id != e.currentTarget.dataset.id)
  189. contexts[i].pause();
  190. },
  191. imgtap(e) {
  192. var attrs = e.currentTarget.dataset.attrs;
  193. if (!attrs.ignore) {
  194. var preview = this.preview,
  195. data = {
  196. id: e.target.id,
  197. src: attrs.src,
  198. ignore: () => preview = false
  199. };
  200. globalData.Parser.onImgtap && globalData.Parser.onImgtap(data);
  201. this.top.$emit('imgtap', data);
  202. if (preview) {
  203. var urls = this.top.imgList,
  204. current = urls[attrs.i] ? parseInt(attrs.i) : (urls = [attrs.src], 0);
  205. uni.previewImage({
  206. current,
  207. urls
  208. })
  209. }
  210. }
  211. },
  212. loadImg(e) {
  213. var i = e.currentTarget.dataset.i;
  214. if (this.lazyLoad && !this.ctrl[i]) {
  215. // #ifdef QUICKAPP-WEBVIEW
  216. this.$set(this.ctrl, i, 0);
  217. this.$nextTick(function() {
  218. // #endif
  219. // #ifndef APP-PLUS
  220. this.$set(this.ctrl, i, 1);
  221. // #endif
  222. // #ifdef QUICKAPP-WEBVIEW
  223. })
  224. // #endif
  225. } else if (this.loading && this.ctrl[i] != 2) {
  226. // #ifdef QUICKAPP-WEBVIEW
  227. this.$set(this.ctrl, i, 0);
  228. this.$nextTick(function() {
  229. // #endif
  230. this.$set(this.ctrl, i, 2);
  231. // #ifdef QUICKAPP-WEBVIEW
  232. })
  233. // #endif
  234. }
  235. },
  236. linkpress(e) {
  237. var jump = true,
  238. attrs = e.currentTarget.dataset.attrs;
  239. attrs.ignore = () => jump = false;
  240. globalData.Parser.onLinkpress && globalData.Parser.onLinkpress(attrs);
  241. this.top.$emit('linkpress', attrs);
  242. if (jump) {
  243. // #ifdef MP
  244. if (attrs['app-id']) {
  245. return uni.navigateToMiniProgram({
  246. appId: attrs['app-id'],
  247. path: attrs.path
  248. })
  249. }
  250. // #endif
  251. if (attrs.href) {
  252. if (attrs.href[0] == '#') {
  253. if (this.top.useAnchor)
  254. this.top.navigateTo({
  255. id: attrs.href.substring(1)
  256. })
  257. } else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0) {
  258. // #ifdef APP-PLUS
  259. plus.runtime.openWeb(attrs.href);
  260. // #endif
  261. // #ifndef APP-PLUS
  262. uni.setClipboardData({
  263. data: attrs.href,
  264. success: () =>
  265. uni.showToast({
  266. title: '链接已复制'
  267. })
  268. })
  269. // #endif
  270. } else
  271. uni.navigateTo({
  272. url: attrs.href,
  273. fail() {
  274. uni.switchTab({
  275. url: attrs.href,
  276. })
  277. }
  278. })
  279. }
  280. }
  281. },
  282. error(e) {
  283. var target = e.currentTarget,
  284. source = target.dataset.source,
  285. i = target.dataset.i;
  286. if (source == 'video' || source == 'audio') {
  287. // 加载其他 source
  288. var index = this.ctrl[i] ? this.ctrl[i].i + 1 : 1;
  289. if (index < this.nodes[i].attrs.source.length)
  290. this.$set(this.ctrl, i, index);
  291. if (e.detail.__args__)
  292. e.detail = e.detail.__args__[0];
  293. } else if (errorImg && source == 'img') {
  294. this.top.imgList.setItem(target.dataset.index, errorImg);
  295. this.$set(this.ctrl, i, 3);
  296. }
  297. this.top && this.top.$emit('error', {
  298. source,
  299. target,
  300. errMsg: e.detail.errMsg
  301. });
  302. },
  303. _loadVideo(e) {
  304. this.$set(this.ctrl, e.target.dataset.i, 0);
  305. },
  306. use(item) {
  307. return !item.c && !inline[item.name] && (item.attrs && item.attrs.style || '').indexOf('display:inline') == -1
  308. }
  309. }
  310. }
  311. </script>
  312. <style>
  313. /* 在这里引入自定义样式 */
  314. /* 链接和图片效果 */
  315. ._a {
  316. display: inline;
  317. padding: 1.5px 0 1.5px 0;
  318. color: #366092;
  319. word-break: break-all;
  320. }
  321. ._hover {
  322. text-decoration: underline;
  323. opacity: 0.7;
  324. }
  325. ._img {
  326. display: inline-block;
  327. max-width: 100%;
  328. overflow: hidden;
  329. }
  330. /* #ifdef MP-WEIXIN */
  331. :host {
  332. display: inline;
  333. }
  334. /* #endif */
  335. /* #ifndef MP-ALIPAY || APP-PLUS */
  336. .interlayer {
  337. display: inherit;
  338. flex-direction: inherit;
  339. flex-wrap: inherit;
  340. align-content: inherit;
  341. align-items: inherit;
  342. justify-content: inherit;
  343. width: 100%;
  344. white-space: inherit;
  345. }
  346. /* #endif */
  347. ._b,
  348. ._strong {
  349. font-weight: bold;
  350. }
  351. /* #ifndef MP-ALIPAY */
  352. ._blockquote,
  353. ._div,
  354. ._p,
  355. ._ol,
  356. ._ul,
  357. ._li {
  358. display: block;
  359. }
  360. /* #endif */
  361. ._code {
  362. font-family: monospace;
  363. }
  364. ._del {
  365. text-decoration: line-through;
  366. }
  367. ._em,
  368. ._i {
  369. font-style: italic;
  370. }
  371. ._h1 {
  372. font-size: 2em;
  373. }
  374. ._h2 {
  375. font-size: 1.5em;
  376. }
  377. ._h3 {
  378. font-size: 1.17em;
  379. }
  380. ._h5 {
  381. font-size: 0.83em;
  382. }
  383. ._h6 {
  384. font-size: 0.67em;
  385. }
  386. ._h1,
  387. ._h2,
  388. ._h3,
  389. ._h4,
  390. ._h5,
  391. ._h6 {
  392. display: block;
  393. font-weight: bold;
  394. }
  395. ._image {
  396. display: block;
  397. width: 100%;
  398. height: 360px;
  399. margin-top: -360px;
  400. opacity: 0;
  401. }
  402. ._ins {
  403. text-decoration: underline;
  404. }
  405. ._li {
  406. flex: 1;
  407. width: 0;
  408. }
  409. ._ol-bef {
  410. width: 36px;
  411. margin-right: 5px;
  412. text-align: right;
  413. }
  414. ._ul-bef {
  415. display: block;
  416. margin: 0 12px 0 23px;
  417. line-height: normal;
  418. }
  419. ._ol-bef,
  420. ._ul-bef {
  421. flex: none;
  422. user-select: none;
  423. }
  424. ._ul-p1 {
  425. display: inline-block;
  426. width: 0.3em;
  427. height: 0.3em;
  428. overflow: hidden;
  429. line-height: 0.3em;
  430. }
  431. ._ul-p2 {
  432. display: inline-block;
  433. width: 0.23em;
  434. height: 0.23em;
  435. border: 0.05em solid black;
  436. border-radius: 50%;
  437. }
  438. ._q::before {
  439. content: '"';
  440. }
  441. ._q::after {
  442. content: '"';
  443. }
  444. ._sub {
  445. font-size: smaller;
  446. vertical-align: sub;
  447. }
  448. ._sup {
  449. font-size: smaller;
  450. vertical-align: super;
  451. }
  452. /* #ifdef MP-ALIPAY || APP-PLUS || QUICKAPP-WEBVIEW */
  453. ._abbr,
  454. ._b,
  455. ._code,
  456. ._del,
  457. ._em,
  458. ._i,
  459. ._ins,
  460. ._label,
  461. ._q,
  462. ._span,
  463. ._strong,
  464. ._sub,
  465. ._sup {
  466. display: inline;
  467. }
  468. /* #endif */
  469. /* #ifdef MP-WEIXIN || MP-QQ */
  470. .__bdo,
  471. .__bdi,
  472. .__ruby,
  473. .__rt {
  474. display: inline-block;
  475. }
  476. /* #endif */
  477. ._video {
  478. position: relative;
  479. display: inline-block;
  480. width: 300px;
  481. height: 225px;
  482. background-color: black;
  483. }
  484. ._video::after {
  485. position: absolute;
  486. top: 50%;
  487. left: 50%;
  488. margin: -15px 0 0 -15px;
  489. content: '';
  490. border-color: transparent transparent transparent white;
  491. border-style: solid;
  492. border-width: 15px 0 15px 30px;
  493. }
  494. </style>