-
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.
- Loading branch information
1 parent
5a74919
commit e838bf4
Showing
31 changed files
with
1,154 additions
and
49 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
examples/embeddable_examples/common/note_saved_object_attributes.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,28 @@ | ||
/* | ||
* 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 { SavedObjectAttributes } from '../../../src/core/types'; | ||
|
||
export const NOTE_SAVED_OBJECT = 'note'; | ||
|
||
export interface NoteSavedObjectAttributes extends SavedObjectAttributes { | ||
to?: string; | ||
from?: string; | ||
message: string; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
The `../todo` folder has two separate examples: a "by reference" and a "by value" todo embeddable example. | ||
This folder combines both examples into a single embeddable, but since we can only have one embeddable factory | ||
represent a single saved object type, this is built off a `note` saved object type. There is more complexity | ||
invovled in making | ||
it a single embeddable - it not only takes in an optional saved object id but can also accept edits to | ||
the values. This is closer to the real world use case we aim for with the Visualize Library. A user | ||
may have an embeddable on a dashboard that is "by value" but they would like to promote it to "by reference". | ||
|
||
Similarly they could break the link and convert back from by reference to by value. | ||
|
||
The input data is: | ||
|
||
```ts | ||
{ | ||
savedObjectId?: string; | ||
attributes: NoteSavedObjectAttributes; | ||
} | ||
``` | ||
|
||
`attributes` represent either the "by value" data, or, edits on top of the saved object id. | ||
|
||
The output data is: | ||
|
||
```ts | ||
{ | ||
savedAttributes?: NoteSavedObjectAttributes; | ||
} | ||
``` | ||
|
||
There is also an action that represents how this setup can be used with a save/create/edit action. | ||
|
||
You can only have one embeddable factory representation for a single saved object, so rather than use the | ||
`Todo` example, this is going to use a new embeddable - `note`. |
91 changes: 91 additions & 0 deletions
91
examples/embeddable_examples/public/note/create_edit_note_component.tsx
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,91 @@ | ||
/* | ||
* 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 React, { useState } from 'react'; | ||
import { EuiModalBody } from '@elastic/eui'; | ||
import { EuiFieldText } from '@elastic/eui'; | ||
import { EuiButton } from '@elastic/eui'; | ||
import { EuiModalFooter } from '@elastic/eui'; | ||
import { EuiModalHeader } from '@elastic/eui'; | ||
import { EuiFormRow } from '@elastic/eui'; | ||
import { NoteSavedObjectAttributes } from '../common'; | ||
|
||
export function CreateEditNoteComponent({ | ||
savedObjectId, | ||
attributes, | ||
onSave, | ||
}: { | ||
savedObjectId?: string; | ||
attributes?: NoteSavedObjectAttributes; | ||
onSave: (attributes: NoteSavedObjectAttributes, saveToLibrary: boolean) => void; | ||
}) { | ||
const [to, setTo] = useState(attributes?.to ?? ''); | ||
const [from, setFrom] = useState(attributes?.from ?? ''); | ||
const [message, setMessage] = useState(attributes?.message ?? ''); | ||
return ( | ||
<EuiModalBody> | ||
<EuiModalHeader> | ||
<h1>{`${savedObjectId ? 'Create new ' : 'Edit '}`}</h1> | ||
</EuiModalHeader> | ||
<EuiModalBody> | ||
<EuiFormRow label="To"> | ||
<EuiFieldText | ||
data-test-subj="toInputField" | ||
value={to} | ||
placeholder="To" | ||
onChange={e => setTo(e.target.value)} | ||
/> | ||
</EuiFormRow> | ||
<EuiFormRow label="From"> | ||
<EuiFieldText | ||
data-test-subj="fromInputField" | ||
value={from} | ||
placeholder="From" | ||
onChange={e => setFrom(e.target.value)} | ||
/> | ||
</EuiFormRow> | ||
<EuiFormRow label="Message"> | ||
<EuiFieldText | ||
data-test-subj="messageInputField" | ||
value={message} | ||
placeholder="Message" | ||
onChange={e => setMessage(e.target.value)} | ||
/> | ||
</EuiFormRow> | ||
</EuiModalBody> | ||
<EuiModalFooter> | ||
{savedObjectId === undefined ? ( | ||
<EuiButton | ||
data-test-subj="saveNoteEmbeddableByValue" | ||
disabled={message === ''} | ||
onClick={() => onSave({ message, to, from }, false)} | ||
> | ||
Save | ||
</EuiButton> | ||
) : null} | ||
<EuiButton | ||
data-test-subj="saveNoteEmbeddableByRef" | ||
disabled={message === ''} | ||
onClick={() => onSave({ message, to, from }, true)} | ||
> | ||
{savedObjectId ? 'Update library item' : 'Save to library'} | ||
</EuiButton> | ||
</EuiModalFooter> | ||
</EuiModalBody> | ||
); | ||
} |
83 changes: 83 additions & 0 deletions
83
examples/embeddable_examples/public/note/edit_note_action.tsx
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,83 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { OverlayStart, SavedObjectsClientContract } from 'kibana/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { NoteSavedObjectAttributes, NOTE_SAVED_OBJECT } from '../common'; | ||
import { createAction } from '../../../../src/plugins/ui_actions/public'; | ||
import { toMountPoint } from '../../../../src/plugins/kibana_react/public'; | ||
import { ViewMode } from '../../../../src/plugins/embeddable/public'; | ||
import { CreateEditNoteComponent } from './create_edit_note_component'; | ||
import { NoteEmbeddable, NOTE_EMBEDDABLE } from './note_embeddable'; | ||
|
||
interface StartServices { | ||
openModal: OverlayStart['openModal']; | ||
savedObjectsClient: SavedObjectsClientContract; | ||
} | ||
|
||
interface ActionContext { | ||
embeddable: NoteEmbeddable; | ||
} | ||
|
||
export const ACTION_EDIT_NOTE = 'ACTION_EDIT_NOTE'; | ||
|
||
export const createEditNoteAction = (getStartServices: () => Promise<StartServices>) => | ||
createAction({ | ||
getDisplayName: () => | ||
i18n.translate('embeddableExamples.note.edit', { defaultMessage: 'Edit' }), | ||
type: ACTION_EDIT_NOTE, | ||
isCompatible: async ({ embeddable }: ActionContext) => { | ||
return ( | ||
embeddable.type === NOTE_EMBEDDABLE && embeddable.getInput().viewMode === ViewMode.EDIT | ||
); | ||
}, | ||
execute: async ({ embeddable }: ActionContext) => { | ||
const { openModal, savedObjectsClient } = await getStartServices(); | ||
const onSave = async (attributes: NoteSavedObjectAttributes, includeInLibrary: boolean) => { | ||
if (includeInLibrary) { | ||
if (embeddable.getInput().savedObjectId) { | ||
await savedObjectsClient.update( | ||
NOTE_SAVED_OBJECT, | ||
embeddable.getInput().savedObjectId!, | ||
attributes | ||
); | ||
embeddable.updateInput({ attributes: undefined }); | ||
embeddable.reload(); | ||
} else { | ||
const savedItem = await savedObjectsClient.create(NOTE_SAVED_OBJECT, attributes); | ||
embeddable.updateInput({ savedObjectId: savedItem.id }); | ||
} | ||
} else { | ||
embeddable.updateInput({ attributes }); | ||
} | ||
}; | ||
const overlay = openModal( | ||
toMountPoint( | ||
<CreateEditNoteComponent | ||
savedObjectId={embeddable.getInput().savedObjectId} | ||
attributes={embeddable.getInput().attributes ?? embeddable.getOutput().savedAttributes} | ||
onSave={(attributes: NoteSavedObjectAttributes, includeInLibrary: boolean) => { | ||
overlay.close(); | ||
onSave(attributes, includeInLibrary); | ||
}} | ||
/> | ||
) | ||
); | ||
}, | ||
}); |
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,22 @@ | ||
/* | ||
* 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 './note_embeddable'; | ||
export * from './note_embeddable_factory'; | ||
export * from './edit_note_action'; |
Oops, something went wrong.