lib/riemann/dash/public/dash.js in riemann-dash-0.2.1 vs lib/riemann/dash/public/dash.js in riemann-dash-0.2.3
- old
+ new
@@ -35,19 +35,23 @@
}
return null;
}
// Find a workspace by ID.
- var workspace = function(id) {
- for (var i = 0; i < workspaces.length; i++) {
- if (workspaces[i].id === id) {
- return workspaces[i];
- }
- }
- return null;
- }
+ var workspaceByProperty = function(property) {
+ return function(match) {
+ return _.find(workspaces, function(w) {
+ return _.isEqual(match, w[property]);
+ }) || null;
+ // for some reason we can't return undefined and we must
+ // explicitly return null upon failure
+ };
+ };
+ var workspace = workspaceByProperty("id");
+ var workspaceByName = workspaceByProperty("name");
+
// Get current workspace.
var currentWorkspace = function() {
return workspace(currentWorkspaceId);
}
@@ -58,11 +62,11 @@
// Preserve current workspace state
var stash = function() {
var currentIndex = currentWorkspaceIndex();
if (currentIndex != null) {
//console.log(util.merge(currentWorkspace(), {view: currentView.json()}));
- workspaces[currentIndex] =
+ workspaces[currentIndex] =
util.merge(currentWorkspace(), {view: currentView.json()});
}
toolbar.workspaces(workspaces);
toolbar.workspace(currentWorkspace());
}
@@ -71,10 +75,23 @@
var w = _.clone(defaultWorkspace);
w.id = util.uniqueId();
return w;
}
+ var getLocation = function() {
+ return window.location.hash.slice(1);
+ };
+
+ var toLocation = function(workspace) {
+ window.history.pushState(workspace, workspace.name, "#" + workspace.name);
+ };
+
+ var switchWorkspaceByName = function(name) {
+ var workspace = workspaceByName(name) || workspaces[0];
+ return switchWorkspace(workspace);
+ };
+
// Switch between workspaces.
var switchWorkspace = function(workspace) {
// Shut down current setup
stash();
view.unfocus();
@@ -85,10 +102,17 @@
// Switch
currentWorkspaceId = workspace.id;
toolbar.workspace(workspace);
+ document.title = workspace.name;
+
+ // update URL
+ if (getLocation() !== workspace.name) {
+ toLocation(workspace);
+ }
+
// Create new view
currentView = view.reify(
$.extend({container: $('#view')}, workspace.view));
currentView.reflow();
}
@@ -119,26 +143,28 @@
workspaces.forEach(function(w) {
w.id = w.id || util.uniqueId();
});
toolbar.workspaces(workspaces);
}
-
+
// Ensure there's a default workspace.
if (workspaces.length === 0) {
workspaces = [defaultWorkspace];
}
- // Don't preserve current state.
var replacement = workspace(currentWorkspace.id);
currentWorkspaceId = null;
- if (replacement) {
- // Load current workspace, if it's still there
- switchWorkspace(replacement);
- } else {
- // Or revert to the first workspace
+
+ var currentLocation = getLocation()
+ if (currentLocation) { // check URL first
+ switchWorkspaceByName(currentLocation);
+ } else if (replacement) { // otherwise use replacement
+ switchWorkspace(replacement)
+ } else { // failing that use the first
switchWorkspace(workspaces[0]);
}
+
});
}
// Save everything.
var save = function() {
@@ -148,11 +174,11 @@
{
server: toolbar.server(),
workspaces: workspaces
},
function() { toastr.info("Configuration saved.") },
- function(xhr, msg) {
+ function(xhr, msg) {
console.log("Error saving config", msg);
toastr.error("Error saving config: " + msg);
}
);
}
@@ -247,10 +273,10 @@
try {
view.focused().refocus();
} catch (e) { }
}
});
-
+
return {
workspaces: function() { return workspaces },
currentWorkspace: function() { return currentWorkspace },
workspaceIndex: workspaceIndex,
switchWorkspace: switchWorkspace,