-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathCommonUtils.js
More file actions
263 lines (234 loc) · 7.14 KB
/
Copy pathCommonUtils.js
File metadata and controls
263 lines (234 loc) · 7.14 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
define([
'knockout',
'atlas-state',
'pages/Page',
'urijs',
],
(
ko,
sharedState,
Page,
URI,
) => {
const build = function (name, viewModelClass, template) {
const component = {
viewModel: {
createViewModel: (params, info) => {
const vm = new viewModelClass(params, info);
if (vm instanceof Page) {
vm.onPageCreated();
}
return vm;
},
},
template,
};
viewModelClass.prototype.componentName = name;
ko.components.register(name, component);
return component;
};
const routeTo = function (path) {
document.location = '#' + path;
};
// service methods for model
function hasRelationship(concept, relationships) {
for (var r = 0; r < concept.RELATIONSHIPS.length; r++) {
for (var i = 0; i < relationships.length; i++) {
if (concept.RELATIONSHIPS[r].RELATIONSHIP_NAME == relationships[i].name) {
if (concept.RELATIONSHIPS[r].RELATIONSHIP_DISTANCE >= relationships[i].range[0] && concept.RELATIONSHIPS[r].RELATIONSHIP_DISTANCE <= relationships[i].range[1]) {
if (relationships[i].vocabulary) {
for (var v = 0; v < relationships[i].vocabulary.length; v++) {
if (relationships[i].vocabulary[v] == concept.VOCABULARY_ID) {
return true;
}
}
} else {
return true;
}
}
}
}
}
return false;
}
function getConceptLinkClass(data) {
var switchContext;
if (data.STANDARD_CONCEPT === undefined) {
switchContext = data.concept.STANDARD_CONCEPT;
} else {
switchContext = data.STANDARD_CONCEPT;
}
switch (switchContext) {
case 'N':
return "non-standard";
case 'C':
return "classification";
case 'S':
return 'standard';
}
}
function contextSensitiveLinkColor(row, data) {
$('a', row)
.addClass(getConceptLinkClass(data));
}
function hasCDM(source) {
return source.daimons.find(daimon => daimon.daimonType == 'CDM') !== undefined;
}
function hasResults(source) {
return source.daimons.find(daimon => daimon.daimonType == 'Results') !== undefined;
}
function renderConceptSetItemSelector(s, p, d) {
let css = '';
let tag = 'i';
if (sharedState.selectedConceptsIndex[d.concept.CONCEPT_ID] == 1) {
css = ' selected';
}
if (!this.canEditCurrentConceptSet()) {
css += ' readonly';
tag = 'span'; // to avoid call to 'click' event handler which is bound to <i> tag
}
return '<' + tag + ' class="fa fa-shopping-cart' + css + '"></' + tag + '>';
}
function renderLink(s, p, d) {
var valid = d.INVALID_REASON_CAPTION == 'Invalid' ? 'invalid' : '';
var linkClass = getConceptLinkClass(d);
return p === 'display'
? '<a class="' + valid + ' ' + linkClass + '" href=\"#/concept/' + d.CONCEPT_ID + '\">' + d.CONCEPT_NAME + '</a>'
: d.CONCEPT_NAME;
}
function renderBoundLink(s, p, d) {
return renderLink(s, p, d.concept);
}
const renderConceptSelector = function (s, p, d) {
var css = '';
var icon = 'fa-shopping-cart';
if (sharedState.selectedConceptsIndex[d.CONCEPT_ID] == 1) {
css = ' selected';
}
return '<i class="fa ' + icon + ' ' + css + '"></i>';
}
const renderHierarchyLink = function (d) {
var valid = d.INVALID_REASON_CAPTION == 'Invalid' || d.STANDARD_CONCEPT != 'S' ? 'invalid' : '';
return '<a class="' + valid + '" href=\"#/concept/' + d.CONCEPT_ID + '\">' + d.CONCEPT_NAME + '</a>';
}
const renderConceptSetCheckbox = function(hasPermissions, field) {
return hasPermissions()
? `<span data-bind="click: d => $component.toggleCheckbox(d, '${field}'), css: { selected: ${field} }" class="fa fa-check"></span>`
: `<span data-bind="css: { selected: ${field}}" class="fa fa-check readonly"></span>`;
}
const createConceptSetItem = function (concept) {
var conceptSetItem = {};
conceptSetItem.concept = {
"CONCEPT_ID": concept.CONCEPT_ID,
"CONCEPT_NAME": concept.CONCEPT_NAME,
"STANDARD_CONCEPT": concept.STANDARD_CONCEPT,
"STANDARD_CONCEPT_CAPTION": concept.STANDARD_CONCEPT_CAPTION,
"INVALID_REASON": concept.INVALID_REASON,
"INVALID_REASON_CAPTION": concept.INVALID_REASON_CAPTION,
"CONCEPT_CODE": concept.CONCEPT_CODE,
"DOMAIN_ID": concept.DOMAIN_ID,
"VOCABULARY_ID": concept.VOCABULARY_ID,
"CONCEPT_CLASS_ID": concept.CONCEPT_CLASS_ID
};
conceptSetItem.isExcluded = ko.observable(false);
conceptSetItem.includeDescendants = ko.observable(false);
conceptSetItem.includeMapped = ko.observable(false);
return conceptSetItem;
}
const syntaxHighlight = function (json) {
if (typeof json != 'string') {
json = ko.toJSON(json, undefined, 2);
}
json = json.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
const getPathwaysUrl = (id, section) => `/pathways/${id}/${section}`;
async function confirmAndDelete({ loading, remove, redirect, message = 'Are you sure?' } = {}) {
if (confirm(message)) {
loading(true);
await remove();
loading(false);
redirect();
}
}
const normalizeUrl = (...parts) => URI(parts.join('/')).normalizePathname().toString();
const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e))));
const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
const toggleConceptSetCheckbox = function(hasPermissions, selectedConcepts, d, field, successFunction) {
if (hasPermissions()) {
const concept = selectedConcepts().find(i => !!i.concept && !!d.concept && i.concept.CONCEPT_ID === d.concept.CONCEPT_ID);
if (!!concept) {
concept[field](!concept[field]());
successFunction();
}
}
}
const escapeTooltip = function(tooltipText) {
return tooltipText.replace(/'/g, "\\'").replace(/"/g, '"');
}
const getPathTo = function(element) {
if (element===document.body)
return element.tagName;
let ix = 0;
const siblings = element.parentNode.childNodes;
for (let i= 0; i<siblings.length; i++) {
const sibling = siblings[i];
if (sibling===element)
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
if (sibling.nodeType===1 && sibling.tagName===element.tagName)
ix++;
}
};
const calculateStringHash = function(string) {
let hash = 0;
if (string.length == 0) {
return hash;
}
for (var i = 0; i < string.length; i++) {
var char = string.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
};
return {
build,
confirmAndDelete,
cartesian,
routeTo,
hasRelationship,
contextSensitiveLinkColor,
hasCDM,
hasResults,
renderConceptSetItemSelector,
renderLink,
renderBoundLink,
renderConceptSelector,
renderHierarchyLink,
renderConceptSetCheckbox,
createConceptSetItem,
syntaxHighlight,
getPathwaysUrl,
normalizeUrl,
toggleConceptSetCheckbox,
escapeTooltip,
getPathTo,
calculateStringHash,
};
});