-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathheader.js
More file actions
156 lines (145 loc) · 5.71 KB
/
header.js
File metadata and controls
156 lines (145 loc) · 5.71 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
/**
* @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.
*/
import { h, iconPerson, getBrowserNotificationPermission, areBrowserNotificationsGranted,
requestBrowserNotificationPermissions, BrowserNotificationPermission } from '/js/src/index.js';
import { spinner } from './spinner.js';
import layoutViewHeader from '../layout/view/header.js';
import objectTreeHeader from '../object/objectTreeHeader.js';
import aboutViewHeader from '../pages/aboutView/components/aboutViewHeader.js';
import LayoutListHeader from '../pages/layoutListView/components/LayoutListHeader.js';
import { objectViewHeader } from '../pages/objectView/components/header.js';
import { filtersPanel } from './filters/filterViews.js';
/**
* Shows header of the application, split with 3 parts:
* - app part on left side
* - page title on center
* - page actions on right side
* @param {Model} model - root model of the application
* @returns {vnode} - header element
*/
export default (model) => {
const specific = headerSpecific(model) || {};
const { centerCol, rightCol, subRow } = specific;
const id = `qcg-header-${model.page}`;
return h('.flex-col', [
h('.flex-row.p2.items-center', { id, key: id }, [
commonHeader(model),
centerCol || h('.flex-grow'),
rightCol || h('.w-25'),
]),
subRow && h('.p2', [subRow]),
filterSpecific(model),
]);
};
/**
* Shows the page specific header (center and right side)
* @param {Model} model - root model of the application
* @returns {{centerCol: vnode, rightCol: vnode} | null} center column and right column
*/
const headerSpecific = (model) => {
const { filterModel, layout, object, page } = model;
switch (page) {
case 'layoutList': return LayoutListHeader();
case 'layoutShow': return layoutViewHeader(layout, filterModel);
case 'objectTree': return objectTreeHeader(object, filterModel);
case 'objectView': return objectViewHeader(model);
case 'about': return aboutViewHeader();
default: return null;
}
};
/**
* Shows the page specific header (center and right side)
* @param {Model} model - root model of the application
* @returns {vnode} - virtual node element
*/
const filterSpecific = (model) => {
const { page, filterModel, layout } = model;
if (page === 'layoutShow' && layout.editEnabled) {
return null;
}
const viewModel = filterModel.getPageTargetModel();
if (!viewModel) {
return null;
}
return filtersPanel(filterModel, viewModel);
};
/**
* Shows app header, common to all pages (profile button + app title)
* @param {Model} model - root model of the application
* @returns {vnode} - virtual node element
*/
const commonHeader = (model) => h('.flex-row.items-center.w-25', [
loginButton(model),
' ',
h('span.f4.gray', {
id: 'qcgTitle',
style: 'cursor: pointer',
onclick: () => model.router.go('?page=layoutList'),
title: 'Go to layouts list page',
}, 'Quality Control'),
model.loader.active && h('span.f4.mh1.gray', spinner()),
]);
/**
* Shows profile button to logout or check who is the current owner of session
* @param {Model} model - root model of the application
* @returns {vnode} - virtual node element
*/
const loginButton = (model) =>
h('.dropdown', {
title: 'Login', class: model.accountMenuEnabled ? 'dropdown-open' : '',
}, [
h('button.btn', { onclick: () => model.toggleAccountMenu() }, iconPerson()),
h('.dropdown-menu', [
h('p.m3.mv2.text-ellipsis', `Welcome ${model.session.name}`),
model.session.personid === 0 // Anonymous user has id 0
? h('p.m3.gray-darker', 'This instance of the application does not require authentication.')
: h('a.menu-item', { onclick: () => alert('Not implemented') }, 'Logout'),
notifyOnRunStartSettingComponent(model.notificationRunStartModel),
]),
]);
/**
* Builds the toggle and its functionality of the "notify on run start" setting
* @param {NotificationRunStartModel} notificationRunStartModel - the notification run start model
* @returns {vnode} - virtual node element
*/
const notifyOnRunStartSettingComponent = (notificationRunStartModel) => {
const browserNotificationPermission = getBrowserNotificationPermission();
const notificationsAvailable = browserNotificationPermission
&& browserNotificationPermission !== BrowserNotificationPermission.DENIED;
const runStartNotificationEnabled = notificationRunStartModel.getBrowserNotificationSetting();
return h(
'label.flex-row.g1.items-center.form-check-label',
{ style: `cursor: ${notificationsAvailable ? 'pointer' : 'not-allowed'};` },
[
h('.switch', [
h('input', {
onchange: async (event) => {
let permissionGranted = false;
if (event.target.checked) {
await requestBrowserNotificationPermissions();
permissionGranted = areBrowserNotificationsGranted();
}
notificationRunStartModel.setBrowserNotificationSetting(permissionGranted);
},
type: 'checkbox',
checked: runStartNotificationEnabled,
}),
h(`span.slider.round.bg-${runStartNotificationEnabled ? 'primary' : 'gray'}`, {
style: `cursor: ${notificationsAvailable ? 'pointer' : 'not-allowed'};`,
}),
]),
'Notify on run start',
],
);
};