2015-07-30 00:22:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Forms controller
|
2016-11-14 20:07:36 +00:00
|
|
|
angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', '$http', '$uibModal',
|
|
|
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http, $uibModal) {
|
2016-06-08 23:56:06 +00:00
|
|
|
|
2015-07-30 00:22:53 +00:00
|
|
|
$scope = $rootScope;
|
2015-08-20 18:47:25 +00:00
|
|
|
$scope.forms = {};
|
|
|
|
$scope.showCreateModal = false;
|
2015-07-30 00:22:53 +00:00
|
|
|
|
2016-07-04 21:31:33 +00:00
|
|
|
$rootScope.languageRegExp = {
|
2016-06-08 23:56:06 +00:00
|
|
|
regExp: /[@!#$%^&*()\-+={}\[\]|\\/'";:`.,~№?<>]+/i,
|
|
|
|
test: function(val) {
|
|
|
|
return !this.regExp.test(val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-14 20:07:36 +00:00
|
|
|
/*
|
|
|
|
** DeleteModal Functions
|
|
|
|
*/
|
|
|
|
$scope.openDeleteModal = function(index){
|
|
|
|
$scope.deleteModal = $uibModal.open({
|
|
|
|
animation: $scope.animationsEnabled,
|
|
|
|
templateUrl: 'deleteModalListForms.html',
|
|
|
|
controller: function($uibModalInstance, items, $scope) {
|
|
|
|
$scope.content = items;
|
|
|
|
|
|
|
|
$scope.cancel = $scope.cancelDeleteModal;
|
|
|
|
|
|
|
|
$scope.deleteForm = function() {
|
2017-03-06 20:39:29 +00:00
|
|
|
$scope.$parent.removeForm(items.formIndex);
|
2017-06-23 17:54:28 +00:00
|
|
|
};
|
2016-11-14 20:07:36 +00:00
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
items: function() {
|
|
|
|
return {
|
|
|
|
currFormTitle: $scope.myforms[index].title,
|
|
|
|
formIndex: index
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$scope.cancelDeleteModal = function(){
|
|
|
|
if($scope.deleteModal){
|
|
|
|
$scope.deleteModal.dismiss('cancel');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-30 00:22:53 +00:00
|
|
|
// Return all user's Forms
|
|
|
|
$scope.findAll = function() {
|
|
|
|
Forms.query(function(_forms){
|
|
|
|
$scope.myforms = _forms;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
//Modal functions
|
|
|
|
$scope.openCreateModal = function(){
|
2015-08-20 18:47:25 +00:00
|
|
|
if(!$scope.showCreateModal){
|
|
|
|
$scope.showCreateModal = true;
|
2015-07-30 00:22:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.closeCreateModal = function(){
|
2015-08-20 18:47:25 +00:00
|
|
|
if($scope.showCreateModal){
|
|
|
|
$scope.showCreateModal = false;
|
2015-07-30 00:22:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.setForm = function (form) {
|
|
|
|
$scope.myform = form;
|
|
|
|
};
|
|
|
|
$scope.goToWithId = function(route, id) {
|
|
|
|
$state.go(route, {'formId': id}, {reload: true});
|
|
|
|
};
|
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
$scope.duplicateForm = function(form_index){
|
|
|
|
var form = _.cloneDeep($scope.myforms[form_index]);
|
|
|
|
delete form._id;
|
2016-06-08 23:56:06 +00:00
|
|
|
|
2015-08-07 21:02:44 +00:00
|
|
|
$http.post('/forms', {form: form})
|
|
|
|
.success(function(data, status, headers){
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.myforms.splice(form_index+1, 0, data);
|
2015-08-07 21:02:44 +00:00
|
|
|
}).error(function(errorResponse){
|
2015-11-06 17:25:30 +00:00
|
|
|
console.error(errorResponse);
|
2015-11-06 17:41:55 +00:00
|
|
|
if(errorResponse === null){
|
2016-06-08 23:56:06 +00:00
|
|
|
$scope.error = errorResponse.data.message;
|
|
|
|
}
|
2015-08-07 21:02:44 +00:00
|
|
|
});
|
2015-11-06 17:41:55 +00:00
|
|
|
};
|
2015-08-07 21:02:44 +00:00
|
|
|
|
2015-07-30 00:22:53 +00:00
|
|
|
// Create new Form
|
2015-11-06 17:25:30 +00:00
|
|
|
$scope.createNewForm = function(){
|
|
|
|
// console.log($scope.forms.createForm);
|
2015-08-20 18:47:25 +00:00
|
|
|
|
2015-07-30 00:22:53 +00:00
|
|
|
var form = {};
|
2015-08-20 18:47:25 +00:00
|
|
|
form.title = $scope.forms.createForm.title.$modelValue;
|
|
|
|
form.language = $scope.forms.createForm.language.$modelValue;
|
2015-07-30 00:22:53 +00:00
|
|
|
|
2015-08-20 18:47:25 +00:00
|
|
|
if($scope.forms.createForm.$valid && $scope.forms.createForm.$dirty){
|
2015-07-30 00:22:53 +00:00
|
|
|
$http.post('/forms', {form: form})
|
|
|
|
.success(function(data, status, headers){
|
2016-06-16 00:38:22 +00:00
|
|
|
//console.log('new form created');
|
2016-06-08 23:56:06 +00:00
|
|
|
// Redirect after save
|
2015-11-06 17:25:30 +00:00
|
|
|
$scope.goToWithId('viewForm.create', data._id+'');
|
2015-07-30 00:22:53 +00:00
|
|
|
}).error(function(errorResponse){
|
2015-08-20 18:47:25 +00:00
|
|
|
console.error(errorResponse);
|
2015-07-30 00:22:53 +00:00
|
|
|
$scope.error = errorResponse.data.message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-08-04 21:06:16 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.removeForm = function(form_index) {
|
|
|
|
if(form_index >= $scope.myforms.length || form_index < 0){
|
|
|
|
throw new Error('Error: form_index in removeForm() must be between 0 and '+$scope.myforms.length-1);
|
|
|
|
}
|
2015-08-04 21:06:16 +00:00
|
|
|
|
2015-08-19 22:29:01 +00:00
|
|
|
$http.delete('/forms/'+$scope.myforms[form_index]._id)
|
2015-08-04 21:06:16 +00:00
|
|
|
.success(function(data, status, headers){
|
2016-06-16 00:38:22 +00:00
|
|
|
//console.log('form deleted successfully');
|
2015-08-19 22:29:01 +00:00
|
|
|
$scope.myforms.splice(form_index, 1);
|
2016-11-14 20:07:36 +00:00
|
|
|
$scope.cancelDeleteModal();
|
2015-08-04 21:06:16 +00:00
|
|
|
}).error(function(error){
|
2016-06-16 00:38:22 +00:00
|
|
|
//console.log('ERROR: Form could not be deleted.');
|
2015-08-04 21:06:16 +00:00
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
};
|
2015-07-30 00:22:53 +00:00
|
|
|
}
|
2016-06-08 23:56:06 +00:00
|
|
|
]);
|