UNPKG

@obliczeniowo/elementary

Version:
204 lines (198 loc) 8.23 kB
import { NG_VALUE_ACCESSOR, FormsModule, ReactiveFormsModule } from '@angular/forms'; import * as i1 from '@obliczeniowo/elementary/value-picker'; import { AbstractValueSetModel, ValuePickerModule } from '@obliczeniowo/elementary/value-picker'; import * as i0 from '@angular/core'; import { EventEmitter, Output, Input, Component, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { of } from 'rxjs'; import { ElementaryMath } from '@obliczeniowo/elementary/math'; class YearValuePickerModel extends AbstractValueSetModel { start; end; next(current) { return of({ value: current && ElementaryMath.minmax(current.value + 1, this.start, this.end) || this.start }); } previous(current) { return of({ value: current && ElementaryMath.minmax(current.value - 1, this.start, this.end) || this.start }); } constructor(start, end) { super(); this.start = start; this.end = end; } init(current) { if (current) { const { value } = current; if (value < this.start || value > this.end) { this.set = new Array(Math.min(this.end - this.start, this.max)).fill(0).map((_, index) => ({ value: this.start + index })); } else { const start = Math.min(value, this.end - this.max + 1); this.set = new Array(Math.min(this.end - this.start, this.max)).fill(0).map((_, index) => ({ value: start + index })); } } else { this.set = new Array(Math.min(this.end - this.start, this.max)).fill(0).map((_, index) => ({ value: this.start + index })); } return of(); } getNextSet() { const end = this.set[this.set.length - 1].value; if (end === this.end) { return of(false); } const start = Math.min(end, this.end - this.max); this.set = new Array(this.max).fill(0).map((_, index) => ({ value: start + index + 1 })); return of(true); } getPreviousSet() { let start = this.set[0].value; if (start === this.start) { return of(false); } start = Math.max(start - this.max, this.start); this.set = new Array(this.max).fill(0).map((_, index) => ({ value: start + index })); return of(true); } getCurrentSet() { return of(this.set); } displayNextPrevious() { return of(true); } decreaseDisabled(current) { return current?.value === this.start; } increaseDisabled(current) { return current?.value === this.end; } selected(item, current) { return current?.value === item.value; } nextDisabled() { return of(this.set[this.set.length - 1].value === this.end); } previousDisabled() { return of(this.set[0].value === this.start); } } class YearPickerComponent { model = new YearValuePickerModel(new Date().getFullYear() - 20, new Date().getFullYear() + 20); current; set start(year) { this.model.start = year; } set end(year) { this.model.end = year; } placeholder; /** * disable the whole things */ disabled = false; display = { nextPrevious: true }; picker = {}; changed = new EventEmitter(); touched = false; onChange = (value) => { }; onTouched = () => { }; registerOnValidatorChange(fn) { this.onChange = fn; } writeValue(set) { this.current = set; } registerOnChange(onChange) { this.onChange = onChange; } registerOnTouched(onTouched) { this.onTouched = onTouched; } setDisabledState(disabled) { this.disabled = disabled; } markAsTouched() { if (this.disabled) { return; } if (!this.touched) { this.onTouched(); this.touched = true; } } itemChanged(item) { this.current = item; this.onChange(this.current); this.changed.emit(this.current); } ngOnInit() { this.model.init(this.current && { value: this.current } || undefined); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.4", ngImport: i0, type: YearPickerComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.0.4", type: YearPickerComponent, isStandalone: false, selector: "obl-year-picker", inputs: { current: "current", start: "start", end: "end", placeholder: "placeholder", disabled: "disabled", display: "display", picker: "picker" }, outputs: { changed: "changed" }, providers: [ { provide: NG_VALUE_ACCESSOR, multi: true, useExisting: YearPickerComponent }, ], ngImport: i0, template: "<obl-value-picker\n [model]=\"model\"\n [current]=\"(current && { value: current }) || undefined\"\n [disabled]=\"disabled\"\n [display]=\"display\"\n [picker]=\"picker\"\n [placeholder]=\"placeholder\"\n (changed)=\"itemChanged($event.value)\"\n></obl-value-picker>\n", styles: [""], dependencies: [{ kind: "component", type: i1.ValuePickerComponent, selector: "obl-value-picker", inputs: ["current", "items", "disabled", "placeholder", "model", "display", "picker"], outputs: ["changed"] }] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.4", ngImport: i0, type: YearPickerComponent, decorators: [{ type: Component, args: [{ selector: 'obl-year-picker', providers: [ { provide: NG_VALUE_ACCESSOR, multi: true, useExisting: YearPickerComponent }, ], standalone: false, template: "<obl-value-picker\n [model]=\"model\"\n [current]=\"(current && { value: current }) || undefined\"\n [disabled]=\"disabled\"\n [display]=\"display\"\n [picker]=\"picker\"\n [placeholder]=\"placeholder\"\n (changed)=\"itemChanged($event.value)\"\n></obl-value-picker>\n" }] }], propDecorators: { current: [{ type: Input }], start: [{ type: Input }], end: [{ type: Input }], placeholder: [{ type: Input }], disabled: [{ type: Input }], display: [{ type: Input }], picker: [{ type: Input }], changed: [{ type: Output }] } }); class YearPickerModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.4", ngImport: i0, type: YearPickerModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.0.4", ngImport: i0, type: YearPickerModule, declarations: [YearPickerComponent], imports: [CommonModule, ValuePickerModule, FormsModule, ReactiveFormsModule], exports: [YearPickerComponent] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.0.4", ngImport: i0, type: YearPickerModule, imports: [CommonModule, ValuePickerModule, FormsModule, ReactiveFormsModule] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.4", ngImport: i0, type: YearPickerModule, decorators: [{ type: NgModule, args: [{ declarations: [ YearPickerComponent ], imports: [ CommonModule, ValuePickerModule, FormsModule, ReactiveFormsModule ], exports: [ YearPickerComponent ] }] }] }); /** * Generated bundle index. Do not edit. */ export { YearPickerComponent, YearPickerModule }; //# sourceMappingURL=obliczeniowo-elementary-year-picker.mjs.map