summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UniversalLanguageSelector/lib/jquery.uls/src/jquery.uls.lcd.js
blob: ceadbf15d01f4f2a3d79f19925666436c1f174b0 (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
/**
 * Universal Language Selector
 * Language category display component - Used for showing the search results,
 * grouped by regions, scripts
 *
 * 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';

	// eslint-disable-next-line no-multi-str
	var noResultsTemplate = '<div class="uls-no-results-view"> \
		<h2 data-i18n="uls-no-results-found" class="uls-no-results-found-title">No results found</h2> \
		<div class="uls-no-results-suggestions"></div> \
		<div class="uls-no-found-more"> \
		<div data-i18n="uls-search-help">You can search by language name, script name, ISO code of language or you can browse by region.</div> \
		</div></div>';

	/**
	 * Language category display
	 * @param {Element} element The container element to which the languages to be displayed
	 * @param {Object} [options] Configuration object
	 * @cfg {Object} [languages] Selectable languages. Keyed by language code, values are autonyms.
	 * @cfg {string[]} [showRegions] Array of region codes to show. Default is
	 *  [ 'WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA' ]
	 * @cfg {number} [itemsPerColumn] Number of languages per column.
	 * @cfg {number} [columns] Number of columns for languages. Default is 4.
	 * @cfg {Function} [languageDecorator] Callback function to be called when a language
	 *  link is prepared - for custom decoration.
	 * @cfg {Function|string[]} [quickList] The languages to display as suggestions for quick
	 *  selection.
	 * @cfg {Function} [clickhandler] Callback when language is selected.
	 * @cfg {jQuery|Function} [noResultsTemplate]
	 */
	function LanguageCategoryDisplay( element, options ) {
		this.$element = $( element );
		this.options = $.extend( {}, $.fn.lcd.defaults, options );
		// Ensure the internal region 'all' is always present
		if ( this.options.showRegions.indexOf( 'all' ) === -1 ) {
			this.options.showRegions.push( 'all' );
		}

		this.$element.addClass( 'uls-lcd' );
		this.regionLanguages = {};
		this.renderTimeout = null;
		this.cachedQuicklist = null;
		this.groupByRegionOverride = null;

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

	LanguageCategoryDisplay.prototype = {
		constructor: LanguageCategoryDisplay,

		/**
		 * Adds language to the language list.
		 * @param {string} langCode
		 * @param {string} [regionCode]
		 * @return {boolean} Whether the language was known and accepted
		 */
		append: function ( langCode, regionCode ) {
			var i, regions;

			if ( !$.uls.data.languages[ langCode ] ) {
				// Language is unknown or not in the list of languages for this context.
				return false;
			}

			if ( !this.isGroupingByRegionEnabled() ) {
				regions = [ 'all' ];

				// Make sure we do not get duplicates
				if ( this.regionLanguages.all.indexOf( langCode ) > -1 ) {
					return true;
				}
			} else {
				if ( regionCode ) {
					regions = [ regionCode ];
				} else {
					regions = $.uls.data.getRegions( langCode );
				}
			}

			for ( i = 0; i < regions.length; i++ ) {
				this.regionLanguages[ regions[ i ] ].push( langCode );
			}

			// Work around the bad interface, delay rendering until we have got
			// all the languages to speed up performance.
			clearTimeout( this.renderTimeout );
			this.renderTimeout = setTimeout( function () {
				this.renderRegions();
			}.bind( this ), 50 );

			return true;
		},

		/**
		 * Whether we should render languages grouped to geographic regions.
		 * @return {boolean}
		 */
		isGroupingByRegionEnabled: function () {
			if ( this.groupByRegionOverride !== null ) {
				return this.groupByRegionOverride;
			} else if ( this.options.groupByRegion !== 'auto' ) {
				return this.options.groupByRegion;
			} else {
				return this.options.columns > 1;
			}
		},

		/**
		 * Override the default region grouping setting.
		 * This is to allow LanguageFilter to disable grouping when displaying search results.
		 *
		 * @param {boolean|null} val True to force grouping, false to disable, null
		 * to undo override.
		 */
		setGroupByRegionOverride: function ( val ) {
			this.groupByRegionOverride = val;
		},

		render: function () {
			var $section,
				$quicklist = this.buildQuicklist(),
				regions = [],
				regionNames = {
					// These are fallback text when i18n library not present
					all: 'All languages', // Used if there is quicklist and no region grouping
					WW: 'Worldwide',
					SP: 'Special',
					AM: 'America',
					EU: 'Europe',
					ME: 'Middle East',
					AS: 'Asia',
					AF: 'Africa',
					PA: 'Pacific'
				};

			if ( $quicklist.length ) {
				regions.push( $quicklist );
			} else {
				// We use CSS to hide the header for 'all' when quicklist is NOT present
				this.$element.addClass( 'uls-lcd--no-quicklist' );
			}

			this.options.showRegions.forEach( function ( regionCode ) {
				this.regionLanguages[ regionCode ] = [];

				$section = $( '<div>' )
					.addClass( 'uls-lcd-region-section hide' )
					.attr( 'data-region', regionCode );

				$( '<h3>' )
					.attr( 'data-i18n', 'uls-region-' + regionCode )
					.addClass( 'uls-lcd-region-title' )
					.text( regionNames[ regionCode ] )
					.appendTo( $section );

				regions.push( $section );
			}.bind( this ) );

			this.$element.append( regions );

			this.i18n();
		},

		/**
		 * Renders a region and displays it if it has content.
		 */
		renderRegions: function () {
			var languages,
				lcd = this;

			this.$element.removeClass( 'uls-no-results' );
			this.$element.children( '.uls-lcd-region-section' ).each( function () {
				var $region = $( this ),
					regionCode = $region.data( 'region' );

				if ( $region.is( '.uls-lcd-quicklist' ) ) {
					return;
				}

				$region.children( '.uls-language-block' ).remove();

				languages = lcd.regionLanguages[ regionCode ];
				if ( !languages || languages.length === 0 ) {
					$region.addClass( 'hide' );
					return;
				}

				lcd.renderRegion(
					$region,
					languages,
					lcd.options.itemsPerColumn,
					lcd.options.columns
				);
				$region.removeClass( 'hide' );

				lcd.regionLanguages[ regionCode ] = [];
			} );

		},

		/**
		 * Adds given languages sorted into rows and columns into given element.
		 * @param {jQuery} $region Element to add language list.
		 * @param {Array} languages List of language codes.
		 * @param {number} itemsPerColumn How many languages fit in a column.
		 * @param {number} columnsPerRow How many columns fit in a row.
		 */
		renderRegion: function ( $region, languages, itemsPerColumn, columnsPerRow ) {
			var columnsClasses, i, lastItem, currentScript, nextScript, force,
				languagesCount = languages.length,
				items = [],
				columns = [],
				rows = [];

			languages = $.uls.data.sortByScriptGroup(
				languages.sort( $.uls.data.sortByAutonym )
			);

			if ( columnsPerRow === 1 ) {
				columnsClasses = 'twelve columns';
			} else if ( columnsPerRow === 2 ) {
				columnsClasses = 'six columns';
			} else {
				columnsClasses = 'three columns';
			}

			if ( this.options.columns === 1 ) {
				// For one-column narrow ULS, just render all the languages
				// in one simple list without separators or script groups
				for ( i = 0; i < languagesCount; i++ ) {
					items.push( this.renderItem( languages[ i ] ) );
				}

				columns.push( $( '<ul>' ).addClass( columnsClasses ).append( items ) );
				rows.push( $( '<div>' ).addClass( 'row uls-language-block' ).append( columns ) );
			} else {
				// For medium and wide ULS, clever column placement
				for ( i = 0; i < languagesCount; i++ ) {
					force = false;
					nextScript = $.uls.data.getScriptGroupOfLanguage( languages[ i + 1 ] );

					lastItem = languagesCount - i === 1;
					// Force column break if script changes and column has more than one
					// row already, but only if grouping by region
					if ( i === 0 || !this.isGroupingByRegionEnabled() ) {
						currentScript = $.uls.data.getScriptGroupOfLanguage( languages[ i ] );
					} else if ( currentScript !== nextScript && items.length > 1 ) {
						force = true;
					}
					currentScript = nextScript;

					items.push( this.renderItem( languages[ i ] ) );

					if ( items.length >= itemsPerColumn || lastItem || force ) {
						columns.push( $( '<ul>' ).addClass( columnsClasses ).append( items ) );
						items = [];
						if ( columns.length >= columnsPerRow || lastItem ) {
							rows.push( $( '<div>' ).addClass( 'row uls-language-block' ).append( columns ) );
							columns = [];
						}
					}
				}
			}

			$region.append( rows );
		},

		/**
		 * Creates dom node representing one item in language list.
		 * @param {string} code Language code
		 * @return {Element}
		 */
		renderItem: function ( code ) {
			var a, name, autonym, li;

			name = this.options.languages[ code ];
			autonym = $.uls.data.getAutonym( code ) || name || code;

			// Not using jQuery as this is performance hotspot
			li = document.createElement( 'li' );
			li.title = name;
			li.setAttribute( 'data-code', code );

			a = document.createElement( 'a' );
			a.appendChild( document.createTextNode( autonym ) );
			a.className = 'autonym';
			a.lang = code;
			a.dir = $.uls.data.getDir( code );

			li.appendChild( a );
			if ( this.options.languageDecorator ) {
				this.options.languageDecorator( $( a ), code );
			}
			return li;
		},

		i18n: function () {
			this.$element.find( '[data-i18n]' ).i18n();
		},

		/**
		 * Adds quicklist as a region.
		 */
		quicklist: function () {
			this.$element.find( '.uls-lcd-quicklist' ).removeClass( 'hide' );
		},

		buildQuicklist: function () {
			var quickList, $quickListSection, $quickListSectionTitle;

			if ( this.cachedQuicklist !== null ) {
				return this.cachedQuicklist;
			}

			if ( $.isFunction( this.options.quickList ) ) {
				this.options.quickList = this.options.quickList();
			}

			if ( !this.options.quickList.length ) {
				this.cachedQuicklist = $( [] );
				return this.cachedQuicklist;
			}

			// Pick only the first elements, because we don't have room for more
			quickList = this.options.quickList;
			quickList = quickList.slice( 0, 16 );
			quickList.sort( $.uls.data.sortByAutonym );

			$quickListSection = $( '<div>' )
				.addClass( 'uls-lcd-region-section uls-lcd-quicklist' );

			$quickListSectionTitle = $( '<h3>' )
				.attr( 'data-i18n', 'uls-common-languages' )
				.addClass( 'uls-lcd-region-title' )
				.text( 'Suggested languages' ); // This is placeholder text if jquery.i18n not present
			$quickListSection.append( $quickListSectionTitle );

			this.renderRegion(
				$quickListSection,
				quickList,
				this.options.itemsPerColumn,
				this.options.columns
			);

			$quickListSectionTitle.i18n();

			this.cachedQuicklist = $quickListSection;
			return this.cachedQuicklist;
		},

		show: function () {
			if ( !this.regionDivs ) {
				this.render();
			}
		},

		/**
		 * Called when a fresh search is started
		 */
		empty: function () {
			this.$element.addClass( 'uls-lcd--no-quicklist' );
			this.$element.find( '.uls-lcd-quicklist' ).addClass( 'hide' );
		},

		focus: function () {
			this.$element.focus();
		},

		/**
		 * No-results event handler
		 * @param {Event} event
		 * @param {Object} data Information about the failed search query
		 */
		noResults: function ( event, data ) {
			var $noResults;

			this.$element.addClass( 'uls-no-results' );

			this.$element.find( '.uls-no-results-view' ).remove();

			if ( typeof this.options.noResultsTemplate === 'function' ) {
				$noResults =
					this.options.noResultsTemplate.call( this, data.query );
			} else if ( this.options.noResultsTemplate instanceof jQuery ) {
				$noResults = this.options.noResultsTemplate;
			} else {
				throw new Error( 'noResultsTemplate option must be ' +
					'either jQuery or function returning jQuery' );
			}

			this.$element.append( $noResults.addClass( 'uls-no-results-view' ).i18n() );
		},

		listen: function () {
			var lcd = this;

			if ( this.options.clickhandler ) {
				this.$element.on( 'click', '.row li', function ( event ) {
					lcd.options.clickhandler.call( this, $( this ).data( 'code' ), event );
				} );
			}
		}
	};

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

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

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

	$.fn.lcd.defaults = {
		// List of languages to show
		languages: [],
		// List of regions to show
		showRegions: [ 'WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA' ],
		// Whether to group by region, defaults to true when columns > 1
		groupByRegion: 'auto',
		// How many items per column until new "row" starts
		itemsPerColumn: 8,
		// Number of columns, only 1, 2 and 4 are supported
		columns: 4,
		// Callback function for language item styling
		languageDecorator: undefined,
		// Likely candidates
		quickList: [],
		// Callback function for language selection
		clickhandler: undefined,
		// Callback function when no search results.
		// If overloaded, it can accept the search string as an argument.
		noResultsTemplate: function () {
			var $suggestionsContainer, $suggestions,
				$noResultsTemplate = $( noResultsTemplate );

			$suggestions = this.buildQuicklist().clone();
			$suggestions.removeClass( 'hide' )
				.find( 'h3' )
				.data( 'i18n', 'uls-no-results-suggestion-title' )
				.text( 'You may be interested in:' )
				.i18n();
			$suggestionsContainer = $noResultsTemplate.find( '.uls-no-results-suggestions' );
			$suggestionsContainer.append( $suggestions );
			return $noResultsTemplate;
		}
	};

}( jQuery ) );