2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-07 21:22:20 +00:00
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
angular.module('forms').directive('submitFormDirective', ['$http', 'TimeCounter', '$filter', '$rootScope', 'Auth', 'SendVisitorData',
|
|
|
|
function ($http, TimeCounter, $filter, $rootScope, Auth, SendVisitorData) {
|
2015-06-29 22:51:29 +00:00
|
|
|
return {
|
2016-06-08 22:39:46 +00:00
|
|
|
templateUrl: 'modules/forms/base/views/directiveViews/form/submit-form.client.view.html',
|
|
|
|
restrict: 'E',
|
2015-08-04 21:06:16 +00:00
|
|
|
scope: {
|
2015-08-25 21:33:52 +00:00
|
|
|
myform:'='
|
2015-08-04 21:06:16 +00:00
|
|
|
},
|
2016-04-21 16:10:12 +00:00
|
|
|
controller: function($document, $window, $scope){
|
2015-11-11 20:29:16 +00:00
|
|
|
$scope.authentication = $rootScope.authentication;
|
2016-04-21 16:10:12 +00:00
|
|
|
$scope.noscroll = false;
|
2016-04-29 03:13:37 +00:00
|
|
|
$scope.forms = {};
|
2016-05-05 13:00:58 +00:00
|
|
|
|
|
|
|
var form_fields_count = $scope.myform.visible_form_fields.filter(function(field){
|
2016-04-21 16:10:12 +00:00
|
|
|
if(field.fieldType === 'statement' || field.fieldType === 'rating'){
|
|
|
|
return false;
|
2016-04-29 03:13:37 +00:00
|
|
|
}
|
2016-04-21 16:10:12 +00:00
|
|
|
return true;
|
|
|
|
}).length;
|
2016-05-05 13:00:58 +00:00
|
|
|
|
|
|
|
var nb_valid = $filter('formValidity')($scope.myform);
|
2016-05-05 13:00:58 +00:00
|
|
|
$scope.translateAdvancementData = {
|
2016-05-05 13:00:58 +00:00
|
|
|
done: nb_valid,
|
|
|
|
total: form_fields_count,
|
|
|
|
answers_not_completed: form_fields_count - nb_valid
|
2016-05-05 13:00:58 +00:00
|
|
|
};
|
2016-04-29 03:13:37 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
$scope.reloadForm = function(){
|
|
|
|
//Reset Form
|
|
|
|
$scope.myform.submitted = false;
|
2016-04-22 19:46:58 +00:00
|
|
|
$scope.myform.form_fields = _.chain($scope.myform.visible_form_fields).map(function(field){
|
2015-11-23 19:19:02 +00:00
|
|
|
field.fieldValue = '';
|
|
|
|
return field;
|
|
|
|
}).value();
|
2015-11-06 21:26:12 +00:00
|
|
|
|
2016-04-12 12:42:41 +00:00
|
|
|
$scope.loading = false;
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.error = '';
|
2016-04-29 03:13:37 +00:00
|
|
|
|
2015-11-06 19:00:45 +00:00
|
|
|
$scope.selected = {
|
2016-04-22 19:46:58 +00:00
|
|
|
_id: '',
|
|
|
|
index: 0
|
2015-11-06 19:00:45 +00:00
|
|
|
};
|
2016-04-29 03:13:37 +00:00
|
|
|
$scope.setActiveField($scope.myform.visible_form_fields[0]._id, 0, false);
|
|
|
|
|
2016-04-29 04:57:04 +00:00
|
|
|
//console.log($scope.selected);
|
2015-11-23 19:19:02 +00:00
|
|
|
//Reset Timer
|
2016-04-29 03:13:37 +00:00
|
|
|
TimeCounter.restartClock();
|
2015-11-23 19:19:02 +00:00
|
|
|
};
|
2016-04-29 03:13:37 +00:00
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
//Fire event when window is scrolled
|
2016-04-21 16:10:12 +00:00
|
|
|
$window.onscroll = function(){
|
|
|
|
$scope.scrollPos = document.body.scrollTop || document.documentElement.scrollTop || 0;
|
|
|
|
var elemBox = document.getElementsByClassName('activeField')[0].getBoundingClientRect();
|
|
|
|
$scope.fieldTop = elemBox.top;
|
|
|
|
$scope.fieldBottom = elemBox.bottom;
|
2016-04-29 03:13:37 +00:00
|
|
|
|
2016-04-22 19:46:58 +00:00
|
|
|
//console.log($scope.forms.myForm);
|
2016-04-29 06:00:41 +00:00
|
|
|
var field_id;
|
|
|
|
var field_index;
|
2016-04-22 19:36:34 +00:00
|
|
|
|
2016-04-21 16:10:12 +00:00
|
|
|
if(!$scope.noscroll){
|
|
|
|
//Focus on submit button
|
2016-05-20 04:38:05 +00:00
|
|
|
if( $scope.selected.index === $scope.myform.visible_form_fields.length-1 && $scope.fieldBottom < 200){
|
2016-04-29 06:00:41 +00:00
|
|
|
field_index = $scope.selected.index+1;
|
|
|
|
field_id = 'submit_field';
|
2016-04-21 16:10:12 +00:00
|
|
|
$scope.setActiveField(field_id, field_index, false);
|
2016-04-29 03:13:37 +00:00
|
|
|
}
|
2016-04-21 16:10:12 +00:00
|
|
|
//Focus on field above submit button
|
2016-05-20 04:38:05 +00:00
|
|
|
else if($scope.selected.index === $scope.myform.visible_form_fields.length){
|
2016-04-29 03:13:37 +00:00
|
|
|
if($scope.fieldTop > 200){
|
2016-04-29 06:00:41 +00:00
|
|
|
field_index = $scope.selected.index-1;
|
2016-05-20 04:38:05 +00:00
|
|
|
field_id = $scope.myform.visible_form_fields[field_index]._id;
|
2016-04-21 16:10:12 +00:00
|
|
|
$scope.setActiveField(field_id, field_index, false);
|
|
|
|
}
|
|
|
|
}else if( $scope.fieldBottom < 0){
|
2016-04-29 06:00:41 +00:00
|
|
|
field_index = $scope.selected.index+1;
|
2016-05-20 04:38:05 +00:00
|
|
|
field_id = $scope.myform.visible_form_fields[field_index]._id;
|
2016-04-21 16:10:12 +00:00
|
|
|
$scope.setActiveField(field_id, field_index, false);
|
|
|
|
}else if ( $scope.selected.index !== 0 && $scope.fieldTop > 0) {
|
2016-04-29 06:00:41 +00:00
|
|
|
field_index = $scope.selected.index-1;
|
2016-05-20 04:38:05 +00:00
|
|
|
field_id = $scope.myform.visible_form_fields[field_index]._id;
|
2016-04-29 03:13:37 +00:00
|
|
|
$scope.setActiveField(field_id, field_index, false);
|
2016-04-21 16:10:12 +00:00
|
|
|
}
|
2016-04-22 19:36:34 +00:00
|
|
|
//console.log('$scope.selected.index: '+$scope.selected.index);
|
|
|
|
//console.log('scroll pos: '+$scope.scrollPos+' fieldTop: '+$scope.fieldTop+' fieldBottom: '+$scope.fieldBottom);
|
2016-04-29 03:13:37 +00:00
|
|
|
$scope.$apply();
|
2016-04-21 16:10:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
/*
|
|
|
|
** Field Controls
|
|
|
|
*/
|
2016-06-07 00:37:09 +00:00
|
|
|
var getActiveField = function(){
|
|
|
|
if($scope.selected === null){
|
|
|
|
console.error('current active field is null');
|
|
|
|
throw new Error('current active field is null');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($scope.selected._id === 'submit_field') {
|
|
|
|
return $scope.myform.form_fields.length - 1;
|
|
|
|
} else {
|
|
|
|
return $scope.selected.index;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-21 16:10:12 +00:00
|
|
|
$scope.setActiveField = $rootScope.setActiveField = function(field_id, field_index, animateScroll) {
|
2016-04-16 20:36:55 +00:00
|
|
|
if($scope.selected === null || $scope.selected._id === field_id){
|
2016-04-29 04:57:04 +00:00
|
|
|
//console.log('not scrolling');
|
|
|
|
//console.log($scope.selected);
|
2016-04-21 16:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-20 19:41:18 +00:00
|
|
|
//console.log('field_id: '+field_id);
|
|
|
|
//console.log('field_index: '+field_index);
|
2016-04-29 04:57:04 +00:00
|
|
|
//console.log($scope.selected);
|
2015-11-23 19:19:02 +00:00
|
|
|
|
|
|
|
$scope.selected._id = field_id;
|
|
|
|
$scope.selected.index = field_index;
|
2016-04-21 16:10:12 +00:00
|
|
|
|
2016-05-05 13:00:58 +00:00
|
|
|
var nb_valid = $filter('formValidity')($scope.myform);
|
2016-05-05 13:00:58 +00:00
|
|
|
$scope.translateAdvancementData = {
|
2016-05-05 13:00:58 +00:00
|
|
|
done: nb_valid,
|
|
|
|
total: form_fields_count,
|
|
|
|
answers_not_completed: form_fields_count - nb_valid
|
2016-05-05 13:00:58 +00:00
|
|
|
};
|
|
|
|
|
2016-04-21 16:10:12 +00:00
|
|
|
if(animateScroll){
|
|
|
|
$scope.noscroll=true;
|
|
|
|
setTimeout(function() {
|
2016-05-13 23:52:28 +00:00
|
|
|
$document.scrollToElement(angular.element('.activeField'), -10, 200).then(function() {
|
|
|
|
$scope.noscroll = false;
|
|
|
|
setTimeout(function() {
|
2016-06-04 10:53:51 +00:00
|
|
|
if (document.querySelectorAll('.activeField .focusOn').length) {
|
|
|
|
//Handle default case
|
2016-05-13 23:52:28 +00:00
|
|
|
document.querySelectorAll('.activeField .focusOn')[0].focus();
|
2016-06-04 10:53:51 +00:00
|
|
|
} else if(document.querySelectorAll('.activeField input').length) {
|
|
|
|
//Handle case for rating input
|
2016-05-13 23:52:28 +00:00
|
|
|
document.querySelectorAll('.activeField input')[0].focus();
|
2016-06-04 10:53:51 +00:00
|
|
|
} else {
|
|
|
|
//Handle case for dropdown input
|
|
|
|
document.querySelectorAll('.activeField .selectize-input')[0].focus();
|
2016-05-13 23:52:28 +00:00
|
|
|
}
|
|
|
|
});
|
2016-04-21 16:10:12 +00:00
|
|
|
});
|
2016-05-13 23:52:28 +00:00
|
|
|
});
|
2017-03-06 20:53:31 +00:00
|
|
|
} else {
|
2016-05-20 04:38:05 +00:00
|
|
|
setTimeout(function() {
|
2017-03-27 20:32:06 +00:00
|
|
|
if (document.querySelectorAll('.activeField .focusOn')[0] !== undefined) {
|
2016-06-04 10:53:51 +00:00
|
|
|
//FIXME: DAVID: Figure out how to set focus without scroll movement in HTML Dom
|
2016-05-20 04:38:05 +00:00
|
|
|
document.querySelectorAll('.activeField .focusOn')[0].focus();
|
2017-03-27 20:32:06 +00:00
|
|
|
} else if(document.querySelectorAll('.activeField input')[0] !== undefined) {
|
2016-05-20 04:38:05 +00:00
|
|
|
document.querySelectorAll('.activeField input')[0].focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-07 00:37:09 +00:00
|
|
|
|
2017-03-06 20:53:31 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
};
|
|
|
|
|
2016-04-12 02:14:38 +00:00
|
|
|
$rootScope.nextField = $scope.nextField = function(){
|
2016-04-29 04:57:04 +00:00
|
|
|
//console.log('nextfield');
|
2016-04-21 16:10:12 +00:00
|
|
|
//console.log($scope.selected.index);
|
2016-05-20 04:38:05 +00:00
|
|
|
//console.log($scope.myform.visible_form_fields.length-1);
|
2016-05-10 17:13:20 +00:00
|
|
|
var selected_index, selected_id;
|
2016-05-20 04:38:05 +00:00
|
|
|
if($scope.selected.index < $scope.myform.visible_form_fields.length-1){
|
2016-05-10 17:13:20 +00:00
|
|
|
selected_index = $scope.selected.index+1;
|
2016-05-20 04:38:05 +00:00
|
|
|
selected_id = $scope.myform.visible_form_fields[selected_index]._id;
|
2016-04-21 16:10:12 +00:00
|
|
|
$rootScope.setActiveField(selected_id, selected_index, true);
|
2016-05-20 04:38:05 +00:00
|
|
|
} else if($scope.selected.index === $scope.myform.visible_form_fields.length-1) {
|
2016-05-20 19:41:18 +00:00
|
|
|
//console.log('Second last element');
|
2016-05-10 17:13:20 +00:00
|
|
|
selected_index = $scope.selected.index+1;
|
|
|
|
selected_id = 'submit_field';
|
2016-04-21 16:10:12 +00:00
|
|
|
$rootScope.setActiveField(selected_id, selected_index, true);
|
2016-04-12 02:14:38 +00:00
|
|
|
}
|
2015-11-23 19:19:02 +00:00
|
|
|
};
|
2016-04-21 16:21:05 +00:00
|
|
|
|
2016-04-12 02:14:38 +00:00
|
|
|
$rootScope.prevField = $scope.prevField = function(){
|
2015-11-23 19:19:02 +00:00
|
|
|
if($scope.selected.index > 0){
|
2016-04-21 16:10:12 +00:00
|
|
|
var selected_index = $scope.selected.index - 1;
|
2016-05-20 04:38:05 +00:00
|
|
|
var selected_id = $scope.myform.visible_form_fields[selected_index]._id;
|
2016-04-21 16:10:12 +00:00
|
|
|
$scope.setActiveField(selected_id, selected_index, true);
|
2015-11-23 19:19:02 +00:00
|
|
|
}
|
|
|
|
};
|
2016-04-29 03:13:37 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
/*
|
|
|
|
** Form Display Functions
|
|
|
|
*/
|
|
|
|
$scope.exitStartPage = function(){
|
|
|
|
$scope.myform.startPage.showStart = false;
|
2016-05-20 04:38:05 +00:00
|
|
|
if($scope.myform.visible_form_fields.length > 0){
|
|
|
|
$scope.selected._id = $scope.myform.visible_form_fields[0]._id;
|
2015-11-23 19:19:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-04 10:53:51 +00:00
|
|
|
$rootScope.goToInvalid = $scope.goToInvalid = function() {
|
2016-05-17 00:30:20 +00:00
|
|
|
document.querySelectorAll('.ng-invalid.focusOn')[0].focus();
|
|
|
|
};
|
|
|
|
|
2017-03-27 20:32:06 +00:00
|
|
|
$rootScope.submitForm = $scope.submitForm = function(cb) {
|
2016-06-07 00:37:09 +00:00
|
|
|
|
2016-04-29 03:13:37 +00:00
|
|
|
var _timeElapsed = TimeCounter.stopClock();
|
|
|
|
$scope.loading = true;
|
2016-06-05 03:13:42 +00:00
|
|
|
|
2016-04-29 03:13:37 +00:00
|
|
|
var form = _.cloneDeep($scope.myform);
|
2016-06-07 00:37:09 +00:00
|
|
|
|
2016-04-29 03:13:37 +00:00
|
|
|
form.timeElapsed = _timeElapsed;
|
|
|
|
|
|
|
|
form.percentageComplete = $filter('formValidity')($scope.myform) / $scope.myform.visible_form_fields.length * 100;
|
|
|
|
delete form.visible_form_fields;
|
|
|
|
|
2016-06-07 00:37:09 +00:00
|
|
|
for(var i=0; i < $scope.myform.form_fields.length; i++){
|
|
|
|
if($scope.myform.form_fields[i].fieldType === 'dropdown' && !$scope.myform.form_fields[i].deletePreserved){
|
|
|
|
$scope.myform.form_fields[i].fieldValue = $scope.myform.form_fields[i].fieldValue.option_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:13:37 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
$scope.submitPromise = $http.post('/forms/' + $scope.myform._id, form)
|
|
|
|
.success(function (data, status, headers) {
|
2017-03-27 20:32:06 +00:00
|
|
|
console.log('\n\n\n\n\nSUBMITTING PROMISE');
|
|
|
|
console.log(data);
|
2016-04-29 03:13:37 +00:00
|
|
|
$scope.myform.submitted = true;
|
|
|
|
$scope.loading = false;
|
2016-06-07 00:37:09 +00:00
|
|
|
SendVisitorData.send($scope.myform, getActiveField(), _timeElapsed);
|
2017-03-27 20:32:06 +00:00
|
|
|
if(cb){
|
|
|
|
cb();
|
|
|
|
}
|
2016-04-29 03:13:37 +00:00
|
|
|
})
|
|
|
|
.error(function (error) {
|
|
|
|
$scope.loading = false;
|
2016-06-01 01:16:24 +00:00
|
|
|
console.error(error);
|
2016-04-29 03:13:37 +00:00
|
|
|
$scope.error = error.message;
|
2017-03-27 20:32:06 +00:00
|
|
|
if(cb){
|
|
|
|
cb(error);
|
|
|
|
}
|
2016-04-29 03:13:37 +00:00
|
|
|
});
|
|
|
|
}, 500);
|
2015-11-23 19:19:02 +00:00
|
|
|
};
|
|
|
|
|
2016-05-17 00:47:34 +00:00
|
|
|
//Reload our form
|
|
|
|
$scope.reloadForm();
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-04-12 02:14:38 +00:00
|
|
|
]);
|