This repository was archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkendo-multi-calendar.ts
More file actions
193 lines (148 loc) · 5 KB
/
Copy pathkendo-multi-calendar.ts
File metadata and controls
193 lines (148 loc) · 5 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
declare namespace kendo {
namespace ui {
interface Calendar {
_todayClick(e: CalendarEvent): void;
}
}
}
namespace kendoExt {
export type CalendarDepth = 'month' | 'year' | 'decade' | 'century';
export interface MultiCalendarOptions extends kendo.ui.CalendarOptions {
values?: Date[];
maxSelectedItems?: number | null;
cleanSelectedItemsOnTodayClick?: boolean;
}
export class MultiCalendar extends kendo.ui.Calendar {
public static fn: MultiCalendar;
private static _views: { [key: string]: number } = {
month: 0,
year: 1,
decade: 2,
century: 3
};
public options: MultiCalendarOptions;
private _values: Date[];
constructor(element: Element | JQuery | string, options?: MultiCalendarOptions) {
super(element as Element, options);
this._values = (options && options.values) || [];
(this as any).navigate();
}
private static isSameDate(first: Date, second: Date) {
return (
first &&
second &&
first.getFullYear() === second.getFullYear() &&
first.getMonth() === second.getMonth() &&
first.getDate() === second.getDate()
);
}
public values(newValues?: Date[]): Date[] {
if (newValues !== undefined && this.canSelectItems(newValues.length)) {
const valuesToClear = this._values.filter(value => {
return newValues.every(newValue => !MultiCalendar.isSameDate(value, newValue));
});
let i = null;
this._values = newValues.filter((item, index) => {
for (i = index - 1; i >= 0; i -= 1) {
if (MultiCalendar.isSameDate(item, newValues[i])) {
return false;
}
}
return true;
});
this.updateSelection(this._values, valuesToClear);
}
return this._values;
}
public value(newValue?: Date | string): Date {
const valueIndex: number = (this._values || [])
.reduce(
(acc, val, index) => (MultiCalendar.isSameDate(val, newValue as Date) ? index : acc),
-1
);
const shouldAddNewValue = valueIndex === -1;
const canAddNewValue =
shouldAddNewValue && this._values && this.canSelectItems(this._values.length + 1);
if (!shouldAddNewValue || canAddNewValue) {
super.value(newValue as Date);
}
if (newValue) {
let valuesToClear: Date[] = [];
if (canAddNewValue) {
this._values.push(newValue as Date);
} else if (!shouldAddNewValue) {
valuesToClear = this._values.splice(valueIndex, 1);
}
this.updateSelection(this._values, valuesToClear);
}
return super.value();
}
public navigate(value: Date, view: string) {
super.navigate(value, view);
this.updateSelection(this._values);
}
public navigateDown(value: Date) {
if (!value) {
return;
}
const index: number = (this as any)._index;
if (index === MultiCalendar._views[this.options.depth as string]) {
this.value(value);
this.trigger('change');
return;
}
this.navigate(value, (index - 1) as any);
}
public _todayClick(e: kendo.ui.CalendarEvent) {
this.selectToday();
super._todayClick(e);
}
private canSelectItems(amount: number): boolean {
const max = this.options.maxSelectedItems;
return !max || amount <= max;
}
private cellByDate(value: string): JQuery {
return (this as any)._table.find('td').filter(function () {
return $(this.firstChild).attr((kendo as any).attr('value')) === value;
});
}
private updateSelection(valuesToSelect: Date[], valuesToClear: Date[] = []) {
if ((this as any)._index === MultiCalendar._views[this.options.depth as string]) {
const selected = 'k-state-selected';
const _value = (this as any)._value;
if (_value) {
this.cellByDate(this.view().toDateString(_value)).removeClass(selected);
}
valuesToSelect.forEach(value => {
this.cellByDate(this.view().toDateString(value)).addClass(selected);
});
valuesToClear.forEach(value => {
this.cellByDate(this.view().toDateString(value)).removeClass(selected);
});
}
}
private selectToday = () => {
const today = new Date();
const value = new Date(today.getFullYear(), today.getMonth(), today.getDate());
const values = this.options.cleanSelectedItemsOnTodayClick
? [value]
: [...this._values, value];
this.values(values);
}
}
MultiCalendar.fn = MultiCalendar.prototype;
MultiCalendar.fn.options = {
...kendo.ui.Calendar.fn.options,
...{
name: 'MultiCalendar',
values: [],
maxSelectedItems: null,
cleanSelectedItemsOnTodayClick: true
}
};
kendo.ui.plugin(MultiCalendar);
}
interface JQuery {
kendoMultiCalendar(options?: kendoExt.MultiCalendarOptions): JQuery;
data(key: 'kendoMultiCalendar'): kendoExt.MultiCalendar;
}