src/app/shared/components/period-date-picker/period-date-picker.component.ts
selector | crm-period-date-picker |
styleUrls | period-date-picker.component.scss |
templateUrl | ./period-date-picker.component.html |
Properties |
Methods |
Inputs |
Outputs |
constructor()
|
fromNow
|
Type:
Default value: |
limit
|
Type: |
period
|
Type: |
readonly
|
Type:
Default value: |
changed
|
$event type: EventEmitter
|
fromMaxDate |
fromMaxDate()
|
Returns :
any
|
fromMinDate |
fromMinDate()
|
Returns :
any
|
ngOnChanges | ||||||||
ngOnChanges(changes: SimpleChanges)
|
||||||||
Parameters :
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
onNavigate |
onNavigate()
|
Returns :
void
|
Private setFromTo |
setFromTo()
|
Returns :
void
|
toMaxDate |
toMaxDate()
|
Returns :
any
|
toMinDate |
toMinDate()
|
Returns :
any
|
from |
from:
|
Type : DatePicker
|
to |
to:
|
Type : DatePicker
|
import {Component, OnInit, Input, Output, EventEmitter, OnChanges, SimpleChanges} from '@angular/core';
import {DatePicker} from "../../utils/date-picker.type";
import {Period} from "../../models/period";
@Component({
selector: 'crm-period-date-picker',
templateUrl: './period-date-picker.component.html',
styleUrls: ['./period-date-picker.component.scss']
})
export class PeriodDatePickerComponent implements OnInit, OnChanges {
@Input() period: Period;
@Output() changed = new EventEmitter<Period>();
@Input() limit: Period;
@Input() fromNow: boolean = false;
@Input() readonly: boolean = false;
from: DatePicker;
to: DatePicker;
constructor() {
}
onNavigate(): void {
const from = this.from ? DatePicker.fromJson(this.from).getFromTime() : null;
const to = this.to ? DatePicker.fromJson(this.to).getToTime() : null;
this.changed.emit(new Period(from, to))
}
ngOnInit() {
this.setFromTo()
}
ngOnChanges(changes: SimpleChanges) {
if (changes['period']) {
this.setFromTo()
}
}
toMinDate() {
if (this.from) {
return this.from
}
if (this.limit && this.limit.from) {
return DatePicker.fromNumber(this.limit.from)
}
}
toMaxDate() {
if (this.limit && this.limit.to) {
return DatePicker.fromNumber(this.limit.to)
}
}
fromMinDate() {
if(this.fromNow) {
return DatePicker.fromNumber(new Date().getTime())
}
if (this.limit && this.limit.from) {
return DatePicker.fromNumber(this.limit.from)
}
}
fromMaxDate() {
if (this.to) {
return this.to
}
if (this.limit && this.limit.to) {
return DatePicker.fromNumber(this.limit.to)
}
}
private setFromTo() {
if (this.period) {
if (this.period.from) {
this.from = DatePicker.fromNumber(this.period.from);
}
if (this.period.to) {
this.to = DatePicker.fromNumber(this.period.to);
}
}
}
}
<div class="form-group row">
<div class="col-md-5">
<div class="input-group">
<input class="form-control"
placeholder="From"
name="dp"
ngbDatepicker
#fromDate="ngbDatepicker"
[(ngModel)]="from"
[minDate]="fromMinDate()"
[maxDate]="fromMaxDate()"
[firstDayOfWeek]="7"
(ngModelChange)="onNavigate()"
[readonly]="true">
<span class="input-group-btn min-width-20">
<button class="btn btn-info" type="button" (click)="fromDate.toggle()" [disabled]="readonly"><i
class="fa fa-calendar"></i></button>
</span>
</div>
</div>
<div class="col-md-2 form-control-label"></div>
<div class="col-md-5">
<div class="input-group">
<input class="form-control"
placeholder="To"
name="dp"
ngbDatepicker
#toDate="ngbDatepicker"
[(ngModel)]="to"
[minDate]="toMinDate()"
[maxDate]="toMaxDate()"
[firstDayOfWeek]="7"
(ngModelChange)="onNavigate()"
[readonly]="true">
<span class="input-group-btn min-width-20">
<button class="btn btn-info" type="button" (click)="toDate.toggle()" [disabled]="readonly"><i
class="fa fa-calendar"></i></button>
</span>
</div>
</div>
</div>