import Plugin from './Plugin'
import yo from 'yo-yo'
/**
* Progress drawer
*
*/
export default class ProgressDrawer extends Plugin {
constructor (core, opts) {
super(core, opts)
this.id = 'ProgressDrawer'
this.title = 'Progress Drawer'
this.type = 'progressindicator'
// set default options
const defaultOptions = {}
// merge default options with the ones set by user
this.opts = Object.assign({}, defaultOptions, opts)
this.render = this.render.bind(this)
}
drawerItem (file) {
const isUploaded = file.progress === 100
const remove = (ev) => {
this.core.emitter.emit('file-remove', file.id)
}
const checkIcon = yo``
const fileIcon = yo`
`
return yo`
${file.type.general === 'image' ? file.previewEl : fileIcon}
${file.uploadURL
? yo`${file.name}`
: yo`${file.name}`
}
${file.progress > 0 && file.progress < 100 ? 'Uploading… ' + file.progress + '%' : ''}
${file.progress === 100 ? 'Completed' : ''}
${isUploaded ? checkIcon : ''}
${isUploaded
? ''
: yo`
`
}
`
}
render (state) {
const files = state.files
const selectedFiles = Object.keys(files).filter((file) => {
return files[file].progress !== 100
})
const uploadedFiles = Object.keys(files).filter((file) => {
return files[file].progress === 100
})
const selectedFileCount = Object.keys(selectedFiles).length
const uploadedFileCount = Object.keys(uploadedFiles).length
const isSomethingSelected = selectedFileCount > 0
const isSomethingUploaded = uploadedFileCount > 0
const isSomethingSelectedOrUploaded = isSomethingSelected || isSomethingUploaded
const autoProceed = this.core.opts.autoProceed
const next = (ev) => {
this.core.emitter.emit('next')
}
return yo`
${Object.keys(files).map((fileID) => {
return this.drawerItem(files[fileID])
})}
${autoProceed
? ''
: yo`
`
}
`
// TODO: add this info to the upload button?
//
// ${isSomethingSelected ? this.core.i18n('filesChosen', {'smart_count': selectedFileCount}) : ''}
// ${isSomethingSelected && isSomethingUploaded ? ', ' : ''}
// ${isSomethingUploaded ? this.core.i18n('filesUploaded', {'smart_count': uploadedFileCount}) : ''}
//
}
install () {
const target = this.opts.target
const plugin = this
this.target = this.mount(target, plugin)
}
}