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
|
/**
* Compact the interlanguage links in the sidebar
*
* Copyright (C) 2012-2014 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland, Niharika Kohli
* 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 GPL-2.0+
* @licence MIT License
*/
( function ( $, mw ) {
'use strict';
/**
* For the given array, remove duplicates
* @param {Array} originalArray
* @return de-duplicated array
*/
function unique( originalArray ) {
var uniqueArray = [];
$.each( originalArray, function ( i, v ) {
if ( $.inArray( v, uniqueArray ) === -1 ) {
uniqueArray.push( v );
}
} );
return uniqueArray;
}
/**
* @class
*/
function CompactInterlanguageList( interlanguageList, options ) {
this.$interlanguageList = $( interlanguageList );
this.options = $.extend( {}, $.fn.compactInterlanguageList.defaults, options );
this.interlanguageList = {};
this.compactList = {};
this.$trigger = null;
this.compactSize = 0;
this.listSize = 0;
this.init();
}
CompactInterlanguageList.prototype = {
/**
* Initialize the plugin
*/
init: function () {
this.interlanguageList = this.getInterlanguageList();
this.listSize = this.getListSize();
if ( this.listSize <= this.options.max ) {
// Not enough languages to compact the list
return;
}
// If the interlanguage list is of moderate size, the compact size is 7.
this.compactSize = ( this.listSize <= 12 ) ? 7 : this.options.max;
this.hideOriginal();
this.compactList = this.getCompactList();
this.render();
this.listen();
},
/**
* Render the compacted interlanguage list and triggers
*/
render: function () {
var language;
for ( language in this.compactList ) {
this.showLanguage( language );
}
this.addTrigger();
},
/**
* Bind to event handlers and listen for events
*/
listen: function () {
var languages,
compactLinks = this,
dir = $( 'html' ).prop( 'dir' ),
interlanguageListLeft,
interlanguageListWidth,
ulsLanguageList = {};
languages = $.map( compactLinks.interlanguageList, function ( language, languageCode ) {
ulsLanguageList[ languageCode ] = language.autonym;
return languageCode;
} );
// Calculate the left and width values
interlanguageListLeft = compactLinks.$interlanguageList.offset().left;
interlanguageListWidth = compactLinks.$interlanguageList.width();
// Attach ULS to the trigger
compactLinks.$trigger.uls( {
onReady: function () {
this.$menu.addClass( 'interlanguage-uls-menu' );
},
/**
* Language selection handler
* @param {string} language language code
*/
onSelect: function ( language ) {
var previousLanguages = mw.uls.getPreviousLanguages();
previousLanguages.push( language );
previousLanguages = unique( previousLanguages );
mw.uls.setPreviousLanguages( previousLanguages );
window.location.href = compactLinks.interlanguageList[ language ].href;
},
onVisible: function () {
// Calculate the positioning of the panel
// according to the position of the trigger icon
if ( dir === 'rtl' ) {
this.left = interlanguageListLeft - this.$menu.width();
} else {
this.left = interlanguageListLeft + interlanguageListWidth;
}
this.$menu.css( 'left', this.left );
},
languageDecorator: function ( $languageLink, language ) {
// set href according to language
$languageLink.prop( 'href', compactLinks.interlanguageList[ language ].href );
},
// Use compact version of ULS
compact: true,
// Top position of the language selector. Top it 250px above to take care of
// caret pointing the trigger. See .interlanguage-uls-menu:after style definition
top: compactLinks.$trigger.offset().top - compactLinks.$trigger.height() / 2 - 250,
// List of languages to be shown
languages: ulsLanguageList,
// Show common languages
quickList: compactLinks.filterByCommonLanguages( languages )
} );
},
/**
* Get the compacted interlanguage list as associative array
* @return {Object}
*/
getCompactList: function () {
var language, languages, compactLanguages, index,
compactedList = {};
languages = $.map( this.interlanguageList, function ( element, index ) {
return index;
} );
compactLanguages = this.compact( languages );
for ( index = 0; index < compactLanguages.length; index++ ) {
language = compactLanguages[ index ];
compactedList[ language ] = this.interlanguageList[ language ];
}
return compactedList;
},
/**
* Compact a given array of languages
* @param {Array} languages
* @return {Array} compacted array
*/
compact: function ( languages ) {
var compactLanguages = [];
// Add user-defined assistant languages on wikis with Translate extension.
compactLanguages = compactLanguages.concat( this.filterByAssistantLanguages() );
// Add previously selected languages.
// Previous languages are always the better suggestion
// because the user has explicitly chosen them.
compactLanguages = compactLanguages.concat( this.filterByPreviousLanguages() );
// Add all common languages to the beginning of array.
// These are the most probable languages predicted by ULS.
compactLanguages = compactLanguages.concat( this.filterByCommonLanguages( languages ) );
// Finally add the whole languages array too.
// We will remove duplicates and cut down to required size.
compactLanguages = compactLanguages.concat( languages );
// Remove duplicates
compactLanguages = unique( compactLanguages );
// Cut to compact size and sort
compactLanguages = compactLanguages.slice( 0, this.compactSize ).sort();
return compactLanguages;
},
/**
* Filter the language list by previous languages.
* Not all previous languages will be present in interlanguage links,
* so we are filtering them.
* @return {Array} List of language codes supported by the article
*/
filterByPreviousLanguages: function ( languages ) {
var previousLanguages = mw.uls.getPreviousLanguages();
return $.grep( previousLanguages, function ( language ) {
return $.inArray( language, languages ) >= 0;
} );
},
/**
* Filter the language list by common languages.
* Common languages are the most probable languages predicted by ULS.
* @return {Array} List of language codes supported by the article
*/
filterByCommonLanguages: function ( languages ) {
var commonLanguages = mw.uls.getFrequentLanguageList();
return $.grep( commonLanguages, function ( language ) {
return $.inArray( language, languages ) >= 0;
} );
},
/**
* Filter the language list by Translate's assistant languages.
* Where available, they're languages deemed useful by the user.
* @return {Array} List of those language codes which are supported by article
*/
filterByAssistantLanguages: function ( languages ) {
var assistantLanguages = mw.user.options.get( 'translate-editlangs' );
if ( assistantLanguages && assistantLanguages !== 'default' ) {
return $.grep( assistantLanguages.split( /,\s*/ ), function ( language ) {
return $.inArray( language, languages ) >= 0;
} );
}
return [];
},
/**
* Find out the existing languages supported
* by the article and fetch their href.
* @return {Object} List of existing language codes and their hrefs
*/
getInterlanguageList: function getInterlanguageList() {
var interlanguageList = {};
this.$interlanguageList.find( 'li.interlanguage-link > a' ).each( function () {
var $this = $( this );
interlanguageList[ $this.attr( 'lang' ) ] = {
href: $this.attr( 'href' ),
autonym: $this.text()
};
} );
return interlanguageList;
},
/**
* Get the size of the interlanguage list
*/
getListSize: function () {
return $.map( this.interlanguageList, function ( item, index ) {
return index;
} ).length;
},
/**
* Hide the original interlanguage list
*/
hideOriginal: function () {
this.$interlanguageList.find( '.interlanguage-link' ).hide();
},
/**
* Add the trigger at the bottom of the language list
*/
addTrigger: function () {
var $trigger;
$trigger = $( '<button>' )
.addClass( 'mw-interlanguage-selector mw-ui-button active' )
.html( $.i18n(
'ext-uls-compact-link-count',
mw.language.convertNumber( this.listSize - this.compactSize )
) );
this.$interlanguageList.append( $trigger );
this.$trigger = $trigger;
},
/**
* Show a language from the interlanguage list
* @param {string} language
*/
showLanguage: function ( language ) {
this.$interlanguageList.find( '.interwiki-' + language ).show();
}
};
/**
* CompactInterlanguageList Plugin
* @param {Object} [option]
*/
$.fn.compactInterlanguageList = function ( option ) {
return this.each( function () {
var $this = $( this ),
data = $this.data( 'compactinterlanguagelist' ),
options = typeof option === 'object' && option;
if ( !data ) {
$this.data( 'compactinterlanguagelist', ( data = new CompactInterlanguageList( this, options ) ) );
}
if ( typeof option === 'string' ) {
data[ option ]();
}
} );
};
/**
* Defaults
*/
$.fn.compactInterlanguageList.defaults = {
max: 9 // Compact the list to this size
};
$( document ).ready( function () {
$( '#p-lang ul' ).compactInterlanguageList();
} );
}( jQuery, mediaWiki ) );
|