130 lines
4.3 KiB
JavaScript
130 lines
4.3 KiB
JavaScript
'use strict';
|
|
|
|
// Forms controller
|
|
angular.module('forms').controller('AdminFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$uibModal', 'myForm', '$filter',
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $uibModal, myForm, $filter) {
|
|
|
|
$scope = $rootScope;
|
|
$scope.animationsEnabled = true;
|
|
$scope.myform = myForm;
|
|
$rootScope.saveInProgress = false;
|
|
|
|
CurrentForm.setForm($scope.myform);
|
|
|
|
$scope.formURL = $scope.myform.admin.username + '.tellform.com';
|
|
|
|
$scope.tabData = [
|
|
{
|
|
heading: $filter('translate')('CREATE_TAB'),
|
|
route: 'viewForm.create'
|
|
},
|
|
{
|
|
heading: $filter('translate')('DESIGN_TAB'),
|
|
route: 'viewForm.design'
|
|
},
|
|
{
|
|
heading: $filter('translate')('CONFIGURE_TAB'),
|
|
route: 'viewForm.configure'
|
|
},
|
|
{
|
|
heading: $filter('translate')('ANALYZE_TAB'),
|
|
route: 'viewForm.analyze'
|
|
}
|
|
];
|
|
|
|
$scope.setForm = function(form){
|
|
$scope.myform = form;
|
|
};
|
|
$rootScope.resetForm = function(){
|
|
$scope.myform = Forms.get({
|
|
formId: $stateParams.formId
|
|
});
|
|
};
|
|
|
|
/*
|
|
** DeleteModal Functions
|
|
*/
|
|
$scope.openDeleteModal = function(){
|
|
$scope.deleteModal = $uibModal.open({
|
|
animation: $scope.animationsEnabled,
|
|
templateUrl: 'myModalContent.html',
|
|
controller: 'AdminFormController',
|
|
resolve: {
|
|
myForm: function(){
|
|
return $scope.myform;
|
|
}
|
|
}
|
|
});
|
|
$scope.deleteModal.result.then(function (selectedItem) {
|
|
$scope.selected = selectedItem;
|
|
}, function () {
|
|
console.log('Modal dismissed at: ' + new Date());
|
|
});
|
|
};
|
|
|
|
|
|
$scope.cancelDeleteModal = function(){
|
|
if($scope.deleteModal){
|
|
$scope.deleteModal.dismiss('cancel');
|
|
}
|
|
};
|
|
|
|
// Remove existing Form
|
|
$scope.removeCurrentForm = function() {
|
|
if($scope.deleteModal && $scope.deleteModal.opened){
|
|
|
|
$scope.deleteModal.close();
|
|
|
|
var form_id = $scope.myform._id;
|
|
if(!form_id) throw new Error('Error - removeCurrentForm(): $scope.myform._id does not exist');
|
|
|
|
$http.delete('/forms/'+form_id)
|
|
.success(function(data, status, headers){
|
|
console.log('form deleted successfully');
|
|
|
|
$state.go('listForms', {}, {reload: true});
|
|
|
|
}).error(function(error){
|
|
console.log('ERROR: Form could not be deleted.');
|
|
console.error(error);
|
|
});
|
|
}
|
|
};
|
|
|
|
// Update existing Form
|
|
$scope.update = $rootScope.update = function(updateImmediately, cb){
|
|
|
|
var continueUpdate = true;
|
|
if(!updateImmediately){
|
|
continueUpdate = !$rootScope.saveInProgress;
|
|
}
|
|
|
|
//Update form **if we are not currently updating** or if **shouldUpdateNow flag is set**
|
|
if(continueUpdate){
|
|
var err = null;
|
|
|
|
if(!updateImmediately){ $rootScope.saveInProgress = true; }
|
|
|
|
$scope.updatePromise = $http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
|
.then(function(response){
|
|
$rootScope.myform = $scope.myform = response.data;
|
|
// console.log(response.data);
|
|
}).catch(function(response){
|
|
console.log('Error occured during form UPDATE.\n');
|
|
// console.log(response.data);
|
|
err = response.data;
|
|
}).finally(function() {
|
|
// console.log('finished updating');
|
|
if(!updateImmediately){$rootScope.saveInProgress = false; }
|
|
|
|
if( (typeof cb) === 'function'){
|
|
return cb(err);
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
|
|
}
|
|
]);
|