mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 20:33:32 +08:00
refactor(badge): add a directive for badges which adds the colors as a class
This makes it so we can add a class to the badge on a tab which will show the right color. References #5007
This commit is contained in:
@ -16,7 +16,7 @@ ion-badge {
|
|||||||
|
|
||||||
@each $color-name, $color-value in $colors-ios {
|
@each $color-name, $color-value in $colors-ios {
|
||||||
|
|
||||||
ion-badge[#{$color-name}] {
|
.badge-#{$color-name} {
|
||||||
background-color: $color-value;
|
background-color: $color-value;
|
||||||
color: inverse($color-value);
|
color: inverse($color-value);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ ion-badge {
|
|||||||
|
|
||||||
@each $color-name, $color-value in $colors-md {
|
@each $color-name, $color-value in $colors-md {
|
||||||
|
|
||||||
ion-badge[#{$color-name}] {
|
.badge-#{$color-name} {
|
||||||
background-color: $color-value;
|
background-color: $color-value;
|
||||||
color: inverse($color-value);
|
color: inverse($color-value);
|
||||||
}
|
}
|
||||||
|
53
ionic/components/badge/badge.ts
Normal file
53
ionic/components/badge/badge.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import {Directive, ElementRef, Renderer, Attribute} from 'angular2/core';
|
||||||
|
|
||||||
|
import {Config} from '../../config/config';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name Badge
|
||||||
|
* @module ionic
|
||||||
|
* @description
|
||||||
|
* Badges are simple components in Ionic containing numbers or text. You can display a badge to indicate that there is new information associated with the item it is on.
|
||||||
|
* @see {@link /docs/v2/components/#badges Badges Component Docs}
|
||||||
|
|
||||||
|
*/
|
||||||
|
@Directive({
|
||||||
|
selector: 'ion-badge'
|
||||||
|
})
|
||||||
|
export class Badge {
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
config: Config,
|
||||||
|
private _elementRef: ElementRef,
|
||||||
|
private _renderer: Renderer
|
||||||
|
) {
|
||||||
|
let element = _elementRef.nativeElement;
|
||||||
|
|
||||||
|
this._readAttrs(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private _readAttrs(element: HTMLElement) {
|
||||||
|
let elementAttrs = element.attributes;
|
||||||
|
let attrName;
|
||||||
|
for (let i = 0, l = elementAttrs.length; i < l; i++) {
|
||||||
|
if (elementAttrs[i].value !== '') continue;
|
||||||
|
|
||||||
|
attrName = elementAttrs[i].name;
|
||||||
|
|
||||||
|
// Ignore attributes item-left, item-right
|
||||||
|
if (attrName.indexOf('item') == -1) {
|
||||||
|
this._setClass(attrName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private _setClass(color: string) {
|
||||||
|
this._renderer.setElementClass(this._elementRef.nativeElement, 'badge-' + color, true);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ import {OverlayNav} from '../components/overlay/overlay';
|
|||||||
import {Menu} from '../components/menu/menu';
|
import {Menu} from '../components/menu/menu';
|
||||||
import {MenuToggle} from '../components/menu/menu-toggle';
|
import {MenuToggle} from '../components/menu/menu-toggle';
|
||||||
import {MenuClose} from '../components/menu/menu-close';
|
import {MenuClose} from '../components/menu/menu-close';
|
||||||
|
import {Badge} from '../components/badge/badge';
|
||||||
import {Button} from '../components/button/button';
|
import {Button} from '../components/button/button';
|
||||||
import {Blur} from '../components/blur/blur';
|
import {Blur} from '../components/blur/blur';
|
||||||
import {Content} from '../components/content/content';
|
import {Content} from '../components/content/content';
|
||||||
@ -117,6 +118,7 @@ export const IONIC_DIRECTIVES = [
|
|||||||
MenuToggle,
|
MenuToggle,
|
||||||
MenuClose,
|
MenuClose,
|
||||||
|
|
||||||
|
Badge,
|
||||||
Button,
|
Button,
|
||||||
Blur,
|
Blur,
|
||||||
Content,
|
Content,
|
||||||
|
Reference in New Issue
Block a user