http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95839
Revision: 95839
Author: santhosh
Date: 2011-08-31 07:22:00 +0000 (Wed, 31 Aug 2011)
Log Message:
-----------
Refactor the menu building code to a seperate method.
Modified Paths:
--------------
trunk/extensions/WebFonts/js/webfonts.js
Modified: trunk/extensions/WebFonts/js/webfonts.js
===================================================================
--- trunk/extensions/WebFonts/js/webfonts.js 2011-08-31 07:03:52 UTC (rev
95838)
+++ trunk/extensions/WebFonts/js/webfonts.js 2011-08-31 07:22:00 UTC (rev
95839)
@@ -8,7 +8,7 @@
/* Version number */
version: "0.1.2",
set: function( font ) {
- if ( font === "none" ) {
+ if ( !font || font === "none" ) {
$.webfonts.reset();
return;
}
@@ -19,7 +19,6 @@
} else {
config = $.webfonts.config.fonts[font];
}
-
//load the style sheet for the font
$.webfonts.loadcss(font);
@@ -139,119 +138,123 @@
* It also apply the font from cookie, if any.
*/
setup: function() {
- var haveSchemes = false;
var config = mw.config.get( "wgWebFontsAvailable" );
// Build font dropdown
- $fontsmenu = $( '<ul />'
).attr('id','webfonts-fontsmenu');
+ $.webfonts.buildMenu(config );
+ //see if there is a font in cookie
+ cookie_font = $.cookie('webfonts-font');
+ if(cookie_font == null){
+ $.webfonts.set( config[0]);
+ //mark it as checked
+ $('#webfont-'+config[0]).attr('checked',
'checked');
+ }
+ else{
+ if (cookie_font !=='none'){
+ $.webfonts.set( cookie_font);
+ //mark it as checked
+
$('#webfont-'+cookie_font).attr('checked', 'checked');
+ }
+ }
+
+ //if there are tags with font-family style definition,
get a list of fonts to be loaded
+ var fontFamilies = new Array();
+ $('body').find('*[style]').each(function(index){
+ if( this.style.fontFamily){
+ var fontFamilyItems =
this.style.fontFamily.split(",");
+ $.each(fontFamilyItems, function(index,
value) {
+ fontFamilies.push(value );
+ });
+ }
+ });
+ //get unique list
+ fontFamilies = $.unique(fontFamilies);
+ //load css for each of the item in fontfamily list
+ $.each(fontFamilies, function(index, fontFamily) {
+ //remove the ' characters if any.
+ fontFamily = fontFamily.replace(/'/g,'');
+ if ( fontFamily in $.webfonts.config.fonts ) {
+ $.webfonts.loadcss(fontFamily);
+ }
+ });
+
+ },
+ buildMenu : function(config) {
+ var haveSchemes = false;
+ // Build font dropdown
+ $fontsMenu = $( '<ul />'
).attr('id','webfonts-fontsmenu');
for ( var scheme in config ) {
- $fontlink = $( '<input>' )
+ $fontLink = $( '<input>' )
.attr("type","radio")
.attr("name","font")
.attr("id","webfont-"+config[scheme])
.attr("value",config[scheme] );
- $fontlabel = $( '<label />' )
+ $fontLabel = $( '<label />' )
.attr("for","webfont-"+config[scheme])
- .append( $fontlink )
- .append( config[scheme] )
-
- $fontmenuitem = $( '<li />' )
+ .append( $fontLink )
+ .append( config[scheme] );
+
+ $fontMenuItem = $( '<li />' )
.val( config[scheme] )
- .append( $fontlabel )
+ .append( $fontLabel );
-
haveSchemes = true;
//some closure trick :)
(function (font) {
- $fontlink.click( function( event ) {
+ $fontLink.click( function( event ) {
$.webfonts.set( font );
})
}) (config[scheme]);
- $fontsmenu.append($fontmenuitem);
+ $fontsMenu.append($fontMenuItem);
+
}
- $resetlink = $( '<input />' )
- .attr("type","radio")
- .attr("name","font")
- .attr("value","webfont-none")
- .attr("id","webfont-none")
- .click( function( event ) {
- $.webfonts.set( 'none');
- });
+ $resetLink = $( '<input />' )
+ .attr("type","radio")
+ .attr("name","font")
+ .attr("value","webfont-none")
+ .attr("id","webfont-none")
+ .click( function( event ) {
+ $.webfonts.set( 'none');
+ });
- $resetlabel = $( '<label />' )
- .attr("for","webfont-none")
- .append( $resetlink )
- .append( mw.msg("webfonts-reset"));
-
- $resetlinkitem = $( '<li />' )
+ $resetLabel = $( '<label />' )
+ .attr("for","webfont-none")
+ .append( $resetLink )
+ .append( mw.msg("webfonts-reset"));
+
+ $resetLinkItem = $( '<li />' )
.val( 'none')
- .append( $resetlabel )
-
+ .append( $resetLabel )
- $fontsmenu.append($resetlinkitem);
+ $fontsMenu.append($resetLinkItem);
+ if ( !haveSchemes ) {
+ // No schemes available, don't show the
tool
+ return;
+ }
- if (haveSchemes ) {
+ var $menuDiv = $( '<div />'
).attr('id','webfonts-fonts')
+ .addClass( 'menu' )
+ .append( $fontsMenu )
+ .append();
- var $menudiv = $( '<div />'
).attr('id','webfonts-fonts')
- .addClass( 'menu' )
- .append( $fontsmenu )
- .append();
-
- var $div = $( '<div />'
).attr('id','webfonts-menu')
+ var $div = $( '<div />' ).attr('id','webfonts-menu')
.addClass( 'webfontMenu' )
.append( "<a href='#'>"+
mw.msg("webfonts-load")+"</a>")
- .append( $menudiv );
+ .append( $menuDiv );
- //this is the fonts link
- var $li = $( '<li />' ).attr('id','pt-webfont')
+ //this is the fonts link
+ var $li = $( '<li />' ).attr('id','pt-webfont')
.append( $div );
- //if rtl, add to the right of top personal
links. Else, to the left
- if($('body').hasClass('rtl')){
- $($('#p-personal ul')[0]).append( $li );
- }
- else{
- $($('#p-personal ul')[0]).prepend( $li
);
- }
- //see if there is a font in cookie
- cookie_font = $.cookie('webfonts-font');
- if(cookie_font == null){
- $.webfonts.set( config[0]);
- //mark it as checked
-
$('#webfont-'+config[0]).attr('checked', 'checked');
- }
- else{
- if (cookie_font !=='none'){
- $.webfonts.set( cookie_font);
- //mark it as checked
-
$('#webfont-'+cookie_font).attr('checked', 'checked');
- }
- }
- }
- //if there are tags with font-family style definition,
get a list of fonts to be loaded
- var fontFamilies = new Array();
- $('body').find('*[style]').each(function(index){
- if( this.style.fontFamily){
- var fontFamilyItems =
this.style.fontFamily.split(",");
- $.each(fontFamilyItems, function(index,
value) {
- fontFamilies.push(value );
- });
- }
- });
- //get unique list
- fontFamilies = $.unique(fontFamilies);
- //load css for each of the item in fontfamily list
- $.each(fontFamilies, function(index, fontFamily) {
- //remove the ' characters if any.
- fontFamily = fontFamily.replace(/'/g,'');
- if ( fontFamily in $.webfonts.config.fonts ) {
- $.webfonts.loadcss(fontFamily);
- }
- });
-
+ //if rtl, add to the right of top personal links. Else,
to the left
+ if($('body').is( '.rtl' ) ){
+ $($('#p-personal ul')[0]).append( $li );
+ }
+ else{
+ $($('#p-personal ul')[0]).prepend( $li );
+ }
}
-
}
$( document ).ready( function() {
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs