Skip to content

Commit

Permalink
Get all namespace and nav stuff working with basepath.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Apr 24, 2018
1 parent b9e8108 commit d48c29a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
9 changes: 6 additions & 3 deletions frontend/public/components/nav.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import * as routingImg from '../imgs/routing.svg';
import * as appsLogoImg from '../imgs/apps-logo.svg';
import * as routingActiveImg from '../imgs/routing-active.svg';
import * as appsLogoActiveImg from '../imgs/apps-logo-active.svg';
import { history } from './utils';
import { history, stripBasePath } from './utils';


const stripNS = href => href.replace(/^\/?k8s\//, '').replace(/^\/?(cluster|all-namespaces|ns\/[^/]*)/, '').replace(/^\//, '');
const stripNS = href => {
href = stripBasePath(href);
return href.replace(/^\/?k8s\//, '').replace(/^\/?(cluster|all-namespaces|ns\/[^/]*)/, '').replace(/^\//, '');
};

class NavLink extends React.PureComponent {
static isActive () {
Expand Down Expand Up @@ -138,7 +141,7 @@ const NavSection = connect(navSectionStateToProps)(
const { activeNamespace, location, children } = this.props;

if (!children) {
return location.startsWith(this.props.activePath);
return stripBasePath(location).startsWith(this.props.activePath);
}

const resourcePath = location ? stripNS(location) : '';
Expand Down
12 changes: 6 additions & 6 deletions frontend/public/components/utils/link.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const basePathPattern = new RegExp(`^/?${window.SERVER_FLAGS.basePath}`);

export const namespacedPrefixes = ['/search', '/overview', '/k8s'];

export const stripBasePath = path => {
path = path.replace(basePathPattern, '/');
path = path.replace(/^\/?k8s\//, '');
return path;
};
export const stripBasePath = path => path.replace(basePathPattern, '/');

export const isNamespaced = path => namespacedPrefixes.filter(p => path.startsWith(p)).length > 0;
export const getNSPrefix = path => {
path = stripBasePath(path);
return namespacedPrefixes.filter(p => path.startsWith(p))[0];
};

export const getNamespace = path => {
path = stripBasePath(path);
const split = path.split('/').filter(x => x);

if (split[1] === 'all-namespaces') {
Expand Down
15 changes: 5 additions & 10 deletions frontend/public/ui/ui-actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as _ from 'lodash-es';
import store from '../redux';
import { history } from '../components/utils/router';
import { ALL_NAMESPACES_KEY } from '../const';
import { namespacedPrefixes, isNamespaced } from '../components/utils/link';
import { getNSPrefix } from '../components/utils/link';
import { allModels } from '../module/k8s/k8s-models';

// URL routes that can be namespaced
Expand All @@ -29,16 +28,12 @@ export const formatNamespacedRouteForResource = (resource, activeNamespace=getAc
};

export const formatNamespaceRoute = (activeNamespace, originalPath, location) => {
if (!isNamespaced(originalPath)) {
return originalPath;
}

const prefix = _.find(namespacedPrefixes, p => originalPath.startsWith(p));
const prefix = getNSPrefix(originalPath);
if (!prefix) {
throw new Error(`no prefix for path ${originalPath}?`);
return originalPath;
}

originalPath = originalPath.substr(prefix.length);
originalPath = originalPath.substr(prefix.length + window.SERVER_FLAGS.basePath.length);

let parts = originalPath.split('/').filter(p => p);
let previousNS = '';
Expand Down Expand Up @@ -90,7 +85,7 @@ export const UIActions = {
// broken direct links and bookmarks
if (namespace !== getActiveNamespace()) {
const oldPath = window.location.pathname;
if (isNamespaced(oldPath)) {
if (getNSPrefix(oldPath)) {
history.pushPath(formatNamespaceRoute(namespace, oldPath, window.location));
}
}
Expand Down

0 comments on commit d48c29a

Please sign in to comment.