nfp_sites/app/widgets/fileupload.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-10-01 11:35:00 +00:00
const Media = require('../api/media')
2019-02-22 14:53:43 +00:00
const FileUpload = {
2019-10-01 11:35:00 +00:00
uploadFile: function(vnode, event) {
2019-02-22 14:53:43 +00:00
if (!event.target.files[0]) return
2019-09-13 13:33:10 +00:00
vnode.state.updateError(vnode, '')
2019-02-22 14:53:43 +00:00
vnode.state.loading = true
Media.uploadMedia(event.target.files[0], vnode.attrs.height || null)
2019-02-22 14:53:43 +00:00
.then(function(res) {
2019-09-13 13:33:10 +00:00
if (vnode.attrs.onupload) {
vnode.attrs.onupload(res)
}
2019-02-22 14:53:43 +00:00
})
.catch(function(err) {
2019-09-13 13:33:10 +00:00
vnode.state.updateError(vnode, err.message)
2019-02-22 14:53:43 +00:00
})
.then(function() {
2019-09-14 19:03:38 +00:00
event.target.value = null
2019-02-22 14:53:43 +00:00
vnode.state.loading = false
m.redraw()
})
},
2019-02-20 16:10:37 +00:00
2019-09-13 13:33:10 +00:00
updateError: function(vnode, error) {
if (vnode.attrs.onerror) {
vnode.attrs.onerror(error)
} else {
vnode.state.error = error
}
},
2019-02-20 16:10:37 +00:00
oninit: function(vnode) {
2019-02-22 14:53:43 +00:00
vnode.state.loading = false
2019-02-20 16:10:37 +00:00
vnode.state.error = ''
},
view: function(vnode) {
2019-09-13 13:33:10 +00:00
let media = vnode.attrs.media
2019-02-20 16:10:37 +00:00
2019-09-13 13:33:10 +00:00
return m('fileupload', {
class: vnode.attrs.class || null,
}, [
m('div.error', {
hidden: !vnode.state.error,
}, vnode.state.error),
(media
? vnode.attrs.useimg
? [ m('img', { src: media.large_url }), m('div.showicon')]
: m('a.display.inside', {
href: media.large_url,
style: {
2019-09-14 19:03:38 +00:00
'background-image': 'url("' + media.large_url + '")',
2019-09-13 13:33:10 +00:00
},
}, m('div.showicon'))
: m('div.inside.showbordericon')
2019-02-20 16:10:37 +00:00
),
2019-02-22 14:53:43 +00:00
m('input', {
accept: 'image/*',
type: 'file',
2019-09-13 13:33:10 +00:00
onchange: this.uploadFile.bind(this, vnode),
2019-02-22 14:53:43 +00:00
}),
2019-09-14 19:03:38 +00:00
(media && vnode.attrs.ondelete ? m('button.remove', { onclick: vnode.attrs.ondelete }) : null),
2019-02-22 14:53:43 +00:00
(vnode.state.loading ? m('div.loading-spinner') : null),
2019-02-20 16:10:37 +00:00
])
2019-09-14 19:03:38 +00:00
},
2019-02-20 16:10:37 +00:00
}
2019-02-22 14:53:43 +00:00
module.exports = FileUpload