-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabstractViewBinding.ts
More file actions
200 lines (147 loc) · 5.21 KB
/
abstractViewBinding.ts
File metadata and controls
200 lines (147 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {IViewBinding} from "../../../engine-js/src/view/IViewBinding";
import {Card} from "../../../engine-js/src/tokenScript/Card";
import {TokenScript} from "../../../engine-js/src/TokenScript";
import {RequestFromView, ViewController, ViewEvent} from "@tokenscript/engine-js/src/view/ViewController";
import {RpcResponse} from "../../../engine-js/src/wallet/IWalletAdapter";
export abstract class AbstractViewBinding implements IViewBinding {
currentCard?: Card;
iframe: HTMLIFrameElement
actionBar: HTMLDivElement;
actionBtn: HTMLButtonElement;
loader: HTMLDivElement;
protected tokenScript: TokenScript
protected _viewController: ViewController;
constructor(protected view: HTMLElement) {
this.iframe = view.querySelector(".tokenscript-frame") as HTMLIFrameElement;
this.loader = view.querySelector(".view-loader") as HTMLDivElement;
this.actionBar = view.querySelector(".action-bar") as HTMLDivElement;
this.actionBtn = view.querySelector(".action-btn") as HTMLButtonElement;
if (this.actionBtn)
this.actionBtn.addEventListener('click', this.confirmAction.bind(this));
window.addEventListener("message", this.handlePostMessageFromView.bind(this));
}
// TODO: This can probably be accessed via view controller
setTokenScript(tokenScript: TokenScript) {
this.tokenScript = tokenScript;
}
setViewController(viewController: ViewController){
this._viewController = viewController;
this.setTokenScript(viewController.tokenScript);
}
get viewController() {
if (!this._viewController)
return this.tokenScript.getViewController();
return this._viewController;
}
viewLoading() {
this.showLoader();
}
viewError(error: Error): void {
// TODO: show error in view
this.hideLoader();
}
async showTokenView(card: Card) {
this.currentCard = card;
this.iframe = await AbstractViewBinding.injectContentView(this.iframe, card, this.viewController);
this.iframe.onload = () => {
this.hideLoader()
}
this.setupConfirmButton(card);
}
async unloadTokenView() {
this.currentCard = null;
this.actionBar.style.display = "none";
this.iframe.srcdoc = "<!DOCTYPE html>";
//this.iframe.contentWindow.location.replace("data:text/html;base64,PCFET0NUWVBFIGh0bWw+");
}
public showLoader(show = true) {
if (show){
this.loader.style.display = "flex";
} else {
this.hideLoader();
}
}
protected hideLoader() {
this.loader.style.display = "none";
// Return focus to iframe
if (this.iframe)
this.iframe.contentWindow.focus();
}
static async injectContentView(iframe: HTMLIFrameElement, card: Card, viewController: ViewController) {
if (!card.view) {
iframe.src = "";
return;
}
if (card.isUrlView) {
iframe.src = card.url;
} else {
const html = await viewController.tokenViewData.renderViewHtml();
if (new URLSearchParams(document.location.search).has("___b64url")){
const blob = new Blob([html], {type: "text/html"});
iframe.src = URL.createObjectURL(blob) + ("#" + viewController.tokenViewData.getCardUrlParameters());
} else {
iframe.srcdoc = html;
}
}
return iframe;
}
private setupConfirmButton(card: Card) {
if ((this.currentCard?.type == "action" || this.currentCard?.type == "onboarding") && this.currentCard.uiButton) {
this.actionBar.style.display = "block";
this.actionBtn.innerText = card.label;
} else {
this.actionBar.style.display = "none";
}
}
abstract confirmAction();
getViewBindingJavascript() {
return VIEW_BINDING_JAVASCRIPT;
}
protected postMessageToView(method: ViewEvent, params: any) {
this.iframe.contentWindow.postMessage({method, params}, "*");
}
dispatchRpcResult(response: RpcResponse): Promise<void> | void {
return this.iframe.contentWindow.postMessage(response, "*");
}
protected async handlePostMessageFromView(event: MessageEvent) {
if (!this.iframe)
return;
if (event.source !== this.iframe.contentWindow) {
return; // Skip message in this event listener
}
//const iframeOrig = new URL(this.iframe.src).origin;
//if (event.origin !== iframeOrig)
//return;
if (!event.data?.method)
return;
console.log("Event from view: ", event.data);
await this.handleMessageFromView(event.data.method, event.data?.params);
}
async handleMessageFromView(method: RequestFromView, params: any) {
// Use main controller to store user-entry values
// TODO: Move user entry value logic out of view controller to avoid this issue
if (method === RequestFromView.PUT_USER_INPUT){
await this.tokenScript.getViewController().handleMessageFromView(method, params);
return;
}
await this.viewController.handleMessageFromView(method, params);
}
async dispatchViewEvent(event: ViewEvent, data: any, id: string) {
switch (event) {
case ViewEvent.TOKENS_UPDATED:
const tokens = {
currentInstance: data
}
console.log("ViewEvent.TOKENS_UPDATED");
this.postMessageToView(event, {oldTokens: tokens, updatedTokens: tokens, cardId: id, id});
return;
default:
this.postMessageToView(event, {...data, id});
return;
}
}
}
// Note: This formerly had post message logic for the card to interact with the engine
// It has since been moved into the card SDK: javascript/card-sdk/src/messaging/PostMessageAdapter.ts
export const VIEW_BINDING_JAVASCRIPT = `
`;