Skip to content

Instantly share code, notes, and snippets.

@YonathanMeguira
Last active December 6, 2018 15:10
Show Gist options
  • Save YonathanMeguira/f7df4f017739383948689376a0a78066 to your computer and use it in GitHub Desktop.
Save YonathanMeguira/f7df4f017739383948689376a0a78066 to your computer and use it in GitHub Desktop.
slider initialization
/**
* @license
* Copyright Datorama. All Rights Reserved.
*
* Use of this source code is governed by an Apache License 2.0 license that can be
* found in the LICENSE file at https://github.com/datorama/client-core/blob/master/LICENSE
*/
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { untilDestroyed } from 'ngx-take-until-destroy';
@Component({
selector: 'dato-slider-test',
template: `
<form [formGroup]="multiCursorForm" style="background: red" *ngIf="loaded">
<p>Multi-Cursor</p>
<dato-input type="number" class="mb-30" formControlName="min"></dato-input>
<dato-input type="number" class="mb-30" formControlName="max"></dato-input>
<dato-slider [options]="multiCursorOptions" [formControl]="multiCursorSliderForm"></dato-slider>
</form>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DatoSliderTestComponent implements OnInit, AfterViewInit, OnDestroy {
multiCursorForm = new FormGroup({
min: new FormControl(-50),
max: new FormControl(40)
});
multiCursorSliderForm = new FormControl(this.multiCursorForm.getRawValue());
multiCursorOptions = {
min: -100,
max: 100,
multiCursor: false,
unit: '%',
multiCursor: true
};
loaded = false;
constructor(private cdr: ChangeDetectorRef) {
}
ngAfterViewInit() {
setTimeout(()=> {
this.loaded = true;
this.cdr.detectChanges();
this.multiCursorForm.valueChanges.pipe(untilDestroyed(this)).subscribe(value => {
this.multiCursorSliderForm.patchValue({
min: parseFloat(value.min),
max: parseFloat(value.max)
}, { emitEvent: false });
});
this.multiCursorSliderForm.valueChanges.pipe(untilDestroyed(this)).subscribe(res => {
this.multiCursorForm.patchValue({ min: res.min, max: res.max }, { emitEvent: false });
});
}, 0);
}
ngOnDestroy() {
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment