Rearranging Files

* Seperating standalone jQuery ULS plugin and extension related code
* lib/jquery.uls can be submodule, but not now.

Change-Id: I7d9cb47daa88dd1a27ceda602a08cab0073caf33
This commit is contained in:
Santhosh Thottingal
2012-08-03 12:24:04 +05:30
parent 96304a6d22
commit 878313d2ec
18 changed files with 30 additions and 30 deletions

View File

@@ -0,0 +1,197 @@
/**
* <explain briefly what this file is about>.
*
* 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";
var ULS = function( element, options ) {
this.$element = $( element );
this.options = $.extend( {}, $.fn.uls.defaults, options );
this.$menu = $( this.options.menu );
this.languages = this.$menu.data( 'languages' );
for ( var code in this.languages ) {
if ( $.uls.data.languages[code] === undefined ) {
window.console && console.log && console.log( "ULS: Unknown language " + code + "." );
delete this.languages[code];
}
}
this.shown = false;
this.$languageFilter = $( 'input#languagefilter' );
this.$noResultsView = $( 'div.uls-no-results-view' );
this.$resultsView = $( 'div.uls-language-list' );
this.$noResultsView.hide();
this.render();
this.listen();
this.ready();
};
ULS.prototype = {
constructor: ULS,
ready: function() {
// Initialize with a full search.
this.$languageFilter.val( "" ).languagefilter( "search" );
},
show: function() {
var pos = $.extend( {}, this.$element.offset(), {
height: this.$element[0].offsetHeight
} );
this.$menu.css( {
top: pos.top + pos.height,
left: '25%'
} );
this.$menu.show();
this.shown = true;
this.$languageFilter.focus();
return this;
},
hide: function() {
this.$menu.hide();
this.shown = false;
return this;
},
render: function() {
// Rendering stuff here
},
noresults: function( search ) {
this.$noResultsView.show();
// FIXME i18n
this.$noResultsView.find( 'span#uls-no-found-search-term' ).text( search );
this.$resultsView.hide();
},
success: function() {
this.$noResultsView.hide();
this.$resultsView.show();
},
listen: function() {
var that = this, lcd;
// Register all event listeners to the ULS here.
that.$element.on( 'click', $.proxy( that.click, that ) );
// Handle click on close button
$( ".icon-close" ).on( 'click', $.proxy( that.click, that ) );
// Handle key press events on the menu
that.$menu.on('keypress', $.proxy(this.keypress, this) )
.on('keyup', $.proxy(this.keyup, this) );
if ( $.browser.webkit || $.browser.msie ) {
this.$menu.on( 'keydown', $.proxy( this.keypress, this ) )
}
lcd = that.$resultsView.lcd( {
languages: that.languages,
clickhandler: function( langCode ) {
if ( that.options.onSelect ) {
that.options.onSelect.call( this, langCode );
}
}
} ).data( "lcd" );
that.$languageFilter.languagefilter( {
$target: lcd,
languages: that.languages,
success: $.proxy( that.success, that ),
noresults: $.proxy( that.noresults, that ),
searchAPI: that.options.searchAPI
} );
// Create region selectors, one per region
$( '.uls-region, .uls-region-link' ).regionselector( {
$target: lcd,
languages: that.languages,
success: function() {
// clear the search field.
that.$languageFilter.languagefilter( 'clear' );
that.success();
}
} );
},
keyup: function( e ) {
if ( !this.shown ) {
return;
}
switch( e.keyCode ) {
case 27: // escape
this.hide();
e.preventDefault();
break;
}
e.stopPropagation();
},
keypress: function( e ) {
if ( !this.shown ) {
return;
}
switch( e.keyCode ) {
case 27: // escape
this.hide();
e.preventDefault();
break;
}
e.stopPropagation();
},
click: function( e ) {
e.stopPropagation();
e.preventDefault();
if ( !this.shown ) {
this.show();
} else {
this.hide();
}
}
};
/* 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: '.uls-menu',
onSelect: null, // Callback function to be called when a language is selected
searchAPI: null // Language search API
};
$.fn.uls.Constructor = ULS;
} )( jQuery );

View File

@@ -0,0 +1,301 @@
/**
* 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";
/**
* Returns the script of the language.
* @param string language code
* @return string
*/
$.uls.data.script = function( language ) {
return $.uls.data.languages[language][0];
};
/**
* Returns the regions in which a language is spoken.
* @param string language code
* @return array of strings
*/
$.uls.data.regions = function( language ) {
return ( $.uls.data.languages[language] && $.uls.data.languages[language][1] ) || 'UNKNOWN';
};
/**
* Returns the autonym of the language.
* @param string language code
* @return string
*/
$.uls.data.autonym = function( language ) {
return $.uls.data.autonyms[language];
};
/**
* Returns an array of all region codes.
* @return array
*/
$.uls.data.allRegions = function() {
var allRegions = [];
for( var 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.languagesInScript = function( script ) {
return $.uls.data.languagesInScripts( [ script ] );
};
/**
* Returns all languages written in the given scripts.
* @param scripts array of strings
* @return array of strings (languages codes)
*/
$.uls.data.languagesInScripts = function( scripts ) {
var languagesInScripts = [];
for ( var language in $.uls.data.languages ) {
for ( var i = 0; i < scripts.length; i++ ) {
if ( scripts[i] === $.uls.data.script(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.languagesInRegion = function( region ) {
return $.uls.data.languagesInRegions( [ region ] );
};
/**
* Returns all languages in given regions.
* @param region array of strings.
* @return array of strings (languages codes)
*/
$.uls.data.languagesInRegions = function( regions ) {
var languagesInRegions = [];
for ( var language in $.uls.data.languages ) {
for ( var i = 0; i < regions.length; i++ ) {
if ( $.inArray( regions[i], $.uls.data.regions( 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.languagesInRegionGroup = function( groupNum ) {
return $.uls.data.languagesInRegions( $.uls.data.regionsInGroup( groupNum ) );
};
/**
* Returns an associative array of languages in a region,
* grouped by script.
* @param string region code
* @return associative array
*/
$.uls.data.languagesByScriptInRegion = function( region ) {
var languagesByScriptInRegion = {};
for ( var language in $.uls.data.languages ) {
if ( $.inArray( region, $.uls.data.regions( language ) ) !== -1 ) {
var script = $.uls.data.script( 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 string region code
* @return associative array
*/
$.uls.data.languagesByScriptGroupInRegion = function( region ) {
return $.uls.data.languagesByScriptGroupInRegions( [ region ] );
};
/**
* Returns an associative array of all languages,
* grouped by script group.
* @return associative array
*/
$.uls.data.allLanguagesByScriptGroup = function() {
return $.uls.data.languagesByScriptGroupInRegions( $.uls.data.allRegions() );
};
/**
* Get the given list of languages sorted by script.
* @param languages Array of language codes
* @return {Object} Array of languages indexed by script codes
*/
$.uls.data.languagesByScriptGroup = function( languages ) {
var languagesByScriptGroup = {}, scriptGroup, language, langScriptGroup;
for ( scriptGroup in $.uls.data.scriptgroups ) {
for ( language in languages ) {
langScriptGroup = $.uls.data.scriptGroupOfLanguage( language );
if( langScriptGroup !== scriptGroup ){
continue;
}
if ( !languagesByScriptGroup[scriptGroup] ) {
languagesByScriptGroup[scriptGroup] = [];
}
languagesByScriptGroup[scriptGroup].push( language );
}
}
return languagesByScriptGroup;
};
/**
* Returns an associative array of languages in several regions,
* grouped by script group.
* @param array of strings - region codes
* @return associative array
*/
$.uls.data.languagesByScriptGroupInRegions = function( regions ) {
var languagesByScriptGroupInRegions = {};
for ( var language in $.uls.data.languages ) {
for ( var i = 0; i < regions.length; i++ ) {
if ( $.inArray( regions[i], $.uls.data.regions( language ) ) !== -1 ) {
var scriptGroup = $.uls.data.scriptGroupOfLanguage( 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.allLanguagesByRegionAndScript = function() {
var allLanguagesByRegionAndScript = {},
region,
regionGroup;
for ( region in $.uls.data.regiongroups ) {
regionGroup = $.uls.data.regiongroups[region];
if ( allLanguagesByRegionAndScript[regionGroup] === undefined ) {
allLanguagesByRegionAndScript[regionGroup] = {};
}
allLanguagesByRegionAndScript[regionGroup][region] = {};
}
for ( var language in $.uls.data.languages ) {
var script = $.uls.data.script( language );
var scriptGroup = $.uls.data.groupOfScript( script );
var regions = $.uls.data.regions( language );
for ( var 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 number groupNum
* @return array of strings
*/
$.uls.data.regionsInGroup = function( groupNum ) {
var regionsInGroup = [];
for ( var 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 string script code
* @return string script group name
*/
$.uls.data.groupOfScript = function( script ) {
for ( var group in $.uls.data.scriptgroups ) {
if ( $.inArray( script, $.uls.data.scriptgroups[group] ) !== -1 ) {
return group;
}
}
return 'Other';
};
/**
* Returns the script group of a language.
* @param string language code
* @return string script group name
*/
$.uls.data.scriptGroupOfLanguage = function( language ) {
return $.uls.data.groupOfScript( $.uls.data.script( language ) );
};
} )( jQuery );

View File

@@ -0,0 +1,338 @@
/**
* 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.data( 'suggestion' ) );
this.$clear = $( '#'+ this.$element.data( 'clear' ) );
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 ( $.browser.webkit || $.browser.msie ) {
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();
} else {
this.options.$target.focus();
}
default:
var that = this;
delay( function() {
that.options.$target.empty();
that.search();
}, 500 );
this.toggleClear();
}
},
clear: function() {
this.$element.val( '' );
this.$element.focus();
this.toggleClear();
this.search();
},
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.languagesByScriptGroup( this.options.languages ),
scriptGroup, langNum, langCode;
this.resultCount = 0;
for ( scriptGroup in languages ) {
for ( langNum = 0; langNum < languages[scriptGroup].length; langNum++ ) {
langCode = languages[scriptGroup][langNum];
if ( query === "" || this.filter( langCode, query ) ) {
if ( this.resultCount === 0 ) {
// Autofill the first result.
this.autofill( langCode );
}
this.render( langCode );
this.resultCount++;
}
}
}
// Also do a search by search API
if( this.options.searchAPI && query ) {
this.searchAPI( query );
} else {
this.resultHandler( query );
}
},
searchAPI: function( query ) {
var that = this;
$.get( that.options.searchAPI, { search: query }, function( result ) {
$.each( result['languagesearch'], function( code, name ) {
that.render( code, name );
that.resultCount++;
} );
that.resultHandler( query );
} );
},
/**
* Handler method to be called once search is over.
* Based on search result call success or noresults callbacks
* @param String query
*/
resultHandler: function( query ) {
if ( this.resultCount === 0 && this.options.noresults ) {
this.options.noresults.call( this, query );
} else if ( this.options.success ) {
this.options.success( this, query, this.resultCount );
}
},
autofill: function( langCode ) {
if ( !this.$suggestion.length ) {
return;
}
if ( !this.$element.val() ) {
this.$suggestion.val( '' );
return;
}
var autonym,
languageName = this.options.languages[langCode],
userInput = this.$element.val(),
suggestion = userInput + languageName.substring( userInput.length, languageName.length );
if ( suggestion !== languageName ) {
// see if it was autonym match
autonym = $.uls.data.autonym( langCode );
suggestion = userInput + autonym.substring( userInput.length, autonym.length );
if ( suggestion !== autonym ) {
// Give up. It may be iso/script code match.
suggestion = "";
}
}
this.$suggestion.val( suggestion );
},
render: function( langCode, languageName ) {
var $target = this.options.$target;
if ( !$target ) {
return;
}
$target.append( langCode, null, languageName );
},
escapeRegex: function( value ) {
// This is a prefix search.
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.autonym( langCode ) ) ||
matcher.test( langCode ) ||
matcher.test( $.uls.data.script( langCode ) );
}
};
$.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.
};
$.fn.languagefilter.Constructor = LanguageFilter;
/* 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.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.regionsInGroup( this.regionGroup );
if ( region ) {
this.regions.push( region );
}
},
test: function( langCode ) {
var langRegions = $.uls.data.regions( langCode ),
region;
for ( var i = 0; i < this.regions.length; i++ ) {
region = this.regions[i];
if ( $.inArray( region, langRegions ) >= 0 ) {
this.render( langCode, region );
return;
}
}
},
show: function() {
var i, regions, language, languagesByScriptGroup, scriptGroup, languages;
// 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' );
}
// Re-populate the list of languages
this.options.$target.empty();
languagesByScriptGroup = $.uls.data.languagesByScriptGroup( this.options.languages );
for ( scriptGroup in languagesByScriptGroup ) {
languages = languagesByScriptGroup[scriptGroup];
for ( i = 0; i < languages.length; i++) {
language = languages[i];
this.test( language );
}
}
if ( this.options.success ) {
this.options.success.call();
}
},
render: function( langCode, region ) {
var $target = this.options.$target;
if ( !$target ) {
return;
}
$target.append( langCode, region );
},
listen: function() {
this.$element.on( 'click', $.proxy( this.click, this ) );
},
click: function( e ) {
e.stopPropagation();
e.preventDefault();
this.show();
}
};
/* 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.
languages: null
};
$.fn.regionselector.Constructor = RegionSelector;
} )( jQuery );

View File

@@ -0,0 +1,172 @@
/**
* <explain briefly what this file is about>.
*
* 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";
var LanguageCategoryDisplay = function( element, options ) {
this.$element = $( element );
this.options = $.extend( {}, $.fn.lcd.defaults, options );
this.$element.addClass( 'lcd' );
this.show();
this.listen();
};
LanguageCategoryDisplay.prototype = {
constructor: LanguageCategoryDisplay,
append: function( langCode, regionCode, languageName ) {
var that = this;
this.addToRegion( langCode, regionCode, languageName );
},
/**
* Check whether a language code is already displayed or not.
* @param langCode
* @return boolean
*/
exists: function( langCode ) {
return this.$element.find( 'li' ).filter(function() {
return $(this).data('code') === langCode;
} ).length > 0;
},
/**
* 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
* @param languageName Optional languageName
*/
addToRegion: function( langCode, region, languageName) {
var that = this;
if ( that.exists( langCode ) ) {
return;
}
var language = $.uls.data.languages[langCode],
langName = languageName
|| $.uls.data.autonym( langCode )
|| that.options.languages[langCode]
|| langCode,
regions = [];
if ( region ) {
regions.push( region );
} else {
regions = $.uls.data.regions( langCode );
}
for ( var i = 0; i < regions.length; i++ ) {
var regionCode = regions[i];
var $li = $( '<li>' )
.data( 'code', langCode )
.append(
$( '<a>' ).prop( 'href', '#' ).prop( 'title', language ).html( langName )
);
// Append the element to the column in the list
var column = that.getColumn( regionCode );
column.append( $li );
if ( that.options.clickhandler ) {
$li.click( function() {
that.options.clickhandler.call( this, langCode );
} );
}
}
},
getColumn: function( regionCode ) {
var $divRegionCode = $( 'div#' + regionCode );
var $rowDiv = $divRegionCode.find( 'div.row:last' );
var $ul = $divRegionCode.find( 'ul:last' );
// Each column can have maximum 10 languages.
if ( $ul.length === 0 || $ul.find( 'li' ).length >= 10 ) {
// Each row can have 4 columns with 10 languages.
$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 );
}
$divRegionCode.show();
return $ul;
},
show: function() {
var that = this;
$.each( $.uls.data.regiongroups, function( regionCode, regionIndex ) {
var $section = $( '<div>' ).addClass( 'twelve columns uls-lcd-region-section' ).prop( 'id', regionCode );
$section.append( $( '<h3>' ).addClass( 'eleven columns uls-lcd-region-section offset-by-one' ).html( regionCode ) );
// FIXME this is regioncode(NA, EU etc). Should be proper localized region name.
that.$element.append( $section );
} );
},
empty: function() {
this.$element.find( 'div.row' ).remove();
this.$element.find( 'div' ).hide();
},
listen: function() {
var that = this;
// The region section need to be in sync with the map filter.
that.$element.scroll( function () {
var inviewRegion = $( 'div.uls-lcd-region-section:first' ).attr( 'id' );
var listtop = that.$element.position().top;
$( 'div.uls-lcd-region-section' ).each( function () {
var offset = $( this ).position().top - listtop;
if ( offset < 0 ) {
inviewRegion = $( this ).attr( 'id' );
} else {
return false;
}
} );
var inview = $.uls.data.regiongroups[inviewRegion];
$( 'div.uls-region' ).removeClass( 'active' );
$( 'div#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
};
$.fn.lcd.Constructor = LanguageCategoryDisplay;
} )( jQuery );