-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathModel.js
More file actions
388 lines (343 loc) · 12.3 KB
/
Model.js
File metadata and controls
388 lines (343 loc) · 12.3 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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
/* global JSROOT */
import {
sessionService, Observable, WebSocketClient, QueryRouter, Loader, Notification,
} from '/js/src/index.js';
import Layout from './layout/Layout.js';
import QCObject from './object/QCObject.js';
import LayoutService from './services/Layout.service.js';
import QCObjectService from './services/QCObject.service.js';
import ObjectViewModel from './pages/objectView/ObjectViewModel.js';
import { setBrowserTabTitle } from './common/utils.js';
import { buildQueryParametersString } from './common/buildQueryParametersString.js';
import AboutViewModel from './pages/aboutView/AboutViewModel.js';
import LayoutListModel from './pages/layoutListView/model/LayoutListModel.js';
import { RequestFields } from './common/RequestFields.enum.js';
import FilterModel from './common/filters/model/FilterModel.js';
import { IntegratedServices } from '../library/enums/Status/integratedServices.enum.js';
import NotificationRunStartModel from './common/notifications/model/NotificationRunStartModel.js';
/**
* Represents the application's state and actions as a class
*/
export default class Model extends Observable {
/**
* Initialize the whole model and sub-models associated
*/
constructor() {
super();
this.session = sessionService.get();
this.session.personid = parseInt(this.session.personid, 10); // Cast, sessionService has only strings
this.loader = new Loader(this);
this.loader.bubbleTo(this);
this.filterModel = new FilterModel(this);
this.filterModel.bubbleTo(this);
this.object = new QCObject(this);
this.object.bubbleTo(this);
this.objectViewModel = new ObjectViewModel(this);
this.objectViewModel.bubbleTo(this);
this.layoutListModel = new LayoutListModel(this);
this.layoutListModel.bubbleTo(this);
this.layout = new Layout(this);
this.layout.bubbleTo(this);
this.notification = new Notification(this);
this.notification.bubbleTo(this);
this.aboutViewModel = new AboutViewModel(this);
this.aboutViewModel.bubbleTo(this);
this.refreshInterval = 0; // Seconds
this.sidebar = true;
this.accountMenuEnabled = false;
this.page = null;
this._isImportVisible = false; // Visibility of modal allowing user to import a layout as JSON
this._isUpdateVisible = false; // Visibility of modal allowing user to edit JSON of an existing layout
// Setup router
this.router = new QueryRouter();
this.router.observe(this.handleLocationChange.bind(this));
// Setup keyboard dispatcher
window.addEventListener('keydown', this.handleKeyboardDown.bind(this));
// Setup WS connection
this.ws = new WebSocketClient();
this.ws.addListener('authed', this.handleWSAuthed.bind(this));
this.ws.addListener('close', this.handleWSClose.bind(this));
this.notificationRunStartModel = new NotificationRunStartModel(this);
this.notificationRunStartModel.bubbleTo(this);
this.initModel();
}
/**
* Initialize steps in a certain order based on
* mandatory information from server
* @returns {undefined}
*/
async initModel() {
this.services = {
object: new QCObjectService(this),
layout: new LayoutService(this),
};
this.loader.get('/api/checkUser');
// JSROOT.settings.ContextMenu = true;
JSROOT.settings.AutoStat = true;
JSROOT.settings.CanEnlarge = false;
JSROOT.settings.DragAndDrop = false;
JSROOT.settings.MoveResize = false; // Div 2
JSROOT.settings.ToolBar = false;
JSROOT.settings.ZoomWheel = false;
JSROOT.settings.ApproxTextSize = true;
JSROOT.settings.fFrameLineColor = 16;
JSROOT.settings.PreferSavedPoints = true;
JSROOT.settings.SmallPad = {
height: 10,
};
// For active run monitoring, the kafka service must be available.
// If we do not yet know the kafka service status, we should request it from the backend
if (!this.aboutViewModel.findService(IntegratedServices.KAFKA)) {
this.aboutViewModel.retrieveIndividualServiceStatus(IntegratedServices.KAFKA);
}
/*
* Init first page
*/
this.handleLocationChange();
}
/**
* Delegates sub-model actions depending on incoming keyboard event
* @param {Event} e - event for which to handle action
* @returns {undefined}
*/
handleKeyboardDown(e) {
// Console.log(`e.keyCode=${e.keyCode}, e.metaKey=${e.metaKey}, e.ctrlKey=${e.ctrlKey}, e.altKey=${e.altKey}`);
const { key } = e;
// Delete key + layout page + object select => delete this object
if (key === 'Delete' &&
this.router.params.page === 'layoutShow' &&
this.layout.editEnabled &&
this.layout.editingTabObject) {
this.layout.deleteTabObject(this.layout.editingTabObject);
} else if (key === 'Escape' && this.isImportVisible) {
this.layout.resetImport();
}
if (
this.router.params.page === 'objectTree' &&
(
key === 'ArrowUp' ||
key === 'ArrowDown' ||
key === 'Enter' ||
key === 'ArrowLeft' ||
key === 'ArrowRight'
)
) {
e.preventDefault(); // Prevent scrolling the page
const searchActive = Boolean(this.object.searchInput?.trim());
if (searchActive) {
// Search navigation
this.object.handleKeyboardNavigationSearchResults(key);
} else {
// Tree navigation
this.object.tree.handleKeyboardNavigation(key, (selectedObject) => this.object.select(selectedObject));
}
}
}
/**
* Handle authed event from WS when connection is ready to be used
* @returns {undefined}
*/
handleWSAuthed() {
// Subscribe to all notifications from server (information service)
this.ws.setFilter(() => true);
}
/**
* Handle close event from WS when connection has been lost (server restart, etc.)
* @returns {undefined}
*/
handleWSClose() {
const self = this;
setTimeout(() => {
self.notification.show('Connection to server has been lost, please reload the page.', 'danger', Infinity);
}, 3000);
}
/**
* Delegates sub-model actions depending new location of the page
* @returns {undefined}
*/
async handleLocationChange() {
this.object.objects = {}; // Remove any in-memory loaded objects
this._clearAllIntervals();
await this.filterModel.filterService.initFilterService();
await this.filterModel.setFilterFromURL();
this.filterModel.setFilterToURL();
await this.aboutViewModel.retrieveIndividualServiceStatus(IntegratedServices.BOOKKEEPING);
this.services.layout.getLayoutsByUserId(this.session.personid, RequestFields.LAYOUT_CARD);
const { params } = this.router;
switch (params.page) {
case 'layoutList':
this.clearURL('layoutList');
this.page = 'layoutList';
setBrowserTabTitle('QCG-Layouts');
this.services.layout.getLayouts(RequestFields.LAYOUT_CARD);
break;
case 'layoutShow':
setBrowserTabTitle('QCG-LayoutShow');
if (!params.layoutId) {
const { definition, pdpBeamType, detector, runType, runNumber } = params;
if (!definition) {
this.notification.show('layoutId in URL was missing. Redirecting to layouts page', 'warning', 3000);
this.router.go('?page=layoutList', true);
return;
} else {
let pdpTemp = undefined;
delete params.pdpBeamType;
if (definition === 'PHYSICS') {
pdpTemp = pdpBeamType;
}
const layout = await this.services.layout.getLayoutByQuery(definition, pdpTemp);
if (!layout) {
this.notification.show(`Layout with definition ${definition} could not be found`, 'warning', 3000);
this.router.go('?page=layoutList', true);
return;
}
const paramsToAdd = { layoutId: layout.id };
delete params.definition;
if (detector) {
let tab = detector;
if (runType) {
tab += `_${runType.toLocaleLowerCase()}`;
}
paramsToAdd.tab = tab;
delete params.detector;
delete params.runType;
}
if (runNumber !== null && runNumber !== undefined) {
paramsToAdd.RunNumber = runNumber;
delete params.runNumber;
}
this.router.go(buildQueryParametersString(params, paramsToAdd), true);
return;
}
}
this.layout.loadItem(this.router.params.layoutId, params?.tab ?? '')
.then(() => {
this.page = 'layoutShow';
if (params.edit) {
this.layout.edit();
// Replace silently and immediately URL to remove 'edit' parameter after a layout creation
this.router.go(`?page=layoutShow&layoutId=${this.router.params.layoutId}`, true, true);
}
this.filterModel.restartRunsModeIntervals(this.layout);
this.notify();
}).catch(() => true); // Error is handled inside loadItem
break;
case 'objectTree':
this.page = 'objectTree';
setBrowserTabTitle('QCG-Tree');
this.object.loadList();
// Data is already loaded at beginning
if (this.object.selected) {
this.object.loadObjectByName(this.object.selected.name);
}
this.filterModel.restartRunsModeIntervals(this.object);
this.notify();
break;
case 'objectView': {
this.page = 'objectView';
this.sidebar = false;
setBrowserTabTitle('QCG-View');
const { params } = this.router;
this.objectViewModel.init(params);
this.filterModel.restartRunsModeIntervals(this.objectViewModel);
this.notify();
break;
}
case 'about':
this.clearURL('about');
this.page = 'about';
setBrowserTabTitle('QCG-About');
this.aboutViewModel.retrieveAllServicesStatus();
this.notify();
break;
default:
// Default route, replace the current one not handled
this.router.go('?page=layoutList', true);
break;
}
}
/**
* Clear URL parameters and redirect to a certain page
* @param {string} pageName - name of the page to be redirected to
* @returns {undefined}
*/
clearURL(pageName) {
this.router.go(`?page=${pageName}`, true, true);
}
/**
* Show or hide sidebar
* @returns {undefined}
*/
toggleSidebar() {
this.sidebar = !this.sidebar;
this.notify();
}
/**
* Toggle account menu dropdown
* @returns {undefined}
*/
toggleAccountMenu() {
this.accountMenuEnabled = !this.accountMenuEnabled;
this.notify();
}
/**
* Clears all active intervals in the application
* @returns {void}
*/
_clearAllIntervals() {
// Clear layout tab interval
if (this.layout?.tabInterval) {
clearInterval(this.layout.tabInterval);
}
// Clear filter model runs mode interval
this.filterModel.clearRunsModeInterval();
}
/**
* Getters / Setters
*/
/**
* Returns the visibility of the import layout modal
* @returns {boolean} - whether import modal is visible
*/
get isImportVisible() {
return this._isImportVisible;
}
/**
* Sets the visibility of the import layout modal
* @param {boolean} value - value to be set for modal visibility
* @returns {undefined}
*/
set isImportVisible(value) {
this._isImportVisible = value ? true : false;
this.notify();
}
/**
* Returns the visibility of the edit JSON layout modal
* @returns {boolean} - whether import modal is visible
*/
get isUpdateVisible() {
return this._isUpdateVisible;
}
/**
* Sets the visibility of the edit JSON layout modal
* @param {boolean} value - value to be set for modal visibility
* @returns {undefined}
*/
set isUpdateVisible(value) {
this._isUpdateVisible = value ? true : false;
this.notify();
}
}