From e65202b434d35cebc6cf11a574f89880b6abe779 Mon Sep 17 00:00:00 2001 From: "Amir E. Aharoni" Date: Wed, 8 Apr 2015 14:53:12 +0300 Subject: [PATCH] Update jquery.uls from upstream To github version: https://github.com/wikimedia/jquery.uls/commit/345097a8bd727cc77b5cf1a1bc523e507fcb7bf3 Show the Common language section only if the list is long enough Bug: T76196 Change-Id: I6aa0d951657962103c356f6254ab4102e5ee765d --- lib/jquery.uls/src/jquery.uls.core.js | 47 +++++++++++++++++---------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/jquery.uls/src/jquery.uls.core.js b/lib/jquery.uls/src/jquery.uls.core.js index fba48592..201d384d 100644 --- a/lib/jquery.uls/src/jquery.uls.core.js +++ b/lib/jquery.uls/src/jquery.uls.core.js @@ -53,6 +53,30 @@ '; /*jshint multistr:false */ + /** + * Count the number of keys in an object. + * Works in a cross-browser way. + * @param {Object} The object. + */ + function objectLength ( obj ) { + var count, key; + + // Some old browsers don't support Object.keys + if ( Object.keys ) { + return Object.keys( obj ).length; + } + + count = 0; + + for ( key in obj ) { + if ( Object.prototype.hasOwnProperty.call( obj, key ) ) { + count++; + } + } + + return count; + } + /** * ULS Public class definition */ @@ -208,7 +232,7 @@ * Bind the UI elements with their event listeners */ listen: function () { - var lcd, columnsOptions, + var lcd, columnsOptions, languagesCount, uls = this; columnsOptions = { @@ -235,10 +259,12 @@ this.$menu.on( 'keydown', $.proxy( this.keypress, this ) ); } + languagesCount = objectLength( this.options.languages ); lcd = this.$resultsView.lcd( { languages: this.languages, columns: columnsOptions[ this.getMenuWidth() ], - quickList: this.options.quickList, + + quickList: languagesCount > 12 ? this.options.quickList : false, clickhandler: $.proxy( this.select, this ), source: this.$languageFilter, showRegions: this.options.showRegions, @@ -354,26 +380,13 @@ * @return string */ getMenuWidth: function () { - var language, - languagesCount = 0; + var languagesCount; if ( this.options.menuWidth ) { return this.options.menuWidth; } - // IE8 does not support Object.keys - if ( Object.keys ) { - languagesCount = Object.keys( this.options.languages ).length; - } else { - for ( language in this.options.languages ) { - if ( Object.prototype.hasOwnProperty.call( - this.options.languages, - language - ) ) { - languagesCount++; - } - } - } + languagesCount = objectLength( this.options.languages ); if ( languagesCount < 12 ) { return 'narrow';