-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,843 additions
and
1,056 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
82 changes: 82 additions & 0 deletions
82
how-to/web-interop-support-context-and-intents/client/src/content/fdc3-intent-view.ts
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,82 @@ | ||
import type { Context } from "@finos/fdc3"; | ||
import { init } from "./api"; | ||
|
||
window.addEventListener("DOMContentLoaded", async () => { | ||
await init(true); | ||
await initializeDOM(); | ||
}); | ||
|
||
/** | ||
* Raise an intent using the fdc3 API. | ||
*/ | ||
async function raiseIntent(): Promise<void> { | ||
if (window.fin !== undefined) { | ||
const context = { | ||
type: "fdc3.contact", | ||
name: "Andy Young", | ||
id: { | ||
email: "[email protected]" | ||
} | ||
}; | ||
const intent = "ViewContact"; | ||
const intentResolver = await window.fdc3.raiseIntent(intent, context); | ||
if (intentResolver !== undefined) { | ||
console.log("Intent resolver received:", intentResolver); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Adds an fdc3 intent listener to the window. | ||
*/ | ||
async function addIntentListener(): Promise<void> { | ||
const intent = "ViewContact"; | ||
if (window.fdc3) { | ||
await window.fdc3.addIntentListener(intent, (ctx, metadata) => { | ||
console.log(`Received Context For Intent: ${intent}`, ctx); | ||
console.log(`Received Metadata With Intent: ${intent}`, metadata); | ||
updateDOMElements(ctx); | ||
}); | ||
} else { | ||
window.addEventListener("fdc3Ready", async () => { | ||
if (window.fdc3) { | ||
await window.fdc3.addIntentListener(intent, (ctx, metadata) => { | ||
console.log(`Received Context For Intent: ${intent}`, ctx); | ||
console.log(`Received Metadata With Intent: ${intent}`, metadata); | ||
updateDOMElements(ctx); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the DOM elements with the provided context. | ||
* @param context The context to update the DOM elements with. | ||
*/ | ||
function updateDOMElements(context: Context): void { | ||
const contextTypeSpan = document.querySelector<HTMLSpanElement>("#contextType"); | ||
const contextNameSpan = document.querySelector<HTMLSpanElement>("#contextName"); | ||
const contextBodyPre = document.querySelector<HTMLPreElement>("#contextBody"); | ||
|
||
if (contextTypeSpan !== null && contextNameSpan !== null && contextBodyPre !== null) { | ||
contextTypeSpan.textContent = context.type; | ||
contextNameSpan.textContent = context.name ?? "No name provided."; | ||
contextBodyPre.textContent = JSON.stringify(context, null, 2); | ||
} | ||
} | ||
|
||
/** | ||
* Initialize the DOM elements. | ||
*/ | ||
async function initializeDOM(): Promise<void> { | ||
const raiseIntentButton = document.querySelector<HTMLButtonElement>("#raiseIntent"); | ||
|
||
if (raiseIntentButton !== null) { | ||
raiseIntentButton.addEventListener("click", async () => { | ||
await raiseIntent(); | ||
}); | ||
} | ||
|
||
await addIntentListener(); | ||
} |
Oops, something went wrong.