From c907ba746ca5db26a2689214f2e3cc239d86cbd0 Mon Sep 17 00:00:00 2001 From: Tom Robertson Date: Sun, 16 Sep 2018 18:16:58 -0700 Subject: [PATCH] Add a multiUpload option to asset manager to allow forcing single file only uploads and configuring the exact filename in the form --- docs/modules/Assets.md | 4 ++++ src/asset_manager/config/config.js | 4 ++++ src/asset_manager/view/FileUploader.js | 24 ++++++++++++++----- test/specs/asset_manager/view/FileUploader.js | 12 ++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/docs/modules/Assets.md b/docs/modules/Assets.md index af4af4c5b..c1a390b24 100644 --- a/docs/modules/Assets.md +++ b/docs/modules/Assets.md @@ -66,6 +66,10 @@ Below the list of currently available options // The credentials setting for the upload request, eg. 'include', 'omit' credentials: 'include', + // Allow uploading multiple files per request. + // If disabled filename will not have '[]' appended + multiUpload: true, + // If true, tries to add automatically uploaded assets. // To make it work the server should respond with a JSON containing assets // in a data key, eg: diff --git a/src/asset_manager/config/config.js b/src/asset_manager/config/config.js index 569520c81..982967aaa 100644 --- a/src/asset_manager/config/config.js +++ b/src/asset_manager/config/config.js @@ -32,6 +32,10 @@ module.exports = { // The credentials setting for the upload request, eg. 'include', 'omit' credentials: 'include', + // Allow uploading multiple files per request. + // If disabled filename will not have '[]' appended + multiUpload: true, + // If true, tries to add automatically uploaded assets. // To make it work the server should respond with a JSON containing assets // in a data key, eg: diff --git a/src/asset_manager/view/FileUploader.js b/src/asset_manager/view/FileUploader.js index 0c536bb8c..5130a7f26 100644 --- a/src/asset_manager/view/FileUploader.js +++ b/src/asset_manager/view/FileUploader.js @@ -7,7 +7,7 @@ module.exports = Backbone.View.extend( template: _.template(`
<%= title %>
- multiple/> + <%= multiUpload ? 'multiple' : '' %>/>
`), @@ -26,6 +26,7 @@ module.exports = Backbone.View.extend( c.disableUpload !== undefined ? c.disableUpload : !c.upload && !c.embedAsBase64; + this.multiUpload = c.multiUpload !== undefined ? c.multiUpload : true; this.events['change #' + this.uploadId] = 'uploadFile'; let uploadFile = c.uploadFile; @@ -78,7 +79,13 @@ module.exports = Backbone.View.extend( const em = this.config.em; const config = this.config; const target = this.target; - const json = typeof text === 'string' ? JSON.parse(text) : text; + let json; + try { + json = typeof text === 'string' ? JSON.parse(text) : text; + } catch (e) { + json = text; + } + em && em.trigger('asset:upload:response', json); if (config.autoAdd && target) { @@ -101,14 +108,18 @@ module.exports = Backbone.View.extend( const config = this.config; const params = config.params; - for (let i = 0; i < files.length; i++) { - body.append(`${config.uploadName}[]`, files[i]); - } - for (let param in params) { body.append(param, params[param]); } + if (this.multiUpload) { + for (let i = 0; i < files.length; i++) { + body.append(`${config.uploadName}[]`, files[i]); + } + } else if (files.length) { + body.append(config.uploadName, files[0]); + } + var target = this.target; const url = config.upload; const headers = config.headers; @@ -229,6 +240,7 @@ module.exports = Backbone.View.extend( title: this.config.uploadText, uploadId: this.uploadId, disabled: this.disabled, + multiUpload: this.multiUpload, pfx: this.pfx }) ); diff --git a/test/specs/asset_manager/view/FileUploader.js b/test/specs/asset_manager/view/FileUploader.js index 20d72b368..8118286af 100644 --- a/test/specs/asset_manager/view/FileUploader.js +++ b/test/specs/asset_manager/view/FileUploader.js @@ -67,6 +67,18 @@ module.exports = { ); }); + test('Handles multiUpload false', () => { + var view = new FileUploader({ + config: { + multiUpload: false + } + }); + view.render(); + expect( + view.$el.find('input[type=file]').prop('multiple') + ).toBeFalsy(); + }); + test('Handles embedAsBase64 parameter', () => { var view = new FileUploader({ config: {