From 62e82fedb1f1fb1c55250407491c1ed3f8e3d6ee Mon Sep 17 00:00:00 2001 From: Brian Nenninger Date: Wed, 23 Mar 2016 01:45:53 -0400 Subject: [PATCH 01/37] Start of md-icon directive and service. --- src/components/icon/icon.ts | 37 +++++++++++++++++++++++++++ src/components/icon/icon_provider.ts | 38 ++++++++++++++++++++++++++++ src/demo-app/demo-app.html | 1 + src/demo-app/demo-app.ts | 2 ++ src/demo-app/icon/icon-demo.html | 3 +++ src/demo-app/icon/icon-demo.ts | 11 ++++++++ src/main.ts | 5 +++- 7 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/components/icon/icon.ts create mode 100644 src/components/icon/icon_provider.ts create mode 100644 src/demo-app/icon/icon-demo.html create mode 100644 src/demo-app/icon/icon-demo.ts diff --git a/src/components/icon/icon.ts b/src/components/icon/icon.ts new file mode 100644 index 000000000000..342ad89f96c6 --- /dev/null +++ b/src/components/icon/icon.ts @@ -0,0 +1,37 @@ +import { + Component, + Directive, + Input, + OnChanges, + SimpleChange, +} from 'angular2/core'; + +import { + MdIconProvider, +} from './icon_provider'; + +@Component({ + template: ``, + selector: 'md-icon', + viewProviders: [MdIconProvider], +}) +export class MdIcon implements OnChanges { + @Input() svgSrc: string; + @Input() svgIcon: string; + + iconSvg: string; + + constructor(private _mdIconProvider: MdIconProvider) { + this.iconSvg = '123456'; + } + + ngOnChanges(changes: {[propertyName: string]: SimpleChange}) { + if (this.svgIcon) { + this._mdIconProvider.loadIcon(this.svgIcon) + .subscribe((svg: string) => {this.iconSvg = svg;}); + } else if (this.svgSrc) { + this._mdIconProvider.loadUrl(this.svgSrc) + .subscribe((svg: string) => {this.iconSvg = svg;}); + } + } +} diff --git a/src/components/icon/icon_provider.ts b/src/components/icon/icon_provider.ts new file mode 100644 index 000000000000..8f654ced9f25 --- /dev/null +++ b/src/components/icon/icon_provider.ts @@ -0,0 +1,38 @@ +import {Injectable} from 'angular2/core'; +import {Http, HTTP_PROVIDERS} from 'angular2/http'; +import {Observable} from 'rxjs/Rx'; + +@Injectable() +export class MdIconProvider { + private _namedIcons: Map>; + private _defaultIcons: Map; + + constructor(private _http: Http) { + this._namedIcons = new Map(); + this._defaultIcons = new Map(); + this._defaultIcons.set('search', ` + + + + + `); + } + + loadIcon(iconName: string): Observable { + if (this._defaultIcons.has(iconName)) { + return Observable.of(this._defaultIcons.get(iconName)); + } + throw Error('Unknown icon: ' + iconName); + } + + loadUrl(url: string): Observable { + return this._http.get(url) + .map(response => this._extractSvg(response.text())); + } + + private _extractSvg(responseText: string) { + const start = responseText.indexOf(''); + const end = responseText.lastIndexOf(''); + return responseText.substring(start, end + ''.length); + } +} diff --git a/src/demo-app/demo-app.html b/src/demo-app/demo-app.html index e1ad0b6cbacd..89327df13085 100644 --- a/src/demo-app/demo-app.html +++ b/src/demo-app/demo-app.html @@ -9,6 +9,7 @@

Angular Material2 Demos

  • Checkbox demo
  • Toolbar demo
  • Radio demo
  • +
  • Icon demo