/* global google*/ angular.module('fasit') .directive('fbMapMultipleGoogleMaps', ['mapService', 'fbGoogleMapsService', '$timeout', function ( mapService: fb.IMapService, fbGoogleMapsService: any, $timeout: ng.ITimeoutService ) { 'use strict'; var mapElement: HTMLElement; var lastKnownLocations: any[] = []; /*istanbul ignore next*/ function link(scope: fb.IFbMapMultipleScope, element: fb.IRootElementService, attrs: ng.IAttributes) { let centrePositionObject: fb.MapCenteringObject = mapService.getInitialCentrePosition(scope.locations); mapElement = element.find('#googleMap')[0]; const centerCoordinates: { Latitud: fb.ChangeTrack; Longitud: fb.ChangeTrack; } = { Latitud: new fb.ChangeTrack(centrePositionObject.latitude), Longitud: new fb.ChangeTrack(centrePositionObject.longitude) }; const map: any = fbGoogleMapsService.getMapElement(centerCoordinates, 'stor'); fbGoogleMapsService.onMapResize(map); let mapInitialisationParams: fb.IMapServiceInitialisationParams = { mapElement: map.element, centreCoordinates: centrePositionObject, mapType: 'google', scope: scope }; let mapsInstance: google.maps.Map = map.mapsInstance; map.element.className = 'fb-map fb-map-google'; mapElement.parentElement.replaceChild(map.element, mapElement); scope.$watch('locations', function (newVal: any[], oldVal: any[]) { google.maps.event.trigger(map.mapsInstance, 'resize'); google.maps.event.trigger(map, 'resize'); map.mapsInstance.setCenter(new google.maps.LatLng(0.0, 0.0)); if (oldVal.length > 0) { mapService.unsetMarkers(map.mapsInstance, oldVal, 'google'); } if (newVal.length > 0) { var mapInitialisationParams: fb.IMapServiceInitialisationParams; var centrePositionObject = mapService.getInitialCentrePosition(newVal); mapInitialisationParams = { mapElement: mapElement, centreCoordinates: centrePositionObject, mapType: 'google', scope: scope }; if (lastKnownLocations && lastKnownLocations.length > 0) { mapService.unsetMarkers(map.mapsInstance, scope.locations, 'google'); lastKnownLocations.splice(0, lastKnownLocations.length); } if (scope.show) { mapService.setMarkers(map.mapsInstance, newVal, mapInitialisationParams); newVal.forEach(loc => lastKnownLocations.push(loc)); } } }); scope.$watch('show', function (newVal, oldVal) { if (newVal !== oldVal) { if (newVal) { $timeout(function () { google.maps.event.trigger(map.mapsInstance, 'resize'); mapService.setMarkers(map.mapsInstance, scope.locations, { mapType: 'google' }); scope.locations.forEach(loc => lastKnownLocations.push(loc)); $timeout(function () { map.element.style.visibility = 'visible'; }); }); } else { map.element.style.visibility = 'hidden'; } } }); if (!map) { return; } else { mapService.setMapHeight(scope.height, map.element); google.maps.event.trigger(map.mapsInstance, 'resize'); mapService.setMarkers(map.mapsInstance, scope.locations, mapInitialisationParams); } scope.$on('$destroy', () => { map.isInUse = false; mapService.unsetMarkers(map.mapsInstance, scope.locations, 'google'); if (lastKnownLocations && lastKnownLocations.length > 0) { mapService.unsetMarkers(map.mapsInstance, lastKnownLocations, 'google'); lastKnownLocations.splice(0, lastKnownLocations.length); } }); } function compile(element: fb.IRootElementService, attrs: any, linker: any) { return link; } return { restrict: 'E', scope: { markercoords: '=', centercoords: '=', show: '=', height: '@', locations: '=', storlek: '=' }, compile: compile, templateUrl: 'app/Directives/fbMapMultipleGoogleMaps/fbMapMultipleGoogleMaps.html' }; }]);