2015-07-06 04:29:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-17 02:45:17 +00:00
|
|
|
|
|
|
|
function removeDateFieldsFunc(o) {
|
2015-11-06 17:25:30 +00:00
|
|
|
var clone = _.clone(o);
|
2016-04-17 02:45:17 +00:00
|
|
|
function eachObject(v,k){
|
2016-05-17 01:53:17 +00:00
|
|
|
|
2016-04-17 02:45:17 +00:00
|
|
|
if(k === 'lastModified' || k === 'created'){
|
2016-05-20 19:41:18 +00:00
|
|
|
delete clone[k];
|
2016-04-17 02:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var i=0; i<clone.length; i++){
|
2016-05-17 01:53:17 +00:00
|
|
|
_.each(clone[i], eachObject);
|
2015-11-06 17:25:30 +00:00
|
|
|
}
|
|
|
|
return clone;
|
2016-04-17 02:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_.mixin({ removeDateFields : removeDateFieldsFunc });
|
2015-10-30 18:40:02 +00:00
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', function($rootScope, $timeout) {
|
2016-05-17 01:53:17 +00:00
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
return {
|
|
|
|
require: ['^form'],
|
|
|
|
restrict: 'AE',
|
|
|
|
link: function($scope, $element, $attrs, $ctrls) {
|
|
|
|
//DAVID: TODO: Do we really need to check if our directive element is ready everytime
|
|
|
|
angular.element(document).ready(function() {
|
|
|
|
|
|
|
|
var $formCtrl = $ctrls[0],
|
|
|
|
savePromise = null;
|
|
|
|
|
|
|
|
$rootScope.finishedRender = false;
|
|
|
|
$scope.$on('editFormFields Started', function(ngRepeatFinishedEvent) {
|
2015-11-06 17:41:55 +00:00
|
|
|
// console.log('hello');
|
2015-11-06 17:25:30 +00:00
|
|
|
$rootScope.finishedRender = false;
|
|
|
|
});
|
|
|
|
$scope.$on('editFormFields Finished', function(ngRepeatFinishedEvent) {
|
|
|
|
$rootScope.finishedRender = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.anyDirtyAndTouched = function(form){
|
|
|
|
var propCount = 0;
|
|
|
|
for(var prop in form) {
|
|
|
|
if(form.hasOwnProperty(prop) && prop[0] !== '$') {
|
|
|
|
propCount++;
|
|
|
|
if(form[prop].$touched && form[prop].$dirty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
var debounceSave = function () {
|
|
|
|
$rootScope.saveInProgress = true;
|
|
|
|
|
|
|
|
$rootScope[$attrs.autoSaveCallback](true,
|
|
|
|
function(err){
|
|
|
|
if(!err){
|
2016-05-17 01:53:17 +00:00
|
|
|
//console.log('\n\nForm data persisted -- setting pristine flag');
|
|
|
|
$formCtrl.$setPristine();
|
|
|
|
$formCtrl.$setUntouched();
|
2015-11-06 17:25:30 +00:00
|
|
|
}else{
|
|
|
|
console.error('Error form data NOT persisted');
|
|
|
|
console.error(err);
|
|
|
|
}
|
2016-05-17 01:53:17 +00:00
|
|
|
});
|
2015-11-06 17:25:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//Update/Save Form if any Form fields are Dirty and Touched
|
|
|
|
$scope.$watch(function(newValue, oldValue) {
|
2016-05-17 01:53:17 +00:00
|
|
|
//console.log('introParagraphStartPage.$dirty: '+$scope.editForm.introParagraphStartPage.$dirty);
|
|
|
|
//console.log('introParagraphStartPage.$touched: '+$scope.editForm.introParagraphStartPage.$touched);
|
2015-11-06 17:25:30 +00:00
|
|
|
if($rootScope.finishedRender && $scope.anyDirtyAndTouched($scope.editForm) && !$rootScope.saveInProgress){
|
2016-05-17 01:53:17 +00:00
|
|
|
//console.log('Form saving started');
|
2015-11-06 17:25:30 +00:00
|
|
|
debounceSave();
|
2016-05-17 01:53:17 +00:00
|
|
|
//console.log('introParagraphStartPage.$dirty AFTER: '+$scope.editForm.introParagraphStartPage.$dirty);
|
2015-11-06 17:25:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-01 01:06:45 +00:00
|
|
|
//Autosave Form when model (specified in $attrs.autoSaveWatch) changes
|
2015-11-06 17:25:30 +00:00
|
|
|
$scope.$watch($attrs.autoSaveWatch, function(newValue, oldValue) {
|
|
|
|
|
|
|
|
newValue = angular.copy(newValue);
|
|
|
|
oldValue = angular.copy(oldValue);
|
|
|
|
|
|
|
|
newValue.form_fields = _.removeDateFields(newValue.form_fields);
|
|
|
|
oldValue.form_fields = _.removeDateFields(oldValue.form_fields);
|
|
|
|
|
|
|
|
var changedFields = !_.isEqual(oldValue.form_fields,newValue.form_fields) || !_.isEqual(oldValue.startPage, newValue.startPage);
|
2015-11-23 19:19:02 +00:00
|
|
|
var changedFieldMap = false;
|
|
|
|
|
|
|
|
if(oldValue.hasOwnProperty('plugins.oscarhost.settings.fieldMap')){
|
|
|
|
changedFieldMap = !!oldValue.plugins.oscarhost.settings.fieldMap && !_.isEqual(oldValue.plugins.oscarhost.settings.fieldMap,newValue.plugins.oscarhost.settings.fieldMap);
|
|
|
|
}
|
2015-11-06 17:25:30 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
//If our form is undefined, don't save form
|
2015-11-06 17:25:30 +00:00
|
|
|
if( (!newValue && !oldValue) || !oldValue ){
|
|
|
|
return;
|
|
|
|
}
|
2016-05-17 01:53:17 +00:00
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
// console.log('Autosaving');
|
|
|
|
// console.log('\n\n----------');
|
|
|
|
// console.log('!$dirty: '+ !$formCtrl.$dirty );
|
|
|
|
// console.log('changedFields: '+changedFields);
|
|
|
|
// console.log('changedFieldMap: '+changedFieldMap);
|
|
|
|
// console.log('finishedRender: '+$rootScope.finishedRender);
|
|
|
|
// console.log('!saveInProgress: '+!$rootScope.saveInProgress);
|
|
|
|
// console.log('newValue: '+newValue);
|
|
|
|
// console.log('oldValue: '+oldValue);
|
|
|
|
// console.log(oldValue.form_fields);
|
|
|
|
// console.log(newValue.form_fields);
|
2015-11-06 17:41:55 +00:00
|
|
|
|
2016-05-17 01:53:17 +00:00
|
|
|
if(oldValue.form_fields.length === 0) {
|
2015-11-23 19:19:02 +00:00
|
|
|
$rootScope.finishedRender = true;
|
2015-11-06 17:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Save form ONLY IF rendering is finished, form_fields have been changed AND currently not save in progress
|
|
|
|
if( $rootScope.finishedRender && ((changedFields && !$formCtrl.$dirty) || changedFieldMap) && !$rootScope.saveInProgress) {
|
|
|
|
|
|
|
|
if(savePromise) {
|
|
|
|
$timeout.cancel(savePromise);
|
|
|
|
savePromise = null;
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:53:17 +00:00
|
|
|
savePromise = $timeout(function() {
|
|
|
|
debounceSave();
|
|
|
|
});
|
2015-11-06 17:25:30 +00:00
|
|
|
}
|
|
|
|
//If we are finished rendering then form saving should be finished
|
|
|
|
else if($rootScope.finishedRender && $rootScope.saveInProgress){
|
|
|
|
$rootScope.saveInProgress = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-05-17 01:53:17 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
}]);
|