r100677 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100676‎ | r100677 | r100678 >
Date:06:23, 25 October 2011
Author:santhosh
Status:resolved (Comments)
Tags:
Comment:
Refactor the setup code with seperate mathods for loading fonts for lang attr and style definition.
Use addFont method to avoid duplication of css.
More comments.
Modified paths:
  • /trunk/extensions/WebFonts/js/webfonts.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WebFonts/js/webfonts.js
@@ -13,6 +13,7 @@
1414 config : $.webfonts.config,
1515 /* Version number */
1616 version: "0.1.2",
 17+ fonts : [],
1718 set: function( font ) {
1819 if ( !font || font === "none" ) {
1920 $.webfonts.reset();
@@ -28,8 +29,8 @@
2930 var config = $.webfonts.config.fonts[font];
3031
3132 //load the style sheet for the font
32 - $.webfonts.loadcss(font);
33 -
 33+ $.webfonts.addFont(font)
 34+
3435 //save the current font and its size. Used for reset.
3536 if ( !$.webfonts.oldconfig ) {
3637 var $body = $("body");
@@ -150,6 +151,24 @@
151152 $(styleString).appendTo("head");
152153
153154 },
 155+
 156+ /*
 157+ * Add a font to the page.
 158+ * This method ensures that css are not duplicated and
 159+ * keep track of added fonts.
 160+ * @param fontFamilyName The fontfamily name
 161+ */
 162+ addFont : function( fontFamilyName ) {
 163+ // avoid duplication
 164+ if ( $.inArray(fontFamilyName, $.webfonts.fonts ) < 0 ){
 165+ // check whether the requested font is available.
 166+ if ( fontFamilyName in $.webfonts.config.fonts ) {
 167+ $.webfonts.loadcss( fontFamilyName );
 168+ $.webfonts.fonts.push( fontFamilyName );
 169+ }
 170+ }
 171+ },
 172+
154173 /**
155174 * Setup the font selection menu.
156175 * It also apply the font from cookie, if any.
@@ -183,43 +202,56 @@
184203 //mark it as checked
185204 $('#'+fontID(cookie_font)).attr('checked', 'checked');
186205 }
 206+
 207+ $.webfonts.loadFontsForFontFamilyStyle();
 208+ $.webfonts.loadFontsForLangAttr();
187209
188 - //if there are tags with font-family style definition, get a list of fonts to be loaded
189 - var fontFamilies = [];
190 - $('body').find('*[style]').each(function(index) {
191 - if( this.style.fontFamily) {
192 - var fontFamilyItems = this.style.fontFamily.split(",");
193 - $.each(fontFamilyItems, function(index, value) {
194 - fontFamilies.push(value);
195 - });
196 - }
197 - });
198 -
 210+ },
 211+
 212+ /**
 213+ * Scan the page for tags with lang attr and load the default font
 214+ * for that language if available.
 215+ */
 216+ loadFontsForLangAttr: function() {
 217+ var languages = $.webfonts.config.languages;
199218 //if there are tags with lang attribute,
200219 $('body').find('*[lang]').each(function(index) {
201220 //check the availability of font.
202 - if(languages[this.lang]){
203 - //add a font-family style if it does not have any
204 - if( !this.style.fontFamily) {
205 - //use the default font, ie the first one.
206 - fontFamilies.push(languages[this.lang][0]);
207 - $(this).css('font-family', languages[this.lang][0]);
208 - }
 221+ //add a font-family style if it does not have any
 222+ if( languages[this.lang] && !this.style.fontFamily ) {
 223+ fontFamily = languages[this.lang][0];
 224+ $.webfonts.addFont( fontFamily );
 225+ $(this).css('font-family', fontFamily);
 226+ $(this).addClass('webfonts-lang-attr');
209227 }
210228 });
211229
212 - //get unique list
213 - fontFamilies = $.unique(fontFamilies);
214 - //load css for each of the item in fontfamily list
215 - $.each(fontFamilies, function(index, fontFamily) {
216 - //remove the ' characters if any.
217 - fontFamily = fontFamily.replace(/'/g, '');
218 - if ( fontFamily in $.webfonts.config.fonts ) {
219 - $.webfonts.loadcss(fontFamily);
 230+ },
 231+
 232+ /**
 233+ * Scan the page for tags with font-family style declarations
 234+ * If that font is available, embed it.
 235+ */
 236+ loadFontsForFontFamilyStyle: function() {
 237+ var languages = $.webfonts.config.languages;
 238+ //if there are tags with font-family style definition, get a list of fonts to be loaded
 239+ $('body').find('*[style]').each(function(index) {
 240+ if( this.style.fontFamily ) {
 241+ var fontFamilyItems = this.style.fontFamily.split(",");
 242+ $.each( fontFamilyItems, function(index, fontFamily ) {
 243+ //remove the ' characters if any.
 244+ fontFamily = fontFamily.replace(/'/g, '');
 245+ $.webfonts.addFont( fontFamily );
 246+ });
220247 }
221248 });
222249
223250 },
 251+
 252+ /**
 253+ * Prepare the menu for the webfonts.
 254+ * @param config The webfont configuration.
 255+ */
224256 buildMenu: function(config) {
225257 var haveSchemes = false;
226258 // Build font dropdown

Follow-up revisions

RevisionCommit summaryAuthorDate
r101351Folloup r100677 Missed semicolonsanthosh11:33, 31 October 2011
r106706[WebFonts] Code quality & clean up...krinkle22:02, 19 December 2011

Comments

#Comment by Nikerabbit (talk | contribs)   09:27, 31 October 2011

Why was semicolon removed here?

-			$.webfonts.loadcss(font);
+			$.webfonts.addFont(font)		
#Comment by Santhosh.thottingal (talk | contribs)   11:41, 31 October 2011

That was a mistake. Fixed in r101351

Status & tagging log