2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Forms controller
|
2015-07-06 04:29:05 +00:00
|
|
|
angular.module('forms').controller('ViewFormController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http',
|
|
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) {
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-07 02:52:55 +00:00
|
|
|
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myform = CurrentForm.getForm();
|
2015-07-02 04:54:46 +00:00
|
|
|
$scope.submissions = undefined;
|
|
|
|
$scope.viewSubmissions = false;
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.showCreateModal = false;
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.table = {
|
|
|
|
masterChecker: true,
|
|
|
|
rows: []
|
|
|
|
};
|
|
|
|
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.setForm = function (form) {
|
|
|
|
$scope.myForm = form;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.openCreateModal = function(){
|
|
|
|
if(!$scope.showCreateModal){
|
|
|
|
$scope.showCreateModal = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.closeCreateModal = function(){
|
|
|
|
if($scope.showCreateModal){
|
|
|
|
$scope.showCreateModal = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//Create new form
|
|
|
|
$scope.createNew = function(){
|
|
|
|
var form = {};
|
|
|
|
form.title = $scope.myForm.name.$modelValue;
|
|
|
|
form.language = $scope.myForm.language.$modelValue;
|
|
|
|
console.log(form);
|
|
|
|
$scope.showCreateModal = true;
|
|
|
|
|
|
|
|
console.log($scope.myForm);
|
|
|
|
if($scope.myForm.$valid && $scope.myForm.$dirty){
|
|
|
|
$http.post('/forms', {form: form})
|
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form created');
|
|
|
|
|
|
|
|
// Clear form fields
|
|
|
|
$scope.myForm = {};
|
|
|
|
// Redirect after save
|
|
|
|
$scope.goToWithId('viewForm', $scope.myform._id);
|
|
|
|
}).error(function(errorResponse){
|
|
|
|
console.log(errorResponse);
|
|
|
|
// $scope.error = errorResponse.data.message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
$scope.saveInProgress = false;
|
|
|
|
$scope.update = function() {
|
|
|
|
if(!$scope.saveInProgress){
|
|
|
|
$scope.saveInProgress = true;
|
|
|
|
|
|
|
|
console.log('start update()');
|
|
|
|
|
2015-07-07 01:21:43 +00:00
|
|
|
$http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
2015-07-06 04:29:05 +00:00
|
|
|
.then(function(response){
|
|
|
|
console.log('form updated successfully');
|
|
|
|
console.log('$scope.saveInProgress: '+$scope.saveInProgress);
|
2015-07-07 01:21:43 +00:00
|
|
|
// $rootScope.goToWithId('viewForm', $scope.myform._id);
|
2015-07-06 04:29:05 +00:00
|
|
|
}).catch(function(response){
|
|
|
|
console.log('Error occured during form UPDATE.\n');
|
|
|
|
console.log(response.data);
|
|
|
|
}).finally(function() {
|
|
|
|
$scope.saveInProgress = false;
|
|
|
|
});
|
2015-07-07 01:21:43 +00:00
|
|
|
}
|
|
|
|
};
|
2015-07-03 23:47:14 +00:00
|
|
|
|
|
|
|
//Table Functions
|
|
|
|
$scope.toggleAllCheckers = function(){
|
|
|
|
console.log('toggleAllCheckers');
|
|
|
|
for(var i=0; i<$scope.table.rows.length; i++){
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.table.rows[i].selected = $scope.table.masterChecker;
|
2015-07-03 23:47:14 +00:00
|
|
|
}
|
2015-07-07 01:21:43 +00:00
|
|
|
};
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.toggleObjSelection = function($event, description) {
|
|
|
|
$event.stopPropagation();
|
|
|
|
console.log('checkbox clicked');
|
2015-07-07 01:21:43 +00:00
|
|
|
};
|
2015-07-02 02:49:35 +00:00
|
|
|
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.rowClicked = function(obj) {
|
|
|
|
console.log('row clicked');
|
|
|
|
obj.selected = !obj.selected;
|
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
//show submissions of Form
|
|
|
|
$scope.showSubmissions = function(){
|
|
|
|
$scope.viewSubmissions = true;
|
2015-07-03 23:47:14 +00:00
|
|
|
if(!$scope.table.rows.length){
|
2015-07-07 01:21:43 +00:00
|
|
|
$http.get('/forms/'+$scope.myform._id+'/submissions')
|
2015-07-02 03:50:57 +00:00
|
|
|
.success(function(data, status, headers){
|
2015-07-02 04:54:46 +00:00
|
|
|
console.log(data);
|
|
|
|
$scope.submissions = data;
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.table.rows = data;
|
2015-07-02 04:54:46 +00:00
|
|
|
console.log('form submissions successfully fetched');
|
|
|
|
})
|
|
|
|
.error(function(err){
|
|
|
|
console.log('Could not fetch form submissions.\nError: '+err);
|
|
|
|
});
|
|
|
|
} else if(!$scope.submissions.length){
|
2015-07-07 01:21:43 +00:00
|
|
|
$http.get('/forms/'+$scope.myform._id+'/submissions')
|
2015-07-02 04:54:46 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
$scope.submissions = data;
|
2015-07-03 23:47:14 +00:00
|
|
|
$scope.table.rows = data;
|
|
|
|
console.log($scope.table.rows);
|
2015-07-02 04:54:46 +00:00
|
|
|
console.log('form submissions successfully fetched');
|
|
|
|
})
|
|
|
|
.error(function(err){
|
|
|
|
console.log('Could not fetch form submissions.\nError: '+err);
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
2015-07-02 04:54:46 +00:00
|
|
|
}
|
|
|
|
console.log($scope.submissions);
|
2015-07-03 01:14:48 +00:00
|
|
|
};
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
//hide submissions of Form
|
|
|
|
$scope.hideSubmissions = function(){
|
|
|
|
$scope.viewSubmissions = false;
|
2015-07-03 01:14:48 +00:00
|
|
|
};
|
2015-07-02 04:54:46 +00:00
|
|
|
|
|
|
|
// Return all user's Forms
|
|
|
|
$scope.findAll = function() {
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myforms = Forms.query();
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Find a specific Form
|
|
|
|
$scope.findOne = function() {
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myform = Forms.get({
|
2015-07-02 04:54:46 +00:00
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
2015-07-07 01:21:43 +00:00
|
|
|
CurrentForm.setForm($scope.myform);
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Remove existing Form
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.remove = function(form_id) {
|
|
|
|
var form = {};
|
|
|
|
if(!form_id){
|
|
|
|
form = CurrentForm.getForm();
|
|
|
|
if(!form) form = $scope.myform;
|
|
|
|
}else {
|
|
|
|
form._id = form_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$http.delete('/forms/'+form._id)
|
2015-07-02 04:54:46 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form deleted successfully');
|
2015-07-07 01:21:43 +00:00
|
|
|
|
|
|
|
if(!form_id){
|
|
|
|
$state.go('listForms');
|
|
|
|
}
|
|
|
|
if($scope.myforms.length > 0){
|
|
|
|
$scope.myforms = _.filter($scope.myforms, function(myform){
|
|
|
|
return myform._id !== form._id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-02 04:54:46 +00:00
|
|
|
}).error(function(error){
|
|
|
|
console.log('ERROR: Form could not be deleted.');
|
|
|
|
console.error(error);
|
|
|
|
});
|
2015-07-06 04:29:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.goToWithId = function(route, id) {
|
|
|
|
$state.go(route, {'formId': id}, {reload: true});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create new Form
|
|
|
|
$rootScope.createOrUpdate = function() {
|
|
|
|
if($scope.isNewForm){
|
|
|
|
// Create new Form object
|
2015-07-07 01:21:43 +00:00
|
|
|
var form = new Forms($scope.myform);
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-07 01:21:43 +00:00
|
|
|
$http.post('/forms', {form: $scope.myform})
|
2015-07-06 04:29:05 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form created');
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
// Clear form fields
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myform = {};
|
2015-07-06 04:29:05 +00:00
|
|
|
// Redirect after save
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.goToWithId('viewForm', $scope.myform._id);
|
2015-07-06 04:29:05 +00:00
|
|
|
}).error(function(errorResponse){
|
|
|
|
console.log(errorResponse.data.message);
|
|
|
|
$scope.error = errorResponse.data.message;
|
|
|
|
});
|
|
|
|
} else{
|
|
|
|
$rootScope.update();
|
|
|
|
}
|
2015-07-02 04:54:46 +00:00
|
|
|
};
|
2015-07-06 04:29:05 +00:00
|
|
|
|
|
|
|
// $rootScope.saveInProgress = false;
|
|
|
|
|
|
|
|
var saveFinished = function() {
|
|
|
|
$rootScope.saveInProgress = false;
|
|
|
|
console.log('update form');
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update existing Form
|
|
|
|
$rootScope.update = function() {
|
|
|
|
|
|
|
|
$rootScope.saveInProgress = true;
|
|
|
|
console.log('update form');
|
|
|
|
|
2015-07-07 01:21:43 +00:00
|
|
|
$http.put('/forms/'+$scope.myform._id, {form: $scope.myform})
|
2015-07-06 04:29:05 +00:00
|
|
|
.then(function(response){
|
|
|
|
console.log('form updated successfully');
|
|
|
|
}).catch(function(response){
|
|
|
|
console.log('Error occured during form UPDATE.\n');
|
|
|
|
console.log(response.data);
|
|
|
|
}).finally(function() {
|
|
|
|
$rootScope.saveInProgress = false;
|
|
|
|
console.log('update form');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$rootScope.resetForm = function(){
|
2015-07-07 01:21:43 +00:00
|
|
|
$scope.myform = Forms.get({
|
2015-07-06 04:29:05 +00:00
|
|
|
formId: $stateParams.formId
|
|
|
|
});
|
2015-07-07 01:21:43 +00:00
|
|
|
};
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
]);
|