Skip to content

Commit

Permalink
EZP-27439: Improve Multi File Upload loading time - frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
dew326 authored and Maciej Kobus committed Jun 9, 2017
1 parent d464e3d commit f2d9826
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
38 changes: 5 additions & 33 deletions bundle/Resources/public/js/plugins/mfu-fileupload-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,51 +265,23 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
* @param event.errorCallback {Function} an error callback
*/
_checkContentCreatePermissions: function (event) {
const defaultContentTypeIdentifier = this.get('defaultContentType').contentTypeIdentifier;
const locationIdentifier = this.get('host').get('contentType').get('identifier');
const locationMappings = this.get('contentTypeByLocationMappings');
const mappedLocation = locationMappings.find(item => item.contentTypeIdentifier === locationIdentifier);
const uniqueIdentifiers = mappedLocation ? [...new Set(mappedLocation.mappings.map(item => item.contentTypeIdentifier))] : [];
let promises = [];

if (uniqueIdentifiers.length) {
promises = uniqueIdentifiers.map(identifier => this._loadContentTypeByIdentifier(identifier));
} else {
promises = [this._loadContentTypeByIdentifier(defaultContentTypeIdentifier)];
}
const locationId = this.get('host').get('location').get('locationId');

Promise.all(promises)
.then(this._checkContentCreatePermissionByContentType.bind(this))
this._makeCheckPermissionsRequest(locationId)
.then(this._setContentCreatePermissionsState.bind(this, event.permissionsStateCallback))
.catch(event.errorCallback);
},

/**
* Checks permissions for creating content of specific content types
*
* @method _checkContentCreatePermissionByContentType
* @protected
* @param data {Array} a list of resolved promises
* @return {Promise}
*/
_checkContentCreatePermissionByContentType: function (data) {
const locationId = this.get('host').get('location').get('locationId');

return Promise.all(data.map(this._makeCheckPermissionsRequest.bind(this, locationId)))
},

/**
* Makes a request to check permissions for a selected location
*
* @method _makeCheckPermissionsRequest
* @protected
* @param locationId {Number} non-REST location id
* @param response {Object} load content type by identifier REST request response
* @return {Promise}
*/
_makeCheckPermissionsRequest: function (locationId, response) {
const contentTypeId = response.document.ContentTypeInfoList.ContentType[0].id;
const url = `api/ezp/v2/multifileupload/v1/check-permission/${locationId}/${contentTypeId}`;
_makeCheckPermissionsRequest: function (locationId) {
const url = `api/ezp/v2/multifileupload/v1/check-permission/${locationId}`;

return new Promise((resolve, reject) => {
this.get('host')
Expand All @@ -333,7 +305,7 @@ YUI.add('mfu-fileupload-plugin', function (Y) {
* @param permissions {Array} a list of resolved content type content create permission checks
*/
_setContentCreatePermissionsState: function (callback, permissions) {
const isContentCreateAllowed = permissions.some(item => item.document.PermissionReport.allowed);
const isContentCreateAllowed = !!permissions.document.PermissionReport.allowedContentTypes.length;

callback(isContentCreateAllowed);
},
Expand Down
18 changes: 18 additions & 0 deletions bundle/Resources/public/js/views/mfu-uploadform-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ YUI.add('mfu-uploadform-view', function (Y) {
return;
}

if (this.get('checkPermissionPrevented')) {
this._setFormActiveState(true);
return;
}

/**
* Checks for content create permissions for a logged in user.
* Listened by {{#crossLink "mfu.Plugin.FileUploadService"}}mfu.Plugin.FileUploadService{{/crossLink}}
Expand Down Expand Up @@ -360,6 +365,19 @@ YUI.add('mfu-uploadform-view', function (Y) {
valueFn: () => Y.eZ.trans('file.upload.permissions.check.error', {}, 'uploadform'),
readOnly: true,
},

/**
* Flag indicating if the permissons check should be ommited.
* If set to truthy value user is allowed to create content.
*
* @attribute checkPermissionPrevented
* @type {Boolean}
* @writeOnce 'initOnly'
*/
checkPermissionPrevented: {
value: false,
writeOnce: 'initOnly',
},
}
});
});
3 changes: 2 additions & 1 deletion bundle/Resources/public/js/views/mfu-uploadpopup-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ YUI.add('mfu-uploadpopup-view', function (Y) {
valueFn: function () {
return new Y.mfu.UploadFormView({
bubbleTargets: this,
onDropCallback: this._updateUploadedFilesList.bind(this)
onDropCallback: this._updateUploadedFilesList.bind(this),
checkPermissionPrevented: true
});
},
readOnly: true,
Expand Down

0 comments on commit f2d9826

Please sign in to comment.