import {Directive, View, CSSClass, ElementRef, Optional, Parent, Attribute} from 'angular2/angular2'; import {IonicConfig} from '../../config/config'; import {IonicComponent} from '../../config/annotations'; import {Ion} from '../ion'; import {Platform} from '../../platform/platform'; import {Button} from '../button/button'; /* 'home': { ios: ['ion-ios-home', 'ion-ios-home-outline'], md: 'ion-md-home' } 1-for-1 swap Map of stuff that's 1-for-1 Always use the same no matter what Cuz it's not in the map of 1-for-1's Different between modes Used different attributes Ionicons SVG ...ios... ...md... Custom SVG File Custom Font Icon */ @Directive({ selector: 'icon', properties: [ 'name', 'iconName' ], host: { '[attr.aria-label]': 'label', '[attr.aria-hidden]': 'ariaHidden', 'role': 'img' } }) export class IconDirective { constructor( elementRef: ElementRef, @Optional() @Parent() parentButton: Button, @Attribute('forward') forward: string, config: IonicConfig ) { let ele = this.ele = elementRef.nativeElement; this.iconLeft = this.iconRight = this.iconOnly = false; this.ariaHidden = true; if (forward !== null) { this.fwdIcon = config.setting('forwardIcon'); } if (parentButton) { // this icon is within a button this.withinButton = true; // check if there is a sibling element (that's not aria hidden) let hasPreviousSiblingElement = !!ele.previousElementSibling; let hasNextSiblingElement = ele.nextElementSibling && ele.nextElementSibling.getAttribute('aria-hidden') !== 'true'; if (!hasPreviousSiblingElement && !hasNextSiblingElement) { // this icon is within a button, and doesn't have a sibling element // check for text nodes to the right and left of this icon element this.iconLeft = (ele.nextSibling && ele.nextSibling.textContent || '').trim() !== ''; this.iconRight = (ele.previousSibling && ele.previousSibling.textContent || '').trim() !== ''; this.iconOnly = !this.iconLeft && !this.iconRight; } // tell the button there's a child icon // the button will set the correct css classes on itself parentButton.registerIcon(this); } } onInit() { if (this.fwdIcon) { this.name = this.fwdIcon; } if (!this.name) return; // add the css class to show the icon font this.ele.classList.add(this.name); // hide the icon when it's within a button // and the button isn't an icon only button this.ariaHidden = (this.withinButton && !this.iconOnly); if (!this.ariaHidden) { // the icon is either not within a button // or the icon is within a button, and its an icon only button this.label = this.name.replace('ion-', '').replace('ios-', '').replace('md-', '').replace('-', ''); } } onDestroy() { this.ele = null; } } @IonicComponent({ selector: 'ion-icon', properties: [ 'md', 'ios' ], host: { 'mode': 'mode' } }) @View({ template: '', directives: [CSSClass] }) export class Icon extends Ion { constructor(elementRef: ElementRef, ionicConfig: IonicConfig) { super(elementRef, ionicConfig); } onIonInit() { this.iconClass = this.ios; console.log('ICON', this.mode); setTimeout(() => { console.log('MODE', this.mode); }); } }