forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithProgress.vue
More file actions
150 lines (130 loc) · 3.35 KB
/
WithProgress.vue
File metadata and controls
150 lines (130 loc) · 3.35 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<div class="q-pa-md column items-start q-gutter-y-md">
<q-file
:value="files"
@input="updateFiles"
label="Pick files"
outlined
multiple
:clearable="!isUploading"
style="max-width: 400px"
>
<template v-slot:file="{ index, file }">
<q-chip
class="full-width q-my-xs"
:removable="isUploading && uploadProgress[index].percent < 1"
square
@remove="cancelFile(index)"
>
<q-linear-progress
class="absolute-full full-height"
:value="uploadProgress[index].percent"
:color="uploadProgress[index].color"
track-color="grey-2"
/>
<q-avatar>
<q-icon :name="uploadProgress[index].icon" />
</q-avatar>
<div class="ellipsis relative-position">
{{ file.name }}
</div>
<q-tooltip>
{{ file.name }}
</q-tooltip>
</q-chip>
</template>
<template v-slot:after v-if="canUpload">
<q-btn
color="primary"
dense
icon="cloud_upload"
round
@click="upload"
:disable="!canUpload"
:loading="isUploading"
/>
</template>
</q-file>
</div>
</template>
<script>
export default {
data () {
return {
files: null,
uploadProgress: [],
uploading: null
}
},
computed: {
isUploading () {
return this.uploading !== null
},
canUpload () {
return this.files !== null
}
},
methods: {
cancelFile (index) {
this.uploadProgress[index] = {
...this.uploadProgress[index],
error: true,
color: 'orange-2'
}
},
updateFiles (files) {
this.files = files
this.uploadProgress = (files || []).map(file => ({
error: false,
color: 'green-2',
percent: 0,
icon: file.type.indexOf('video/') === 0
? 'movie'
: (file.type.indexOf('image/') === 0
? 'photo'
: (file.type.indexOf('audio/') === 0
? 'audiotrack'
: 'insert_drive_file'
)
)
}))
},
upload () {
clearTimeout(this.uploading)
const allDone = this.uploadProgress.every(progress => progress.percent === 1)
this.uploadProgress = this.uploadProgress.map(progress => ({
...progress,
error: false,
color: 'green-2',
percent: allDone === true ? 0 : progress.percent
}))
this.__updateUploadProgress()
},
__updateUploadProgress () {
let done = true
this.uploadProgress = this.uploadProgress.map(progress => {
if (progress.percent === 1 || progress.error === true) {
return progress
}
const percent = Math.min(1, progress.percent + Math.random() / 10)
const error = percent < 1 && Math.random() > 0.95
if (error === false && percent < 1 && done === true) {
done = false
}
return {
...progress,
error,
color: error === true ? 'red-2' : 'green-2',
percent
}
})
this.uploading = done !== true
? setTimeout(this.__updateUploadProgress, 300)
: null
}
},
beforeDestroy () {
clearTimeout(this.uploading)
}
}
</script>