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) {
|
|
|
|
$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;
|
|
|
|
};
|
|
|
|
|
2016-07-06 01:48:25 +00:00
|
|
|
var debounceSave = function (diffChanges) {
|
2015-11-06 17:25:30 +00:00
|
|
|
|
2016-07-06 01:48:25 +00:00
|
|
|
$rootScope[$attrs.autoSaveCallback](true, diffChanges,
|
2015-11-06 17:25:30 +00:00
|
|
|
function(err){
|
|
|
|
if(!err){
|
2016-05-17 01:53:17 +00:00
|
|
|
$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
|
|
|
};
|
2016-11-02 20:32:08 +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) {
|
|
|
|
|
2016-11-02 20:32:08 +00:00
|
|
|
if( !newValue || !oldValue ) {
|
|
|
|
$rootScope.finishedRender = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
newValue = angular.copy(newValue);
|
|
|
|
oldValue = angular.copy(oldValue);
|
|
|
|
|
2016-11-02 19:43:04 +00:00
|
|
|
delete newValue.visible_form_fields;
|
|
|
|
delete oldValue.visible_form_fields;
|
2016-11-02 19:35:01 +00:00
|
|
|
newValue.form_fields = _.removeDateFields(newValue.form_fields);
|
|
|
|
oldValue.form_fields = _.removeDateFields(oldValue.form_fields);
|
2015-11-06 17:25:30 +00:00
|
|
|
|
2016-11-02 19:35:01 +00:00
|
|
|
var changedFields = !!DeepDiff.diff(oldValue, newValue) && DeepDiff.diff(oldValue, newValue).length > 0;
|
2015-11-23 19:19:02 +00:00
|
|
|
|
2016-11-02 19:35:01 +00:00
|
|
|
//If our form is undefined, don't save form
|
2016-11-02 20:32:08 +00:00
|
|
|
if(!changedFields){
|
2016-11-02 19:35:01 +00:00
|
|
|
$rootScope.finishedRender = true;
|
|
|
|
return;
|
|
|
|
}
|
2015-11-06 17:25:30 +00:00
|
|
|
|
2016-11-02 19:35:01 +00:00
|
|
|
if(oldValue.form_fields.length === 0) {
|
|
|
|
$rootScope.finishedRender = true;
|
|
|
|
}
|
2016-05-17 01:53:17 +00:00
|
|
|
|
2016-11-02 19:43:04 +00:00
|
|
|
//console.log('Autosaving');
|
|
|
|
//console.log('\n\n----------');
|
|
|
|
//console.log('!$dirty: '+ !$formCtrl.$dirty );
|
2016-11-02 20:32:08 +00:00
|
|
|
|
2015-11-06 17:25:30 +00:00
|
|
|
// console.log('changedFieldMap: '+changedFieldMap);
|
2016-11-02 20:32:08 +00:00
|
|
|
//console.log('finishedRender: '+$rootScope.finishedRender);
|
2016-11-02 19:43:04 +00:00
|
|
|
//console.log('!saveInProgress: '+!$rootScope.saveInProgress);
|
2015-11-06 17:25:30 +00:00
|
|
|
// 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
|
|
|
|
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
|
2016-11-02 19:43:04 +00:00
|
|
|
if( $rootScope.finishedRender && (changedFields) && !$rootScope.saveInProgress) {
|
2015-11-06 17:25:30 +00:00
|
|
|
|
|
|
|
if(savePromise) {
|
|
|
|
$timeout.cancel(savePromise);
|
|
|
|
savePromise = null;
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:53:17 +00:00
|
|
|
savePromise = $timeout(function() {
|
2016-07-06 01:48:25 +00:00
|
|
|
$rootScope.saveInProgress = true;
|
2016-11-02 19:35:01 +00:00
|
|
|
|
2016-07-06 01:48:25 +00:00
|
|
|
delete newValue.visible_form_fields;
|
|
|
|
delete newValue.visible_form_fields;
|
|
|
|
var _diff = DeepDiff.diff(oldValue, newValue);
|
|
|
|
debounceSave(_diff);
|
2016-05-17 01:53:17 +00:00
|
|
|
});
|
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
|
|
|
}]);
|