Skip to content

Commit

Permalink
feat(chips): finished Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Nathan authored and brandyscarney committed Aug 24, 2016
1 parent 421f637 commit 0dece72
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/components/chip/chip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +87,6 @@ ion-chip {

font-size: 1.4em;

color: $chip-remove-color;
color: $chip-remove-icon-color;
}
}
76 changes: 66 additions & 10 deletions src/components/chip/chip.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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: '<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>'
})

export class Chip {
private deletable: boolean = false;

constructor(
config: Config,
Expand All @@ -25,31 +28,84 @@ 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();
}
}
}

/**
* @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);
}
}
}
4 changes: 2 additions & 2 deletions src/components/chip/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

<p>
<ion-chip>
<ion-label>Default</ion-label>
Default
</ion-chip>
</p>
<p>
<ion-chip>
<ion-label>Another Longer Text</ion-label>
Another With Longer Text
</ion-chip>
</p>

Expand Down
35 changes: 20 additions & 15 deletions src/components/chip/test/delete/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,38 @@
<ion-content padding style="text-align:center">

<p>
<ion-chip>
<ion-label>Default</ion-label>
<button ion-button icon-only clear dark (click)="deleteClicked()">
<ion-icon name="close-circle"></ion-icon>
</button>
<ion-chip deletable>
Default
</ion-chip>
</p>

<p>
<ion-chip>
<ion-chip deletable>
<ion-icon name="pin" primary></ion-icon>
<ion-label>Primary</ion-label>
<button ion-button icon-only clear dark (click)="deleteClicked()">
<ion-icon name="close-circle"></ion-icon>
</button>
With Icon
</ion-chip>
</p>

<p>
<ion-chip>
<ion-chip deletable>
<ion-avatar>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
</ion-avatar>
<ion-label>Default</ion-label>
<button ion-button icon-only clear dark (click)="deleteClicked()">
<ion-icon name="close-circle"></ion-icon>
</button>
With Avatar
</ion-chip>
</p>

<p>
<ion-chip deletable>
<b>This Should Not Be Seen</b>

With Junk Elements
<ion-icon name="pin" primary></ion-icon>
<ion-avatar>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
</ion-avatar>
<hr>
This Should Not Be Seen Either
</ion-chip>
</p>

Expand Down
12 changes: 6 additions & 6 deletions src/components/chip/test/icon/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
<p>
<ion-chip>
<ion-icon name="pin"></ion-icon>
<ion-label>Default</ion-label>
Default
</ion-chip>
</p>
<p>
<ion-chip>
<ion-icon name="pin" primary></ion-icon>
<ion-label>Primary</ion-label>
Primary
</ion-chip>
</p>
<p>
<ion-chip>
<ion-icon name="pin" secondary></ion-icon>
<ion-label>Secondary</ion-label>
Secondary
</ion-chip>
</p>
<p>
<ion-chip>
<ion-icon name="pin" danger></ion-icon>
<ion-label>Danger</ion-label>
Danger
</ion-chip>
</p>
<p>
<ion-chip>
<ion-icon name="pin" light></ion-icon>
<ion-label>Light</ion-label>
Light
</ion-chip>
</p>
<p>
<ion-chip>
<ion-icon name="pin" dark></ion-icon>
<ion-label>Dark</ion-label>
Dark
</ion-chip>
</p>

Expand Down
2 changes: 1 addition & 1 deletion src/components/chip/test/image/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<ion-avatar>
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
</ion-avatar>
<ion-label>Default</ion-label>
Default
</ion-chip>
</p>

Expand Down

0 comments on commit 0dece72

Please sign in to comment.