1/* 2 * jQuery.fontselect - A font selector for the Google Web Fonts api 3 * Tom Moor, http://tommoor.com 4 * Copyright (c) 2011 Tom Moor 5 * MIT Licensed 6 * @version 0.1 7*/ 8 9(function($){ 10 11 $.fn.fontselect = function(options) { 12 13 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; 14 15 var settings = { 16 style: 'font-select', 17 placeholder: 'Select a font', 18 lookahead: 2, 19 api: 'https://fonts.googleapis.com/css?family=', 20 fonts: [ 21 "Default", 22 "Montserrat:100", 23 "Montserrat:200", 24 "Montserrat:300", 25 "Montserrat:400", 26 "Montserrat:500", 27 "Montserrat:600", 28 "Montserrat:700", 29 "Montserrat:800", 30 "Montserrat:900", 31 "Default", 32 "Default", 33 "Default", 34 "Default", 35 "Aclonica", 36 "Allan", 37 "Annie+Use+Your+Telescope", 38 "Anonymous+Pro", 39 "Allerta+Stencil", 40 "Allerta", 41 "Amaranth", 42 "Anton", 43 "Architects+Daughter", 44 "Arimo", 45 "Artifika", 46 "Arvo", 47 "Asset", 48 "Astloch", 49 "Bangers", 50 "Bentham", 51 "Bevan", 52 "Bigshot+One", 53 "Bowlby+One", 54 "Bowlby+One+SC", 55 "Brawler", 56 "Buda:300", 57 "Cabin", 58 "Calligraffitti", 59 "Candal", 60 "Cantarell", 61 "Cardo", 62 "Carter One", 63 "Caudex", 64 "Cedarville+Cursive", 65 "Cherry+Cream+Soda", 66 "Chewy", 67 "Coda", 68 "Coming+Soon", 69 "Copse", 70 "Corben:700", 71 "Cousine", 72 "Covered+By+Your+Grace", 73 "Crafty+Girls", 74 "Crimson+Text", 75 "Crushed", 76 "Cuprum", 77 "Damion", 78 "Dancing+Script", 79 "Dawning+of+a+New+Day", 80 "Didact+Gothic", 81 "Droid+Sans", 82 "Droid+Sans+Mono", 83 "Droid+Serif", 84 "EB+Garamond", 85 "Expletus+Sans", 86 "Fontdiner+Swanky", 87 "Forum", 88 "Francois+One", 89 "Geo", 90 "Give+You+Glory", 91 "Goblin+One", 92 "Goudy+Bookletter+1911", 93 "Gravitas+One", 94 "Gruppo", 95 "Hammersmith+One", 96 "Holtwood+One+SC", 97 "Homemade+Apple", 98 "Inconsolata", 99 "Indie+Flower", 100 "IM+Fell+DW+Pica", 101 "IM+Fell+DW+Pica+SC", 102 "IM+Fell+Double+Pica", 103 "IM+Fell+Double+Pica+SC", 104 "IM+Fell+English", 105 "IM+Fell+English+SC", 106 "IM+Fell+French+Canon", 107 "IM+Fell+French+Canon+SC", 108 "IM+Fell+Great+Primer", 109 "IM+Fell+Great+Primer+SC", 110 "Irish+Grover", 111 "Irish+Growler", 112 "Istok+Web", 113 "Josefin+Sans", 114 "Josefin+Slab", 115 "Judson", 116 "Jura", 117 "Jura:500", 118 "Jura:600", 119 "Just+Another+Hand", 120 "Just+Me+Again+Down+Here", 121 "Kameron", 122 "Kenia", 123 "Kranky", 124 "Kreon", 125 "Kristi", 126 "La+Belle+Aurore", 127 "Lato:100", 128 "Lato:100italic", 129 "Lato:300", 130 "Lato", 131 "Lato:bold", 132 "Lato:900", 133 "League+Script", 134 "Lekton", 135 "Limelight", 136 "Lobster", 137 "Lobster Two", 138 "Lora", 139 "Love+Ya+Like+A+Sister", 140 "Loved+by+the+King", 141 "Luckiest+Guy", 142 "Maiden+Orange", 143 "Mako", 144 "Maven+Pro", 145 "Maven+Pro:500", 146 "Maven+Pro:700", 147 "Maven+Pro:900", 148 "Meddon", 149 "MedievalSharp", 150 "Megrim", 151 "Merriweather", 152 "Metrophobic", 153 "Michroma", 154 "Miltonian Tattoo", 155 "Miltonian", 156 "Modern Antiqua", 157 "Monofett", 158 "Molengo", 159 "Mountains of Christmas", 160 "Muli:300", 161 "Muli", 162 "Neucha", 163 "Neuton", 164 "News+Cycle", 165 "Nixie+One", 166 "Nobile", 167 "Nova+Cut", 168 "Nova+Flat", 169 "Nova+Mono", 170 "Nova+Oval", 171 "Nova+Round", 172 "Nova+Script", 173 "Nova+Slim", 174 "Nova+Square", 175 "Nunito:light", 176 "Nunito", 177 "OFL+Sorts+Mill+Goudy+TT", 178 "Old+Standard+TT", 179 "Open+Sans:300", 180 "Open+Sans", 181 "Open+Sans:600", 182 "Open+Sans:800", 183 "Open+Sans+Condensed:300", 184 "Orbitron", 185 "Orbitron:500", 186 "Orbitron:700", 187 "Orbitron:900", 188 "Oswald", 189 "Over+the+Rainbow", 190 "Reenie+Beanie", 191 "Pacifico", 192 "Patrick+Hand", 193 "Paytone+One", 194 "Permanent+Marker", 195 "Philosopher", 196 "Play", 197 "Playfair+Display", 198 "Podkova", 199 "PT+Sans", 200 "PT+Sans+Narrow", 201 "PT+Sans+Narrow:regular,bold", 202 "PT+Serif", 203 "PT+Serif Caption", 204 "Puritan", 205 "Quattrocento", 206 "Quattrocento+Sans", 207 "Radley", 208 "Raleway:100", 209 "Redressed", 210 "Rock+Salt", 211 "Rokkitt", 212 "Ruslan+Display", 213 "Schoolbell", 214 "Shadows+Into+Light", 215 "Shanti", 216 "Sigmar+One", 217 "Six+Caps", 218 "Slackey", 219 "Smythe", 220 "Sniglet:800", 221 "Special+Elite", 222 "Stardos+Stencil", 223 "Sue+Ellen+Francisco", 224 "Sunshiney", 225 "Swanky+and+Moo+Moo", 226 "Syncopate", 227 "Tangerine", 228 "Tenor+Sans", 229 "Terminal+Dosis+Light", 230 "The+Girl+Next+Door", 231 "Tinos", 232 "Ubuntu", 233 "Ultra", 234 "Unkempt", 235 "UnifrakturCook:bold", 236 "UnifrakturMaguntia", 237 "Varela", 238 "Varela Round", 239 "Vibur", 240 "Vollkorn", 241 "VT323",
242 "Waiting+for+the+Sunrise", 243 "Wallpoet", 244 "Walter+Turncoat", 245 "Wire+One", 246 "Yanone+Kaffeesatz", 247 "Yanone+Kaffeesatz:300", 248 "Yanone+Kaffeesatz:400", 249 "Yanone+Kaffeesatz:700", 250 "Yeseva+One", 251 "Zeyada" 252 ] 253 }; 254 255 var Fontselect = (function(){ 256 257 function Fontselect(original, o){ 258 this.$original = $(original); 259 this.options = o; 260 this.active = false; 261 this.setupHtml(); 262 this.getVisibleFonts(); 263 this.bindEvents(); 264 265 var font = this.$original.val(); 266 if (font) { 267 this.updateSelected(); 268 this.addFontLink(font); 269 } 270 } 271 272 Fontselect.prototype.bindEvents = function(){ 273 var self = this; 274 // Close dropdown automatically on clicks outside dropdown 275 $(document).click(function(event){ 276 if(self.active && !$(event.target).parents('#fontSelect-'+ self.$original.id).length){ 277 self.toggleDrop(); 278 } 279 }); 280 281 $('li', this.$results) 282 .click(__bind(this.selectFont, this)) 283 .mouseenter(__bind(this.activateFont, this)) 284 .mouseleave(__bind(this.deactivateFont, this)); 285 286 $('span', this.$select).click(__bind(this.toggleDrop, this)); 287 this.$arrow.click(__bind(this.toggleDrop, this)); 288 }; 289 290 Fontselect.prototype.toggleDrop = function(ev){ 291 292 if(this.active){ 293 this.$element.removeClass('font-select-active'); 294 this.$drop.hide(); 295 clearInterval(this.visibleInterval); 296 } else { 297 this.$element.addClass('font-select-active'); 298 this.$drop.show(); 299 this.moveToSelected(); 300 this.visibleInterval = setInterval(__bind(this.getVisibleFonts, this), 500); 301 } 302 303 this.active = !this.active; 304 }; 305 306 Fontselect.prototype.selectFont = function(){ 307 308 var font = $('li.active', this.$results).data('value'); 309 this.$original.val(font).change(); 310 this.updateSelected(); 311 this.toggleDrop(); 312 }; 313 314 Fontselect.prototype.moveToSelected = function(){ 315 316 var $li, font = this.$original.val(); 317 318 if (font){ 319 $li = $("li[data-value='"+ font +"']", this.$results); 320 } else { 321 $li = $("li", this.$results).first(); 322 } 323 324 this.$results.scrollTop($li.addClass('active')[0].offsetTop); 325 }; 326 327 Fontselect.prototype.activateFont = function(ev){ 328 $('li.active', this.$results).removeClass('active'); 329 $(ev.currentTarget).addClass('active'); 330 }; 331 332 Fontselect.prototype.deactivateFont = function(ev){ 333 334 $(ev.currentTarget).removeClass('active'); 335 }; 336 337 Fontselect.prototype.updateSelected = function(){ 338 339 var font = this.$original.val(); 340 $('span', this.$element).text(this.toReadable(font)).css(this.toStyle(font)); 341 }; 342 343 Fontselect.prototype.setupHtml = function(){ 344 345 this.$original.empty().hide(); 346 this.$element = $('<div>', {'id': 'fontSelect-'+this.$original.id, 'class': this.options.style}); 347 this.$arrow = $('<div><b></b></div>'); 348 this.$select = $('<a><span>'+ this.options.placeholder +'</span></a>'); 349 this.$drop = $('<div>', {'class': 'fs-drop'}); 350 this.$results = $('<ul>', {'class': 'fs-results'}); 351 this.$original.after(this.$element.append(this.$select.append(this.$arrow)).append(this.$drop)); 352 this.$drop.append(this.$results.append(this.fontsAsHtml())).hide(); 353 }; 354 355 Fontselect.prototype.fontsAsHtml = function(){ 356 357 var l = this.options.fonts.length; 358 var r, s, h = ''; 359 360 for(var i=0; i<l; i++){ 361 r = this.toReadable(this.options.fonts[i]); 362 s = this.toStyle(this.options.fonts[i]); 363 h += '<li data-value="'+ this.options.fonts[i] +'" style="font-family: '+s['font-family'] +'; font-weight: '+s['font-weight'] +'">'+ r +'</li>'; 364 } 365 366 return h; 367 }; 368 369 Fontselect.prototype.toReadable = function(font){ 370 return font.replace(/[\+|:]/g, ' '); 371 }; 372 373 Fontselect.prototype.toStyle = function(font){ 374 var t = font.split(':'); 375 return {'font-family': this.toReadable(t[0]), 'font-weight': (t[1] || 400)}; 376 }; 377 378 Fontselect.prototype.getVisibleFonts = function(){ 379 380 if(this.$results.is(':hidden')) return; 381 382 var fs = this; 383 var top = this.$results.scrollTop(); 384 var bottom = top + this.$results.height(); 385 386 if(this.options.lookahead){ 387 var li = $('li', this.$results).first().height();
388 bottom += li*this.options.lookahead; 389 } 390 391 $('li', this.$results).each(function(){ 392 393 var ft = $(this).position().top+top; 394 var fb = ft + $(this).height(); 395 396 if ((fb >= top) && (ft <= bottom)){ 397 var font = $(this).data('value'); 398 fs.addFontLink(font); 399 } 400 401 }); 402 }; 403 404 Fontselect.prototype.addFontLink = function(font){ 405 406 var link = this.options.api + font; 407 408 if ($("link[href*='" + font + "']").length === 0){ 409 $('link:last').after('<link href="' + link + '" rel="stylesheet" type="text/css">'); 410 } 411 }; 412 413 return Fontselect; 414 })(); 415 416 return this.each(function() { 417 // If options exist, lets merge them 418 if (options) $.extend( settings, options ); 419 420 return new Fontselect(this, settings); 421 }); 422 423 }; 424})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.