1
- import { Directive , ElementRef , Renderer , Attribute } from '@angular/core' ;
1
+ import { Component , ElementRef , Renderer , Attribute } from '@angular/core' ;
2
2
3
3
import { Config } from '../../config/config' ;
4
4
@@ -10,10 +10,13 @@ import { Config } from '../../config/config';
10
10
* @see {@link /docs/v2/components/#chips Chips Component Docs }
11
11
**/
12
12
13
- @Directive ( {
14
- selector : 'ion-chip'
13
+ @Component ( {
14
+ selector : 'ion-chip' ,
15
+ template : '<span class="hide"><ng-content></ng-content></span><ion-label></ion-label><button clear dark (click)="deleteChip()" *ngIf="deletable"><ion-icon name="close-circle"></ion-icon></button>'
15
16
} )
17
+
16
18
export class Chip {
19
+ private deletable : boolean = false ;
17
20
18
21
constructor (
19
22
config : Config ,
@@ -25,31 +28,84 @@ export class Chip {
25
28
this . _readAttrs ( element ) ;
26
29
}
27
30
31
+ /**
32
+ * @private
33
+ */
34
+ ngOnInit ( ) {
35
+ this . _fixContent ( this . _elementRef . nativeElement ) ;
36
+ }
37
+
28
38
/**
29
39
* @private
30
40
*/
31
41
private _readAttrs ( element : HTMLElement ) {
42
+
32
43
let elementAttrs = element . attributes ;
33
44
let attrName : string ;
34
45
35
- console . info ( "mushroom" ) ;
36
-
37
46
for ( let i = 0 , l = elementAttrs . length ; i < l ; i ++ ) {
38
47
if ( elementAttrs [ i ] . value !== '' ) continue ;
39
48
40
49
attrName = elementAttrs [ i ] . name ;
41
50
42
- // Ignore attributes item-left, item-right
43
- if ( attrName . indexOf ( 'item' ) === - 1 ) {
44
- this . _setClass ( attrName ) ;
51
+ if ( attrName === 'deletable' ) {
52
+ this . _setDeletable ( ) ;
45
53
}
46
54
}
47
55
}
48
56
49
57
/**
50
58
* @private
51
59
*/
52
- private _setClass ( color : string ) {
53
- this . _renderer . setElementClass ( this . _elementRef . nativeElement , 'chip-' + color , true ) ;
60
+ private deleteChip ( ) {
61
+ this . _elementRef . nativeElement . remove ( ) ;
62
+ }
63
+
64
+ /**
65
+ * @private
66
+ */
67
+ private _setDeletable ( ) {
68
+ this . deletable = true ;
69
+ }
70
+
71
+
72
+ /**
73
+ * @private
74
+ */
75
+ private _fixContent ( element : HTMLElement ) {
76
+ // Take the first text node and add it to the label.
77
+ // Take the first icon or avatar and add it to the chip.
78
+ // Discard everything else.
79
+
80
+ let childNodes : NodeList = element . childNodes ;
81
+ let innerNode : Node = childNodes [ 0 ] ;
82
+ let labelNode : Node = childNodes [ 1 ] ;
83
+ let addedNodes : NodeList = innerNode . childNodes ;
84
+ element . removeChild ( innerNode ) ;
85
+
86
+ let missing = { image : true , text : true } ;
87
+ let childNode : Node ;
88
+ let imageNode : Node ;
89
+ let text : string ;
90
+ for ( let i = 0 , l = addedNodes . length ; i < l ; i ++ ) {
91
+ childNode = addedNodes [ i ] ;
92
+ if ( childNode . nodeType === 3 && missing . text ) {
93
+ text = childNode . textContent . trim ( ) ;
94
+ if ( text !== '' ) {
95
+ labelNode . textContent = text ;
96
+ missing . text = false ;
97
+ }
98
+ }
99
+ if ( childNode . nodeType === 1 && missing . image ) {
100
+ name = childNode . nodeName ;
101
+ if ( childNode . nodeName === 'ION-ICON' || childNode . nodeName === 'ION-AVATAR' ) {
102
+ missing . image = false ;
103
+ imageNode = childNode ;
104
+ }
105
+ }
106
+ }
107
+ if ( imageNode ) {
108
+ element . insertBefore ( imageNode , element . firstChild ) ;
109
+ }
54
110
}
55
111
}
0 commit comments