label fix & tooltips support

This commit is contained in:
Artem Halushko 2020-03-13 14:24:56 +02:00
parent e77754a6b1
commit 09a446bdad
11 changed files with 91 additions and 93 deletions

View File

@ -518,3 +518,50 @@ export function parseFunction(source: string, params: string[] = []): Function {
} }
return res; return res;
} }
export function parseTemplate(template, data) {
function formatValue(value, pattern) {
}
let res = '';
try {
let variables = '';
let expressions = template
.match(/\{(.*?)\}/g) // find expressions
.map(exp => exp.replace(/{|}/g, '')) // remove brackets
.map(exp => exp.split(':'))
.filter(arr => !!arr[1]) //filter expressions without format
.reduce((res, current) => {
res[current[0]] = current[1];
return res;
}, {});
for (let key in data) {
if (!key.includes('|'))
variables += `let ${key} = '${expressions[key] ? padValue(data[key], +expressions[key]) : data[key]}';`;
}
template = template.replace(/:\d+}/g, '}');
res = safeExecute(parseFunction(variables + 'return' + '`' + template + '`'));
}
catch (ex) {
}
return res;
}
export function padValue(val: any, dec: number): string {
let strVal;
let n;
val = parseFloat(val);
n = (val < 0);
val = Math.abs(val);
if (dec > 0) {
strVal = val.toFixed(dec).toString()
} else {
strVal = Math.round(val).toString();
}
strVal = (n ? '-' : '') + strVal;
return strVal;
}

View File

@ -20,7 +20,7 @@ import BaseGauge = CanvasGauges.BaseGauge;
import { FontStyle, FontWeight } from '@home/components/widget/lib/settings.models'; import { FontStyle, FontWeight } from '@home/components/widget/lib/settings.models';
import * as tinycolor_ from 'tinycolor2'; import * as tinycolor_ from 'tinycolor2';
import { ColorFormats } from 'tinycolor2'; import { ColorFormats } from 'tinycolor2';
import { isDefined, isString, isUndefined } from '@core/utils'; import { isDefined, isString, isUndefined, padValue } from '@core/utils';
const tinycolor = tinycolor_; const tinycolor = tinycolor_;
@ -765,24 +765,6 @@ function drawDigitalMinMax(context: DigitalGaugeCanvasRenderingContext2D, option
drawText(context, options, 'MinMax', options.maxValue+'', maxX, maxY); drawText(context, options, 'MinMax', options.maxValue+'', maxX, maxY);
} }
function padValue(val: any, options: CanvasDigitalGaugeOptions): string {
const dec = options.valueDec;
let strVal;
let n;
val = parseFloat(val);
n = (val < 0);
val = Math.abs(val);
if (dec > 0) {
strVal = val.toFixed(dec).toString()
} else {
strVal = Math.round(val).toString();
}
strVal = (n ? '-' : '') + strVal;
return strVal;
}
function drawDigitalValue(context: DigitalGaugeCanvasRenderingContext2D, options: CanvasDigitalGaugeOptions, value: any) { function drawDigitalValue(context: DigitalGaugeCanvasRenderingContext2D, options: CanvasDigitalGaugeOptions, value: any) {
if (options.hideValue) return; if (options.hideValue) return;
@ -792,7 +774,7 @@ function drawDigitalValue(context: DigitalGaugeCanvasRenderingContext2D, options
const textX = Math.round(baseX + width / 2); const textX = Math.round(baseX + width / 2);
const textY = valueY; const textY = valueY;
let text = options.valueText || padValue(value, options); let text = options.valueText || padValue(value, options.valueDec);
text += options.symbol; text += options.symbol;
context.save(); context.save();

View File

@ -98,26 +98,6 @@ export default abstract class LeafletMap {
this.map.invalidateSize(true); this.map.invalidateSize(true);
} }
createTooltip(marker, dsIndex, settings, markerArgs) {
var popup = L.popup();
popup.setContent('');
marker.bindPopup(popup, { autoClose: settings.autocloseTooltip, closeOnClick: false });
if (settings.displayTooltipAction == 'hover') {
marker.off('click');
marker.on('mouseover', function () {
this.openPopup();
});
marker.on('mouseout', function () {
this.closePopup();
});
}
return {
markerArgs: markerArgs,
popup: popup,
locationSettings: settings,
dsIndex: dsIndex
}
}
onResize() { onResize() {
@ -169,6 +149,9 @@ export default abstract class LeafletMap {
if (!location.equals(marker.location)) { if (!location.equals(marker.location)) {
marker.updateMarkerPosition(location); marker.updateMarkerPosition(location);
} }
if (settings.showTooltip) {
marker.updateMarkerTooltip(data);
}
marker.setDataSources(data, dataSources); marker.setDataSources(data, dataSources);
marker.updateMarkerIcon(settings); marker.updateMarkerIcon(settings);
} }
@ -207,11 +190,11 @@ export default abstract class LeafletMap {
updatePolyline(data, dataSources, settings) { updatePolyline(data, dataSources, settings) {
this.ready$.subscribe(() => { this.ready$.subscribe(() => {
this.poly.updatePolyline(settings, data, dataSources); this.poly.updatePolyline(settings, data, dataSources);
const bounds = this.bounds.extend(this.poly.leafletPoly.getBounds()); /* const bounds = this.bounds.extend(this.poly.leafletPoly.getBounds());
if (bounds.isValid()) { if (bounds.isValid()) {
this.map.fitBounds(bounds); this.map.fitBounds(bounds);
this.bounds = bounds; this.bounds = bounds;
} }*/
}); });
} }
} }

View File

@ -46,10 +46,11 @@ export enum MapProviders {
} }
export interface MarkerSettings extends MapOptions { export interface MarkerSettings extends MapOptions {
tooltipPattern?: any;
icon?: any; icon?: any;
showLabel?: boolean, showLabel?: boolean,
draggable?: boolean, draggable?: boolean,
displayTooltip?: boolean, showTooltip?: boolean,
color?: string, color?: string,
currentImage?: string; currentImage?: string;
useMarkerImageFunction?: boolean, useMarkerImageFunction?: boolean,

View File

@ -40,7 +40,7 @@ export class MapWidgetController implements MapWidgetInterface {
schema; schema;
data; data;
constructor(public mapProvider: MapProviders, private drawRoutes, ctx, $element) { constructor(public mapProvider: MapProviders, private drawRoutes, public ctx, $element) {
if (this.map) { if (this.map) {
this.map.map.remove(); this.map.map.remove();
delete this.map; delete this.map;
@ -73,6 +73,7 @@ export class MapWidgetController implements MapWidgetInterface {
colorFunction: parseFunction(settings.colorFunction, functionParams), colorFunction: parseFunction(settings.colorFunction, functionParams),
polygonColorFunction: parseFunction(settings.polygonColorFunction, functionParams), polygonColorFunction: parseFunction(settings.polygonColorFunction, functionParams),
markerImageFunction: parseFunction(settings.markerImageFunction, ['data', 'images', 'dsData', 'dsIndex']), markerImageFunction: parseFunction(settings.markerImageFunction, ['data', 'images', 'dsData', 'dsIndex']),
labelColor: this.ctx.widgetConfig.color,
tooltipPattern: settings.tooltipPattern || tooltipPattern: settings.tooltipPattern ||
"<b>${entityName}</b><br/><br/><b>Latitude:</b> ${" + settings.latKeyName + ":7}<br/><b>Longitude:</b> ${" + settings.lngKeyName + ":7}", "<b>${entityName}</b><br/><br/><b>Latitude:</b> ${" + settings.latKeyName + ":7}<br/><b>Longitude:</b> ${" + settings.lngKeyName + ":7}",
defaultCenterPosition: settings?.defaultCenterPosition?.split(',') || [0, 0], defaultCenterPosition: settings?.defaultCenterPosition?.split(',') || [0, 0],
@ -120,7 +121,6 @@ export class MapWidgetController implements MapWidgetInterface {
//const providerInfo = providerSets[mapProvider]; //const providerInfo = providerSets[mapProvider];
let schema = initSchema(); let schema = initSchema();
addToSchema(schema,this.getProvidersSchema()); addToSchema(schema,this.getProvidersSchema());
addGroupInfo(schema, "Map Provider Settings"); addGroupInfo(schema, "Map Provider Settings");
addToSchema(schema, commonMapSettingsSchema); addToSchema(schema, commonMapSettingsSchema);
addGroupInfo(schema, "Common Map Settings"); addGroupInfo(schema, "Common Map Settings");

View File

@ -31,12 +31,12 @@ export function createTooltip(target, settings, targetArgs?) {
this.closePopup(); this.closePopup();
}); });
} }
return { return popup/*{
markerArgs: targetArgs, markerArgs: targetArgs,
popup: popup, popup: popup,
locationSettings: settings, locationSettings: settings,
dsIndex: settings.dsIndex dsIndex: settings.dsIndex
}; };*/
} }

View File

@ -22,7 +22,10 @@
background-position: center center; background-position: center center;
} }
.leaflet-div-icon { .leaflet-div-icon,
background: none; .tb-marker-label,
.tb-marker-label:before {
border: none; border: none;
background: none;
box-shadow: none;
} }

View File

@ -16,7 +16,7 @@
import L from 'leaflet'; import L from 'leaflet';
import { MarkerSettings } from './map-models'; import { MarkerSettings } from './map-models';
import { aspectCache, safeExecute, parseFunction } from '@app/core/utils'; import { aspectCache, safeExecute, parseTemplate } from '@app/core/utils';
import { createTooltip } from './maps-utils'; import { createTooltip } from './maps-utils';
export class Marker { export class Marker {
@ -32,24 +32,21 @@ export class Marker {
constructor(private map: L.Map, location: L.LatLngExpression, public settings: MarkerSettings, data, dataSources, onClickListener?, markerArgs?, onDragendListener?) { constructor(private map: L.Map, location: L.LatLngExpression, public settings: MarkerSettings, data, dataSources, onClickListener?, markerArgs?, onDragendListener?) {
//this.map = map; //this.map = map;
this.location = location; this.location = location;
this.setDataSources(data, dataSources) this.setDataSources(data, dataSources);
this.leafletMarker = L.marker(location, { this.leafletMarker = L.marker(location, {
draggable: settings.draggable draggable: settings.draggable
}); });
this.createMarkerIcon((iconInfo) => { this.createMarkerIcon((iconInfo) => {
this.leafletMarker.setIcon(iconInfo.icon); this.leafletMarker.setIcon(iconInfo.icon);
if (settings.showLabel) { this.tooltipOffset = [0, -iconInfo.size[1] + 10];
this.tooltipOffset = [0, -iconInfo.size[1] + 10]; this.updateMarkerLabel(settings);
// this.updateMarkerLabel(settings) this.leafletMarker.addTo(map);
} });
this.leafletMarker.addTo(map) if (settings.showTooltip) {
}
);
if (settings.displayTooltip) {
this.tooltip = createTooltip(this.leafletMarker, settings, markerArgs); this.tooltip = createTooltip(this.leafletMarker, settings, markerArgs);
this.tooltip.setContent(parseTemplate(this.settings.tooltipPattern, data));
} }
if (onClickListener) { if (onClickListener) {
@ -67,35 +64,22 @@ export class Marker {
this.dataSources = dataSources; this.dataSources = dataSources;
} }
updateMarkerTooltip(data) {
this.tooltip.setContent(parseTemplate(this.settings.tooltipPattern, data));
}
updateMarkerPosition(position: L.LatLngExpression) { updateMarkerPosition(position: L.LatLngExpression) {
this.leafletMarker.setLatLng(position); this.leafletMarker.setLatLng(position);
} }
updateMarkerLabel(settings) { updateMarkerLabel(settings) {
function getText(template, data) {
let res = null;
try {
let variables = '';
for (let key in data) {
if (!key.includes('|'))
variables += `var ${key} = '${data[key]}';`;
}
res = safeExecute(parseFunction(variables + 'return' + '`' + template + '`'));
}
catch (ex) {
}
return res;
}
this.leafletMarker.unbindTooltip(); this.leafletMarker.unbindTooltip();
if (settings.showLabel) { if (settings.showLabel) {
if (settings.useLabelFunction) { if (settings.useLabelFunction) {
settings.labelText = safeExecute(settings.labelFunction, [this.data, this.dataSources, this.data.dsIndex]) settings.labelText = safeExecute(settings.labelFunction, [this.data, this.dataSources, this.data.dsIndex])
} }
else settings.labelText = getText(settings.label, this.data); else settings.labelText = parseTemplate(settings.label, this.data);
this.leafletMarker.bindTooltip(`<div style="color: ${settings.labelColor};"><b>${settings.labelText}</b></div>`, this.leafletMarker.bindTooltip(`<div style="color: ${settings.labelColor};"><b>${settings.labelText}</b></div>`,
{ className: 'tb-marker-label', permanent: true, direction: 'top', offset: this.tooltipOffset }); { className: 'tb-marker-label', permanent: true, direction: 'top', offset: this.tooltipOffset });
} }
@ -110,10 +94,8 @@ export class Marker {
updateMarkerIcon(settings) { updateMarkerIcon(settings) {
this.createMarkerIcon((iconInfo) => { this.createMarkerIcon((iconInfo) => {
this.leafletMarker.setIcon(iconInfo.icon); this.leafletMarker.setIcon(iconInfo.icon);
if (settings.showLabel) { this.tooltipOffset = [0, -iconInfo.size[1] + 10];
this.tooltipOffset = [0, -iconInfo.size[1] + 10]; this.updateMarkerLabel(settings);
this.updateMarkerLabel(settings)
}
}); });
} }
@ -121,7 +103,7 @@ export class Marker {
if (this.settings.icon) { if (this.settings.icon) {
onMarkerIconReady({ onMarkerIconReady({
size: [30,30], size: [30, 30],
icon: this.settings.icon, icon: this.settings.icon,
}); });
return; return;

View File

@ -34,7 +34,7 @@ export class Polygon {
opacity: settings.polygonStrokeOpacity opacity: settings.polygonStrokeOpacity
}).addTo(this.map); }).addTo(this.map);
if (settings.displayTooltip) { if (settings.showTooltip) {
this.tooltip = createTooltip(this.leafletPoly, location.dsIndex, settings); this.tooltip = createTooltip(this.leafletPoly, location.dsIndex, settings);
} }
if (onClickListener) { if (onClickListener) {

View File

@ -48,6 +48,7 @@ export class TripAnimationComponent implements OnInit, AfterViewInit {
this.widgetConfig = this.ctx.widgetConfig; this.widgetConfig = this.ctx.widgetConfig;
const settings = { const settings = {
normalizationStep: 1000, normalizationStep: 1000,
showLabel: false,
buttonColor: tinycolor(this.widgetConfig.color).setAlpha(0.54).toRgbString(), buttonColor: tinycolor(this.widgetConfig.color).setAlpha(0.54).toRgbString(),
disabledButtonColor: tinycolor(this.widgetConfig.color).setAlpha(0.3).toRgbString(), disabledButtonColor: tinycolor(this.widgetConfig.color).setAlpha(0.3).toRgbString(),
rotationAngle: 0 rotationAngle: 0
@ -73,7 +74,7 @@ export class TripAnimationComponent implements OnInit, AfterViewInit {
timeUpdated(time) { timeUpdated(time) {
const currentPosition = this.interpolatedData.map(dataSource => dataSource[time]); const currentPosition = this.interpolatedData.map(dataSource => dataSource[time]);
this.mapWidget.map.updateMarkers(currentPosition); this.mapWidget.map.updateMarkers(currentPosition);
} }
calculateIntervals() { calculateIntervals() {

View File

@ -108,4 +108,3 @@ const tinycolor = tinycolor_;
(window as any).TbCanvasDigitalGauge = TbCanvasDigitalGauge; (window as any).TbCanvasDigitalGauge = TbCanvasDigitalGauge;
(window as any).TbMapWidgetV2 = TbMapWidgetV2; (window as any).TbMapWidgetV2 = TbMapWidgetV2;
(window as any).TbTripAnimationWidget = TbTripAnimationWidget; (window as any).TbTripAnimationWidget = TbTripAnimationWidget;
console.log("TbTripAnimationWidget", TbTripAnimationWidget)