summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UniversalLanguageSelector/lib/jquery.uls/src/jquery.uls.core.js
blob: e46078783361c2d403af8599a48e6e7230ef7bd9 (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
 * Universal Language Selector
 * ULS core component.
 *
 * Copyright (C) 2012 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 ( $ ) {
	'use strict';

	var template, ULS;

	// Region numbers in id attributes also appear in the langdb.
	// eslint-disable-next-line no-multi-str
	template = '<div class="grid uls-menu"> \
			<div id="search" class="row uls-search"> \
				<div class="uls-search-wrapper"> \
					<label class="uls-search-label" for="uls-languagefilter"></label>\
					<div class="uls-search-input-wrapper">\
						<span class="uls-languagefilter-clear"></span>\
						<input type="text" class="uls-filterinput uls-filtersuggestion"\
							disabled="true" autocomplete="off">\
						<input type="text" class="uls-filterinput uls-languagefilter"\
							maxlength="40"\
							data-clear="uls-languagefilter-clear"\
							data-suggestion="uls-filtersuggestion"\
							placeholder="Search for a language" autocomplete="off">\
					</div>\
				</div>\
			</div>\
			<div class="row uls-language-list"></div>\
			<div class="row" id="uls-settings-block"></div>\
		</div>';

	/**
	 * ULS Public class definition
	 * @param {Element} element
	 * @param {Object} options
	 */
	ULS = function ( element, options ) {
		var code;
		this.$element = $( element );
		this.options = $.extend( {}, $.fn.uls.defaults, options );
		this.$menu = $( template );
		this.languages = this.options.languages;

		for ( code in this.languages ) {
			if ( $.uls.data.languages[ code ] === undefined ) {
				// Language is unknown to ULS.
				delete this.languages[ code ];
			}
		}

		this.left = this.options.left;
		this.top = this.options.top;
		this.shown = false;
		this.initialized = false;
		this.shouldRecreate = false;
		this.menuWidth = this.getMenuWidth();

		this.$languageFilter = this.$menu.find( '.uls-languagefilter' );
		this.$resultsView = this.$menu.find( '.uls-language-list' );

		this.render();
		this.listen();
		this.ready();
	};

	ULS.prototype = {
		constructor: ULS,

		/**
		 * A "hook" that runs after the ULS constructor.
		 * At this point it is not guaranteed that the ULS has its dimensions
		 * and that the languages lists are initialized.
		 *
		 * To use it, pass a function as the onReady parameter
		 * in the options when initializing ULS.
		 */
		ready: function () {
			if ( this.options.onReady ) {
				this.options.onReady.call( this );
			}
		},

		/**
		 * A "hook" that runs after the ULS panel becomes visible
		 * by using the show method.
		 *
		 * To use it, pass a function as the onVisible parameter
		 * in the options when initializing ULS.
		 */
		visible: function () {
			if ( this.options.onVisible ) {
				this.options.onVisible.call( this );
			}
		},

		/**
		 * Calculate the position of ULS
		 * Returns an object with top and left properties.
		 * @return {Object}
		 */
		position: function () {
			var pos,
				top = this.top,
				left = this.left;

			if ( top === undefined ) {
				pos = $.extend( {}, this.$element.offset(), {
					height: this.$element[ 0 ].offsetHeight
				} );
				top = pos.top + pos.height;
			}

			if ( left === undefined ) {
				left = $( window ).width() / 2 - this.$menu.outerWidth() / 2;
			}

			return {
				top: top,
				left: left
			};
		},

		/**
		 * Show the ULS window
		 */
		show: function () {
			var widthClasses = {
				wide: 'uls-wide',
				medium: 'uls-medium',
				narrow: 'uls-narrow'
			};

			this.$menu.addClass( widthClasses[ this.menuWidth ] );

			if ( !this.initialized ) {
				$( 'body' ).prepend( this.$menu );
				this.i18n();
				this.initialized = true;
			}

			this.$menu.css( this.position() );
			this.$menu.show();
			this.$menu.scrollIntoView();
			this.shown = true;

			if ( !this.isMobile() ) {
				this.$languageFilter.focus();
			}

			this.visible();
		},

		i18n: function () {
			if ( $.i18n ) {
				this.$menu.find( '[data-i18n]' ).i18n();
				this.$languageFilter.prop( 'placeholder', $.i18n( 'uls-search-placeholder' ) );
			}
		},

		/**
		 * Hide the ULS window
		 */
		hide: function () {
			this.$menu.hide();
			this.shown = false;

			this.$menu.removeClass( 'uls-wide uls-medium uls-narrow' );

			if ( this.shouldRecreate ) {
				this.recreateLanguageFilter();
			}

			if ( this.options.onCancel ) {
				this.options.onCancel.call( this );
			}
		},

		/**
		 * Render the UI elements.
		 * Does nothing by default. Can be used for customization.
		 */
		render: function () {
			// Rendering stuff here
		},

		/**
		 * Callback for results found context.
		 */
		success: function () {
			this.$resultsView.show();
		},

		createLanguageFilter: function () {
			var lcd, languagesCount,
				columnsOptions = {
					wide: 4,
					medium: 2,
					narrow: 1
				};

			languagesCount = Object.keys( this.options.languages ).length;
			lcd = this.$resultsView.lcd( {
				languages: this.languages,
				columns: columnsOptions[ this.menuWidth ],

				quickList: languagesCount > 12 ? this.options.quickList : [],
				clickhandler: this.select.bind( this ),
				showRegions: this.options.showRegions,
				languageDecorator: this.options.languageDecorator,
				noResultsTemplate: this.options.noResultsTemplate,
				itemsPerColumn: this.options.itemsPerColumn,
				groupByRegion: this.options.groupByRegion
			} ).data( 'lcd' );

			this.$languageFilter.languagefilter( {
				lcd: lcd,
				languages: this.languages,
				ulsPurpose: this.options.ulsPurpose,
				searchAPI: this.options.searchAPI,
				onSelect: this.select.bind( this )
			} );

			this.$languageFilter.on( 'noresults.uls', lcd.noResults.bind( lcd ) );
		},

		recreateLanguageFilter: function () {
			this.$resultsView.removeData( 'lcd' );
			this.$resultsView.empty();
			this.$languageFilter.removeData( 'languagefilter' );
			this.createLanguageFilter();

			this.shouldRecreate = false;
		},

		/**
		 * Bind the UI elements with their event listeners
		 */
		listen: function () {
			// Register all event listeners to the ULS here.
			this.$element.on( 'click', this.click.bind( this ) );

			// Don't do anything if pressing on empty space in the ULS
			this.$menu.on( 'click', function ( e ) {
				e.stopPropagation();
			} );

			// Handle key press events on the menu
			this.$menu.on( 'keydown', this.keypress.bind( this ) );

			this.createLanguageFilter();

			this.$languageFilter.on( 'resultsfound.uls', this.success.bind( this ) );

			$( 'html' ).click( this.cancel.bind( this ) );
			$( window ).resize( $.fn.uls.debounce( this.resize.bind( this ), 250 ) );
		},

		resize: function () {
			var menuWidth = this.getMenuWidth();

			if ( this.menuWidth === menuWidth ) {
				return;
			}

			this.menuWidth = menuWidth;
			this.shouldRecreate = true;
			if ( !this.shown ) {
				this.recreateLanguageFilter();
			}
		},

		/**
		 * On select handler for search results
		 * @param {string} langCode
		 * @param {Object} event The jQuery click event
		 */
		select: function ( langCode, event ) {
			this.hide();
			if ( this.options.onSelect ) {
				this.options.onSelect.call( this, langCode, event );
			}
		},

		/**
		 * On cancel handler for the uls menu
		 * @param {Event} e
		 */
		cancel: function ( e ) {
			if ( e && ( this.$element.is( e.target ) ||
				$.contains( this.$element[ 0 ], e.target ) ) ) {
				return;
			}

			this.hide();
		},

		keypress: function ( e ) {
			if ( !this.shown ) {
				return;
			}

			if ( e.keyCode === 27 ) { // escape
				this.cancel();
				e.preventDefault();
				e.stopPropagation();
			}
		},

		click: function () {
			if ( this.shown ) {
				this.hide();
			} else {
				this.show();
			}
		},

		/**
		 * Get the panel menu width parameter
		 * @return {string}
		 */
		getMenuWidth: function () {
			var languagesCount,
				screenWidth = document.documentElement.clientWidth;

			if ( this.options.menuWidth ) {
				return this.options.menuWidth;
			}

			languagesCount = Object.keys( this.options.languages ).length;

			if ( screenWidth > 900 && languagesCount >= 48 ) {
				return 'wide';
			}

			if ( screenWidth > 500 && languagesCount >= 24 ) {
				return 'medium';
			}

			return 'narrow';
		},

		isMobile: function () {
			return navigator.userAgent.match( /(iPhone|iPod|iPad|Android|BlackBerry)/ );
		}
	};

	/* ULS PLUGIN DEFINITION
	 * =========================== */

	$.fn.uls = function ( option ) {
		return this.each( function () {
			var $this = $( this ),
				data = $this.data( 'uls' ),
				options = typeof option === 'object' && option;

			if ( !data ) {
				$this.data( 'uls', ( data = new ULS( this, options ) ) );
			}

			if ( typeof option === 'string' ) {
				data[ option ]();
			}
		} );
	};

	$.fn.uls.defaults = {
		// CSS top position for the dialog
		top: undefined,
		// CSS left position for the dialog
		left: undefined,
		// Callback function when user selects a language
		onSelect: undefined,
		// Callback function when the dialog is closed without selecting a language
		onCancel: undefined,
		// Callback function when ULS has initialized
		onReady: undefined,
		// Callback function when ULS dialog is shown
		onVisible: undefined,
		// Languages to be used for ULS, default is all languages
		languages: $.uls.data.getAutonyms(),
		// The options are wide (4 columns), medium (2 columns), and narrow (1 column).
		// If not specified, it will be set automatically.
		menuWidth: undefined,
		// What is this ULS used for.
		// Should be set for distinguishing between different instances of ULS
		// in the same application.
		ulsPurpose: '',
		// Used by LCD
		quickList: [],
		// Used by LCD
		showRegions: undefined,
		// Used by LCD
		languageDecorator: undefined,
		// Used by LCD
		noResultsTemplate: undefined,
		// Used by LCD
		itemsPerColumn: undefined,
		// Used by LCD
		groupByRegion: undefined,
		// Used by LanguageFilter
		searchAPI: undefined
	};

	// Define a dummy i18n function, if jquery.i18n not integrated.
	if ( !$.fn.i18n ) {
		$.fn.i18n = function () {};
	}

	/**
	 * Creates and returns a new debounced version of the passed function,
	 * which will postpone its execution, until after wait milliseconds have elapsed
	 * since the last time it was invoked.
	 *
	 * @param {Function} fn Function to be debounced.
	 * @param {number} wait Wait interval in milliseconds.
	 * @param {boolean} [immediate] Trigger the function on the leading edge of the wait interval,
	 * instead of the trailing edge.
	 * @return {Function} Debounced function.
	 */
	$.fn.uls.debounce = function ( fn, wait, immediate ) {
		var timeout;

		return function () {
			var callNow, self = this,
				later = function () {
					timeout = null;
					if ( !immediate ) {
						fn.apply( self, arguments );
					}
				};

			callNow = immediate && !timeout;
			clearTimeout( timeout );
			timeout = setTimeout( later, wait || 100 );

			if ( callNow ) {
				fn.apply( self, arguments );
			}
		};
	};

	/*
	 * Simple scrollIntoView plugin.
	 * Scrolls the element to the viewport smoothly if it is not already.
	 */
	$.fn.scrollIntoView = function () {
		return this.each( function () {
			var scrollPosition,
				$window = $( window ),
				windowHeight = $window.height(),
				windowTop = $window.scrollTop(),
				windowBottom = windowTop + windowHeight,
				$element = $( this ),
				panelHeight = $element.height(),
				panelTop = $element.offset().top,
				panelBottom = panelTop + panelHeight;

			if ( ( panelTop < windowTop ) || ( panelBottom > windowBottom ) ) {
				if ( windowTop > panelTop ) {
					scrollPosition = panelTop;
				} else {
					scrollPosition = panelBottom - windowHeight;
				}
				$( 'html, body' ).stop().animate( {
					scrollTop: scrollPosition
				}, 500 );
			}
		} );
	};

	$.fn.uls.Constructor = ULS;
}( jQuery ) );