mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-12 07:57:41 +00:00
GUAC-278: Migrate to GuacamoleSessionStorage which uses localStorage only if available.
This commit is contained in:
@@ -21,96 +21,140 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Maintains state across multiple Guacamole pages via HTML5 Web Storage.
|
||||
* @constructor
|
||||
* Global storage for Guacamole pages.
|
||||
*/
|
||||
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
|
||||
* @type String
|
||||
*/
|
||||
var guac_state = this;
|
||||
var stored_json = "{}";
|
||||
|
||||
/**
|
||||
* The last read state object.
|
||||
* @private
|
||||
* Called whenever an item value changes.
|
||||
*
|
||||
* @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,
|
||||
* deleted, or new properties.
|
||||
* All attached listeners.
|
||||
*
|
||||
* @type onchange[]
|
||||
*/
|
||||
this.reload = function() {
|
||||
var listeners = [];
|
||||
|
||||
// Pull current state
|
||||
var new_state = JSON.parse(localStorage.getItem("GUACAMOLE_STATE") || "{}");
|
||||
|
||||
// Assign new state
|
||||
var old_state = state;
|
||||
state = new_state;
|
||||
/**
|
||||
* Notifies all listeners that an item has changed.
|
||||
*
|
||||
* @param {String} name The name of the item that changed.
|
||||
* @param value The new item value.
|
||||
*/
|
||||
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) {
|
||||
|
||||
// If value changed, call handler
|
||||
var old = old_state[name];
|
||||
if (old != new_state[name]) {
|
||||
|
||||
// Call change handler
|
||||
if (guac_state.onchange)
|
||||
guac_state.onchange(state, new_state, name);
|
||||
/**
|
||||
* Returns the value stored within the item having the given name.
|
||||
*
|
||||
* @param {String} name The name of the item to read.
|
||||
* @param [value] The default value, if any.
|
||||
* @return The value of the given item.
|
||||
*/
|
||||
this.getItem = 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) {}
|
||||
}
|
||||
|
||||
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
|
||||
window.addEventListener("storage", guac_state.reload, false);
|
||||
window.addEventListener("storage", function reload() {
|
||||
|
||||
// Initial load
|
||||
guac_state.reload();
|
||||
// Pull current state
|
||||
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);
|
||||
};
|
||||
|
||||
})();
|
||||
|
Reference in New Issue
Block a user