From 067b49ec50ef1db31f017972e683bed6d7e5866f Mon Sep 17 00:00:00 2001 From: Brendon Muir Date: Thu, 7 Jun 2018 19:59:33 +1200 Subject: [PATCH] Add onBeforeRestoreAll and onRestoreAll `onBeforeRestore` and `onRestore` are now granular and provide the target form. They also don't trigger if there is no data to restore for that particular form. `onBeforeRestoreAll` and `onRestoreAll` operate like `onBeforeRestore` and `onRestore` used to. No tests so far. --- sisyphus.js | 75 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/sisyphus.js b/sisyphus.js index 16cb449..9588a38 100644 --- a/sisyphus.js +++ b/sisyphus.js @@ -127,7 +127,9 @@ autoRelease: true, onBeforeSave: function() {}, onSave: function() {}, + onBeforeRestoreAll: function() {}, onBeforeRestore: function() {}, + onRestoreAll: function() {}, onRestore: function() {}, onRelease: function() {} }; @@ -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(); } @@ -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 *