Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onBeforeRestoreAll and onRestoreAll #154

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 59 additions & 16 deletions sisyphus.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@
autoRelease: true,
onBeforeSave: function() {},
onSave: function() {},
onBeforeRestoreAll: function() {},
onBeforeRestore: function() {},
onRestoreAll: function() {},
onRestore: function() {},
onRelease: function() {}
};
Expand Down Expand Up @@ -172,7 +174,7 @@
return false;
}

var callback_result = self.options.onBeforeRestore.call( self );
var callback_result = self.options.onBeforeRestoreAll.call( self );
if ( callback_result === undefined || callback_result ) {
self.restoreAllData();
}
Expand Down Expand Up @@ -321,28 +323,69 @@

self.targets.each( function() {
var target = $( this );
var targetFormIdAndName = getElementIdentifier( $( this ) );

self.findFieldsToProtect( target ).each( function() {
if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
// Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
return true;
}
var field = $( this );
var prefix = (self.options.locationBased ? self.href : "") + targetFormIdAndName + getElementIdentifier( field ) + self.options.customKeySuffix;
var resque = self.browserStorage.get( prefix );
if ( resque !== null ) {
self.restoreFieldsData( field, resque );
restored = true;
}
} );
if ( self.restoreData(target) ) {
restored = true;
}
} );

if ( restored ) {
self.options.onRestore.call( self );
self.options.onRestoreAll.call( self );
}
},

restoreData: function ( target ) {
var self = this;

if ( self.dataIsRestorable( target ) ) {
var callback_result = self.options.onBeforeRestore.call( self, target );

if ( callback_result === undefined || callback_result ) {
var targetFormIdAndName = getElementIdentifier( target );

self.findFieldsToProtect( target ).each( function() {
if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
// Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
return true;
}
var field = $( this );
var prefix = (self.options.locationBased ? self.href : "") + targetFormIdAndName + getElementIdentifier( field ) + self.options.customKeySuffix;
var resque = self.browserStorage.get( prefix );
if ( resque !== null ) {
self.restoreFieldsData( field, resque );
self.options.onRestore.call( self, target );
}
} );
}

return true;
} else {
return false;
}
},

dataIsRestorable: function ( target ) {
var self = this;
var restorable = false;
var targetFormIdAndName = getElementIdentifier( target );

self.findFieldsToProtect( target ).each( function() {
if ( $.inArray( this, self.options.excludeFields ) !== -1 ) {
return true;
}

var field = $( this );
var prefix = (self.options.locationBased ? self.href : "") + targetFormIdAndName + getElementIdentifier( field ) + self.options.customKeySuffix;
var resque = self.browserStorage.get( prefix );
if ( resque !== null ) {
restorable = true;
return false;
}
} );

return restorable;
},

/**
* Restore form field data from local storage
*
Expand Down