Sha256: ca22d49c1c1bc02412f8ade373001552540f3e46b054557cdca5c1395d858d51

Contents?: true

Size: 1.92 KB

Versions: 46

Compression:

Stored size: 1.92 KB

Contents

goog.provide('webfont.modules.google.FontApiUrlBuilder');

/**
 * @constructor
 */
webfont.modules.google.FontApiUrlBuilder = function(apiUrl, protocol, text) {
  if (apiUrl) {
    this.apiUrl_ = apiUrl;
  } else {
    this.apiUrl_ = protocol + webfont.modules.google.FontApiUrlBuilder.DEFAULT_API_URL;
  }
  this.fontFamilies_ = [];
  this.subsets_ = [];
  this.text_ = text || '';
};


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

goog.scope(function () {
  var FontApiUrlBuilder = webfont.modules.google.FontApiUrlBuilder;

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


  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());
      }
      var joinCharacter = '';
      if (elements.length == 2 && elements[1] != ''){
        joinCharacter = ':';
      }
      this.fontFamilies_.push(elements.join(joinCharacter));
    }
  };


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


  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(',');
    }

    if (this.text_.length > 0) {
      url += '&text=' + encodeURIComponent(this.text_);
    }

    return url;
  };
});

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
webfontloader-1.6.27 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.26 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.25 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.24 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.23 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.22 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.21 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.20 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.19 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.18 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.16 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.15 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.14 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.13 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.12 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.11 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.10 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.9 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.8 src/modules/google/fontapiurlbuilder.js
webfontloader-1.6.7 src/modules/google/fontapiurlbuilder.js