r91818 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91817‎ | r91818 | r91819 >
Date:07:02, 10 July 2011
Author:santhosh
Status:deferred
Tags:
Comment:
Refactored the css construction part to a new function.
Modified paths:
  • /trunk/extensions/WebFonts/js/webfonts.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WebFonts/js/webfonts.js
@@ -2,8 +2,10 @@
33
44 $.webfonts = {
55
 6+
 7+ oldconfig: false,
 8+ config : mw.config.get( "wgWebFonts" ),
69 /* Version number */
7 - oldconfig: false,
810 version: "0.1.2",
911 set: function( font ) {
1012 if ( font === "none" ) {
@@ -11,36 +13,16 @@
1214 return;
1315 }
1416
15 - var config = mw.config.get( "wgWebFonts" );
16 - if ( !font in config.fonts ) {
 17+ if ( !font in $.webfonts.config.fonts ) {
1718 console.log( "Requested unknown font", font );
1819 return;
1920 } else {
20 - config = config.fonts[font];
 21+ config = $.webfonts.config.fonts[font];
2122 }
2223
23 - var styleString =
24 - "<style type='text/css'>\n@font-face {\n"
25 - + "\tfont-family: '"+font+"';\n";
26 - if ( 'eot' in config ) {
27 - styleString += "\tsrc: url('"+config.eot+"');\n";
28 - }
29 - //If the font is present locally, use it.
30 - styleString += "\tsrc: local('"+ font +"'),";
 24+ //load the style sheet for the font
 25+ $.webfonts.loadcss(font);
3126
32 - if ( 'woff' in config ) {
33 - styleString += "\t\turl('"+config.woff+"') format('woff'),";
34 - }
35 - if ( 'svg' in config ) {
36 - styleString += "\t\turl('"+config.svg+"#"+font+"') format('svg'),";
37 - }
38 - if ( 'ttf' in config ) {
39 - styleString += "\t\turl('"+config.ttf+"') format('truetype');\n";
40 - }
41 -
42 - styleString += "\tfont-weight: normal;\n}\n</style>\n";
43 - $(styleString).appendTo("head" );
44 -
4527 //save the current font and its size. Used for reset.
4628 if ( !$.webfonts.oldconfig ) {
4729 $.webfonts.oldconfig = {
@@ -120,7 +102,39 @@
121103 });
122104 });
123105 },
124 -
 106+
 107+ /*
 108+ * Construct the css required for the fontfamily, inject it to the head of the body
 109+ * so that it gets loaded.
 110+ * @param fontfamily The fontfamily name
 111+ */
 112+ loadcss: function(fontfamily){
 113+ var fontconfig = $.webfonts.config.fonts[fontfamily];
 114+ var styleString =
 115+ "<style type='text/css'>\n@font-face {\n"
 116+ + "\tfont-family: '"+fontfamily+"';\n";
 117+ if ( 'eot' in fontconfig ) {
 118+ styleString += "\tsrc: url('"+fontconfig.eot+"');\n";
 119+ }
 120+ //If the font is present locally, use it.
 121+ styleString += "\tsrc: local('"+ fontfamily +"'),";
 122+
 123+ if ( 'woff' in fontconfig ) {
 124+ styleString += "\t\turl('"+fontconfig.woff+"') format('woff'),";
 125+ }
 126+ if ( 'svg' in fontconfig ) {
 127+ styleString += "\t\turl('"+fontconfig.svg+"#"+fontfamily+"') format('svg'),";
 128+ }
 129+ if ( 'ttf' in fontconfig ) {
 130+ styleString += "\t\turl('"+fontconfig.ttf+"') format('truetype');\n";
 131+ }
 132+
 133+ styleString += "\tfont-weight: normal;\n}\n</style>\n";
 134+
 135+ //inject the css to the head of the page.
 136+ $(styleString).appendTo("head" );
 137+
 138+ },
125139 /**
126140 * Setup the font selection menu.
127141 * It also apply the font from cookie, if any.

Status & tagging log