/
tagService.js
261 lines (211 loc) · 6.38 KB
/
tagService.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
/**
* Responsible for handling tag-related operations
*/
import { getTagKey, tagToString, getTagId, getTagName, undefinedOrEmpty, getScenarioId } from '../util.js'
import { forEach, sortBy, filter, values } from "lodash";
export default function TagService (dataService) {
'use strict';
/**
* Maps tag IDs to tags and their scenarios
*/
var tagScenarioMap = getTagScenarioMap(dataService.getTestCases());
/**
* Maps tag keys to tag nodes
*/
var tagNodeMap = {};
/**
* Maps tag names to list of tags with the same name
*/
var tagNameMap = {};
/**
* An array of root tags
*/
var rootTags;
/**
* Goes through all scenarios to find all tags.
* For each tag found, builds up a map of tag keys to tag entries,
* where a tag entry contains the tag definition and the list of scenarios
* that are tagged with that tag
*/
function getTagScenarioMap (scenarios) {
var tagScenarioMap = {};
forEach(scenarios, function (testCase) {
forEach(testCase.scenarios, function (scenario) {
scenario.tags = [];
forEach(scenario.tagIds, function (tagId) {
var tag = addEntry(tagId).tag;
scenario.tags.push(tag);
function addEntry (tagId) {
var tag = getTagByTagId(tagId);
var tagKey = getTagKey(tag);
var tagEntry = tagScenarioMap[tagKey];
if (!tagEntry) {
tagEntry = {
tag: tag,
scenarios: []
};
tagScenarioMap[tagKey] = tagEntry;
}
if (tagEntry.scenarios.indexOf(scenario) == -1) {
tagEntry.scenarios.push(scenario);
}
forEach(tagEntry.tag.tags, function (tagId) {
addEntry(tagId);
});
return tagEntry;
}
});
});
});
return tagScenarioMap;
}
function getRootTags () {
if (!rootTags) {
rootTags = calculateRootTags();
}
return rootTags;
}
/**
* Builds up a hierarchy of tag nodes that is shown in the
* navigation and returns the list of root nodes
*/
function calculateRootTags () {
forEach(values(tagScenarioMap), function (tagEntry) {
if (tagEntry.tag.hideInNav) return;
var tagNode = getTagNode(tagEntry);
var name = getTagName(tagEntry.tag);
var nameNode = tagNameMap[name];
if (!nameNode) {
nameNode = createNameNode(name);
tagNameMap[name] = nameNode;
}
nameNode.addTagNode(tagNode);
});
var nameNodesWithMultipleEntries = filter(values(tagNameMap), function (nameNode) {
return nameNode.subTags().length > 1;
});
forEach(nameNodesWithMultipleEntries, function (nameNode) {
forEach(nameNode.subTags(), function (subTag) {
subTag.nameNode = nameNode;
});
});
var nodesWithoutParents = filter(values(tagNodeMap), function (tagNode) {
return undefinedOrEmpty(tagNode.tag().tags) && !tagNode.nameNode;
});
return sortBy(nameNodesWithMultipleEntries.concat(nodesWithoutParents),
function (tagNode) {
return tagNode.nodeName();
});
function getTagNode (tagEntry) {
var tag = tagEntry.tag;
var key = getTagKey(tag);
var tagNode = tagNodeMap[key];
if (!tagNode) {
tagNode = createTagNode(tagEntry);
tagNodeMap[key] = tagNode;
if (tag.tags && tag.tags.length > 0) {
forEach(tag.tags, function (parentTagId) {
var parentTag = getTagByTagId(parentTagId);
var parentTagEntry = tagScenarioMap[getTagKey(parentTag)];
var parentTagNode = getTagNode(parentTagEntry);
parentTagNode.addTagNode(tagNode);
});
}
}
return tagNode;
}
function createTagNode (tagEntry) {
var tag = tagEntry.tag;
var scenarios = tagEntry.scenarios;
var node = createNode(tagToString(tag));
node.url = function () {
return '#tagid/' + window.encodeURIComponent(getTagId(tag)) +
(tag.value ? '/' + window.encodeURIComponent(tag.value) : '');
};
node.scenarios = function () {
return scenarios;
};
node.tag = function () {
return tag
};
return node;
}
/**
* A name node is a pseudo tag node that
* has as sub nodes all tags with the same name
*/
function createNameNode (name) {
var node = createNode(name);
node.url = function () {
return '#tag/' + window.encodeURIComponent(name);
};
node.scenarios = function () {
var scenarioMap = {};
forEach(node.subTags(), function (subTag) {
forEach(subTag.scenarios(), function (scenario) {
scenarioMap[getScenarioId(scenario)] = scenario;
});
});
return values(scenarioMap);
};
return node;
}
function createNode (name) {
var subTags = [];
return {
nodeName: function () {
return name;
},
leafs: function () {
return _.filter(subTags, function (t) {
return !t.hasChildren();
});
},
childNodes: function () {
return _.filter(subTags, function (t) {
return t.hasChildren();
});
},
hasChildren: function () {
return subTags.length > 0;
},
addTagNode: function (tagNode) {
subTags.push(tagNode);
},
subTags: function () {
return subTags;
}
}
}
}
function getScenariosByTag (tag) {
return tagScenarioMap[getTagKey(tag)].scenarios;
}
function getTagByKey (tagKey) {
var tagEntry = tagScenarioMap[tagKey];
return tagEntry && tagEntry.tag;
}
function getTagNameNode (name) {
return tagNameMap[name];
}
function getTagByTagId (tagId) {
var tagInstance = dataService.getTagFile().tags[tagId];
var tagType = dataService.getTagFile().tagTypeMap[tagInstance.tagType];
var tag = Object.create(tagType);
tag.value = tagInstance.value;
if (tagInstance.description) {
tag.description = tagInstance.description;
}
if (tagInstance.href) {
tag.href = tagInstance.href;
}
return tag;
}
return {
getScenariosByTag: getScenariosByTag,
getTagByTagId: getTagByTagId,
getTagByKey: getTagByKey,
getRootTags: getRootTags,
getTagNameNode: getTagNameNode
};
}