-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathang.daterange.picker.component.ts
103 lines (77 loc) · 3.27 KB
/
ang.daterange.picker.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Component, ComponentRef, ElementRef, OnInit, Input, Output, EventEmitter, ViewContainerRef } from '@angular/core';
import { DatePickerComponent } from './ang.datepicker.component'
@Component({
selector: 'ang-daterange-picker',
templateUrl: './ang.daterange.picker.component.html',
styleUrls: ['./ang.daterange.picker.component.css']
})
export class DaterangePickerComponent implements OnInit {
public startDate: Date;
public endDate: Date;
private el: ElementRef;
public startDateText: string = '';
public endDateText: string = '';
private selfComponentRef: ComponentRef<any>;
@Output() onCloseDaterangePicker: EventEmitter<any> = new EventEmitter<any>();
@Output() onSelectedDaterange: EventEmitter<any> = new EventEmitter<any>();
constructor(_el: ElementRef) {
this.el = _el;
this.el.nativeElement.style.position = 'absolute';
this.startDate = new Date();
this.endDate = new Date();
}
ngOnInit() {
}
onSelectStartDate($event: any) {
this.startDate = $event.date;
this.startDateText = $event.dateText;
}
onSelectEndDate($event: any) {
this.endDate = $event.date;
this.endDateText = $event.dateText;
}
onApplySelectedDateRange() {
this.onSelectedDaterange.emit({startDate: this.startDate, endDate: this.endDate});
this.destroyComponentRef();
}
onCloseContextualMenu() {
this.onCloseDaterangePicker.emit();
this.destroyComponentRef();
}
destroyComponentRef() {
if (this.selfComponentRef) this.selfComponentRef.destroy();
}
setComponentRef(_componentRef: ComponentRef<any>) {
this.selfComponentRef = _componentRef;
}
static initWithData(_viewContainer: ViewContainerRef, _componentFactory: any): any {
let daterangePickerComponentRef: ComponentRef<any> = _viewContainer.createComponent(_componentFactory);
let instance: DaterangePickerComponent = daterangePickerComponentRef.instance;
instance.setComponentRef(daterangePickerComponentRef);
let targetLeftOffset: number = instance.getViewContainerSpaceOffset(_viewContainer, "left");
let targetTopOffset: number = instance.getViewContainerSpaceOffset(_viewContainer, "top");
instance.setStyleSpaceProperty("left", targetLeftOffset);
let height: number = instance.getViewContainerDOMSpaceProperty(_viewContainer, "clientHeight");
instance.setStyleSpaceProperty("top", targetTopOffset + height + 4);
return instance;
}
getViewContainerDOMSpaceProperty(_viewContainer: ViewContainerRef, _property: string = ''): number {
return parseInt(_viewContainer.element.nativeElement[_property]);
}
setStyleSpaceProperty(property: string = '', pixels: number = 0) {
this.el.nativeElement.style[property] = pixels.toString()+"px";
}
getViewContainerSpaceOffset(_viewContainer: ViewContainerRef, _property: string = ''): number {
let targetMargin = _viewContainer.element.nativeElement.style['margin-'+_property];
if(!targetMargin) {
targetMargin = 0;
} else {
targetMargin = parseInt(targetMargin.replace( /^\D+/g, '')); // Remove non-number characters like 'px'
}
switch(_property) {
case 'left': return _viewContainer.element.nativeElement.offsetLeft;
case 'top': return _viewContainer.element.nativeElement.offsetTop;
default: return 0;
}
}
}