phylotree 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,452 @@
1
+ import * as d3 from "d3";
2
+ import * as _ from "underscore";
3
+ import * as events from "./events";
4
+ import { isLeafNode } from "../nodes";
5
+ import { isNodeCollapsed, hasHiddenNodes } from "./nodes";
6
+ import { predefined_selecters } from "./options";
7
+
8
+ let d3_layout_phylotree_context_menu_id = "d3_layout_phylotree_context_menu";
9
+
10
+ export function nodeDropdownMenu(node, container, phylotree, options, event) {
11
+ let menu_object = d3
12
+ .select(container)
13
+ .select("#" + d3_layout_phylotree_context_menu_id);
14
+
15
+ if (menu_object.empty()) {
16
+ menu_object = d3
17
+ .select(container)
18
+ .append("div")
19
+ .attr("id", d3_layout_phylotree_context_menu_id)
20
+ .attr("class", "dropdown-menu")
21
+ .attr("role", "menu");
22
+ }
23
+
24
+ menu_object.selectAll("a").remove();
25
+ menu_object.selectAll("h6").remove();
26
+ menu_object.selectAll("div").remove();
27
+
28
+ if (node) {
29
+ if (
30
+ !_.some([
31
+ Boolean(node.menu_items),
32
+ options["hide"],
33
+ options["selectable"],
34
+ options["collapsible"]
35
+ ]) ||
36
+ !options["show-menu"]
37
+ )
38
+ return;
39
+ if (!isLeafNode(node)) {
40
+ if (options["collapsible"]) {
41
+ menu_object
42
+ .append("a")
43
+ .attr("class", "dropdown-item")
44
+ .attr("tabindex", "-1")
45
+ .text(isNodeCollapsed(node) ? "Expand Subtree" : "Collapse Subtree")
46
+ .on("click", d => {
47
+ menu_object.style("display", "none");
48
+ this.toggleCollapse(node).update();
49
+ });
50
+ if (options["selectable"]) {
51
+ menu_object.append("div").attr("class", "dropdown-divider");
52
+ menu_object
53
+ .append("h6")
54
+ .attr("class", "dropdown-header")
55
+ .text("Toggle selection");
56
+ }
57
+ }
58
+
59
+ if (options["selectable"]) {
60
+ menu_object
61
+ .append("a")
62
+ .attr("class", "dropdown-item")
63
+ .attr("tabindex", "-1")
64
+ .text("All descendant branches")
65
+ .on("click", function(d) {
66
+ menu_object.style("display", "none");
67
+ phylotree.modifySelection(
68
+ phylotree.selectAllDescendants(node, true, true)
69
+ );
70
+ });
71
+
72
+ menu_object
73
+ .append("a")
74
+ .attr("class", "dropdown-item")
75
+ .attr("tabindex", "-1")
76
+ .text("All terminal branches")
77
+ .on("click", function(d) {
78
+ menu_object.style("display", "none");
79
+ phylotree.modifySelection(
80
+ phylotree.selectAllDescendants(node, true, false)
81
+ );
82
+ });
83
+
84
+ menu_object
85
+ .append("a")
86
+ .attr("class", "dropdown-item")
87
+ .attr("tabindex", "-1")
88
+ .text("All internal branches")
89
+ .on("click", function(d) {
90
+ menu_object.style("display", "none");
91
+ phylotree.modifySelection(
92
+ phylotree.selectAllDescendants(node, false, true)
93
+ );
94
+ });
95
+ }
96
+ }
97
+
98
+ if (node.parent) {
99
+ if (options["selectable"]) {
100
+ menu_object
101
+ .append("a")
102
+ .attr("class", "dropdown-item")
103
+ .attr("tabindex", "-1")
104
+ .text("Incident branch")
105
+ .on("click", function(d) {
106
+ menu_object.style("display", "none");
107
+ phylotree.modifySelection([node]);
108
+ });
109
+
110
+ menu_object
111
+ .append("a")
112
+ .attr("class", "dropdown-item")
113
+ .attr("tabindex", "-1")
114
+ .text("Path to root")
115
+ .on("click", d => {
116
+ menu_object.style("display", "none");
117
+ this.modifySelection(this.phylotree.pathToRoot(node));
118
+ });
119
+
120
+ if (options["reroot"] || options["hide"]) {
121
+ menu_object.append("div").attr("class", "dropdown-divider");
122
+ }
123
+ }
124
+
125
+ if (options["reroot"]) {
126
+ menu_object
127
+ .append("a")
128
+ .attr("class", "dropdown-item")
129
+ .attr("tabindex", "-1")
130
+ .text("Reroot on this node")
131
+ .on("click", d => {
132
+ menu_object.style("display", "none");
133
+ this.phylotree.reroot(node);
134
+ this.update();
135
+ });
136
+ }
137
+
138
+ if (options["hide"]) {
139
+ menu_object
140
+ .append("a")
141
+ .attr("class", "dropdown-item")
142
+ .attr("tabindex", "-1")
143
+ .text("Hide this " + (isLeafNode(node) ? "node" : "subtree"))
144
+ .on("click", d => {
145
+ menu_object.style("display", "none");
146
+ this.modifySelection([node], "notshown", true, true)
147
+ .updateHasHiddenNodes()
148
+ .update();
149
+ });
150
+ }
151
+ }
152
+
153
+ if (hasHiddenNodes(node)) {
154
+ menu_object
155
+ .append("a")
156
+ .attr("class", "dropdown-item")
157
+ .attr("tabindex", "-1")
158
+ .text("Show all descendant nodes")
159
+ .on("click", function(d) {
160
+ menu_object.style("display", "none");
161
+ phylotree
162
+ .modifySelection(
163
+ phylotree.selectAllDescendants(node, true, true),
164
+ "notshown",
165
+ true,
166
+ true,
167
+ "false"
168
+ )
169
+ .updateHasHiddenNodes()
170
+ .update();
171
+ });
172
+ }
173
+
174
+ // now see if we need to add user defined menus
175
+
176
+ var has_user_elements = [];
177
+ if ("menu_items" in node && typeof node["menu_items"] === "object") {
178
+ node["menu_items"].forEach(function(d) {
179
+ if (d.length == 3) {
180
+ if (!d[2] || d[2](node)) {
181
+ has_user_elements.push([d[0], d[1]]);
182
+ }
183
+ }
184
+ });
185
+ }
186
+
187
+ if (has_user_elements.length) {
188
+ const show_divider_options = [
189
+ options["hide"],
190
+ options["selectable"],
191
+ options["collapsible"]
192
+ ];
193
+
194
+ if (_.some(show_divider_options)) {
195
+ menu_object.append("div").attr("class", "dropdown-divider");
196
+ }
197
+
198
+ has_user_elements.forEach(function(d) {
199
+ menu_object
200
+ .append("a")
201
+ .attr("class", "dropdown-item")
202
+ .attr("tabindex", "-1")
203
+ .text((d[0])(node)) // eslint-disable-line
204
+ .on("click", _.partial(d[1], node));
205
+ });
206
+ }
207
+
208
+ let tree_container = document.querySelector(container); // eslint-disable-line
209
+ let rect = tree_container.getBoundingClientRect();
210
+
211
+ menu_object
212
+ .style("position", "absolute")
213
+ .style("left", "" + (event.clientX - rect.x + 12 ) + "px")
214
+ .style("top", "" + (event.clientY - rect.y ) + "px")
215
+ .style("display", "block");
216
+ } else {
217
+ menu_object.style("display", "none");
218
+ }
219
+
220
+ }
221
+
222
+ export function addCustomMenu(node, name, callback, condition) {
223
+ if (!("menu_items" in node)) {
224
+ node["menu_items"] = [];
225
+ }
226
+ if (
227
+ !node["menu_items"].some(function(d) {
228
+ return d[0] == name && d[1] == callback && d[2] == condition;
229
+ })
230
+ ) {
231
+ node["menu_items"].push([name, callback, condition]);
232
+ }
233
+ }
234
+
235
+ /**
236
+ *
237
+ * Modify the current selection, via functional programming.
238
+ *
239
+ * @param {Function} node_selecter A function to apply to each node, which
240
+ * determines whether they become part of the current selection. Alternatively,
241
+ * if ``restricted-selectable`` mode is enabled, a string describing one of
242
+ * the pre-defined restricted-selectable options.
243
+ * @param {String} attr (Optional) The selection attribute to modify.
244
+ * @param {Boolean} place (Optional) Whether or not ``placenodes`` should be called.
245
+ * @param {Boolean} skip_refresh (Optional) Whether or not a refresh is called.
246
+ * @param {String} mode (Optional) Can be ``"toggle"``, ``"true"``, or ``"false"``.
247
+ * @returns The current ``this``.
248
+ *
249
+ */
250
+ export function modifySelection(
251
+ node_selecter,
252
+ attr,
253
+ place,
254
+ skip_refresh,
255
+ mode
256
+ ) {
257
+
258
+ attr = attr || this.selection_attribute_name;
259
+ mode = mode || "toggle";
260
+
261
+ // check if node_selecter is a value of pre-defined selecters
262
+
263
+ if (this.options["restricted-selectable"].length) {
264
+ // the selection must be from a list of pre-determined selections
265
+ if (_.contains(_.keys(predefined_selecters), node_selecter)) {
266
+ node_selecter = predefined_selecters[node_selecter];
267
+ } else {
268
+ return;
269
+ }
270
+ }
271
+
272
+ if (
273
+ (this.options["restricted-selectable"] || this.options["selectable"]) &&
274
+ !this.options["binary-selectable"]
275
+ ) {
276
+ var do_refresh = false;
277
+
278
+ if (typeof node_selecter === "function") {
279
+ this.links.forEach(function(d) {
280
+ let select_me = node_selecter(d);
281
+ d[attr] = d[attr] || false;
282
+ if (d[attr] != select_me) {
283
+ d[attr] = select_me;
284
+ do_refresh = true;
285
+ d.target[attr] = select_me;
286
+ }
287
+ });
288
+ } else {
289
+ node_selecter.forEach(function(d) {
290
+ var new_value;
291
+ switch (mode) {
292
+ case "true":
293
+ new_value = true;
294
+ break;
295
+ case "false":
296
+ new_value = false;
297
+ break;
298
+ default:
299
+ new_value = !d[attr];
300
+ break;
301
+ }
302
+
303
+ if (d[attr] != new_value) {
304
+ d[attr] = new_value;
305
+ do_refresh = true;
306
+ }
307
+ });
308
+
309
+ this.links.forEach(function(d) {
310
+ d[attr] = d.target[attr];
311
+ });
312
+ }
313
+
314
+ var counts;
315
+
316
+ if (do_refresh) {
317
+ if (!skip_refresh) {
318
+ events.triggerRefresh(this);
319
+ }
320
+ if (this.countHandler) {
321
+ counts = {};
322
+ counts[attr] = this.links.reduce(function(p, c) {
323
+ return p + (c[attr] ? 1 : 0);
324
+ }, 0);
325
+ events.countUpdate(this, counts, this.countHandler);
326
+ }
327
+
328
+ if (place) {
329
+ this.placenodes();
330
+ }
331
+ }
332
+ } else if (this.options["binary-selectable"]) {
333
+ if (typeof node_selecter === "function") {
334
+ this.links.forEach(function(d) {
335
+ var select_me = node_selecter(d);
336
+ d[attr] = d[attr] || false;
337
+
338
+ if (d[attr] != select_me) {
339
+ d[attr] = select_me;
340
+ do_refresh = true;
341
+ d.target[attr] = select_me;
342
+ }
343
+
344
+ this.options["attribute-list"].forEach(function(type) {
345
+ if (type != attr && d[attr] === true) {
346
+ d[type] = false;
347
+ d.target[type] = false;
348
+ }
349
+ });
350
+ });
351
+ } else {
352
+ node_selecter.forEach(function(d) {
353
+ var new_value;
354
+ new_value = !d[attr];
355
+
356
+ if (d[attr] != new_value) {
357
+ d[attr] = new_value;
358
+ do_refresh = true;
359
+ }
360
+ });
361
+
362
+ this.links.forEach(function(d) {
363
+ d[attr] = d.target[attr];
364
+ this.options["attribute-list"].forEach(function(type) {
365
+ if (type != attr && d[attr] !== true) {
366
+ d[type] = false;
367
+ d.target[type] = false;
368
+ }
369
+ });
370
+ });
371
+ }
372
+
373
+ if (do_refresh) {
374
+ if (!skip_refresh) {
375
+ events.triggerRefresh(this);
376
+ }
377
+ if (this.countHandler()) {
378
+ counts = {};
379
+ counts[attr] = this.links.reduce(function(p, c) {
380
+ return p + (c[attr] ? 1 : 0);
381
+ }, 0);
382
+ this.countUpdate(this, counts, this.countHandler());
383
+ }
384
+
385
+ if (place) {
386
+ this.placenodes();
387
+ }
388
+ }
389
+ }
390
+
391
+ if (this.selectionCallback && attr != "tag") {
392
+ this.selectionCallback(this.getSelection());
393
+ }
394
+
395
+ this.refresh();
396
+ this.update();
397
+ return this;
398
+ }
399
+
400
+ /**
401
+ * Get nodes which are currently selected.
402
+ *
403
+ * @returns {Array} An array of nodes that match the current selection.
404
+ */
405
+ export function getSelection() {
406
+ return this.nodes.filter(d => {
407
+ return d[this.selection_attribute_name];
408
+ });
409
+ }
410
+
411
+ /**
412
+ * Select all descendents of a given node, with options for selecting
413
+ * terminal/internal nodes.
414
+ *
415
+ * @param {Node} node The node whose descendents should be selected.
416
+ * @param {Boolean} terminal Whether to include terminal nodes.
417
+ * @param {Boolean} internal Whther to include internal nodes.
418
+ * @returns {Array} An array of selected nodes.
419
+ */
420
+ export function selectAllDescendants(node, terminal, internal) {
421
+ let selection = [];
422
+
423
+ function sel(d) {
424
+ if (isLeafNode(d)) {
425
+ if (terminal) {
426
+ if (d != node) selection.push(d);
427
+ }
428
+ } else {
429
+ if (internal) {
430
+ if (d != node) selection.push(d);
431
+ }
432
+ d.children.forEach(sel);
433
+ }
434
+ }
435
+
436
+ sel(node);
437
+ return selection;
438
+ }
439
+
440
+ /**
441
+ * Getter/setter for the selection callback. This function is called
442
+ * every time the current selection is modified, and its argument is
443
+ * an array of nodes that make up the current selection.
444
+ *
445
+ * @param {Function} callback (Optional) The selection callback function.
446
+ * @returns The current ``selectionCallback`` if getting, or the current ``this`` if setting.
447
+ */
448
+ export function selectionCallback(callback) {
449
+ if (!callback) return this.selectionCallback;
450
+ this.selectionCallback = callback;
451
+ return this;
452
+ }