Files
mediawiki-extensions-Univer…/resources/ext.uls.core.js
Santhosh Thottingal 05eb91bee3 cleanup and optimization
* reused jquery object  $( 'input#languagefilter' )
* handled the clean button click and escape press on ULS
* some code cleanup.

Change-Id: I5b8b597dceb4cf273a7eff2761f42557828e630b
2012-07-24 14:29:32 +05:30

183 lines
4.4 KiB
JavaScript

/**
* <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.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
},
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 = $( 'div.uls-language-list' ).lcd( {
languages: that.languages,
clickhandler: function( langCode ) {
if ( that.options.onSelect ) {
that.options.onSelect.call( this, langCode );
}
}
} ).data( "lcd" );
that.$languageFilter.languagefilter( {
$target: $lcd, //$( 'ul.uls-language-filter-result' ),
languages: that.languages
} );
// Create region selectors, one per region
$( '.uls-region' ).regionselector( {
$target: $lcd,
// FIXME This is confusing: languages and source are actually data for ULS.
languages: that.languages,
callback: function () {
// clear the search field.
that.$languageFilter.val( "" );
}
} );
$( '.clear-button' ).on( 'click', function() {
// go to the ready state.
that.ready();
} );
},
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
};
$.fn.uls.Constructor = ULS;
} )( jQuery );