2015-07-06 04:29:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('forms').directive('autoSaveForm', ['$rootScope', '$timeout', function($rootScope, $timeout) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
require: ['^form'],
|
2015-07-21 23:25:45 +00:00
|
|
|
// scope: {
|
|
|
|
// callback: '&autoSaveCallback'
|
|
|
|
// },
|
2015-07-06 04:29:05 +00:00
|
|
|
link: function($scope, $element, $attrs, $ctrls) {
|
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
$rootScope.finishedRender = false;
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
if($rootScope.watchCount === undefined){
|
2015-07-10 20:31:09 +00:00
|
|
|
$rootScope.watchCount = 0;
|
|
|
|
}
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
var difference = function(array){
|
2015-07-09 22:15:34 +00:00
|
|
|
var rest = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-09 22:15:34 +00:00
|
|
|
var containsEquals = function(obj, target) {
|
2015-07-21 23:25:45 +00:00
|
|
|
if (obj === null) return false;
|
2015-07-06 04:29:05 +00:00
|
|
|
return _.any(obj, function(value) {
|
|
|
|
return _.isEqual(value, target);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return _.filter(array, function(value){ return !containsEquals(rest, value); });
|
|
|
|
};
|
|
|
|
|
|
|
|
var $formCtrl = $ctrls[0];
|
|
|
|
var savePromise = null;
|
2015-07-28 22:29:07 +00:00
|
|
|
// $scope.finishedRender = false;
|
2015-07-06 04:29:05 +00:00
|
|
|
var expression = $attrs.autoSaveForm || 'true';
|
|
|
|
|
|
|
|
$scope.$on('ngRepeatStarted', function(ngRepeatFinishedEvent) {
|
2015-07-28 22:29:07 +00:00
|
|
|
$rootScope.finishedRender = false;
|
2015-07-10 20:31:09 +00:00
|
|
|
$rootScope.watchCount = 0;
|
2015-07-06 04:29:05 +00:00
|
|
|
});
|
|
|
|
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
|
2015-07-28 22:29:07 +00:00
|
|
|
$rootScope.finishedRender = true;
|
2015-07-06 04:29:05 +00:00
|
|
|
});
|
|
|
|
|
2015-07-07 02:52:55 +00:00
|
|
|
$scope.$watch('myform.form_fields', function(newValue, oldValue) {
|
2015-07-27 18:11:43 +00:00
|
|
|
console.log('watchCount: '+$rootScope.watchCount);
|
2015-07-07 02:52:55 +00:00
|
|
|
if(difference(oldValue,newValue).length === 0 || oldValue === undefined){
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 20:31:09 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log('\n\n----------\n$dirty: '+( $formCtrl.$dirty ) );
|
|
|
|
// console.log('form_fields changed: '+difference(oldValue,newValue).length );
|
2015-07-07 02:52:55 +00:00
|
|
|
// console.log('$valid: '+$formCtrl.$valid);
|
2015-07-28 22:29:07 +00:00
|
|
|
// console.log('finishedRender: '+$rootScope.finishedRender);
|
|
|
|
// console.log('saveInProgress: '+$rootScope.saveInProgress);
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
if($rootScope.finishedRender && ($formCtrl.$dirty || difference(oldValue,newValue).length !== 0) && !$rootScope.saveInProgress) {
|
2015-07-10 20:31:09 +00:00
|
|
|
$rootScope.watchCount++;
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
// if($rootScope.watchCount === 1) {
|
2015-07-10 20:31:09 +00:00
|
|
|
|
|
|
|
if(savePromise) {
|
|
|
|
$timeout.cancel(savePromise);
|
|
|
|
}
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-10 20:31:09 +00:00
|
|
|
savePromise = $timeout(function() {
|
|
|
|
savePromise = null;
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
$rootScope[$attrs.autoSaveCallback](
|
|
|
|
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-07-21 23:25:45 +00:00
|
|
|
$rootScope.watchCount = 0;
|
|
|
|
$formCtrl.$setPristine();
|
2015-07-28 22:29:07 +00:00
|
|
|
// $rootScope.saveInProgress = false;
|
2015-07-21 23:25:45 +00:00
|
|
|
}else{
|
|
|
|
console.log('Error form data NOT persisted');
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-10 20:31:09 +00:00
|
|
|
});
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-28 22:29:07 +00:00
|
|
|
// }
|
2015-07-27 18:11:43 +00:00
|
|
|
}else{
|
|
|
|
return;
|
2015-07-06 04:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}]);
|