forked from technikhil314/angular-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar-component.js
166 lines (166 loc) · 6.65 KB
/
calendar-component.js
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
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var moment = require('moment');
require('moment-range');
var CalendarComponent = (function () {
function CalendarComponent() {
this.dateChanged = new core_1.EventEmitter();
this.monthChanged = new core_1.EventEmitter();
}
Object.defineProperty(CalendarComponent.prototype, "monthText", {
get: function () {
var months = moment.monthsShort();
return months[this.month];
},
enumerable: true,
configurable: true
});
CalendarComponent.prototype.getWeekNumbers = function (monthRange) {
var weekNumbers = [];
var indexOf = [].indexOf || function (item) { for (var i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item)
return i;
} return -1; };
monthRange.by('days', function (moment) {
var ref;
if (weekNumbers.length < 6 && (ref = moment.week(), indexOf.call(weekNumbers, ref)) < 0) {
return weekNumbers.push(moment.week());
}
});
return weekNumbers;
};
CalendarComponent.prototype.getWeeksRange = function (weeks, year, month) {
var weeksRange = [];
for (var i = 0, len = weeks.length; i < len; i++) {
var week = weeks[i];
var firstWeekDay = void 0, lastWeekDay = void 0;
if (i > 0 && week < weeks[i - 1]) {
firstWeekDay = moment([year, month]).add(1, "year").week(week).day(0);
lastWeekDay = moment([year, month]).add(1, "year").week(week).day(6);
}
else {
firstWeekDay = moment([year, month]).week(week).day(0);
lastWeekDay = moment([year, month]).week(week).day(6);
}
var weekRange = moment.range(firstWeekDay, lastWeekDay);
weeksRange.push(weekRange);
}
return weeksRange;
};
CalendarComponent.prototype.createCalendarGridData = function () {
var year = this.year;
var month = this.month;
var startDate = moment([year, month]);
var firstDay = moment(startDate).startOf('month');
var endDay = moment(startDate).add(60, 'd');
var monthRange = moment.range(firstDay, endDay);
var weeksRange = [];
weeksRange = this.getWeeksRange(this.getWeekNumbers(monthRange), year, month);
var weekList = [];
weeksRange.map(function (week) {
var daysList = [];
week.by('days', function (day) {
daysList.push(day);
});
weekList.push(daysList);
});
this.weekList = weekList;
};
CalendarComponent.prototype.ngOnChanges = function (changes) {
this.createCalendarGridData();
};
CalendarComponent.prototype.isDisabled = function (day) {
if (day.isBefore(moment(this.minDate, this.format)) || day.isAfter(moment(this.maxDate, this.format))) {
return true;
}
};
CalendarComponent.prototype.isDateAvailable = function (day) {
if (day.get('month') !== this.month) {
return false;
}
if (this.inactiveBeforeStart && !this.isLeft && day.isBefore(this.selectedFromDate)) {
return false;
}
return true;
};
CalendarComponent.prototype.isSelectedDate = function (day) {
if (day.get('month') === this.month && day.isSameOrAfter(this.selectedFromDate, 'date') && day.isSameOrBefore(this.selectedToDate, 'date')) {
return true;
}
};
CalendarComponent.prototype.dateSelected = function (day) {
this.dateChanged.emit({
day: day,
isLeft: this.isLeft
});
};
CalendarComponent.prototype.monthSelected = function (value) {
this.monthChanged.emit({
value: value,
isLeft: this.isLeft
});
};
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "month", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "year", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "selectedFromDate", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "selectedToDate", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Boolean)
], CalendarComponent.prototype, "isLeft", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "format", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "minDate", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], CalendarComponent.prototype, "maxDate", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Boolean)
], CalendarComponent.prototype, "inactiveBeforeStart", void 0);
__decorate([
core_1.Output(),
__metadata('design:type', Object)
], CalendarComponent.prototype, "dateChanged", void 0);
__decorate([
core_1.Output(),
__metadata('design:type', Object)
], CalendarComponent.prototype, "monthChanged", void 0);
CalendarComponent = __decorate([
core_1.Component({
moduleId: module.id,
selector: 'calendar',
templateUrl: './calendar-component.html'
}),
__metadata('design:paramtypes', [])
], CalendarComponent);
return CalendarComponent;
}());
exports.CalendarComponent = CalendarComponent;