Skip to content

Commit

Permalink
Add redirect function to kibana_legacy legacy application service (#6…
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Apr 21, 2020
1 parent a10ae66 commit 4aa9bf6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/legacy/core_plugins/kibana/public/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ import { showAppRedirectNotification } from '../../../../plugins/kibana_legacy/p
import 'leaflet';
import { localApplicationService } from './local_application_service';

npSetup.plugins.kibanaLegacy.forwardApp('doc', 'discover', { keepPrefix: true });
npSetup.plugins.kibanaLegacy.forwardApp('context', 'discover', { keepPrefix: true });
npSetup.plugins.kibanaLegacy.registerLegacyAppAlias('doc', 'discover', { keepPrefix: true });
npSetup.plugins.kibanaLegacy.registerLegacyAppAlias('context', 'discover', { keepPrefix: true });

localApplicationService.attachToAngular(routes);

routes.enable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,29 @@ export class LocalApplicationService {
}
});

npStart.plugins.kibanaLegacy.getForwards().forEach(({ legacyAppId, newAppId, keepPrefix }) => {
angularRouteManager.when(matchAllWithPrefix(legacyAppId), {
resolveRedirectTo: ($location: ILocationService) => {
const url = $location.url();
return `/${newAppId}${keepPrefix ? url : url.replace(legacyAppId, '')}`;
npStart.plugins.kibanaLegacy.getForwards().forEach(forwardDefinition => {
angularRouteManager.when(matchAllWithPrefix(forwardDefinition.legacyAppId), {
outerAngularWrapperRoute: true,
reloadOnSearch: false,
reloadOnUrl: false,
template: '<span></span>',
controller($location: ILocationService) {
const newPath = forwardDefinition.rewritePath($location.url());
npStart.core.application.navigateToApp(forwardDefinition.newAppId, { path: newPath });
},
});
});

npStart.plugins.kibanaLegacy
.getLegacyAppAliases()
.forEach(({ legacyAppId, newAppId, keepPrefix }) => {
angularRouteManager.when(matchAllWithPrefix(legacyAppId), {
resolveRedirectTo: ($location: ILocationService) => {
const url = $location.url();
return `/${newAppId}${keepPrefix ? url : url.replace(legacyAppId, '')}`;
},
});
});
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export const npSetup = {
},
kibanaLegacy: {
registerLegacyApp: () => {},
registerLegacyAppAlias: () => {},
forwardApp: () => {},
config: {
defaultAppId: 'home',
Expand Down Expand Up @@ -362,6 +363,7 @@ export const npStart = {
kibanaLegacy: {
getApps: () => [],
getForwards: () => [],
getLegacyAppAliases: () => [],
config: {
defaultAppId: 'home',
},
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/kibana_legacy/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Start = jest.Mocked<ReturnType<KibanaLegacyPlugin['start']>>;

const createSetupContract = (): Setup => ({
forwardApp: jest.fn(),
registerLegacyAppAlias: jest.fn(),
registerLegacyApp: jest.fn(),
config: {
defaultAppId: 'home',
Expand All @@ -37,6 +38,7 @@ const createSetupContract = (): Setup => ({

const createStartContract = (): Start => ({
getApps: jest.fn(),
getLegacyAppAliases: jest.fn(),
getForwards: jest.fn(),
config: {
defaultAppId: 'home',
Expand Down
60 changes: 55 additions & 5 deletions src/plugins/kibana_legacy/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ import { Observable } from 'rxjs';
import { ConfigSchema } from '../config';
import { getDashboardConfig } from './dashboard_config';

interface ForwardDefinition {
interface LegacyAppAliasDefinition {
legacyAppId: string;
newAppId: string;
keepPrefix: boolean;
}

interface ForwardDefinition {
legacyAppId: string;
newAppId: string;
rewritePath: (legacyPath: string) => string;
}

export type AngularRenderedAppUpdater = (
app: AppBase
) => Partial<AppUpdatableFields & { activeUrl: string }> | undefined;
Expand All @@ -54,7 +60,8 @@ export interface AngularRenderedApp extends App {

export class KibanaLegacyPlugin {
private apps: AngularRenderedApp[] = [];
private forwards: ForwardDefinition[] = [];
private legacyAppAliases: LegacyAppAliasDefinition[] = [];
private forwardDefinitions: ForwardDefinition[] = [];

constructor(private readonly initializerContext: PluginInitializerContext<ConfigSchema>) {}

Expand Down Expand Up @@ -94,17 +101,55 @@ export class KibanaLegacyPlugin {
* renaming or nesting plugins. For route changes after the prefix, please
* use the routing mechanism of your app.
*
* This method just redirects URLs within the legacy `kibana` app.
*
* @param legacyAppId The name of the old app to forward URLs from
* @param newAppId The name of the new app that handles the URLs now
* @param options Whether the prefix of the old app is kept to nest the legacy
* path into the new path
*/
forwardApp: (
registerLegacyAppAlias: (
legacyAppId: string,
newAppId: string,
options: { keepPrefix: boolean } = { keepPrefix: false }
) => {
this.forwards.push({ legacyAppId, newAppId, ...options });
this.legacyAppAliases.push({ legacyAppId, newAppId, ...options });
},

/**
* Forwards URLs within the legacy `kibana` app to a new platform application.
*
* @param legacyAppId The name of the old app to forward URLs from
* @param newAppId The name of the new app that handles the URLs now
* @param rewritePath Function to rewrite the legacy sub path of the app to the new path in the core app
* path into the new path
*
* Example usage:
* ```
* kibanaLegacy.forwardApp(
* 'old',
* 'new',
* path => {
* const [, id] = /old/item\/(.*)$/.exec(path) || [];
* if (!id) {
* return '#/home';
* }
* return '#/items/${id}';
* }
* );
* ```
* This will cause the following redirects:
*
* * app/kibana#/old/ -> app/new#/home
* * app/kibana#/old/item/123 -> app/new#/items/123
*
*/
forwardApp: (
legacyAppId: string,
newAppId: string,
rewritePath: (legacyPath: string) => string
) => {
this.forwardDefinitions.push({ legacyAppId, newAppId, rewritePath });
},

/**
Expand Down Expand Up @@ -132,7 +177,12 @@ export class KibanaLegacyPlugin {
* @deprecated
* Just exported for wiring up with legacy platform, should not be used.
*/
getForwards: () => this.forwards,
getLegacyAppAliases: () => this.legacyAppAliases,
/**
* @deprecated
* Just exported for wiring up with legacy platform, should not be used.
*/
getForwards: () => this.forwardDefinitions,
config: this.initializerContext.config.get(),
dashboardConfig: getDashboardConfig(!application.capabilities.dashboard.showWriteControls),
};
Expand Down

0 comments on commit 4aa9bf6

Please sign in to comment.