-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm.js
133 lines (133 loc) · 2.97 KB
/
vm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
export default {
name: 'Timeline',
props: {
timelineData: {
type: Array,
default() {
return [];
}
},
pageSize: {
type: Number,
default: 10
},
paginationType: {
type: String,
default: 'frontend'
},
spacing: {
type: Number,
default: 180
},
titleWidth: {
type: Number,
default: 100
},
circleWidth: {
type: Number,
default: 10
},
startCircleWidth: {
type: Number,
default: 30
}
},
data() {
return {
timelineLoading: false,
list: [],
current: 1,
next: true,
remoteEmit: false,
};
},
computed: {
circleLeft() {
return (this.titleWidth - this.spacing) / 2;
},
verticalBarLeft() {
return (this.spacing + this.circleLeft) + this.circleWidth / 2 - 1;
},
startCircleLeft() {
return (this.spacing - this.verticalBarLeft) + this.startCircleWidth / 2 - 1;
}
},
watch: {
timelineData: {
handler(newlval) {
if (this.paginationType === 'remote') {
if (this.remoteEmit) {
this._remoteRender(newlval);
} else {
console.info('异步从接口获取数据,请重置父组件中的请求参数: pagination.current = 1');
this.list = [];
this.current = 1;
this._remoteRender(newlval);
}
}
this.remoteEmit = false;
},
deep: true
}
},
methods: {
// 前端静态数据判断next
_hasNext() {
return this.timelineData.length > this.list.length;
},
// 前端静态数据init
_appendList(timelineList) {
if (this.paginationType !== 'remote') {
timelineList.forEach((item, index) => {
if (index < this.pageSize) {
this.list.push(item);
}
});
this.next = this._hasNext();
}
},
// 前端静态数据分页
_frontendPagination(index) {
const lens = this.pageSize + index + 1;
for (let i = index + 1; i < lens; i++) {
if (this.timelineData[i]) {
this.list.push(this.timelineData[i]);
}
}
this.next = this._hasNext();
},
// remote 分页
_remotePagination() {
this.$emit('fetch-remote', {
current: this.current,
size: this.pageSize
});
this.remoteEmit = true;
},
_remoteRender(newlval) {
newlval.forEach((item, index) => {
if (index < this.pageSize) {
this.list.push(item);
}
});
if (newlval && newlval.length < Number(this.pageSize)) {
this.next = false;
} else {
this.next = true;
}
this.current += 1;
},
onPagination(index) {
if (this.next) {
if (this.paginationType === 'remote') {
this._remotePagination();
} else {
this._frontendPagination(index);
}
}
}
},
created() {
this._appendList(this.timelineData);
}
};