-
Notifications
You must be signed in to change notification settings - Fork 7
/
mapframe.js
181 lines (156 loc) · 4.96 KB
/
mapframe.js
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
/**
* Frame module.
*
* Once the page is loaded and ready, turn all `<mapframe/>` tags into
* interactive maps.
*
* @borrows Kartographer.Frame as Frame
* @borrows Kartographer.Frame as ext.kartographer.frame
* @class Kartographer.Frame
* @singleton
*/
const util = require( 'ext.kartographer.util' );
const kartobox = require( 'ext.kartographer.box' );
const router = require( 'mediawiki.router' );
/**
* References the mapframe containers of the page.
*
* @type {HTMLElement[]}
*/
const maps = [];
/**
* @private
*/
let routerInited = false;
/**
* Gets the map data attached to an element.
*
* @param {HTMLElement} element Element
* @return {Object} Map properties
* @return {number} return.latitude
* @return {number} return.longitude
* @return {number} return.zoom
* @return {string} return.lang Language code
* @return {string} return.style Map style
* @return {string[]} return.overlays Overlay groups
* @return {string} return.captionText
*/
function getMapData( element ) {
const $el = $( element );
const $caption = $el.parent().find( '.thumbcaption' );
let captionText = '';
if ( $caption[ 0 ] ) {
captionText = $caption.get( 0 ).innerText;
}
return {
latitude: +$el.data( 'lat' ),
longitude: +$el.data( 'lon' ),
zoom: +$el.data( 'zoom' ),
lang: $el.data( 'lang' ) || util.getDefaultLanguage(),
style: $el.data( 'style' ),
overlays: $el.data( 'overlays' ) || [],
captionText: captionText
};
}
/**
* @param {Object} data
* @param {jQuery} $container
* @return {Object} map KartographerMap
*/
function initMapBox( data, $container ) {
const index = maps.length;
data.enableFullScreenButton = true;
const map = kartobox.map( {
featureType: 'mapframe',
container: $container.get( 0 ),
center: [ data.latitude, data.longitude ],
zoom: data.zoom,
lang: data.lang,
style: data.style,
fullScreenRoute: '/map/' + index,
allowFullScreen: true,
dataGroups: data.overlays,
captionText: data.captionText,
alwaysStatic: mw.config.get( 'wgKartographerStaticMapframePreview' )
} );
$container.removeAttr( 'href' );
$container.find( 'img' ).remove();
maps[ index ] = map;
// Special case for collapsed maps.
// When the container is initially hidden Leaflet is not able to
// calculate the expected size when visible. We need to force
// updating the map to the new container size on `expand`.
// eslint-disable-next-line no-jquery/no-sizzle
if ( !$container.is( ':visible' ) ) {
$container.closest( '.mw-collapsible' )
.on( 'afterExpand.mw-collapsible', map.invalidateSizeAndSetInitialView.bind( map ) );
// If MobileFrontend is active do the same for collapsible sections
// Unfortunately doesn't work when those sections are immediately
// made visible again on page load.
mw.hook( 'mobileFrontend.section-toggled' ).add( map.invalidateSizeAndSetInitialView.bind( map ) );
}
return map;
}
/**
* Create a mapbox from a given element.
*
* @param {HTMLElement} element Parsed <mapframe> element
*/
function initMapframeFromElement( element ) {
const map = initMapBox( getMapData( element ), $( element ) );
mw.hook( 'wikipage.maps' ).fire( [ map ], false /* isFullScreen */ );
}
/**
* This code will be executed once the article is rendered and ready.
*/
mw.hook( 'wikipage.content' ).add( ( $content ) => {
// `wikipage.content` may be fired more than once.
while ( maps.length ) {
maps.pop().remove();
}
// Wait for next draw cycle, so that the JS map is not initialized before it is on screen.
// If initialized too early leaflet uses incorrect dimensions. phab:T151524
// https://github.com/Leaflet/Leaflet/issues/4200
requestAnimationFrame( () => {
// Attributes starting with "data-mw" are banned from user content in Sanitizer; checking for their presence
// guarantees that they were generated by Kartographer
$content.find( '.mw-kartographer-map[data-mw-kartographer]' )
.each( ( i, el ) => {
initMapBox( getMapData( el ), $( el ) );
} );
// Allow customizations of interactive maps in article.
mw.hook( 'wikipage.maps' ).fire( maps, false /* isFullScreen */ );
if ( routerInited ) {
return;
}
// execute this piece of code only once
routerInited = true;
// Opens a map in full screen. #/map/:id(/:zoom)(/:latitude)(/:longitude)
// Examples:
// #/map/0
// #/map/0/5
// #/map/0/16/-122.4006/37.7873
router.route( /map\/([0-9]+)(?:\/([0-9]+))?(?:\/([+-]?\d+\.?\d{0,5})?\/([+-]?\d+\.?\d{0,5})?)?/, ( maptagId, zoom, latitude, longitude ) => {
const map = maps[ maptagId ];
if ( !map ) {
router.navigate( '' );
return;
}
let position;
if ( zoom !== undefined && latitude !== undefined && longitude !== undefined ) {
position = {
center: [ +latitude, +longitude ],
zoom: +zoom
};
} else {
position = map.getInitialMapPosition();
}
map.openFullScreen( position );
} );
// Check if we need to open a map in full screen.
router.checkRoute();
} );
} );
module.exports = {
initMapframeFromElement: initMapframeFromElement
};