mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-10 07:01:21 +00:00
Maintain proper history with roughly six entries, continuously update thumbnails.
This commit is contained in:
@@ -3,8 +3,53 @@
|
||||
*/
|
||||
GuacamoleHistory = new (function() {
|
||||
|
||||
var history =
|
||||
JSON.parse(localStorage.getItem("GUAC_HISTORY") || "{}");
|
||||
/**
|
||||
* Reference to this GuacamoleHistory.
|
||||
*/
|
||||
var guac_history = this;
|
||||
|
||||
/**
|
||||
* The number of entries to allow before removing old entries based on the
|
||||
* cutoff.
|
||||
*/
|
||||
var IDEAL_LENGTH = 6;
|
||||
|
||||
/**
|
||||
* The maximum age of a history entry before it is removed, in
|
||||
* milliseconds.
|
||||
*/
|
||||
var CUTOFF_AGE = 900000;
|
||||
|
||||
var history = {};
|
||||
|
||||
function truncate() {
|
||||
|
||||
// Avoid history growth beyond defined number of entries
|
||||
if (history.length > IDEAL_LENGTH) {
|
||||
|
||||
// Build list of entries
|
||||
var entries = [];
|
||||
for (var old_entry in history)
|
||||
entries.push(old_entry);
|
||||
|
||||
// Sort list
|
||||
entries.sort(GuacamoleHistory.Entries.compare);
|
||||
|
||||
// Remove entries until length is ideal or all are recent
|
||||
var now = new Date().getTime();
|
||||
while (entries.length > IDEAL_LENGTH
|
||||
&& entries[0].accessed - now > CUTOFF_AGE) {
|
||||
|
||||
// Remove entry
|
||||
var removed = entries.shift();
|
||||
delete history[removed.id];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the URL for the thumbnail of the connection with the given ID,
|
||||
@@ -29,12 +74,67 @@ GuacamoleHistory = new (function() {
|
||||
|
||||
// Store entry in history
|
||||
history[id] = entry;
|
||||
truncate();
|
||||
|
||||
// Save updated history
|
||||
localStorage.setItem("GUAC_HISTORY", JSON.stringify(history));
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Reloads all history data.
|
||||
*/
|
||||
this.reload = function() {
|
||||
|
||||
// Get old and new for comparison
|
||||
var old_history = history;
|
||||
var new_history = JSON.parse(localStorage.getItem("GUAC_HISTORY") || "{}");
|
||||
|
||||
// Update history
|
||||
history = new_history;
|
||||
|
||||
// Call onchange handler as necessary
|
||||
if (guac_history.onchange) {
|
||||
|
||||
// Produce union of all known IDs
|
||||
var known_ids = {};
|
||||
for (var new_id in new_history) known_ids[new_id] = true;
|
||||
for (var old_id in old_history) known_ids[old_id] = true;
|
||||
|
||||
// For each known ID
|
||||
for (var id in known_ids) {
|
||||
|
||||
// Get entries
|
||||
var old_entry = old_history[id];
|
||||
var new_entry = new_history[id];
|
||||
|
||||
// Call handler for all changed
|
||||
if (!old_entry || !new_entry
|
||||
|| old_entry.accessed != new_entry.accessed)
|
||||
guac_history.onchange(id, old_entry, new_entry);
|
||||
|
||||
}
|
||||
|
||||
} // end onchange
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Event handler called whenever a history entry is changed.
|
||||
*
|
||||
* @event
|
||||
* @param {String} id The ID of the connection whose history entry is
|
||||
* changing.
|
||||
* @param {GuacamoleHistory.Entry} old_entry The old value of the entry, if
|
||||
* any.
|
||||
* @param {GuacamoleHistory.Entry} new_entry The new value of the entry, if
|
||||
* any.
|
||||
*/
|
||||
this.onchange = null;
|
||||
|
||||
// Initial load
|
||||
guac_history.reload();
|
||||
|
||||
})();
|
||||
|
||||
/**
|
||||
@@ -66,4 +166,7 @@ GuacamoleHistory.Entry = function(id, thumbnail, last_access) {
|
||||
this.accessed = last_access;
|
||||
|
||||
};
|
||||
|
||||
|
||||
GuacamoleHistory.Entry.compare = function(a, b) {
|
||||
return a.accessed - b.accessed;
|
||||
};
|
||||
|
Reference in New Issue
Block a user