forked from NASA-AMMOS/MMGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathessence.js
More file actions
518 lines (466 loc) · 18.1 KB
/
essence.js
File metadata and controls
518 lines (466 loc) · 18.1 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
Copyright 2019 NASA/JPL-Caltech
Tariq Soliman
Fred Calef III
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import $ from 'jquery'
import WebSocket from 'isomorphic-ws'
import M from 'materialize-css'
import F_ from './Basics/Formulae_/Formulae_'
import T_ from './Basics/Test_/Test_'
import L_ from './Basics/Layers_/Layers_'
import Viewer_ from './Basics/Viewer_/Viewer_'
import Map_ from './Basics/Map_/Map_'
import Globe_ from './Basics/Globe_/Globe_'
import UserInterface_ from './Basics/UserInterface_/UserInterface_'
import ToolController_ from './Basics/ToolController_/ToolController_'
import CursorInfo from './Ancillary/CursorInfo'
import ContextMenu from './Ancillary/ContextMenu'
import Coordinates from './Ancillary/Coordinates'
import Description from './Ancillary/Description'
import ScaleBar from './Ancillary/ScaleBar'
import ScaleBox from './Ancillary/ScaleBox'
import Compass from './Ancillary/Compass'
//import Swap from './Ancillary/Swap'
import QueryURL from './Ancillary/QueryURL'
import TimeControl from './Ancillary/TimeControl'
import calls from '../pre/calls'
import { mmgisAPI_, mmgisAPI } from './mmgisAPI/mmgisAPI'
import { makeMissionNotFoundDiv } from './LandingPage/LandingPage'
import { stylize } from './Ancillary/Stylize'
//Requiring UserInterface_ initializes itself
if (typeof window.mmgisglobal.groups === 'string') {
window.mmgisglobal.groups = window.mmgisglobal.groups.replace(
/"/g,
'"'
)
try {
window.mmgisglobal.groups = JSON.parse(window.mmgisglobal.groups)
} catch (err) {
console.warn('User groups failed to parse.')
}
}
if (typeof window.mmgisglobal.HOSTS === 'string') {
try {
window.mmgisglobal.HOSTS = JSON.parse(
window.mmgisglobal.HOSTS.replace(/"/gi, '"')
)
} catch (err) {
window.mmgisglobal.HOSTS = {}
}
} else {
window.mmgisglobal.HOSTS = {}
}
if (typeof window.mmgisglobal.PORT === 'string') {
window.mmgisglobal.PORT = parseInt(window.mmgisglobal.PORT || '8888', 10)
}
window.mmgisglobal.lastInteraction = Date.now()
$('body').on('mousemove', function () {
window.mmgisglobal.lastInteraction = Math.floor(Date.now() / 1000)
})
window.mmgisglobal.ctrlDown = false
window.mmgisglobal.shiftDown = false
let tabFocusAdded = false
// Check whether control button and shift is pressed
//17 is ctrl, 91, 93, and 224 are MAC metakeys
$(document).keydown(function (e) {
if (
e.which == '17' ||
e.which == '91' ||
e.which == '93' ||
e.which == '224'
)
window.mmgisglobal.ctrlDown = true
if (e.which == '16') window.mmgisglobal.shiftDown = true
})
$(document).keyup(function (e) {
if (
e.which == '17' ||
e.which == '91' ||
e.which == '93' ||
e.which == '224'
)
window.mmgisglobal.ctrlDown = false
if (e.which == '16') window.mmgisglobal.shiftDown = false
// On tab, add tab styles
if (e.which == '9' && !tabFocusAdded) {
document.styleSheets[0].insertRule(
'.toolButton:focus,#barBottom > i:focus,#topBarTitleName:focus,.mainInfo > div:focus,#mainDescription:focus,#SearchType:focus,#auto_search:focus,#loginoutButton:focus,#mapSplitInnerLeft:focus,#mapSplitInnerRight:focus,#globeSplitInnerLeft:focus,#globeSplitInnerRight:focus {box-shadow: inset 0px 0px 0px 2px var(--color-mmgis) !important;}',
1
)
tabFocusAdded = true
}
})
$(document.body).keydown(function (e) {
if (
ToolController_.activeTool == null &&
!$('#loginModal').length &&
UserInterface_.getPanelPercents().globe == 0 &&
e.shiftKey &&
e.keyCode === 84
) {
T_.toggle()
}
})
var essence = {
configData: null,
hasSwapped: false,
ws: null,
initialWebSocketRetryInterval: 60000, // 1 minute
webSocketRetryInterval: 60000, // Start with this time and double if disconnected
webSocketPingInterval: null,
connectWebSocket: function (path, initial) {
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
if (
essence.ws === undefined ||
essence.ws === null ||
(essence.ws && essence.ws.readyState === 3)
) {
essence.initWebSocket(path)
// If we're trying to start the WebSocket for the first time, we know we're not connected already
// so we don't need to retry to connect yet
if (!initial) {
clearInterval(essence.webSocketPingInterval)
essence.webSocketRetryInterval =
essence.webSocketRetryInterval * 2
essence.webSocketPingInterval = setInterval(
essence.connectWebSocket,
essence.webSocketRetryInterval,
path,
false
) // 10 seconds
}
}
},
initWebSocket: function (path) {
essence.ws = new WebSocket(path)
essence.ws.onerror = function (e) {
console.log(`Unable to connect to WebSocket at ${path}`)
M.Toast.dismissAll()
const asMinutes = essence.webSocketRetryInterval / 60000 || ''
M.toast({
html: `Not connected to WebSocket. Retrying in ${
asMinutes >= 1 ? parseInt(asMinutes) : asMinutes.toFixed(2)
} minute${asMinutes > 1 ? 's' : ''}...`,
displayLength: 10000,
classes: 'mmgisToast failure',
})
}
essence.ws.onopen = function () {
console.log('Websocket connection opened...')
UserInterface_.removeLayerUpdateButton()
M.Toast.dismissAll()
if (
essence.webSocketRetryInterval >
essence.initialWebSocketRetryInterval
) {
/*
M.toast({
html: 'Successfully connected to WebSocket',
displayLength: 1600,
classes: 'mmgisToast',
})
*/
essence.webSocketRetryInterval =
essence.initialWebSocketRetryInterval
clearInterval(essence.webSocketPingInterval)
essence.webSocketPingInterval = setInterval(
essence.connectWebSocket,
essence.webSocketRetryInterval,
path,
false
) // 1 minute
}
}
essence.ws.onmessage = function (data) {
if (data.data) {
try {
const parsed = JSON.parse(data.data)
const mission = essence.configData.msv.mission
if (
!parsed.body.mission ||
parsed.body.mission !== mission
) {
return
}
if ('info' in parsed) {
const { type, layerName } = parsed.info
if (
type === 'addLayer' ||
type === 'updateLayer' ||
type === 'removeLayer'
) {
calls.api(
'get',
{
mission,
},
async function (data) {
if (Array.isArray(layerName)) {
// If we're adding an array of new layers, add each layer to the queue individually
for (let layer in layerName) {
L_.addLayerQueue.push({
newLayerName: layerName[layer],
data,
type,
})
}
} else {
// Otherwise only a single new layer was added
L_.addLayerQueue.push({
newLayerName: layerName,
data,
type,
})
}
if (parsed.forceClientUpdate) {
// Force update the client side
await L_.updateQueueLayers()
} else {
UserInterface_.updateLayerUpdateButton(
'ADD_LAYER'
)
}
},
function (e) {
console.warn(
"Warning: Couldn't load: " +
mission +
' configuration.'
)
}
)
} else {
if (parsed.body && parsed.body.config) {
UserInterface_.updateLayerUpdateButton('RELOAD')
}
}
} else {
if (parsed.body && parsed.body.config) {
UserInterface_.updateLayerUpdateButton('RELOAD')
}
}
// Dispatch `websocketChange` event
let _event = new CustomEvent('websocketChange', {
detail: {
layer:
typeof layerName !== 'undefined'
? layerName
: null,
type: typeof type !== 'undefined' ? type : null,
data: parsed,
},
})
document.dispatchEvent(_event)
} catch (e) {
console.warn(
`Error parsing data from MMGIS websocket: ${e}`
)
}
}
}
essence.ws.onclose = function () {
console.log('Closed websocket connection...', new Date())
UserInterface_.updateLayerUpdateButton('DISCONNECTED')
}
},
init: async function (config, missionsList, swapping) {
//Save the config data
essence.configData = config
//Make sure url matches mission
var urlSplit = window.location.href.split('?')
var url = urlSplit[0]
if (
urlSplit.length == 1 ||
swapping ||
(urlSplit[1] && urlSplit[1].split('=')[0] === 'forcelanding') ||
(urlSplit[1] && urlSplit[1].split('=')[0] === '_preview')
) {
//then no parameters or old ones
url =
window.location.href.split('?')[0] +
'?mission=' +
config.msv.mission
window.history.replaceState('', '', url)
L_.url = window.location.href
}
if (swapping) {
essence.hasSwapped = true
L_.clear()
//UserInterface_.refresh();
}
//Try querying the urlSite
var urlOnLayers = null
if (!swapping) urlOnLayers = QueryURL.queryURL()
//Parse all the configData
await L_.init(essence.configData, missionsList, urlOnLayers)
if (swapping) {
ToolController_.clear()
Viewer_.clearImage()
//ToolController_.init( L_.tools );
}
//Update mission title
document.title =
(window.mmgisglobal.name || 'MMGIS') + ' - ' + L_.mission
//Set radii
F_.setRadius('major', L_.radius.major)
F_.setRadius('minor', L_.radius.minor)
//Initialize CursorInfo
if (!swapping) CursorInfo.init()
//Make the globe
if (!swapping) Globe_.init()
//Make the viewer
if (!swapping) Viewer_.init()
//Make the map
if (swapping) Map_.clear()
//Make the time control
TimeControl.init()
Map_.init(essence.fina)
//Now that the map is made
Coordinates.init()
ContextMenu.init()
if (!swapping) {
Description.init(L_.mission, L_.site, Map_, L_)
ScaleBar.init(ScaleBox)
Compass.init()
} else {
Coordinates.refresh()
ScaleBar.refresh()
Compass.refresh()
}
//Swap.make(this)
// Enable MMGIS backend websockets
if (
window.mmgisglobal.PORT &&
window.mmgisglobal.ENABLE_MMGIS_WEBSOCKETS === 'true'
) {
const port = parseInt(window.mmgisglobal.PORT || '8888', 10)
const protocol =
window.location.protocol.indexOf('https') !== -1 ? 'wss' : 'ws'
const path =
window.mmgisglobal.NODE_ENV === 'development'
? `${protocol}://localhost:${port}${
window.mmgisglobal.WEBSOCKET_ROOT_PATH ||
window.mmgisglobal.ROOT_PATH ||
''
}/`
: `${protocol}://${window.location.host}${
window.mmgisglobal.WEBSOCKET_ROOT_PATH ||
window.mmgisglobal.ROOT_PATH ||
''
}/`
essence.connectWebSocket(path, true)
essence.webSocketPingInterval = setInterval(
essence.connectWebSocket,
essence.webSocketRetryInterval,
path,
false
) // 10 seconds
}
},
swapMission(to) {
//console.log( to );
//Close all tools since they only update when reopened
ToolController_.closeActiveTool()
if (window.mmgisglobal.SERVER == 'node') {
calls.api(
'get',
{
mission: to,
},
function (data) {
essence.makeMission(data)
},
function (e) {
console.log(
"Warning: Couldn't load: " + to + ' configuration.'
)
makeMissionNotFoundDiv()
}
)
} else {
$.getJSON(
'Missions/' +
to +
'/' +
'config.json' +
'?nocache=' +
new Date().getTime(),
async function (data) {
await essence.makeMission(data)
}
).fail(function () {
console.log(
"Warning: Couldn't load: " +
'Missions/' +
to +
'/' +
'config.json'
)
makeMissionNotFoundDiv()
})
}
},
makeMission: async function (data) {
//Remove swap tool from data.tools
for (var i = data.tools.length - 1; i > 0; i--) {
if (data.tools[i].name === 'Swap') {
data.tools.splice(i, 1)
}
}
//Add swap to data.tools
if (essence.configData) {
for (var i in essence.configData.tools) {
if (essence.configData.tools[i].name === 'Swap') {
data.tools.push(essence.configData.tools[i])
break
}
}
if (
JSON.stringify(essence.configData.panels) !==
JSON.stringify(data.panels)
) {
data.panels = ['viewer', 'map', 'globe']
}
}
await essence.init(data, L_.missionsList, true)
},
fina: function () {
if (!essence.finalized) {
// Only finalize once
essence.finalized = true
if (essence.hasSwapped) Globe_.reset()
//FinalizeGlobe
Globe_.fina(Coordinates)
//Finalize Layers_
L_.fina(
Viewer_,
Map_,
Globe_,
UserInterface_,
Coordinates,
TimeControl
)
//Finalize the interface
UserInterface_.fina(L_, Viewer_, Map_, Globe_)
//Finalize the Viewer
Viewer_.fina(Map_)
//Finalize the TimeControl
TimeControl.fina()
// Finalize the mmgisAPI
mmgisAPI_.fina(Map_)
stylize()
}
},
}
window.mmgisglobal.setConfiguration = essence.init
export default essence