-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleaflet.module
279 lines (260 loc) · 7.96 KB
/
leaflet.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* @file
* Provide Leaflet API integration and field formatters.
*/
// This file ships with the field formatter code.
require_once __DIR__ . '/leaflet.formatters.inc';
/**
* Implements hook_theme().
*/
function leaflet_theme($existing, $type, $theme, $path) {
return array(
'leaflet_map' => array(
'arguments' => array('map_id' => NULL, 'height' => '400px'),
'template' => 'leaflet_map',
),
);
}
/**
* Implements hook_library_info().
*/
function leaflet_library_info() {
$module_path = backdrop_get_path('module', 'leaflet');
$library_path = $module_path . '/libraries/leaflet/';
$plugins_path = $module_path . '/libraries/leaflet_plugins/';
$libraries['leaflet'] = array(
'title' => t('Leaflet JavaScript Library'),
'version' => '1.9.4',
'website' => 'https://leafletjs.com/',
'js' => array(
// This setting is needed in order to properly render market images.
'leaflet_root_url' => array(
'type' => 'inline',
'data' => 'L_ROOT_URL = "' . base_path() . $library_path . '";',
'group' => JS_LIBRARY,
),
$library_path . 'leaflet.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
$plugins_path . 'Leaflet.fullscreen.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
$plugins_path . 'L.Control.Zoomslider.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
$plugins_path . 'Control.Coordinates.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
$plugins_path . 'leaflet.viewcenter.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
// For AdvAgg module. See [#2294639] This runs after leaflet.js.
'leaflet_imagepath' => array(
'type' => 'inline',
'data' => 'L.Icon.Default.imagePath = "' . base_path() . $library_path . 'images/";',
),
// Load integration file AFTER libraries.
$module_path . '/js/leaflet.backdrop.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
),
),
'css' => array(
$library_path . 'leaflet.css' => array(
'type' => 'file',
'media' => 'all',
),
$plugins_path . 'leaflet.fullscreen.css' => array(
'type' => 'file',
'media' => 'all',
),
$plugins_path . 'L.Control.Zoomslider.css' => array(
'type' => 'file',
'media' => 'all',
),
$plugins_path . 'Control.Coordinates.css' => array(
'type' => 'file',
'media' => 'all',
),
$plugins_path . 'leaflet.viewcenter.css' => array(
'type' => 'file',
'media' => 'all',
),
),
);
return $libraries;
}
/**
* Attach Leaflet-required client files and return renderable array for a map.
*
* @param array $map
* Map definition as returned my leaflet_map_get_info().
* @param array $features
* Associative array of map features.
* @param string $height
* The height of the map.
*
* @return array
* A renderable array.
*/
function leaflet_build_map(array $map, array $features = array(), $height = '400px') {
// Function backdrop_html_id() is unreliable when creating unique IDs.
// We attach a pseudo random number instead to make multiple maps on a page
// work properly with maps in blocks.
$map_id = 'leaflet-map-' . rand();
$build = array(
'#theme' => 'head_tag',
'#tag' => 'div',
'#value' => '',
'#attributes' => array(
'id' => $map_id,
'style' => 'height: ' . $height,
),
);
// Allow map definitions to provide a default icon:
if (isset($map['icon']['iconUrl'])) {
foreach ($features as &$feature) {
if (!isset($feature['icon'])) {
$feature['icon'] = $map['icon'];
}
}
}
$settings = array(
'mapId' => $map_id,
'map' => $map,
// Also expose our additional (mostly plugin) controls to hook.
'mapControls' => array(
'ControlFullscreen' => TRUE,
'ControlScale' => TRUE,
'ControlZoomslider' => TRUE,
'ControlCoordinates' => TRUE,
'ControlViewCenter' => TRUE,
),
'features' => $features,
);
backdrop_alter('leaflet_map_prebuild', $settings);
$build['#attached']['js'][] = array(
'data' => array('leaflet' => array($settings)),
'type' => 'setting',
);
if (module_exists('geoip_tokens')) {
$build['#attached']['library'][] = array('geoip_tokens', 'geoip_tokens');
}
$build['#attached']['css'][] = array(
'data' => backdrop_get_path('module', 'leaflet') . '/css/leaflet_extras.css',
);
// Load the leaflet library, which includes integration files.
$build['#attached']['library'][] = array('leaflet', 'leaflet');
// Let other modules properly attach libraries as well [#2567387].
backdrop_alter('leaflet_build_map', $build);
return $build;
}
/**
* DEPRECATED. Use leaflet_build_map() instead.
*
* Load all Leaflet required client files and return markup for a map.
*
* @param array $map
* Map definition as returned my leaflet_map_get_info().
* @param array $features
* Associative array of map features.
* @param string $height
* The height of the map.
*
* @return string
* map markup
*/
function leaflet_render_map(array $map, array $features = array(), $height = '400px') {
$build = leaflet_build_map($map, $features, $height);
return render($build);
}
/**
* Get all available Leaflet map definitions.
*
* @string $map
* The name of the map defined in hook_leaflet_map_get_info().
*/
function leaflet_map_get_info($map = NULL) {
static $backdrop_static_fast;
if (!isset($backdrop_static_fast)) {
$backdrop_static_fast['leaflet_map_info'] = &backdrop_static(__FUNCTION__);
}
$map_info = &$backdrop_static_fast['leaflet_map_info'];
if (empty($map_info)) {
if ($cache = cache_get("leaflet_map_info")) {
$map_info = $cache->data;
}
else {
$map_info = module_invoke_all('leaflet_map_info');
// Let other modules alter the map info.
backdrop_alter('leaflet_map_info', $map_info);
cache_set("leaflet_map_info", $map_info);
}
}
if (empty($map)) {
return $map_info;
}
elseif (isset($map_info[$map])) {
return $map_info[$map];
}
}
/**
* Implements hook_leaflet_map_info().
*
* Return a default map for the module.
*/
function leaflet_leaflet_map_info() {
return array(
'OSM Mapnik' => array(
'label' => 'OSM Mapnik',
'description' => t('Leaflet default map.'),
// 'center' is used when map contains no features, or every time the map
// is loaded if "force" is TRUE. Otherwise, the map will center itself
// intelligently based on the features in the map.
// RdB: bad things happen when 'center' is specified and Leaflet
// MarkerCluster is used, see https://drupal.org/node/2144935
// Also, a hard-coded center is not a great idea.
// @code
// 'center' => array(
// 'lat' => 45.526513,
// 'lon' => -122.674833,
// 'force' => FALSE,
// ),
// @endcode
// @todo Create a "Default center" setting?
'settings' => array(
// Setting "zoom" forces a zoom level on every map load.
// 'zoom' => 17,
// The "zoomDefault" is only used when no features are present.
'zoomDefault' => 10,
'minZoom' => 0,
'maxZoom' => 19,
'dragging' => TRUE,
'touchZoom' => TRUE,
'scrollWheelZoom' => TRUE,
'doubleClickZoom' => TRUE,
'zoomControl' => TRUE,
'attributionControl' => TRUE,
'trackResize' => TRUE,
'fadeAnimation' => TRUE,
'zoomAnimation' => TRUE,
'closePopupOnClick' => TRUE,
),
'layers' => array(
'earth' => array(
'urlTemplate' => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
'options' => array(
'attribution' => '<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap </a> contributors',
'maxZoom' => 19,
),
),
),
),
);
}