Progress bar. Close #199

pull/212/head
Henrique Dias 2017-08-15 11:08:58 +01:00
parent 83bc555094
commit ec190d28a8
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
7 changed files with 351 additions and 306 deletions

View File

@ -318,18 +318,36 @@ export default {
handleFiles (files, base, overwrite = false) {
buttons.loading('upload')
let promises = []
let progress = new Array(files.length).fill(0)
for (let file of files) {
promises.push(api.post(this.$route.path + base + file.name, file, overwrite))
let onupload = (id) => (event) => {
progress[id] = (event.loaded / event.total) * 100
let sum = 0
for (let i = 0; i < progress.length; i++) {
sum += progress[i]
}
this.$store.commit('setProgress', Math.ceil(sum / progress.length))
}
for (let i = 0; i < files.length; i++) {
let file = files[i]
promises.push(api.post(this.$route.path + base + file.name, file, overwrite, onupload(i)))
}
let finish = () => {
buttons.success('upload')
this.$store.commit('setProgress', 0)
}
Promise.all(promises)
.then(() => {
buttons.success('upload')
finish()
this.$store.commit('setReload', true)
})
.catch(error => {
buttons.done('upload')
finish()
this.$store.commit('showError', error)
})

View File

@ -145,3 +145,19 @@ main {
#breadcrumbs span a {
padding: .2em;
}
#progress {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 3px;
z-index: 9999999999;
}
#progress div {
height: 100%;
background-color: #40c4ff;
width: 0;
transition: .2s ease width;
}

View File

@ -15,6 +15,7 @@ const state = {
staticGen: document.querySelector('meta[name="staticgen"]').getAttribute('content'),
baseURL: document.querySelector('meta[name="base"]').getAttribute('content'),
jwt: '',
progress: 0,
schedule: '',
loading: false,
reload: false,

View File

@ -61,6 +61,9 @@ const mutations = {
},
setSchedule: (state, value) => {
state.schedule = value
},
setProgress: (state, value) => {
state.progress = value
}
}

View File

@ -56,7 +56,7 @@ export function remove (url) {
})
}
export function post (url, content = '', overwrite = false) {
export function post (url, content = '', overwrite = false, onupload) {
url = removePrefix(url)
return new Promise((resolve, reject) => {
@ -64,6 +64,10 @@ export function post (url, content = '', overwrite = false) {
request.open('POST', `${store.state.baseURL}/api/resource${url}`, true)
request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`)
if (typeof onupload === 'function') {
request.upload.onprogress = onupload
}
if (overwrite) {
request.setRequestHeader('Action', `override`)
}

View File

@ -1,5 +1,8 @@
<template>
<div>
<div id="progress">
<div v-bind:style="{ width: $store.state.progress + '%' }"></div>
</div>
<site-header></site-header>
<sidebar></sidebar>
<main>

File diff suppressed because one or more lines are too long