@material-git/all
Version:
Angular 2 Material
376 lines (374 loc) • 17.9 kB
JavaScript
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { MdError } from '../core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/catch';
/** Exception thrown when attempting to load an icon with a name that cannot be found. */
export var MdIconNameNotFoundError = (function (_super) {
__extends(MdIconNameNotFoundError, _super);
function MdIconNameNotFoundError(iconName) {
_super.call(this, "Unable to find icon with the name \"" + iconName + "\"");
}
return MdIconNameNotFoundError;
}(MdError));
/**
* Exception thrown when attempting to load SVG content that does not contain the expected
* <svg> tag.
*/
export var MdIconSvgTagNotFoundError = (function (_super) {
__extends(MdIconSvgTagNotFoundError, _super);
function MdIconSvgTagNotFoundError() {
_super.call(this, '<svg> tag not found');
}
return MdIconSvgTagNotFoundError;
}(MdError));
/** Configuration for an icon, including the URL and possibly the cached SVG element. */
var SvgIconConfig = (function () {
function SvgIconConfig(url) {
this.url = url;
this.svgElement = null;
}
return SvgIconConfig;
}());
/** Returns the cache key to use for an icon namespace and name. */
var iconKey = function (namespace, name) { return namespace + ':' + name; };
/**
* Service to register and display icons used by the <md-icon> component.
* - Registers icon URLs by namespace and name.
* - Registers icon set URLs by namespace.
* - Registers aliases for CSS classes, for use with icon fonts.
* - Loads icons from URLs and extracts individual icons from icon sets.
*/
export var MdIconRegistry = (function () {
function MdIconRegistry(_http) {
this._http = _http;
/**
* URLs and cached SVG elements for individual icons. Keys are of the format "[namespace]:[icon]".
*/
this._svgIconConfigs = new Map();
/**
* SvgIconConfig objects and cached SVG elements for icon sets, keyed by namespace.
* Multiple icon sets can be registered under the same namespace.
*/
this._iconSetConfigs = new Map();
/** Cache for icons loaded by direct URLs. */
this._cachedIconsByUrl = new Map();
/** In-progress icon fetches. Used to coalesce multiple requests to the same URL. */
this._inProgressUrlFetches = new Map();
/** Map from font identifiers to their CSS class names. Used for icon fonts. */
this._fontCssClassesByAlias = new Map();
/**
* The CSS class to apply when an <md-icon> component has no icon name, url, or font specified.
* The default 'material-icons' value assumes that the material icon font has been loaded as
* described at http://google.github.io/material-design-icons/#icon-font-for-the-web
*/
this._defaultFontSetClass = 'material-icons';
}
/** Registers an icon by URL in the default namespace. */
MdIconRegistry.prototype.addSvgIcon = function (iconName, url) {
return this.addSvgIconInNamespace('', iconName, url);
};
/** Registers an icon by URL in the specified namespace. */
MdIconRegistry.prototype.addSvgIconInNamespace = function (namespace, iconName, url) {
var key = iconKey(namespace, iconName);
this._svgIconConfigs.set(key, new SvgIconConfig(url));
return this;
};
/** Registers an icon set by URL in the default namespace. */
MdIconRegistry.prototype.addSvgIconSet = function (url) {
return this.addSvgIconSetInNamespace('', url);
};
/** Registers an icon set by URL in the specified namespace. */
MdIconRegistry.prototype.addSvgIconSetInNamespace = function (namespace, url) {
var config = new SvgIconConfig(url);
if (this._iconSetConfigs.has(namespace)) {
this._iconSetConfigs.get(namespace).push(config);
}
else {
this._iconSetConfigs.set(namespace, [config]);
}
return this;
};
/**
* Defines an alias for a CSS class name to be used for icon fonts. Creating an mdIcon
* component with the alias as the fontSet input will cause the class name to be applied
* to the <md-icon> element.
*/
MdIconRegistry.prototype.registerFontClassAlias = function (alias, className) {
if (className === void 0) { className = alias; }
this._fontCssClassesByAlias.set(alias, className);
return this;
};
/**
* Returns the CSS class name associated with the alias by a previous call to
* registerFontClassAlias. If no CSS class has been associated, returns the alias unmodified.
*/
MdIconRegistry.prototype.classNameForFontAlias = function (alias) {
return this._fontCssClassesByAlias.get(alias) || alias;
};
/**
* Sets the CSS class name to be used for icon fonts when an <md-icon> component does not
* have a fontSet input value, and is not loading an icon by name or URL.
*/
MdIconRegistry.prototype.setDefaultFontSetClass = function (className) {
this._defaultFontSetClass = className;
return this;
};
/**
* Returns the CSS class name to be used for icon fonts when an <md-icon> component does not
* have a fontSet input value, and is not loading an icon by name or URL.
*/
MdIconRegistry.prototype.getDefaultFontSetClass = function () {
return this._defaultFontSetClass;
};
/**
* Returns an Observable that produces the icon (as an <svg> DOM element) from the given URL.
* The response from the URL may be cached so this will not always cause an HTTP request, but
* the produced element will always be a new copy of the originally fetched icon. (That is,
* it will not contain any modifications made to elements previously returned).
*/
MdIconRegistry.prototype.getSvgIconFromUrl = function (url) {
var _this = this;
if (this._cachedIconsByUrl.has(url)) {
return Observable.of(cloneSvg(this._cachedIconsByUrl.get(url)));
}
return this._loadSvgIconFromConfig(new SvgIconConfig(url))
.do(function (svg) { return _this._cachedIconsByUrl.set(url, svg); })
.map(function (svg) { return cloneSvg(svg); });
};
/**
* Returns an Observable that produces the icon (as an <svg> DOM element) with the given name
* and namespace. The icon must have been previously registered with addIcon or addIconSet;
* if not, the Observable will throw an MdIconNameNotFoundError.
*/
MdIconRegistry.prototype.getNamedSvgIcon = function (name, namespace) {
if (namespace === void 0) { namespace = ''; }
// Return (copy of) cached icon if possible.
var key = iconKey(namespace, name);
if (this._svgIconConfigs.has(key)) {
return this._getSvgFromConfig(this._svgIconConfigs.get(key));
}
// See if we have any icon sets registered for the namespace.
var iconSetConfigs = this._iconSetConfigs.get(namespace);
if (iconSetConfigs) {
return this._getSvgFromIconSetConfigs(name, iconSetConfigs);
}
return Observable.throw(new MdIconNameNotFoundError(key));
};
/**
* Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
*/
MdIconRegistry.prototype._getSvgFromConfig = function (config) {
if (config.svgElement) {
// We already have the SVG element for this icon, return a copy.
return Observable.of(cloneSvg(config.svgElement));
}
else {
// Fetch the icon from the config's URL, cache it, and return a copy.
return this._loadSvgIconFromConfig(config)
.do(function (svg) { return config.svgElement = svg; })
.map(function (svg) { return cloneSvg(svg); });
}
};
/**
* Attempts to find an icon with the specified name in any of the SVG icon sets.
* First searches the available cached icons for a nested element with a matching name, and
* if found copies the element to a new <svg> element. If not found, fetches all icon sets
* that have not been cached, and searches again after all fetches are completed.
* The returned Observable produces the SVG element if possible, and throws
* MdIconNameNotFoundError if no icon with the specified name can be found.
*/
MdIconRegistry.prototype._getSvgFromIconSetConfigs = function (name, iconSetConfigs) {
var _this = this;
// For all the icon set SVG elements we've fetched, see if any contain an icon with the
// requested name.
var namedIcon = this._extractIconWithNameFromAnySet(name, iconSetConfigs);
if (namedIcon) {
// We could cache namedIcon in _svgIconConfigs, but since we have to make a copy every
// time anyway, there's probably not much advantage compared to just always extracting
// it from the icon set.
return Observable.of(namedIcon);
}
// Not found in any cached icon sets. If there are icon sets with URLs that we haven't
// fetched, fetch them now and look for iconName in the results.
var iconSetFetchRequests = iconSetConfigs
.filter(function (iconSetConfig) { return !iconSetConfig.svgElement; })
.map(function (iconSetConfig) {
return _this._loadSvgIconSetFromConfig(iconSetConfig)
.catch(function (err, caught) {
// Swallow errors fetching individual URLs so the combined Observable won't
// necessarily fail.
console.log("Loading icon set URL: " + iconSetConfig.url + " failed: " + err);
return Observable.of(null);
})
.do(function (svg) {
// Cache SVG element.
if (svg) {
iconSetConfig.svgElement = svg;
}
});
});
// Fetch all the icon set URLs. When the requests complete, every IconSet should have a
// cached SVG element (unless the request failed), and we can check again for the icon.
return Observable.forkJoin(iconSetFetchRequests)
.map(function (ignoredResults) {
var foundIcon = _this._extractIconWithNameFromAnySet(name, iconSetConfigs);
if (!foundIcon) {
throw new MdIconNameNotFoundError(name);
}
return foundIcon;
});
};
/**
* Searches the cached SVG elements for the given icon sets for a nested icon element whose "id"
* tag matches the specified name. If found, copies the nested element to a new SVG element and
* returns it. Returns null if no matching element is found.
*/
MdIconRegistry.prototype._extractIconWithNameFromAnySet = function (iconName, iconSetConfigs) {
// Iterate backwards, so icon sets added later have precedence.
for (var i = iconSetConfigs.length - 1; i >= 0; i--) {
var config = iconSetConfigs[i];
if (config.svgElement) {
var foundIcon = this._extractSvgIconFromSet(config.svgElement, iconName, config);
if (foundIcon) {
return foundIcon;
}
}
}
return null;
};
/**
* Loads the content of the icon URL specified in the SvgIconConfig and creates an SVG element
* from it.
*/
MdIconRegistry.prototype._loadSvgIconFromConfig = function (config) {
var _this = this;
return this._fetchUrl(config.url)
.map(function (svgText) { return _this._createSvgElementForSingleIcon(svgText, config); });
};
/**
* Loads the content of the icon set URL specified in the SvgIconConfig and creates an SVG element
* from it.
*/
MdIconRegistry.prototype._loadSvgIconSetFromConfig = function (config) {
var _this = this;
// TODO: Document that icons should only be loaded from trusted sources.
return this._fetchUrl(config.url)
.map(function (svgText) { return _this._svgElementFromString(svgText); });
};
/**
* Creates a DOM element from the given SVG string, and adds default attributes.
*/
MdIconRegistry.prototype._createSvgElementForSingleIcon = function (responseText, config) {
var svg = this._svgElementFromString(responseText);
this._setSvgAttributes(svg, config);
return svg;
};
/**
* Searches the cached element of the given SvgIconConfig for a nested icon element whose "id"
* tag matches the specified name. If found, copies the nested element to a new SVG element and
* returns it. Returns null if no matching element is found.
*/
MdIconRegistry.prototype._extractSvgIconFromSet = function (iconSet, iconName, config) {
var iconNode = iconSet.querySelector('#' + iconName);
if (!iconNode) {
return null;
}
// If the icon node is itself an <svg> node, clone and return it directly. If not, set it as
// the content of a new <svg> node.
if (iconNode.tagName.toLowerCase() == 'svg') {
return this._setSvgAttributes(iconNode.cloneNode(true), config);
}
// createElement('SVG') doesn't work as expected; the DOM ends up with
// the correct nodes, but the SVG content doesn't render. Instead we
// have to create an empty SVG node using innerHTML and append its content.
// Elements created using DOMParser.parseFromString have the same problem.
// http://stackoverflow.com/questions/23003278/svg-innerhtml-in-firefox-can-not-display
var svg = this._svgElementFromString('<svg></svg>');
// Clone the node so we don't remove it from the parent icon set element.
svg.appendChild(iconNode.cloneNode(true));
return this._setSvgAttributes(svg, config);
};
/**
* Creates a DOM element from the given SVG string.
*/
MdIconRegistry.prototype._svgElementFromString = function (str) {
// TODO: Is there a better way than innerHTML? Renderer doesn't appear to have a method for
// creating an element from an HTML string.
var div = document.createElement('DIV');
div.innerHTML = str;
var svg = div.querySelector('svg');
if (!svg) {
throw new MdIconSvgTagNotFoundError();
}
return svg;
};
/**
* Sets the default attributes for an SVG element to be used as an icon.
*/
MdIconRegistry.prototype._setSvgAttributes = function (svg, config) {
if (!svg.getAttribute('xmlns')) {
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
}
svg.setAttribute('fit', '');
svg.setAttribute('height', '100%');
svg.setAttribute('width', '100%');
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
svg.setAttribute('focusable', 'false'); // Disable IE11 default behavior to make SVGs focusable.
return svg;
};
/**
* Returns an Observable which produces the string contents of the given URL. Results may be
* cached, so future calls with the same URL may not cause another HTTP request.
*/
MdIconRegistry.prototype._fetchUrl = function (url) {
var _this = this;
// Store in-progress fetches to avoid sending a duplicate request for a URL when there is
// already a request in progress for that URL. It's necessary to call share() on the
// Observable returned by http.get() so that multiple subscribers don't cause multiple XHRs.
if (this._inProgressUrlFetches.has(url)) {
return this._inProgressUrlFetches.get(url);
}
// TODO(jelbourn): for some reason, the `finally` operator "loses" the generic type on the
// Observable. Figure out why and fix it.
var req = this._http.get(url)
.map(function (response) { return response.text(); })
.finally(function () {
_this._inProgressUrlFetches.delete(url);
})
.share();
this._inProgressUrlFetches.set(url, req);
return req;
};
MdIconRegistry = __decorate([
Injectable(),
__metadata('design:paramtypes', [Http])
], MdIconRegistry);
return MdIconRegistry;
}());
/** Clones an SVGElement while preserving type information. */
function cloneSvg(svg) {
return svg.cloneNode(true);
}
//# sourceMappingURL=icon-registry.js.map