Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
fix: remove pre-defined events, should be dynamic, because plug-ins c…
Browse files Browse the repository at this point in the history
…ould use different events
  • Loading branch information
erik-lieben committed Sep 10, 2016
1 parent 71c5f3c commit d445050
Showing 1 changed file with 10 additions and 60 deletions.
70 changes: 10 additions & 60 deletions src/chartist.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { bindable, bindingMode } from "aurelia-framework";
import { bindable, bindingMode, customElement } from "aurelia-framework";
import * as chartist from "chartist";

export class Chartist {
@customElement("chartist")
export class ChartistElement {

public element: HTMLElement;
public chart;

@bindable()
public type: string;
Expand All @@ -17,29 +19,17 @@ export class Chartist {
@bindable({ bindingMode: bindingMode.oneTime })
public responsiveOptions: Array<chartist.IResponsiveOptionTuple<any>>;

@bindable({ attribute: "draw"})
public drawCall: (data) => void;
public eventsToAttachOnAttached = [];

@bindable({ attribute: "options-changed"})
public optionsChangedCall: (data) => void;

@bindable({ attribute: "animation-beging"})
public animationBeginCall: (data) => void;

@bindable({ attribute: "animation-end"})
public animationEndCall: (data) => void;

@bindable({ attribute: "data-changed"})
public dataChangedCall: (data) => void;

@bindable({ attribute: "created"})
public createdCall: (data) => void;

private chart;
private readonly allowedTypes = ["Bar", "Line", "Pie"];

public attached() {
this.renderChart();

// events that we tried to add before the object was created
for (let item of this.eventsToAttachOnAttached) {
this.chart.on(item.name, item.value);
}
}

public detached() {
Expand All @@ -66,30 +56,6 @@ export class Chartist {
this.renderChart();
}

public drawCallChanged(newValue, oldValue) {
this.callChanged("draw", newValue, oldValue);
}

public optionsChangedCallChanged(newValue, oldValue) {
this.callChanged("optionsChanged", newValue, oldValue);
}

public animationBeginCallChanged(newValue, oldValue) {
this.callChanged("animationBegin", newValue, oldValue);
}

public animationEndCallChanged(newValue, oldValue) {
this.callChanged("animationEnd", newValue, oldValue);
}

public dataCallChanged(newValue, oldValue) {
this.callChanged("data", newValue, oldValue);
}

public createdCallChanged(newValue, oldValue) {
this.callChanged("created", newValue, oldValue);
}

private renderChart() {
if (!this.data) {
console.warn("Chartist data is not set on element");
Expand All @@ -108,20 +74,4 @@ export class Chartist {
this.chart = chartist[this.type](this.element, this.data, this.options, this.responsiveOptions);
}
}

private callChanged(event: string, newValue, oldValue) {

if (!this.element || !this.chart) {
return;
}

if (newValue === undefined && oldValue) {
this.chart.off(event, oldValue);
} else if (newValue) {
this.chart.on(event, newValue);
if (oldValue) {
this.chart.off(event, oldValue);
}
}
}
}

0 comments on commit d445050

Please sign in to comment.