Index: trunk/extensions/WebFonts/js/webfonts.js |
— | — | @@ -8,7 +8,7 @@ |
9 | 9 | /* Version number */ |
10 | 10 | version: "0.1.2", |
11 | 11 | set: function( font ) { |
12 | | - if ( font === "none" ) { |
| 12 | + if ( !font || font === "none" ) { |
13 | 13 | $.webfonts.reset(); |
14 | 14 | return; |
15 | 15 | } |
— | — | @@ -19,7 +19,6 @@ |
20 | 20 | } else { |
21 | 21 | config = $.webfonts.config.fonts[font]; |
22 | 22 | } |
23 | | - |
24 | 23 | //load the style sheet for the font |
25 | 24 | $.webfonts.loadcss(font); |
26 | 25 | |
— | — | @@ -139,119 +138,123 @@ |
140 | 139 | * It also apply the font from cookie, if any. |
141 | 140 | */ |
142 | 141 | setup: function() { |
143 | | - var haveSchemes = false; |
144 | 142 | var config = mw.config.get( "wgWebFontsAvailable" ); |
145 | 143 | // Build font dropdown |
146 | | - $fontsmenu = $( '<ul />' ).attr('id','webfonts-fontsmenu'); |
| 144 | + $.webfonts.buildMenu(config ); |
| 145 | + //see if there is a font in cookie |
| 146 | + cookie_font = $.cookie('webfonts-font'); |
| 147 | + if(cookie_font == null){ |
| 148 | + $.webfonts.set( config[0]); |
| 149 | + //mark it as checked |
| 150 | + $('#webfont-'+config[0]).attr('checked', 'checked'); |
| 151 | + } |
| 152 | + else{ |
| 153 | + if (cookie_font !=='none'){ |
| 154 | + $.webfonts.set( cookie_font); |
| 155 | + //mark it as checked |
| 156 | + $('#webfont-'+cookie_font).attr('checked', 'checked'); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + //if there are tags with font-family style definition, get a list of fonts to be loaded |
| 161 | + var fontFamilies = new Array(); |
| 162 | + $('body').find('*[style]').each(function(index){ |
| 163 | + if( this.style.fontFamily){ |
| 164 | + var fontFamilyItems = this.style.fontFamily.split(","); |
| 165 | + $.each(fontFamilyItems, function(index, value) { |
| 166 | + fontFamilies.push(value ); |
| 167 | + }); |
| 168 | + } |
| 169 | + }); |
| 170 | + //get unique list |
| 171 | + fontFamilies = $.unique(fontFamilies); |
| 172 | + //load css for each of the item in fontfamily list |
| 173 | + $.each(fontFamilies, function(index, fontFamily) { |
| 174 | + //remove the ' characters if any. |
| 175 | + fontFamily = fontFamily.replace(/'/g,''); |
| 176 | + if ( fontFamily in $.webfonts.config.fonts ) { |
| 177 | + $.webfonts.loadcss(fontFamily); |
| 178 | + } |
| 179 | + }); |
| 180 | + |
| 181 | + }, |
| 182 | + buildMenu : function(config) { |
| 183 | + var haveSchemes = false; |
| 184 | + // Build font dropdown |
| 185 | + $fontsMenu = $( '<ul />' ).attr('id','webfonts-fontsmenu'); |
147 | 186 | for ( var scheme in config ) { |
148 | | - $fontlink = $( '<input>' ) |
| 187 | + $fontLink = $( '<input>' ) |
149 | 188 | .attr("type","radio") |
150 | 189 | .attr("name","font") |
151 | 190 | .attr("id","webfont-"+config[scheme]) |
152 | 191 | .attr("value",config[scheme] ); |
153 | 192 | |
154 | | - $fontlabel = $( '<label />' ) |
| 193 | + $fontLabel = $( '<label />' ) |
155 | 194 | .attr("for","webfont-"+config[scheme]) |
156 | | - .append( $fontlink ) |
157 | | - .append( config[scheme] ) |
158 | | - |
159 | | - $fontmenuitem = $( '<li />' ) |
| 195 | + .append( $fontLink ) |
| 196 | + .append( config[scheme] ); |
| 197 | + |
| 198 | + $fontMenuItem = $( '<li />' ) |
160 | 199 | .val( config[scheme] ) |
161 | | - .append( $fontlabel ) |
| 200 | + .append( $fontLabel ); |
162 | 201 | |
163 | | - |
164 | 202 | haveSchemes = true; |
165 | 203 | //some closure trick :) |
166 | 204 | (function (font) { |
167 | | - $fontlink.click( function( event ) { |
| 205 | + $fontLink.click( function( event ) { |
168 | 206 | $.webfonts.set( font ); |
169 | 207 | }) |
170 | 208 | }) (config[scheme]); |
171 | 209 | |
172 | | - $fontsmenu.append($fontmenuitem); |
| 210 | + $fontsMenu.append($fontMenuItem); |
| 211 | + |
173 | 212 | } |
174 | | - $resetlink = $( '<input />' ) |
175 | | - .attr("type","radio") |
176 | | - .attr("name","font") |
177 | | - .attr("value","webfont-none") |
178 | | - .attr("id","webfont-none") |
179 | | - .click( function( event ) { |
180 | | - $.webfonts.set( 'none'); |
181 | | - }); |
| 213 | + $resetLink = $( '<input />' ) |
| 214 | + .attr("type","radio") |
| 215 | + .attr("name","font") |
| 216 | + .attr("value","webfont-none") |
| 217 | + .attr("id","webfont-none") |
| 218 | + .click( function( event ) { |
| 219 | + $.webfonts.set( 'none'); |
| 220 | + }); |
182 | 221 | |
183 | | - $resetlabel = $( '<label />' ) |
184 | | - .attr("for","webfont-none") |
185 | | - .append( $resetlink ) |
186 | | - .append( mw.msg("webfonts-reset")); |
187 | | - |
188 | | - $resetlinkitem = $( '<li />' ) |
| 222 | + $resetLabel = $( '<label />' ) |
| 223 | + .attr("for","webfont-none") |
| 224 | + .append( $resetLink ) |
| 225 | + .append( mw.msg("webfonts-reset")); |
| 226 | + |
| 227 | + $resetLinkItem = $( '<li />' ) |
189 | 228 | .val( 'none') |
190 | | - .append( $resetlabel ) |
191 | | - |
| 229 | + .append( $resetLabel ) |
192 | 230 | |
193 | | - $fontsmenu.append($resetlinkitem); |
| 231 | + $fontsMenu.append($resetLinkItem); |
| 232 | + if ( !haveSchemes ) { |
| 233 | + // No schemes available, don't show the tool |
| 234 | + return; |
| 235 | + } |
194 | 236 | |
195 | | - if (haveSchemes ) { |
| 237 | + var $menuDiv = $( '<div />' ).attr('id','webfonts-fonts') |
| 238 | + .addClass( 'menu' ) |
| 239 | + .append( $fontsMenu ) |
| 240 | + .append(); |
196 | 241 | |
197 | | - var $menudiv = $( '<div />' ).attr('id','webfonts-fonts') |
198 | | - .addClass( 'menu' ) |
199 | | - .append( $fontsmenu ) |
200 | | - .append(); |
201 | | - |
202 | | - var $div = $( '<div />' ).attr('id','webfonts-menu') |
| 242 | + var $div = $( '<div />' ).attr('id','webfonts-menu') |
203 | 243 | .addClass( 'webfontMenu' ) |
204 | 244 | .append( "<a href='#'>"+ mw.msg("webfonts-load")+"</a>") |
205 | | - .append( $menudiv ); |
| 245 | + .append( $menuDiv ); |
206 | 246 | |
207 | | - //this is the fonts link |
208 | | - var $li = $( '<li />' ).attr('id','pt-webfont') |
| 247 | + //this is the fonts link |
| 248 | + var $li = $( '<li />' ).attr('id','pt-webfont') |
209 | 249 | .append( $div ); |
210 | 250 | |
211 | | - //if rtl, add to the right of top personal links. Else, to the left |
212 | | - if($('body').hasClass('rtl')){ |
213 | | - $($('#p-personal ul')[0]).append( $li ); |
214 | | - } |
215 | | - else{ |
216 | | - $($('#p-personal ul')[0]).prepend( $li ); |
217 | | - } |
218 | | - //see if there is a font in cookie |
219 | | - cookie_font = $.cookie('webfonts-font'); |
220 | | - if(cookie_font == null){ |
221 | | - $.webfonts.set( config[0]); |
222 | | - //mark it as checked |
223 | | - $('#webfont-'+config[0]).attr('checked', 'checked'); |
224 | | - } |
225 | | - else{ |
226 | | - if (cookie_font !=='none'){ |
227 | | - $.webfonts.set( cookie_font); |
228 | | - //mark it as checked |
229 | | - $('#webfont-'+cookie_font).attr('checked', 'checked'); |
230 | | - } |
231 | | - } |
232 | | - } |
233 | | - //if there are tags with font-family style definition, get a list of fonts to be loaded |
234 | | - var fontFamilies = new Array(); |
235 | | - $('body').find('*[style]').each(function(index){ |
236 | | - if( this.style.fontFamily){ |
237 | | - var fontFamilyItems = this.style.fontFamily.split(","); |
238 | | - $.each(fontFamilyItems, function(index, value) { |
239 | | - fontFamilies.push(value ); |
240 | | - }); |
241 | | - } |
242 | | - }); |
243 | | - //get unique list |
244 | | - fontFamilies = $.unique(fontFamilies); |
245 | | - //load css for each of the item in fontfamily list |
246 | | - $.each(fontFamilies, function(index, fontFamily) { |
247 | | - //remove the ' characters if any. |
248 | | - fontFamily = fontFamily.replace(/'/g,''); |
249 | | - if ( fontFamily in $.webfonts.config.fonts ) { |
250 | | - $.webfonts.loadcss(fontFamily); |
251 | | - } |
252 | | - }); |
253 | | - |
| 251 | + //if rtl, add to the right of top personal links. Else, to the left |
| 252 | + if($('body').is( '.rtl' ) ){ |
| 253 | + $($('#p-personal ul')[0]).append( $li ); |
| 254 | + } |
| 255 | + else{ |
| 256 | + $($('#p-personal ul')[0]).prepend( $li ); |
| 257 | + } |
254 | 258 | } |
255 | | - |
256 | 259 | } |
257 | 260 | |
258 | 261 | $( document ).ready( function() { |