dash = (function() { var defaultWorkspace = { name: "Riemann", view: { type: 'Balloon', child: { type: 'VStack', children: [ { type: 'Title', title: "Riemann" }, { type: 'Help', } ] } } }; var currentWorkspaceId = null; var workspaces = []; var currentView; // Find a workspace's position. var workspaceIndex = function(workspace) { if (workspace === null || workspace === undefined) { return null; } for (var i = 0; i < workspaces.length; i++) { if (workspaces[i].id === workspace.id) { return i; } } return null; } // Find a workspace by ID. 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); } var currentWorkspaceIndex = function() { return workspaceIndex({id: currentWorkspaceId}); } // Preserve current workspace state var stash = function() { var currentIndex = currentWorkspaceIndex(); if (currentIndex != null) { //console.log(util.merge(currentWorkspace(), {view: currentView.json()})); workspaces[currentIndex] = util.merge(currentWorkspace(), {view: currentView.json()}); } toolbar.workspaces(workspaces); toolbar.workspace(currentWorkspace()); } // Make a new workspace var newWorkspace = function() { 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(); if (currentView) { currentView.delete(); } // 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(); } // Delete a workspace. var deleteWorkspace = function(workspace) { console.log("Deleting", workspace); var index = currentWorkspaceIndex(); console.log("at", index); workspaces = _.filter(workspaces, function(w) { return w.id !== workspace.id }); console.log("New workspaces:", workspaces); toolbar.workspaces(workspaces); console.log("Switching to", workspaces[Math.max(index, workspaces.length - 1)]); switchWorkspace(workspaces[Math.min(index, workspaces.length - 1)]); } // Reload the dash. var reload = function() { persistence.load(function(config) { // Server var server = config.server || '127.0.0.1:5556'; subs.server(server); toolbar.server(server); // Workspaces if (config.workspaces) { workspaces = config.workspaces; 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]; } var replacement = workspace(currentWorkspace.id); currentWorkspaceId = null; 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() { stash(); persistence.save( { server: toolbar.server(), workspaces: workspaces }, function() { toastr.info("Configuration saved.") }, function(xhr, msg) { console.log("Error saving config", msg); toastr.error("Error saving config: " + msg); } ); } var help = function() { var dialog = $( '