Skip to content

Commit

Permalink
Refactor and add change detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Çağatay Çivici committed Apr 26, 2016
1 parent 5fdd03b commit eb0d902
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions components/inputswitch/inputswitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class InputSwitch implements ControlValueAccessor, AfterViewInit {
private offLabelChild: any;

private offset: any;

initialized: boolean = false;

constructor(private el: ElementRef, private domHandler: DomHandler) {}

Expand Down Expand Up @@ -110,19 +112,27 @@ export class InputSwitch implements ControlValueAccessor, AfterViewInit {
this.onContainer.style.width = 0 + 'px';
this.onLabelChild.style.marginLeft = -this.offset + 'px';
}

this.initialized = true;
}

toggle(event,checkbox) {
if(!this.disabled) {
checkbox.focus();
this.checked = !this.checked;
this.checked ? this.checkUI() : this.uncheckUI();
if(this.checked) {
this.checked = false;
this.uncheckUI()
}
else {
this.checked = true;
this.checkUI();
}

this.onModelChange(this.checked);
this.onModelTouched();
this.onChange.emit({
originalEvent: event,
checked: this.checked
})
});
checkbox.focus();
}
}

Expand Down Expand Up @@ -151,6 +161,13 @@ export class InputSwitch implements ControlValueAccessor, AfterViewInit {

writeValue(value: any) : void {
this.value = value;

if(this.initialized) {
if(this.value === true)
this.checkUI();
else
this.uncheckUI();
}
}

registerOnChange(fn: Function): void {
Expand Down

0 comments on commit eb0d902

Please sign in to comment.