forked from microsoft/AdaptiveCards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add other handles to RenderOptions and rename to IRenderOptions (micr…
- Loading branch information
1 parent
6a75a94
commit ad3516d
Showing
2 changed files
with
55 additions
and
45 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
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 |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import { Action, AdaptiveCard, HostConfig } from "./adaptivecards"; | ||
import { Action, ShowCardAction, CardElement, AdaptiveCard, HostConfig } from "./adaptivecards"; | ||
import { IAdaptiveCard } from "./schema"; | ||
|
||
export interface RenderOptions { | ||
export interface IRenderOptions { | ||
hostConfig?: HostConfig | string | object; | ||
onAnchorClicked?: (anchor: HTMLAnchorElement) => boolean; | ||
onExecuteAction?: (action: Action) => void; | ||
onValidationError?: (error: string) => void; | ||
onElementVisibilityChanged?: (element: CardElement) => void; | ||
onInlineCardExpanded?: (action: ShowCardAction, isExpanded: boolean) => void; | ||
onParseElement?: (element: CardElement, json: any) => void; | ||
processMarkdown?: (text: string) => string; | ||
} | ||
|
||
export function renderCard(card: IAdaptiveCard | string, options?: RenderOptions): HTMLElement { | ||
|
||
export function renderCard(card: IAdaptiveCard | string, options?: IRenderOptions): HTMLElement { | ||
if (typeof card === "string") { | ||
card = <IAdaptiveCard>JSON.parse(card); | ||
} | ||
|
||
options = options || {}; | ||
|
||
// Parse the host config | ||
let hostConfig: HostConfig; | ||
if (isHostConfig(options.hostConfig)) { | ||
hostConfig = options.hostConfig; | ||
// Setup a card | ||
let adaptiveCard = new AdaptiveCard(); | ||
|
||
if (typeof options.hostConfig === "string") { | ||
adaptiveCard.hostConfig = new HostConfig(JSON.parse(options.hostConfig)); | ||
} | ||
else if (typeof options.hostConfig === "object" && !(options.hostConfig instanceof HostConfig)) { | ||
adaptiveCard.hostConfig = new HostConfig(options.hostConfig); | ||
} | ||
else if (options.hostConfig instanceof String) { | ||
hostConfig = new HostConfig(JSON.parse(options.hostConfig)); | ||
} else { | ||
hostConfig = new HostConfig(options.hostConfig); | ||
else { | ||
adaptiveCard.hostConfig = options.hostConfig; | ||
} | ||
|
||
// Parse the card | ||
let adaptiveCard = new AdaptiveCard(); | ||
adaptiveCard.parse(card); | ||
adaptiveCard.hostConfig = hostConfig; | ||
adaptiveCard.onAnchorClicked = options.onAnchorClicked; | ||
adaptiveCard.onExecuteAction = options.onExecuteAction; | ||
|
||
// Process markdown | ||
adaptiveCard.onElementVisibilityChanged = options.onElementVisibilityChanged; | ||
adaptiveCard.onInlineCardExpanded = options.onInlineCardExpanded; | ||
adaptiveCard.onParseElement = options.onParseElement; | ||
|
||
if (options.processMarkdown) { | ||
AdaptiveCard.processMarkdown = options.processMarkdown; | ||
} | ||
|
||
// Parse the card | ||
adaptiveCard.parse(card); | ||
|
||
// Render the card | ||
return adaptiveCard.render(); | ||
} | ||
|
||
function isHostConfig(hostConfig: HostConfig | string | any): hostConfig is HostConfig { | ||
// TODO: make this check better | ||
return hostConfig && hostConfig.getContainerStyleDefinition; | ||
} | ||
} |