2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
angular.module('forms').directive('submitFormDirective', ['$http', '$timeout', 'TimeCounter', 'Auth', '$filter', '$rootScope',
|
2015-08-21 00:17:14 +00:00
|
|
|
function ($http, $timeout, TimeCounter, Auth, $filter, $rootScope) {
|
2015-06-29 22:51:29 +00:00
|
|
|
return {
|
2015-08-25 21:33:52 +00:00
|
|
|
templateUrl: 'modules/forms/views/directiveViews/form/submit-form.client.view.html',
|
2015-08-04 21:06:16 +00:00
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
2015-08-25 21:33:52 +00:00
|
|
|
myform:'='
|
2015-08-04 21:06:16 +00:00
|
|
|
},
|
2015-06-29 22:51:29 +00:00
|
|
|
controller: function($scope){
|
2015-11-11 20:29:16 +00:00
|
|
|
$scope.authentication = $rootScope.authentication;
|
|
|
|
|
2015-11-06 21:26:12 +00:00
|
|
|
angular.element(document).ready(function() {
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.error = '';
|
2015-11-06 19:00:45 +00:00
|
|
|
$scope.selected = {
|
2015-11-06 21:26:12 +00:00
|
|
|
_id: '',
|
|
|
|
index: null,
|
2015-11-06 19:00:45 +00:00
|
|
|
};
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.submitted = false;
|
|
|
|
|
2015-11-12 19:17:25 +00:00
|
|
|
TimeCounter.startClock();
|
2015-08-06 05:52:59 +00:00
|
|
|
|
2015-09-15 22:21:49 +00:00
|
|
|
$scope.exitStartPage = function(){
|
|
|
|
$scope.myform.startPage.showStart = false;
|
2015-11-06 21:26:12 +00:00
|
|
|
if($scope.myform.form_fields.length > 0){
|
|
|
|
$scope.selected._id = $scope.myform.form_fields[0]._id;
|
|
|
|
}
|
2015-08-07 21:02:44 +00:00
|
|
|
};
|
2015-11-06 19:00:45 +00:00
|
|
|
|
|
|
|
$scope.nextField = function(){
|
2015-11-11 06:15:59 +00:00
|
|
|
if($scope.selected.index < $scope.myform.form_fields.length-1){
|
2015-11-06 19:00:45 +00:00
|
|
|
$scope.selected.index++;
|
|
|
|
$scope.selected._id = $scope.myform.form_fields[$scope.selected.index]._id;
|
2015-11-12 19:17:25 +00:00
|
|
|
$scope.setActiveField($scope.selected._id, $scope.selected.index);
|
2015-11-06 19:00:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
$scope.prevField = function(){
|
|
|
|
if($scope.selected.index > 0){
|
|
|
|
$scope.selected.index = $scope.selected.index - 1;
|
|
|
|
$scope.selected._id = $scope.myform.form_fields[$scope.selected.index]._id;
|
2015-11-12 19:17:25 +00:00
|
|
|
$scope.setActiveField($scope.selected._id, $scope.selected.index);
|
2015-11-06 19:00:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$rootScope.setActiveField = function(field_id, field_index) {
|
|
|
|
if($scope.selected === null){
|
|
|
|
$scope.selected = {
|
|
|
|
_id: '',
|
|
|
|
index: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
console.log('field_id: '+field_id);
|
|
|
|
console.log('field_index: '+field_index);
|
|
|
|
console.log($scope.selected);
|
|
|
|
$scope.selected._id = field_id;
|
|
|
|
$scope.selected.index = field_index;
|
|
|
|
};
|
|
|
|
$scope.hideOverlay = function(){
|
2015-11-06 21:26:12 +00:00
|
|
|
$scope.selected = {
|
|
|
|
_id: '',
|
|
|
|
index: null,
|
|
|
|
};
|
2015-08-07 21:02:44 +00:00
|
|
|
};
|
2015-08-06 05:52:59 +00:00
|
|
|
|
2015-11-06 19:00:45 +00:00
|
|
|
$scope.submitForm = function(){
|
2015-08-21 00:17:14 +00:00
|
|
|
var _timeElapsed = TimeCounter.stopClock();
|
2015-08-06 05:52:59 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
var form = _.cloneDeep($scope.myform);
|
|
|
|
form.timeElapsed = _timeElapsed;
|
|
|
|
|
|
|
|
// console.log('percentageComplete: '+$filter('formValidity')($scope.myform)/$scope.myform.visible_form_fields.length*100+'%');
|
|
|
|
form.percentageComplete = $filter('formValidity')($scope.myform)/$scope.myform.visible_form_fields.length*100;
|
|
|
|
delete form.visible_form_fields;
|
2015-08-06 05:52:59 +00:00
|
|
|
|
|
|
|
$scope.authentication = Auth;
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.submitPromise = $http.post('/forms/'+$scope.myform._id, form)
|
2015-08-06 05:52:59 +00:00
|
|
|
.success(function(data, status, headers){
|
|
|
|
console.log('form submitted successfully');
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.myform.submitted = true;
|
2015-08-06 05:52:59 +00:00
|
|
|
})
|
|
|
|
.error(function(error){
|
|
|
|
console.log(error);
|
|
|
|
$scope.error = error.message;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.reloadForm = function(){
|
2015-08-25 21:33:52 +00:00
|
|
|
//Reset Timer
|
2015-08-21 00:17:14 +00:00
|
|
|
TimeCounter.stopClock();
|
|
|
|
TimeCounter.startClock();
|
2015-08-25 21:33:52 +00:00
|
|
|
|
|
|
|
//Reset Form
|
|
|
|
$scope.myform.submitted = false;
|
|
|
|
$scope.myform.form_fields = _.chain($scope.myform.form_fields).map(function(field){
|
|
|
|
field.fieldValue = '';
|
|
|
|
return field;
|
|
|
|
}).value();
|
2015-08-06 05:52:59 +00:00
|
|
|
};
|
2015-11-06 21:26:12 +00:00
|
|
|
});
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|