1 | |
---|
2 | /* |
---|
3 | * Guacamole - Clientless Remote Desktop |
---|
4 | * Copyright (C) 2010 Michael Jumper |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU Affero General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU Affero General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Affero General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * Maintains state across multiple Guacamole pages via HTML5 Web Storage. |
---|
22 | * @constructor |
---|
23 | */ |
---|
24 | function GuacamoleSessionState() { |
---|
25 | |
---|
26 | /** |
---|
27 | * Reference to this GuacamoleSessionState. |
---|
28 | * @private |
---|
29 | */ |
---|
30 | var guac_state = this; |
---|
31 | |
---|
32 | /** |
---|
33 | * The last read state object. |
---|
34 | * @private |
---|
35 | */ |
---|
36 | var state = localStorage.getItem("GUACAMOLE_STATE") || {}; |
---|
37 | |
---|
38 | /** |
---|
39 | * Reloads the internal state, sending onchange events for all changed, |
---|
40 | * deleted, or new properties. |
---|
41 | */ |
---|
42 | this.reload = function() { |
---|
43 | |
---|
44 | // Pull current state |
---|
45 | var new_state = JSON.parse(localStorage.getItem("GUACAMOLE_STATE") || "{}"); |
---|
46 | |
---|
47 | // Assign new state |
---|
48 | var old_state = state; |
---|
49 | state = new_state; |
---|
50 | |
---|
51 | // Check if any values are different |
---|
52 | for (var name in new_state) { |
---|
53 | |
---|
54 | // If value changed, call handler |
---|
55 | var old = old_state[name]; |
---|
56 | if (old != new_state[name]) { |
---|
57 | |
---|
58 | // Call change handler |
---|
59 | if (guac_state.onchange) |
---|
60 | guac_state.onchange(state, new_state, name); |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | }; |
---|
67 | |
---|
68 | /** |
---|
69 | * Sets the given property to the given value. |
---|
70 | * |
---|
71 | * @param {String} name The name of the property to change. |
---|
72 | * @param value An arbitrary value. |
---|
73 | */ |
---|
74 | this.setProperty = function(name, value) { |
---|
75 | state[name] = value; |
---|
76 | localStorage.setItem("GUACAMOLE_STATE", JSON.stringify(state)); |
---|
77 | }; |
---|
78 | |
---|
79 | /** |
---|
80 | * Returns the value stored under the property having the given name. |
---|
81 | * |
---|
82 | * @param {String} name The name of the property to read. |
---|
83 | * @return The value of the given property. |
---|
84 | */ |
---|
85 | this.getProperty = function(name) { |
---|
86 | return state[name]; |
---|
87 | }; |
---|
88 | |
---|
89 | /** |
---|
90 | * Event which is fired whenever a property value is changed externally. |
---|
91 | * |
---|
92 | * @event |
---|
93 | * @param old_state An object whose properties' values are the old values |
---|
94 | * of this GuacamoleSessionState. |
---|
95 | * @param new_state An object whose properties' values are the new values |
---|
96 | * of this GuacamoleSessionState. |
---|
97 | * @param {String} name The name of the property that is being changed. |
---|
98 | */ |
---|
99 | this.onchange = null; |
---|
100 | |
---|
101 | // Reload when modified |
---|
102 | window.addEventListener("storage", guac_state.reload, false); |
---|
103 | |
---|
104 | // Initial load |
---|
105 | guac_state.reload(); |
---|
106 | |
---|
107 | } |
---|