Skip to content

Commit

Permalink
✋ Implement setOrientationProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-rasevych-criteo committed Nov 20, 2023
1 parent fe1315a commit d70a8ca
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/criteo-mraid.js

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/mraid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import {
} from "./sdkfeature";
import { initialPosition, Position } from "./position";
import { ResizeProperties, ResizePropertiesValidator } from "./resize";
import {
defaultOrientationProperties,
Orientation,
OrientationProperties,
validateOrientationProperties,
} from "./orientationproperties";

export class MRAIDImplementation implements MRAIDApi, SDKApi {
private eventsCoordinator: EventsCoordinator;
Expand Down Expand Up @@ -57,6 +63,8 @@ export class MRAIDImplementation implements MRAIDApi, SDKApi {

private currentResizeProperties?: ResizeProperties = undefined;

private currentOrientationProperties = defaultOrientationProperties.clone();

constructor(
eventsCoordinator: EventsCoordinator,
sdkInteractor: SdkInteractor,
Expand Down Expand Up @@ -362,6 +370,31 @@ export class MRAIDImplementation implements MRAIDApi, SDKApi {
}
}

getOrientationProperties(): OrientationProperties {
return this.currentOrientationProperties.clone();
}

setOrientationProperties(
orientationProperties: OrientationProperties | Anything
): void {
const errorMessage = validateOrientationProperties(orientationProperties);
if (errorMessage) {
this.logger.log(LogLevel.Error, "setOrientationProperties", errorMessage);
} else {
this.currentOrientationProperties.forceOrientation =
orientationProperties.forceOrientation ??
defaultOrientationProperties.forceOrientation;
this.currentOrientationProperties.allowOrientationChange =
orientationProperties.allowOrientationChange ??
defaultOrientationProperties.allowOrientationChange;

this.sdkInteractor.setOrientationProperties(
this.currentOrientationProperties.allowOrientationChange,
this.currentOrientationProperties.forceOrientation
);
}
}

// #endregion

// #region SDKApi
Expand Down
16 changes: 16 additions & 0 deletions src/mraidapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Size } from "./size";
import { SdkFeature } from "./sdkfeature";
import { Position } from "./position";
import { ResizeProperties } from "./resize";
import { OrientationProperties } from "./orientationproperties";

export interface MRAIDApi {
/**
Expand Down Expand Up @@ -201,4 +202,19 @@ export interface MRAIDApi {
* @param resizeProperties
*/
setResizeProperties(resizeProperties: ResizeProperties | Anything): void;

/**
* @returns current OrientationProperties object
* If {@link setOrientationProperties} is never called then this method
* returns default values for properties
*/
getOrientationProperties(): OrientationProperties;

/**
* Sets OrientationProperties object
* @param orientationProperties
*/
setOrientationProperties(
orientationProperties: OrientationProperties | Anything
): void;
}
15 changes: 15 additions & 0 deletions src/mraidbridge/androidmraidbridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MraidBridge } from "./mraidbridge";
import { LogLevel } from "../log/loglevel";
import { ClosePosition } from "../resize";
import { Orientation } from "../orientationproperties";

/**
* Interaction object is defined in global scope (window) so we are using
Expand Down Expand Up @@ -37,6 +38,10 @@ export declare interface CriteoInterface {
customClosePosition: string,
allowOffscreen: boolean
): void;
setOrientationProperties(
allowOrientationChange: boolean,
forceOrientation: string
): void;
}

export class AndroidMraidBridge implements MraidBridge {
Expand Down Expand Up @@ -78,6 +83,16 @@ export class AndroidMraidBridge implements MraidBridge {
);
}

setOrientationProperties(
allowOrientationChange: boolean,
forceOrientation: Orientation
): void {
this.getMraidBridge()?.setOrientationProperties(
allowOrientationChange,
forceOrientation
);
}

private getMraidBridge(): CriteoInterface | undefined | null {
// criteoMraidBridge object is not injected into iframe on Android
// but doc says it should be. It is always available on topmost window
Expand Down
18 changes: 18 additions & 0 deletions src/mraidbridge/iosmraidbridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MraidBridge } from "./mraidbridge";
import { LogLevel } from "../log/loglevel";
import { ClosePosition } from "../resize";
import { Orientation } from "../orientationproperties";

// #region MessageHandler

Expand Down Expand Up @@ -60,6 +61,11 @@ export declare interface ResizeIosMessage extends IosMessage {
allowOffscreen: boolean;
}

export declare interface SetOrientationPropertiesMessage extends IosMessage {
allowOrientationChange: boolean;
forceOrientation: string;
}

// #endregion

export class IosMraidBridge implements MraidBridge {
Expand Down Expand Up @@ -125,6 +131,18 @@ export class IosMraidBridge implements MraidBridge {
this.postMessage(resizeMessage);
}

setOrientationProperties(
allowOrientationChange: boolean,
forceOrientation: Orientation
): void {
const orientationPropertiesMessage: SetOrientationPropertiesMessage = {
action: "set_orientation_properties",
allowOrientationChange,
forceOrientation,
};
this.postMessage(orientationPropertiesMessage);
}

private postMessage(message: IosMessage) {
window?.webkit?.messageHandlers?.criteoMraidBridge?.postMessage(message);
}
Expand Down
5 changes: 5 additions & 0 deletions src/mraidbridge/mraidbridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LogLevel } from "../log/loglevel";
import { ClosePosition } from "../resize";
import { Orientation } from "../orientationproperties";

/**
* Defines API for interaction with native platforms (iOS and Android)
Expand All @@ -19,4 +20,8 @@ export interface MraidBridge {
customClosePosition: ClosePosition,
allowOffscreen: boolean
): void;
setOrientationProperties(
allowOrientationChange: boolean,
forceOrientation: Orientation
): void;
}
10 changes: 10 additions & 0 deletions src/mraidbridge/sdkinteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MraidBridge } from "./mraidbridge";
import { LogLevel } from "../log/loglevel";
import { Anything } from "../utils";
import { ClosePosition } from "../resize";
import { Orientation } from "../orientationproperties";

/**
* Composite which delegates calls to native platforms handlers
Expand Down Expand Up @@ -63,6 +64,15 @@ export class SdkInteractor {
});
}

setOrientationProperties(
allowOrientationChange: boolean,
forceOrientation: Orientation
) {
this.callForAll((bridge) => {
bridge.setOrientationProperties(allowOrientationChange, forceOrientation);
});
}

private callForAll(lambda: (mraidBridge: MraidBridge) => void) {
this.bridges.forEach((bridge) => {
lambda(bridge);
Expand Down
71 changes: 71 additions & 0 deletions src/orientationproperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Anything } from "./utils";
import { ExpandProperties } from "./expand";

export enum Orientation {
Portrait = "portrait",
Landscape = "landscape",
None = "none",
}

export class OrientationProperties {
allowOrientationChange: boolean | Anything;

forceOrientation: Orientation | Anything;

constructor(allowOrientationChange: boolean, forceOrientation: Orientation) {
this.allowOrientationChange = allowOrientationChange;
this.forceOrientation = forceOrientation;
}

clone(): OrientationProperties {
return new OrientationProperties(
this.allowOrientationChange,
this.forceOrientation
);
}
}

export const defaultOrientationProperties = new OrientationProperties(
true,
Orientation.None
);

export function joinedOrientations(): string {
return `[${Object.values(Orientation).join(", ")}]`;
}

export function validateOrientationProperties(
orientationProperties: OrientationProperties | Anything
): string | null {
if (!orientationProperties) {
return "Orientation properties object is not passed";
}
if (Object.keys(orientationProperties).length === 0) {
return "Orientation properties object is empty";
}
let hasAnyProperty = false;
Object.keys(defaultOrientationProperties).forEach((property) => {
if (Object.prototype.hasOwnProperty.call(orientationProperties, property)) {
hasAnyProperty = true;
}
});
if (!hasAnyProperty) {
return "The orientation properties object does not contain the 'allowOrientationChange' or 'forceOrientation' properties";
}

const { allowOrientationChange, forceOrientation } = orientationProperties;

if (allowOrientationChange) {
if (typeof allowOrientationChange !== "boolean") {
return "'allowOrientationChange' should be boolean";
}
}

if (forceOrientation) {
if (Object.values(Orientation).includes(forceOrientation)) {
return null;
}
return `'forceOrientation' should be one of ${joinedOrientations()}`;
}
return null;
}
Loading

0 comments on commit d70a8ca

Please sign in to comment.