Files
weave-scope/client/app/scripts/utils/router-utils.js
Filip Barl 69fd397217 Initial version of the resource view (#2296)
* Added resource view selector button

* Showing resource boxes in the resource view

* Crude CPU resource view prototype

* Improved the viewMode state logic

* Extracted zooming into a separate wrapper component

* Split the layout selectors between graph-view and resource-view

* Proper zooming logic for the resource view

* Moved all node networks utils to selectors

* Improved the zoom caching logic

* Further refactoring of selectors

* Added sticky labels to the resource boxes

* Added panning translation limits in the resource view

* Renamed GridModeSelector -> ViewModeSelector

* Polished the topology resource view selection logic

* Search bar hidden in the resource view

* Added per-layer topology names to the resource view

* Made metric selectors work for the resource view

* Adjusted the viewport selectors

* Renamed viewport selector to canvas (+ maximal zoom fix)

* Showing more useful metric info in the resource box labels

* Fetching only necessary nodes for the resource view

* Refactored the resource view layer component

* Addressed first batch UI comments (from the Scope meeting)

* Switch to deep zooming transform in the resource view to avoid SVG precision errors

* Renamed and moved resource view components

* Polished all the resource view components

* Changing the available metrics selection

* Improved and polished the state transition logic for the resource view

* Separated zoom limits from the zoom active state

* Renaming and bunch of comments

* Addressed all the UI comments (@davkal + @fons)

* Made graph view selectors independent from resource view selectors
2017-03-24 14:51:53 +01:00

117 lines
3.6 KiB
JavaScript

import page from 'page';
import { route } from '../actions/app-actions';
import { storageGet, storageSet } from './storage-utils';
//
// page.js won't match the routes below if ":state" has a slash in it, so replace those before we
// load the state into the URL.
//
const SLASH = '/';
const SLASH_REPLACEMENT = '<SLASH>';
const PERCENT = '%';
const PERCENT_REPLACEMENT = '<PERCENT>';
const STORAGE_STATE_KEY = 'scopeViewState';
function encodeURL(url) {
return url
.replace(new RegExp(PERCENT, 'g'), PERCENT_REPLACEMENT)
.replace(new RegExp(SLASH, 'g'), SLASH_REPLACEMENT);
}
export function decodeURL(url) {
return decodeURIComponent(url.replace(new RegExp(SLASH_REPLACEMENT, 'g'), SLASH))
.replace(new RegExp(PERCENT_REPLACEMENT, 'g'), PERCENT);
}
export function parseHashState(hash = window.location.hash) {
const urlStateString = hash
.replace('#!/state/', '')
.replace('#!/', '') || '{}';
return JSON.parse(decodeURL(urlStateString));
}
function shouldReplaceState(prevState, nextState) {
// Opening a new terminal while an existing one is open.
const terminalToTerminal = (prevState.controlPipe && nextState.controlPipe);
// Closing a terminal.
const closingTheTerminal = (prevState.controlPipe && !nextState.controlPipe);
return terminalToTerminal || closingTheTerminal;
}
export function getUrlState(state) {
const cp = state.get('controlPipes').last();
const nodeDetails = state.get('nodeDetails').toIndexedSeq().map(details => ({
id: details.id, label: details.label, topologyId: details.topologyId
}));
const urlState = {
controlPipe: cp ? cp.toJS() : null,
nodeDetails: nodeDetails.toJS(),
topologyViewMode: state.get('topologyViewMode'),
pinnedMetricType: state.get('pinnedMetricType'),
pinnedSearches: state.get('pinnedSearches').toJS(),
searchQuery: state.get('searchQuery'),
selectedNodeId: state.get('selectedNodeId'),
gridSortedBy: state.get('gridSortedBy'),
gridSortedDesc: state.get('gridSortedDesc'),
topologyId: state.get('currentTopologyId'),
topologyOptions: state.get('topologyOptions').toJS(), // all options,
contrastMode: state.get('contrastMode')
};
if (state.get('showingNetworks')) {
urlState.showingNetworks = true;
if (state.get('pinnedNetwork')) {
urlState.pinnedNetwork = state.get('pinnedNetwork');
}
}
return urlState;
}
export function updateRoute(getState) {
const state = getUrlState(getState());
const stateUrl = encodeURL(JSON.stringify(state));
const dispatch = false;
const prevState = parseHashState();
// back up state in storage as well
storageSet(STORAGE_STATE_KEY, stateUrl);
if (shouldReplaceState(prevState, state)) {
// Replace the top of the history rather than pushing on a new item.
page.replace(`/state/${stateUrl}`, state, dispatch);
} else {
page.show(`/state/${stateUrl}`, state, dispatch);
}
}
export function getRouter(dispatch, initialState) {
// strip any trailing '/'s.
page.base(window.location.pathname.replace(/\/$/, ''));
page('/', () => {
// recover from storage state on empty URL
const storageState = storageGet(STORAGE_STATE_KEY);
if (storageState) {
// push storage state to URL
window.location.hash = `!/state/${storageState}`;
const parsedState = JSON.parse(decodeURL(storageState));
const mergedState = Object.assign(initialState, parsedState);
dispatch(route(mergedState));
} else {
dispatch(route(initialState));
}
});
page('/state/:state', (ctx) => {
const state = JSON.parse(decodeURL(ctx.params.state));
dispatch(route(state));
});
return page;
}