|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { App, AppUnmount } from 'kibana/public'; |
| 21 | +import { UIRoutes } from 'ui/routes'; |
| 22 | +import { ILocationService, IScope } from 'angular'; |
| 23 | +import { npStart } from 'ui/new_platform'; |
| 24 | +import { htmlIdGenerator } from '@elastic/eui'; |
| 25 | + |
| 26 | +interface ForwardDefinition { |
| 27 | + legacyAppId: string; |
| 28 | + newAppId: string; |
| 29 | + keepPrefix: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +const matchAllWithPrefix = (prefixOrApp: string | App) => |
| 33 | + `/${typeof prefixOrApp === 'string' ? prefixOrApp : prefixOrApp.id}/:tail*?`; |
| 34 | + |
| 35 | +/** |
| 36 | + * To be able to migrate and shim parts of the Kibana app plugin |
| 37 | + * while still running some parts of it in the legacy world, this |
| 38 | + * service emulates the core application service while using the global |
| 39 | + * angular router to switch between apps without page reload. |
| 40 | + * |
| 41 | + * The id of the apps is used as prefix of the route - when switching between |
| 42 | + * to apps, the current application is unmounted. |
| 43 | + * |
| 44 | + * This service becomes unnecessary once the platform provides a central |
| 45 | + * router that handles switching between applications without page reload. |
| 46 | + */ |
| 47 | +export class LocalApplicationService { |
| 48 | + private apps: App[] = []; |
| 49 | + private forwards: ForwardDefinition[] = []; |
| 50 | + private idGenerator = htmlIdGenerator('kibanaAppLocalApp'); |
| 51 | + |
| 52 | + /** |
| 53 | + * Register an app to be managed by the application service. |
| 54 | + * This method works exactly as `core.application.register`. |
| 55 | + * |
| 56 | + * When an app is mounted, it is responsible for routing. The app |
| 57 | + * won't be mounted again if the route changes within the prefix |
| 58 | + * of the app (its id). It is fine to use whatever means for handling |
| 59 | + * routing within the app. |
| 60 | + * |
| 61 | + * When switching to a URL outside of the current prefix, the app router |
| 62 | + * shouldn't do anything because it doesn't own the routing anymore - |
| 63 | + * the local application service takes over routing again, |
| 64 | + * unmounts the current app and mounts the next app. |
| 65 | + * |
| 66 | + * @param app The app descriptor |
| 67 | + */ |
| 68 | + register(app: App) { |
| 69 | + this.apps.push(app); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Forwards every URL starting with `legacyAppId` to the same URL starting |
| 74 | + * with `newAppId` - e.g. `/legacy/my/legacy/path?q=123` gets forwarded to |
| 75 | + * `/newApp/my/legacy/path?q=123`. |
| 76 | + * |
| 77 | + * When setting the `keepPrefix` option, the new app id is simply prepended. |
| 78 | + * The example above would become `/newApp/legacy/my/legacy/path?q=123`. |
| 79 | + * |
| 80 | + * This method can be used to provide backwards compatibility for URLs when |
| 81 | + * renaming or nesting plugins. For route changes after the prefix, please |
| 82 | + * use the routing mechanism of your app. |
| 83 | + * |
| 84 | + * @param legacyAppId The name of the old app to forward URLs from |
| 85 | + * @param newAppId The name of the new app that handles the URLs now |
| 86 | + * @param options Whether the prefix of the old app is kept to nest the legacy |
| 87 | + * path into the new path |
| 88 | + */ |
| 89 | + forwardApp( |
| 90 | + legacyAppId: string, |
| 91 | + newAppId: string, |
| 92 | + options: { keepPrefix: boolean } = { keepPrefix: false } |
| 93 | + ) { |
| 94 | + this.forwards.push({ legacyAppId, newAppId, ...options }); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Wires up listeners to handle mounting and unmounting of apps to |
| 99 | + * the legacy angular route manager. Once all apps within the Kibana |
| 100 | + * plugin are using the local route manager, this implementation can |
| 101 | + * be switched to a more lightweight implementation. |
| 102 | + * |
| 103 | + * @param angularRouteManager The current `ui/routes` instance |
| 104 | + */ |
| 105 | + attachToAngular(angularRouteManager: UIRoutes) { |
| 106 | + this.apps.forEach(app => { |
| 107 | + const wrapperElementId = this.idGenerator(); |
| 108 | + angularRouteManager.when(matchAllWithPrefix(app), { |
| 109 | + outerAngularWrapperRoute: true, |
| 110 | + reloadOnSearch: false, |
| 111 | + reloadOnUrl: false, |
| 112 | + template: `<div style="height:100%" id="${wrapperElementId}"></div>`, |
| 113 | + controller($scope: IScope) { |
| 114 | + const element = document.getElementById(wrapperElementId)!; |
| 115 | + let unmountHandler: AppUnmount | null = null; |
| 116 | + let isUnmounted = false; |
| 117 | + $scope.$on('$destroy', () => { |
| 118 | + if (unmountHandler) { |
| 119 | + unmountHandler(); |
| 120 | + } |
| 121 | + isUnmounted = true; |
| 122 | + }); |
| 123 | + (async () => { |
| 124 | + unmountHandler = await app.mount({ core: npStart.core }, { element, appBasePath: '' }); |
| 125 | + // immediately unmount app if scope got destroyed in the meantime |
| 126 | + if (isUnmounted) { |
| 127 | + unmountHandler(); |
| 128 | + } |
| 129 | + })(); |
| 130 | + }, |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + this.forwards.forEach(({ legacyAppId, newAppId, keepPrefix }) => { |
| 135 | + angularRouteManager.when(matchAllWithPrefix(legacyAppId), { |
| 136 | + resolveRedirectTo: ($location: ILocationService) => { |
| 137 | + const url = $location.url(); |
| 138 | + return `/${newAppId}${keepPrefix ? url : url.replace(legacyAppId, '')}`; |
| 139 | + }, |
| 140 | + }); |
| 141 | + }); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +export const localApplicationService = new LocalApplicationService(); |
0 commit comments