/
staticframe.js
194 lines (168 loc) · 4.69 KB
/
staticframe.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
182
183
184
185
186
187
188
189
190
191
192
193
194
/* globals require */
/**
* Static Frame module.
*
* Once the page is loaded and ready, turn all `<mapframe/>` tags into links
* that open a full screen map.
*
* @alias ext.kartographer.staticframe
* @class Kartographer.StaticFrame
* @singleton
*/
module.exports = ( function ( $, mw, util, kartolink, router ) {
/**
* References the mapframe containers of the page.
*
* @type {HTMLElement[]}
*/
var maps = [],
/**
* @private
* @ignore
*/
routerInited = false;
/**
* Gets the map data attached to an element.
*
* @param {HTMLElement} element Element
* @return {Object|null} Map properties
* @return {number} return.latitude
* @return {number} return.longitude
* @return {number} return.zoom
* @return {string} return.style Map style
* @return {string[]} return.overlays Overlay groups
*/
function getMapData( element ) {
var $el = $( element ),
$caption = $el.parent().find( '.thumbcaption' ),
captionText = '';
// Prevent users from adding map divs directly via wikitext
if ( $el.attr( 'mw-data' ) !== 'interface' ) {
return null;
}
if ( $caption[ 0 ] ) {
captionText = $caption.text();
}
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
};
}
/**
* This code will be executed once the article is rendered and ready.
*
* @ignore
*/
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var mapsInArticle = [],
promises = [];
// `wikipage.content` may be fired more than once.
$.each( maps, function () {
maps.pop().$container.off( 'click.kartographer' );
} );
$content.find( '.mw-kartographer-map' ).each( function ( index ) {
var container = this,
$container = $( container ),
link,
data,
deferred = $.Deferred();
mw.loader.using( 'oojs-ui', function () {
var button = new OO.ui.ButtonWidget( {
icon: 'fullScreen',
title: mw.msg( 'kartographer-fullscreen-text' ),
framed: true
} ),
$div = $( '<div class="mw-kartographer-fullScreen"></div>' ).append( button.$element );
$container.append( $div );
$container.append(
'<div class="mw-kartographer-attribution">' +
mw.message( 'kartographer-attribution-short' ).parse() +
'</div>'
);
} );
$container.attr( 'href', '#/map/' + index );
data = getMapData( container );
if ( data ) {
data.enableFullScreenButton = true;
link = kartolink.link( {
featureType: 'mapframe',
container: container,
center: [ data.latitude, data.longitude ],
zoom: data.zoom,
lang: data.lang,
dataGroups: data.overlays,
captionText: data.captionText,
fullScreenRoute: '/map/' + index
} );
mw.track( 'mediawiki.kartographer', {
action: 'view',
isFullScreen: false,
feature: link
} );
link.$container.click( function () {
// We need this hack to differentiate these events from `hashopen` events.
link.clicked = true;
mw.track( 'mediawiki.kartographer', {
action: 'activate',
isFullScreen: true,
feature: link
} );
} );
mapsInArticle.push( link );
maps[ index ] = link;
promises.push( deferred.promise() );
}
} );
// Allow customizations of interactive maps in article.
$.when( promises ).then( function () {
if ( routerInited ) {
return;
}
// execute this piece of code only once
routerInited = true;
// Opens a map in full screen. #/map(/: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})?)?/, function ( maptagId, zoom, latitude, longitude ) {
var link = maps[ maptagId ],
position;
if ( !link ) {
router.navigate( '' );
return;
}
if ( zoom !== undefined && latitude !== undefined && longitude !== undefined ) {
position = {
center: [ +latitude, +longitude ],
zoom: +zoom
};
}
// We need this hack to differentiate these events from `open` events.
if ( !link.fullScreenMap && !link.clicked ) {
mw.track( 'mediawiki.kartographer', {
action: 'hashopen',
isFullScreen: true,
feature: link
} );
link.clicked = false;
}
link.openFullScreen( position );
} );
// Check if we need to open a map in full screen.
router.checkRoute();
} );
} );
return maps;
}(
jQuery,
mediaWiki,
require( 'ext.kartographer.util' ),
require( 'ext.kartographer.linkbox' ),
require( 'mediawiki.router' )
) );