summaryrefslogtreecommitdiff
blob: 265fa48b08068d89d4d03530cca47b9b9656daac (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
 * ULS interface integration logic
 *
 * Copyright (C) 2012-2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
 * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
 * contributors. See CREDITS for a list.
 *
 * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
 * have to do anything special to choose one license or the other and you don't
 * have to notify anyone which license you are using. You are free to use
 * UniversalLanguageSelector in commercial projects as long as the copyright
 * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
 *
 * @file
 * @ingroup Extensions
 * @licence GNU General Public Licence 2.0 or later
 * @licence MIT License
 */
( function ( $, mw ) {
	'use strict';

	/**
	 * Construct the display settings link
	 * @return {jQuery}
	 */
	function displaySettings() {
		var $displaySettingsTitle, displaySettingsText, $displaySettings;

		displaySettingsText = $.i18n( 'ext-uls-display-settings-desc' );
		$displaySettingsTitle = $( '<div data-i18n="ext-uls-display-settings-title">' )
			.addClass( 'settings-title' )
			.attr( 'title', displaySettingsText );
		$displaySettings = $( '<div>' )
			.addClass( 'display-settings-block' )
			.prop( 'id', 'display-settings-block' )
			.append( $displaySettingsTitle.i18n() );

		return $displaySettings;
	}

	/**
	 * Construct the input settings link
	 * @returns {jQuery}
	 */
	function inputSettings() {
		var $inputSettingsTitle, inputSettingsText, $inputSettings;

		inputSettingsText = $.i18n( 'ext-uls-input-settings-desc' );
		$inputSettingsTitle = $( '<div data-i18n="ext-uls-input-settings-title">' )
			.addClass( 'settings-title' )
			.attr( 'title', inputSettingsText );
		$inputSettings = $( '<div>' )
			.addClass( 'input-settings-block' )
			.prop( 'id', 'input-settings-block' )
			.append( $inputSettingsTitle.i18n() );

		return $inputSettings;
	}

	/**
	 * Add display settings link to the settings bar in ULS
	 * @param {Object} uls The ULS object
	 */
	function addDisplaySettings( uls ) {
		var $displaySettings = displaySettings();

		uls.$menu.find( '#uls-settings-block' ).append( $displaySettings );
		$displaySettings.on( 'click', function () {
			var languagesettings = $displaySettings.data( 'languagesettings' ),
				displaySettingsOptions = {
					defaultModule: 'display'
				},
				ulsPosition = mw.config.get( 'wgULSPosition' ),
				anonMode = ( mw.user.isAnon() &&
					!mw.config.get( 'wgULSAnonCanChangeLanguage' ) );

			if ( !languagesettings ) {
				// If the ULS trigger is shown in the top personal menu,
				// closing the display settings must show the main ULS
				// languages list, unless we are in anon mode and thus
				// cannot show the language list
				if ( ulsPosition === 'personal' && !anonMode ) {
					displaySettingsOptions.onClose = function () {
						uls.show();
					};
				}
				$.extend( displaySettingsOptions, uls.position() );
				mw.loader.using( mw.uls.languageSettingsModules, function () {
					$displaySettings.languagesettings( displaySettingsOptions )
						.click();
				} );
			}
			mw.hook( 'mw.uls.settings.open' ).fire( 'uls' );
			uls.hide();
		} );
	}

	/**
	 * Add input settings link to the settings bar in ULS
	 * @param {Object} uls The ULS object
	 */
	function addInputSettings( uls ) {
		var $inputSettings = inputSettings();

		uls.$menu.find( '#uls-settings-block' ).append( $inputSettings );
		$inputSettings.on( 'click', function () {
			var position = uls.position(),
				languagesettings = $inputSettings.data( 'languagesettings' );

			if ( !languagesettings ) {
				mw.loader.using( mw.uls.languageSettingsModules, function () {
					$inputSettings.languagesettings( {
						defaultModule: 'input',
						onClose: function () {
							uls.show();
						},
						top: position.top,
						left: position.left
					} ).click();
				} );
			}

			mw.hook( 'mw.uls.settings.open' ).fire( 'uls' );
			uls.hide();
		} );
	}

	/**
	 * Helper function to make the uls triggers accessible with the keyboard.
	 * @param {jQuery} $target One or more jQuery elements.
	 * @since 2013.07
	 */
	function addAccessibilityFeatures( $target ) {
		// tabindex=0 makes it appear when tabbing targets.
		// See also http://www.w3.org/TR/wai-aria/roles#button
		$target.attr( {
			tabIndex: 0,
			role: 'button',
			'aria-haspopup': true
		} );
		// TODO:
		// * aria-pressed true/false when popup is open
		// * aria-controls to reference to the popup

		// Remove outline when clicking
		$target.click( function () {
			$( this ).css( 'outline', 'none' );
		} );
		// Allow outline to appear again if keyboard activated
		$target.blur( function () {
			$( this ).css( 'outline', '' );
		} );

		// Make Enter act the same as clicking. This has the unfortunate side
		// effect of removing the outline.
		$target.keydown( function ( event ) {
			// Enter
			if ( event.keyCode === 13 ) {
				$( this ).click();
				event.preventDefault();
				event.stopPropagation();
			}
		} );
	}

	/**
	 * The tooltip to be shown when language changed using ULS.
	 * It also allows to undo the language selection.
	 */
	function showULSTooltip() {
		var ulsPosition = mw.config.get( 'wgULSPosition' ),
			currentLang = mw.config.get( 'wgUserLanguage' ),
			previousLang,
			previousLanguageAutonym,
			$ulsTrigger,
			anonMode,
			rtlPage = $( 'body' ).hasClass( 'rtl' ),
			tipsyGravity = {
				personal: 'n',
				interlanguage: rtlPage ? 'e' : 'w'
			},
			previousLanguages = mw.uls.getPreviousLanguages() || [];

		previousLang = previousLanguages.slice( -1 )[0];

		$ulsTrigger = ( ulsPosition === 'interlanguage' ) ?
			$( '.uls-settings-trigger' ) :
			$( '.uls-trigger' );

		if ( previousLang === currentLang ) {
			$ulsTrigger.tipsy( { gravity: rtlPage ? 'e' : 'w' } );

			return;
		}

		previousLanguages.push( currentLang );
		mw.uls.setPreviousLanguages( previousLanguages );

		anonMode = ( mw.user.isAnon() && !mw.config.get( 'wgULSAnonCanChangeLanguage' ) );

		if ( anonMode || !previousLang ) {
			// Do not show tooltip
			return;
		}

		previousLanguageAutonym = $.cookie( mw.uls.previousLanguageAutonymCookie ) ||
			previousLang;

		// Attach a tipsy tooltip to the trigger
		$ulsTrigger.tipsy( {
			gravity: tipsyGravity[ulsPosition],
			delayOut: 3000,
			html: true,
			fade: true,
			trigger: 'manual',
			title: function () {
				var link;

				link = $( '<a>' ).text( previousLanguageAutonym )
					.attr( {
						href: '#',
						'class': 'uls-prevlang-link',
						lang: previousLang,
						// We could get dir from uls.data,
						// but we are trying to avoid loading it
						// and 'auto' is safe enough in this context
						dir: 'auto'
					} );

				// Get the html of the link by wrapping it in div first
				link = $( '<div>' ).html( link ).html();

				return mw.msg( 'ext-uls-undo-language-tooltip-text', link );
			}
		} );

		// Now that we set the previous languages,
		// we can set the cookie of the previous autonym.
		// TODO: Refactor this, because it doesn't directly belong
		// to the tooltip.
		$.cookie( mw.uls.previousLanguageAutonymCookie,
			mw.config.get( 'wgULSCurrentAutonym' ),
			{ path: '/' }
		);

		function showTipsy( timeout ) {
			var tipsyTimer = 0;

			$ulsTrigger.tipsy( 'show' );
			// if the mouse is over the tooltip, do not hide
			$( '.tipsy' ).on( 'mouseover', function () {
				window.clearTimeout( tipsyTimer );
			} );
			$( '.tipsy' ).on( 'mouseout', function () {
				tipsyTimer = window.setTimeout( hideTipsy, timeout );
			} );

			// hide the tooltip when clicked on it
			$( '.tipsy' ).on( 'click', hideTipsy );

			// Event handler for links in the tooltip.
			// It looks like the tipsy is always created from scratch so that
			// there wont be multiple event handlers bound to same click.
			$( 'a.uls-prevlang-link' ).on( 'click.ulstipsy', function ( event ) {
				var deferred = $.Deferred();

				event.preventDefault();
				deferred.done( function () {
					mw.uls.changeLanguage( event.target.lang );
				} );

				mw.hook( 'mw.uls.language.revert' ).fire( deferred );

				// Delay is zero if event logging is not enabled
				window.setTimeout( function () {
					deferred.resolve();
				}, mw.config.get( 'wgULSEventLogging' ) * 500 );
			} );
			tipsyTimer = window.setTimeout( hideTipsy, timeout );
		}

		function hideTipsy() {
			$ulsTrigger.tipsy( 'hide' );
		}

		// The interlanguage position needs some time to settle down
		window.setTimeout( function () {
			// Show the tipsy tooltip on page load.
			showTipsy( 6000 );
		}, 700 );

		// manually show the tooltip
		$ulsTrigger.on( 'mouseover', function () {
			// show only if the ULS panel is not shown
			if ( !$( '.uls-menu:visible' ).length ) {
				showTipsy( 3000 );
			}
		} );
	}

	$( document ).ready( function () {
		mw.uls.init( function () {
			var $triggers,
				$pLang,
				$ulsTrigger = $( '.uls-trigger' ),
				rtlPage = $( 'body' ).hasClass( 'rtl' ),
				anonMode = ( mw.user.isAnon() &&
					!mw.config.get( 'wgULSAnonCanChangeLanguage' ) ),
				imeSelector = mw.config.get( 'wgULSImeSelectors' ).join( ', ' ),
				ulsPosition = mw.config.get( 'wgULSPosition' );

			if ( ulsPosition === 'interlanguage' ) {
				// TODO: Refactor this block
				// The interlanguage links section
				$pLang = $( '#p-lang' );
				// Add an element near the interlanguage links header
				$ulsTrigger = $( '<span>' ).addClass( 'uls-settings-trigger' );
				// Append ULS cog to languages section, but make sure it is visible.
				$pLang.show().prepend( $ulsTrigger );
				// Take care of any other elements with this class.
				$ulsTrigger = $( '.uls-settings-trigger' );
				// Remove the dummy link, which was added to make sure that the section appears
				$pLang.find( '.uls-p-lang-dummy' ).remove();

				if ( !$pLang.find( 'div ul' ).children().length ) {
					// Replace the title of the interlanguage links area
					// if there are no interlanguage links
					$pLang.find( 'h3' )
						.text( mw.msg( 'uls-plang-title-languages' ) );
				}

				$ulsTrigger.attr( {
					title: mw.msg( 'ext-uls-select-language-settings-icon-tooltip' )
				} );

				$ulsTrigger.on( 'click', function ( e, eventParams ) {
					var languagesettings = $ulsTrigger.data( 'languagesettings' ),
						languageSettingsOptions;

					if ( languagesettings ) {
						if ( !languagesettings.shown ) {
							mw.hook( 'mw.uls.settings.open' ).fire( eventParams && eventParams.source || 'interlanguage' );
						}
					} else {
						// Initialize the Language settings window
						languageSettingsOptions = {
							defaultModule: 'display',
							onVisible: function () {
								var topRowHeight, caretHeight, caretWidth,
									$caretBefore = $( '<span>' ).addClass( 'caret-before' ),
									$caretAfter = $( '<span>' ).addClass( 'caret-after' ),
									ulsTriggerWidth = this.$element.width(),
									ulsTriggerOffset = this.$element.offset();

								// Add the callout caret triangle
								// pointing to the trigger icon
								this.$window.addClass( 'callout' );
								this.$window.prepend( $caretBefore, $caretAfter );

								// Calculate the positioning of the panel
								// according to the position of the trigger icon
								if ( rtlPage ) {
									caretWidth = parseInt( $caretBefore.css( 'border-left-width' ), 10 );
									this.left = ulsTriggerOffset.left - this.$window.width() - caretWidth;
								} else {
									caretWidth = parseInt( $caretBefore.css( 'border-right-width' ), 10 );
									this.left = ulsTriggerOffset.left + ulsTriggerWidth + caretWidth;
								}

								topRowHeight = this.$window.find( '.row' ).height();
								caretHeight = parseInt( $caretBefore.css( 'top' ), 10 );
								this.top = ulsTriggerOffset.top - topRowHeight - caretHeight / 2;

								this.position();
							}
						};

						mw.loader.using( mw.uls.languageSettingsModules, function () {
							$ulsTrigger.languagesettings( languageSettingsOptions ).click();
						} );

						e.stopPropagation();
					}
				} );
			} else if ( anonMode ) {
				$ulsTrigger.on( 'click', function ( e, eventParams ) {
					var languagesettings = $ulsTrigger.data( 'languagesettings' );

					e.preventDefault();

					if ( languagesettings ) {
						if ( !languagesettings.shown ) {
							mw.hook( 'mw.uls.settings.open' ).fire( eventParams && eventParams.source || 'personal' );
						}
					} else {
						mw.loader.using( mw.uls.languageSettingsModules, function () {
							$ulsTrigger.languagesettings();

							$ulsTrigger.trigger( 'click', eventParams );
						} );
					}
				} );
			} else {
				$ulsTrigger.on( 'click', function ( e, eventParams ) {
					var uls = $ulsTrigger.data( 'uls' );

					e.preventDefault();

					if ( uls ) {
						if ( !uls.shown ) {
							mw.hook( 'mw.uls.settings.open' ).fire( eventParams && eventParams.source || 'personal' );
						}
					} else {
						mw.loader.using( 'ext.uls.mediawiki', function () {
							$ulsTrigger.uls( {
								quickList: function () {
									return mw.uls.getFrequentLanguageList();
								},
								onReady: function () {
									var uls = this;
									mw.loader.using( mw.uls.languageSettingsModules, function () {
										addDisplaySettings( uls );
										addInputSettings( uls );
									} );
								},
								onSelect: function ( language ) {
									mw.uls.changeLanguage( language );
								},
								onVisible: function () {
									mw.uls.addEventLoggingTriggers();
								}
							} );

							// Allow styles to apply first and position to work by
							// delaying the activation after them.
							window.setTimeout( function () {
								$ulsTrigger.trigger( 'click', eventParams );
							}, 0 );
						} );
					}
				} );
			}

			// At this point we don't care which kind of trigger it is
			$triggers = $( '.uls-settings-trigger, .uls-trigger' );
			addAccessibilityFeatures( $triggers );

			// Bind language settings to preferences page link
			$( '#uls-preferences-link' )
				.text( mw.msg( 'ext-uls-language-settings-preferences-link' ) )
				.click( function () {
					$ulsTrigger.trigger( 'click', {
						source: 'preferences'
					} );

					return false;
				} );

			showULSTooltip();

			$( 'body' ).on( 'focus.imeinit', imeSelector, function () {
				var $input = $( this );
				$( 'body' ).off( '.imeinit' );
				mw.loader.using( 'ext.uls.ime', function () {
					mw.ime.setup();
					mw.ime.handleFocus( $input );
				} );
			} );
		} );
	} );
}( jQuery, mediaWiki ) );