/
Application.js
307 lines (284 loc) · 9.61 KB
/
Application.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
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
define(
[
'knockout',
'services/http',
'services/AuthAPI',
'services/role',
'appConfig',
'lscache',
'atlas-state',
'jquery',
'services/Execution',
'services/SourceAPI',
'services/EventBus',
'services/ConceptSet',
'utils/CommonUtils',
'utils/BemHelper',
'const',
'databindings',
'less!app.less',
],
(
ko,
httpService,
authApi,
roleService,
config,
lscache,
sharedState,
$, // TODO: get rid of jquery
executionService,
sourceApi,
EventBus,
conceptSetService,
commonUtils,
BemHelper,
constants,
) => {
return class Application {
constructor(router) {
// establish base priorities for daimons
this.evidencePriority = 0;
this.vocabularyPriority = 0;
this.densityPriority = 0;
this.router = router;
this.EventBus = EventBus;
const bemHelper = new BemHelper('app');
this.classes = bemHelper.run.bind(bemHelper);
this.currentConceptSet = sharedState.ConceptSet.current;
this.currentConceptSetSource = sharedState.ConceptSet.source;
this.currentCohortDefinition = sharedState.CohortDefinition.current;
this.hasUnsavedChanges = ko.pureComputed(() => {
return (sharedState.CohortDefinition.dirtyFlag().isDirty()
|| sharedState.ConceptSet.dirtyFlag().isDirty()
|| sharedState.IRAnalysis.dirtyFlag().isDirty()
|| sharedState.CohortPathways.dirtyFlag().isDirty()
|| sharedState.estimationAnalysis.dirtyFlag().isDirty()
|| sharedState.predictionAnalysis.dirtyFlag().isDirty()
|| sharedState.CohortCharacterization.dirtyFlag().isDirty()
);
});
this.initializationComplete = ko.pureComputed(() => {
return sharedState.appInitializationStatus() != constants.applicationStatuses.initializing;
});
this.appInitializationStatus = sharedState.appInitializationStatus;
this.noSourcesAvailable = ko.pureComputed(() => {
return this.appInitializationStatus() === constants.applicationStatuses.noSourcesAvailable && this.router.currentView() !== 'ohdsi-configuration';
});
this.appInitializationErrorMessage = ko.computed(() => {
if (this.noSourcesAvailable()) {
return 'the current webapi has no sources defined.<br/>please add one or more on <a href="#/configure">configuration</a> page.'
} else if (this.appInitializationStatus() !== constants.applicationStatuses.noSourcesAvailable) {
return 'unable to connect to an instance of the webapi.<br/>please contact your administrator to resolve this issue.'
}
});
this.pageTitle = ko.pureComputed(() => {
let pageTitle = "ATLAS";
switch (this.router.currentView()) {
case 'loading':
pageTitle = `${pageTitle}: Loading`;
break;
default:
pageTitle = `${pageTitle}: ${this.router.activeRoute().title}`;
break;
}
if (this.hasUnsavedChanges()) {
pageTitle = `*${pageTitle} (unsaved)`;
}
return pageTitle;
})
}
/**
* Performs initial setup
* @returns Promise
*/
bootstrap() {
const promise = new Promise(async (resolve, reject) => {
$.support.cors = true;
sharedState.appInitializationStatus(ko.observable(constants.applicationStatuses.initializing));
config.api.isExecutionEngineAvailable = ko.observable(false);
ko.applyBindings({
// provide to a view access to both model and the router via this.router
...this,
}, document.getElementsByTagName('html')[0]);
httpService.setUnauthorizedHandler(() => authApi.resetAuthParams());
httpService.setUserTokenGetter(() => authApi.getAuthorizationHeader());
if (config.userAuthenticationEnabled) {
try {
await authApi.loadUserInfo();
} catch (e) {
reject(e.message);
}
}
authApi.isAuthenticated.subscribe(executionService.checkExecutionEngineStatus);
this.attachGlobalEventListeners();
await executionService.checkExecutionEngineStatus(authApi.isAuthenticated());
resolve();
});
return promise;
}
/**
* Fetches all the data required for app's functioning
* @returns Promise
*/
synchronize() {
const promise = Promise.all([
this.initServiceInformation(),
roleService.updateRoles(),
]);
promise.then(() => {
this.router.run();
});
return promise;
}
// service methods
attachGlobalEventListeners() {
const self = this;
// handle select all
$(document)
.on('click', 'th i.fa.fa-shopping-cart', function () {
if (self.currentConceptSet() == undefined) {
var newConceptSet = {
name: ko.observable("New Concept Set"),
id: 0
}
self.currentConceptSet(newConceptSet);
}
var table = $(this)
.closest('.dataTable')
.DataTable();
var concepts = table.rows({
search: 'applied'
})
.data();
var selectedConcepts = sharedState.selectedConcepts();
for (var i = 0; i < concepts.length; i++) {
var concept = concepts[i];
if (sharedState.selectedConceptsIndex[concept.CONCEPT_ID]) {
// ignore if already selected
} else {
var conceptSetItem = commonUtils.createConceptSetItem(concept);
sharedState.selectedConceptsIndex[concept.CONCEPT_ID] = 1;
selectedConcepts.push(conceptSetItem)
}
}
sharedState.selectedConcepts(selectedConcepts);
for (var i = 0; i < table.rows()[0].length; i++) {
table.cell(i,0).data('<i class="fa fa-shopping-cart"></i>');
}
});
// handling concept set selections
$(document)
.on('click', 'td i.fa.fa-shopping-cart, .asset-heading i.fa.fa-shopping-cart', function () {
if (self.currentConceptSet() == undefined) {
var newConceptSet = {
name: ko.observable("New Concept Set"),
id: 0
}
self.currentConceptSet({
name: ko.observable('New Concept Set'),
id: 0
});
self.currentConceptSetSource('repository');
}
$(this)
.toggleClass('selected');
var concept = ko.contextFor(this)
.$data;
if ($(this)
.hasClass('selected')) {
var conceptSetItem = commonUtils.createConceptSetItem(concept);
sharedState.selectedConceptsIndex[concept.CONCEPT_ID] = 1;
sharedState.selectedConcepts.push(conceptSetItem);
conceptSetService.setConceptSetExpressionExportItems();
} else {
delete sharedState.selectedConceptsIndex[concept.CONCEPT_ID];
sharedState.selectedConcepts.remove(function (i) {
return i.concept.CONCEPT_ID === concept.CONCEPT_ID;
});
}
// If we are updating a concept set that is part of a cohort definition
// then we need to notify any dependent observables about this change in the concept set
if (self.currentCohortDefinition() && self.currentConceptSetSource() === "cohort") {
var conceptSet = self.currentCohortDefinition()
.expression()
.ConceptSets()
.find(function (item) {
return item.id === self.currentConceptSet().id;
});
if (!$(this).hasClass("selected")) {
conceptSet.expression.items.remove(function (i) {
return i.concept.CONCEPT_ID === concept.CONCEPT_ID;
});
}
conceptSet.expression.items.valueHasMutated();
conceptSetService.resolveConceptSetExpressionSimple(conceptSet.expression);
}
});
// concept set selector handling
$(document)
.on('click', '.conceptSetTable i.fa.fa-shopping-cart', function () {
$(this)
.toggleClass('selected');
var conceptSetItem = ko.contextFor(this)
.$data;
delete sharedState.selectedConceptsIndex[conceptSetItem.concept.CONCEPT_ID];
sharedState.selectedConcepts.remove(function (i) {
return i.concept.CONCEPT_ID == conceptSetItem.concept.CONCEPT_ID;
});
self.currentCohortDefinition() && self.currentCohortDefinition().expression().ConceptSets.valueHasMutated();
conceptSetService.resolveConceptSetExpression();
});
$(window)
.bind('beforeunload', function () {
if (self.hasUnsavedChanges())
return "Changes will be lost if you do not save.";
});
}
/**
* Initialize all service information asynchronously
* @returns Promise
*/
initServiceInformation() {
console.info('Initializing service information');
return new Promise((resolve, reject) => {
const serviceCacheKey = 'ATLAS|' + config.api.url;
const cachedService = lscache.get(serviceCacheKey);
if (cachedService && cachedService.sources) {
console.info('cached service');
config.api = cachedService;
sourceApi.setSharedStateSources(cachedService.sources);
resolve();
} else {
sharedState.sources([]);
if (config.userAuthenticationEnabled && !authApi.isAuthenticated()) {
this.authSubscription = authApi.isAuthenticated.subscribe(async (isAuthed) => {
if (isAuthed) {
sharedState.appInitializationStatus(await sourceApi.initSourcesConfig());
this.authSubscription.dispose();
console.info('Re-initialized service information');
}
});
sharedState.appInitializationStatus(constants.applicationStatuses.running);
resolve();
return;
} else {
sourceApi.initSourcesConfig()
.then(function (appStatus) {
sharedState.appInitializationStatus(appStatus);
console.info('Init sources from server');
resolve();
});
}
}
});
}
checkOAuthError() {
let hash = window.location.hash;
if (hash && hash.includes("oauth_error_email")) {
alert("Empty email received from oauth server. Check whether it has public access");
}
}
}
}
)