From 0dece7252bac8c8f0720fba7a7eb6297fbfa462d Mon Sep 17 00:00:00 2001 From: Lee Nathan Date: Mon, 15 Aug 2016 17:45:49 -0700 Subject: [PATCH] feat(chips): finished Component --- src/components/chip/chip.scss | 4 +- src/components/chip/chip.ts | 76 ++++++++++++++++++++--- src/components/chip/test/basic/main.html | 4 +- src/components/chip/test/delete/main.html | 35 ++++++----- src/components/chip/test/icon/main.html | 12 ++-- src/components/chip/test/image/main.html | 2 +- 6 files changed, 97 insertions(+), 36 deletions(-) diff --git a/src/components/chip/chip.scss b/src/components/chip/chip.scss index a5c3eb2b39f..c840d9d8e33 100644 --- a/src/components/chip/chip.scss +++ b/src/components/chip/chip.scss @@ -12,7 +12,7 @@ $chip-margin: 2px 0 !default; $chip-label-color: rgba(0, 0, 0, .87) !default; $chip-icon-color: rgba(0, 0, 0, .87) !default; -$chip-remove-color: rgba(137, 137, 137, .54) !default; +$chip-remove-icon-color: rgba(137, 137, 137, .54) !default; ion-chip { display: inline-flex; @@ -87,6 +87,6 @@ ion-chip { font-size: 1.4em; - color: $chip-remove-color; + color: $chip-remove-icon-color; } } diff --git a/src/components/chip/chip.ts b/src/components/chip/chip.ts index 21c7429d9bc..e79c06bda72 100644 --- a/src/components/chip/chip.ts +++ b/src/components/chip/chip.ts @@ -1,4 +1,4 @@ -import { Directive, ElementRef, Renderer, Attribute } from '@angular/core'; +import { Component, ElementRef, Renderer, Attribute } from '@angular/core'; import { Config } from '../../config/config'; @@ -10,10 +10,13 @@ import { Config } from '../../config/config'; * @see {@link /docs/v2/components/#chips Chips Component Docs} **/ -@Directive({ - selector: 'ion-chip' +@Component({ + selector: 'ion-chip', + template: '' }) + export class Chip { + private deletable: boolean = false; constructor( config: Config, @@ -25,23 +28,28 @@ export class Chip { this._readAttrs(element); } + /** + * @private + */ + ngOnInit() { + this._fixContent(this._elementRef.nativeElement); + } + /** * @private */ private _readAttrs(element: HTMLElement) { + let elementAttrs = element.attributes; let attrName: string; - console.info("mushroom"); - 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); + if (attrName === 'deletable') { + this._setDeletable(); } } } @@ -49,7 +57,55 @@ export class Chip { /** * @private */ - private _setClass(color: string) { - this._renderer.setElementClass(this._elementRef.nativeElement, 'chip-' + color, true); + private deleteChip() { + this._elementRef.nativeElement.remove(); + } + + /** + * @private + */ + private _setDeletable() { + this.deletable = true; + } + + + /** + * @private + */ + private _fixContent(element: HTMLElement) { + // Take the first text node and add it to the label. + // Take the first icon or avatar and add it to the chip. + // Discard everything else. + + let childNodes: NodeList = element.childNodes; + let innerNode: Node = childNodes[0]; + let labelNode: Node = childNodes[1]; + let addedNodes: NodeList = innerNode.childNodes; + element.removeChild(innerNode); + + let missing = {image: true, text: true}; + let childNode: Node; + let imageNode: Node; + let text: string; + for (let i = 0, l = addedNodes.length; i < l; i++) { + childNode = addedNodes[i]; + if (childNode.nodeType === 3 && missing.text) { + text = childNode.textContent.trim(); + if (text !== '') { + labelNode.textContent = text; + missing.text = false; + } + } + if (childNode.nodeType === 1 && missing.image) { + name = childNode.nodeName; + if (childNode.nodeName === 'ION-ICON' || childNode.nodeName === 'ION-AVATAR') { + missing.image = false; + imageNode = childNode; + } + } + } + if (imageNode) { + element.insertBefore(imageNode, element.firstChild); + } } } diff --git a/src/components/chip/test/basic/main.html b/src/components/chip/test/basic/main.html index 38be6b8758f..b2eba2d36e1 100644 --- a/src/components/chip/test/basic/main.html +++ b/src/components/chip/test/basic/main.html @@ -11,12 +11,12 @@

- Default + Default

- Another Longer Text + Another With Longer Text

diff --git a/src/components/chip/test/delete/main.html b/src/components/chip/test/delete/main.html index 910a4d28a90..b970395c2be 100644 --- a/src/components/chip/test/delete/main.html +++ b/src/components/chip/test/delete/main.html @@ -10,33 +10,38 @@

- - Default - + + Default

- + - Primary - + With Icon

- + - Default - + With Avatar + +

+ +

+ + This Should Not Be Seen + + With Junk Elements + + + + +


+ This Should Not Be Seen Either

diff --git a/src/components/chip/test/icon/main.html b/src/components/chip/test/icon/main.html index caf3c7d0853..e04a54e444f 100644 --- a/src/components/chip/test/icon/main.html +++ b/src/components/chip/test/icon/main.html @@ -12,37 +12,37 @@

- Default + Default

- Primary + Primary

- Secondary + Secondary

- Danger + Danger

- Light + Light

- Dark + Dark

diff --git a/src/components/chip/test/image/main.html b/src/components/chip/test/image/main.html index 0f05271813a..a83f99d31c1 100644 --- a/src/components/chip/test/image/main.html +++ b/src/components/chip/test/image/main.html @@ -14,7 +14,7 @@ - Default + Default