tellform/public/modules/forms/directives/auto-save.client.directive.js

158 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-07-06 04:29:05 +00:00
'use strict';
angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', function($rootScope, $timeout) {
return {
require: ['^form'],
2015-08-05 22:24:24 +00:00
restrict: 'AE',
controller: function ($scope) {
},
2015-07-06 04:29:05 +00:00
link: function($scope, $element, $attrs, $ctrls) {
2015-08-05 22:24:24 +00:00
angular.element(document).ready(function() {
var $formCtrl = $ctrls[0],
savePromise = null;
2015-07-06 04:29:05 +00:00
2015-08-05 22:24:24 +00:00
$rootScope.finishedRender = false;
$scope.$on('editFormFieldsStarted', function(ngRepeatFinishedEvent) {
$rootScope.finishedRender = false;
});
$scope.$on('editFormFieldsFinished', function(ngRepeatFinishedEvent) {
$rootScope.finishedRender = true;
});
2015-07-28 22:29:07 +00:00
2015-08-05 22:24:24 +00:00
$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;
};
2015-08-05 22:24:24 +00:00
var debounceUpdates = function () {
2015-07-06 04:29:05 +00:00
2015-08-05 22:24:24 +00:00
// console.log('Saving Form');
if(savePromise) {
$timeout.cancel(savePromise);
}else {
savePromise = $timeout(function() {
2015-07-06 04:29:05 +00:00
2015-08-05 22:24:24 +00:00
$rootScope[$attrs.autoSaveCallback](
function(err){
if(!err){
// console.log('\n\nForm data persisted -- setting pristine flag');
// console.log('\n\n---------\nUpdate form CLIENT');
// console.log(Date.now());
$formCtrl.$setPristine();
console.log($rootScope.saveInProgress);
savePromise = null;
// $formCtrl.$setUntouched();
}else{
console.error('Error form data NOT persisted');
console.error(err);
}
});
2015-07-10 20:31:09 +00:00
2015-08-05 22:24:24 +00:00
});
}
};
// $scope.$watch('editForm', function(newValue, oldValue) {
// console.log('watch editForm');
// if($scope.anyDirtyAndTouched($scope.editForm)){
// console.log('ready to save text input');
// }
// });
$scope.$watch(function(newValue, oldValue) {
// console.log('watch editForm');
if($scope.anyDirtyAndTouched($scope.editForm) && !$rootScope.saveInProgress){
console.log('ready to save text input');
2015-08-04 21:06:16 +00:00
console.log('Saving Form');
2015-08-05 22:24:24 +00:00
$rootScope.saveInProgress = true;
$rootScope[$attrs.autoSaveCallback](false,
function(err){
if(!err){
console.log('\n\nForm data persisted -- setting pristine flag');
// console.log('\n\n---------\nUpdate form CLIENT');
// console.log(Date.now());
$formCtrl.$setPristine();
}else{
console.error('Error form data NOT persisted');
console.error(err);
}
});
}
});
//Autosave Form when model (specificed in $attrs.autoSaveWatch) changes
$scope.$watch($attrs.autoSaveWatch, function(newValue, oldValue) {
var changedFields = !_.isEqual(oldValue,newValue);
if( (!newValue && !oldValue) || !oldValue ){
return;
}
// console.log('\n\n----------');
// console.log('$dirty: '+ $formCtrl.$dirty );
// console.log('changedFields: '+changedFields);
// console.log('finishedRender: '+$rootScope.finishedRender);
// console.log('saveInProgress: '+$rootScope.saveInProgress);
// console.log('newValue: '+newValue);
// console.log('oldValue: '+oldValue);
// var anyCurrentlyDirtyNotFocused = $scope.anyDirtyAndTouched($scope.editForm);
// console.log('anyCurrentlyDirtyNotFocused: '+anyCurrentlyDirtyNotFocused);
// console.log('properties of $scope.editForm');
// for(var item in $scope.editForm){
// console.log(item);
// }
//Save form ONLY IF rendering is finished, form_fields have been change AND currently not save in progress
if($rootScope.finishedRender && (changedFields && !$formCtrl.$dirty) && !$rootScope.saveInProgress) {
2015-07-10 20:31:09 +00:00
if(savePromise) {
$timeout.cancel(savePromise);
2015-08-05 22:24:24 +00:00
savePromise = null;
2015-07-10 20:31:09 +00:00
}
2015-07-06 04:29:05 +00:00
2015-07-10 20:31:09 +00:00
savePromise = $timeout(function() {
2015-08-05 22:24:24 +00:00
console.log('Saving Form');
$rootScope.saveInProgress = true;
$rootScope[$attrs.autoSaveCallback](false,
function(err){
if(!err){
2015-07-28 22:29:07 +00:00
console.log('\n\nForm data persisted -- setting pristine flag');
// console.log('\n\n---------\nUpdate form CLIENT');
// console.log(Date.now());
2015-08-05 22:24:24 +00:00
$formCtrl.$setPristine();
}else{
2015-08-04 21:06:16 +00:00
console.error('Error form data NOT persisted');
console.error(err);
}
2015-08-05 22:24:24 +00:00
});
});
}else if($rootScope.finishedRender && $rootScope.saveInProgress){
$rootScope.saveInProgress = false;
}
}, true);
});
2015-07-06 04:29:05 +00:00
}
};
2015-08-05 22:24:24 +00:00
}]);