Skip to content

Commit

Permalink
Add extensible components API to create components which can expose A…
Browse files Browse the repository at this point in the history
…PI and render endpoints
  • Loading branch information
karelhala committed Aug 31, 2017
1 parent 0e2ef08 commit d175095
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/javascript/custom-typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare var ManageIQ: any;
declare var Rx: any;
49 changes: 49 additions & 0 deletions app/javascript/extensible-components/index.ts
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.');
}
});
28 changes: 28 additions & 0 deletions app/javascript/extensible-components/lib.ts
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);
}
1 change: 1 addition & 0 deletions app/javascript/packs/extensible-componenets-common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../extensible-components');

0 comments on commit d175095

Please sign in to comment.