1
+ ( function ( ) {
2
+ "use strict" ;
3
+
4
+ function leafletmapController ( $scope , $q , LeafletMapFactory ) {
5
+
6
+ var leafLetAccessToken = $scope . model . config . accessToken ;
7
+ var mapStarted = false ;
8
+ var myMap = null ;
9
+ var mapMarkers = [ ] ;
10
+
11
+ $scope . model . value = $scope . model . value || { latLng : [ ] } ;
12
+ $scope . mapId = "leaflet-map-" + $scope . $id ;
13
+
14
+ function isMapElementVisible ( ) {
15
+ return angular . element ( "#" + $scope . mapId ) . is ( ":visible" ) ;
16
+ }
17
+
18
+ function dragMarker ( e ) {
19
+ $scope . model . value . latLng = [ e . target . _latlng . lat , e . target . _latlng . lng ] ;
20
+ }
21
+
22
+ function showMapContextMenu ( e ) {
23
+ $scope . ctxLat = e . latlng . lat ;
24
+ $scope . ctxLon = e . latlng . lng ;
25
+
26
+ var ctxMenu = angular . element ( "#" + $scope . mapId ) . parent ( ) . find ( ".map-context-menu" ) [ 0 ] ;
27
+ ctxMenu . style . display = "block" ;
28
+ ctxMenu . style . left = ( event . pageX ) + "px" ;
29
+ ctxMenu . style . top = ( event . pageY ) + "px" ;
30
+ }
31
+
32
+ function hideMapContextMenu ( ) {
33
+ $scope . ctxLat = null ;
34
+ $scope . ctxLon = null ;
35
+
36
+ var ctxMenu = angular . element ( "#" + $scope . mapId ) . parent ( ) . find ( ".map-context-menu" ) [ 0 ] ;
37
+ ctxMenu . style . display = "none" ;
38
+ }
39
+
40
+ function onZoomEnd ( e ) {
41
+ hideMapContextMenu ( ) ;
42
+ $scope . model . value . zoom = e . target . _zoom ;
43
+ }
44
+
45
+ function attemptDrawMap ( ) {
46
+ if ( ! mapStarted ) {
47
+ drawMap ( ) . then ( function ( ) {
48
+ mapStarted = true ;
49
+ } ,
50
+ function ( e ) {
51
+ console . log ( e ) ;
52
+ } ) ;
53
+ } else {
54
+ resetMap ( ) ;
55
+ }
56
+ }
57
+
58
+ function drawMap ( ) {
59
+ return $q ( function ( resolve , reject ) {
60
+ var lat = $scope . model . value . latLng [ 0 ] ;
61
+ var lng = $scope . model . value . latLng [ 1 ] ;
62
+ var zoom = $scope . model . value . zoom ;
63
+ var placeMarker = true ;
64
+
65
+ if ( isNaN ( lat ) ) {
66
+ lat = $scope . model . config . defaultLat || 0 ;
67
+ placeMarker = false ;
68
+ }
69
+ if ( isNaN ( lng ) ) {
70
+ lng = $scope . model . config . defaultLng || 0 ;
71
+ placeMarker = false ;
72
+ }
73
+ if ( isNaN ( zoom ) ) {
74
+ zoom = $scope . model . config . defaultZoom || 2 ;
75
+ }
76
+
77
+ try {
78
+ myMap = L . map ( $scope . mapId ) . setView ( [ lat , lng ] , zoom ) ;
79
+ myMap . on ( "contextmenu" , showMapContextMenu ) ;
80
+ myMap . on ( "click" , hideMapContextMenu ) ;
81
+ myMap . on ( "zoomend" , onZoomEnd ) ;
82
+ L . tileLayer (
83
+ "https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}" ,
84
+ {
85
+ attribution :
86
+ 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>' ,
87
+ maxZoom : 18 ,
88
+ id : "mapbox/streets-v11" ,
89
+ tileSize : 512 ,
90
+ zoomOffset : - 1 ,
91
+ accessToken : leafLetAccessToken
92
+ } ) . addTo ( myMap ) ;
93
+
94
+ if ( placeMarker ) {
95
+ var marker = L . marker ( [ lat , lng ] , { draggable : true } ) . addTo ( myMap ) ;
96
+ marker . on ( "dragend" , dragMarker ) ;
97
+ mapMarkers = [ marker ] ;
98
+ }
99
+
100
+ return resolve ( ) ;
101
+ } catch ( e ) {
102
+ return reject ( e ) ;
103
+ }
104
+ } ) ;
105
+ }
106
+
107
+ function resetMap ( ) {
108
+ // clear markers
109
+ for ( var i = 0 ; i < mapMarkers . length ; i ++ ) {
110
+ myMap . removeLayer ( mapMarkers [ i ] ) ;
111
+ }
112
+
113
+ // recenter map and add new marker
114
+ var latLng = $scope . model . value . latLng ;
115
+
116
+ if ( latLng && ! isNaN ( latLng [ 0 ] ) && ! isNaN ( latLng [ 1 ] ) ) {
117
+ myMap . setView ( latLng , myMap . _zoom ) ;
118
+ var marker = L . marker ( latLng , { draggable : true } ) . addTo ( myMap ) ;
119
+ marker . on ( "dragend" , dragMarker ) ;
120
+ mapMarkers = [ marker ] ;
121
+ }
122
+ }
123
+
124
+ function initMaps ( ) {
125
+ LeafletMapFactory . initializeMaps ( )
126
+ . then ( function ( ) {
127
+ $scope . $watch ( isMapElementVisible , function ( newVal ) {
128
+ if ( newVal === true ) {
129
+ attemptDrawMap ( ) ;
130
+ }
131
+ } ) ;
132
+ $scope . $watch ( "model.value.latLng" ,
133
+ function ( newVal ) {
134
+ if ( isMapElementVisible ( ) ) {
135
+ attemptDrawMap ( ) ;
136
+ }
137
+ } ,
138
+ true ) ;
139
+ } ,
140
+ function ( ) {
141
+ console . log ( "Failed to load map resources." ) ;
142
+ } ) ;
143
+ }
144
+
145
+ $scope . setContextMarker = function ( ) {
146
+ $scope . model . value . latLng = [ $scope . ctxLat , $scope . ctxLon ] ;
147
+ hideMapContextMenu ( ) ;
148
+ } ;
149
+
150
+ initMaps ( ) ;
151
+ }
152
+
153
+ angular . module ( "umbraco" ) . controller ( "LeafletMap.Controller" , leafletmapController ) ;
154
+
155
+ } ) ( ) ;
0 commit comments