UNPKG

@angular-mdc/web

Version:
8 lines 13.9 kB
/** * @license * Copyright (c) Dominic Carretto * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/trimox/angular-mdc-web/blob/master/LICENSE */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common"),require("@angular/cdk/coercion"),require("rxjs/operators"),require("@angular/common/http"),require("@angular/platform-browser"),require("rxjs")):"function"==typeof define&&define.amd?define("@angular-mdc/web/icon",["exports","@angular/core","@angular/common","@angular/cdk/coercion","rxjs/operators","@angular/common/http","@angular/platform-browser","rxjs"],e):e(((t=t||self).ng=t.ng||{},t.ng.web=t.ng.web||{},t.ng.web.icon={}),t.ng.core,t.ng.common,t.ng.cdk.coercion,t.rxjs.operators,t.ng.common.http,t.ng.platformBrowser,t.rxjs)}(this,(function(t,e,n,o,r,i,s,a){"use strict";var c=function(){function t(){}return t.decorators=[{type:e.Directive,args:[{selector:"[materialIcons]",host:{class:"material-icons"}}]}],t}();function l(t){return Error('Unable to find icon with the name "'+t+'"')}function u(){return Error("Could not find HttpClient provider for use with Angular Material icons. Please include the HttpClientModule from @angular/common/http in your app imports.")}function p(t){return Error("The URL provided to MatIconRegistry was not trusted as a resource URL via Angular's DomSanitizer. Attempted URL was \""+t+'".')}function h(t){return Error("The literal provided to MatIconRegistry was not trusted as safe HTML by Angular's DomSanitizer. Attempted literal was \""+t+'".')}var f=function(t,e){this.options=e,this.url=null,this.svgElement=null,t.nodeName?this.svgElement=t:this.url=t},g=function(){function t(t,e,n){this._httpClient=t,this._sanitizer=e,this._svgIconConfigs=new Map,this._iconSetConfigs=new Map,this._cachedIconsByUrl=new Map,this._inProgressUrlFetches=new Map,this._fontCssClassesByAlias=new Map,this._defaultFontSetClass=["material-icons"],this._document=n}return t.prototype.addSvgIcon=function(t,e,n){return this.addSvgIconInNamespace("",t,e,n)},t.prototype.addSvgIconLiteral=function(t,e,n){return this.addSvgIconLiteralInNamespace("",t,e,n)},t.prototype.addSvgIconInNamespace=function(t,e,n,o){return this._addSvgIconConfig(t,e,new f(n,o))},t.prototype.addSvgIconLiteralInNamespace=function(t,n,o,r){var i=this._sanitizer.sanitize(e.SecurityContext.HTML,o);if(!i)throw h(o);var s=this._createSvgElementForSingleIcon(i,r);return(this._addSvgIconConfig(t,n,new f(s,r)))},t.prototype.addSvgIconSet=function(t,e){return this.addSvgIconSetInNamespace("",t,e)},t.prototype.addSvgIconSetLiteral=function(t,e){return this.addSvgIconSetLiteralInNamespace("",t,e)},t.prototype.addSvgIconSetInNamespace=function(t,e,n){return this._addSvgIconSetConfig(t,new f(e,n))},t.prototype.addSvgIconSetLiteralInNamespace=function(t,n,o){var r=this._sanitizer.sanitize(e.SecurityContext.HTML,n);if(!r)throw h(n);var i=this._svgElementFromString(r);return(this._addSvgIconSetConfig(t,new f(i,o)))},t.prototype.registerFontClassAlias=function(t,e){return void 0===e&&(e=t),this._fontCssClassesByAlias.set(t,e),this},t.prototype.classNameForFontAlias=function(t){return this._fontCssClassesByAlias.get(t)||t},t.prototype.setDefaultFontSetClass=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this._defaultFontSetClass=t,this},t.prototype.getDefaultFontSetClass=function(){return this._defaultFontSetClass},t.prototype.getSvgIconFromUrl=function(t){var n=this,o=this._sanitizer.sanitize(e.SecurityContext.RESOURCE_URL,t);if(!o)throw p(t);var i=this._cachedIconsByUrl.get(o);return i?a.of(m(i)):this._loadSvgIconFromConfig(new f(t)).pipe(r.tap((function(t){return n._cachedIconsByUrl.set(o,t)})),r.map((function(t){return m(t)})))},t.prototype.getNamedSvgIcon=function(t,e){void 0===e&&(e="");var n=_(e,t),o=this._svgIconConfigs.get(n);if(o)return this._getSvgFromConfig(o);var r=this._iconSetConfigs.get(e);return r?this._getSvgFromIconSetConfigs(t,r):a.throwError(l(n))},t.prototype.ngOnDestroy=function(){this._svgIconConfigs.clear(),this._iconSetConfigs.clear(),this._cachedIconsByUrl.clear()},t.prototype._getSvgFromConfig=function(t){return t.svgElement?a.of(m(t.svgElement)):this._loadSvgIconFromConfig(t).pipe(r.tap((function(e){return t.svgElement=e})),r.map((function(t){return m(t)})))},t.prototype._getSvgFromIconSetConfigs=function(t,n){var o=this,i=this._extractIconWithNameFromAnySet(t,n);if(i)return a.of(i);var s=n.filter((function(t){return!t.svgElement})).map((function(t){return o._loadSvgIconSetFromConfig(t).pipe(r.catchError((function(n){var r=o._sanitizer.sanitize(e.SecurityContext.RESOURCE_URL,t.url);return console.error("Loading icon set URL: "+r+" failed: "+n.message),a.of(null)})))}));return a.forkJoin(s).pipe(r.map((function(){var e=o._extractIconWithNameFromAnySet(t,n);if(!e)throw l(t);return e})))},t.prototype._extractIconWithNameFromAnySet=function(t,e){for(var n=e.length-1;n>=0;n--){var o=e[n];if(o.svgElement){var r=this._extractSvgIconFromSet(o.svgElement,t,o.options);if(r)return r}}return null},t.prototype._loadSvgIconFromConfig=function(t){var e=this;return this._fetchUrl(t.url).pipe(r.map((function(n){return e._createSvgElementForSingleIcon(n,t.options)})))},t.prototype._loadSvgIconSetFromConfig=function(t){var e=this;return t.svgElement?a.of(t.svgElement):this._fetchUrl(t.url).pipe(r.map((function(n){return t.svgElement||(t.svgElement=e._svgElementFromString(n)),t.svgElement})))},t.prototype._createSvgElementForSingleIcon=function(t,e){var n=this._svgElementFromString(t);return this._setSvgAttributes(n,e),n},t.prototype._extractSvgIconFromSet=function(t,e,n){var o=t.querySelector('[id="'+e+'"]');if(!o)return null;var r=o.cloneNode(!0);if(r.removeAttribute("id"),"svg"===r.nodeName.toLowerCase())return this._setSvgAttributes(r,n);if("symbol"===r.nodeName.toLowerCase())return this._setSvgAttributes(this._toSvgElement(r),n);var i=this._svgElementFromString("<svg></svg>");return i.appendChild(r),this._setSvgAttributes(i,n)},t.prototype._svgElementFromString=function(t){var e=this._document.createElement("DIV");e.innerHTML=t;var n=e.querySelector("svg");if(!n)throw Error("<svg> tag not found");return n},t.prototype._toSvgElement=function(t){for(var e=this._svgElementFromString("<svg></svg>"),n=t.attributes,o=0;o<n.length;o++){var r=n[o],i=r.name,s=r.value;"id"!==i&&e.setAttribute(i,s)}for(o=0;o<t.childNodes.length;o++)t.childNodes[o].nodeType===this._document.ELEMENT_NODE&&e.appendChild(t.childNodes[o].cloneNode(!0));return e},t.prototype._setSvgAttributes=function(t,e){return t.setAttribute("fit",""),t.setAttribute("height","100%"),t.setAttribute("width","100%"),t.setAttribute("preserveAspectRatio","xMidYMid meet"),t.setAttribute("focusable","false"),e&&e.viewBox&&t.setAttribute("viewBox",e.viewBox),t},t.prototype._fetchUrl=function(t){var n=this;if(!this._httpClient)throw u();if(null==t)throw Error('Cannot fetch icon from URL "'+t+'".');var o=this._sanitizer.sanitize(e.SecurityContext.RESOURCE_URL,t);if(!o)throw p(t);var i=this._inProgressUrlFetches.get(o);if(i)return i;var s=this._httpClient.get(o,{responseType:"text"}).pipe(r.finalize((function(){return n._inProgressUrlFetches.delete(o)})),r.share());return this._inProgressUrlFetches.set(o,s),s},t.prototype._addSvgIconConfig=function(t,e,n){return this._svgIconConfigs.set(_(t,e),n),this},t.prototype._addSvgIconSetConfig=function(t,e){var n=this._iconSetConfigs.get(t);return n?n.push(e):this._iconSetConfigs.set(t,[e]),this},t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[{type:i.HttpClient,decorators:[{type:e.Optional}]},{type:s.DomSanitizer},{type:void 0,decorators:[{type:e.Optional},{type:e.Inject,args:[n.DOCUMENT]}]}]},t.ɵprov5=e["ɵɵdefineInjectable"]({factory:function(){return new t(e["ɵɵinject"](i.HttpClient,8),e["ɵɵinject"](s.DomSanitizer),e["ɵɵinject"](n.DOCUMENT,8))},token:t,providedIn:"root"}),t}();function d(t,e,n,o){return t||new g(e,n,o)}var v={provide:g,deps:[[new e.Optional,new e.SkipSelf,g],[new e.Optional,i.HttpClient],s.DomSanitizer,[new e.Optional,n.DOCUMENT]],useFactory:d};function m(t){return t.cloneNode(!0)}function _(t,e){return t+":"+e}function S(){var t=e.inject(n.DOCUMENT),o=t?t.location:null;return{getPathname:function(){return o?o.pathname+o.search:""}}}var I=new e.InjectionToken("mdc-icon-location",{providedIn:"root",factory:S}),y=["clip-path","color-profile","src","cursor","fill","filter","marker","marker-start","marker-mid","marker-end","mask","stroke"],C=y.map((function(t){return"["+t+"]"})).join(", "),E=/^url\(['"]?#(.*?)['"]?\)$/,F=function(){function t(t,e,n,o){this.elementRef=t,this._iconRegistry=e,this._location=o,this._previousFontSetClass=[],this._inline=!1,this.role="img",this._fontSet="",this._fontIcon="",this._clickable=!1,n||this._getHostElement().setAttribute("aria-hidden","true")}return Object.defineProperty(t.prototype,"inline",{get:function(){return this._inline},set:function(t){this._inline=o.coerceBooleanProperty(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"fontSet",{get:function(){return this._fontSet},set:function(t){this._fontSet=this._cleanupFontValue(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"fontIcon",{get:function(){return this._fontIcon},set:function(t){this._fontIcon=this._cleanupFontValue(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"clickable",{get:function(){return this._clickable},set:function(t){this._clickable=o.coerceBooleanProperty(t),this._clickable?this.role="button":this.role=null},enumerable:!0,configurable:!0}),t.prototype.ngAfterViewChecked=function(){var t=this._elementsWithExternalReferences;if(t&&this._location&&t.size){var e=this._location.getPathname();e!==this._previousPath&&(this._previousPath=e,this._prependPathToReferences(e))}},t.prototype.ngOnDestroy=function(){var t;null===(t=this._elementsWithExternalReferences)||void 0===t||t.clear()},t.prototype._splitIconName=function(t){if(!t)return["",""];var e=t.split(":");switch(e.length){case 1:return["",e[0]];case 2:return e;default:throw Error('Invalid icon name: "'+t+'"')}},t.prototype.ngOnChanges=function(t){var e=this,n=t.svgIcon;if(n)if(this.svgIcon){var o=this._splitIconName(this.svgIcon),i=o[0],s=o[1];this._iconRegistry.getNamedSvgIcon(s,i).pipe(r.take(1)).subscribe((function(t){return e._setSvgElement(t)}),(function(t){return console.log("Error retrieving icon: "+t.message)}))}else n.previousValue&&this._clearSvgElement();this._usingFontIcon()&&this._updateFontIconClasses()},t.prototype.ngOnInit=function(){this._usingFontIcon()&&this._updateFontIconClasses()},t.prototype._usingFontIcon=function(){return!this.svgIcon},t.prototype._setSvgElement=function(t){this._clearSvgElement();for(var e=t.querySelectorAll("style"),n=0;n<e.length;n++)e[n].textContent+=" ";if(this._location){var o=this._location.getPathname();this._previousPath=o,this._cacheChildrenWithExternalReferences(t),this._prependPathToReferences(o)}this._getHostElement().appendChild(t)},t.prototype._clearSvgElement=function(){var t=this._getHostElement(),e=t.childNodes.length;for(this._elementsWithExternalReferences&&this._elementsWithExternalReferences.clear();e--;){var n=t.childNodes[e];1===n.nodeType&&"svg"!==n.nodeName.toLowerCase()||t.removeChild(n)}},t.prototype._updateFontIconClasses=function(){if(this._usingFontIcon()){var t=this._getHostElement(),e=this.fontSet?[this._iconRegistry.classNameForFontAlias(this.fontSet)]:this._iconRegistry.getDefaultFontSetClass();this._previousFontSetClass.forEach((function(e){return t.classList.remove(e)})),e.forEach((function(e){return t.classList.add(e)})),this._previousFontSetClass=e,this.fontIcon!==this._previousFontIconClass&&(this._previousFontIconClass&&t.classList.remove(this._previousFontIconClass),this.fontIcon&&t.classList.add(this.fontIcon),this._previousFontIconClass=this.fontIcon)}},t.prototype._cleanupFontValue=function(t){return"string"==typeof t?t.trim().split(" ")[0]:t},t.prototype._prependPathToReferences=function(t){var e=this._elementsWithExternalReferences;e&&e.forEach((function(e,n){e.forEach((function(e){n.setAttribute(e.name,"url('"+t+"#"+e.value+"')")}))}))},t.prototype._cacheChildrenWithExternalReferences=function(t){for(var e=t.querySelectorAll(C),n=this._elementsWithExternalReferences=this._elementsWithExternalReferences||new Map,o=function(t){y.forEach((function(o){var r=e[t],i=r.getAttribute(o),s=i?i.match(E):null;if(s){var a=n.get(r);a||(a=[],n.set(r,a)),a.push({name:o,value:s[1]})}}))},r=0;r<e.length;r++)o(r)},t.prototype._getHostElement=function(){return this.elementRef.nativeElement},t.decorators=[{type:e.Component,args:[{selector:"mdc-icon, [mdcIcon]",exportAs:"mdcIcon",host:{"[attr.role]":"role",class:"ngx-mdc-icon","[class.ngx-mdc-icon--clickable]":"clickable","[class.ngx-mdc-icon--inline]":"inline"},template:"<ng-content></ng-content>",encapsulation:e.ViewEncapsulation.None,changeDetection:e.ChangeDetectionStrategy.OnPush}]}],t.ctorParameters=function(){return[{type:e.ElementRef},{type:g},{type:String,decorators:[{type:e.Attribute,args:["aria-hidden"]}]},{type:void 0,decorators:[{type:e.Inject,args:[I]}]}]},t.propDecorators={inline:[{type:e.Input}],svgIcon:[{type:e.Input}],role:[{type:e.Input}],fontSet:[{type:e.Input}],fontIcon:[{type:e.Input}],clickable:[{type:e.Input}]},t}(),b=[F,c],w=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{exports:b,declarations:b}]}],t}();t.ICON_REGISTRY_PROVIDER=v,t.ICON_REGISTRY_PROVIDER_FACTORY=d,t.MDC_ICON_LOCATION=I,t.MDC_ICON_LOCATION_FACTORY=S,t.MdcIcon=F,t.MdcIconModule=w,t.MdcIconRegistry=g,t.MdcMaterialIcons=c,t.getMdcIconFailedToSanitizeLiteralError=h,t.getMdcIconFailedToSanitizeUrlError=p,t.getMdcIconNameNotFoundError=l,t.getMdcIconNoHttpProviderError=u,Object.defineProperty(t,"__esModule",{value:!0})}));