-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensible components API to create components which can expose A…
…PI and render endpoints
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare var ManageIQ: any; | ||
declare var Rx: any; |
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,49 @@ | ||
import { IExtensionComponent, IMiQApiCallback } from './lib'; | ||
|
||
/** | ||
* Class for easy creation of extensible component. | ||
*/ | ||
export class ExtensibleComponent { | ||
public unsubscribe: Function; | ||
constructor(public name: string, public api: IMiQApiCallback, public render: IMiQApiCallback){} | ||
} | ||
|
||
/** | ||
* Create new object which will hold extension components on MiQ main object. | ||
*/ | ||
ManageIQ.extensionComponents = ManageIQ.extensionComponents || {}; | ||
|
||
/** | ||
* Subject from from Rxjs to send message that we want to register new component. | ||
*/ | ||
ManageIQ.extensionComponents.source = new Rx.Subject(); | ||
|
||
/** | ||
* Components will be saved in items which will be set. To easy remove of components which no longer exists. | ||
*/ | ||
ManageIQ.extensionComponents.items = new Set(); | ||
|
||
/** | ||
* Helper function to create new component. | ||
* @param name string name of new component. | ||
* @param api callback functions to change inner logic of component. | ||
* @param render callback function to apply render functions. | ||
*/ | ||
ManageIQ.extensionComponents.newComponent = function(name: string, api?: IMiQApiCallback, render?: IMiQApiCallback) { | ||
const newCmp = new ExtensibleComponent(name, api, render); | ||
ManageIQ.extensionComponents.source.onNext({action: 'add', payload: newCmp}); | ||
} | ||
|
||
/** | ||
* Subscribe to extensionComponents source to add new components to items object. | ||
*/ | ||
ManageIQ.extensionComponents.source.subscribe((event: IExtensionComponent) => { | ||
if (event.action === 'add' && event.hasOwnProperty('payload')) { | ||
event.payload.unsubscribe = () => { | ||
ManageIQ.extensionComponents.items.delete(event.payload); | ||
} | ||
ManageIQ.extensionComponents.items.add(event.payload); | ||
} else { | ||
throw new Error('Unsupported action with extension components.'); | ||
} | ||
}); |
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 @@ | ||
export interface IExtensionComponent { | ||
action: string; | ||
payload: any; | ||
} | ||
|
||
export interface IMiQApiCallback { | ||
[propName: string]: Function; | ||
} | ||
|
||
export interface IExtensibleComponent { | ||
extensibleComponent: any; | ||
apiCallbacks: () => IMiQApiCallback; | ||
renderCallbacks: () => IMiQApiCallback; | ||
} | ||
|
||
export const extensionSource = ManageIQ.extensionComponents.source; | ||
|
||
export const extensionItems = ManageIQ.extensionComponents.items; | ||
|
||
/** | ||
* Helper function to create new component. | ||
* @param name string name of new component. | ||
* @param api callback functions to change inner logic of component. | ||
* @param render callback function to apply render functions. | ||
*/ | ||
export function newComponent(name: string, api?: IMiQApiCallback, render?: IMiQApiCallback) { | ||
ManageIQ.extensionComponents.newComponent(name, api, render); | ||
} |
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 @@ | ||
require('../extensible-components'); |