-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate ui/doc_title to New platform (#48121)
* create NP docTitle service and bridge LP to it Signed-off-by: pgayvallet <[email protected]> * properly prefix the docTitle public types Signed-off-by: pgayvallet <[email protected]> * update documentation Signed-off-by: pgayvallet <[email protected]> * replace direct NP access with closure to avoid error when importing module in tests with no usages Signed-off-by: pgayvallet <[email protected]> * remove arrow functions for doc generation Signed-off-by: pgayvallet <[email protected]> * remove get$ from the api Signed-off-by: pgayvallet <[email protected]> * remove apply param and excludeBase option Signed-off-by: pgayvallet <[email protected]> * remove removed export Signed-off-by: pgayvallet <[email protected]> * adapt legacy service to new api Signed-off-by: pgayvallet <[email protected]> * add entry about docTitle in the migration guide Signed-off-by: pgayvallet <[email protected]> * add link in migration guide Signed-off-by: pgayvallet <[email protected]> * update generated doc Signed-off-by: pgayvallet <[email protected]> * update chrome mock Signed-off-by: pgayvallet <[email protected]> * update snapshots due to api change Signed-off-by: pgayvallet <[email protected]> * remove ChromeDocTitleChange in favor of inline type Signed-off-by: pgayvallet <[email protected]>
- Loading branch information
1 parent
39aa439
commit ccd01a3
Showing
18 changed files
with
402 additions
and
51 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
docs/development/core/public/kibana-plugin-public.chromedoctitle.change.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ChromeDocTitle](./kibana-plugin-public.chromedoctitle.md) > [change](./kibana-plugin-public.chromedoctitle.change.md) | ||
|
||
## ChromeDocTitle.change() method | ||
|
||
Changes the current document title. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
change(newTitle: string | string[]): void; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| newTitle | <code>string | string[]</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`void` | ||
|
||
## Example | ||
|
||
How to change the title of the document | ||
|
||
```ts | ||
chrome.docTitle.change('My application title') | ||
chrome.docTitle.change(['My application', 'My section']) | ||
|
||
``` | ||
|
39 changes: 39 additions & 0 deletions
39
docs/development/core/public/kibana-plugin-public.chromedoctitle.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ChromeDocTitle](./kibana-plugin-public.chromedoctitle.md) | ||
|
||
## ChromeDocTitle interface | ||
|
||
APIs for accessing and updating the document title. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface ChromeDocTitle | ||
``` | ||
|
||
## Methods | ||
|
||
| Method | Description | | ||
| --- | --- | | ||
| [change(newTitle)](./kibana-plugin-public.chromedoctitle.change.md) | Changes the current document title. | | ||
| [reset()](./kibana-plugin-public.chromedoctitle.reset.md) | Resets the document title to it's initial value. (meaning the one present in the title meta at application load.) | | ||
|
||
## Example 1 | ||
|
||
How to change the title of the document | ||
|
||
```ts | ||
chrome.docTitle.change('My application') | ||
|
||
``` | ||
|
||
## Example 2 | ||
|
||
How to reset the title of the document to it's initial value | ||
|
||
```ts | ||
chrome.docTitle.reset() | ||
|
||
``` | ||
|
17 changes: 17 additions & 0 deletions
17
docs/development/core/public/kibana-plugin-public.chromedoctitle.reset.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ChromeDocTitle](./kibana-plugin-public.chromedoctitle.md) > [reset](./kibana-plugin-public.chromedoctitle.reset.md) | ||
|
||
## ChromeDocTitle.reset() method | ||
|
||
Resets the document title to it's initial value. (meaning the one present in the title meta at application load.) | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
reset(): void; | ||
``` | ||
<b>Returns:</b> | ||
|
||
`void` | ||
|
13 changes: 13 additions & 0 deletions
13
docs/development/core/public/kibana-plugin-public.chromestart.doctitle.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-public](./kibana-plugin-public.md) > [ChromeStart](./kibana-plugin-public.chromestart.md) > [docTitle](./kibana-plugin-public.chromestart.doctitle.md) | ||
|
||
## ChromeStart.docTitle property | ||
|
||
APIs for accessing and updating the document title. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
docTitle: ChromeDocTitle; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/core/public/chrome/doc_title/doc_title_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { DocTitleService } from './doc_title_service'; | ||
|
||
describe('DocTitleService', () => { | ||
const defaultTitle = 'KibanaTest'; | ||
const document = { title: '' }; | ||
|
||
const getStart = (title: string = defaultTitle) => { | ||
document.title = title; | ||
return new DocTitleService().start({ document }); | ||
}; | ||
|
||
beforeEach(() => { | ||
document.title = defaultTitle; | ||
}); | ||
|
||
describe('#change()', () => { | ||
it('changes the title of the document', async () => { | ||
getStart().change('TitleA'); | ||
expect(document.title).toEqual('TitleA - KibanaTest'); | ||
}); | ||
|
||
it('appends the baseTitle to the title', async () => { | ||
const start = getStart('BaseTitle'); | ||
start.change('TitleA'); | ||
expect(document.title).toEqual('TitleA - BaseTitle'); | ||
start.change('TitleB'); | ||
expect(document.title).toEqual('TitleB - BaseTitle'); | ||
}); | ||
|
||
it('accepts string arrays as input', async () => { | ||
const start = getStart(); | ||
start.change(['partA', 'partB']); | ||
expect(document.title).toEqual(`partA - partB - ${defaultTitle}`); | ||
start.change(['partA', 'partB', 'partC']); | ||
expect(document.title).toEqual(`partA - partB - partC - ${defaultTitle}`); | ||
}); | ||
}); | ||
|
||
describe('#reset()', () => { | ||
it('resets the title to the initial value', async () => { | ||
const start = getStart('InitialTitle'); | ||
start.change('TitleA'); | ||
expect(document.title).toEqual('TitleA - InitialTitle'); | ||
start.reset(); | ||
expect(document.title).toEqual('InitialTitle'); | ||
}); | ||
}); | ||
|
||
describe('#__legacy.setBaseTitle()', () => { | ||
it('allows to change the baseTitle after startup', async () => { | ||
const start = getStart('InitialTitle'); | ||
start.change('WithInitial'); | ||
expect(document.title).toEqual('WithInitial - InitialTitle'); | ||
start.__legacy.setBaseTitle('NewBaseTitle'); | ||
start.change('WithNew'); | ||
expect(document.title).toEqual('WithNew - NewBaseTitle'); | ||
start.reset(); | ||
expect(document.title).toEqual('NewBaseTitle'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { compact, flattenDeep, isString } from 'lodash'; | ||
|
||
interface StartDeps { | ||
document: { title: string }; | ||
} | ||
|
||
/** | ||
* APIs for accessing and updating the document title. | ||
* | ||
* @example | ||
* How to change the title of the document | ||
* ```ts | ||
* chrome.docTitle.change('My application') | ||
* ``` | ||
* | ||
* @example | ||
* How to reset the title of the document to it's initial value | ||
* ```ts | ||
* chrome.docTitle.reset() | ||
* ``` | ||
* | ||
* @public | ||
* */ | ||
export interface ChromeDocTitle { | ||
/** | ||
* Changes the current document title. | ||
* | ||
* @example | ||
* How to change the title of the document | ||
* ```ts | ||
* chrome.docTitle.change('My application title') | ||
* chrome.docTitle.change(['My application', 'My section']) | ||
* ``` | ||
* | ||
* @param newTitle The new title to set, either a string or string array | ||
*/ | ||
change(newTitle: string | string[]): void; | ||
/** | ||
* Resets the document title to it's initial value. | ||
* (meaning the one present in the title meta at application load.) | ||
*/ | ||
reset(): void; | ||
|
||
/** @internal */ | ||
__legacy: { | ||
setBaseTitle(baseTitle: string): void; | ||
}; | ||
} | ||
|
||
const defaultTitle: string[] = []; | ||
const titleSeparator = ' - '; | ||
|
||
/** @internal */ | ||
export class DocTitleService { | ||
private document = { title: '' }; | ||
private baseTitle = ''; | ||
|
||
public start({ document }: StartDeps): ChromeDocTitle { | ||
this.document = document; | ||
this.baseTitle = document.title; | ||
|
||
return { | ||
change: (title: string | string[]) => { | ||
this.applyTitle(title); | ||
}, | ||
reset: () => { | ||
this.applyTitle(defaultTitle); | ||
}, | ||
__legacy: { | ||
setBaseTitle: baseTitle => { | ||
this.baseTitle = baseTitle; | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
private applyTitle(title: string | string[]) { | ||
this.document.title = this.render(title); | ||
} | ||
|
||
private render(title: string | string[]) { | ||
const parts = [...(isString(title) ? [title] : title), this.baseTitle]; | ||
// ensuring compat with legacy that might be passing nested arrays | ||
return compact(flattenDeep(parts)).join(titleSeparator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export * from './doc_title_service'; |
Oops, something went wrong.