/
ftrackWidget.ts
321 lines (290 loc) · 7.23 KB
/
ftrackWidget.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* ftrackWidget module.
*
* Handle communication with the ftrack web application.
*/
export interface WidgetLoadMessage {
topic: "ftrack.widget.load";
data: {
credentials: CredentialsType;
theme: "light" | "dark";
entity: Entity;
targetOrigin?: string;
};
}
export interface WidgetUpdateMessage {
topic: "ftrack.widget.update";
data: {
entity: Entity;
targetOrigin?: string;
};
}
type WidgetMessage = WidgetLoadMessage | WidgetUpdateMessage;
export interface Entity {
id: string;
type: string;
}
export interface CredentialsType {
serverUrl: string;
apiUser: string;
apiKey: string;
csrfToken: string;
}
let targetOrigin: string;
let credentials: CredentialsType = {
serverUrl: "",
apiUser: "",
apiKey: "",
csrfToken: "",
};
let entity: Entity;
let onWidgetLoadCallback: (content: WidgetLoadMessage) => void,
onWidgetUpdateCallback: (content: WidgetUpdateMessage) => void;
/**
* Open sidebar for *entityType*, *entityId*.
*/
export function openSidebar(entityType: string, entityId: string) {
console.debug("Opening sidebar", entityType, entityId);
window.parent.postMessage(
{
topic: "ftrack.application.open-sidebar",
data: {
type: entityType,
id: entityId,
},
},
targetOrigin || credentials.serverUrl,
);
}
/**
* Open actions window for *selection*.
* @param selection List of entities to open actions for.
*/
export function openActions(selection: Entity[]) {
console.debug("Opening actions", selection);
window.parent.postMessage(
{
topic: "ftrack.application.open-actions",
data: {
selection: selection,
},
},
targetOrigin || credentials.serverUrl,
);
}
/**
* Close action window for current widget.
*/
export function closeWidget() {
console.debug("Close widget");
window.parent.postMessage(
{
topic: "ftrack.application.close-widget",
},
targetOrigin || credentials.serverUrl,
);
}
/**
* Open preview for *componentId*.
*/
export function openPreview(componentId: string) {
console.debug("Open preview", componentId);
window.parent.postMessage(
{
topic: "ftrack.application.open-preview",
data: {
componentId: componentId,
},
},
targetOrigin || credentials.serverUrl,
);
}
/**
* Navigate web app to *entityType*, *entityId*.
*/
export function navigate(entityType: string, entityId: string, module: string) {
module = module || "project";
console.debug("Navigating", entityType, entityId);
window.parent.postMessage(
{
topic: "ftrack.application.navigate",
data: {
type: entityType,
id: entityId,
module: module,
},
},
targetOrigin || credentials.serverUrl,
);
}
/**
* Update credentials and entity, call callback when widget loads.
*/
function onWidgetLoad(content: WidgetLoadMessage) {
console.debug("Widget loaded", content);
if (content.data.targetOrigin) {
targetOrigin = content.data.targetOrigin;
}
credentials = content.data.credentials;
entity = content.data.entity;
if (onWidgetLoadCallback) {
onWidgetLoadCallback(content);
}
window.dispatchEvent(
new CustomEvent("ftrackWidgetLoad", {
detail: {
credentials: credentials,
entity: entity,
},
}),
);
if (entity) {
window.dispatchEvent(
new CustomEvent("ftrackWidgetUpdate", { detail: { entity } }),
);
}
}
/**
* Update entity and call callback whent wigdet is updated.
*/
function onWidgetUpdate(content: WidgetUpdateMessage) {
console.debug("Widget updated", content);
entity = content.data.entity;
if (onWidgetUpdateCallback) {
onWidgetUpdateCallback(content);
}
window.dispatchEvent(
new CustomEvent("ftrackWidgetUpdate", { detail: { entity } }),
);
}
/**
* Handle post messages.
*/
function onPostMessageReceived(event: MessageEvent) {
const content = (event.data || {}) as WidgetMessage;
if (!content.topic) {
return;
}
console.debug('Got "' + content.topic + '" event.', content);
if (content.topic === "ftrack.widget.load") {
onWidgetLoad(content);
} else if (content.topic === "ftrack.widget.update") {
onWidgetUpdate(content);
}
}
/**
* Return current entity.
*/
export function getEntity() {
return entity;
}
/**
* Return API credentials.
*/
export function getCredentials() {
return credentials;
}
/**
* On document clicked forward to parent application
*/
function onDocumentClick() {
window.parent.postMessage(
{
topic: "ftrack.application.document-clicked",
data: {},
},
targetOrigin || credentials.serverUrl,
);
}
/**
* On document keydown forward to parent application.
*/
function onDocumentKeyDown(event: KeyboardEvent) {
// Ignore events when focus is in an textarea/input/contenteditable.
const target = event.target;
if (!target || !(target instanceof HTMLElement)) return;
const tagName = target.tagName.toLowerCase();
if (
["textarea", "input"].indexOf(tagName) !== -1 ||
target.isContentEditable
) {
return;
}
// Copy event data to KeyboardEvent constructor argument.
const fields: Array<keyof KeyboardEvent> = [
"key",
"code",
"location",
"ctrlKey",
"shiftKey",
"altKey",
"metaKey",
"repeat",
"isComposing",
"charCode",
"keyCode",
"which",
];
const eventData: Partial<KeyboardEvent> = Object.fromEntries(
fields.map((field) => [field, event[field]]),
);
window.parent.postMessage(
{
topic: "ftrack.application.document-keydown",
data: eventData,
},
targetOrigin || credentials.serverUrl,
);
}
/**
* On widget hashchange, forward information to parent application.
*/
function onHashChange() {
window.parent.postMessage(
{
topic: "ftrack.widget.hashchange",
data: {
hash: window.location.hash,
},
},
targetOrigin || credentials.serverUrl,
);
}
/** Options for {@link initialize} */
export interface InitializeOptions {
/** Specify to receive a callback when widget has loaded. */
onWidgetLoad?: (content: WidgetLoadMessage) => void;
/** Specify to receive a callback when widget has updated. */
onWidgetUpdate?: (content: WidgetUpdateMessage) => void;
}
/** Get current ftrack theme */
export function getActiveTheme() {
const parameters = new URLSearchParams(window.location.search);
return parameters.get("theme");
}
/**
* Initialize module with *options*.
*
* Should be called after `DOMContentLoaded` has fired.
*
* Will also fire custom events on the current `window`
* ftrackWidgetUpdate, ftrackWidgetLoad
*
* @param options Options to initialize with.
*/
export function initialize(options: InitializeOptions = {}) {
if (options.onWidgetLoad) {
onWidgetLoadCallback = options.onWidgetLoad;
}
if (options.onWidgetUpdate) {
onWidgetUpdateCallback = options.onWidgetUpdate;
}
// Listen to post messages.
window.addEventListener("message", onPostMessageReceived, false);
window.parent.postMessage({ topic: "ftrack.widget.ready" }, "*");
// Forward click and keydown events to parent.
document.addEventListener("click", onDocumentClick);
document.addEventListener("keydown", onDocumentKeyDown);
// Forward hashchange events to parent
window.addEventListener("hashchange", onHashChange);
}