1 /* See license.txt for terms of usage */ 2 3 define([ 4 "firebug/lib/trace", 5 ], 6 function(FBTrace) { 7 8 // ********************************************************************************************* // 9 // Constants 10 11 var Persist = {}; 12 13 const overrideDefaultsWithPersistedValuesTimeout = 500; 14 15 // ********************************************************************************************* // 16 // Persistence (cross page refresh) 17 18 Persist.persistObjects = function(panel, panelState) 19 { 20 // Persist the location and selection so we can restore them in case of a reload 21 if (panel.location) 22 panelState.persistedLocation = Persist.persistObject(panel.location, panel.context); // fn(context)->location 23 24 if (panel.selection) 25 panelState.persistedSelection = Persist.persistObject(panel.selection, panel.context); 26 27 if (FBTrace.DBG_INITIALIZE) 28 FBTrace.sysout("lib.persistObjects "+panel.name+" panel.location:"+panel.location+ 29 " panel.selection:"+panel.selection+" panelState:", panelState); 30 }; 31 32 Persist.persistObject = function(object, context) 33 { 34 var rep = Firebug.getRep(object, context); 35 return rep ? rep.persistObject(object, context) : null; 36 }; 37 38 Persist.restoreLocation = function(panel, panelState) 39 { 40 var restored = false; 41 42 if (!panel.location && panelState && panelState.persistedLocation) 43 { 44 var location = panelState.persistedLocation(panel.context); 45 46 if (FBTrace.DBG_INITIALIZE) 47 FBTrace.sysout("lib.restoreObjects "+panel.name+" persistedLocation: "+location+ 48 " panelState:", panelState); 49 50 if (location) 51 { 52 panel.navigate(location); 53 restored = true; 54 } 55 } 56 57 if (!panel.location) 58 panel.navigate(null); 59 60 if (FBTrace.DBG_INITIALIZE) 61 FBTrace.sysout("lib.restoreLocation panel.location: "+panel.location+" restored: "+ 62 restored+" panelState:", panelState); 63 64 return restored; 65 }; 66 67 Persist.restoreSelection = function(panel, panelState) 68 { 69 var needRetry = false; 70 71 if (!panel.selection && panelState && panelState.persistedSelection) 72 { 73 var selection = panelState.persistedSelection(panel.context); 74 if (selection) 75 panel.select(selection); 76 else 77 needRetry = true; 78 } 79 80 if (!panel.selection) // Couldn't restore the selection, so select the default object 81 panel.select(null); 82 83 if (needRetry) 84 { 85 function overrideDefaultWithPersistedSelection() 86 { 87 if (panel.selection == panel.getDefaultSelection() && panelState.persistedSelection) 88 { 89 var selection = panelState.persistedSelection(panel.context); 90 if (selection) 91 panel.select(selection); 92 } 93 94 if (FBTrace.DBG_INITIALIZE) 95 FBTrace.sysout("lib.overrideDefaultsWithPersistedValues "+panel.name+ 96 " panel.location: "+panel.location+" panel.selection: "+panel.selection+ 97 " panelState:", panelState); 98 } 99 100 // If we couldn't restore the selection, wait a bit and try again 101 panel.context.setTimeout(overrideDefaultWithPersistedSelection, 102 overrideDefaultsWithPersistedValuesTimeout); 103 } 104 105 if (FBTrace.DBG_INITIALIZE) 106 FBTrace.sysout("lib.restore "+panel.name+" needRetry "+needRetry+" panel.selection: "+ 107 panel.selection+" panelState:", panelState); 108 }; 109 110 Persist.restoreObjects = function(panel, panelState) 111 { 112 Persist.restoreLocation(panel, panelState); 113 Persist.restoreSelection(panel, panelState); 114 }; 115 116 Persist.getPersistedState = function(context, panelName) 117 { 118 if (!context) 119 return null; 120 121 var persistedState = context.persistedState; 122 if (!persistedState) 123 persistedState = context.persistedState = {}; 124 125 if (!persistedState.panelState) 126 persistedState.panelState = {}; 127 128 var panelState = persistedState.panelState[panelName]; 129 if (!panelState) 130 panelState = persistedState.panelState[panelName] = {}; 131 132 return panelState; 133 }; 134 135 // ********************************************************************************************* // 136 137 return Persist; 138 139 // ********************************************************************************************* // 140 }); 141