Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spaces mapping #18778

Merged
merged 4 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/server/config/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ export default function (kbnServer, server, config) {
return kbnServer.config;
});

server.decorate('request', 'getBasePath', function () {
return kbnServer.config.get('server.basePath');
});

const unusedKeys = getUnusedConfigKeys(kbnServer.disabledPluginSpecs, kbnServer.settings, config.get())
.map(key => `"${key}"`);

Expand Down
1 change: 0 additions & 1 deletion src/server/config/complete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe('server/config completeMixin()', function () {
callCompleteMixin();
sinon.assert.callCount(server.decorate, 2);
sinon.assert.calledWithExactly(server.decorate, 'server', 'config', sinon.match.func);
sinon.assert.calledWithExactly(server.decorate, 'request', 'getBasePath', sinon.match.func);
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { setupConnection } from './setup_connection';
import { setupRedirectServer } from './setup_redirect_server';
import { registerHapiPlugins } from './register_hapi_plugins';
import { setupBasePathRewrite } from './setup_base_path_rewrite';
import { setupBasePathProvider } from './setup_base_path_provider';
import { setupXsrf } from './xsrf';

export default async function (kbnServer, server, config) {
Expand All @@ -20,6 +21,7 @@ export default async function (kbnServer, server, config) {

setupConnection(server, config);
setupBasePathRewrite(server, config);
setupBasePathProvider(server, config);
await setupRedirectServer(config);
registerHapiPlugins(server);

Expand Down
19 changes: 19 additions & 0 deletions src/server/http/setup_base_path_provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function setupBasePathProvider(server, config) {

server.decorate('request', 'setBasePath', function (basePath) {
const request = this;
if (request.app._basePath) {
throw new Error(`Request basePath was previously set. Setting multiple times is not supported.`);
}
request.app._basePath = basePath;
});

server.decorate('request', 'getBasePath', function () {
const request = this;

const serverBasePath = config.get('server.basePath');
const requestBasePath = request.app._basePath || '';

return `${serverBasePath}${requestBasePath}`;
});
}
7 changes: 6 additions & 1 deletion x-pack/plugins/spaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { resolve } from 'path';
import { validateConfig } from './server/lib/validate_config';
import { checkLicense } from './server/lib/check_license';
import { initSpacesApi } from './server/routes/api/v1/spaces';
import { initSpacesRequestInterceptors } from './server/lib/space_request_interceptors';
import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status';
import mappings from './mappings.json';

Expand Down Expand Up @@ -36,7 +37,9 @@ export const spaces = (kibana) => new kibana.Plugin({
mappings,
home: ['plugins/spaces/register_feature'],
injectDefaultVars: function () {
return { };
return {
spaces: []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a better way to do this, but I need the spaces var here so that angular can inject it here. The default isn't really used, but I make use of the override available here.
This allows the Space Selection screen to have all the information it needs to render, instead of having the client make another ajax call once the page loads. We'd end up with another loading screen while we waited for the Space Selector to load the available spaces.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... so if we don't define it here we can't provide it via the render call? That's super weird...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - the Angular controller would actually fail because there isn't a "spacesProvider" registered with the system. At least that's what happened with my testing...I could be overlooking a better solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen crazier things, it works for me!

};
}
},

Expand All @@ -53,5 +56,7 @@ export const spaces = (kibana) => new kibana.Plugin({
validateConfig(config, message => server.log(['spaces', 'warning'], message));

initSpacesApi(server);

initSpacesRequestInterceptors(server);
}
});
6 changes: 6 additions & 0 deletions x-pack/plugins/spaces/mappings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{
"spaceId": {
"type": "keyword"
},
"space": {
"properties": {
"name": {
"type": "text"
},
"urlContext": {
"type": "keyword"
},
"description": {
"type": "text"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`it renders without crashing 1`] = `
<div
className="euiHeader"
>
<div
className="euiHeaderSection euiHeaderSection--left"
>
<div
className="euiHeaderBreadcrumbs"
/>
</div>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { PageHeader } from './page_header';
import { DeleteSpacesButton } from './delete_spaces_button';

import { Notifier, toastNotifications } from 'ui/notify';
import { UrlContext } from './url_context';
import { toUrlContext, isValidUrlContext } from '../lib/url_context_utils';

export class ManageSpacePage extends React.Component {
state = {
Expand Down Expand Up @@ -105,6 +107,13 @@ export class ManageSpacePage extends React.Component {
/>
</EuiFormRow>

<UrlContext
space={this.state.space}
editingExistingSpace={this.editingExistingSpace()}
editable={true}
onChange={this.onUrlContextChange}
/>

<EuiFlexGroup>
<EuiFlexItem grow={false}>
<EuiButton fill onClick={this.saveSpace}>Save</EuiButton>
Expand Down Expand Up @@ -146,10 +155,21 @@ export class ManageSpacePage extends React.Component {
};

onNameChange = (e) => {
const canUpdateContext = !this.editingExistingSpace();

let {
urlContext
} = this.state.space;

if (canUpdateContext) {
urlContext = toUrlContext(e.target.value);
}

this.setState({
space: {
...this.state.space,
name: e.target.value
name: e.target.value,
urlContext
}
});
};
Expand All @@ -163,6 +183,15 @@ export class ManageSpacePage extends React.Component {
});
};

onUrlContextChange = (e) => {
this.setState({
space: {
...this.state.space,
urlContext: toUrlContext(e.target.value)
}
});
};

saveSpace = () => {
this.setState({
validate: true
Expand All @@ -176,16 +205,18 @@ export class ManageSpacePage extends React.Component {
_performSave = () => {
const {
name = '',
id = name.toLowerCase().replace(/\s/g, '-'),
description
id = toUrlContext(name),
description,
urlContext
} = this.state.space;

const { httpAgent, chrome } = this.props;

const params = {
name,
id,
description
description,
urlContext
};

const overwrite = this.editingExistingSpace();
Expand Down Expand Up @@ -249,12 +280,39 @@ export class ManageSpacePage extends React.Component {
return {};
};

validateUrlContext = () => {
if (!this.state.validate) {
return {};
}

const {
urlContext
} = this.state.space;

if (!urlContext) {
return {
isInvalid: true,
error: 'URL Context is required'
};
}

if (!isValidUrlContext(urlContext)) {
return {
isInvalid: true,
error: 'URL Context only allows a-z, 0-9, and the "-" character'
};
}

return {};

};

validateForm = () => {
if (!this.state.validate) {
return {};
}

const validations = [this.validateName(), this.validateDescription()];
const validations = [this.validateName(), this.validateDescription(), this.validateUrlContext()];
if (validations.some(validation => validation.isInvalid)) {
return {
isInvalid: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { PageHeader } from './page_header';
import { render } from 'enzyme';
import renderer from 'react-test-renderer';

test('it renders without crashing', () => {
const component = renderer.create(
<PageHeader breadcrumbs={[]}/>
);
expect(component).toMatchSnapshot();
});

test('it renders breadcrumbs', () => {
const component = render(
<PageHeader breadcrumbs={[{
id: 'id-1',
href: 'myhref-1',
current: false
}, {
id: 'id-2',
href: 'myhref-2',
current: true
}]}
/>
);

expect(component.find('a')).toHaveLength(2);

});
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export class SpacesGridPage extends Component {
field: 'description',
name: 'Description',
sortable: true
}, {
field: 'urlContext',
name: 'URL Context',
sortable: true
}];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import {
EuiFormRow,
EuiLink,
EuiFieldText,
EuiCallOut,
EuiSpacer
} from '@elastic/eui';

export class UrlContext extends Component {
textFieldRef = null;

state = {
editing: false
};

render() {
const {
urlContext = ''
} = this.props.space;

return (
<Fragment>
<EuiFormRow
label={this.getLabel()}
helpText={this.getHelpText()}
>
<div>
<EuiFieldText
readOnly={!this.state.editing}
placeholder={this.state.editing ? null : 'give your space a name to see its URL Context'}
value={urlContext}
onChange={this.onChange}
inputRef={(ref) => this.textFieldRef = ref}
/>
{this.getCallOut()}
</div>
</EuiFormRow>
</Fragment>
);
}

getLabel = () => {
const editLinkText = this.state.editing ? `[stop editing]` : `[edit]`;
return (<p>URL Context <EuiLink onClick={this.onEditClick}>{editLinkText}</EuiLink></p>);
};

getHelpText = () => {
return (<p>Links within Kibana will include this space identifier</p>);
};

getCallOut = () => {
if (this.props.editingExistingSpace && this.state.editing) {
return (
<Fragment>
<EuiSpacer />
<EuiCallOut
color={'warning'}
size={'s'}
title={'Changing the URL Context will invalidate all existing links and bookmarks to this space'}
/>
</Fragment>
);
}

return null;
};

onEditClick = () => {
this.setState({
editing: !this.state.editing
}, () => {
if (this.textFieldRef && this.state.editing) {
this.textFieldRef.focus();
}
});
};

onChange = (e) => {
if (!this.state.editing) return;
this.props.onChange(e);
};
}

UrlContext.propTypes = {
space: PropTypes.object.isRequired,
editable: PropTypes.bool.isRequired,
editingExistingSpace: PropTypes.bool.isRequired,
onChange: PropTypes.func
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export function toUrlContext(value = '') {
return value.toLowerCase().replace(/[^a-z0-9]/g, '-');
}

export function isValidUrlContext(value = '') {
return value === toUrlContext(value);
}
Loading