add support custom actions
This commit is contained in:
parent
76a8a60d76
commit
cc5ebfbff7
@ -524,9 +524,10 @@ export function parseFunction(source: any, params: string[] = []): Function {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseTemplate(template: string, data: object) {
|
export function parseTemplate(template: string, data: object) {
|
||||||
let res = '';
|
let res = '';
|
||||||
try {
|
try {
|
||||||
|
template = template.replace(/<link-act/g, '<a').replace(/link-act>/g, 'a>').replace(/name=(\'|")(.*?)(\'|")/g, `class='tb-custom-action' id='$2'`);
|
||||||
let variables = '';
|
let variables = '';
|
||||||
const expressions = template
|
const expressions = template
|
||||||
.match(/\{(.*?)\}/g) // find expressions
|
.match(/\{(.*?)\}/g) // find expressions
|
||||||
|
|||||||
@ -65,7 +65,7 @@ export default abstract class LeafletMap {
|
|||||||
let addMarker: L.Control;
|
let addMarker: L.Control;
|
||||||
this.map.on('mouseup', (e: L.LeafletMouseEvent) => {
|
this.map.on('mouseup', (e: L.LeafletMouseEvent) => {
|
||||||
mousePositionOnMap = e.latlng;
|
mousePositionOnMap = e.latlng;
|
||||||
})
|
});
|
||||||
const dragListener = (e: L.DragEndEvent) => {
|
const dragListener = (e: L.DragEndEvent) => {
|
||||||
if (e.type === 'dragend' && mousePositionOnMap) {
|
if (e.type === 'dragend' && mousePositionOnMap) {
|
||||||
const newMarker = L.marker(mousePositionOnMap).addTo(this.map);
|
const newMarker = L.marker(mousePositionOnMap).addTo(this.map);
|
||||||
@ -83,7 +83,14 @@ export default abstract class LeafletMap {
|
|||||||
this.createMarker(ds.entityName, updatedEnttity, this.datasources, this.options, false);
|
this.createMarker(ds.entityName, updatedEnttity, this.datasources, this.options, false);
|
||||||
}
|
}
|
||||||
datasourcesList.append(dsItem);
|
datasourcesList.append(dsItem);
|
||||||
})
|
});
|
||||||
|
const deleteBtn = document.createElement('a');
|
||||||
|
deleteBtn.appendChild(document.createTextNode('Delete position'));
|
||||||
|
deleteBtn.setAttribute('color', 'red');
|
||||||
|
deleteBtn.onclick = () => {
|
||||||
|
this.map.removeLayer(newMarker);
|
||||||
|
}
|
||||||
|
datasourcesList.append(deleteBtn);
|
||||||
const popup = L.popup();
|
const popup = L.popup();
|
||||||
popup.setContent(datasourcesList);
|
popup.setContent(datasourcesList);
|
||||||
newMarker.bindPopup(popup).openPopup();
|
newMarker.bindPopup(popup).openPopup();
|
||||||
@ -96,6 +103,7 @@ export default abstract class LeafletMap {
|
|||||||
img.src = `assets/add_location.svg`;
|
img.src = `assets/add_location.svg`;
|
||||||
img.style.width = '32px';
|
img.style.width = '32px';
|
||||||
img.style.height = '32px';
|
img.style.height = '32px';
|
||||||
|
img.title = "Drag and drop to add marker";
|
||||||
img.onclick = this.dragMarker;
|
img.onclick = this.dragMarker;
|
||||||
img.draggable = true;
|
img.draggable = true;
|
||||||
const draggableImg = new L.Draggable(img);
|
const draggableImg = new L.Draggable(img);
|
||||||
@ -106,7 +114,6 @@ export default abstract class LeafletMap {
|
|||||||
onRemove(map) {
|
onRemove(map) {
|
||||||
},
|
},
|
||||||
dragMarker: this.dragMarker
|
dragMarker: this.dragMarker
|
||||||
|
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
L.control.addMarker = (opts) => {
|
L.control.addMarker = (opts) => {
|
||||||
@ -193,8 +200,8 @@ export default abstract class LeafletMap {
|
|||||||
|
|
||||||
convertToCustomFormat(position: L.LatLng): object {
|
convertToCustomFormat(position: L.LatLng): object {
|
||||||
return {
|
return {
|
||||||
[this.options.latKeyName]: position.lat,
|
[this.options.latKeyName]: position.lat % 180,
|
||||||
[this.options.lngKeyName]: position.lng
|
[this.options.lngKeyName]: position.lng % 180
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -54,6 +54,7 @@ export enum MapProviders {
|
|||||||
|
|
||||||
export type MarkerSettings = {
|
export type MarkerSettings = {
|
||||||
tooltipPattern?: any;
|
tooltipPattern?: any;
|
||||||
|
tooltipActions: object;
|
||||||
icon?: any;
|
icon?: any;
|
||||||
showLabel?: boolean;
|
showLabel?: boolean;
|
||||||
label: string;
|
label: string;
|
||||||
@ -99,7 +100,8 @@ export type PolygonSettings = {
|
|||||||
polygonColor: string;
|
polygonColor: string;
|
||||||
autocloseTooltip: boolean;
|
autocloseTooltip: boolean;
|
||||||
displayTooltipAction: string;
|
displayTooltipAction: string;
|
||||||
|
tooltipActions: object;
|
||||||
|
|
||||||
polygonColorFunction?: GenericFunction;
|
polygonColorFunction?: GenericFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +110,7 @@ export type PolylineSettings = {
|
|||||||
autocloseTooltip: boolean;
|
autocloseTooltip: boolean;
|
||||||
displayTooltipAction: string;
|
displayTooltipAction: string;
|
||||||
useColorFunction: any;
|
useColorFunction: any;
|
||||||
|
tooltipActions: object;
|
||||||
color: string;
|
color: string;
|
||||||
useStrokeOpacityFunction: any;
|
useStrokeOpacityFunction: any;
|
||||||
strokeOpacity: number;
|
strokeOpacity: number;
|
||||||
|
|||||||
@ -30,12 +30,12 @@ import {
|
|||||||
} from './schemes';
|
} from './schemes';
|
||||||
import { MapWidgetStaticInterface, MapWidgetInterface } from './map-widget.interface';
|
import { MapWidgetStaticInterface, MapWidgetInterface } from './map-widget.interface';
|
||||||
import { OpenStreetMap, TencentMap, GoogleMap, HEREMap, ImageMap } from './providers';
|
import { OpenStreetMap, TencentMap, GoogleMap, HEREMap, ImageMap } from './providers';
|
||||||
import { parseFunction, parseArray, parseData } from '@core/utils';
|
import { parseFunction, parseArray, parseData, safeExecute } from '@core/utils';
|
||||||
import { initSchema, addToSchema, mergeSchemes, addCondition, addGroupInfo } from '@core/schema-utils';
|
import { initSchema, addToSchema, mergeSchemes, addCondition, addGroupInfo } from '@core/schema-utils';
|
||||||
import { forkJoin } from 'rxjs';
|
import { forkJoin } from 'rxjs';
|
||||||
import { WidgetContext } from '@app/modules/home/models/widget-component.models';
|
import { WidgetContext } from '@app/modules/home/models/widget-component.models';
|
||||||
import { getDefCenterPosition } from './maps-utils';
|
import { getDefCenterPosition } from './maps-utils';
|
||||||
import { JsonSettingsSchema } from '@shared/models/widget.models';
|
import { JsonSettingsSchema, WidgetActionDescriptor } from '@shared/models/widget.models';
|
||||||
import { EntityId } from '@shared/models/id/entity-id';
|
import { EntityId } from '@shared/models/id/entity-id';
|
||||||
import { AttributeScope } from '@shared/models/telemetry/telemetry.models';
|
import { AttributeScope } from '@shared/models/telemetry/telemetry.models';
|
||||||
import { AttributeService } from '@core/http/attribute.service';
|
import { AttributeService } from '@core/http/attribute.service';
|
||||||
@ -45,6 +45,7 @@ import { Type } from '@angular/core';
|
|||||||
export class MapWidgetController implements MapWidgetInterface {
|
export class MapWidgetController implements MapWidgetInterface {
|
||||||
|
|
||||||
constructor(public mapProvider: MapProviders, private drawRoutes: boolean, public ctx: WidgetContext, $element: HTMLElement) {
|
constructor(public mapProvider: MapProviders, private drawRoutes: boolean, public ctx: WidgetContext, $element: HTMLElement) {
|
||||||
|
console.log("MapWidgetController -> constructor -> ctx", ctx)
|
||||||
if (this.map) {
|
if (this.map) {
|
||||||
this.map.map.remove();
|
this.map.map.remove();
|
||||||
delete this.map;
|
delete this.map;
|
||||||
@ -55,7 +56,11 @@ export class MapWidgetController implements MapWidgetInterface {
|
|||||||
$element = ctx.$container[0];
|
$element = ctx.$container[0];
|
||||||
}
|
}
|
||||||
this.settings = this.initSettings(ctx.settings);
|
this.settings = this.initSettings(ctx.settings);
|
||||||
|
const descriptors = this.ctx.actionsApi.getActionDescriptors('tooltipAction');
|
||||||
|
this.settings.tooltipActions = {};
|
||||||
|
descriptors.forEach(descriptor => {
|
||||||
|
this.settings.tooltipActions[descriptor.name] = ($event) => this.onTooltipClick(descriptor, $event);
|
||||||
|
}, this.settings.tooltipActions);
|
||||||
const MapClass = providerSets[this.provider]?.MapClass;
|
const MapClass = providerSets[this.provider]?.MapClass;
|
||||||
if (!MapClass) {
|
if (!MapClass) {
|
||||||
return;
|
return;
|
||||||
@ -69,6 +74,7 @@ export class MapWidgetController implements MapWidgetInterface {
|
|||||||
schema: JsonSettingsSchema;
|
schema: JsonSettingsSchema;
|
||||||
data;
|
data;
|
||||||
settings: UnitedMapSettings;
|
settings: UnitedMapSettings;
|
||||||
|
actions: Map<string, Map<string, (widgetContext: WidgetContext) => void>>;
|
||||||
|
|
||||||
public static dataKeySettingsSchema(): object {
|
public static dataKeySettingsSchema(): object {
|
||||||
return {};
|
return {};
|
||||||
@ -86,7 +92,6 @@ export class MapWidgetController implements MapWidgetInterface {
|
|||||||
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');
|
||||||
|
|
||||||
if (drawRoutes) {
|
if (drawRoutes) {
|
||||||
addToSchema(schema, routeMapSettingsSchema);
|
addToSchema(schema, routeMapSettingsSchema);
|
||||||
addGroupInfo(schema, 'Route Map Settings');
|
addGroupInfo(schema, 'Route Map Settings');
|
||||||
@ -118,6 +123,18 @@ export class MapWidgetController implements MapWidgetInterface {
|
|||||||
onInit() {
|
onInit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private onTooltipClick(descriptor: WidgetActionDescriptor, $event: any) {
|
||||||
|
if ($event) {
|
||||||
|
$event.stopPropagation();
|
||||||
|
}
|
||||||
|
// safeExecute(parseFunction(descriptor.customFunction, ['$event', 'widgetContext']), [$event, this.ctx])
|
||||||
|
const entityInfo = this.ctx.actionsApi.getActiveEntityInfo();
|
||||||
|
const entityId = entityInfo ? entityInfo.entityId : null;
|
||||||
|
const entityName = entityInfo ? entityInfo.entityName : null;
|
||||||
|
const entityLabel = entityInfo ? entityInfo.entityLabel : null;
|
||||||
|
this.ctx.actionsApi.handleWidgetAction($event, descriptor, entityId, entityName, null, entityLabel);
|
||||||
|
}
|
||||||
|
|
||||||
setMarkerLocation = (e) => {
|
setMarkerLocation = (e) => {
|
||||||
const attributeService = this.ctx.$injector.get(AttributeService);
|
const attributeService = this.ctx.$injector.get(AttributeService);
|
||||||
forkJoin(
|
forkJoin(
|
||||||
@ -194,7 +211,7 @@ interface IProvider {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const providerSets: {[key: string]: IProvider} = {
|
export const providerSets: { [key: string]: IProvider } = {
|
||||||
'openstreet-map': {
|
'openstreet-map': {
|
||||||
MapClass: OpenStreetMap,
|
MapClass: OpenStreetMap,
|
||||||
schema: openstreetMapSettingsSchema,
|
schema: openstreetMapSettingsSchema,
|
||||||
|
|||||||
@ -18,10 +18,24 @@ import L from 'leaflet';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { MarkerSettings, PolylineSettings, PolygonSettings } from './map-models';
|
import { MarkerSettings, PolylineSettings, PolygonSettings } from './map-models';
|
||||||
|
|
||||||
export function createTooltip(target: L.Layer, settings: MarkerSettings | PolylineSettings | PolygonSettings): L.Popup {
|
export function createTooltip(target: L.Layer,
|
||||||
|
settings: MarkerSettings | PolylineSettings | PolygonSettings,
|
||||||
|
content?: string | HTMLElement): L.Popup {
|
||||||
const popup = L.popup();
|
const popup = L.popup();
|
||||||
popup.setContent('');
|
popup.setContent(content);
|
||||||
|
console.log(settings);
|
||||||
|
|
||||||
target.bindPopup(popup, { autoClose: settings.autocloseTooltip, closeOnClick: false });
|
target.bindPopup(popup, { autoClose: settings.autocloseTooltip, closeOnClick: false });
|
||||||
|
target.on('popupopen', () => {
|
||||||
|
let actions = document.getElementsByClassName('tb-custom-action');
|
||||||
|
Array.from(actions).forEach(
|
||||||
|
(element: HTMLElement) => {
|
||||||
|
if (element && settings.tooltipActions[element.id]) {
|
||||||
|
console.log(settings.tooltipActions[element.id]);
|
||||||
|
element.addEventListener('click', settings.tooltipActions[element.id])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
if (settings.displayTooltipAction === 'hover') {
|
if (settings.displayTooltipAction === 'hover') {
|
||||||
target.off('click');
|
target.off('click');
|
||||||
target.on('mouseover', function () {
|
target.on('mouseover', function () {
|
||||||
@ -51,5 +65,4 @@ export function getDefCenterPosition(position) {
|
|||||||
if (typeof (position) === 'object')
|
if (typeof (position) === 'object')
|
||||||
return position;
|
return position;
|
||||||
return [0, 0];
|
return [0, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,8 +43,7 @@ export class Marker {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (settings.showTooltip) {
|
if (settings.showTooltip) {
|
||||||
this.tooltip = createTooltip(this.leafletMarker, settings);
|
this.tooltip = createTooltip(this.leafletMarker, settings, parseTemplate(this.settings.tooltipPattern, data));
|
||||||
this.tooltip.setContent(parseTemplate(this.settings.tooltipPattern, data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onClickListener) {
|
if (onClickListener) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user