GUAC-278: Migrate to GuacamoleSessionStorage which uses localStorage only if available.

This commit is contained in:
Michael Jumper
2014-04-17 16:28:08 -07:00
parent 1ccbbd8efe
commit 69da9bfc16
4 changed files with 138 additions and 104 deletions

View File

@@ -841,7 +841,7 @@ GuacUI.Client.updateDisplayScale = function() {
// If auto-fit is enabled, scale display // If auto-fit is enabled, scale display
if (!GuacUI.Client.overrideAutoFit if (!GuacUI.Client.overrideAutoFit
&& GuacUI.sessionState.getProperty("auto-fit", true)) { && GuacamoleSessionStorage.getItem("auto-fit", true)) {
// Calculate scale to fit screen // Calculate scale to fit screen
var fit_scale = Math.min( var fit_scale = Math.min(
@@ -1065,8 +1065,9 @@ GuacUI.Client.attach = function(guac) {
GuacUI.Client.titlePrefix = null; GuacUI.Client.titlePrefix = null;
// Update clipboard with current data // Update clipboard with current data
if (GuacUI.sessionState.getProperty("clipboard")) var clipboard = GuacamoleSessionStorage.getItem("clipboard");
guac.setClipboard(GuacUI.sessionState.getProperty("clipboard")); if (clipboard)
guac.setClipboard(clipboard);
break; break;
@@ -1131,7 +1132,7 @@ GuacUI.Client.attach = function(guac) {
// Set contents when done // Set contents when done
reader.onend = function clipboard_text_end() { reader.onend = function clipboard_text_end() {
GuacUI.sessionState.setProperty("clipboard", data); GuacamoleSessionStorage.setItem("clipboard", data);
}; };
}; };
@@ -1417,12 +1418,12 @@ GuacUI.Client.attach = function(guac) {
}; };
GuacUI.sessionState.onchange = function(old_state, new_state, name) { GuacamoleSessionStorage.addChangeListener(function(name, value) {
if (name === "clipboard" && GuacUI.Client.attachedClient) if (name === "clipboard" && GuacUI.Client.attachedClient)
GuacUI.Client.attachedClient.setClipboard(new_state[name]); GuacUI.Client.attachedClient.setClipboard(value);
else if (name === "auto-fit") else if (name === "auto-fit")
GuacUI.Client.updateDisplayScale(); GuacUI.Client.updateDisplayScale();
}; });
var long_press_start_x = 0; var long_press_start_x = 0;
var long_press_start_y = 0; var long_press_start_y = 0;

View File

@@ -26,11 +26,6 @@
*/ */
var GuacUI = GuacUI || {}; var GuacUI = GuacUI || {};
/**
* Current session state, including settings.
*/
GuacUI.sessionState = new GuacamoleSessionState();
/** /**
* Creates a new element having the given tagname and CSS class. * Creates a new element having the given tagname and CSS class.
*/ */
@@ -186,7 +181,7 @@ GuacUI.Audio = new (function() {
this.supported = []; this.supported = [];
// If sound disabled, we're done now. // If sound disabled, we're done now.
if (GuacUI.sessionState.getProperty("disable-sound", false)) if (GuacamoleSessionStorage.getItem("disable-sound", false))
return; return;
// Build array of supported audio formats // Build array of supported audio formats

View File

@@ -59,7 +59,6 @@ var GuacamoleRootUI = {
"connections" : document.getElementById("connection-list-ui") "connections" : document.getElementById("connection-list-ui")
}, },
"session_state" : new GuacamoleSessionState(),
"parameters" : null "parameters" : null
}; };
@@ -363,7 +362,7 @@ window.name = "";
GuacamoleRootUI.settings.auto_fit.onchange = GuacamoleRootUI.settings.auto_fit.onchange =
GuacamoleRootUI.settings.auto_fit.onclick = function() { GuacamoleRootUI.settings.auto_fit.onclick = function() {
GuacamoleRootUI.session_state.setProperty( GuacamoleSessionStorage.setItem(
"auto-fit", GuacamoleRootUI.settings.auto_fit.checked); "auto-fit", GuacamoleRootUI.settings.auto_fit.checked);
}; };
@@ -375,7 +374,7 @@ GuacamoleRootUI.settings.auto_fit.onclick = function() {
GuacamoleRootUI.settings.disable_sound.onchange = GuacamoleRootUI.settings.disable_sound.onchange =
GuacamoleRootUI.settings.disable_sound.onclick = function() { GuacamoleRootUI.settings.disable_sound.onclick = function() {
GuacamoleRootUI.session_state.setProperty( GuacamoleSessionStorage.setItem(
"disable-sound", GuacamoleRootUI.settings.disable_sound.checked); "disable-sound", GuacamoleRootUI.settings.disable_sound.checked);
}; };
@@ -389,8 +388,7 @@ GuacamoleRootUI.fields.clipboard.onchange = function() {
// Set value if changed // Set value if changed
var new_value = GuacamoleRootUI.fields.clipboard.value; var new_value = GuacamoleRootUI.fields.clipboard.value;
if (GuacamoleRootUI.session_state.getProperty("clipboard") != new_value) GuacamoleSessionStorage.setItem("clipboard", new_value);
GuacamoleRootUI.session_state.setProperty("clipboard", new_value);
}; };
@@ -398,43 +396,39 @@ GuacamoleRootUI.fields.clipboard.onchange = function() {
* Update element states when session state changes * Update element states when session state changes
*/ */
GuacamoleRootUI.session_state.onchange =
function(old_state, new_state, name) { GuacamoleSessionStorage.addChangeListener(function(name, value) {
// Clipboard // Clipboard
if (name == "clipboard") if (name === "clipboard")
GuacamoleRootUI.fields.clipboard.value = new_state[name]; GuacamoleRootUI.fields.clipboard.value = value;
// Auto-fit display // Auto-fit display
else if (name == "auto-fit") else if (name === "auto-fit")
GuacamoleRootUI.fields.auto_fit.checked = new_state[name]; GuacamoleRootUI.fields.auto_fit.checked = value;
// Disable Sound // Disable Sound
else if (name == "disable-sound") else if (name === "disable-sound")
GuacamoleRootUI.fields.disable_sound.checked = new_state[name]; GuacamoleRootUI.fields.disable_sound.checked = value;
}; });
/* /*
* Initialize clipboard with current data * Initialize clipboard with current data
*/ */
if (GuacamoleRootUI.session_state.getProperty("clipboard")) GuacamoleRootUI.fields.clipboard.value = GuacamoleSessionStorage.getItem("clipboard", "");
GuacamoleRootUI.fields.clipboard.value =
GuacamoleRootUI.session_state.getProperty("clipboard");
/* /*
* Initialize auto-fit setting in UI * Initialize auto-fit setting in UI
*/ */
GuacamoleRootUI.settings.auto_fit.checked = GuacamoleRootUI.settings.auto_fit.checked = GuacamoleSessionStorage.getItem("auto-fit", true);
GuacamoleRootUI.session_state.getProperty("auto-fit", true);
/* /*
* Initialize disable-sound setting in UI * Initialize disable-sound setting in UI
*/ */
GuacamoleRootUI.settings.disable_sound.checked = GuacamoleRootUI.settings.disable_sound.checked = GuacamoleSessionStorage.getItem("disable-sound", false);
GuacamoleRootUI.session_state.getProperty("disable-sound", false);
/* /*
* Set handler for logout * Set handler for logout

View File

@@ -21,96 +21,140 @@
*/ */
/** /**
* Maintains state across multiple Guacamole pages via HTML5 Web Storage. * Global storage for Guacamole pages.
* @constructor
*/ */
function GuacamoleSessionState() { GuacamoleSessionStorage = (opener && opener.GuacamoleSessionStorage) || new (function() {
/** /**
* Reference to this GuacamoleSessionState. * The contents of storage, as a JSON string containing name/value pairs as
* properties.
*
* @private * @private
* @type String
*/ */
var guac_state = this; var stored_json = "{}";
/** /**
* The last read state object. * Called whenever an item value changes.
* @private *
* @callback onchange
* @param {String} name The name of the item changed.
* @param value The new item value.
*/ */
var state = localStorage.getItem("GUACAMOLE_STATE") || {};
/** /**
* Reloads the internal state, sending onchange events for all changed, * All attached listeners.
* deleted, or new properties. *
* @type onchange[]
*/ */
this.reload = function() { var listeners = [];
// Pull current state /**
var new_state = JSON.parse(localStorage.getItem("GUACAMOLE_STATE") || "{}"); * Notifies all listeners that an item has changed.
*
// Assign new state * @param {String} name The name of the item that changed.
var old_state = state; * @param value The new item value.
state = new_state; */
function __notify_changed(name, value) {
for (var i=0; i<listeners.length; i++)
listeners[i](name, value);
}
// Check if any values are different /**
for (var name in new_state) { * Returns the value stored within the item having the given name.
*
// If value changed, call handler * @param {String} name The name of the item to read.
var old = old_state[name]; * @param [value] The default value, if any.
if (old != new_state[name]) { * @return The value of the given item.
*/
// Call change handler this.getItem = function(name, value) {
if (guac_state.onchange)
guac_state.onchange(state, new_state, name);
// Attempt to read JSON from localStorage, default to local variable
var json = stored_json;
if (localStorage) {
try {
json = localStorage.getItem("GUACAMOLE_STATE") || "{}";
} }
catch (ignore) {}
}
var obj = JSON.parse(json);
if (obj[name] !== undefined)
return obj[name];
return value;
};
/**
* Sets the item having the given name to the given value.
*
* @param {String} name The name of the item to change.
* @param [value] An arbitrary value.
*/
this.setItem = function(name, value) {
// Attempt to read JSON from localStorage, default to local variable
var json = stored_json;
if (localStorage) {
try {
json = localStorage.getItem("GUACAMOLE_STATE") || "{}";
}
catch (ignore) {}
}
// Modify object property
var obj = JSON.parse(json);
var old = obj[name];
obj[name] = value;
// Notify of change
if (old !== value)
__notify_changed(name, value);
// Attempt to set JSON within localStorage, default to local variable
stored_json = JSON.stringify(obj);
if (localStorage) {
try {
localStorage.setItem("GUACAMOLE_STATE", stored_json);
}
catch (ignore) {}
} }
}; };
/**
* Sets the given property to the given value.
*
* @param {String} name The name of the property to change.
* @param [value] An arbitrary value.
*/
this.setProperty = function(name, value) {
state[name] = value;
localStorage.setItem("GUACAMOLE_STATE", JSON.stringify(state));
};
/**
* Returns the value stored under the property having the given name.
*
* @param {String} name The name of the property to read.
* @param value The default value, if any.
* @return The value of the given property.
*/
this.getProperty = function(name, value) {
var current = state[name];
if (current === undefined)
return value;
return current;
};
/**
* Event which is fired whenever a property value is changed externally.
*
* @event
* @param old_state An object whose properties' values are the old values
* of this GuacamoleSessionState.
* @param new_state An object whose properties' values are the new values
* of this GuacamoleSessionState.
* @param {String} name The name of the property that is being changed.
*/
this.onchange = null;
// Reload when modified // Reload when modified
window.addEventListener("storage", guac_state.reload, false); window.addEventListener("storage", function reload() {
// Initial load // Pull current state
guac_state.reload(); var new_json = localStorage.getItem("GUACAMOLE_STATE") || "{}";
var new_state = JSON.parse(new_json);
var old_state = JSON.parse(stored_json);
} // Check if any values are different
for (var name in new_state) {
// If value changed, notify
var old = old_state[name];
if (old !== new_state[name])
__notify_changed(name, new_state[name]);
}
stored_json = new_json;
}, false);
/**
* Ensures that the given function will be called for each change in
* item value. The function must accept a single argument which will be
* the name of the item changed.
*
* @param {onchange} onchange The function to call when an item changes.
*/
this.addChangeListener = function(onchange) {
listeners.push(onchange);
};
})();