Skip to content

Commit 9996b99

Browse files
committed
优化uParse组件
1 parent dfc252b commit 9996b99

14 files changed

Lines changed: 133 additions & 161 deletions
Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,90 @@
11
<template>
2-
<image
3-
:mode="node.attr.mode"
4-
:lazy-load="node.attr.lazyLoad"
5-
:class="node.classStr"
6-
:style="newStyleStr || node.styleStr"
7-
:data-src="node.attr.src"
8-
:src="node.attr.src"
9-
@tap="wxParseImgTap"
10-
@load="wxParseImgLoad"
11-
/>
2+
<image :mode="node.attr.mode" :lazy-load="node.attr.lazyLoad" :class="node.classStr" :style="newStyleStr || node.styleStr"
3+
:data-src="node.attr.src" :src="node.attr.src" @tap="wxParseImgTap" @load="wxParseImgLoad" />
124
</template>
135

146
<script>
15-
export default {
16-
name: 'wxParseImg',
17-
data() {
18-
return {
19-
newStyleStr: '',
20-
preview: true,
21-
};
22-
},
23-
props: {
24-
node: {
25-
type: Object,
26-
default() {
27-
return {};
28-
},
29-
},
30-
},
31-
methods: {
32-
wxParseImgTap(e) {
33-
if (!this.preview) return;
34-
const { src } = e.currentTarget.dataset;
35-
if (!src) return;
36-
let parent = this.$parent;
37-
while(!parent.preview || typeof parent.preview !== 'function') {// TODO 遍历获取父节点执行方法
38-
parent = parent.$parent;
39-
}
40-
parent.preview(src, e);
41-
},
42-
// 图片视觉宽高计算函数区
43-
wxParseImgLoad(e) {
44-
const { src } = e.currentTarget.dataset;
45-
if (!src) return;
46-
const { width, height } = e.mp.detail;
47-
const recal = this.wxAutoImageCal(width, height);
48-
const { imageheight, imageWidth } = recal;
49-
const { padding, mode } = this.node.attr;
50-
const { styleStr } = this.node;
51-
const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`;
52-
this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px;`;
53-
},
54-
// 计算视觉优先的图片宽高
55-
wxAutoImageCal(originalWidth, originalHeight) {
56-
// 获取图片的原始长宽
57-
const { padding } = this.node.attr;
58-
const windowWidth = this.node.$screen.width - (2 * padding);
59-
const results = {};
7+
export default {
8+
name: 'wxParseImg',
9+
data() {
10+
return {
11+
newStyleStr: '',
12+
preview: true,
13+
};
14+
},
15+
props: {
16+
node: {
17+
type: Object,
18+
default () {
19+
return {};
20+
},
21+
},
22+
},
23+
inject: ['uparse'],
24+
methods: {
25+
wxParseImgTap(e) {
26+
if (!this.preview) return;
27+
const {
28+
src
29+
} = e.currentTarget.dataset;
30+
if (!src) return;
31+
this.uparse.preview(src, e);
32+
},
33+
// 图片视觉宽高计算函数区
34+
wxParseImgLoad(e) {
35+
const {
36+
src
37+
} = e.currentTarget.dataset;
38+
if (!src) return;
39+
const {
40+
width,
41+
height
42+
} = e.mp.detail;
43+
const recal = this.wxAutoImageCal(width, height);
44+
const {
45+
imageheight,
46+
imageWidth
47+
} = recal;
48+
const {
49+
padding,
50+
mode
51+
} = this.node.attr;
52+
const {
53+
styleStr
54+
} = this.node;
55+
const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`;
56+
this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px;`;
57+
},
58+
// 计算视觉优先的图片宽高
59+
wxAutoImageCal(originalWidth, originalHeight) {
60+
// 获取图片的原始长宽
61+
const {
62+
padding
63+
} = this.node.attr;
64+
const windowWidth = this.node.$screen.width - (2 * padding);
65+
const results = {};
6066
61-
if (originalWidth < 60 || originalHeight < 60) {
62-
const { src } = this.node.attr;
63-
let parent = this.$parent;
64-
while(!parent.preview || typeof parent.preview !== 'function') {
65-
parent = parent.$parent;
67+
if (originalWidth < 60 || originalHeight < 60) {
68+
const {
69+
src
70+
} = this.node.attr;
71+
this.uparse.removeImageUrl(src);
72+
this.preview = false;
6673
}
67-
parent.removeImageUrl(src);
68-
this.preview = false;
69-
}
7074
71-
// 判断按照那种方式进行缩放
72-
if (originalWidth > windowWidth) {
73-
// 在图片width大于手机屏幕width时候
74-
results.imageWidth = windowWidth;
75-
results.imageheight = windowWidth * (originalHeight / originalWidth);
76-
} else {
77-
// 否则展示原来的数据
78-
results.imageWidth = originalWidth;
79-
results.imageheight = originalHeight;
80-
}
75+
// 判断按照那种方式进行缩放
76+
if (originalWidth > windowWidth) {
77+
// 在图片width大于手机屏幕width时候
78+
results.imageWidth = windowWidth;
79+
results.imageheight = windowWidth * (originalHeight / originalWidth);
80+
} else {
81+
// 否则展示原来的数据
82+
results.imageWidth = originalWidth;
83+
results.imageheight = originalHeight;
84+
}
8185
82-
return results;
83-
},
84-
},
85-
};
86+
return results;
87+
}
88+
}
89+
};
8690
</script>

‎components/uParse/src/components/wxParseTemplate0.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,15 @@
9090
wxParseVideo,
9191
wxParseAudio,
9292
},
93+
inject: ['uparse'],
9394
methods: {
9495
wxParseATap(e) {
9596
const {
9697
href
9798
} = e.currentTarget.dataset;// TODO currentTarget才有dataset
9899
if (!href) return;
99-
let parent = this.$parent;
100-
while(!parent.preview || typeof parent.preview !== 'function') {// TODO 遍历获取父节点执行方法
101-
parent = parent.$parent;
102-
}
103-
parent.navigate(href, e);
104-
},
105-
},
100+
this.uparse.navigate(href, e);
101+
}
102+
}
106103
};
107104
</script>

‎components/uParse/src/components/wxParseTemplate1.vue‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,14 @@
8282
wxParseVideo,
8383
wxParseAudio,
8484
},
85+
inject: ['uparse'],
8586
methods: {
8687
wxParseATap(e) {
8788
const {
8889
href
8990
} = e.currentTarget.dataset;
9091
if (!href) return;
91-
let parent = this.$parent;
92-
while(!parent.preview || typeof parent.preview !== 'function') {
93-
parent = parent.$parent;
94-
}
95-
parent.navigate(href, e);
92+
this.uparse.navigate(href, e);
9693
},
9794
},
9895
};

‎components/uParse/src/components/wxParseTemplate10.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,15 @@
8080
wxParseVideo,
8181
wxParseAudio,
8282
},
83+
inject: ['uparse'],
8384
methods: {
8485
wxParseATap(e) {
8586
const {
8687
href
8788
} = e.currentTarget.dataset;
8889
if (!href) return;
89-
let parent = this.$parent;
90-
while(!parent.preview || typeof parent.preview !== 'function') {
91-
parent = parent.$parent;
92-
}
93-
parent.navigate(href, e);
94-
},
95-
},
90+
this.uparse.navigate(href, e);
91+
}
92+
}
9693
};
9794
</script>

‎components/uParse/src/components/wxParseTemplate11.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,15 @@
7070
wxParseVideo,
7171
wxParseAudio,
7272
},
73+
inject: ['uparse'],
7374
methods: {
7475
wxParseATap(e) {
7576
const {
7677
href
7778
} = e.currentTarget.dataset;
7879
if (!href) return;
79-
let parent = this.$parent;
80-
while(!parent.preview || typeof parent.preview !== 'function') {
81-
parent = parent.$parent;
82-
}
83-
parent.navigate(href, e);
84-
},
85-
},
80+
this.uparse.navigate(href, e);
81+
}
82+
}
8683
};
8784
</script>

‎components/uParse/src/components/wxParseTemplate2.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@
8181
wxParseVideo,
8282
wxParseAudio,
8383
},
84+
inject: ['uparse'],
8485
methods: {
8586
wxParseATap(e) {
8687
const {
8788
href
8889
} = e.currentTarget.dataset;
8990
if (!href) return;
90-
let parent = this.$parent;
91-
while(!parent.preview || typeof parent.preview !== 'function') {
92-
parent = parent.$parent;
93-
}
94-
parent.navigate(href, e);
95-
},
96-
},
91+
this.uparse.navigate(href, e);
92+
}
93+
}
9794
};
9895
</script>

‎components/uParse/src/components/wxParseTemplate3.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@
8181
wxParseVideo,
8282
wxParseAudio,
8383
},
84+
inject: ['uparse'],
8485
methods: {
8586
wxParseATap(e) {
8687
const {
8788
href
8889
} = e.currentTarget.dataset;
8990
if (!href) return;
90-
let parent = this.$parent;
91-
while(!parent.preview || typeof parent.preview !== 'function') {
92-
parent = parent.$parent;
93-
}
94-
parent.navigate(href, e);
95-
},
96-
},
91+
this.uparse.navigate(href, e);
92+
}
93+
}
9794
};
9895
</script>

‎components/uParse/src/components/wxParseTemplate4.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@
8181
wxParseVideo,
8282
wxParseAudio,
8383
},
84+
inject: ['uparse'],
8485
methods: {
8586
wxParseATap(e) {
8687
const {
8788
href
8889
} = e.currentTarget.dataset;
8990
if (!href) return;
90-
let parent = this.$parent;
91-
while(!parent.preview || typeof parent.preview !== 'function') {
92-
parent = parent.$parent;
93-
}
94-
parent.navigate(href, e);
95-
},
96-
},
91+
this.uparse.navigate(href, e);
92+
}
93+
}
9794
};
9895
</script>

‎components/uParse/src/components/wxParseTemplate5.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@
8181
wxParseVideo,
8282
wxParseAudio,
8383
},
84+
inject: ['uparse'],
8485
methods: {
8586
wxParseATap(e) {
8687
const {
8788
href
8889
} = e.currentTarget.dataset;
8990
if (!href) return;
90-
let parent = this.$parent;
91-
while(!parent.preview || typeof parent.preview !== 'function') {
92-
parent = parent.$parent;
93-
}
94-
parent.navigate(href, e);
95-
},
96-
},
91+
this.uparse.navigate(href, e);
92+
}
93+
}
9794
};
9895
</script>

‎components/uParse/src/components/wxParseTemplate6.vue‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@
8181
wxParseVideo,
8282
wxParseAudio,
8383
},
84+
inject: ['uparse'],
8485
methods: {
8586
wxParseATap(e) {
8687
const {
8788
href
8889
} = e.currentTarget.dataset;
8990
if (!href) return;
90-
let parent = this.$parent;
91-
while(!parent.preview || typeof parent.preview !== 'function') {
92-
parent = parent.$parent;
93-
}
94-
parent.navigate(href, e);
95-
},
96-
},
91+
this.uparse.navigate(href, e);
92+
}
93+
}
9794
};
9895
</script>

0 commit comments

Comments
 (0)