Files
Adam Bradley 32e03d6790 ionicons 3.0
2015-08-31 13:51:47 -05:00

149 lines
4.0 KiB
TypeScript

import {Directive, View, NgClass, ElementRef, Optional, Host, Attribute, Renderer} from 'angular2/angular2';
import {IonicConfig} from '../../config/config';
import {IonicComponent} from '../../config/annotations';
import {Ion} from '../ion';
import {Button} from '../button/button';
/**
* TODO
*/
@Directive({
selector: 'icon',
properties: [
'name',
'ios',
'md',
'isActive'
],
host: {
'[attr.aria-label]': 'label',
'[attr.aria-hidden]': 'ariaHidden',
'role': 'img'
}
})
export class Icon {
/**
* TODO
* @param {ElementRef} elementRef TODO
* @param {Button} hostButton TODO
* @param {IonicConfig} config TODO
* @param {Renderer} renderer TODO
*/
constructor(
private elementRef: ElementRef,
@Optional() @Host() hostButton: Button,
config: IonicConfig,
private renderer: Renderer
) {
let ele = elementRef.nativeElement;
this.mode = config.setting('iconMode');
this.iconLeft = this.iconRight = this.iconOnly = false;
this.ariaHidden = true;
if (ele.hasAttribute('forward')) {
this.name = config.setting('forwardIcon');
}
this.iconAttr = null;
for (let i = 0, l = ele.attributes.length; i < l; i++) {
if (ele.attributes[i].value === '' && /_|item-|is-active|class/.test(ele.attributes[i].name) !== true) {
this.iconAttr = ele.attributes[i].name;
}
}
if (hostButton) {
// 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
hostButton.registerIcon(this);
}
}
/**
* TODO
*/
onInit() {
if (this.mode == 'ios' && this.ios) {
this.name = this.ios;
} else if (this.mode == 'md' && this.md) {
this.name = this.md;
} else {
this.name = (this.name ? this.name : this.iconAttr);
if (!(/^ion-/.test(this.name))) {
// not an exact icon being used
// add mode specific prefix
this.name = 'ion-' + this.mode + '-' + this.name;
}
}
if (!this.name) return;
// hide the icon when it's within a button
// and the button isn't an icon only button
this.ariaHidden = (this.withinButton && !this.iconOnly);
this.update();
}
get isActive() {
return (this._isActive === undefined || this._isActive === true || this._isActive === 'true');
}
set isActive(val) {
this._isActive = val;
this.update();
}
update() {
if (this.name && this.mode == 'ios') {
if (this.isActive) {
if (/-outline/.test(this.name)) {
this.name = this.name.replace('-outline', '');
}
} else if (!(/-outline/.test(this.name))) {
this.name += '-outline';
}
}
if (this._name !== this.name) {
if (this._name) {
this.renderer.setElementClass(this.elementRef, this._name, false);
}
this._name = this.name;
this.renderer.setElementClass(this.elementRef, this.name, true);
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('-', ' ');
}
}
}
}