-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
71e61fa
commit 3090ca9
Showing
53 changed files
with
3,261 additions
and
318 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
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,4 @@ | ||
{ | ||
"name": "embeddable_api", | ||
"version": "kibana" | ||
} |
78 changes: 78 additions & 0 deletions
78
src/legacy/core_plugins/embeddable_api/public/actions/action.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,78 @@ | ||
/* | ||
* 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 { EuiContextMenuItemIcon } from '@elastic/eui'; | ||
|
||
import { Container } from '../containers'; | ||
import { Embeddable } from '../embeddables'; | ||
|
||
export interface ExecuteActionContext< | ||
E extends Embeddable = Embeddable, | ||
C extends Container = Container, | ||
AC extends {} = {} | ||
> { | ||
embeddable: E; | ||
container?: C; | ||
triggerContext?: AC; | ||
} | ||
|
||
export interface ActionContext<E extends Embeddable = Embeddable, C extends Container = Container> { | ||
embeddable: E; | ||
container?: C; | ||
} | ||
|
||
export abstract class Action< | ||
E extends Embeddable = Embeddable, | ||
C extends Container = Container, | ||
T extends {} = {} | ||
> { | ||
/** | ||
* Determined the order when there is more than one action matched to a trigger. | ||
* Higher numbers are displayed first. | ||
*/ | ||
public priority: number = 0; | ||
|
||
constructor(public readonly id: string) {} | ||
|
||
/** | ||
* Optional icon that can be displayed along with the title. | ||
*/ | ||
public getIcon(context: ActionContext): EuiContextMenuItemIcon | undefined { | ||
return undefined; | ||
} | ||
|
||
/** | ||
* Returns a title to be displayed to the user. | ||
* @param context | ||
*/ | ||
public abstract getTitle(context: ActionContext): string; | ||
|
||
/** | ||
* Returns a promise that resolves to true if this action is compatible given the context, | ||
* otherwise resolves to false. | ||
*/ | ||
public isCompatible(context: ActionContext): Promise<boolean> { | ||
return Promise.resolve(true); | ||
} | ||
|
||
/** | ||
* Executes the action. | ||
*/ | ||
public abstract execute(context: ExecuteActionContext<E, C, T>): void; | ||
} |
78 changes: 78 additions & 0 deletions
78
src/legacy/core_plugins/embeddable_api/public/actions/action_registry.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,78 @@ | ||
/* | ||
* 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 { Action } from './action'; | ||
import { Embeddable } from '../embeddables'; | ||
import { Container } from '../containers'; | ||
import { triggerRegistry } from '../triggers'; | ||
|
||
class ActionRegistry { | ||
private actions: { [key: string]: Action } = {}; | ||
|
||
public addAction(action: Action) { | ||
if (this.getAction(action.id)) { | ||
throw new Error('Action already exists'); | ||
} | ||
this.actions[action.id] = action; | ||
} | ||
|
||
public getAction(id: string) { | ||
return this.actions[id]; | ||
} | ||
|
||
public removeAction(id: string) { | ||
delete this.actions[id]; | ||
} | ||
|
||
public getActions() { | ||
return this.actions; | ||
} | ||
|
||
public reset() { | ||
this.actions = {}; | ||
} | ||
|
||
public async getActionsForTrigger( | ||
triggerId: string, | ||
context: { embeddable: Embeddable; container?: Container } | ||
) { | ||
const trigger = triggerRegistry.getTrigger(triggerId); | ||
|
||
if (!trigger) { | ||
throw new Error(`Trigger with id ${triggerId} does not exist`); | ||
} | ||
|
||
const actions: Action[] = []; | ||
const promises = trigger.actionIds.map(async id => { | ||
const action = actionRegistry.getAction(id); | ||
if (!action) { | ||
throw new Error(`Action ${id} does not exist`); | ||
} | ||
if (!context || (await action.isCompatible(context))) { | ||
actions.push(action); | ||
} | ||
}); | ||
|
||
await Promise.all(promises); | ||
|
||
return actions; | ||
} | ||
} | ||
|
||
export const actionRegistry = new ActionRegistry(); |
91 changes: 91 additions & 0 deletions
91
src/legacy/core_plugins/embeddable_api/public/actions/apply_filter_action.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,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 { Container, ContainerInput } from '../containers'; | ||
import { EmbeddableInput, Embeddable, EmbeddableOutput } from '../embeddables'; | ||
import { APPLY_FILTER_TRIGGER, triggerRegistry } from '../triggers'; | ||
import { Filter } from '../types'; | ||
import { Action } from './action'; | ||
import { actionRegistry } from './action_registry'; | ||
|
||
interface ApplyFilterContainerInput extends ContainerInput { | ||
filters: Filter[]; | ||
} | ||
|
||
const APPLY_FILTER_ACTION_ID = 'APPLY_FILTER_ACTION_ID'; | ||
|
||
function containerAcceptsFilterInput( | ||
container: Embeddable | Container<{ id: string }, {}, ApplyFilterContainerInput> | ||
): container is Container<{ id: string }, {}, ApplyFilterContainerInput> { | ||
return ( | ||
(container as Container<{ id: string }, {}, ApplyFilterContainerInput>).getInput().filters !== | ||
undefined | ||
); | ||
} | ||
|
||
export class ApplyFilterAction extends Action< | ||
Embeddable, | ||
Container<EmbeddableInput, EmbeddableOutput, ApplyFilterContainerInput>, | ||
{ filters: Filter[] } | ||
> { | ||
constructor() { | ||
super(APPLY_FILTER_ACTION_ID); | ||
} | ||
|
||
public getTitle() { | ||
return 'Apply filter to current view'; | ||
} | ||
|
||
public isCompatible(context: { embeddable: Embeddable }) { | ||
let root = context.embeddable; | ||
while (root.parent) { | ||
root = root.parent; | ||
} | ||
|
||
return Promise.resolve(containerAcceptsFilterInput(root)); | ||
} | ||
|
||
public execute({ | ||
embeddable, | ||
triggerContext, | ||
}: { | ||
embeddable: Embeddable; | ||
triggerContext?: { filters: Filter[] }; | ||
}) { | ||
if (!triggerContext) { | ||
throw new Error('Applying a filter requires a filter as context'); | ||
} | ||
let root = embeddable; | ||
while (root.parent) { | ||
root = root.parent; | ||
} | ||
(root as Container<{ id: string }, {}, ApplyFilterContainerInput>).updateInput({ | ||
filters: triggerContext.filters, | ||
}); | ||
} | ||
} | ||
const applyFilterAction = new ApplyFilterAction(); | ||
if (!actionRegistry.getAction(applyFilterAction.id)) { | ||
actionRegistry.addAction(new ApplyFilterAction()); | ||
} | ||
|
||
triggerRegistry.attachAction({ | ||
triggerId: APPLY_FILTER_TRIGGER, | ||
actionId: APPLY_FILTER_ACTION_ID, | ||
}); |
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
Oops, something went wrong.