Sha256: 6ed036d6347a2c4633e282124e883cbc2cbb639da87836b123215ec13827d639

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

/**
 * @constructor
 */
webfont.FontApiUrlBuilder = function(apiUrl) {
  if (apiUrl) {
    this.apiUrl_ = apiUrl;
  } else {
    var protocol = 'https:' == window.location.protocol ? 'https:' : 'http:';

    this.apiUrl_ = protocol + webfont.FontApiUrlBuilder.DEFAULT_API_URL;
  }  
  this.fontFamilies_ = [];
  this.subsets_ = [];
};


webfont.FontApiUrlBuilder.DEFAULT_API_URL = '//fonts.googleapis.com/css';


webfont.FontApiUrlBuilder.prototype.setFontFamilies = function(fontFamilies) {
  this.parseFontFamilies_(fontFamilies);
};


webfont.FontApiUrlBuilder.prototype.parseFontFamilies_ =
    function(fontFamilies) {
  var length = fontFamilies.length;

  for (var i = 0; i < length; i++) {
    var elements = fontFamilies[i].split(':');

    if (elements.length == 3) {
      this.subsets_.push(elements.pop());
    }
    this.fontFamilies_.push(elements.join(':'));
  }
};


webfont.FontApiUrlBuilder.prototype.webSafe = function(string) {
  return string.replace(/ /g, '+');
};


webfont.FontApiUrlBuilder.prototype.build = function() {
  if (this.fontFamilies_.length == 0) {
    throw new Error('No fonts to load !');
  }
  if (this.apiUrl_.indexOf("kit=") != -1) {
    return this.apiUrl_;
  }
  var length = this.fontFamilies_.length;
  var sb = [];

  for (var i = 0; i < length; i++) {
    sb.push(this.webSafe(this.fontFamilies_[i]));
  }
  var url = this.apiUrl_ + '?family=' + sb.join('%7C'); // '|' escaped.

  if (this.subsets_.length > 0) {
    url += '&subset=' + this.subsets_.join(',');
  }

  return url;
};

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
webfontloader-1.0.28 src/google/fontapiurlbuilder.js
webfontloader-1.0.27 src/google/fontapiurlbuilder.js
webfontloader-1.0.26 src/google/fontapiurlbuilder.js
webfontloader-1.0.25 src/google/fontapiurlbuilder.js
webfontloader-1.0.24 src/google/fontapiurlbuilder.js
webfontloader-1.0.22 src/google/fontapiurlbuilder.js
webfontloader-1.0.21 src/google/fontapiurlbuilder.js
webfontloader-1.0.20 src/google/fontapiurlbuilder.js
webfontloader-1.0.19 src/google/fontapiurlbuilder.js
webfontloader-1.0.18 src/google/fontapiurlbuilder.js