-
Notifications
You must be signed in to change notification settings - Fork 5
/
FacetTreeServiceHelpers.js
145 lines (109 loc) · 4.97 KB
/
FacetTreeServiceHelpers.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
var Step = require('./Step');
var Path = require('./Path');
var PathHead = require('./PathHead');
var shared = require('../util/shared');
var Promise = shared.Promise;
var FacetNodeState = require('./FacetNodeState');
var ListFilter = require('../service/ListFilter');
var ObjectUtils = require('../util/ObjectUtils');
var PromiseUtils = require('../util/PromiseUtils');
var FacetTreeServiceHelpers = {//Class.create({
// initialize: function(facetService) {
// this.facetService = facetService;
// },
fetchFacetTree: function(facetService, facetTreeState, startPath) {
var result = FacetTreeServiceHelpers.fetchFacetTreePathRec(facetService, facetTreeState, startPath).then(function(superRootFacet) {
var r = startPath == null ? superRootFacet.outgoing.children[0] : superRootFacet;
return r;
});
return result;
},
/**
* Note: This method fetches the *sub*Facets at a given path
* If the startPath is null, conceptually the children of the 'superRoot' facets are returned,
* which is an array containing solely the 'root' facet.
*/
fetchFacetTreePathRec: function(facetService, facetTreeState, startPath) {
// startPath must not be undefined, but may be null to indicate the root facet
startPath = startPath || null;
var pathExpansions = facetTreeState.getPathExpansions();
var pathToDirection = facetTreeState.getPathToDirection();
var isExpanded = startPath ? pathExpansions.contains(startPath) : true;
var dir = pathToDirection.get(startPath) || 1;
var promises = [];
if(isExpanded) {
var pathHead;
var p;
if(dir == null || dir === 1 || dir === 2) {
pathHead = startPath ? new PathHead(startPath, false) : null;
p = this.fetchFacetTreePathHeadRec(facetService, facetTreeState, pathHead);
promises.push(p);
} else {
promises.push(null);
}
if(dir === -1 || dir === 2) {
pathHead = startPath ? new PathHead(startPath, true) : null;
p = this.fetchFacetTreePathHeadRec(facetService, facetTreeState, pathHead);
promises.push(p);
} else {
promises.push(null);
}
}
return PromiseUtils.all(promises).spread(function(outgoing, incoming) {
var r = {
path: startPath,
isExpanded: isExpanded,
outgoing: outgoing,
incoming: incoming
};
return r;
});
},
fetchFacetTreePathHeadRec: function(facetService, facetTreeState, pathHead) {
var pathHeadToFilter = facetTreeState.getPathHeadToFilter();
var listFilter = pathHeadToFilter.get(pathHead) || new ListFilter(null, 10);
//console.log('ListFilter for ' + pathHead + ': ', JSON.stringify(listFilter));
//var pathHead = startPath ? new PathHead(startPath, state.isInverse()) : null;
var result = Promise
.resolve(facetService.prepareListService(pathHead))
.then(function(listService) {
var p1 = listService.fetchItems(listFilter.getConcept(), listFilter.getLimit(), listFilter.getOffset());
var p2 = listService.fetchCount(listFilter.getConcept());
return [p1, p2];
}).spread(function(facetEntries, countInfo) {
// FacetInfos
// |- countInfo
// |- labelInfo
var facetInfos = facetEntries.map(function(entry) {
return entry.val;
});
// Pluck the ID attributes
var subPromises = facetInfos.map(function(facetInfo) {
//console.log('subPath:', JSON.stringify(facetEntries));
var subPath = facetInfo.path;
if(!subPath) {
throw new Error('Could not obtain a path for the sub facets of ' + JSON.stringify(facetInfo));
}
var r = FacetTreeServiceHelpers.fetchFacetTreePathRec(facetService, facetTreeState, subPath);
return r;
});
var r = PromiseUtils.all(subPromises).then(function(children) {
facetInfos.forEach(function(facetInfo, i) {
var child = children[i];
ObjectUtils.extend(facetInfo, child);
});
var info = {
path: pathHead ? pathHead.getPath() : null,
pathHead: pathHead,
childCountInfo: countInfo,
listFilter: listFilter,
children: facetInfos
};
return info;
});
return r;
});
return result;
},
};
module.exports = FacetTreeServiceHelpers;