Skip to content

Commit

Permalink
[CANVAS] Adds Canvas Platform Service (#64453) (#65000)
Browse files Browse the repository at this point in the history
* Adds Canvas Platform Service

* Fix type check

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Corey Robertson and elasticmachine committed May 1, 2020
1 parent f39a1e6 commit a9871e0
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import { get } from 'lodash';
import { getWindow } from '../../lib/get_window';
import { CANVAS_APP } from '../../../common/lib/constants';
import { getCoreStart, getStartPlugins } from '../../legacy';
import { platformService } from '../../services';

export function trackRouteChange() {
const basePath = getCoreStart().http.basePath.get();
// storage.set(LOCALSTORAGE_LASTPAGE, pathname);
getStartPlugins().__LEGACY.trackSubUrlForApp(
CANVAS_APP,
getStartPlugins().__LEGACY.absoluteToParsedUrl(get(getWindow(), 'location.href'), basePath)
);
const basePath = platformService.getService().coreStart.http.basePath.get();

platformService
.getService()
.startPlugins.__LEGACY.trackSubUrlForApp(
CANVAS_APP,
platformService
.getService()
.startPlugins.__LEGACY.absoluteToParsedUrl(get(getWindow(), 'location.href'), basePath)
);
}
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/canvas/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const shimStartPlugins: CanvasStartDeps = {
__LEGACY: {
// ToDo: Copy directly into canvas
absoluteToParsedUrl,
// ToDo: Copy directly into canvas
formatMsg,
// ToDo: Won't be a part of New Platform. Will need to handle internally
trackSubUrlForApp: chrome.trackSubUrlForApp,
},
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/canvas/public/lib/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { ChromeBreadcrumb } from '../../../../../../src/core/public';
import { getCoreStart } from '../legacy';
import { platformService } from '../services';

export const getBaseBreadcrumb = () => ({
text: 'Canvas',
Expand All @@ -24,6 +24,6 @@ export const getWorkpadBreadcrumb = ({
};

export const setBreadcrumb = (paths: ChromeBreadcrumb | ChromeBreadcrumb[]) => {
const setBreadCrumbs = getCoreStart().chrome.setBreadcrumbs;
const setBreadCrumbs = platformService.getService().coreStart.chrome.setBreadcrumbs;
setBreadCrumbs(Array.isArray(paths) ? paths : [paths]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { AxiosPromise } from 'axios';
import { API_ROUTE_CUSTOM_ELEMENT } from '../../common/lib/constants';
import { fetch } from '../../common/lib/fetch';
import { CustomElement } from '../../types';
import { getCoreStart } from '../legacy';
import { platformService } from '../services';

const getApiPath = function() {
const basePath = getCoreStart().http.basePath.get();
const basePath = platformService.getService().coreStart.http.basePath.get();
return `${basePath}${API_ROUTE_CUSTOM_ELEMENT}`;
};

Expand Down
10 changes: 5 additions & 5 deletions x-pack/legacy/plugins/canvas/public/lib/documentation_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getCoreStart } from '../legacy';
import { platformService } from '../services';

export const getDocumentationLinks = () => ({
canvas: `${getCoreStart().docLinks.ELASTIC_WEBSITE_URL}guide/en/kibana/${
getCoreStart().docLinks.DOC_LINK_VERSION
canvas: `${platformService.getService().coreStart.docLinks.ELASTIC_WEBSITE_URL}guide/en/kibana/${
platformService.getService().coreStart.docLinks.DOC_LINK_VERSION
}/canvas.html`,
numeral: `${getCoreStart().docLinks.ELASTIC_WEBSITE_URL}guide/en/kibana/${
getCoreStart().docLinks.DOC_LINK_VERSION
numeral: `${platformService.getService().coreStart.docLinks.ELASTIC_WEBSITE_URL}guide/en/kibana/${
platformService.getService().coreStart.docLinks.DOC_LINK_VERSION
}/guide/numeral.html`,
});
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/canvas/public/lib/es_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import { API_ROUTE } from '../../common/lib/constants';
import { fetch } from '../../common/lib/fetch';
import { ErrorStrings } from '../../i18n';
import { notifyService } from '../services';
import { getCoreStart } from '../legacy';
import { platformService } from '../services';

const { esService: strings } = ErrorStrings;

const getApiPath = function() {
const basePath = getCoreStart().http.basePath.get();
const basePath = platformService.getService().coreStart.http.basePath.get();
return basePath + API_ROUTE;
};

const getSavedObjectsClient = function() {
return getCoreStart().savedObjects.client;
return platformService.getService().coreStart.savedObjects.client;
};

const getAdvancedSettings = function() {
return getCoreStart().uiSettings;
return platformService.getService().coreStart.uiSettings;
};

export const getFields = (index = '_all') => {
Expand Down
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/canvas/public/lib/workpad_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DEFAULT_WORKPAD_CSS,
} from '../../common/lib/constants';
import { fetch } from '../../common/lib/fetch';
import { getCoreStart } from '../legacy';
import { platformService } from '../services';
/*
Remove any top level keys from the workpad which will be rejected by validation
*/
Expand Down Expand Up @@ -43,17 +43,17 @@ const sanitizeWorkpad = function(workpad) {
};

const getApiPath = function() {
const basePath = getCoreStart().http.basePath.get();
const basePath = platformService.getService().coreStart.http.basePath.get();
return `${basePath}${API_ROUTE_WORKPAD}`;
};

const getApiPathStructures = function() {
const basePath = getCoreStart().http.basePath.get();
const basePath = platformService.getService().coreStart.http.basePath.get();
return `${basePath}${API_ROUTE_WORKPAD_STRUCTURES}`;
};

const getApiPathAssets = function() {
const basePath = getCoreStart().http.basePath.get();
const basePath = platformService.getService().coreStart.http.basePath.get();
return `${basePath}${API_ROUTE_WORKPAD_ASSETS}`;
};

Expand Down
19 changes: 16 additions & 3 deletions x-pack/legacy/plugins/canvas/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export interface CanvasStartDeps {
uiActions: UiActionsStart;
__LEGACY: {
absoluteToParsedUrl: (url: string, basePath: string) => any;
formatMsg: any;
trackSubUrlForApp: Chrome['trackSubUrlForApp'];
};
}
Expand All @@ -64,6 +63,7 @@ export class CanvasPlugin
implements Plugin<CanvasSetup, CanvasStart, CanvasSetupDeps, CanvasStartDeps> {
// TODO: Do we want to completely move canvas_plugin_src into it's own plugin?
private srcPlugin = new CanvasSrcPlugin();
private startPlugins: CanvasStartDeps | undefined;

public setup(core: CoreSetup<CanvasStartDeps>, plugins: CanvasSetupDeps) {
const { api: canvasApi, registries } = getPluginApi(plugins.expressions);
Expand All @@ -73,14 +73,26 @@ export class CanvasPlugin
core.application.register({
id: 'canvas',
title: 'Canvas App',
async mount(context, params) {
mount: async (context, params) => {
// Load application bundle
const { renderApp, initializeCanvas, teardownCanvas } = await import('./application');

// Get start services
const [coreStart, depsStart] = await core.getStartServices();

const canvasStore = await initializeCanvas(core, coreStart, plugins, depsStart, registries);
// TODO: We only need this to get the __LEGACY stuff that isn't coming from getStartSevices.
// We won't need this as soon as we move over to NP Completely
if (!this.startPlugins) {
throw new Error('Start Plugins not ready at mount time');
}

const canvasStore = await initializeCanvas(
core,
coreStart,
plugins,
this.startPlugins,
registries
);

const unmount = renderApp(coreStart, depsStart, params, canvasStore);

Expand Down Expand Up @@ -115,6 +127,7 @@ export class CanvasPlugin
}

public start(core: CoreStart, plugins: CanvasStartDeps) {
this.startPlugins = plugins;
this.srcPlugin.start(core, plugins);
initLoadingIndicator(core.http.addLoadingCountSource);
}
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/canvas/public/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { CoreSetup, CoreStart } from '../../../../../../src/core/public';
import { CanvasSetupDeps, CanvasStartDeps } from '../plugin';
import { notifyServiceFactory } from './notify';
import { platformServiceFactory } from './platform';

export type CanvasServiceFactory<Service> = (
coreSetup: CoreSetup,
Expand Down Expand Up @@ -49,6 +50,7 @@ export type ServiceFromProvider<P> = P extends CanvasServiceProvider<infer T> ?

export const services = {
notify: new CanvasServiceProvider(notifyServiceFactory),
platform: new CanvasServiceProvider(platformServiceFactory),
};

export interface CanvasServices {
Expand All @@ -70,4 +72,4 @@ export const stopServices = () => {
Object.entries(services).forEach(([key, provider]) => provider.stop());
};

export const { notify: notifyService } = services;
export const { notify: notifyService, platform: platformService } = services;
24 changes: 24 additions & 0 deletions x-pack/legacy/plugins/canvas/public/services/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { CanvasServiceFactory } from '.';
import { CoreStart, CoreSetup, CanvasSetupDeps, CanvasStartDeps } from '../plugin';

interface PlatformService {
coreSetup: CoreSetup;
coreStart: CoreStart;
setupPlugins: CanvasSetupDeps;
startPlugins: CanvasStartDeps;
}

export const platformServiceFactory: CanvasServiceFactory<PlatformService> = (
coreSetup,
coreStart,
setupPlugins,
startPlugins
) => {
return { coreSetup, coreStart, setupPlugins, startPlugins };
};
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/canvas/public/state/initial_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/

import { get } from 'lodash';
import { getCoreStart } from '../legacy';
import { platformService } from '../services';
import { getDefaultWorkpad } from './defaults';

export const getInitialState = path => {
const state = {
app: {}, // Kibana stuff in here
assets: {}, // assets end up here
transient: {
canUserWrite: getCoreStart().application.capabilities.canvas.save,
canUserWrite: platformService.getService().coreStart.application.capabilities.canvas.save,
zoomScale: 1,
elementStats: {
total: 0,
Expand Down
26 changes: 15 additions & 11 deletions x-pack/legacy/plugins/canvas/public/state/reducers/workpad.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { handleActions } from 'redux-actions';
import { getCoreStart } from '../../legacy';
import { platformService } from '../../services';
import { getDefaultWorkpad } from '../defaults';
import {
setWorkpad,
Expand All @@ -22,11 +22,13 @@ import { APP_ROUTE_WORKPAD } from '../../../common/lib/constants';
export const workpadReducer = handleActions(
{
[setWorkpad]: (workpadState, { payload }) => {
getCoreStart().chrome.recentlyAccessed.add(
`${APP_ROUTE_WORKPAD}/${payload.id}`,
payload.name,
payload.id
);
platformService
.getService()
.coreStart.chrome.recentlyAccessed.add(
`${APP_ROUTE_WORKPAD}/${payload.id}`,
payload.name,
payload.id
);
return payload;
},

Expand All @@ -39,11 +41,13 @@ export const workpadReducer = handleActions(
},

[setName]: (workpadState, { payload }) => {
getCoreStart().chrome.recentlyAccessed.add(
`${APP_ROUTE_WORKPAD}/${workpadState.id}`,
payload,
workpadState.id
);
platformService
.getService()
.coreStart.chrome.recentlyAccessed.add(
`${APP_ROUTE_WORKPAD}/${workpadState.id}`,
payload,
workpadState.id
);
return { ...workpadState, name: payload };
},

Expand Down

0 comments on commit a9871e0

Please sign in to comment.