Update jquery.uls and make it more modular

jquery.uls RL module split to
* jquery.uls (language selector UI/UX)
* jquery.uls.grid (Foundation grid framework)
* jquery.uls.data (language database)
* jquery.uls.compact (compact mode ULS)

This contains updates from jquery.uls upstream

Change-Id: Iaddb1228c076f698498d3b0554061624b6e433c6
This commit is contained in:
Santhosh Thottingal
2013-03-26 10:13:02 +05:30
committed by Niklas Laxström
parent c4f0438597
commit d4cea039b4
16 changed files with 2631 additions and 2241 deletions

View File

@@ -0,0 +1,403 @@
/**
* 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';
// Region numbers in id attributes also appear in the langdb.
/*jshint multistr:true */
var template = '\
<div class="grid uls-menu uls-wide"> \
<div class="row"> \
<span id="uls-close" class="icon-close"></span> \
</div> \
<div class="row"> \
<div class="uls-title-region seven columns">\
<h1 data-i18n="uls-select-language" class="uls-title">Select Language</h1>\
</div>\
<div class="five columns map-block" id="map-block">\
<div class="row">\
<div data-regiongroup="1" id="uls-region-1" class="three columns uls-region uls-region-1">\
<a data-i18n="uls-region-WW">Worldwide</a>\
</div>\
<div class="nine columns">\
<div class="row uls-worldmap">\
<div data-regiongroup="2" id="uls-region-2" class="four columns uls-region">\
<a data-i18n="uls-region-AM">America</a>\
</div>\
<div data-regiongroup="3" id="uls-region-3" class="four columns uls-region">\
<a><span data-i18n="uls-region-EU">Europe</span><br>\
<span data-i18n="uls-region-ME">Middle East</span><br>\
<span data-i18n="uls-region-AF">Africa</span></a>\
</div>\
<div data-regiongroup="4" id="uls-region-4" class="four columns uls-region">\
<a><span data-i18n="uls-region-AS">Asia</span><br>\
<span data-i18n="uls-region-PA">Pacific</span></a>\
</div>\
</div>\
</div>\
</div>\
</div>\
</div>\
<div id="search" class="row search"> \
<div class="one column">\
<span class="search-label"></span>\
</div>\
<div class="ten columns">\
<div id="search-input-block" class="search-input-block">\
<input type="text" class="filterinput filtersuggestion" id="filtersuggestion" disabled="true"\
autocomplete="off" /> <input type="text" class="filterinput languagefilter" id="languagefilter"\
data-clear="languagefilter-clear" data-suggestion="filtersuggestion"\
placeholder="Language search" autocomplete="off" />\
</div>\
</div>\
<div class="one column">\
<span id="languagefilter-clear" class="languagefilter-clear"></span>\
</div>\
</div>\
<div class="row uls-language-list"></div>\
<div class="row" id="settings-block"></div>\
</div> ';
/*jshint multistr:false */
/**
* ULS Public class definition
*/
var ULS = function ( element, options ) {
this.$element = $( element );
this.options = $.extend( {}, $.fn.uls.defaults, options );
this.$menu = $( template );
this.languages = this.options.languages;
for ( var code in this.languages ) {
if ( $.uls.data.languages[code] === undefined ) {
if ( window.console && window.console.log ) {
window.console.log( 'ULS: Unknown language ' + code + '.' );
}
delete this.languages[code];
}
}
this.left = this.options.left;
this.top = this.options.top;
this.shown = false;
this.initialized = false;
this.$languageFilter = this.$menu.find( '#languagefilter' );
this.$regionFilters = this.$menu.find( '.uls-region' );
this.$resultsView = this.$menu.find( 'div.uls-language-list' );
this.render();
this.listen();
this.ready();
};
ULS.prototype = {
constructor: ULS,
ready: function () {
if ( this.options.onReady ) {
this.options.onReady.call( this );
}
},
/**
* Calculate the position of ULS
* Returns an object with top and left properties.
* @returns {Object}
*/
position: function () {
var pos = $.extend( {}, this.$element.offset(), {
height: this.$element[0].offsetHeight
} );
return {
top: this.top || pos.top + pos.height,
left: this.left || '25%'
};
},
/**
* Show the ULS window
*/
show: function () {
var pos = this.position();
this.$menu.css( {
top: pos.top,
left: '25%'
} );
if ( this.options.compact ) {
this.$menu.addClass( 'uls-compact' );
}
if ( !this.initialized ) {
$( 'body' ).prepend( this.$menu );
this.i18n();
// Initialize with a full search.
// This happens on first time click of uls trigger.
this.defaultSearch();
this.initialized = true;
}
// hide any other ULS visible
$( '.uls-menu' ).hide();
this.$menu.show();
this.shown = true;
if ( !this.isMobile() ) {
this.$languageFilter.focus();
}
},
i18n: function () {
if ( $.i18n ) {
this.$menu.find( '[data-i18n]' ).i18n();
this.$languageFilter.prop( 'placeholder', $.i18n( 'uls-search-placeholder' ) );
}
},
defaultSearch: function () {
this.$resultsView.lcd( 'empty' );
if ( this.options.lazyload ) {
this.$regionFilters.first().regionselector( 'show' );
} else {
this.$regionFilters.regionselector( 'show' );
}
},
/**
* Hide the ULS window
*/
hide: function () {
this.$menu.hide();
this.shown = false;
},
/**
* Render the UI elements.
* Does nothing by default. Can be used for customization.
*/
render: function () {
// Rendering stuff here
},
/**
* Callback for no results found context.
*/
noresults: function () {
this.$resultsView.lcd( 'noResults' );
},
/**
* callback for results found context.
*/
success: function () {
this.$resultsView.show();
},
/**
* Bind the UI elements with their event listeners
*/
listen: function () {
var lcd,
uls = this;
// Register all event listeners to the ULS here.
uls.$element.on( 'click', $.proxy( uls.click, uls ) );
uls.$languageFilter.on( 'searchclear', $.proxy( uls.defaultSearch, uls ) );
// Close when clicking on the close button
uls.$menu.find( '#uls-close' ).on( 'click', $.proxy( uls.cancel, uls ) );
// Don't do anything if pressing on empty space in the ULS
uls.$menu.on( 'click', function ( e ) {
e.stopPropagation();
} );
// Handle key press events on the menu
uls.$menu.on( 'keypress', $.proxy( this.keypress, this ) )
.on( 'keyup', $.proxy( this.keyup, this ) );
if ( this.eventSupported( 'keydown' ) ) {
this.$menu.on( 'keydown', $.proxy( this.keypress, this ) );
}
lcd = uls.$resultsView.lcd( {
languages: uls.languages,
quickList: uls.options.quickList,
clickhandler: $.proxy( uls.select, uls ),
lazyload: uls.options.lazyload,
source: uls.$languageFilter,
showRegions: uls.options.showRegions
} ).data( 'lcd' );
uls.$languageFilter.languagefilter( {
$target: lcd,
languages: uls.languages,
success: function () {
$( '.regionselector' ).removeClass( 'active' );
uls.success();
},
noresults: function () {
$( '.regionselector' ).removeClass( 'active' );
uls.noresults();
},
searchAPI: uls.options.searchAPI,
onSelect: $.proxy( uls.select, uls )
} );
// Create region selectors, one per region
this.$menu.find( '.uls-region, .uls-region-link' ).regionselector( {
$target: lcd,
languages: uls.languages,
success: function ( regionfilter ) {
// Deactivate search filtering
uls.$languageFilter.languagefilter( 'deactivate' );
// If it is the WW region, show the quicklist
if ( regionfilter.regionGroup === 1 ) {
lcd.quicklist();
}
// Show 'results view' if we are in no results mode
uls.success();
},
noresults: function () {
uls.$languageFilter.languagefilter( 'clear' );
}
} );
$( 'html' ).click( $.proxy( this.hide, this ) );
},
/**
* On select handler for search results
* @param langCode
*/
select: function ( langCode ) {
this.hide();
if ( this.options.onSelect ) {
this.options.onSelect.call( this, langCode );
}
},
/**
* On cancel handler for the uls menu
*/
cancel: function () {
this.hide();
if ( this.options.onCancel ) {
this.options.onCancel.call( this );
}
},
keyup: function ( e ) {
if ( !this.shown ) {
return;
}
if ( e.keyCode === 27 ) { // escape
this.cancel();
e.preventDefault();
e.stopPropagation();
}
},
keypress: function ( e ) {
if ( !this.shown ) {
return;
}
if ( e.keyCode === 27 ) { // escape
this.cancel();
e.preventDefault();
e.stopPropagation();
}
},
click: function ( e ) {
e.stopPropagation();
e.preventDefault();
if ( this.shown ) {
this.hide();
} else {
this.show();
}
},
eventSupported: function ( eventName ) {
var isSupported = eventName in this.$menu;
if ( !isSupported ) {
this.$element.setAttribute( eventName, 'return;' );
isSupported = typeof this.$element[eventName] === 'function';
}
return isSupported;
},
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 = {
menu: template,
onSelect: null, // Callback function to be called when a language is selected
searchAPI: null, // Language search API
languages: $.uls.data.getAutonyms(), // Languages to be used for ULS, default is all languages
quickList: null, // Array of language codes or function that returns such
lazyload: true, // Lazy load the language list when scrolled.
compact: false, // Show ULS in compact mode
showRegions: ['WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA']
};
// Define a dummy i18n function, if jquery.i18n not integrated.
if ( !$.fn.i18n ) {
$.fn.i18n = function () {
};
}
$.fn.uls.Constructor = ULS;
} ( jQuery ) );

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,463 @@
/**
* Utility functions for querying language data.
*
* 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';
/**
* Is this language a redirect to another language?
* @param language string Language code
* @return Target language code if it's a redirect or false if it's not
*/
$.uls.data.isRedirect = function ( language ) {
return ( $.uls.data.languages[language] !== undefined &&
$.uls.data.languages[language].length === 1 ) ? $.uls.data.languages[language][0] : false;
};
/**
* Returns the script of the language.
* @param language string Language code
* @return string
*/
$.uls.data.getScript = function ( language ) {
var target = $.uls.data.isRedirect( language );
if ( target ) {
return $.uls.data.getScript( target );
}
return $.uls.data.languages[language][0];
};
/**
* Returns the regions in which a language is spoken.
* @param language string Language code
* @return array|string 'UNKNOWN'
*/
$.uls.data.getRegions = function ( language ) {
var target = $.uls.data.isRedirect( language );
if ( target ) {
return $.uls.data.getRegions( target );
}
return ( $.uls.data.languages[language] && $.uls.data.languages[language][1] ) || 'UNKNOWN';
};
/**
* Returns the autonym of the language.
* @param language string Language code
* @return string
*/
$.uls.data.getAutonym = function ( language ) {
var target = $.uls.data.isRedirect( language );
if ( target ) {
return $.uls.data.getAutonym( target );
}
return ( $.uls.data.languages[language] && $.uls.data.languages[language][2] ) || language;
};
/**
* Returns all language codes and corresponding autonyms
* @return array
*/
$.uls.data.getAutonyms = function () {
var language,
autonymsByCode = {};
for ( language in $.uls.data.languages ) {
if ( $.uls.data.isRedirect( language ) ) {
continue;
}
autonymsByCode[language] = $.uls.data.getAutonym( language );
}
return autonymsByCode;
};
/**
* Returns an array of all region codes.
* @return array
*/
$.uls.data.getAllRegions = function () {
var region,
allRegions = [];
for ( region in $.uls.data.regiongroups ) {
allRegions.push( region );
}
return allRegions;
};
/**
* Returns all languages written in script.
* @param script string
* @return array of strings (languages codes)
*/
$.uls.data.getLanguagesInScript = function ( script ) {
return $.uls.data.getLanguagesInScripts( [ script ] );
};
/**
* Returns all languages written in the given scripts.
* @param scripts array of strings
* @return array of strings (languages codes)
*/
$.uls.data.getLanguagesInScripts = function ( scripts ) {
var language, i,
languagesInScripts = [];
for ( language in $.uls.data.languages ) {
if ( $.uls.data.isRedirect( language ) ) {
continue;
}
for ( i = 0; i < scripts.length; i++ ) {
if ( scripts[i] === $.uls.data.getScript( language ) ) {
languagesInScripts.push( language );
break;
}
}
}
return languagesInScripts;
};
/**
* Returns all languages in a given region.
* @param region string
* @return array of strings (languages codes)
*/
$.uls.data.getLanguagesInRegion = function ( region ) {
return $.uls.data.getLanguagesInRegions( [ region ] );
};
/**
* Returns all languages in given regions.
* @param regions array of strings.
* @return array of strings (languages codes)
*/
$.uls.data.getLanguagesInRegions = function ( regions ) {
var language, i,
languagesInRegions = [];
for ( language in $.uls.data.languages ) {
if ( $.uls.data.isRedirect( language ) ) {
continue;
}
for ( i = 0; i < regions.length; i++ ) {
if ( $.inArray( regions[i], $.uls.data.getRegions( language ) ) !== -1 ) {
languagesInRegions.push( language );
break;
}
}
}
return languagesInRegions;
};
/**
* Returns all languages in a region group.
* @param groupNum number.
* @return array of strings (languages codes)
*/
$.uls.data.getLanguagesInRegionGroup = function ( groupNum ) {
return $.uls.data.getLanguagesInRegions( $.uls.data.getRegionsInGroup( groupNum ) );
};
/**
* Returns an associative array of languages in a region,
* grouped by script.
* @param region string Region code
* @return associative array
*/
$.uls.data.getLanguagesByScriptInRegion = function ( region ) {
var language, script,
languagesByScriptInRegion = {};
for ( language in $.uls.data.languages ) {
if ( $.uls.data.isRedirect( language ) ) {
continue;
}
if ( $.inArray( region, $.uls.data.getRegions( language ) ) !== -1 ) {
script = $.uls.data.getScript( language );
if ( languagesByScriptInRegion[script] === undefined ) {
languagesByScriptInRegion[script] = [];
}
languagesByScriptInRegion[script].push( language );
}
}
return languagesByScriptInRegion;
};
/**
* Returns an associative array of languages in a region,
* grouped by script group.
* @param region string Region code
* @return associative array
*/
$.uls.data.getLanguagesByScriptGroupInRegion = function ( region ) {
return $.uls.data.getLanguagesByScriptGroupInRegions( [ region ] );
};
/**
* Returns an associative array of all languages,
* grouped by script group.
* @return associative array
*/
$.uls.data.getAllLanguagesByScriptGroup = function () {
return $.uls.data.getLanguagesByScriptGroupInRegions( $.uls.data.getAllRegions() );
};
/**
* Get the given list of languages grouped by script.
* @param languages Array of language codes
* @return {Object} Array of languages indexed by script codes
*/
$.uls.data.getLanguagesByScriptGroup = function ( languages ) {
var languagesByScriptGroup = {},
language, codeToAdd, langScriptGroup;
for ( language in languages ) {
codeToAdd = $.uls.data.isRedirect( language ) || language;
langScriptGroup = $.uls.data.getScriptGroupOfLanguage( codeToAdd );
if ( !languagesByScriptGroup[langScriptGroup] ) {
languagesByScriptGroup[langScriptGroup] = [];
}
// Prevent duplicate adding of redirects
if ( $.inArray( codeToAdd, languagesByScriptGroup[langScriptGroup] ) === -1 ) {
languagesByScriptGroup[langScriptGroup].push( codeToAdd );
}
}
return languagesByScriptGroup;
};
/**
* Returns an associative array of languages in several regions,
* grouped by script group.
* @param regions array of strings - region codes
* @return associative array
*/
$.uls.data.getLanguagesByScriptGroupInRegions = function ( regions ) {
var language, i, scriptGroup,
languagesByScriptGroupInRegions = {};
for ( language in $.uls.data.languages ) {
if ( $.uls.data.isRedirect( language ) ) {
continue;
}
for ( i = 0; i < regions.length; i++ ) {
if ( $.inArray( regions[i], $.uls.data.getRegions( language ) ) !== -1 ) {
scriptGroup = $.uls.data.getScriptGroupOfLanguage( language );
if ( languagesByScriptGroupInRegions[scriptGroup] === undefined ) {
languagesByScriptGroupInRegions[scriptGroup] = [];
}
languagesByScriptGroupInRegions[scriptGroup].push( language );
break;
}
}
}
return languagesByScriptGroupInRegions;
};
/**
* Returns an array of languages grouped by region group,
* region, script group and script.
* @return associative array
*/
$.uls.data.getAllLanguagesByRegionAndScript = function () {
var region, regionGroup, language,
script, scriptGroup, regions, regionNum,
allLanguagesByRegionAndScript = {};
for ( region in $.uls.data.regiongroups ) {
regionGroup = $.uls.data.regiongroups[region];
if ( allLanguagesByRegionAndScript[regionGroup] === undefined ) {
allLanguagesByRegionAndScript[regionGroup] = {};
}
allLanguagesByRegionAndScript[regionGroup][region] = {};
}
for ( language in $.uls.data.languages ) {
if ( $.uls.data.isRedirect( language ) ) {
continue;
}
script = $.uls.data.getScript( language );
scriptGroup = $.uls.data.getGroupOfScript( script );
regions = $.uls.data.getRegions( language );
for ( regionNum = 0; regionNum < regions.length; regionNum++ ) {
region = regions[regionNum];
regionGroup = $.uls.data.regiongroups[region];
if ( allLanguagesByRegionAndScript[regionGroup][region][scriptGroup] === undefined ) {
allLanguagesByRegionAndScript[regionGroup][region][scriptGroup] = {};
}
if ( allLanguagesByRegionAndScript[regionGroup][region][scriptGroup][script] === undefined ) {
allLanguagesByRegionAndScript[regionGroup][region][scriptGroup][script] = [];
}
allLanguagesByRegionAndScript[regionGroup][region][scriptGroup][script].push( language );
}
}
return allLanguagesByRegionAndScript;
};
/**
* Returns all regions in a region group.
* @param groupNum int
* @return array of strings
*/
$.uls.data.getRegionsInGroup = function ( groupNum ) {
var region,
regionsInGroup = [];
for ( region in $.uls.data.regiongroups ) {
if ( $.uls.data.regiongroups[region] === groupNum ) {
regionsInGroup.push( region );
}
}
return regionsInGroup;
};
/**
* Returns the script group of a script or 'Other' if it doesn't
* belong to any group.
* @param script string Script code
* @return string script group name
*/
$.uls.data.getGroupOfScript = function ( script ) {
var scriptGroup;
for ( scriptGroup in $.uls.data.scriptgroups ) {
if ( $.inArray( script, $.uls.data.scriptgroups[scriptGroup] ) !== -1 ) {
return scriptGroup;
}
}
return 'Other';
};
/**
* Returns the script group of a language.
* @param language string Language code
* @return string script group name
*/
$.uls.data.getScriptGroupOfLanguage = function ( language ) {
return $.uls.data.getGroupOfScript( $.uls.data.getScript( language ) );
};
/**
* A callback for sorting languages by autonym.
* Can be used as an argument to a sort function.
* @param a string Language code
* @param b string Language code
*/
$.uls.data.sortByAutonym = function ( a, b ) {
var autonymA = $.uls.data.getAutonym( a ) || a,
autonymB = $.uls.data.getAutonym( b ) || b;
return ( autonymA.toLowerCase() < autonymB.toLowerCase() ) ? -1 : 1;
};
/**
* Check if a language is right-to-left.
* @param language string Language code
* @return boolean
*/
$.uls.data.isRtl = function ( language ) {
return $.inArray( $.uls.data.getScript( language ), $.uls.data.rtlscripts ) !== -1;
};
/**
* Return the direction of the language
* @param language string Language code
* @return string
*/
$.uls.data.getDir = function ( language ) {
return $.uls.data.isRtl( language ) ? 'rtl' : 'ltr';
};
/**
* Returns the languages spoken in a territory.
* @param territory string Territory code
* @return list of language codes
*/
$.uls.data.getLanguagesInTerritory = function ( territory ) {
return $.uls.data.territories[territory];
};
/**
* Adds a language in run time and sets its options as provided.
* If the target option is provided, the language is defined as a redirect.
* Other possible options are script, regions and autonym.
*
* @param code string New language code.
* @param options Object Language properties.
* @return list of language codes
*/
$.uls.data.addLanguage = function( code, options ) {
if ( options.target ) {
$.uls.data.languages[code] = [options.target];
} else {
$.uls.data.languages[code] = [options.script, options.regions, options.autonym];
}
};
/**
* Removes a language from the langdb in run time.
*
* @param code string Language code to delete.
* @return true if the language was removed, false otherwise.
*/
$.uls.data.deleteLanguage = function( code ) {
if ( $.uls.data.languages[code] ) {
delete $.uls.data.languages[code];
return true;
}
return false;
};
} ( jQuery ) );

View File

@@ -0,0 +1,345 @@
/**
* jQuery language filter plugin.
*
* 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
*/
/**
* Usage: $( 'inputbox' ).languagefilter();
* The values for autocompletion is from the options.languages.
* The data is in the format of languagecode:languagename.
*/
(function ( $ ) {
'use strict';
var LanguageFilter = function( element, options ) {
this.$element = $( element );
this.options = $.extend( {}, $.fn.regionselector.defaults, options );
this.$element.addClass( 'languagefilter' );
this.resultCount = 0;
this.$suggestion = this.$element.parents().find( '#' + this.$element.data( 'suggestion' ) );
this.$clear = this.$element.parents().find( '#'+ this.$element.data( 'clear' ) );
this.selectedLanguage = null;
this.listen();
};
var delay = ( function() {
var timer = 0;
return function( callback, milliseconds ) {
clearTimeout( timer );
timer = setTimeout( callback, milliseconds );
};
} () );
LanguageFilter.prototype = {
listen: function() {
this.$element.on( 'keypress', $.proxy( this.keyup, this ) )
.on( 'keyup', $.proxy( this.keyup, this ) );
if ( this.eventSupported( 'keydown' ) ) {
this.$element.on( 'keydown', $.proxy( this.keyup, this ) );
}
if ( this.$clear.length ) {
this.$clear.on( 'click' , $.proxy( this.clear, this ) );
}
this.toggleClear();
},
keyup: function( e ) {
switch( e.keyCode ) {
case 9: // Tab -> Autocomplete
var suggestion = this.$suggestion.val();
if ( suggestion && suggestion !== this.$element.val() ) {
this.$element.val( suggestion );
e.preventDefault();
e.stopPropagation();
}
break;
case 13: // Enter
if ( !this.options.onSelect ) {
break;
}
var query = $.trim( this.$element.val() ).toLowerCase();
if ( this.selectedLanguage ) {
// this.selectLanguage will be populated from a matching search
this.options.onSelect( this.selectedLanguage );
} else if ( this.options.languages[query] ) {
// Search is yet to happen (in timeout delay),
// but we have a matching language code.
this.options.onSelect( query );
}
break;
default:
var languageFilter = this;
if ( e.which < 32 ) {
// ignore any ASCII control characters
break;
}
this.selectedLanguage = null;
delay( function() {
if ( !languageFilter.$element.val() ) {
languageFilter.clear();
} else {
languageFilter.options.$target.empty();
languageFilter.search();
}
}, 300 );
this.toggleClear();
}
},
/**
* Clears the current search removing
* clear buttons and suggestions.
*/
deactivate: function() {
this.$element.val( '' );
if ( !$.fn.uls.Constructor.prototype.isMobile() ) {
this.$element.focus();
}
this.toggleClear();
this.autofill();
},
/**
* Clears the search and shows all languages
*/
clear: function() {
this.deactivate();
this.$element.trigger( 'searchclear' );
},
/**
* Toggles the visibility of clear icon depending
* on whether there is anything to clear.
*/
toggleClear: function() {
if ( !this.$clear.length ) {
return;
}
if ( this.$element.val() ) {
this.$clear.show();
} else {
this.$clear.hide();
}
},
search: function() {
var query = $.trim( this.$element.val() ),
languages = $.uls.data.getLanguagesByScriptGroup( this.options.languages ),
scriptGroup, langNum, langCode;
this.resultCount = 0;
for ( scriptGroup in languages ) {
var languagesInScript = languages[scriptGroup];
languagesInScript.sort( $.uls.data.sortByAutonym );
for ( langNum = 0; langNum < languagesInScript.length; langNum++ ) {
langCode = languagesInScript[langNum];
if ( query === '' || this.filter( langCode, query ) ) {
if ( this.resultCount === 0 ) {
// Autofill the first result.
this.autofill( langCode );
}
if ( query.toLowerCase() === langCode ) {
this.selectedLanguage = langCode;
}
this.render( langCode );
this.resultCount++;
}
}
}
// Also do a search by search API
if( !this.resultCount && this.options.searchAPI && query ) {
this.searchAPI( query );
} else {
this.resultHandler( query );
}
},
searchAPI: function( query ) {
var languageFilter = this;
$.get( languageFilter.options.searchAPI, { search: query }, function( result ) {
$.each( result.languagesearch, function( code, name ) {
if ( languageFilter.resultCount === 0 ) {
// Autofill the first result.
languageFilter.autofill( code, name );
}
languageFilter.render( code );
languageFilter.resultCount++;
} );
languageFilter.resultHandler( query );
} );
},
/**
* Handler method to be called once search is over.
* Based on search result call success or noresults callbacks
* @param query string
*/
resultHandler: function( query ) {
if ( this.resultCount === 0 && this.options.noresults ) {
this.$suggestion.val( '' );
this.options.noresults.call( this, query );
} else if ( this.options.success ) {
this.options.success( this, query, this.resultCount );
}
},
autofill: function( langCode, languageName ) {
if ( !this.$suggestion.length ) {
return;
}
if ( !this.$element.val() ) {
this.$suggestion.val( '' );
return;
}
this.selectedLanguage = langCode;
languageName = languageName || this.options.languages[langCode];
var autonym,
userInput = this.$element.val(),
suggestion = userInput + languageName.substring( userInput.length, languageName.length );
if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) {
// see if it was autonym match
autonym = $.uls.data.getAutonym( langCode ) || '';
suggestion = userInput + autonym.substring( userInput.length, autonym.length );
if ( suggestion !== autonym ) {
// Give up. It may be an ISO/script code match.
suggestion = '';
}
}
// Make sure that it is a visual prefix.
if ( !isVisualPrefix( userInput, suggestion ) ) {
suggestion = '';
}
this.$suggestion.val( suggestion );
},
render: function( langCode ) {
var $target = this.options.$target;
if ( !$target ) {
return;
}
$target.append( langCode, null );
},
escapeRegex: function( value ) {
return value.replace( /[\-\[\]{}()*+?.,\\\^$\|#\s]/g, '\\$&' );
},
/**
* A search match happens if any of the following passes:
* a) Language name in current user interface language
* 'starts with' search string.
* b) Language autonym 'starts with' search string.
* c) ISO 639 code match with search string.
* d) ISO 15924 code for the script match the search string.
*/
filter: function( langCode, searchTerm ) {
// FIXME script is ISO 15924 code. We might need actual name of script.
var matcher = new RegExp( '^' + this.escapeRegex( searchTerm ), 'i' ),
languageName = this.options.languages[langCode];
return matcher.test( languageName ) ||
matcher.test( $.uls.data.getAutonym( langCode ) ) ||
matcher.test( langCode ) ||
matcher.test( $.uls.data.getScript( langCode ) );
},
eventSupported: function ( eventName ) {
var isSupported = eventName in this.$element;
if ( !isSupported ) {
this.$element.setAttribute( eventName, 'return;' );
isSupported = typeof this.$element[eventName] === 'function';
}
return isSupported;
}
};
$.fn.languagefilter = function( option ) {
return this.each( function() {
var $this = $( this ),
data = $this.data( 'languagefilter' ),
options = typeof option === 'object' && option;
if ( !data ) {
$this.data( 'languagefilter', ( data = new LanguageFilter( this, options ) ) );
}
if ( typeof option === 'string' ) {
data[option]();
}
} );
};
$.fn.languagefilter.defaults = {
$target: null, // Where to append the results
searchAPI: null,
languages: null, // Languages as code:name format.
noresults: null, // callback for no results found case
success: null, // callback if any results found.
onSelect: null // Language select handler - like enter in filter textbox.
};
$.fn.languagefilter.Constructor = LanguageFilter;
/**
* Check if a prefix is visually prefix of a string
* @param prefix string
* @param string string
*/
function isVisualPrefix( prefix, string ) {
// Pre-base vowel signs of Indic languages. A vowel sign is called pre-base if
// consonant + vowel becomes [vowel][consonant] when rendered. Eg: ക + െ => കെ
var prebases = 'െേൈൊോൌெேைொோௌେୈୋୌિਿिিেৈোৌෙේෛොෝෞ';
return prebases.indexOf( string[prefix.length] ) <= 0;
}
} ( jQuery ) );

View File

@@ -0,0 +1,333 @@
/**
* 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';
/*jshint multistr:true */
var noResultsTemplate = '\
<div class="twelve columns uls-no-results-view">\
<h2 data-i18n="uls-no-results-found" class="eleven columns end offset-by-one uls-no-results-found-title">\
No results found\
</h2>\
<div id="uls-no-found-more" class="uls-no-found-more">\
<div class="ten columns end offset-by-one">\
<p>\
<span data-i18n="uls-search-help">You can search by language name, \
script name, ISO code of language or \
you can browse by region:</span>\
<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">Europe</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">Africa</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">Pacific</a>, \
<a class="uls-region-link" data-i18n="uls-region-WW" data-region="WW">Worldwide</a>.\
</p>\
</div>\
</div>\
</div>';
/*jshint multistr:false */
var LanguageCategoryDisplay = function ( element, options ) {
this.$element = $( element );
this.options = $.extend( {}, $.fn.lcd.defaults, options );
this.$element.addClass( 'lcd' );
this.regionDivs = {};
this.$element.append( $( noResultsTemplate ) );
this.$noResults = this.$element.find( 'div.uls-no-results-view' );
this.render();
this.listen();
};
LanguageCategoryDisplay.prototype = {
constructor: LanguageCategoryDisplay,
append: function ( langCode, regionCode ) {
this.addToRegion( langCode, regionCode );
this.$noResults.hide();
},
/**
* Add the language to a region.
* If the region parameter is given, add to that region alone
* Otherwise to all regions that this language belongs.
* @param langCode
* @param region Optional region
*/
addToRegion: function ( langCode, region ) {
var lcd = this,
language = lcd.options.languages[langCode],
langName = $.uls.data.getAutonym( langCode ) || language || langCode,
regions = [];
if ( region ) {
regions.push( region );
} else {
regions = $.uls.data.getRegions( langCode );
}
// World wide languages need not be repeated in all regions.
if ( $.inArray( 'WW', regions ) > -1 ) {
regions = [ 'WW' ];
}
for ( var i = 0; i < regions.length; i++ ) {
var regionCode = regions[i];
var $li = $( '<li>' )
.data( 'code', langCode )
.attr( {
lang: langCode,
dir: $.uls.data.getDir( langCode )
} )
.append(
$( '<a>' ).prop( 'title', language ).html( langName )
);
// Append the element to the column in the list
var $column = lcd.getColumn( regionCode );
var lastLanguage = $column.find( 'li:last' ).data( 'code' );
if ( lastLanguage ) {
var lastScriptGroup = $.uls.data.getScriptGroupOfLanguage( lastLanguage ),
currentScriptGroup = $.uls.data.getScriptGroupOfLanguage( langCode );
if ( lastScriptGroup !== currentScriptGroup ) {
if ( $column.find( 'li' ).length > 2 ) {
// If column already has 2 or more languages, add a new column
$column = lcd.getColumn( regionCode, true );
}
}
}
$column.append( $li );
}
},
/**
* Get a column to add language.
* @param regionCode string The region code
* @param forceNew bool whether a new column must be created or not
*/
getColumn: function ( regionCode, forceNew ) {
var $divRegionCode, $rowDiv, $ul;
forceNew = forceNew || false;
$divRegionCode = this.regionDivs[regionCode];
$rowDiv = $divRegionCode.find( 'div.row:last' );
$ul = $rowDiv.find( 'ul:last' );
// Each column can have maximum 8 languages.
if ( $ul.length === 0 || $ul.find( 'li' ).length >= 8 || forceNew ) {
$ul = $( '<ul>' ).addClass( 'three columns end' );
if ( $rowDiv.length === 0 || $rowDiv.find( 'ul' ).length >= 4 ) {
$rowDiv = $( '<div>' ).addClass( 'row uls-language-block' );
$divRegionCode.append( $rowDiv );
$ul.addClass( 'offset-by-one' );
}
$rowDiv.append( $ul );
}
// Don't show the region unless it was enabled
if ( $.inArray( regionCode, this.options.showRegions ) > -1 ) {
$divRegionCode.show();
}
return $ul;
},
render: function () {
var $section, $sectionTitle,
lcd = this,
regions = {
// These are fallback text when i18n library not present
WW: 'Worldwide',
SP: 'Special',
AM: 'America',
EU: 'Europe',
ME: 'Middle East',
AS: 'Asia',
AF: 'Africa',
PA: 'Pacific'
};
$.each( $.uls.data.regiongroups, function ( regionCode, regionIndex ) {
$section = $( '<div>' ).addClass( 'twelve columns uls-lcd-region-section' ).prop( 'id', regionCode );
$sectionTitle = $( '<h3 data-i18n="uls-region-' + regionCode + '">' )
.addClass( 'eleven columns uls-lcd-region-section uls-lcd-region-title offset-by-one' )
.text( regions[regionCode] );
$section.append( $sectionTitle );
lcd.$element.append( $section );
$section.hide();
lcd.regionDivs[regionCode] = $section;
} );
this.$noResults.hide();
this.i18n();
},
i18n: function ( ) {
this.$element.find( '[data-i18n]' ).i18n();
},
quicklist: function () {
if ( $.isFunction( this.options.quickList ) ) {
this.options.quickList = this.options.quickList();
}
if ( !this.options.quickList ) {
return;
}
// Pick only the first elements, because we don't have room for more
var quickList = this.options.quickList;
quickList = quickList.slice( 0, 16 );
quickList.sort( $.uls.data.sortByAutonym );
var $quickListSection = $( '<div>' ).addClass( 'twelve columns uls-lcd-region-section' ).prop( 'id', 'uls-lcd-quicklist' );
var $quickListSectionTitle = $( '<h3 data-i18n="uls-common-languages">' )
.addClass( 'eleven columns uls-lcd-region-section uls-lcd-region-title offset-by-one' )
.text( 'Common languages' ); // This is placeholder text if jquery.i18n not present
$quickListSection.append( $quickListSectionTitle );
this.$element.prepend( $quickListSection );
this.regionDivs.quick = $quickListSection;
for ( var i = 0; i < quickList.length; i++) {
var $column = this.getColumn( 'quick', i % 4 === 0 );
var langCode = quickList[i];
var language = this.options.languages[langCode];
var langName = $.uls.data.getAutonym( langCode ) || language || langCode;
var $li = $( '<li>' )
.data( 'code', langCode )
.attr( {
lang: langCode,
dir: $.uls.data.getDir( langCode )
} )
.append(
$( '<a>' ).prop( 'title', language ).html( langName )
);
$column.append( $li );
}
$quickListSection.show();
$quickListSectionTitle.i18n();
return $quickListSection;
},
show: function () {
if ( !this.regionDivs ) {
this.render();
}
},
empty: function () {
this.$element.find( 'div.uls-language-block' ).remove();
this.$element.find( 'div.uls-lcd-region-section' ).hide();
},
focus: function () {
this.$element.focus();
},
noResults: function () {
this.$noResults.show();
var $suggestions = this.quicklist();
$suggestions.find( 'h3' )
.data( 'i18n', 'uls-no-results-suggestion-title' )
.text( 'You may be interested in:' )
.i18n();
this.$noResults.find( 'h2' ).after( $suggestions );
},
listen: function () {
var lcd = this;
if ( this.options.clickhandler ) {
this.$element.on( 'click', 'div.row li', function () {
lcd.options.clickhandler.call( this, $( this ).data( 'code' ) );
} );
}
// The region section need to be in sync with the map filter.
lcd.$element.scroll( function () {
var inview, inviewRegion,
$ulsLanguageList = $( this ),
scrollTop = $ulsLanguageList.position().top,
scrollBottom = $ulsLanguageList.height();
if ( lcd.options.lazyload && lcd.options.source.val() === '' ) {
if ( this.offsetHeight + this.scrollTop >= this.scrollHeight / 2 ) {
lcd.$element.trigger( 'scrollend' );
}
}
// The region section need to be in sync with the map filter.
inviewRegion = 'WW';
lcd.$element.find( 'div.uls-lcd-region-section' ).each( function () {
var $lcdRegionSection = $( this ),
top = $lcdRegionSection.position().top,
height = $lcdRegionSection.height(),
padding = 10;
if ( top - padding <= scrollTop && height > scrollBottom ) {
inviewRegion = $lcdRegionSection.attr( 'id' );
return true;
}
} );
inview = $.uls.data.regiongroups[inviewRegion];
$( '.regionselector' ).removeClass( 'active' );
$( '#uls-region-' + inview ).addClass( 'active' );
} );
}
};
$.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 = {
languages: null,
showRegions: ['WW', 'AM', 'EU', 'ME', 'AF', 'AS', 'PA'],
lazyload: true
};
$.fn.lcd.Constructor = LanguageCategoryDisplay;
} ( jQuery ) );

View File

@@ -0,0 +1,202 @@
/**
* jQuery region filter plugin.
*
* 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';
/* RegionSelector plugin definition */
/**
* Region selector is a language selector based on regions.
* Usage: $( 'jqueryselector' ).regionselector( options );
* The attached element should have data-regiongroup attribute
* that defines the regiongroup for the selector.
*/
var RegionSelector = function ( element, options ) {
this.$element = $( element );
this.options = $.extend( {}, $.fn.regionselector.defaults, options );
this.$element.addClass( 'regionselector' );
this.regions = [];
this.cache= null;
this.regionGroup = this.$element.data( 'regiongroup' );
this.init();
this.listen();
};
RegionSelector.prototype = {
constructor: RegionSelector,
init: function () {
var region = this.$element.data( 'region' );
this.regions = $.uls.data.getRegionsInGroup( this.regionGroup );
if ( region ) {
this.regions.push( region );
}
},
test: function ( langCode ) {
var langRegions = $.uls.data.getRegions( langCode ),
region;
for ( var i = 0; i < this.regions.length; i++ ) {
region = this.regions[i];
if ( $.inArray( region, langRegions ) >= 0 ) {
this.render( langCode, region );
this.cache[langCode] = region;
return;
}
}
},
show: function () {
if ( this.cache ) {
// If the result cache is present, render the results from there.
//noinspection JSUnusedAssignment
var result = null;
for ( result in this.cache ) {
this.render( result, this.cache[result] );
}
} else {
this.cache = {};
// Get the languages grouped by script group
var languagesByScriptGroup = $.uls.data.getLanguagesByScriptGroup( this.options.languages );
// Make sure that we go by the original order
// of script groups
for ( var scriptGroup in $.uls.data.scriptgroups ) {
// Get the languages for the script group
var languages = languagesByScriptGroup[scriptGroup];
// It's possible that some script groups are missing
if ( !languages ) {
continue;
}
// Sort it based on autonym
languages.sort( $.uls.data.sortByAutonym );
for ( var i = 0; i < languages.length; i++ ) {
// Check whether it belongs to the region
this.test( languages[i] );
}
}
}
if ( this.options.success ) {
this.options.success( this );
}
},
render: function ( langCode, region ) {
var $target = this.options.$target;
if ( !$target ) {
return;
}
$target.append( langCode, region );
},
next: function () {
var regionSelector = this;
if ( !this.$element.hasClass( 'active' ) ) {
return true;
}
// Do not respond to all scroll end events, but only after a short interval
delay( function () {
var nextRegionGroupNumber = regionSelector.$element.data( 'regiongroup' ) + 1,
$nextRegion = $( '#uls-region-' + nextRegionGroupNumber ),
nextRegionSelector = $nextRegion.length && $nextRegion.data( 'regionselector' );
// If cache is defined, then it is already rendered and there's no need
// to re-render it.
if ( nextRegionSelector && nextRegionSelector.cache === null ) {
nextRegionSelector.show();
}
}, 100 );
return false;
},
listen: function () {
this.$element.on( 'click', $.proxy( this.click, this ) );
this.options.$target.$element.bind( 'scrollend', $.proxy( this.next, this ) );
},
click: function ( e ) {
// Don't do anything if a region is selected already
if ( this.$element.hasClass( 'active' ) ) {
return;
}
// Re-populate the list of languages
this.options.$target.empty();
this.show();
// Make the selected region (and it only) active
$( '.regionselector' ).removeClass( 'active' );
if ( this.regionGroup ) {
// if there is a region group, make it active.
this.$element.addClass( 'active' );
}
}
};
/* RegionSelector plugin definition */
$.fn.regionselector = function ( option ) {
return this.each( function () {
var $this = $( this ),
data = $this.data( 'regionselector' ),
options = typeof option === 'object' && option;
if ( !data ) {
$this.data( 'regionselector', ( data = new RegionSelector( this, options ) ) );
}
if ( typeof option === 'string' ) {
data[option]();
}
} );
};
$.fn.regionselector.defaults = {
$target: null, // Where to render the results
success: null, // callback if any results found.
noresults: null, // callback when no results to show
languages: null
};
$.fn.regionselector.Constructor = RegionSelector;
var delay = ( function () {
var timer = 0;
return function ( callback, milliseconds ) {
clearTimeout( timer );
timer = setTimeout( callback, milliseconds );
};
} () );
} ( jQuery ) );