UNPKG

7.15 kBJavaScriptView Raw
1import * as d3 from "d3";
2import dataModel from "../datamodel/dataModel";
3import query from "../query/query";
4import provider from "../provider/provider";
5import logger from "../logger/logger";
6import runner from "../runner/runner";
7import result from "../result/result";
8import tools from "../tools/tools";
9import graph from "../graph/graph";
10import {update} from "../popoto";
11
12var taxonomy = {};
13taxonomy.containerId = "popoto-taxonomy";
14
15/**
16 * Create the taxonomy panel HTML elements.
17 */
18taxonomy.createTaxonomyPanel = function () {
19 var htmlContainer = d3.select("#" + taxonomy.containerId);
20
21 var taxoUL = htmlContainer.append("ul")
22 .attr("class", "ppt-taxo-ul");
23
24 var data = taxonomy.generateTaxonomiesData();
25
26 var taxos = taxoUL.selectAll(".taxo").data(data);
27
28 var taxoli = taxos.enter().append("li")
29 .attr("id", function (d) {
30 return d.id
31 })
32 .attr("class", "ppt-taxo-li")
33 .attr("value", function (d) {
34 return d.label;
35 });
36
37 taxoli.append("span")
38 .attr("class", function (d) {
39 return "ppt-icon " + provider.taxonomy.getCSSClass(d.label, "span-icon");
40 })
41 .html(" ");
42
43 taxoli.append("span")
44 .attr("class", "ppt-label")
45 .text(function (d) {
46 return provider.taxonomy.getTextValue(d.label);
47 });
48
49 taxoli.append("span")
50 .attr("class", "ppt-count");
51
52 // Add an on click event on the taxonomy to clear the graph and set this label as root
53 taxoli.on("click", taxonomy.onClick);
54
55 taxonomy.addTaxonomyChildren(taxoli);
56
57 // The count is updated for each labels.
58 var flattenData = [];
59 data.forEach(function (d) {
60 flattenData.push(d);
61 if (d.children) {
62 taxonomy.flattenChildren(d, flattenData);
63 }
64 });
65
66 if (!graph.DISABLE_COUNT) {
67 taxonomy.updateCount(flattenData);
68 }
69};
70
71/**
72 * Recursive function to flatten data content.
73 *
74 */
75taxonomy.flattenChildren = function (d, vals) {
76 d.children.forEach(function (c) {
77 vals.push(c);
78 if (c.children) {
79 vals.concat(taxonomy.flattenChildren(c, vals));
80 }
81 });
82};
83
84/**
85 * Updates the count number on a taxonomy.
86 *
87 * @param taxonomyData
88 */
89taxonomy.updateCount = function (taxonomyData) {
90 var statements = [];
91
92 taxonomyData.forEach(function (taxo) {
93 statements.push(
94 {
95 "statement": query.generateTaxonomyCountQuery(taxo.label)
96 }
97 );
98 });
99
100 (function (taxonomies) {
101 logger.info("Count taxonomies ==>");
102 runner.run(
103 {
104 "statements": statements
105 })
106 .then(function (results) {
107 logger.info("<== Count taxonomies");
108
109 for (var i = 0; i < taxonomies.length; i++) {
110 var count = results[i].records[0].get('count').toString();
111 d3.select("#" + taxonomies[i].id)
112 .select(".ppt-count")
113 .text(" (" + count + ")");
114 }
115 }, function (error) {
116 logger.error(error);
117 d3.select("#popoto-taxonomy")
118 .selectAll(".ppt-count")
119 .text(" (0)");
120 })
121 .catch(function (error) {
122 logger.error(error);
123 d3.select("#popoto-taxonomy")
124 .selectAll(".ppt-count")
125 .text(" (0)");
126 });
127 })(taxonomyData);
128};
129
130/**
131 * Recursively generate the taxonomy children elements.
132 *
133 * @param selection
134 */
135taxonomy.addTaxonomyChildren = function (selection) {
136 selection.each(function (d) {
137 var li = d3.select(this);
138
139 var children = d.children;
140 if (d.children) {
141 var childLi = li.append("ul")
142 .attr("class", "ppt-taxo-sub-ul")
143 .selectAll("li")
144 .data(children)
145 .enter()
146 .append("li")
147 .attr("id", function (d) {
148 return d.id
149 })
150 .attr("class", "ppt-taxo-sub-li")
151 .attr("value", function (d) {
152 return d.label;
153 });
154
155 childLi.append("span")
156 .attr("class", function (d) {
157 return "ppt-icon " + provider.taxonomy.getCSSClass(d.label, "span-icon");
158 })
159 .html("&nbsp;");
160
161 childLi.append("span")
162 .attr("class", "ppt-label")
163 .text(function (d) {
164 return provider.taxonomy.getTextValue(d.label);
165 });
166
167 childLi.append("span")
168 .attr("class", "ppt-count");
169
170 childLi.on("click", taxonomy.onClick);
171
172 taxonomy.addTaxonomyChildren(childLi);
173 }
174
175 });
176};
177
178taxonomy.onClick = function () {
179 d3.event.stopPropagation();
180
181 var label = this.attributes.value.value;
182
183 dataModel.nodes.length = 0;
184 dataModel.links.length = 0;
185
186 // Reinitialize internal label generator
187 graph.node.internalLabels = {};
188
189 update();
190 graph.mainLabel = label;
191 if (provider.node.getSchema(label) !== undefined) {
192 graph.addSchema(provider.node.getSchema(label));
193 } else {
194 graph.addRootNode(label);
195 }
196 graph.hasGraphChanged = true;
197 result.hasChanged = true;
198 graph.ignoreCount = false;
199 update();
200 tools.center();
201};
202
203/**
204 * Parse the list of label providers and return a list of data object containing only searchable labels.
205 * @returns {Array}
206 */
207taxonomy.generateTaxonomiesData = function () {
208 var id = 0;
209 var data = [];
210 // Retrieve root providers (searchable and without parent)
211 for (var label in provider.node.Provider) {
212 if (provider.node.Provider.hasOwnProperty(label)) {
213 if (provider.node.getProperty(label, "isSearchable") && !provider.node.Provider[label].parent) {
214 data.push({
215 "label": label,
216 "id": "popoto-lbl-" + id++
217 });
218 }
219 }
220 }
221
222 // Add children data for each provider with children.
223 data.forEach(function (d) {
224 if (provider.node.getProvider(d.label).hasOwnProperty("children")) {
225 id = taxonomy.addChildrenData(d, id);
226 }
227 });
228
229 return data;
230};
231
232/**
233 * Add children providers data.
234 * @param parentData
235 * @param id
236 */
237taxonomy.addChildrenData = function (parentData, id) {
238 parentData.children = [];
239
240 provider.node.getProvider(parentData.label).children.forEach(function (d) {
241 var childProvider = provider.node.getProvider(d);
242 var childData = {
243 "label": d,
244 "id": "popoto-lbl-" + id++
245 };
246 if (childProvider.hasOwnProperty("children")) {
247 id = taxonomy.addChildrenData(childData, id);
248 }
249 if (provider.node.getProperty(d, "isSearchable")) {
250 parentData.children.push(childData);
251 }
252 });
253
254 return id;
255};
256
257
258export default taxonomy;
\No newline at end of file