thingsboard-aplcache

fixed label/tooltip updating

11/21/2018 1:58:42 PM

Details

diff --git a/ui/src/app/widget/lib/google-map.js b/ui/src/app/widget/lib/google-map.js
index 2fe7ce4..ccf931a 100644
--- a/ui/src/app/widget/lib/google-map.js
+++ b/ui/src/app/widget/lib/google-map.js
@@ -221,7 +221,7 @@ export default class TbGoogleMap {
     /* eslint-enable no-undef */
 
     /* eslint-disable no-undef */
-    createMarker(location, settings, onClickListener, markerArgs) {
+    createMarker(location, dsIndex, settings, onClickListener, markerArgs) {
         var marker;
         if (settings.showLabel) {
             marker = new MarkerWithLabel({
@@ -244,7 +244,7 @@ export default class TbGoogleMap {
         });
 
         if (settings.displayTooltip) {
-            this.createTooltip(marker, settings.tooltipPattern, settings.tooltipReplaceInfo, settings.autocloseTooltip, markerArgs);
+            this.createTooltip(marker, dsIndex, settings, markerArgs);
         }
 
         if (onClickListener) {
@@ -261,13 +261,13 @@ export default class TbGoogleMap {
     /* eslint-enable no-undef */
 
     /* eslint-disable no-undef */
-    createTooltip(marker, pattern, replaceInfo, autoClose, markerArgs) {
+    createTooltip(marker, dsIndex, settings, markerArgs) {
         var popup = new google.maps.InfoWindow({
             content: ''
         });
         var map = this;
         marker.addListener('click', function() {
-            if (autoClose) {
+            if (settings.autocloseTooltip) {
                 map.tooltips.forEach((tooltip) => {
                     tooltip.popup.close();
                 });
@@ -277,8 +277,8 @@ export default class TbGoogleMap {
         this.tooltips.push( {
             markerArgs: markerArgs,
             popup: popup,
-            pattern: pattern,
-            replaceInfo: replaceInfo
+            locationSettings: settings,
+            dsIndex: dsIndex
         });
     }
     /* eslint-enable no-undef */
diff --git a/ui/src/app/widget/lib/image-map.js b/ui/src/app/widget/lib/image-map.js
index 18e6ed0..74f5841 100644
--- a/ui/src/app/widget/lib/image-map.js
+++ b/ui/src/app/widget/lib/image-map.js
@@ -298,7 +298,7 @@ export default class TbImageMap {
         onMarkerIconReady(iconInfo);
     }
 
-    createMarker(position, settings, onClickListener, markerArgs) {
+    createMarker(position, dsIndex, settings, onClickListener, markerArgs) {
         var pos = this.posFunction(position.x, position.y);
         var x = pos.x * this.width;
         var y = pos.y * this.height;
@@ -319,7 +319,7 @@ export default class TbImageMap {
         });
 
         if (settings.displayTooltip) {
-            this.createTooltip(marker, settings.tooltipPattern, settings.tooltipReplaceInfo, settings.autocloseTooltip, markerArgs);
+            this.createTooltip(marker, dsIndex, settings, markerArgs);
         }
 
         if (onClickListener) {
@@ -348,15 +348,15 @@ export default class TbImageMap {
         }
     }
 
-    createTooltip(marker, pattern, replaceInfo, autoClose, markerArgs) {
+    createTooltip(marker, dsIndex, settings, markerArgs) {
         var popup = L.popup();
         popup.setContent('');
-        marker.bindPopup(popup, {autoClose: autoClose, closeOnClick: false});
+        marker.bindPopup(popup, {autoClose: settings.autocloseTooltip, closeOnClick: false});
         this.tooltips.push( {
             markerArgs: markerArgs,
             popup: popup,
-            pattern: pattern,
-            replaceInfo: replaceInfo
+            locationSettings: settings,
+            dsIndex: dsIndex
         });
     }
 
diff --git a/ui/src/app/widget/lib/map-widget2.js b/ui/src/app/widget/lib/map-widget2.js
index 5e2dac2..0ed70c5 100644
--- a/ui/src/app/widget/lib/map-widget2.js
+++ b/ui/src/app/widget/lib/map-widget2.js
@@ -210,14 +210,30 @@ export default class TbMapWidgetV2 {
 
         var tbMap = this;
 
-        function updateLocationLabel(location) {
-            if (location.settings.showLabel && location.settings.labelReplaceInfo.variables.length) {
-                location.settings.labelText = fillPattern(location.settings.label,
-                    location.settings.labelReplaceInfo, tbMap.subscription.data);
+        function updateLocationLabel(location, dataMap) {
+            if (location.settings.showLabel) {
+                if (location.settings.useLabelFunction && location.settings.labelFunction) {
+                    try {
+                        location.settings.label = location.settings.labelFunction(dataMap.dataMap, dataMap.dsDataMap, location.dsIndex);
+                    } catch (e) {
+                        location.settings.label = null;
+                    }
+                    if (location.settings.label) {
+                        var datasources = tbMap.subscription.datasources;
+                        location.settings.label = tbMap.utils.createLabelFromDatasource(datasources[location.dsIndex], location.settings.label);
+                        location.settings.labelReplaceInfo = processPattern(location.settings.label, datasources, location.dsIndex);
+                        location.settings.labelText = location.settings.label;
+                    }
+                }
+                if (location.settings.labelReplaceInfo.variables.length) {
+                    location.settings.labelText = fillPattern(location.settings.label,
+                        location.settings.labelReplaceInfo, tbMap.subscription.data);
+                }
                 tbMap.map.updateMarkerLabel(location.marker, location.settings);
             }
         }
 
+
         function calculateLocationColor(location, dataMap) {
             if (location.settings.useColorFunction && location.settings.colorFunction) {
                 var color;
@@ -267,7 +283,7 @@ export default class TbMapWidgetV2 {
         }
 
         function updateLocationStyle(location, dataMap) {
-            updateLocationLabel(location);
+            updateLocationLabel(location, dataMap);
             var color = calculateLocationColor(location, dataMap);
             var image = calculateLocationMarkerImage(location, dataMap);
             updateLocationColor(location, color, image);
@@ -281,7 +297,7 @@ export default class TbMapWidgetV2 {
                 if (image && (!location.settings.currentImage || !angular.equals(location.settings.currentImage, image))) {
                     location.settings.currentImage = image;
                 }
-                location.marker = tbMap.map.createMarker(markerLocation, location.settings,
+                location.marker = tbMap.map.createMarker(markerLocation, location.dsIndex, location.settings,
                     function (event) {
                         tbMap.callbacks.onLocationClick(location);
                         locationRowClick(event, location);
@@ -401,25 +417,11 @@ export default class TbMapWidgetV2 {
                         settings: angular.copy(tbMap.locationSettings)
                     };
                     if (location.settings.showLabel) {
-                        if (location.settings.useLabelFunction && location.settings.labelFunction) {
-                            try {
-                                location.settings.label = location.settings.labelFunction(dataMap.dataMap, dataMap.dsDataMap, location.dsIndex);
-                            } catch (e) {
-                                location.settings.label = null;
-                            }
-                        }
                         location.settings.label = tbMap.utils.createLabelFromDatasource(currentDatasource, location.settings.label);
                         location.settings.labelReplaceInfo = processPattern(location.settings.label, datasources, currentDatasourceIndex);
                         location.settings.labelText = location.settings.label;
                     }
                     if (location.settings.displayTooltip) {
-                        if (location.settings.useTooltipFunction && location.settings.tooltipFunction) {
-                            try {
-                                location.settings.tooltipPattern = location.settings.tooltipFunction(dataMap.dataMap, dataMap.dsDataMap, location.dsIndex);
-                            } catch (e) {
-                                location.settings.tooltipPattern = null;
-                            }
-                        }
                         location.settings.tooltipPattern = tbMap.utils.createLabelFromDatasource(currentDatasource, location.settings.tooltipPattern);
                         location.settings.tooltipReplaceInfo = processPattern(location.settings.tooltipPattern, datasources, currentDatasourceIndex);
                     }
@@ -457,6 +459,25 @@ export default class TbMapWidgetV2 {
             }
         }
 
+        function createTooltipContent(tooltip, data, datasources) {
+            var content;
+            var settings = tooltip.locationSettings;
+            if (settings.useTooltipFunction && settings.tooltipFunction) {
+                var dataMap = toLabelValueMap(data, datasources);
+                try {
+                    settings.tooltipPattern = settings.tooltipFunction(dataMap.dataMap, dataMap.dsDataMap, tooltip.dsIndex);
+                } catch (e) {
+                    settings.tooltipPattern = null;
+                }
+                if (settings.tooltipPattern) {
+                    settings.tooltipPattern = tbMap.utils.createLabelFromDatasource(datasources[tooltip.dsIndex], settings.tooltipPattern);
+                    settings.tooltipReplaceInfo = processPattern(settings.tooltipPattern, datasources, tooltip.dsIndex);
+                }
+            }
+            content = fillPattern(settings.tooltipPattern, settings.tooltipReplaceInfo, data);
+            return fillPatternWithActions(content, 'onTooltipAction', tooltip.markerArgs);
+        }
+
         if (this.map && this.map.inited() && this.subscription) {
             if (this.subscription.data) {
                 if (!this.locations) {
@@ -465,10 +486,9 @@ export default class TbMapWidgetV2 {
                     updateLocations(this.subscription.data, this.subscription.datasources);
                 }
                 var tooltips = this.map.getTooltips();
-                for (var t=0; t < tooltips.length; t++) {
+                for (var t = 0; t < tooltips.length; t++) {
                     var tooltip = tooltips[t];
-                    var text = fillPattern(tooltip.pattern, tooltip.replaceInfo, this.subscription.data);
-                    text = fillPatternWithActions(text, 'onTooltipAction', tooltip.markerArgs);
+                    var text = createTooltipContent(tooltip, this.subscription.data, this.subscription.datasources);
                     tooltip.popup.setContent(text);
                 }
             }
diff --git a/ui/src/app/widget/lib/openstreet-map.js b/ui/src/app/widget/lib/openstreet-map.js
index 2d5f959..2f9c1bc 100644
--- a/ui/src/app/widget/lib/openstreet-map.js
+++ b/ui/src/app/widget/lib/openstreet-map.js
@@ -126,7 +126,7 @@ export default class TbOpenStreetMap {
         onMarkerIconReady(iconInfo);
     }
 
-    createMarker(location, settings, onClickListener, markerArgs) {
+    createMarker(location, dsIndex, settings, onClickListener, markerArgs) {
         var marker = L.marker(location, {});
         var opMap = this;
         this.createMarkerIcon(marker, settings, (iconInfo) => {
@@ -140,7 +140,7 @@ export default class TbOpenStreetMap {
         });
 
         if (settings.displayTooltip) {
-            this.createTooltip(marker, settings.tooltipPattern, settings.tooltipReplaceInfo, settings.autocloseTooltip, markerArgs);
+            this.createTooltip(marker, dsIndex, settings, markerArgs);
         }
 
         if (onClickListener) {
@@ -154,15 +154,15 @@ export default class TbOpenStreetMap {
         this.map.removeLayer(marker);
     }
 
-    createTooltip(marker, pattern, replaceInfo, autoClose, markerArgs) {
+    createTooltip(marker, dsIndex, settings, markerArgs) {
         var popup = L.popup();
         popup.setContent('');
-        marker.bindPopup(popup, {autoClose: autoClose, closeOnClick: false});
+        marker.bindPopup(popup, {autoClose: settings.autocloseTooltip, closeOnClick: false});
         this.tooltips.push( {
             markerArgs: markerArgs,
             popup: popup,
-            pattern: pattern,
-            replaceInfo: replaceInfo
+            locationSettings: settings,
+            dsIndex: dsIndex
         });
     }
 
diff --git a/ui/src/app/widget/lib/tencent-map.js b/ui/src/app/widget/lib/tencent-map.js
index e0a9c5c..94fc43a 100644
--- a/ui/src/app/widget/lib/tencent-map.js
+++ b/ui/src/app/widget/lib/tencent-map.js
@@ -224,7 +224,7 @@ export default class TbTencentMap {
     /* eslint-enable no-undef */
 
     /* eslint-disable no-undef */
-    createMarker(location, settings, onClickListener, markerArgs) {
+    createMarker(location, dsIndex, settings, onClickListener, markerArgs) {
         var marker = new qq.maps.Marker({
             position: location
         });
@@ -247,7 +247,7 @@ export default class TbTencentMap {
         });
 
         if (settings.displayTooltip) {
-            this.createTooltip(marker, settings.tooltipPattern, settings.tooltipReplaceInfo, settings.autocloseTooltip, markerArgs);
+            this.createTooltip(marker, dsIndex, settings, markerArgs);
         }
 
         if (onClickListener) {
@@ -268,13 +268,13 @@ export default class TbTencentMap {
     /* eslint-enable no-undef */
 
     /* eslint-disable no-undef */
-    createTooltip(marker, pattern, replaceInfo, autoClose, markerArgs) {
+    createTooltip(marker, dsIndex, settings, markerArgs) {
         var popup = new qq.maps.InfoWindow({
             map :this.map
         });
         var map = this;
         qq.maps.event.addListener(marker, 'click', function() {
-            if (autoClose) {
+            if (settings.autocloseTooltip) {
                 map.tooltips.forEach((tooltip) => {
                     tooltip.popup.close();
                 });
@@ -285,8 +285,8 @@ export default class TbTencentMap {
         this.tooltips.push( {
             markerArgs: markerArgs,
             popup: popup,
-            pattern: pattern,
-            replaceInfo: replaceInfo
+            locationSettings: settings,
+            dsIndex: dsIndex
         });
     }
     /* eslint-enable no-undef */