Update jquery.uls from upstream

* Bug fixes

Change-Id: Ibb57ba4465ddb0878260cc23d4b7fe8a01a272a8
This commit is contained in:
Santhosh Thottingal
2012-11-19 12:25:29 +05:30
committed by Gerrit Code Review
parent f8947b9686
commit d8599bee18

View File

@@ -1,4 +1,4 @@
/*! jquery.uls - v0.1.0 - 2012-11-11 /*! jquery.uls - v0.1.0 - 2012-11-19
* https://github.com/wikimedia/jquery.uls * https://github.com/wikimedia/jquery.uls
* Copyright (c) 2012 Santhosh Thottingal; Licensed GPL, MIT */ * Copyright (c) 2012 Santhosh Thottingal; Licensed GPL, MIT */
@@ -512,13 +512,13 @@
<span data-i18n="uls-search-help">You can search by language name, \ <span data-i18n="uls-search-help">You can search by language name, \
script name, ISO code of language or \ script name, ISO code of language or \
you can browse by region:</span>\ you can browse by region:</span>\
<a class="uls-region-link" data-i18n="uls-region-AM" data-region="AM" href="#">America</a>, \ <a class="uls-region-link" data-i18n="uls-region-AM" data-region="AM">America</a>, \
<a class="uls-region-link" data-i18n="uls-region-EU" data-region="EU" href="#">Europe</a>, \ <a class="uls-region-link" data-i18n="uls-region-EU" data-region="EU">Europe</a>, \
<a class="uls-region-link" data-i18n="uls-region-ME" data-region="ME" href="#">Middle East</a>, \ <a class="uls-region-link" data-i18n="uls-region-ME" data-region="ME">Middle East</a>, \
<a class="uls-region-link" data-i18n="uls-region-AF" data-region="AF" href="#">Africa</a>, \ <a class="uls-region-link" data-i18n="uls-region-AF" data-region="AF">Africa</a>, \
<a class="uls-region-link" data-i18n="uls-region-AS" data-region="AS" href="#">Asia</a>, \ <a class="uls-region-link" data-i18n="uls-region-AS" data-region="AS">Asia</a>, \
<a class="uls-region-link" data-i18n="uls-region-PA" data-region="PA" href="#">Pacific</a>, \ <a class="uls-region-link" data-i18n="uls-region-PA" data-region="PA">Pacific</a>, \
<a class="uls-region-link" data-i18n="uls-region-WW" data-region="WW" href="#">Worldwide</a>.\ <a class="uls-region-link" data-i18n="uls-region-WW" data-region="WW">Worldwide</a>.\
</p>\ </p>\
</div>\ </div>\
</div>\ </div>\
@@ -827,11 +827,13 @@
this.$suggestion = this.$element.parents().find( '#' + this.$element.data( 'suggestion' ) ); this.$suggestion = this.$element.parents().find( '#' + this.$element.data( 'suggestion' ) );
this.$clear = this.$element.parents().find( '#'+ this.$element.data( 'clear' ) ); this.$clear = this.$element.parents().find( '#'+ this.$element.data( 'clear' ) );
this.selectedLanguage = null; this.selectedLanguage = null;
this.listen(); this.listen();
}; };
var delay = ( function() { var delay = ( function() {
var timer = 0; var timer = 0;
return function( callback, milliseconds ) { return function( callback, milliseconds ) {
clearTimeout( timer ); clearTimeout( timer );
timer = setTimeout( callback, milliseconds ); timer = setTimeout( callback, milliseconds );
@@ -843,12 +845,15 @@
listen: function() { listen: function() {
this.$element.on( 'keypress', $.proxy( this.keyup, this ) ) this.$element.on( 'keypress', $.proxy( this.keyup, this ) )
.on( 'keyup', $.proxy( this.keyup, this ) ); .on( 'keyup', $.proxy( this.keyup, this ) );
if ( this.eventSupported( 'keydown' ) ) { if ( this.eventSupported( 'keydown' ) ) {
this.$element.on( 'keydown', $.proxy( this.keyup, this ) ); this.$element.on( 'keydown', $.proxy( this.keyup, this ) );
} }
if ( this.$clear.length ) { if ( this.$clear.length ) {
this.$clear.on( 'click' , $.proxy( this.clear, this ) ); this.$clear.on( 'click' , $.proxy( this.clear, this ) );
} }
this.toggleClear(); this.toggleClear();
}, },
@@ -856,6 +861,7 @@
switch( e.keyCode ) { switch( e.keyCode ) {
case 9: // Tab -> Autocomplete case 9: // Tab -> Autocomplete
var suggestion = this.$suggestion.val(); var suggestion = this.$suggestion.val();
if ( suggestion && suggestion !== this.$element.val() ) { if ( suggestion && suggestion !== this.$element.val() ) {
this.$element.val( suggestion ); this.$element.val( suggestion );
e.preventDefault(); e.preventDefault();
@@ -866,7 +872,9 @@
if ( !this.options.onSelect ) { if ( !this.options.onSelect ) {
break; break;
} }
var query = $.trim( this.$element.val() ).toLowerCase(); var query = $.trim( this.$element.val() ).toLowerCase();
if ( this.selectedLanguage ) { if ( this.selectedLanguage ) {
// this.selectLanguage will be populated from a matching search // this.selectLanguage will be populated from a matching search
this.options.onSelect( this.selectedLanguage ); this.options.onSelect( this.selectedLanguage );
@@ -875,10 +883,18 @@
// but we have a matching language code. // but we have a matching language code.
this.options.onSelect( query ); this.options.onSelect( query );
} }
break; break;
default: default:
var that = this; var that = this;
if ( e.which < 32 ) {
// ignore any ASCII control characters
break;
}
this.selectedLanguage = null; this.selectedLanguage = null;
delay( function() { delay( function() {
if ( !that.$element.val() ) { if ( !that.$element.val() ) {
that.clear(); that.clear();
@@ -887,6 +903,7 @@
that.search(); that.search();
} }
}, 300 ); }, 300 );
this.toggleClear(); this.toggleClear();
} }
}, },
@@ -897,7 +914,11 @@
*/ */
deactivate: function() { deactivate: function() {
this.$element.val( '' ); this.$element.val( '' );
this.$element.focus();
if ( !$.fn.uls.Constructor.prototype.isMobile() ) {
this.$element.focus();
}
this.toggleClear(); this.toggleClear();
this.autofill(); this.autofill();
}, },
@@ -907,7 +928,7 @@
*/ */
clear: function() { clear: function() {
this.deactivate(); this.deactivate();
this.$element.trigger( 'seachclear' ); this.$element.trigger( 'searchclear' );
}, },
/** /**
@@ -930,25 +951,33 @@
var query = $.trim( this.$element.val() ), var query = $.trim( this.$element.val() ),
languages = $.uls.data.getLanguagesByScriptGroup( this.options.languages ), languages = $.uls.data.getLanguagesByScriptGroup( this.options.languages ),
scriptGroup, langNum, langCode; scriptGroup, langNum, langCode;
this.resultCount = 0; this.resultCount = 0;
for ( scriptGroup in languages ) { for ( scriptGroup in languages ) {
var languagesInScript = languages[scriptGroup]; var languagesInScript = languages[scriptGroup];
languagesInScript.sort( $.uls.data.sortByAutonym ); languagesInScript.sort( $.uls.data.sortByAutonym );
for ( langNum = 0; langNum < languagesInScript.length; langNum++ ) { for ( langNum = 0; langNum < languagesInScript.length; langNum++ ) {
langCode = languagesInScript[langNum]; langCode = languagesInScript[langNum];
if ( query === "" || this.filter( langCode, query ) ) { if ( query === "" || this.filter( langCode, query ) ) {
if ( this.resultCount === 0 ) { if ( this.resultCount === 0 ) {
// Autofill the first result. // Autofill the first result.
this.autofill( langCode ); this.autofill( langCode );
} }
if ( query.toLowerCase() === langCode ) { if ( query.toLowerCase() === langCode ) {
this.selectedLanguage = langCode; this.selectedLanguage = langCode;
} }
this.render( langCode ); this.render( langCode );
this.resultCount++; this.resultCount++;
} }
} }
} }
// Also do a search by search API // Also do a search by search API
if( !this.resultCount && this.options.searchAPI && query ) { if( !this.resultCount && this.options.searchAPI && query ) {
this.searchAPI( query ); this.searchAPI( query );
@@ -959,6 +988,7 @@
searchAPI: function( query ) { searchAPI: function( query ) {
var that = this; var that = this;
$.get( that.options.searchAPI, { search: query }, function( result ) { $.get( that.options.searchAPI, { search: query }, function( result ) {
$.each( result['languagesearch'], function( code, name ) { $.each( result['languagesearch'], function( code, name ) {
if ( that.resultCount === 0 ) { if ( that.resultCount === 0 ) {
@@ -990,15 +1020,19 @@
if ( !this.$suggestion.length ) { if ( !this.$suggestion.length ) {
return; return;
} }
if ( !this.$element.val() ) { if ( !this.$element.val() ) {
this.$suggestion.val( '' ); this.$suggestion.val( '' );
return; return;
} }
this.selectedLanguage = langCode; this.selectedLanguage = langCode;
languageName = languageName || this.options.languages[langCode]; languageName = languageName || this.options.languages[langCode];
var autonym, var autonym,
userInput = this.$element.val(), userInput = this.$element.val(),
suggestion = userInput + languageName.substring( userInput.length, languageName.length ); suggestion = userInput + languageName.substring( userInput.length, languageName.length );
if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) { if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) {
// see if it was autonym match // see if it was autonym match
autonym = $.uls.data.getAutonym( langCode ) || ''; autonym = $.uls.data.getAutonym( langCode ) || '';
@@ -1008,18 +1042,22 @@
suggestion = ""; suggestion = "";
} }
} }
// Make sure that it is a visual prefix. // Make sure that it is a visual prefix.
if ( !isVisualPrefix( userInput, suggestion ) ) { if ( !isVisualPrefix( userInput, suggestion ) ) {
suggestion = ""; suggestion = "";
} }
this.$suggestion.val( suggestion ); this.$suggestion.val( suggestion );
}, },
render: function( langCode ) { render: function( langCode ) {
var $target = this.options.$target; var $target = this.options.$target;
if ( !$target ) { if ( !$target ) {
return; return;
} }
$target.append( langCode, null ); $target.append( langCode, null );
}, },
@@ -1039,6 +1077,7 @@
// FIXME script is ISO 15924 code. We might need actual name of script. // FIXME script is ISO 15924 code. We might need actual name of script.
var matcher = new RegExp( "^" + this.escapeRegex( searchTerm ), 'i' ), var matcher = new RegExp( "^" + this.escapeRegex( searchTerm ), 'i' ),
languageName = this.options.languages[langCode]; languageName = this.options.languages[langCode];
return matcher.test( languageName ) || return matcher.test( languageName ) ||
matcher.test( $.uls.data.getAutonym( langCode ) ) || matcher.test( $.uls.data.getAutonym( langCode ) ) ||
matcher.test( langCode ) || matcher.test( langCode ) ||
@@ -1063,9 +1102,11 @@
var $this = $( this ), var $this = $( this ),
data = $this.data( 'languagefilter' ), data = $this.data( 'languagefilter' ),
options = typeof option === 'object' && option; options = typeof option === 'object' && option;
if ( !data ) { if ( !data ) {
$this.data( 'languagefilter', ( data = new LanguageFilter( this, options ) ) ); $this.data( 'languagefilter', ( data = new LanguageFilter( this, options ) ) );
} }
if ( typeof option === 'string' ) { if ( typeof option === 'string' ) {
data[option](); data[option]();
} }
@@ -1301,8 +1342,8 @@
* @licence MIT License * @licence MIT License
*/ */
(function ( $ ) { ( function ( $ ) {
"use strict"; 'use strict';
// Region numbers in id attributes also appear in the langdb. // Region numbers in id attributes also appear in the langdb.
var template = '\ var template = '\
@@ -1369,7 +1410,7 @@
for ( var code in this.languages ) { for ( var code in this.languages ) {
if ( $.uls.data.languages[code] === undefined ) { if ( $.uls.data.languages[code] === undefined ) {
if ( window.console && window.console.log ) { if ( window.console && window.console.log ) {
window.console.log( "ULS: Unknown language " + code + "." ); window.console.log( 'ULS: Unknown language ' + code + '.' );
} }
delete this.languages[code]; delete this.languages[code];
} }
@@ -1430,7 +1471,10 @@
} }
this.$menu.show(); this.$menu.show();
this.shown = true; this.shown = true;
this.$languageFilter.focus();
if ( !this.isMobile() ) {
this.$languageFilter.focus();
}
}, },
i18n: function() { i18n: function() {
@@ -1486,61 +1530,69 @@
*/ */
listen: function() { listen: function() {
var lcd, var lcd,
that = this; uls = this,
cancelProxy = $.proxy( uls.cancel, uls );
// Register all event listeners to the ULS here. // Register all event listeners to the ULS here.
that.$element.on( 'click', $.proxy( that.click, that ) ); uls.$element.on( 'click', $.proxy( uls.click, uls ) );
that.$languageFilter.on( 'seachclear', $.proxy( that.defaultSearch, that ) ); uls.$languageFilter.on( 'searchclear', $.proxy( uls.defaultSearch, uls ) );
// Handle click on close button
this.$menu.find( "#uls-close" ).on( 'click', $.proxy( that.cancel, that ) ); // Close when clicking on the close button
uls.$menu.find( '#uls-close' ).on( 'click', cancelProxy );
// Don't do anything if pressing on empty space in the ULS
uls.$menu.on( 'click', function ( e ) {
e.stopPropagation();
} );
// Close ULS if clicking elsewhere
$( document ).on( 'click', cancelProxy );
// Handle key press events on the menu // Handle key press events on the menu
that.$menu.on('keypress', $.proxy(this.keypress, this) ) uls.$menu.on( 'keypress', $.proxy( this.keypress, this ) )
.on('keyup', $.proxy(this.keyup, this) ); .on( 'keyup', $.proxy( this.keyup, this ) );
if ( this.eventSupported( 'keydown' ) ) { if ( this.eventSupported( 'keydown' ) ) {
this.$menu.on( 'keydown', $.proxy( this.keypress, this ) ); this.$menu.on( 'keydown', $.proxy( this.keypress, this ) );
} }
lcd = that.$resultsView.lcd( { lcd = uls.$resultsView.lcd( {
languages: that.languages, languages: uls.languages,
quickList: that.options.quickList, quickList: uls.options.quickList,
clickhandler: $.proxy( that.select, that ), clickhandler: $.proxy( uls.select, uls ),
lazyload: that.options.lazyload, lazyload: uls.options.lazyload,
source: that.$languageFilter source: uls.$languageFilter
} ).data( "lcd" ); } ).data( 'lcd' );
that.$languageFilter.languagefilter( { uls.$languageFilter.languagefilter( {
$target: lcd, $target: lcd,
languages: that.languages, languages: uls.languages,
success: function() { success: function() {
$( '.regionselector' ).removeClass( 'active' ); $( '.regionselector' ).removeClass( 'active' );
that.success(); uls.success();
}, },
noresults: function() { noresults: function() {
$( '.regionselector' ).removeClass( 'active' ); $( '.regionselector' ).removeClass( 'active' );
that.noresults(); uls.noresults();
}, },
searchAPI: that.options.searchAPI, searchAPI: uls.options.searchAPI,
onSelect: $.proxy( that.select, that ) onSelect: $.proxy( uls.select, uls )
} ); } );
// Create region selectors, one per region // Create region selectors, one per region
this.$menu.find( '.uls-region, .uls-region-link' ).regionselector( { this.$menu.find( '.uls-region, .uls-region-link' ).regionselector( {
$target: lcd, $target: lcd,
languages: that.languages, languages: uls.languages,
success: function( regionfilter ) { success: function( regionfilter ) {
// Deactivate search filtering // Deactivate search filtering
that.$languageFilter.languagefilter( 'deactivate' ); uls.$languageFilter.languagefilter( 'deactivate' );
// If it is WW region, show the quicklist // If it is WW region, show the quicklist
if ( regionfilter.regionGroup === 1 ) { if ( regionfilter.regionGroup === 1 ) {
lcd.quicklist(); lcd.quicklist();
} }
// Show 'results view' if we are in no results mode // Show 'results view' if we are in no results mode
that.success(); uls.success();
}, },
noresults: function() { noresults: function() {
that.$languageFilter.languagefilter( 'clear' ); uls.$languageFilter.languagefilter( 'clear' );
} }
} ); } );
}, },
@@ -1591,7 +1643,9 @@
click: function( e ) { click: function( e ) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
if ( !this.shown ) { if ( this.shown ) {
this.hide();
} else {
this.show(); this.show();
} }
}, },
@@ -1604,6 +1658,10 @@
isSupported = typeof this.$element[eventName] === 'function'; isSupported = typeof this.$element[eventName] === 'function';
} }
return isSupported; return isSupported;
},
isMobile: function () {
return navigator.userAgent.match( /(iPhone|iPod|iPad|Android|BlackBerry)/ );
} }
}; };