UNPKG

9.56 kBJavaScriptView Raw
1import { a as __extends } from './tslib.es6-863e3717.js';
2import { select, event as event$1 } from 'd3';
3import { C as Component } from './Component-5b66527b.js';
4
5var ACTIONELEMENTHEIGHT = 28;
6var ACTIONELEMENTMAXWIDTH = 200;
7var ACTIONELEMENTCONTAINERMAXHEIGHT = 200;
8var VERTICALPADDING = 4;
9var ContextMenu = /** @class */ (function (_super) {
10 __extends(ContextMenu, _super);
11 function ContextMenu(drawChart, renderTarget) {
12 var _this = _super.call(this, renderTarget) || this;
13 _this.contextMenuVisible = false;
14 _this.drawChart = drawChart;
15 _this.contextMenuElement = select(renderTarget).insert("div", ":first-child")
16 .attr("class", "tsi-contextMenu")
17 .style("left", "0px")
18 .style("top", "0px");
19 return _this;
20 }
21 ContextMenu.prototype.launchSubMenu = function (parent, subMenuActions, subLevel, top) {
22 var container = this.contextMenuElement
23 .selectAll(".tsi-actionElementContainer" + subLevel)
24 .data([{ subLevel: subLevel }]);
25 var enteredContainer = container.enter()
26 .append('div')
27 .attr("class", function (d) { return "tsi-actionElementContainer tsi-actionElementContainer" + d.subLevel; })
28 .merge(container)
29 .style('max-height', ACTIONELEMENTCONTAINERMAXHEIGHT + "px")
30 .style('display', 'block');
31 this.createActionElements(enteredContainer, subMenuActions, subLevel);
32 this.positionAEC(enteredContainer, subMenuActions.length, top, subLevel);
33 container.exit().remove();
34 };
35 ContextMenu.prototype.positionAEC = function (container, subMenuActionsCount, top, subLevel) {
36 this.verticalPositionAEC(container, top, subMenuActionsCount, subLevel);
37 this.horizontalPositionAEC(container, subLevel);
38 };
39 ContextMenu.prototype.shouldHorizontalFlip = function (rawLeft) {
40 var containerLeft = rawLeft + this.left;
41 var totalWidth = select(this.renderTarget).node().getBoundingClientRect().width;
42 return ((containerLeft + ACTIONELEMENTMAXWIDTH) > totalWidth);
43 };
44 ContextMenu.prototype.shouldVerticalFlip = function (rawTop, elementCount) {
45 var containerTop = rawTop + this.top;
46 var totalHeight = select(this.renderTarget).node().getBoundingClientRect().height;
47 var heightOfElements = Math.min(elementCount * ACTIONELEMENTHEIGHT + (VERTICALPADDING * 2), ACTIONELEMENTCONTAINERMAXHEIGHT);
48 return ((containerTop + heightOfElements) > totalHeight);
49 };
50 //determine position relative to the chart as a whole
51 ContextMenu.prototype.getRelativeHorizontalPosition = function (node, isLeft) {
52 if (isLeft === void 0) { isLeft = true; }
53 return node.getBoundingClientRect().x + (isLeft ? 0 : node.getBoundingClientRect().width) - this.renderTarget.getBoundingClientRect().x;
54 };
55 ContextMenu.prototype.verticalPositionAEC = function (actionElementContainer, rawTop, elementCount, subLevel) {
56 var totalHeight = this.contextMenuElement.node().getBoundingClientRect().height;
57 if (this.shouldVerticalFlip(rawTop, elementCount)) {
58 actionElementContainer.style('bottom', (totalHeight - rawTop) - (subLevel === 0 ? 0 : ACTIONELEMENTHEIGHT + VERTICALPADDING) + "px")
59 .style('top', null);
60 }
61 else {
62 actionElementContainer.style('top', rawTop - VERTICALPADDING + "px")
63 .style('bottom', null);
64 }
65 };
66 ContextMenu.prototype.horizontalPositionAEC = function (actionElementContainer, subLevel) {
67 var leftPosition = 0;
68 var rightPosition = 0;
69 if (subLevel !== 0) {
70 var oneLevelUp = this.contextMenuElement.selectAll(".tsi-actionElementContainer" + (subLevel - 1));
71 if (oneLevelUp.size()) {
72 rightPosition = this.getRelativeHorizontalPosition(oneLevelUp.nodes()[0], false) - this.left;
73 leftPosition = this.getRelativeHorizontalPosition(oneLevelUp.nodes()[0], true) - this.left;
74 }
75 }
76 if (this.shouldHorizontalFlip(rightPosition)) {
77 actionElementContainer.style('left', null)
78 .style('right', 0 - leftPosition + "px");
79 }
80 else {
81 actionElementContainer.style('left', rightPosition + "px")
82 .style('right', null);
83 }
84 };
85 ContextMenu.prototype.getActionElementContainerTop = function (launchedFromActionNode) {
86 if (launchedFromActionNode === void 0) { launchedFromActionNode = null; }
87 if (launchedFromActionNode === null) {
88 return 0;
89 }
90 return launchedFromActionNode.getBoundingClientRect().top -
91 this.contextMenuElement.node().getBoundingClientRect().top;
92 };
93 ContextMenu.prototype.removeSubMenusAboveLevel = function (level) {
94 select(this.renderTarget).selectAll('.tsi-actionElementContainer').filter(function (subMenuD) {
95 return (subMenuD.subLevel > level);
96 }).remove();
97 };
98 ContextMenu.prototype.createActionElements = function (container, actions, subLevel) {
99 if (subLevel === void 0) { subLevel = 0; }
100 var self = this;
101 var actionElements = container.selectAll(".tsi-actionElement")
102 .data(actions.map(function (a) {
103 a.subLevel = subLevel;
104 return a;
105 }));
106 var actionElementsEntered = actionElements.enter()
107 .append("div")
108 .attr("class", "tsi-actionElement")
109 .classed('tsi-hasSubMenu', function (d) { return d.isNested; })
110 .merge(actionElements)
111 .text(function (d) { return d.name; })
112 .on('mouseenter', function (d, i) {
113 if (d.isNested) {
114 self.launchSubMenu(select(this), d.subActions, subLevel + 1, self.getActionElementContainerTop(this));
115 self.subMenuFromActionIndex = i;
116 self.subMenuFromSubLevel = d.subLevel;
117 return;
118 }
119 if ((d.subLevel === self.subMenuFromSubLevel && i !== self.subMenuFromActionIndex) || d.subLevel < self.subMenuFromSubLevel) {
120 self.removeSubMenusAboveLevel(d.subLevel);
121 }
122 })
123 .on("click", function (d, i) {
124 if (d.isNested) {
125 return;
126 }
127 if (self.endTime) { // if endTime is present, this is a brush action
128 var startTime = self.startTime ? self.startTime.toISOString().slice(0, -5) + "Z" : null;
129 var endTime = self.endTime ? self.endTime.toISOString().slice(0, -5) + "Z" : null;
130 d.action(startTime, endTime);
131 }
132 else {
133 var timestamp = self.startTime ? self.startTime.toISOString().slice(0, -5) + "Z" : null;
134 d.action(self.ae, self.splitBy, timestamp, event);
135 }
136 self.hide();
137 if (self.onClick)
138 self.onClick();
139 });
140 actionElements.exit().remove();
141 };
142 ContextMenu.prototype.draw = function (chartComponentData, renderTarget, options, mousePosition, aggKey, splitBy, onClick, startTime, endTime, event) {
143 var _this = this;
144 if (onClick === void 0) { onClick = null; }
145 if (startTime === void 0) { startTime = null; }
146 if (endTime === void 0) { endTime = null; }
147 this.contextMenuVisible = true;
148 if (!endTime) {
149 this.actions = chartComponentData.displayState[aggKey].contextMenuActions;
150 this.ae = chartComponentData.displayState[aggKey].aggregateExpression;
151 }
152 else {
153 this.actions = options.brushContextMenuActions;
154 }
155 this.splitBy = splitBy;
156 this.startTime = startTime;
157 this.endTime = endTime;
158 this.onClick = onClick;
159 _super.prototype.themify.call(this, this.contextMenuElement, options.theme);
160 this.left = mousePosition[0];
161 this.top = mousePosition[1];
162 this.contextMenuElement
163 .style('left', this.left + 'px')
164 .style('top', this.top + 'px');
165 this.contextMenuElement.selectAll('*').remove();
166 this.contextMenuElement.style("display", "block")
167 .on('mouseleave', function () {
168 _this.removeSubMenusAboveLevel(0);
169 });
170 var actionContainer = this.contextMenuElement
171 .selectAll('.tsi-actionElementContainer0')
172 .data([{ subLevel: 0 }]);
173 var actionContainerEntered = actionContainer.enter()
174 .append('div')
175 .attr('class', 'tsi-actionElementContainer tsi-actionElementContainer0');
176 this.createActionElements(actionContainerEntered, this.actions);
177 this.positionAEC(actionContainerEntered, this.actions.length, 0, 0);
178 var self = this;
179 select(this.renderTarget).on("click", function () {
180 if (!select(event$1.srcElement).classed('tsi-actionElement')) {
181 if (onClick) {
182 onClick();
183 }
184 self.hide();
185 }
186 });
187 };
188 ContextMenu.prototype.hide = function () {
189 this.contextMenuElement.style("display", "none");
190 this.contextMenuVisible = false;
191 };
192 return ContextMenu;
193}(Component));
194
195export { ContextMenu as C };