Progress bar. Close #199

Former-commit-id: cf8ec044a2531b295b89da915cee439eba6ccd0b [formerly 6d77a48968796dc6db51602486c07d6f2ecc00e6] [formerly 4aa44181ea637eeaba8e7756a76bbf5cd47b6928 [formerly ec190d28a8]]
Former-commit-id: 5e8e25fee1ae1119d5aaf5bf25e83f258ad13d2a [formerly 574db23a689aaf5bbe533d2e322ae98fc0b6d1de]
Former-commit-id: 5b5dfebfb692b62188c216f0cb22f8cdefdd82ab
This commit is contained in:
Henrique Dias 2017-08-15 11:08:58 +01:00
parent 4071a58107
commit e4144ad2b2
7 changed files with 51 additions and 6 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>

View File

@ -1 +1 @@
4b205fed13570c943ad67f9fc4db4f51aeb62cec
e6431a506c73616a73d9a490fffb4e5098bb7fb4