Skip to content

Commit

Permalink
feat: Added support for chip events
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyboy committed Nov 29, 2016
1 parent f6010cd commit 5b64ae7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/components/chips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Component} from "@angular/core"
<div class="chip">Tag1<i class="close material-icons">close</i></div>
<div class="chip">Tag2<i class="close material-icons">close</i></div>
<br/><hr/>
<div class="chips" materialize="material_chip"></div>
<div class="chips" materialize="material_chip" (chip.add)="add($event.detail)" (chip.delete)="delete($event.detail)"></div>
<div class="chips chips-initial" materialize="material_chip" [materializeParams]="[chipsInit]"></div>
<div class="chips chips-placeholder" materialize="material_chip" [materializeParams]="[chipsPlaceholder]"></div>
<br/><hr/>
Expand All @@ -29,4 +29,11 @@ export class Chips {
secondaryPlaceholder: 'Enter a tag',
};

add(chip) {
console.log("Chip added: " + chip.tag);
}

delete(chip) {
console.log("Chip deleted: " + chip.tag);
}
}
4 changes: 2 additions & 2 deletions src/custom-event-polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function CustomEvent ( type, params = { bubbles: false, cancelable: false, detail: undefined } ) {
export function CustomEvent ( type, detail = undefined, params = { bubbles: false, cancelable: false } ) {
var event = document.createEvent( 'CustomEvent' );
event.initCustomEvent( type, params.bubbles, params.cancelable, params.detail );
event.initCustomEvent( type, params.bubbles, params.cancelable, detail );
return event;
}
if ("Event" in window) {
Expand Down
12 changes: 12 additions & 0 deletions src/materialize-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
picker.set('select', jQueryElement.val(), ...this._params));
}

if (this.isChips()) {
const nativeElement = this._el.nativeElement;
const jQueryElement = $(nativeElement);
jQueryElement.on("chip.add", (e,chip) => nativeElement.dispatchEvent((<any>CustomEvent("chip.add",chip))));
jQueryElement.on("chip.delete", (e,chip) => nativeElement.dispatchEvent((<any>CustomEvent("chip.delete",chip))));
jQueryElement.on("chip.select", (e,chip) => nativeElement.dispatchEvent((<any>CustomEvent("chip.select",chip))));
}

if (this.isTextarea()) {
this._el.nativeElement.dispatchEvent((<any>CustomEvent("autoresize", {bubbles: true, cancelable: false, detail: undefined})));
}
Expand Down Expand Up @@ -209,6 +217,10 @@ export class MaterializeDirective implements AfterViewInit,DoCheck,OnChanges,OnD
return (this._functionName && this._functionName === "pickadate");
}

private isChips() {
return (this._functionName && this._functionName === "material_chip");
}

private isAutocomplete() {
return (this._functionName && this._functionName === "autocomplete");
}
Expand Down

0 comments on commit 5b64ae7

Please sign in to comment.