61 lines
2 KiB
JavaScript
61 lines
2 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
// Forms controller
|
||
|
angular.module('forms').controller('ListFormsController', ['$rootScope', '$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http',
|
||
|
function($rootScope, $scope, $stateParams, $state, Forms, CurrentForm, $http) {
|
||
|
|
||
|
$scope = $rootScope;
|
||
|
$rootScope.showCreateModal = false;
|
||
|
|
||
|
// Return all user's Forms
|
||
|
$scope.findAll = function() {
|
||
|
Forms.query(function(_forms){
|
||
|
$scope.myforms = _forms;
|
||
|
});
|
||
|
};
|
||
|
|
||
|
//Modal functions
|
||
|
$scope.openCreateModal = function(){
|
||
|
if(!$rootScope.showCreateModal){
|
||
|
$rootScope.showCreateModal = true;
|
||
|
}
|
||
|
};
|
||
|
$scope.closeCreateModal = function(){
|
||
|
if($rootScope.showCreateModal){
|
||
|
$rootScope.showCreateModal = false;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
$scope.setForm = function (form) {
|
||
|
$scope.myform = form;
|
||
|
};
|
||
|
$scope.goToWithId = function(route, id) {
|
||
|
$state.go(route, {'formId': id}, {reload: true});
|
||
|
};
|
||
|
|
||
|
// Create new Form
|
||
|
$scope.createNew = function(){
|
||
|
var form = {};
|
||
|
form.title = $scope.myform.name.$modelValue;
|
||
|
form.language = $scope.myform.language.$modelValue;
|
||
|
console.log(form);
|
||
|
$rootScope.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', data._id+'');
|
||
|
}).error(function(errorResponse){
|
||
|
console.log(errorResponse);
|
||
|
$scope.error = errorResponse.data.message;
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
]);
|