|
| 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 { AppMountParameters } from 'kibana/public'; |
| 21 | +import ReactDOM from 'react-dom'; |
| 22 | +import React from 'react'; |
| 23 | +import { createHashHistory, createBrowserHistory } from 'history'; |
| 24 | +import { TodoAppPage } from './todo'; |
| 25 | + |
| 26 | +export interface AppOptions { |
| 27 | + appInstanceId: string; |
| 28 | + appTitle: string; |
| 29 | + historyType: History; |
| 30 | +} |
| 31 | + |
| 32 | +export enum History { |
| 33 | + Browser, |
| 34 | + Hash, |
| 35 | +} |
| 36 | + |
| 37 | +export const renderApp = ( |
| 38 | + { appBasePath, element }: AppMountParameters, |
| 39 | + { appInstanceId, appTitle, historyType }: AppOptions |
| 40 | +) => { |
| 41 | + const history = |
| 42 | + historyType === History.Browser |
| 43 | + ? createBrowserHistory({ basename: appBasePath }) |
| 44 | + : createHashHistory(); |
| 45 | + ReactDOM.render( |
| 46 | + <TodoAppPage |
| 47 | + history={history} |
| 48 | + appInstanceId={appInstanceId} |
| 49 | + appTitle={appTitle} |
| 50 | + appBasePath={appBasePath} |
| 51 | + isInitialRoute={() => { |
| 52 | + const stripTrailingSlash = (path: string) => |
| 53 | + path.charAt(path.length - 1) === '/' ? path.substr(0, path.length - 1) : path; |
| 54 | + const currentAppUrl = stripTrailingSlash(history.createHref(history.location)); |
| 55 | + if (historyType === History.Browser) { |
| 56 | + // browser history |
| 57 | + const basePath = stripTrailingSlash(appBasePath); |
| 58 | + return currentAppUrl === basePath && !history.location.search && !history.location.hash; |
| 59 | + } else { |
| 60 | + // hashed history |
| 61 | + return currentAppUrl === '#' && !history.location.search; |
| 62 | + } |
| 63 | + }} |
| 64 | + />, |
| 65 | + element |
| 66 | + ); |
| 67 | + |
| 68 | + return () => ReactDOM.unmountComponentAtNode(element); |
| 69 | +}; |
0 commit comments