UNPKG

6.87 kBJavaScriptView Raw
1import { a as __extends } from './tslib.es6-863e3717.js';
2import { U as Utils } from './Utils-5f9f1f09.js';
3import { select } from 'd3';
4import { S as ServerClient } from './ServerClient-f4bd7870.js';
5import { H as HistoryPlayback } from './HistoryPlayback-643bc8b2.js';
6
7var ProcessGraphic = /** @class */ (function (_super) {
8 __extends(ProcessGraphic, _super);
9 function ProcessGraphic(renderTarget) {
10 var _this = _super.call(this, renderTarget) || this;
11 _this.serverClient = new ServerClient();
12 _this.currentCancelTrigger = null;
13 return _this;
14 }
15 ProcessGraphic.prototype.render = function (environmentFqdn, getToken, graphicSrc, data, chartOptions) {
16 this.graphicSrc = graphicSrc;
17 this.renderBase(environmentFqdn, getToken, data, chartOptions);
18 };
19 ProcessGraphic.prototype.loadResources = function () {
20 var _this = this;
21 return new Promise(function (resolve, reject) {
22 var image = new Image();
23 image.onload = function () {
24 _this.graphic = image;
25 _this.graphicOriginalWidth = image.width;
26 _this.graphicOriginalHeight = image.height;
27 _this.component.node().appendChild(_this.graphic);
28 resolve(null);
29 };
30 image.onerror = function (errorMessage) {
31 console.log(errorMessage);
32 reject(errorMessage);
33 };
34 image.src = _this.graphicSrc;
35 });
36 };
37 ProcessGraphic.prototype.draw = function () {
38 var graphicContainerWidth = this.renderTarget.clientWidth;
39 var graphicContainerHeight = this.renderTarget.clientHeight - this.playbackSliderHeight;
40 this.componentContainer
41 .style('width', graphicContainerWidth + "px")
42 .style('height', graphicContainerHeight + "px");
43 var resizedImageDim = this.getResizedImageDimensions(graphicContainerWidth, graphicContainerHeight, this.graphicOriginalWidth, this.graphicOriginalHeight);
44 this.component
45 .style('width', resizedImageDim.width + "px")
46 .style('height', resizedImageDim.height + "px");
47 this.drawBase();
48 };
49 ProcessGraphic.prototype.getResizedImageDimensions = function (containerWidth, containerHeight, imageWidth, imageHeight) {
50 if (containerWidth >= imageWidth && containerHeight >= imageHeight) {
51 return {
52 width: imageWidth,
53 height: imageHeight
54 };
55 }
56 // Calculate the factor we would need to multiply width by to make it fit in the container.
57 // Do the same for height. The smallest of those two corresponds to the largest size reduction
58 // needed. Multiply both width and height by the smallest factor to a) ensure we maintain the
59 // aspect ratio of the image b) ensure the image fits inside the container.
60 var widthFactor = containerWidth / imageWidth;
61 var heightFactor = containerHeight / imageHeight;
62 var resizeFactor = Math.min(widthFactor, heightFactor);
63 return {
64 width: imageWidth * resizeFactor,
65 height: imageHeight * resizeFactor
66 };
67 };
68 ProcessGraphic.prototype.getDataPoints = function (results) {
69 var _this = this;
70 if (Array.isArray(results)) {
71 var dataPoints = results.map(function (r, i) {
72 var value = _this.parseTsqResponse(r);
73 var color = typeof (_this.tsqExpressions[i].color) === 'function'
74 ? _this.tsqExpressions[i].color(value)
75 : _this.tsqExpressions[i].color;
76 return {
77 value: value,
78 alias: _this.tsqExpressions[i].alias,
79 x: _this.tsqExpressions[i].positionX,
80 y: _this.tsqExpressions[i].positionY,
81 color: _this.sanitizeAttribute(color),
82 onClick: _this.tsqExpressions[i].onElementClick
83 };
84 });
85 this.updateDataMarkers(dataPoints);
86 }
87 };
88 ProcessGraphic.prototype.updateDataMarkers = function (graphicValues) {
89 var _this = this;
90 var textElements = this.component.selectAll('div')
91 .data(graphicValues);
92 var newElements = textElements.enter()
93 .append('div')
94 .classed('tsi-process-graphic-label', true);
95 newElements.append('div')
96 .classed('title', true);
97 newElements.append('div')
98 .classed('value', true);
99 newElements.merge(textElements)
100 .classed('tsi-dark', false)
101 .classed('tsi-light', false)
102 .classed(Utils.getTheme(this.chartOptions.theme), true)
103 .style('left', function (tsqe) { return tsqe.x + "%"; })
104 .style('top', function (tsqe) { return tsqe.y + "%"; });
105 // Trigger glow css animation when values update.
106 var highlightCssClass = 'tsi-label-highlight';
107 this.component.selectAll('.tsi-process-graphic-label')
108 .data(graphicValues)
109 .classed(highlightCssClass, true)
110 .classed('clickable', function (tsqe) { return tsqe.onClick !== null; })
111 .on('animationend', function () {
112 select(this).classed(highlightCssClass, false);
113 })
114 .on('click', function (tsqe) {
115 if (typeof (tsqe.onClick) === 'function') {
116 tsqe.onClick({
117 timeStamp: _this.playbackControls.currentTimeStamp,
118 value: tsqe.value,
119 color: tsqe.color
120 });
121 }
122 });
123 this.component.selectAll('.title')
124 .data(graphicValues)
125 .text(function (tsqe) { return tsqe.alias || ''; });
126 this.component.selectAll('.value')
127 .data(graphicValues)
128 .text(function (tsqe) { return tsqe.value !== null ? Utils.formatYAxisNumber(tsqe.value) : '--'; })
129 .style('color', function (tsqe) { return tsqe.color; });
130 };
131 ProcessGraphic.prototype.parseTsqResponse = function (response) {
132 return (response && response.properties && response.properties[0] && response.properties[0].values)
133 ? response.properties[0].values[0]
134 : null;
135 };
136 ProcessGraphic.prototype.sanitizeAttribute = function (str) {
137 var sanitized = String(str);
138 var illegalChars = ['"', "'", '?', '<', '>', ';'];
139 illegalChars.forEach(function (c) { sanitized = sanitized.split(c).join(''); });
140 return sanitized;
141 };
142 return ProcessGraphic;
143}(HistoryPlayback));
144
145export { ProcessGraphic as P };