2015-08-18 21:44:36 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Forms controller
|
2015-10-30 18:40:02 +00:00
|
|
|
angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$modal', 'myForm',
|
|
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $modal, myForm) {
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
$scope = $rootScope;
|
|
|
|
|
2015-10-30 18:40:02 +00:00
|
|
|
$scope.myform = myForm;
|
2015-08-18 21:44:36 +00:00
|
|
|
$rootScope.saveInProgress = false;
|
2015-10-30 18:40:02 +00:00
|
|
|
CurrentForm.setForm($scope.myform);
|
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
$scope.tabData = [
|
|
|
|
{
|
|
|
|
heading: 'Create',
|
|
|
|
route: 'viewForm.create'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
heading: 'Design',
|
|
|
|
route: 'viewForm.design',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
heading: 'Configure',
|
|
|
|
route: 'viewForm.configure',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
heading: 'Analyze',
|
|
|
|
route: 'viewForm.analyze',
|
|
|
|
}
|
|
|
|
];
|
2015-11-06 17:27:01 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
// Find a specific Form
|
|
|
|
$scope.findOne = function(){
|
2015-09-15 22:21:49 +00:00
|
|
|
Forms.get({
|
2015-08-18 21:44:36 +00:00
|
|
|
formId: $stateParams.formId
|
2015-09-15 22:21:49 +00:00
|
|
|
}, function(form){
|
|
|
|
CurrentForm.setForm(form);
|
|
|
|
$scope.myform = form;
|
|
|
|
$scope.myform._id = $stateParams.formId;
|
|
|
|
}, function(err){
|
|
|
|
console.error('Could not fetch form');
|
|
|
|
console.error(err);
|
2015-08-18 21:44:36 +00:00
|
|
|
});
|
|
|
|
};
|
2015-09-15 22:21:49 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
$scope.setForm = function(form){
|
|
|
|
$scope.myform = form;
|
|
|
|
};
|
|
|
|
$rootScope.resetForm = function(){
|
|
|
|
$scope.myform = Forms.get({
|
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** DeleteModal Functions
|
|
|
|
*/
|
|
|
|
$scope.openDeleteModal = function(){
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.deleteModal = $modal.open({
|
2015-08-18 21:44:36 +00:00
|
|
|
animation: $scope.animationsEnabled,
|
|
|
|
templateUrl: 'myModalContent.html',
|
|
|
|
controller: 'AdminFormController',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
$scope.cancelDeleteModal = function(){
|
2015-08-19 22:29:01 +00:00
|
|
|
if($scope.deleteModal){
|
|
|
|
$scope.deleteModal.dismiss('cancel');
|
2015-08-18 21:44:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove existing Form
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.removeCurrentForm = function() {
|
|
|
|
if($scope.deleteModal && $scope.deleteModal.opened){
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.deleteModal.close();
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
var form_id = $scope.myform._id;
|
|
|
|
if(!form_id) throw new Error('Error - removeCurrentForm(): $scope.myform._id does not exist');
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
$http.delete('/forms/'+form_id)
|
2015-08-18 21:44:36 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form deleted successfully');
|
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
$state.go('listForms', {}, {reload: true});
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
}).error(function(error){
|
|
|
|
console.log('ERROR: Form could not be deleted.');
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update existing Form
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.update = $rootScope.update = function(updateImmediately, cb){
|
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
var continueUpdate = true;
|
2015-08-19 22:29:01 +00:00
|
|
|
if(!updateImmediately){
|
|
|
|
continueUpdate = !$rootScope.saveInProgress;
|
2015-08-18 21:44:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
//Update form if we **are not currently updating** or if **shouldUpdateNow flag is set**
|
2015-08-18 21:44:36 +00:00
|
|
|
if(continueUpdate){
|
2015-08-19 22:29:01 +00:00
|
|
|
// console.log('begin updating form');
|
2015-08-18 21:44:36 +00:00
|
|
|
var err = null;
|
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
if(!updateImmediately){ $rootScope.saveInProgress = true; }
|
2015-08-18 21:44:36 +00:00
|
|
|
|
|
|
|
$scope.updatePromise = $http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
|
|
|
.then(function(response){
|
|
|
|
$rootScope.myform = $scope.myform = response.data;
|
2015-08-19 22:29:01 +00:00
|
|
|
// console.log(response.data);
|
2015-08-18 21:44:36 +00:00
|
|
|
}).catch(function(response){
|
2015-10-30 20:14:48 +00:00
|
|
|
console.log('Error occured during form UPDATE.\n');
|
2015-08-19 22:29:01 +00:00
|
|
|
// console.log(response.data);
|
2015-08-18 21:44:36 +00:00
|
|
|
err = response.data;
|
|
|
|
}).finally(function() {
|
2015-08-19 22:29:01 +00:00
|
|
|
// console.log('finished updating');
|
|
|
|
if(!updateImmediately){$rootScope.saveInProgress = false; }
|
|
|
|
|
|
|
|
if( (typeof cb) === 'function'){
|
|
|
|
cb(err);
|
|
|
|
}
|
2015-08-18 21:44:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-30 18:40:02 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
}
|
|
|
|
]);
|