@microbiome-informatics/genome-properties-viewer 0.0.0-stage → 1.0.0-beta.2

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.
package/src/modal.js ADDED
@@ -0,0 +1,56 @@
1
+ import * as d3 from "./d3";
2
+ /**
3
+ * Manager for a modla dialog, that has an overlay grey area and a dialog box in the middle
4
+ */
5
+ class GPModal {
6
+ /**
7
+ * Appends the divs for the modal overlay and the popup.
8
+ * Inside the popup creates a `div` element with a refence in `this.content`.
9
+ * @param {HTMLElement} element - DOM element where the modal should be appended.
10
+ */
11
+ constructor(element) {
12
+ this.fixed = false;
13
+ this.mask = d3
14
+ .selectAll(element)
15
+ .append("div")
16
+ .attr("class", "gp-modal")
17
+ .on("click", () => !this.fixed && this.setVisibility(false));
18
+ this.popup = d3
19
+ .selectAll(element)
20
+ .append("div")
21
+ .attr("class", "gp-modal-popup");
22
+ this.content = this.popup.append("div").attr("class", "gp-modal-content");
23
+ }
24
+
25
+ /**
26
+ * Sets the visibility of the components of the modal
27
+ * @param {Boolean} visibility - visibility of the components of the modal
28
+ */
29
+ setVisibility(visibility) {
30
+ if (!visibility) this.fixed = false;
31
+ this.mask.classed("gp-modal-active", visibility);
32
+ this.popup.classed("gp-modal-active", visibility);
33
+ this.content.classed("gp-modal-active", visibility);
34
+ }
35
+
36
+ /**
37
+ * Sets the content of the popup, and sets the visibility to `true`
38
+ * @param {String} content - HTML to be appended in the dialog.
39
+ * @param {Boolean} fixed - If `false`, a click on the layover area closes the dialog..
40
+ */
41
+ showContent(content, fixed = false) {
42
+ this.fixed = fixed;
43
+ this.content.html(content);
44
+ this.setVisibility(true);
45
+ }
46
+
47
+ /**
48
+ * Returns the reference(created in d3) to the content object.
49
+ * @return {D3Selector} Reference(created in d3) to the content object.
50
+ */
51
+ getContentElement() {
52
+ return this.content;
53
+ }
54
+ }
55
+
56
+ export default GPModal;
package/src/zoomer.js ADDED
@@ -0,0 +1,128 @@
1
+ import * as d3 from "./d3";
2
+
3
+ /**
4
+ * Creates a panel with a slider and +/- buttons to allow resizing the view
5
+ */
6
+ class ZoomPanel {
7
+ /**
8
+ * Sets all the passed options in class atributes and initiates a d3.scale for the slider.
9
+ * @param {Object} options - All the available options of this class:
10
+ * @example <caption>Options defaults and explanations.</caption>
11
+ * ```javascript
12
+ * {
13
+ * x = 0, // X coordinate to locate the panel
14
+ * y = 0, // Y coordinate to locate the panel
15
+ * centerX = 17, // X coordinate where the buttons should be centered to
16
+ * top = 30, // Space in top before the center of the first button, use to create a gap for the sort button
17
+ * r = 10, // radius of the buttons
18
+ * padding = 3, // padding in between buttons and slider
19
+ * scrollH = 40, // Height of the scroller/slider
20
+ * scrollW = 10, // Width of the scroller/slider
21
+ * container = null, // D3 selection where the panel will be added
22
+ * domain = [0, 100], // Array of 2 positions defining the domain of values the function will return
23
+ * function_plus = null, // Callback for when the + button gets clicked
24
+ * function_less = null, // Callback for when the - button gets clicked
25
+ * function_slide = null, // Callback for when the slider gets dragged
26
+ * }
27
+ * ```
28
+ */
29
+ constructor({
30
+ x = 0,
31
+ y = 0,
32
+ centerX = 17,
33
+ top = 30,
34
+ r = 10,
35
+ padding = 3,
36
+ scrollH = 40,
37
+ scrollW = 10,
38
+ container = null,
39
+ domain = [0, 100],
40
+ function_plus = null,
41
+ function_less = null,
42
+ function_slide = null,
43
+ }) {
44
+ this.x = x;
45
+ this.y = y;
46
+ this.centerX = centerX;
47
+ this.top = top;
48
+ this.r = r;
49
+ this.padding = padding;
50
+ this.scrollH = scrollH;
51
+ this.scrollW = scrollW;
52
+ this.container = container;
53
+ this.domain = domain;
54
+ this.function_plus = function_plus;
55
+ this.function_less = function_less;
56
+ this.function_slide = function_slide;
57
+ this.slider = d3
58
+ .scaleLinear()
59
+ .domain(domain)
60
+ .range([top + r + scrollH, top + r + padding]);
61
+ }
62
+
63
+ /**
64
+ * Appends a group in the given container that includes the SVG elements to represent the zoomer.
65
+ * It also sets the events binding them to the callback funtions passed in the options object.
66
+ * @summary If the description is long, write your summary here. Otherwise, feel free to remove this.
67
+ */
68
+ draw_panel() {
69
+ this.zoom_panel = this.container
70
+ .append("g")
71
+ .attr("class", "gpv-zoomer")
72
+ .attr("transform", `translate(${this.x}, ${this.y})`);
73
+ ZoomPanel.add_button(
74
+ this.zoom_panel,
75
+ "+",
76
+ this.centerX,
77
+ this.top,
78
+ this.r
79
+ ).on("click", this.function_plus);
80
+ this.zoom_panel
81
+ .append("line")
82
+ .attr("x1", this.centerX)
83
+ .attr("x2", this.centerX)
84
+ .attr("y1", this.top + this.r + this.padding)
85
+ .attr("y2", this.top + this.r + this.padding + this.scrollH)
86
+ .style("stroke", "lightgrey");
87
+ this.zoomBar = this.zoom_panel
88
+ .append("rect")
89
+ .attr("x", this.centerX - this.scrollW / 2)
90
+ .attr("y", this.slider(this.domain[0]))
91
+ .attr("width", this.scrollW)
92
+ .attr("height", 4)
93
+ .style("cursor", "ns-resize")
94
+ .style("fill", "darkgrey")
95
+ .call(d3.drag().on("drag", this.function_slide));
96
+
97
+ ZoomPanel.add_button(
98
+ this.zoom_panel,
99
+ "-",
100
+ this.centerX,
101
+ this.top + 2 * this.r + 2 * this.padding + this.scrollH,
102
+ this.r
103
+ ).on("click", this.function_less);
104
+ }
105
+
106
+ /**
107
+ * Updates the position of the panel
108
+ */
109
+ refresh() {
110
+ this.zoom_panel.attr("transform", `translate(${this.x}, ${this.y})`);
111
+ }
112
+
113
+ /**
114
+ * Add a new circular button into a panel
115
+ * @param {Object} panel - D3 selector of the panel to add the button
116
+ * @param {String} text - Text for the button. the function doesn't check if the text doesn't fit the given radius
117
+ * @param {Number} x - X coordinate in the panel
118
+ * @param {Number} y - Y coordinate in the panel
119
+ * @param {Number} r - radius of the circles
120
+ */
121
+ static add_button(panel, text, x, y, r) {
122
+ const c = panel.append("circle").attr("cx", x).attr("cy", y).attr("r", r);
123
+ panel.append("text").attr("x", x).attr("y", y).text(text);
124
+ return c;
125
+ }
126
+ }
127
+
128
+ export default ZoomPanel;