forked from bleenco/ng2-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathng2-datepicker.ts
More file actions
178 lines (148 loc) · 4.84 KB
/
ng2-datepicker.ts
File metadata and controls
178 lines (148 loc) · 4.84 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {Component, ViewContainerRef, Input, Output, EventEmitter, AfterViewInit} from 'angular2/core';
import {NgIf, NgFor, NgClass, NgModel, FORM_DIRECTIVES, ControlValueAccessor} from 'angular2/common';
import * as moment_ from 'moment';
const moment: moment.MomentStatic = (<any>moment_)['default'] || moment_;
@Component({
selector: 'datepicker[ngModel]',
templateUrl: 'node_modules/ng2-datepicker/templates/ng2-datepicker.html',
styleUrls: ['node_modules/ng2-datepicker/styles/ng2-datepicker.css'],
directives: [FORM_DIRECTIVES, NgIf, NgFor, NgClass]
})
export class DatePicker implements ControlValueAccessor, AfterViewInit {
public isOpened: boolean;
public dateValue: string;
public viewValue: string;
public days: Array<Object>;
public dayNames: Array<string>;
private el: any;
private date: any;
private viewContainer: ViewContainerRef;
private onChange: Function;
private onTouched: Function;
private cd: any;
private cannonical: number;
@Input('model-format') modelFormat: string;
@Input('view-format') viewFormat: string;
@Input('init-date') initDate: string;
@Input('first-week-day-sunday') firstWeekDaySunday: boolean;
@Input('static') isStatic: boolean;
@Output() changed: EventEmitter<Date> = new EventEmitter<Date>();
constructor(cd: NgModel, viewContainer: ViewContainerRef) {
cd.valueAccessor = this;
this.cd = cd;
this.viewContainer = viewContainer;
this.el = viewContainer.element.nativeElement;
this.init();
}
ngAfterViewInit() {
this.initValue();
}
public openDatepicker(): void {
this.isOpened = true;
}
public closeDatepicker(): void {
this.isOpened = false;
}
public prevYear(): void {
this.date.subtract(1, 'Y');
this.generateCalendar(this.date);
}
public prevMonth(): void {
this.date.subtract(1, 'M');
this.generateCalendar(this.date);
}
public nextYear(): void {
this.date.add(1, 'Y');
this.generateCalendar(this.date);
}
public nextMonth(): void {
this.date.add(1, 'M');
this.generateCalendar(this.date);
}
public selectDate(e, date): void {
e.preventDefault();
if (this.isSelected(date)) return;
let selectedDate = moment(date.day + '.' + date.month + '.' + date.year, 'DD.MM.YYYY');
this.setValue(selectedDate);
this.closeDatepicker();
this.changed.emit(selectedDate.toDate());
}
private generateCalendar(date): void {
let lastDayOfMonth = date.endOf('month').date();
let month = date.month();
let year = date.year();
let n = 1;
let firstWeekDay = null;
this.dateValue = date.format('MMMM YYYY');
this.days = [];
if (this.firstWeekDaySunday === true) {
firstWeekDay = date.set('date', 2).day();
} else {
firstWeekDay = date.set('date', 1).day();
}
if (firstWeekDay !== 1) {
n -= firstWeekDay - 1;
}
for (let i = n; i <= lastDayOfMonth; i += 1) {
if (i > 0) {
this.days.push({ day: i, month: month + 1, year: year, enabled: true });
} else {
this.days.push({ day: null, month: null, year: null, enabled: false });
}
}
}
isSelected(date) {
let selectedDate = moment(date.day + '.' + date.month + '.' + date.year, 'DD.MM.YYYY');
return selectedDate.toDate().getTime() === this.cannonical;
}
private generateDayNames(): void {
this.dayNames = [];
let date = this.firstWeekDaySunday === true ? moment('2015-06-07') : moment('2015-06-01');
for (let i = 0; i < 7; i += 1) {
this.dayNames.push(date.format('ddd'));
date.add('1', 'd');
}
}
private initMouseEvents(): void {
let body = document.getElementsByTagName('body')[0];
body.addEventListener('click', (e) => {
if (!this.isOpened || !e.target) return;
if (this.el !== e.target && !this.el.contains(e.target)) {
this.closeDatepicker();
}
}, false);
}
private setValue(value: any): void {
let val = moment(value, this.modelFormat || 'YYYY-MM-DD');
this.viewValue = val.format(this.viewFormat || 'Do MMMM YYYY');
this.cd.viewToModelUpdate(val.format(this.modelFormat || 'YYYY-MM-DD'));
this.cannonical = val.toDate().getTime();
}
private initValue(): void {
setTimeout(() => {
if (!this.initDate) {
this.setValue(moment().format(this.modelFormat || 'YYYY-MM-DD'));
} else {
this.setValue(moment(this.initDate, this.modelFormat || 'YYYY-MM-DD'));
}
});
}
writeValue(value: string): void {
if (!value) return;
this.setValue(value);
}
registerOnChange(fn: (_: any) => {}): void {
this.onChange = fn;
}
registerOnTouched(fn: (_: any) => {}): void {
this.onTouched = fn;
}
private init(): void {
this.isOpened = false;
this.date = moment();
this.firstWeekDaySunday = false;
this.generateDayNames();
this.generateCalendar(this.date);
this.initMouseEvents();
}
}