Skip to content

Commit

Permalink
Added support events for uploading and uploaded/uploadfailed against …
Browse files Browse the repository at this point in the history
…file field and gallery inputs
  • Loading branch information
anthonyjb committed Jul 25, 2018
1 parent 510adc6 commit 0bccccb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions module/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,19 @@ export class FileField {
this._uploader.init()

// Set up event handlers for the uploader
$.dispatch(this.input, 'uploading')

$.listen(
this._uploader.uploader,
{
'aborted cancelled error': () => {
$.dispatch(this.input, 'uploadfailed')
this.clear()
},

'uploaded': (event) => {
$.dispatch(this.input, 'uploaded')

try {
// Extract the asset from the response
const behaviour = this._behaviours.asset
Expand All @@ -471,6 +476,7 @@ export class FileField {

// Populate the field
this.populate(asset)

} catch (error) {
if (error instanceof ResponseError) {
// Clear the field
Expand All @@ -485,6 +491,7 @@ export class FileField {
throw error
}
}

}
}
)
Expand Down
6 changes: 6 additions & 0 deletions module/ui/gallery-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,14 @@ export class GalleryItem {
this._uploader.init()

// Set up event handlers for the uploader
$.dispatch(this._gallery.input, 'uploading')

$.listen(
this._uploader.uploader,
{
'cancelled aborted error': () => {
$.dispatch(this._gallery.input, 'uploadfailed')

// Remove the item
this.destroy()

Expand All @@ -324,6 +328,8 @@ export class GalleryItem {
},

'uploaded': (event) => {
$.dispatch(this._gallery.input, 'uploaded')

try {
// Extract the asset from the response
const asset = behaviourMap.asset[behaviours.asset](
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "manhattan-assets",
"version": "1.0.0-alpha.4",
"version": "1.0.0-beta.1",
"description": "File and image uploads for forms.",
"engines": {
"node": ">=8.9.4"
Expand Down
10 changes: 9 additions & 1 deletion spec/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
data-mh-gallery--upload-url="http://3dtotal.local/x/upload-picture"
data-mh-gallery--crop-aspect-ratio="2.5"
data-mh-gallery--fix-crop-aspect-ratio

>
</form>

<script src="../node_modules/manhattan-essentials/umd/index.js"></script>
Expand All @@ -49,6 +49,14 @@
for (let galleryElm of $.many('[data-mh-gallery]')) {
let gallery = new Gallery(galleryElm, {})
gallery.init()

$.listen(
galleryElm,
{
'uploading': () => { console.log('up') },
'uploaded uploadfailed': () => { console.log('down') }
}
)
}

</script>
Expand Down
8 changes: 8 additions & 0 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
for (let fieldElm of $.many('[data-mh-file-field]')) {
let field = new FileField(fieldElm)
field.init()

$.listen(
fieldElm,
{
'uploading': () => { console.log('up') },
'uploaded uploadfailed': () => { console.log('down') }
}
)
}

</script>
Expand Down

0 comments on commit 0bccccb

Please sign in to comment.