Skip to content

Commit

Permalink
Merge 8bb5e02 into 890d279
Browse files Browse the repository at this point in the history
  • Loading branch information
drjova committed Sep 7, 2016
2 parents 890d279 + 8bb5e02 commit 4e80b10
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 105 deletions.
208 changes: 127 additions & 81 deletions src/invenio-records-js/controllers/InvenioRecordsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* Invenio records controller.
*/
function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
InvenioRecordsAPI) {
$timeout, InvenioRecordsAPI) {

// Parameters

Expand Down Expand Up @@ -154,79 +154,116 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
return deferred.promise;
}


/**
* Records actions
* Remove any empty values from the data
* @memberof InvenioRecordsCtrl
* @function invenioRecordsActions
* @param {Object} evt - The event object.
* @param {String} type - The invenio action type.
* @param {String} method - The invenio request method.
* @param {Object} successCallback - Call function after success.
* @param {Object} errorCallback - Call function after error..
* @function defaultDataPrepare
* @param {Object} data - Provided by ``extra-params.data``.
* @param {Object} unwanted - A list with unwanted values.
*
*/
function invenioRecordsActions(evt, type, method, successCallback, errorCallback) {
function removeEmptyValues(data, unwanted) {
var _unwantend = unwanted || [[null], [{}], '', [undefined]];
angular.forEach(data, function(value, key) {
angular.forEach(_unwantend, function(_value) {
if (angular.equals(_value, value)) {
delete data[key];
}
});
});
return data;
}

// Get the endpoints and do the request
getEndpoints().then(
function success() {
var _data = angular.merge({}, vm.invenioRecordsModel);

var unwatend = [[null], [{}], '', [undefined]];
angular.forEach(_data, function(value, key) {
angular.forEach(unwatend, function(_value) {
if (angular.equals(_value, value)) {
delete _data[key];
}
});
});
/**
* The data preparation before make a request
* @memberof InvenioRecordsCtrl
* @function defaultRequestPrepare
* @param {Object} url - The request url.
* @param {Object} method - The request method.
* @param {Object} model -The data model.
* @param {Object} extraParams - Provided by ``extra-params``.
*
*/
function defaultRequestPrepare(url, method, model, extraParams) {
// Clean the model
var _model = removeEmptyValues(model) || {};
// Pass all the data information
var _data = angular.merge(
{},
extraParams.data || {},
_model
);
return {
url: url,
method: (method || 'PUT').toUpperCase(),
data: _data,
headers: extraParams.headers || {}
};
}

if (!angular.isUndefined(vm.invenioRecordsEndpoints[type])) {
InvenioRecordsAPI.request({
url: vm.invenioRecordsEndpoints[type],
method: (method || 'PUT').toUpperCase(),
data: {
metadata: _data
},
headers: vm.invenioRecordsArgs.headers || {}
}).then(
successCallback,
errorCallback
);
} else {
errorCallback({
type: 'danger',
data: {
message: 'The action type is not supported.'
}
});
}
},
errorCallback);
/**
* Make the API request with the _data payload
* @memberof InvenioRecordsCtrl
* @function makeActionRequest
* @param {String} type - The action type (any existing key from ``links``).
* @param {String} method - The method (POST, PUT, DELETE).
*/
function makeActionRequest(type, method) {
var request = (vm.requestPrepare || defaultRequestPrepare)
.call(this,
vm.invenioRecordsEndpoints[type],
method,
vm.invenioRecordsModel,
vm.invenioRecordsArgs
);
return InvenioRecordsAPI.request(request);
}

/**
* Handle the redirection after a success action if needed
* @memberof InvenioRecordsCtrl
* @function handleActionRedirection
* @param {String} redirect_path - The url to redirect on success.
*/
function handleActionRedirection(redirect_path) {
// Redirect if defined
if (!angular.isUndefined(redirect_path) && redirect_path !== '') {
// Redirect to new location
var _url = redirect_path;
if (redirect_path.substr(0, 1) !== '/' && redirect_path.substr(0, 4) !== 'http') {
// Find the url
_url = vm.invenioRecordsEndpoints[redirect_path];
}
$window.location.href = _url;
}
}

/**
* Action handler
* @memberof InvenioRecordsCtrl
* @function invenioRecordsHandler
* @param {string} type - The action key from ``links`` object.
* @param {string} method - The request method type i.e. GET, POST, PUT.
* @param {string} redirect_path - The url to redirect on success.
* @param {Array} actions - Actions array [[type, method]].
* @param {String} redirect_path - The url to redirect on success.
*/
function invenioRecordsHandler(type, method, redirect_path) {
function invenioRecordsHandler(actions, redirect_path) {

var _actions = (typeof(actions[0]) === 'string') ? [actions] : actions;
/**
* After a successful request
* @memberof invenioRecordsHandler
* @function actionSuccessful
* @param {Object} response - The action request response.
* @param {Object} responses - The promise action request responses.
*/
function actionSuccessful(response) {
function actionSuccessful(responses) {
// NOTE: We keep only the response of the last action!!
var response = responses[responses.length - 1] || {};

$rootScope.$broadcast('invenio.records.alert', {
type: 'success',
data: response.data,
action: type,
action: _actions
});

// Update the endpoints
if (!angular.isUndefined(response.data.links)){
$rootScope.$broadcast(
Expand All @@ -235,19 +272,13 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
}

// Trigger successful event for action
$rootScope.$broadcast('invenio.records.action.success', type);
$rootScope.$broadcast('invenio.records.action.success', _actions);

// Stop loadig idicator
$rootScope.$broadcast('invenio.records.loading.stop');
if (!angular.isUndefined(redirect_path) && redirect_path !== '') {
// Redirect to new location
var _url = redirect_path;
if (redirect_path.substr(0, 1) !== '/' && redirect_path.substr(0, 4) !== 'http') {
// Find the url
_url = vm.invenioRecordsEndpoints[redirect_path];
}
$window.location.href = _url;
}

// Redirect if defined
handleActionRedirection(redirect_path || undefined);
}
/**
* After an errored request
Expand All @@ -259,7 +290,6 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
$rootScope.$broadcast('invenio.records.alert', {
type: 'danger',
data: response.data,
action: type
});

if (response.data.status === 400 && response.data.errors) {
Expand Down Expand Up @@ -295,12 +325,20 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
// Start loading
$rootScope.$broadcast('invenio.records.loading.start');

// Request submission
$scope.$broadcast(
'invenio.records.action',
type,
method,
actionSuccessful,
// Get the endpoints and do the request
getEndpoints().then(
function() {
var promises = [];
angular.forEach(_actions, function(action, index) {
this.push(
makeActionRequest(action[0], action[1])
);
}, promises);
$q.all(promises).then(
actionSuccessful,
actionErrored
);
},
actionErrored
);
}
Expand Down Expand Up @@ -357,26 +395,32 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
// Reset the error
vm.invenioRecordsAlert = null;
// Attach the error to the scope
vm.invenioRecordsAlert = data;
$timeout(function() {
vm.invenioRecordsAlert = data;
}, 0);
}

/**
* Prepare the form after the action
* @memberof InvenioRecordsCtrl
* @function invenioRecordsActionFinished
* @param {Object} evt - The event object.
* @param {Object} type - The action type
* @param {Object} types - The action types.
*/
function invenioRecordsActionSuccess(evt, type) {
function invenioRecordsActionSuccess(evt, types) {
// Set the form to pristine if it's self or publish
if (['publish', 'self'].indexOf(type) > -1) {
var _types = [];
// Get all the types requested
angular.forEach(types, function(item, index) {
this.push(item[0]);
}, _types);
// Change the form state
if (_types.indexOf('self') > -1) {
$scope.depositionForm.$setPristine();
// Set the form to submitted if it's published
if (type === 'publish') {
$scope.depositionForm.$setSubmitted();
}
} else if (_types.indexOf('publish') > -1) {
$scope.depositionForm.$setPristine();
$scope.depositionForm.$setSubmitted();
}
// Set the form to $invalid if it's deleted/discarded
}

/**
Expand Down Expand Up @@ -423,16 +467,17 @@ function InvenioRecordsCtrl($scope, $rootScope, $q, $window, $location,
vm.actionHandler = invenioRecordsHandler;
// Remove validation
vm.removeValidationMessage = invenioRecordsRemoveValidation;
// Remove empty values
vm.removeEmptyValues = removeEmptyValues;
// Prepare for action request
vm.defaultRequestPrepare = defaultRequestPrepare;

////////////

// Listener

// Local

// When invenio.records action requested
$scope.$on('invenio.records.action', invenioRecordsActions);

// When the module initialized
$scope.$on('invenio.records.init', invenioRecordsInit);

Expand Down Expand Up @@ -469,6 +514,7 @@ InvenioRecordsCtrl.$inject = [
'$q',
'$window',
'$location',
'$timeout',
'InvenioRecordsAPI',
];

Expand Down
2 changes: 0 additions & 2 deletions src/invenio-records-js/directives/invenioRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function invenioRecords() {
* @param {InvenioRecordsCtrl} vm - Invenio records controller.
*/
function link(scope, element, attrs, vm) {

// Upadate parameters
// Get extra template parameters
var templateParams = {
Expand All @@ -80,7 +79,6 @@ function invenioRecords() {
};
// Get the record object
var record = JSON.parse(attrs.record || '{}');

// Spread the love of initialization
scope.$broadcast(
'invenio.records.init', args, endpoints, record, links
Expand Down
10 changes: 5 additions & 5 deletions src/invenio-records-js/templates/actions.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<div ng-hide="recordsVM.invenioRecordsAlert.type == 'success' && recordsVM.invenioRecordsArgs.templateParams.responseParams[recordsVM.invenioRecordsAlert.action].hide">
<div ng-show="recordsVM.invenioRecordsEndpoints.self">
<ul class="list-inline text-right">
<li><button ng-disabled="depositionForm.$invalid || depositionForm.$prestine || recordsVM.invenioRecordsLoading" class="btn btn-default" ng-click="recordsVM.actionHandler('self', 'PUT')">Save</button></li>
<li><button ng-disabled="depositionForm.$invalid || depositionForm.$dirty || recordsVM.invenioRecordsLoading" class="btn btn-success" ng-click="recordsVM.actionHandler('publish', 'POST')">Publish</button></li>
<li><button ng-disabled="recordsVM.invenioRecordsLoading" ng-hide="!recordsVM.invenioRecordsEndpoints.discard" class="btn btn-danger" ng-click="recordsVM.actionHandler('discard', 'PUT')">Discard</button></li>
<li><button ng-disabled="depositionForm.$invalid || depositionForm.$prestine || recordsVM.invenioRecordsLoading" class="btn btn-default" ng-click="recordsVM.actionHandler(['self', 'PUT'])">Save</button></li>
<li><button ng-disabled="depositionForm.$invalid || depositionForm.$dirty || recordsVM.invenioRecordsLoading" class="btn btn-success" ng-click="recordsVM.actionHandler(['publish', 'POST'])">Publish</button></li>
<li><button ng-disabled="recordsVM.invenioRecordsLoading" ng-hide="!recordsVM.invenioRecordsEndpoints.discard" class="btn btn-danger" ng-click="recordsVM.actionHandler(['discard', 'PUT'])">Discard</button></li>
</ul>
</div>

<div ng-show="!recordsVM.invenioRecordsEndpoints.self">
<ul class="list-inline text-right">
<li><button ng-disabled="depositionForm.$invalid || recordsVM.invenioRecordsLoading" class="btn btn-default" ng-click="recordsVM.actionHandler('self', 'PUT')">Save</button></li>
<li><button ng-disabled="recordsVM.invenioRecordsLoading" ng-hide="!recordsVM.invenioRecordsEndpoints.initialization" class="btn btn-danger" ng-click="recordsVM.actionHandler('self', 'DELETE')">Delete</button></li>
<li><button ng-disabled="depositionForm.$invalid || recordsVM.invenioRecordsLoading" class="btn btn-default" ng-click="recordsVM.actionHandler(['self', 'PUT'])">Save</button></li>
<li><button ng-disabled="recordsVM.invenioRecordsLoading" ng-hide="!recordsVM.invenioRecordsEndpoints.initialization" class="btn btn-danger" ng-click="recordsVM.actionHandler(['self', 'DELETE'])">Delete</button></li>
</ul>
</div>
</div>

0 comments on commit 4e80b10

Please sign in to comment.