-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
d583ba4
commit f5b7df3
Showing
1 changed file
with
121 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,121 @@ | ||
export enum IterableActionSource { | ||
PUSH = 'PUSH', | ||
APP_LINK = 'APP_LINK', | ||
IN_APP = 'IN_APP', | ||
EMBEDDED = 'EMBEDDED' | ||
} | ||
|
||
export interface IterableAction { | ||
type: string; | ||
data: string; | ||
} | ||
|
||
export interface IterableActionContext { | ||
action: IterableAction; | ||
source: IterableActionSource; | ||
} | ||
|
||
interface IterableConfig { | ||
urlHandler?: { | ||
handleIterableURL( | ||
uri: string, | ||
actionContext: IterableActionContext | ||
): boolean; | ||
}; | ||
customActionHandler?: { | ||
handleIterableCustomAction( | ||
action: IterableAction, | ||
actionContext: IterableActionContext | ||
): boolean; | ||
}; | ||
} | ||
|
||
class IterableActionRunnerConfig { | ||
static config: IterableConfig = {}; | ||
} | ||
|
||
class IterableActionRunnerImpl { | ||
private static TAG = 'IterableActionRunner'; | ||
|
||
executeAction( | ||
context: any, | ||
action: IterableAction | null, | ||
source: IterableActionSource | ||
): boolean { | ||
if (action === null) { | ||
return false; | ||
} | ||
|
||
const actionContext: IterableActionContext = { action, source }; | ||
|
||
if (action.type === 'openUrl') { | ||
return this.openUri(action.data, actionContext); | ||
} else { | ||
return this.callCustomActionIfSpecified(action, actionContext); | ||
} | ||
} | ||
|
||
private openUri(uri: string, actionContext: IterableActionContext): boolean { | ||
if (IterableActionRunnerConfig.config.urlHandler) { | ||
if ( | ||
IterableActionRunnerConfig.config.urlHandler.handleIterableURL( | ||
uri, | ||
actionContext | ||
) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
window.open(uri, '_blank'); | ||
|
||
return true; | ||
} | ||
|
||
private callCustomActionIfSpecified( | ||
action: IterableAction, | ||
actionContext: IterableActionContext | ||
): boolean { | ||
if (action.type && action.type !== '') { | ||
if (IterableActionRunnerConfig.config.customActionHandler) { | ||
return IterableActionRunnerConfig.config.customActionHandler.handleIterableCustomAction( | ||
action, | ||
actionContext | ||
); | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
IterableActionRunnerConfig.config = { | ||
urlHandler: { | ||
handleIterableURL( | ||
uri: string, | ||
actionContext: IterableActionContext | ||
): boolean { | ||
global.open(uri, '_blank', 'noopener,noreferrer'); | ||
return true; | ||
} | ||
}, | ||
customActionHandler: { | ||
handleIterableCustomAction( | ||
action: IterableAction, | ||
actionContext: IterableActionContext | ||
): boolean { | ||
let uri = ''; | ||
if (action.type.startsWith('action://')) { | ||
uri = action.type.replace('action://', ''); | ||
} else if (action.type.startsWith('iterable://')) { | ||
uri = action.type.replace('iterable://', ''); | ||
} | ||
|
||
global.open(uri, '_blank', 'noopener,noreferrer'); | ||
return true; | ||
} | ||
} | ||
}; | ||
|
||
export class IterableActionRunner extends IterableActionRunnerImpl { | ||
static config = IterableActionRunnerConfig.config; | ||
} |