Skip to content

Commit

Permalink
Split View microfrontends (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmarkus authored Jul 25, 2019
1 parent 34dcf8a commit 42e423c
Show file tree
Hide file tree
Showing 24 changed files with 1,717 additions and 170 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ The lerna-changelog tool detects changes based on PR labels and maps them to sec
* [#211](https://github.com/SAP/luigi/pull/211) Multiple path parameters do not get replaced in view url ([@pekura](https://github.com/pekura))
* [#212](https://github.com/SAP/luigi/pull/212) Fix failing unit tests ([@dariadomagala](https://github.com/dariadomagala))
* [#206](https://github.com/SAP/luigi/pull/206) Center the logo ([@dariadomagala](https://github.com/dariadomagala))
* [#196](https://github.com/SAP/luigi/pull/196) Fix for goBack when not using micro-frontend without routing ([@maxmarkus](https://github.com/maxmarkus))
* [#196](https://github.com/SAP/luigi/pull/196) Fix for goBack when not using micro frontend without routing ([@maxmarkus](https://github.com/maxmarkus))
* [#177](https://github.com/SAP/luigi/pull/177) Allow multiple init and update listeners ([@maxmarkus](https://github.com/maxmarkus))

#### :memo: Documentation
Expand Down
53 changes: 53 additions & 0 deletions client/luigi-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ export declare interface ModalSettings {
size?: 'l' | 'm' | 's';
}

export declare interface SplitViewSettings {
title?: string;
size?: number;
}

export enum SplitViewEvents {
'expand',
'collapse',
'resize',
'close'
}

export declare interface SplitViewInstance {
collapse: () => void;
expand: () => void;
setSize: (value: number) => void;
on: (key: SplitViewEvents, callback: () => void) => string; //
exists: () => boolean;
getSize: () => number;
isCollapsed: () => boolean;
isExpanded: () => boolean;
}

export declare interface Context {
authData?: AuthData;
context?: { parentNavigationContext?: string[] };
Expand Down Expand Up @@ -127,6 +150,18 @@ export declare interface UxManager {
* @param {string} locale locale to be set as the current locale
*/
setCurrentLocale: (locale: string) => void;

/**
* Checks if the current micro frontend is displayed inside a split view
* @returns {boolean} indicating if it is loaded inside a split view
*/
isSplitView: () => boolean;

/**
* Checks if the current micro frontend is displayed inside a modal
* @returns {boolean} indicating if it is loaded inside a modal
*/
isModal: () => boolean;
}

export declare interface LinkManager {
Expand Down Expand Up @@ -219,6 +254,24 @@ export declare interface LinkManager {
* LuigiClient.linkManager().openAsModal('projects/pr1/users', {title:'Users', size:'m'});
*/
openAsModal: (nodepath: string, modalSettings?: ModalSettings) => void;

/**
* Opens a view in a split view. You can specify the split view's title and size. If you don't specify the title, it is the node label. If there is no node label, the title remains empty. The default size of the split view is `40`, which means 40% height of the split view.
* @memberof linkManager
* @param {string} path navigation path
* @param {Object} splitViewSettings opens a view in a split view. Use these settings to configure the split view's behaviour
* @param {string} splitViewSettings.title split view title. By default, it is the node label. If there is no label, it is left empty
* @param {number} [splitViewSettings.size=40] height of the split view in percent
* @returns {Object} instance of the SplitView. It provides event listeners and you can the functions to control its behavior
* @see {@link splitView} for further documentation about the returned instance
* @example
* const splitViewHandle = LuigiClient.linkManager().openAsSplitView('projects/pr1/logs', {title: 'Logs', size: 40});
*/
openAsSplitView: (
path: string,
splitViewSettings?: SplitViewSettings
) => SplitViewInstance;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions client/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ class Helpers {
* @private
* @param {string} name event name
* @param {function} eventFn callback function
* @returns {boolean}
* @returns {string} listener id
*/
addEventListener(name, eventFn) {
const listenerId = this.getRandomId();
this.listeners.push({
name,
eventFn,
listenerId: this.getRandomId()
listenerId
});
return listenerId;
}

/**
Expand Down
35 changes: 32 additions & 3 deletions client/src/linkManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LuigiClientBase } from './baseClass';
import { helpers } from './helpers';
import { splitViewHandle } from './splitViewHandle';

/**
* The Link Manager allows you to navigate to another route. Use it instead of an internal router to:
Expand All @@ -13,7 +14,7 @@ export class linkManager extends LuigiClientBase {
* @private
*/
constructor(values) {
// @param {Object} values TODO: is it necessary at all, where is it used?
// @param {object} values TODO: is it necessary at all, where is it used?
super();
Object.assign(this, values);

Expand All @@ -37,16 +38,26 @@ export class linkManager extends LuigiClientBase {
* @param {Object} modalSettings opens a view in a modal. Use these settings to configure the modal's title and size
* @param {string} modalSettings.title modal title. By default, it is the node label. If there is no label, it is left empty
* @param {('l'|'m'|'s')} [modalSettings.size="l"] size of the modal
* @param {Object} splitViewSettings opens a view in a split view. Use these settings to configure the split view's behaviour
* @param {string} splitViewSettings.title split view title. By default, it is the node label. If there is no label, it is left empty
* @param {number} [splitViewSettings.size=40] height of the split view in percent
* @param {boolean} [splitViewSettings.collapsed=false] creates split view but leaves it closed initially
* @example
* LuigiClient.linkManager().navigate('/overview')
* LuigiClient.linkManager().navigate('users/groups/stakeholders')
* LuigiClient.linkManager().navigate('/settings', null, true) // preserve view
*/
navigate(path, sessionId, preserveView, modalSettings) {
navigate(path, sessionId, preserveView, modalSettings, splitViewSettings) {
if (this.options.errorSkipNavigation) {
this.options.errorSkipNavigation = false;
return;
}
if (modalSettings && splitViewSettings) {
console.warn(
'modalSettings and splitViewSettings cannot be used together. Only modal setting will be taken into account.'
);
}

this.options.preserveView = preserveView;
const relativePath = path[0] !== '/';
const navigationOpenMsg = {
Expand All @@ -55,7 +66,8 @@ export class linkManager extends LuigiClientBase {
params: Object.assign(this.options, {
link: path,
relative: relativePath,
modal: modalSettings
modal: modalSettings,
splitView: splitViewSettings
})
};

Expand All @@ -76,6 +88,23 @@ export class linkManager extends LuigiClientBase {
this.navigate(path, 0, true, modalSettings || {});
}

/**
* Opens a view in a split view. You can specify the split view's title and size. If you don't specify the title, it is the node label. If there is no node label, the title remains empty. The default size of the split view is `40`, which means 40% height of the split view.
* @memberof linkManager
* @param {string} path navigation path
* @param {Object} splitViewSettings opens a view in a split view. Use these settings to configure the split view's behaviour
* @param {string} splitViewSettings.title split view title. By default, it is the node label. If there is no label, it is left empty
* @param {number} [splitViewSettings.size=40] height of the split view in percent
* @returns {Object} instance of the SplitView. It provides Event listeners and you can use the available functions to control its behavior.
* @see {@link splitView} for further documentation about the returned instance
* @example
* const splitViewHandle = LuigiClient.linkManager().openAsSplitView('projects/pr1/logs', {title: 'Logs', size: 40});
*/
openAsSplitView(path, splitViewSettings = {}) {
this.navigate(path, 0, true, undefined, splitViewSettings);
return new splitViewHandle(splitViewSettings);
}

/**
* Sets the current navigation context to that of a specific parent node which has the {@link navigation-configuration.md navigationContext} field declared in the navigation configuration. This navigation context is then used by the `navigate` function.
* @memberof linkManager
Expand Down
168 changes: 168 additions & 0 deletions client/src/splitViewHandle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { LuigiClientBase } from './baseClass';
import { helpers } from './helpers';

/**
* Split view
Allows to open a micro frontend in a split screen in the lower part of the content area. Open it by calling `const splitViewHandle = LuigiClient.linkManager().openAsSplitView`.
At a given time, you can open only one split view. It closes automatically when you navigate to a different route.
When you call `handle.collapse()`, the split view gets destroyed. It recreates when you use `handle.expand()`.
`openAsSplitView` returns an instance of the split view handle. The functions, actions, and event handlers listed below allow you to control and manage the split view.
* @name splitView
*/
export class splitViewHandle extends LuigiClientBase {
/**
* @private
*/
constructor(settings) {
super();

this.validSplitViewEvents = ['expand', 'collapse', 'resize', 'close'];

this.splitView = {
exists: true,
size: 40,
collapsed: false
};

Object.assign(this, settings);

const removeEventListeners = () => {
this.splitView.listeners.forEach(id => helpers.removeEventListener(id));
};

this.splitView.listeners = [
helpers.addEventListener(`luigi.navigation.splitview.internal`, e => {
Object.assign(this.splitView, e.data.data);
})
];
this.on('resize', newSize => {
this.splitView.size = newSize;
});
this.on('close', removeEventListeners);
window.onunload = () => removeEventListeners;
}

/*
* @private
*/
sendSplitViewEvent(action, data) {
helpers.sendPostMessageToLuigiCore({
msg: `luigi.navigation.splitview.${action}`,
data
});
}

/**
* Collapses the split view
* @memberof splitView
* @example
* splitViewHandle.collapse();
*/
collapse() {
this.sendSplitViewEvent('collapse');
}
/**
* Expands the split view
* @memberof splitView
* @example
* splitViewHandle.expand();
*/
expand() {
this.sendSplitViewEvent('expand');
}

/**
* Closes and destroys the split view
* @memberof splitView
* @example
* splitViewHandle.close();
*/
close() {
this.sendSplitViewEvent('close');
}
/**
* Sets the height of the split view
* @memberof splitView
* @param {number} value lower height in percent
* @example
* splitViewHandle.setSize(60);
*/
setSize(value) {
this.sendSplitViewEvent('resize', value);
}
/**
* Registers a listener for split view events
* @memberof splitView
* @param {('expand'|'collapse'|'resize'|'close')} name event name
* @param {function} callback gets called when this event gets triggered by Luigi
* @returns {string} listener id
* @example
* const listenerId = splitViewHandle.on('expand', () => {});
* const listenerId = splitViewHandle.on('collapse', () => {});
* const listenerId = splitViewHandle.on('resize', () => {});
* const listenerId = splitViewHandle.on('close', () => {});
**/
on(name, callback) {
if (!this.validSplitViewEvents.includes(name)) {
console.warn(name + ' is not a valid split view event');
return false;
}
const id = helpers.addEventListener(
`luigi.navigation.splitview.${name}.ok`,
e => callback(e.data.data)
);
this.splitView.listeners.push(id);
return id;
}
/**
* Unregisters a split view listener
* @memberof splitView
* @param {string} id listener id
* @example
* splitViewHandle.removeEventListener(listenerId);
*/
removeEventListener(id) {
return helpers.removeEventListener(id);
}

/**
* Gets the split view status
* @memberof splitView
* @returns {boolean} true if a split view is loaded
* @example
* splitViewHandle.exists();
*/
exists() {
return this.splitView.exists;
}
/**
* Reads the size of the split view
* @memberof splitView
* @returns {number} height in percent
* @example
* splitViewHandle.getSize();
*/
getSize() {
return this.splitView.size;
}
/**
* Reads the collapse status
* @memberof splitView
* @returns {boolean} true if the split view is currently collapsed
* @example
* splitViewHandle.isCollapsed();
*/
isCollapsed() {
return this.splitView.collapsed;
}
/**
* Reads the expand status
* @memberof splitView
* @returns {boolean} true if the split view is currently expanded
* @example
* splitViewHandle.isExpanded();
*/
isExpanded() {
return !this.splitView.collapsed;
}
}
33 changes: 24 additions & 9 deletions client/src/uxManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,31 @@ class UxManager extends LuigiClientBase {
*/
setCurrentLocale(locale) {
if (locale) {
window.parent.postMessage(
{
msg: 'luigi.ux.set-current-locale',
data: {
currentLocale: locale
}
},
'*'
);
helpers.sendPostMessageToLuigiCore({
msg: 'luigi.ux.set-current-locale',
data: {
currentLocale: locale
}
});
}
}

/**
* Checks if the current micro-frontend is displayed inside a split view
* @returns {boolean} indicating if it is loaded inside a split view
* @memberof uxManager
*/
isSplitView() {
return lifecycleManager.currentContext?.internal?.splitView;
}

/**
* Checks if the current micro-frontend is displayed inside a modal
* @returns {boolean} indicating if it is loaded inside a modal
* @memberof uxManager
*/
isModal() {
return lifecycleManager.currentContext?.internal?.modal;
}
}
export const uxManager = new UxManager();
Loading

0 comments on commit 42e423c

Please sign in to comment.