Skip to content

Commit

Permalink
Add isInsideShellModal() method to know if app is running inside Sh…
Browse files Browse the repository at this point in the history
…ell Modal
  • Loading branch information
Sébastien Barbier committed Apr 5, 2021
1 parent 4f6172a commit a1f95c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Messages to handle MODAL open and close event
- Add `isInsideShellModal()` method to know if app is running inside Shell Modal

## [1.9.2] - 2021-02-25

Expand Down
20 changes: 20 additions & 0 deletions src/ShellSdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ describe('Shell Sdk', () => {

expect(technicianId).toEqual(42);
expect(servicecallId).toEqual(1337);
expect(sdk.isInsideShellModal()).toEqual(false);
});

it('should trigger onViewState after request_context then send to parent loading_success', () => {
Expand Down Expand Up @@ -444,4 +445,23 @@ describe('Shell Sdk', () => {
const isInsideShell = ShellSdk.isInsideShell();
expect(isInsideShell).toEqual(window.top !== window.self);
});

it('IsInsideShellModal() return true when REQUIRE_CONTEXT has isInsideShellModal: true', () => {
let technicianId: number;
let servicecallId: number;

sdk = ShellSdk.init(sdkTarget, sdkOrigin, windowMock);

windowMockCallback({
data: {
type: SHELL_EVENTS.Version1.REQUIRE_CONTEXT,
value: {
message: 'test data',
isInsideShellModal: true,
},
},
});

expect(sdk.isInsideShellModal()).toEqual(true);
});
});
7 changes: 7 additions & 0 deletions src/ShellSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class ShellSdk {

private static _instance: ShellSdk;
private isRoot: boolean; // Is root if on `init`, target value is null.
private isInsideModal: boolean;

private postMessageHandler:
| (<T>(type: EventType, value: T, to?: string[]) => void)
Expand Down Expand Up @@ -48,6 +49,7 @@ export class ShellSdk {
this.initMessageApi();
this.debugger = new Debugger(winRef, debugId);
this.isRoot = target == null;
this.isInsideModal = false;
}

public static init(
Expand Down Expand Up @@ -84,6 +86,10 @@ export class ShellSdk {
return winRef.self !== winRef.top;
}

public isInsideShellModal(): boolean {
return this.isInsideModal;
}

public setAllowedOrigins(allowedOrigins: string[] | '*' = []) {
this.allowedOrigins = allowedOrigins === '*' ? [] : allowedOrigins;
}
Expand Down Expand Up @@ -478,6 +484,7 @@ export class ShellSdk {
? JSON.parse(payload.value)
: payload.value;
const viewState = context.viewState;
this.isInsideModal = !!context.isInsideShellModal;
if (viewState) {
for (const key of Object.keys(viewState)) {
const thisSubscribers = this.subscribersViewStateMap.get(`${key}`);
Expand Down

0 comments on commit a1f95c8

Please sign in to comment.