-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatamanagement.js
executable file
·171 lines (139 loc) · 6.02 KB
/
datamanagement.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
function injectData(vegaSpec, ftRawArticlesArray) {
let articles = [];
let relations = [];
// mapping of forTEXT node and taxonomy IDs to unique viz IDs
let ftIdToVizId = new Map();
let nextVizId = 0;
// generator of unique IDs across nodes and taxonomies
let vizIdProvider = function(ftId) {
if (!ftIdToVizId.has(ftId)) {
ftIdToVizId.set(ftId, nextVizId);
nextVizId++;
}
return ftIdToVizId.get(ftId);
}
// setup parent articles for the forTEXT categories
let root = {
'name' : 'forTEXT',
'id' : vizIdProvider('root')
}
articles.push(root);
let ressourcen = {
'name' : 'Ressourcen',
'id' : vizIdProvider('Ressourcen'),
'parent' : ftIdToVizId.get('root')
}
articles.push(ressourcen);
let routinen = {
'name' : 'Routinen',
'id' : vizIdProvider('Routinen'),
'parent' : ftIdToVizId.get('root')
}
articles.push(routinen);
let tools = {
'name' : 'Tools',
'id' : vizIdProvider('Tools'),
'parent' : ftIdToVizId.get('root')
}
articles.push(tools);
// loop forTEXT site data and extract articles and relations between articles
ftRawArticlesArray.forEach(function(ftArticle) {
let article = {};
let baseUrl = 'https://fortext.net/';
article.name = ftArticle.node_title;
article.id = vizIdProvider('n'+ftArticle.nid);
article.url = baseUrl + ftArticle._field_data.nid.entity.path.alias;
if (ftArticle.field_field_title_short !== undefined
&& ftArticle.field_field_title_short.length !== 0) {
let shortTitle = ftArticle.field_field_title_short[0].raw.value;
if (shortTitle !== undefined && shortTitle !== '') {
article.name = shortTitle;
}
}
// extract and add forTEXT sub categories
if (ftArticle.field_field_resource_category.length !== 0) {
// add Ressource's sub category if it hasn't been added yet
if (!ftIdToVizId.has('t'+ftArticle.field_field_resource_category[0].raw.tid)) {
let resourceCategory = {
'name' : ftArticle.field_field_resource_category[0].raw.taxonomy_term.name,
'id' : vizIdProvider('t'+ftArticle.field_field_resource_category[0].raw.tid),
'parent' : ftIdToVizId.get('Ressourcen')
}
articles.push(resourceCategory);
}
article.parent = vizIdProvider('t'+ftArticle.field_field_resource_category[0].raw.tid);
article.parentname = ftArticle.field_field_resource_category[0].raw.taxonomy_term.name;
article.parenturl = baseUrl + ftArticle.field_field_resource_category[0].raw.taxonomy_term.path.alias;
}
else if (ftArticle.field_field_routine_category.length !== 0) {
// add Routine's sub category if it hasn't been added yet
if (!ftIdToVizId.has('t'+ftArticle.field_field_routine_category[0].raw.tid)) {
let resourceCategory = {
'name' : ftArticle.field_field_routine_category[0].raw.taxonomy_term.name,
'id' : vizIdProvider('t'+ftArticle.field_field_routine_category[0].raw.tid),
'parent' : ftIdToVizId.get('Routinen')
}
articles.push(resourceCategory);
}
article.parent = vizIdProvider('t'+ftArticle.field_field_routine_category[0].raw.tid);
article.parentname = ftArticle.field_field_routine_category[0].raw.taxonomy_term.name;
article.parenturl = baseUrl + ftArticle.field_field_routine_category[0].raw.taxonomy_term.path.alias;
}
else if (ftArticle.field_field_tool_category.length !== 0) {
// add Tool's sub category if it hasn't been added yet
if (!ftIdToVizId.has('t'+ftArticle.field_field_tool_category[0].raw.tid)) {
let resourceCategory = {
'name' : ftArticle.field_field_tool_category[0].raw.taxonomy_term.name,
'id' : vizIdProvider('t'+ftArticle.field_field_tool_category[0].raw.tid),
'parent' : ftIdToVizId.get('Tools')
}
articles.push(resourceCategory);
}
article.parent = vizIdProvider('t'+ftArticle.field_field_tool_category[0].raw.tid);
article.parentname = ftArticle.field_field_tool_category[0].raw.taxonomy_term.name;
article.parenturl = baseUrl + ftArticle.field_field_tool_category[0].raw.taxonomy_term.path.alias;
}
else if (ftArticle.field_field_video_category.length !== 0) { // Video Ressources
// add Ressource's sub category if it hasn't been added yet
if (!ftIdToVizId.has('t'+ftArticle.field_field_video_category[0].raw.tid)) {
let resourceCategory = {
'name' : ftArticle.field_field_video_category[0].raw.taxonomy_term.name,
'id' : vizIdProvider('t'+ftArticle.field_field_video_category[0].raw.tid),
'parent' : ftIdToVizId.get('Ressourcen')
}
articles.push(resourceCategory);
}
article.parent = vizIdProvider('t'+ftArticle.field_field_video_category[0].raw.tid);
article.parentname = ftArticle.field_field_video_category[0].raw.taxonomy_term.name;
article.parenturl = baseUrl + ftArticle.field_field_video_category[0].raw.taxonomy_term.path.alias;
}
articles.push(article);
// handle relations between articles
if (ftArticle._field_data.nid.entity.field_related_content !== undefined
&& !Array.isArray(ftArticle._field_data.nid.entity.field_related_content)) {
ftArticle._field_data.nid.entity.field_related_content.und.forEach(function(target) {
let relation = {
'source' : vizIdProvider('n'+ftArticle.nid),
'target' : vizIdProvider('n'+target.nid)
}
relations.push(relation);
});
}
});
//there might be some related content that does not appear as an article
let availableArticleVizIds = new Set();
articles.forEach(article => availableArticleVizIds.add(article.id));
relations = relations.filter(relation => availableArticleVizIds.has(relation.target));
// console.log(articles);
// console.log(relations);
let treeData = vegaSpec.data.find(function(dataSpec) {
return dataSpec.name === 'tree';
});
delete treeData.url;
treeData.values = articles;
let relationData = vegaSpec.data.find(function(dataSpec) {
return dataSpec.name === 'relations';
});
delete relationData.url;
relationData.values = relations;
}