UNPKG

11.1 kBJavaScriptView Raw
1import { Component, EventEmitter, Input, Output } from '@angular/core';
2import { DateFormatter } from './date-formatter';
3// const MIN_DATE:Date = void 0;
4// const MAX_DATE:Date = void 0;
5// const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
6/*
7 const KEYS = {
8 13: 'enter',
9 32: 'space',
10 33: 'pageup',
11 34: 'pagedown',
12 35: 'end',
13 36: 'home',
14 37: 'left',
15 38: 'up',
16 39: 'right',
17 40: 'down'
18 };
19 */
20export var DatePickerInnerComponent = (function () {
21 function DatePickerInnerComponent() {
22 this.selectionDone = new EventEmitter(undefined);
23 this.update = new EventEmitter(false);
24 this.activeDateChange = new EventEmitter(undefined);
25 this.stepDay = {};
26 this.stepMonth = {};
27 this.stepYear = {};
28 this.modes = ['day', 'month', 'year'];
29 this.dateFormatter = new DateFormatter();
30 }
31 Object.defineProperty(DatePickerInnerComponent.prototype, "activeDate", {
32 get: function () {
33 return this._activeDate;
34 },
35 set: function (value) {
36 this._activeDate = value;
37 },
38 enumerable: true,
39 configurable: true
40 });
41 // todo: add formatter value to Date object
42 DatePickerInnerComponent.prototype.ngOnInit = function () {
43 // todo: use date for unique value
44 this.uniqueId = 'datepicker-' + '-' + Math.floor(Math.random() * 10000);
45 if (this.initDate) {
46 this.activeDate = this.initDate;
47 this.selectedDate = new Date(this.activeDate.valueOf());
48 this.update.emit(this.activeDate);
49 }
50 else if (this.activeDate === undefined) {
51 this.activeDate = new Date();
52 }
53 };
54 // this.refreshView should be called here to reflect the changes on the fly
55 // tslint:disable-next-line:no-unused-variable
56 DatePickerInnerComponent.prototype.ngOnChanges = function (changes) {
57 this.refreshView();
58 };
59 DatePickerInnerComponent.prototype.setCompareHandler = function (handler, type) {
60 if (type === 'day') {
61 this.compareHandlerDay = handler;
62 }
63 if (type === 'month') {
64 this.compareHandlerMonth = handler;
65 }
66 if (type === 'year') {
67 this.compareHandlerYear = handler;
68 }
69 };
70 DatePickerInnerComponent.prototype.compare = function (date1, date2) {
71 if (date1 === undefined || date2 === undefined) {
72 return undefined;
73 }
74 if (this.datepickerMode === 'day' && this.compareHandlerDay) {
75 return this.compareHandlerDay(date1, date2);
76 }
77 if (this.datepickerMode === 'month' && this.compareHandlerMonth) {
78 return this.compareHandlerMonth(date1, date2);
79 }
80 if (this.datepickerMode === 'year' && this.compareHandlerYear) {
81 return this.compareHandlerYear(date1, date2);
82 }
83 return void 0;
84 };
85 DatePickerInnerComponent.prototype.setRefreshViewHandler = function (handler, type) {
86 if (type === 'day') {
87 this.refreshViewHandlerDay = handler;
88 }
89 if (type === 'month') {
90 this.refreshViewHandlerMonth = handler;
91 }
92 if (type === 'year') {
93 this.refreshViewHandlerYear = handler;
94 }
95 };
96 DatePickerInnerComponent.prototype.refreshView = function () {
97 if (this.datepickerMode === 'day' && this.refreshViewHandlerDay) {
98 this.refreshViewHandlerDay();
99 }
100 if (this.datepickerMode === 'month' && this.refreshViewHandlerMonth) {
101 this.refreshViewHandlerMonth();
102 }
103 if (this.datepickerMode === 'year' && this.refreshViewHandlerYear) {
104 this.refreshViewHandlerYear();
105 }
106 };
107 DatePickerInnerComponent.prototype.dateFilter = function (date, format) {
108 return this.dateFormatter.format(date, format);
109 };
110 DatePickerInnerComponent.prototype.isActive = function (dateObject) {
111 if (this.compare(dateObject.date, this.activeDate) === 0) {
112 this.activeDateId = dateObject.uid;
113 return true;
114 }
115 return false;
116 };
117 DatePickerInnerComponent.prototype.createDateObject = function (date, format) {
118 var dateObject = {};
119 dateObject.date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
120 dateObject.label = this.dateFilter(date, format);
121 dateObject.selected = this.compare(date, this.selectedDate) === 0;
122 dateObject.disabled = this.isDisabled(date);
123 dateObject.current = this.compare(date, new Date()) === 0;
124 dateObject.customClass = this.getCustomClassForDate(dateObject.date);
125 return dateObject;
126 };
127 DatePickerInnerComponent.prototype.split = function (arr, size) {
128 var arrays = [];
129 while (arr.length > 0) {
130 arrays.push(arr.splice(0, size));
131 }
132 return arrays;
133 };
134 // Fix a hard-reproducible bug with timezones
135 // The bug depends on OS, browser, current timezone and current date
136 // i.e.
137 // var date = new Date(2014, 0, 1);
138 // console.log(date.getFullYear(), date.getMonth(), date.getDate(),
139 // date.getHours()); can result in "2013 11 31 23" because of the bug.
140 DatePickerInnerComponent.prototype.fixTimeZone = function (date) {
141 var hours = date.getHours();
142 return new Date(date.getFullYear(), date.getMonth(), date.getDate(), hours === 23 ? hours + 2 : 0);
143 };
144 DatePickerInnerComponent.prototype.select = function (date, isManual) {
145 if (isManual === void 0) { isManual = true; }
146 if (this.datepickerMode === this.minMode) {
147 if (!this.activeDate) {
148 this.activeDate = new Date(0, 0, 0, 0, 0, 0, 0);
149 }
150 this.activeDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
151 if (isManual) {
152 this.selectionDone.emit(this.activeDate);
153 }
154 }
155 else {
156 this.activeDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
157 this.datepickerMode = this.modes[this.modes.indexOf(this.datepickerMode) - 1];
158 }
159 this.selectedDate = new Date(this.activeDate.valueOf());
160 this.update.emit(this.activeDate);
161 this.refreshView();
162 };
163 DatePickerInnerComponent.prototype.move = function (direction) {
164 var expectedStep;
165 if (this.datepickerMode === 'day') {
166 expectedStep = this.stepDay;
167 }
168 if (this.datepickerMode === 'month') {
169 expectedStep = this.stepMonth;
170 }
171 if (this.datepickerMode === 'year') {
172 expectedStep = this.stepYear;
173 }
174 if (expectedStep) {
175 var year = this.activeDate.getFullYear() + direction * (expectedStep.years || 0);
176 var month = this.activeDate.getMonth() + direction * (expectedStep.months || 0);
177 this.activeDate = new Date(year, month, 1);
178 this.refreshView();
179 this.activeDateChange.emit(this.activeDate);
180 }
181 };
182 DatePickerInnerComponent.prototype.toggleMode = function (direction) {
183 direction = direction || 1;
184 if ((this.datepickerMode === this.maxMode && direction === 1) ||
185 (this.datepickerMode === this.minMode && direction === -1)) {
186 return;
187 }
188 this.datepickerMode = this.modes[this.modes.indexOf(this.datepickerMode) + direction];
189 this.refreshView();
190 };
191 DatePickerInnerComponent.prototype.getCustomClassForDate = function (date) {
192 var _this = this;
193 if (!this.customClass) {
194 return '';
195 }
196 // todo: build a hash of custom classes, it will work faster
197 var customClassObject = this.customClass
198 .find(function (customClass) {
199 return customClass.date.valueOf() === date.valueOf() &&
200 customClass.mode === _this.datepickerMode;
201 }, this);
202 return customClassObject === undefined ? '' : customClassObject.clazz;
203 };
204 DatePickerInnerComponent.prototype.compareDateDisabled = function (date1Disabled, date2) {
205 if (date1Disabled === undefined || date2 === undefined) {
206 return undefined;
207 }
208 if (date1Disabled.mode === 'day' && this.compareHandlerDay) {
209 return this.compareHandlerDay(date1Disabled.date, date2);
210 }
211 if (date1Disabled.mode === 'month' && this.compareHandlerMonth) {
212 return this.compareHandlerMonth(date1Disabled.date, date2);
213 }
214 if (date1Disabled.mode === 'year' && this.compareHandlerYear) {
215 return this.compareHandlerYear(date1Disabled.date, date2);
216 }
217 return undefined;
218 };
219 DatePickerInnerComponent.prototype.isDisabled = function (date) {
220 var _this = this;
221 var isDateDisabled = false;
222 if (this.dateDisabled) {
223 this.dateDisabled.forEach(function (disabledDate) {
224 if (_this.compareDateDisabled(disabledDate, date) === 0) {
225 isDateDisabled = true;
226 }
227 });
228 }
229 return (isDateDisabled || (this.minDate && this.compare(date, this.minDate) < 0) ||
230 (this.maxDate && this.compare(date, this.maxDate) > 0));
231 };
232 DatePickerInnerComponent.decorators = [
233 { type: Component, args: [{
234 selector: 'datepicker-inner',
235 template: "\n <div *ngIf=\"datepickerMode\" class=\"well well-sm bg-faded p-a card\" role=\"application\" ><!--&lt;!&ndash;ng-keydown=\"keydown($event)\"&ndash;&gt;-->\n <ng-content></ng-content>\n </div>\n "
236 },] },
237 ];
238 /** @nocollapse */
239 DatePickerInnerComponent.ctorParameters = function () { return []; };
240 DatePickerInnerComponent.propDecorators = {
241 'datepickerMode': [{ type: Input },],
242 'startingDay': [{ type: Input },],
243 'yearRange': [{ type: Input },],
244 'minDate': [{ type: Input },],
245 'maxDate': [{ type: Input },],
246 'minMode': [{ type: Input },],
247 'maxMode': [{ type: Input },],
248 'showWeeks': [{ type: Input },],
249 'formatDay': [{ type: Input },],
250 'formatMonth': [{ type: Input },],
251 'formatYear': [{ type: Input },],
252 'formatDayHeader': [{ type: Input },],
253 'formatDayTitle': [{ type: Input },],
254 'formatMonthTitle': [{ type: Input },],
255 'onlyCurrentMonth': [{ type: Input },],
256 'shortcutPropagation': [{ type: Input },],
257 'customClass': [{ type: Input },],
258 'monthColLimit': [{ type: Input },],
259 'yearColLimit': [{ type: Input },],
260 'dateDisabled': [{ type: Input },],
261 'initDate': [{ type: Input },],
262 'selectionDone': [{ type: Output },],
263 'update': [{ type: Output },],
264 'activeDateChange': [{ type: Output },],
265 'activeDate': [{ type: Input },],
266 };
267 return DatePickerInnerComponent;
268}());
269//# sourceMappingURL=datepicker-inner.component.js.map
\No newline at end of file