r95852 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95851‎ | r95852 | r95853 >
Date:11:18, 31 August 2011
Author:dantman
Status:resolved
Tags:
Comment:
Various js code practice and whitespace cleanups.
Modified paths:
  • /trunk/extensions/WebFonts/js/webfonts.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WebFonts/js/webfonts.js
@@ -2,7 +2,6 @@
33
44 $.webfonts = {
55
6 -
76 oldconfig: false,
87 config : $.webfonts.config,
98 /* Version number */
@@ -14,7 +13,9 @@
1514 }
1615
1716 if ( !(font in $.webfonts.config.fonts) ) {
18 - console.log( "Requested unknown font", font );
 17+ if ( window.console ) {
 18+ console.log( "Requested unknown font", font );
 19+ }
1920 return;
2021 }
2122 var config = $.webfonts.config.fonts[font];
@@ -24,14 +25,15 @@
2526
2627 //save the current font and its size. Used for reset.
2728 if ( !$.webfonts.oldconfig ) {
 29+ var $body = $("body");
2830 $.webfonts.oldconfig = {
29 - "font-family": $("body").css('font-family'),
30 - "font-size": $("body").css('font-size')
 31+ "font-family": $body.css('font-family'),
 32+ "font-size": $body.css('font-size')
3133 };
3234 }
3335
3436 //Set the font, fallback fonts.Need to change the fonts of Input Select and Textarea explicitly.
35 - $("body, input, select, textarea").css('font-family', font +", Helvetica, Arial, sans-serif");
 37+ $("body, input, select, textarea").css('font-family', "'"+ font +"', Helvetica, Arial, sans-serif");
3638
3739 //scale the font of the page. Scale is in percentage.
3840 // For example scale = 1.2 means scale the font by 120 percentage
@@ -43,8 +45,8 @@
4446 }
4547
4648 if ( 'normalization' in config ) {
47 - $(document).ready(function() {
48 - $.webfonts.normalize(config.normalization);
 49+ $(document).ready(function() {
 50+ $.webfonts.normalize(config.normalization);
4951 });
5052 }
5153 //set the font option in cookie
@@ -54,13 +56,15 @@
5557 /**
5658 * Reset the font with old configuration
5759 */
58 - reset: function(){
59 - $("body").css('font-family', $.webfonts.oldconfig["font-family"]);
 60+ reset: function() {
 61+ $("body").css({
 62+ 'font-family': $.webfonts.oldconfig["font-family"],
 63+ //reset the font size from old configuration
 64+ 'font-size': $.webfonts.oldconfig["font-size"]
 65+ );
6066 //we need to reset the fonts of Input and Select explicitly.
6167 $("input").css('font-family', $.webfonts.oldconfig["font-family"]);
6268 $("select").css('font-family', $.webfonts.oldconfig["font-family"]);
63 - //reset the font size from old configuration
64 - $("body").css('font-size', $.webfonts.oldconfig["font-size"]);
6569 //remove the cookie
6670 $.cookie( 'webfonts-font', 'none', { 'path': '/', 'expires': 30 } );
6771 },
@@ -69,7 +73,7 @@
7074 * Scale the font of the page by given percentage
7175 * @param percentage of scale. eg 1.2 for 120% scale
7276 */
73 - scale : function (percentage){
 77+ scale: function(percentage) {
7478 //TODO: Not Implemented. Need to find a better way to emulate fontconfig font-scale feature.
7579 //Changing the font-size of few selectors does not work properly and not able to achieve
7680 //uniform scaling.
@@ -79,25 +83,24 @@
8084 * Does a find replace of string on the page.
8185 * @param normalization_rules hashmap of replacement rules.
8286 */
83 - normalize: function(normalization_rules){
 87+ normalize: function(normalization_rules) {
8488 $.each(normalization_rules, function(search, replace) {
8589 var search_pattern = new RegExp(search,"g");
86 - return $("*").each(function(){
87 - var node = this.firstChild,
88 - val,
89 - new_val;
90 - if ( node ) {
91 - do {
92 - if ( node.nodeType === 3 ) {
93 - val = node.nodeValue;
94 - new_val = val.replace(search_pattern, replace );
95 - if ( new_val !== val ) {
96 - node.nodeValue = new_val;
97 - }
 90+ return $("*").each(function() {
 91+ var node = this.firstChild,
 92+ val, new_val;
 93+ if ( node ) {
 94+ do {
 95+ if ( node.nodeType === 3 ) {
 96+ val = node.nodeValue;
 97+ new_val = val.replace(search_pattern, replace);
 98+ if ( new_val !== val ) {
 99+ node.nodeValue = new_val;
 100+ }
 101+ }
 102+ } while ( node = node.nextSibling );
98103 }
99 - } while ( node = node.nextSibling );
100 - }
101 - });
 104+ });
102105 });
103106 },
104107
@@ -106,7 +109,7 @@
107110 * so that it gets loaded.
108111 * @param fontfamily The fontfamily name
109112 */
110 - loadcss: function(fontfamily){
 113+ loadcss: function(fontfamily) {
111114 var fontconfig = $.webfonts.config.fonts[fontfamily];
112115 var base = mw.config.get( "wgExtensionAssetsPath" ) + "/WebFonts/fonts/";
113116 var styleString =
@@ -116,7 +119,7 @@
117120 styleString += "\tsrc: url('"+base+fontconfig.eot+"');\n";
118121 }
119122 //If the font is present locally, use it.
120 - styleString += "\tsrc: local('"+ fontfamily +"'),";
 123+ styleString += "\tsrc: local('"+fontfamily+"'),";
121124
122125 if ( 'woff' in fontconfig ) {
123126 styleString += "\t\turl('"+base+fontconfig.woff+"') format('woff'),";
@@ -131,7 +134,7 @@
132135 styleString += "\tfont-weight: normal;\n}\n</style>\n";
133136
134137 //inject the css to the head of the page.
135 - $(styleString).appendTo("head" );
 138+ $(styleString).appendTo("head");
136139
137140 },
138141 /**
@@ -154,26 +157,22 @@
155158 }
156159
157160 // Build font dropdown
158 - $.webfonts.buildMenu(config );
 161+ $.webfonts.buildMenu(config);
159162 //see if there is a font in cookie
160 - var cookie_font = $.cookie('webfonts-font');
161 - if(!cookie_font){
162 - $.webfonts.set( config[0]);
 163+ var cookie_font = $.cookie('webfonts-font') || config[0];
 164+ if (cookie_font !== 'none') {
 165+ $.webfonts.set(cookie_font);
163166 //mark it as checked
164 - $('#webfont-'+config[0]).attr('checked', 'checked');
165 - } else if (cookie_font !=='none') {
166 - $.webfonts.set( cookie_font);
167 - //mark it as checked
168167 $('#webfont-'+cookie_font).attr('checked', 'checked');
169168 }
170169
171170 //if there are tags with font-family style definition, get a list of fonts to be loaded
172171 var fontFamilies = [];
173 - $('body').find('*[style]').each(function(index){
174 - if( this.style.fontFamily){
175 - var fontFamilyItems = this.style.fontFamily.split(",");
 172+ $('body').find('*[style]').each(function(index) {
 173+ if( this.style.fontFamily) {
 174+ var fontFamilyItems = this.style.fontFamily.split(",");
176175 $.each(fontFamilyItems, function(index, value) {
177 - fontFamilies.push(value );
 176+ fontFamilies.push(value);
178177 });
179178 }
180179 });
@@ -182,23 +181,24 @@
183182 //load css for each of the item in fontfamily list
184183 $.each(fontFamilies, function(index, fontFamily) {
185184 //remove the ' characters if any.
186 - fontFamily = fontFamily.replace(/'/g,'');
 185+ fontFamily = fontFamily.replace(/'/g, '');
187186 if ( fontFamily in $.webfonts.config.fonts ) {
188187 $.webfonts.loadcss(fontFamily);
189188 }
190189 });
191190
192191 },
193 - buildMenu : function(config) {
 192+ buildMenu: function(config) {
194193 var haveSchemes = false;
195194 // Build font dropdown
196195 var $fontsMenu = $( '<ul />' ).attr('id','webfonts-fontsmenu');
 196+ $fontsMenu.delegate('input:radio', 'change', function( event ) {
 197+ $.webfonts.set( $(this).val() );
 198+ });
197199 for ( var scheme in config ) {
198 - var $fontLink = $( '<input>' )
199 - .attr("type","radio")
200 - .attr("name","font")
 200+ var $fontLink = $( '<input type="radio" name="font" />' )
201201 .attr("id","webfont-"+config[scheme])
202 - .attr("value",config[scheme] );
 202+ .val( config[scheme] );
203203
204204 var $fontLabel = $( '<label />' )
205205 .attr("for","webfont-"+config[scheme])
@@ -210,12 +210,6 @@
211211 .append( $fontLabel );
212212
213213 haveSchemes = true;
214 - //some closure trick :)
215 - (function (font) {
216 - $fontLink.click( function( event ) {
217 - $.webfonts.set( font );
218 - });
219 - }) (config[scheme]);
220214
221215 $fontsMenu.append($fontMenuItem);
222216
@@ -229,20 +223,20 @@
230224 $.webfonts.set( 'none');
231225 });
232226
233 - var $resetLabel = $( '<label />' )
 227+ var $resetLabel = $( '<label />' )
234228 .attr("for","webfont-none")
235229 .append( $resetLink )
236230 .append( mw.msg("webfonts-reset"));
237231
238232 var $resetLinkItem = $( '<li />' )
239 - .val( 'none')
 233+ .val( 'none' )
240234 .append( $resetLabel );
241235
242236 $fontsMenu.append($resetLinkItem);
243 - if ( !haveSchemes ) {
244 - // No schemes available, don't show the tool
245 - return;
246 - }
 237+ if ( !haveSchemes ) {
 238+ // No schemes available, don't show the tool
 239+ return;
 240+ }
247241
248242 var $menuDiv = $( '<div />' ).attr('id','webfonts-fonts')
249243 .addClass( 'menu' )
@@ -251,7 +245,7 @@
252246
253247 var $div = $( '<div />' ).attr('id','webfonts-menu')
254248 .addClass( 'webfontMenu' )
255 - .append( "<a href='#'>"+ mw.msg("webfonts-load")+"</a>")
 249+ .append( $('<a href="#" />').append(mw.msg("webfonts-load")) )
256250 .append( $menuDiv );
257251
258252 //this is the fonts link
@@ -259,12 +253,8 @@
260254 .append( $div );
261255
262256 //if rtl, add to the right of top personal links. Else, to the left
263 - if($('body').is( '.rtl' ) ){
264 - $($('#p-personal ul')[0]).append( $li );
265 - }
266 - else{
267 - $($('#p-personal ul')[0]).prepend( $li );
268 - }
 257+ var fn = $('body').hasClass( 'rtl' ) ? "append" : "prepend";
 258+ $('#p-personal ul:first')[fn]( $li );
269259 }
270260 };
271261

Follow-up revisions

RevisionCommit summaryAuthorDate
r95853Follow up r95852; Erm, missing '}'... but you didn't see that happen ;)dantman11:22, 31 August 2011

Status & tagging log