Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
230 lines (208 loc) · 7.34 KB
/
Copy pathindex.js
File metadata and controls
230 lines (208 loc) · 7.34 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Boot module: folds page configuration into TogetherJS, then starts or stops
* a session. Importing core/togetherjs.js first guarantees the TogetherJS
* object exists before any other module's top-level code runs. */
import TogetherJS from "./core/togetherjs.js";
import session from "./core/session.js";
import * as registry from "./core/registry.js";
import storage from "./core/storage.js";
import util from "./core/util.js";
/* Feature modules. Order matters: session.js must finish evaluating before
these do, because they attach handlers to the session object at load time.
RequireJS expressed this as session.js's `features` array; here it is just
import order. Each of these registers itself with core/registry.js so the
modules on an import cycle can find one another at call time. */
import "./ui/ui.js";
import "./core/peers.js";
import "./core/who.js";
import "./core/startup.js";
import "./ui/windowing.js";
import "./ui/chat.js";
import "./ui/visibility.js";
import "./sync/cursor.js";
import "./sync/forms.js";
import "./sync/videos.js";
import "./sync/youtube.js";
import "./rtc/index.js";
/* Fold TogetherJSConfig / TogetherJSConfig_* page globals into the config. */
function applyPageConfiguration() {
if (window.TowTruckConfig) {
console.warn("TowTruckConfig is deprecated; please use TogetherJSConfig");
if (window.TogetherJSConfig) {
console.warn("Ignoring TowTruckConfig in favor of TogetherJSConfig");
} else {
window.TogetherJSConfig = window.TowTruckConfig;
}
}
if (window.TogetherJSConfig && !window.TogetherJSConfig.loaded) {
TogetherJS.config(window.TogetherJSConfig);
window.TogetherJSConfig.loaded = true;
}
// TogetherJSConfig_on_* are folded into the "on" configuration value.
var globalOns = {};
var attr;
var attrName;
for (attr in window) {
if (attr.indexOf("TogetherJSConfig_on_") === 0) {
attrName = attr.substr("TogetherJSConfig_on_".length);
globalOns[attrName] = window[attr];
} else if (attr.indexOf("TogetherJSConfig_") === 0) {
attrName = attr.substr("TogetherJSConfig_".length);
TogetherJS.config(attrName, window[attr]);
} else if (attr.indexOf("TowTruckConfig_on_") === 0) {
attrName = attr.substr("TowTruckConfig_on_".length);
console.warn("TowTruckConfig_* is deprecated, please rename", attr, "to TogetherJSConfig_on_" + attrName);
globalOns[attrName] = window[attr];
} else if (attr.indexOf("TowTruckConfig_") === 0) {
attrName = attr.substr("TowTruckConfig_".length);
console.warn("TowTruckConfig_* is deprecated, please rename", attr, "to TogetherJSConfig_" + attrName);
TogetherJS.config(attrName, window[attr]);
}
}
var ons = TogetherJS.config.get("on");
for (attr in globalOns) {
if (Object.prototype.hasOwnProperty.call(globalOns, attr)) {
ons[attr] = globalOns[attr];
}
}
TogetherJS.config("on", ons);
for (attr in ons) {
TogetherJS.on(attr, ons[attr]);
}
var hubOns = TogetherJS.config.get("hub_on");
if (hubOns) {
for (attr in hubOns) {
if (Object.prototype.hasOwnProperty.call(hubOns, attr)) {
TogetherJS.hub.on(attr, hubOns[attr]);
}
}
}
}
/* Work out which element, if any, started the session, so the dock can fly out
of it. Accepts an event, an element, or a jQuery-ish array. */
function startTargetFrom(event) {
try {
if (event && typeof event == "object") {
if (event.target && typeof event) {
return event.target;
}
if (event.nodeType == 1) {
return event;
}
if (event[0] && event[0].nodeType == 1) {
return event[0];
}
}
} catch (e) {
console.warn("Error determining starting button:", e);
}
return null;
}
TogetherJS._boot = function (event) {
if (TogetherJS.running) {
session.close();
return;
}
TogetherJS.startup.button = startTargetFrom(event);
applyPageConfiguration();
if (!TogetherJS.startup.reason) {
// A call to TogetherJS() from a button must have started TogetherJS
TogetherJS.startup.reason = "started";
}
TogetherJS.addStyle();
TogetherJS._loaded = true;
// initShareId() distinguishes a fresh launch from a continued session by
// this flag; without it, it throws and the whole start chain dies silently.
TogetherJS.startup._launch = true;
session.start();
};
/* Escape hatch for tests and debugging. The RequireJS build exposed the whole
module graph through TogetherJS.require(); this is the deliberate, much
smaller replacement. Not part of the supported API. */
TogetherJS._internals = { session: session, registry: registry, storage: storage, util: util };
TogetherJS.reinitialize = function () {
if (TogetherJS.running) {
session.emit("reinitialize");
}
};
TogetherJS.refreshUserData = function () {
if (TogetherJS.running) {
session.emit("refresh-user-data");
}
};
TogetherJS.send = function (msg) {
if (!TogetherJS.running) {
throw new Error("You cannot use TogetherJS.send() when TogetherJS is not running");
}
session.appSend(msg);
};
/* True for whoever started the session, false for everyone who joined it. */
TogetherJS.isCreator = function () {
return TogetherJS.running && !session.isClient;
};
TogetherJS.shareUrl = function () {
return TogetherJS.running ? session.shareUrl() : null;
};
// It's nice to replace this early, before the load event fires, so we conflict
// as little as possible with the app we are embedded in:
var hash = location.hash.replace(/^#/, "");
var m = /&?togetherjs=([^&]*)/.exec(hash);
if (m) {
TogetherJS.startup._joinShareId = m[1];
TogetherJS.startup.reason = "joined";
location.hash = hash.substr(0, m.index) + hash.substr(m.index + m[0].length);
}
function onload() {
if (TogetherJS.startup._joinShareId) {
TogetherJS();
return;
}
if (window._TogetherJSBookmarklet) {
delete window._TogetherJSBookmarklet;
TogetherJS();
return;
}
var prefix = TogetherJS.config.get("storagePrefix");
var value = sessionStorage.getItem(prefix + "-session.status");
if (value) {
value = JSON.parse(value);
if (value && value.running) {
TogetherJS.startup.continued = true;
TogetherJS.startup.reason = value.startupReason;
TogetherJS();
}
} else if (
window.TogetherJSConfig_autoStart ||
(window.TogetherJSConfig && window.TogetherJSConfig.autoStart)
) {
TogetherJS.startup.reason = "joined";
TogetherJS();
}
}
function conditionalActivate() {
if (window.TogetherJSConfig_noAutoStart) {
return;
}
// A page can define this function to defer TogetherJS from starting
var callToStart = window.TogetherJSConfig_callToStart;
if (!callToStart && window.TowTruckConfig_callToStart) {
callToStart = window.TowTruckConfig_callToStart;
console.warn("Please rename TowTruckConfig_callToStart to TogetherJSConfig_callToStart");
}
if (window.TogetherJSConfig && window.TogetherJSConfig.callToStart) {
callToStart = window.TogetherJSConfig.callToStart;
}
if (callToStart) {
callToStart(onload);
} else {
onload();
}
}
conditionalActivate();
if (window.TogetherJSConfig_enableShortcut) {
TogetherJS.listenForShortcut();
}
export default TogetherJS;
export { TogetherJS };