-
Notifications
You must be signed in to change notification settings - Fork 26
/
init.js
304 lines (270 loc) · 9.04 KB
/
init.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
( function ( M, $ ) {
var inSample, inStable, experiment,
settings = M.require( 'mobile.settings/settings' ),
util = M.require( 'mobile.startup/util' ),
time = M.require( 'mobile.modifiedBar/time' ),
token = settings.get( 'mobile-betaoptin-token' ),
BetaOptinPanel = M.require( 'mobile.betaoptin/BetaOptinPanel' ),
loader = M.require( 'mobile.overlays/moduleLoader' ),
router = M.require( 'mobile.startup/router' ),
context = M.require( 'mobile.context/context' ),
references = M.require( 'mobile.references/references' ),
cleanuptemplates = M.require( 'mobile.issues/cleanuptemplates' ),
useNewMediaViewer = context.isBetaGroupMember(),
overlayManager = M.require( 'mobile.startup/overlayManager' ),
page = M.getCurrentPage(),
SchemaMobileWebUIClickTracking = M.require(
'mobile.loggingSchemas/SchemaMobileWebUIClickTracking' ),
uiSchema = new SchemaMobileWebUIClickTracking(),
schemaMobileWebLanguageSwitcher = M.require(
'mobile.loggingSchemas/schemaMobileWebLanguageSwitcher' ),
thumbs = page.getThumbnails(),
experiments = mw.config.get( 'wgMFExperiments' ) || {},
betaOptinPanel;
/**
* Event handler for clicking on an image thumbnail
* @param {jQuery.Event} ev
* @ignore
*/
function onClickImage( ev ) {
ev.preventDefault();
router.navigate( '#/media/' + encodeURIComponent( $( this ).data( 'thumb' ).getFileName() ) );
}
/**
* Add routes to images and handle clicks
* @method
* @ignore
*/
function initMediaViewer() {
if ( !mw.config.get( 'wgImagesDisabled' ) ) {
$.each( thumbs, function ( i, thumb ) {
thumb.$el.off().data( 'thumb', thumb ).on( 'click', onClickImage );
} );
}
}
/**
* Hijack the Special:Languages link and replace it with a trigger to a LanguageOverlay
* that displays the same data
* @ignore
*/
function initButton() {
var $languageSelector = $( '#page-secondary-actions' ).find( '.languageSelector' );
/**
* Log impression when the language button is seen by the user
* @ignore
*/
function logLanguageButtonImpression() {
if ( util.isElementInViewport( $languageSelector ) ) {
M.off( 'scroll', logLanguageButtonImpression );
schemaMobileWebLanguageSwitcher.log( {
event: 'languageButtonImpression'
} );
}
}
/**
* Return the number of times the user has clicked on the language button
*
* @ignore
* @param {Number} tapCount
* @return {String}
*/
function getLanguageButtonTappedBucket( tapCount ) {
var bucket;
if ( tapCount === 0 ) {
bucket = '0';
} else if ( tapCount >= 1 && tapCount <= 4 ) {
bucket = '1-4';
} else if ( tapCount >= 5 && tapCount <= 20 ) {
bucket = '5-20';
} else if ( tapCount > 20 ) {
bucket = '20+';
}
bucket += ' taps';
return bucket;
}
if ( $languageSelector.length ) {
schemaMobileWebLanguageSwitcher.log( {
event: 'pageLoaded',
beaconCapable: $.isFunction( navigator.sendBeacon )
} );
M.on( 'scroll', logLanguageButtonImpression );
// maybe the button is already visible?
logLanguageButtonImpression();
$languageSelector.on( 'click', function ( ev ) {
var previousTapCount = settings.get( 'mobile-language-button-tap-count' ),
tapCountBucket;
ev.preventDefault();
router.navigate( '/languages' );
uiSchema.log( {
name: 'languages'
} );
// when local storage is not available or when the key has not been previously saved
if ( previousTapCount === false || previousTapCount === null ) {
tapCountBucket = 'unknown';
previousTapCount = 0;
} else {
previousTapCount = parseInt( previousTapCount, 10 );
tapCountBucket = getLanguageButtonTappedBucket( previousTapCount );
}
settings.save( 'mobile-language-button-tap-count', previousTapCount + 1 );
schemaMobileWebLanguageSwitcher.log( {
event: 'languageButtonTap',
languageButtonVersion: 'bottom-of-article',
languageButtonTappedBucket: tapCountBucket,
primaryLanguageOfUser: getDeviceLanguage() || 'unknown'
} );
} );
}
}
/**
* Return the language code of the device in lowercase
*
* @ignore
* @returns {String|undefined}
*/
function getDeviceLanguage() {
var lang = navigator && navigator.languages ?
navigator.languages[0] :
navigator.language || navigator.userLanguage ||
navigator.browserLanguage || navigator.systemLanguage;
return lang ? lang.toLowerCase() : undefined;
}
/**
* Load image overlay
* @method
* @ignore
* @uses ImageOverlay
* @param {String} title Url of image
* @returns {jQuery.Deferred}
*/
function loadImageOverlay( title ) {
var result = $.Deferred(),
rlModuleName = useNewMediaViewer ? 'mobile.mediaViewer.beta' : 'mobile.mediaViewer',
moduleName = useNewMediaViewer ? 'ImageOverlayBeta' : 'ImageOverlay';
loader.loadModule( rlModuleName ).done( function () {
var ImageOverlay = M.require( rlModuleName + '/' + moduleName );
result.resolve(
new ImageOverlay( {
api: new mw.Api(),
thumbnails: thumbs,
title: decodeURIComponent( title )
} )
);
} );
return result;
}
// Routes
overlayManager.add( /^\/media\/(.+)$/, loadImageOverlay );
overlayManager.add( /^\/languages$/, function () {
var result = $.Deferred(),
languageOverlayExperiment = experiments.languageOverlay || false,
languageOverlayModule = 'mobile.languages';
if ( languageOverlayExperiment &&
mw.experiments.getBucket( languageOverlayExperiment, mw.user.sessionId() ) === 'A'
) {
languageOverlayModule = 'mobile.languages.structured';
}
loader.loadModule( languageOverlayModule, true ).done( function ( loadingOverlay ) {
var PageGateway = M.require( 'mobile.startup/PageGateway' ),
gateway = new PageGateway( new mw.Api() ),
LanguageOverlay = M.require( languageOverlayModule + '/LanguageOverlay' );
gateway.getPageLanguages( mw.config.get( 'wgPageName' ) ).done( function ( data ) {
loadingOverlay.hide();
result.resolve( new LanguageOverlay( {
currentLanguage: mw.config.get( 'wgContentLanguage' ),
languages: data.languages,
variants: data.variants,
languageSwitcherSchema: schemaMobileWebLanguageSwitcher,
deviceLanguage: getDeviceLanguage()
} ) );
} );
} );
return result;
} );
// for Special:Uploads
M.on( 'photo-loaded', initMediaViewer );
// Setup
$( function () {
initButton();
initMediaViewer();
references.setup();
} );
// Access the beta optin experiment if available.
experiment = experiments.betaoptin || false;
// local storage is supported in this case, when ~ means it was dismissed
if ( experiment && token !== false && token !== '~' && !page.isMainPage() && !page.inNamespace( 'special' ) ) {
if ( !token ) {
token = mw.user.generateRandomSessionId();
settings.save( 'mobile-betaoptin-token', token );
}
inStable = !context.isBetaGroupMember();
inSample = mw.experiments.getBucket( experiment, token ) === 'A';
if ( inStable && ( inSample || mw.util.getParamValue( 'debug' ) ) ) {
betaOptinPanel = new BetaOptinPanel( {
postUrl: mw.util.getUrl( 'Special:MobileOptions', {
returnto: page.title
} )
} )
.on( 'hide', function () {
settings.save( 'mobile-betaoptin-token', '~' );
} )
.appendTo( M.getCurrentPage().getLeadSectionElement() );
}
}
// Setup the issues banner on the page
cleanuptemplates.init();
// Show it in edit preview.
M.on( 'edit-preview', function ( overlay ) {
cleanuptemplates.init( overlay.$el );
} );
// Remove any traces of the tag experiment in the HTML
// Remove when cache clears (https://phabricator.wikimedia.org/T113686)
$( '.browse-tags' ).remove();
// let the interested parties know whether the panel is shown
mw.track( 'minerva.betaoptin', {
isPanelShown: betaOptinPanel !== undefined
} );
/**
* Initialisation function for last modified module.
*
* Enhances an element representing a time
* to show a human friendly date in seconds, minutes, hours, days
* months or years
* @ignore
* @param {JQuery.Object} [$lastModifiedLink]
*/
function initHistoryLink( $lastModifiedLink ) {
var delta, historyUrl, msg,
ts, username, gender;
historyUrl = $lastModifiedLink.attr( 'href' );
ts = $lastModifiedLink.data( 'timestamp' );
username = $lastModifiedLink.data( 'user-name' ) || false;
gender = $lastModifiedLink.data( 'user-gender' );
if ( ts ) {
delta = time.getTimeAgoDelta( parseInt( ts, 10 ) );
if ( time.isRecent( delta ) ) {
$lastModifiedLink.closest( '.last-modified-bar' ).addClass( 'active' );
}
msg = time.getLastModifiedMessage( ts, username, gender, historyUrl );
$lastModifiedLink.replaceWith( msg );
}
}
/**
* Initialisation function for last modified times
*
* Enhances .modified-enhancement element
* to show a human friendly date in seconds, minutes, hours, days
* months or years
* @ignore
*/
function initModifiedInfo() {
$( '.modified-enhancement' ).each( function () {
initHistoryLink( $( this ) );
} );
}
$( function () {
// Update anything else that needs enhancing (e.g. watchlist)
initModifiedInfo();
initHistoryLink( $( '#mw-mf-last-modified a' ) );
} );
}( mw.mobileFrontend, jQuery ) );