-
Notifications
You must be signed in to change notification settings - Fork 3
/
greenlight.js
126 lines (105 loc) · 3.52 KB
/
greenlight.js
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
let makeMain;
let Library;
let boards;
let minibrowser;
let text;
let widgets;
let apiKey;
let path;
function setPath(string) {
path = string;
window._production = path || ".";
}
function loadGreenlight(cleanup, options, moreOptions) {
let version = path;
let p0 = new Promise((resolve, _reject) => {
if (document.querySelector("#croquetSrc")) {
return resolve();
}
let script = document.createElement("script");
script.id = "croquetSrc";
script.src = `${version}/croquet/croquet.min.js`;
script.type = "text/javascript";
document.body.appendChild(script);
script.onload = resolve;
script.onerror = () => {throw new Error("croquet could not be loaded");};
return script;
});
let p1 = new Promise((resolve, _reject) => {
if (document.querySelector("#pitchStyle")) {
return resolve();
}
let link = document.createElement("link");
link.id = "pitchStyle";
link.href = `${version}/src/pitch.css`;
link.rel = "stylesheet";
document.head.appendChild(link);
link.onload = resolve;
link.onerror = () => {throw new Error("croquet could not be loaded");};
return link;
});
return Promise.all([p0, p1]).then(() => {
let p2 = import(`${version}/croquet/croquet-virtual-dom.js`).then((mod) => {
makeMain = mod.makeMain;
Library = mod.Library;
});
let p3 = import(`${version}/src/p.js`).then((mod) => {
boards = mod.boards;
minibrowser = mod.minibrowser;
text = mod.text;
});
let p4 = import(`${version}/croquet/widgets.js`).then((mod) => {
widgets = mod.widgets;
});
let p5 = import(`${version}/apiKey.js`).then((mod) => {
apiKey = mod.default;
});
return Promise.all([p2, p3, p4, p5]).then(() => {
cleanup();
join(options, moreOptions);
}).catch((err) => {
console.error(err);
throw new Error("croquet could not be loaded");
});
});
}
function join(options, moreOptions) {
let library = new Library();
library.addLibrary("boards", boards);
library.addLibrary("widgets", widgets);
library.addLibrary("minibrowser", minibrowser);
library.addLibrary("text", text);
let cSessionName = "boards.p-" + options.sessionName;
let location = window.location;
let origin = location.origin;
let query = new URL(window.location).searchParams;
query.delete("r");
query.delete("_");
if (moreOptions) {
for (let k in moreOptions) {
query.append(k, moreOptions[k]);
}
}
let str = query.toString();
let ampersand = str.length > 0 ? "&" : "";
let newLocation = `${origin}${location.pathname}?r=${options.sessionName}${ampersand}${str}`;
window.history.pushState("launch", "Pitch", newLocation);
window.onpopstate = (_event) => {
window.location.assign(location);
};
let initials = options.initials;
if (/[^_a-z0-9]/i.test(initials)) {
let d = () => Math.floor(Math.random() * 10);
initials = `${d()}${d()}`;
}
makeMain("boards.p", {
autoSleep: false,
viewIdDebugSuffix: initials,
tps: 2,
apiKey,
appId: "io.croquet.vdom.greenlight",
eventRateLimit: 60,
}, library, cSessionName, `${path}/greenlight.svg`, true)();
}
window._setPath = setPath;
window._loadGreenlight = loadGreenlight;