-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkpicalendar.js
287 lines (246 loc) · 10.5 KB
/
kpicalendar.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Legend calendar builds a simple css grid based month view suitable for adding color coded accents to show
items that belong to a specific date
*/
class kpiCalendar {
constructor(selector, month, year) {
// activeDate is the first day of the month of the current calendar view.
this.activeDate = new Date(this.#validYear(year), this.#validMonth(month), 1);
this.elem = jQuery(selector || '.kpic-calendar, .kpic-calendar-fill').first();
this.type = (this.elem.hasClass('kpic-calendar')) ? 'standard' : 'fill';
// if true then do not display week 7 if not in current month, false (default) show only last week of view
this.truncateView = typeof this.elem.data("truncateview") === 'undefined' ? true : this.elem.data("truncateview") == "1";
if (this.elem.data('monthdisplay')) {
var fmt = this.elem.data('monthformat') || 'long';
this.setMonthDisplay(this.elem.data('monthdisplay'), fmt);
}
this.legendSelector = this.elem.data('legend');
this.kpiItems = [];
this.#buildCalendarElement();
}
// Public Methods
setLegendSelector(s) {
s = s || '';
if (s === '' && this.legendSelector) {
// if s is blank, and there was a previous legendSelector, hide it.
jQuery(this.legendSelector).hide();
} else {
this.#buildLegend();
}
}
setMonthDisplay(selector, format, callback) {
this.monthDisplay = {
elem: jQuery(selector),
format: format || 'long',
callback: callback || null
}
this.#updateMonthName();
}
setMonth(month, year, callback) {
var newDate = new Date(this.#validYear(year), this.#validMonth(month), 1);
if (newDate.toDateString() !== this.activeDate.toDateString()) {
this.activeDate = new Date(newDate);
this.#buildCalendarElement();
}
if (typeof callback === "function") {
callback(this.activeDate)
};
}
addMonth(v, callback) {
if (!parseInt(v)) {
return;
}
var nd = new Date(this.activeDate);
nd = new Date(nd.setMonth(nd.getMonth() + v));
this.setMonth(nd.getMonth(), nd.getFullYear(), callback);
}
addItem(key, label, cssClass, title, text, color, uid) {
if (typeof key === 'object') {
var kpiItem = {
key: key.key,
label: key.label || '',
cssClass: key.cssClass || 'kpic-data-item-default',
title: key.title || '',
text: key.text || '',
color: key.color || '',
uid: key.uid || this.#uuid()
}
} else {
var kpiItem = {
key: key,
label: label || '',
cssClass: cssClass || 'kpic-data-item-default',
title: title || '',
text: text || '',
color: color || '',
uid: uid || this.#uuid()
}
}
this.kpiItems.push(kpiItem);
this.#addKpiDomItem(kpiItem);
}
resetItems() {
this.kpiItems = [];
this.elem.find('.kpic-data-items').empty();
this.elem.find('.kpic-data-items-count').text('');
}
getISODate() {
return this.activeDate.toISOString().slice(0, 10);
}
#buildLegend(selector) {
function getLegend(item) {
//if (jQuery(`.kpic-data-item[data-uid="${item.uid}"]`).length > 0) {
return { label: item.label, color: item.color, cssClass: item.cssClass, uid: item.uid }
}
selector = selector || this.legendSelector;
if (selector && jQuery(selector).length > 0) {
let legendArray = this.kpiItems.map(getLegend);
let answer = [];
// get only unique data elements for elements on the page
legendArray.forEach(item => {
if (jQuery(`.kpic-data-item[data-uid="${item.uid}"]`).length > 0) {
if (!answer.some(y => y.label === item.label)) {
answer.push({
label: item.label,
color: item.color,
cssClass: item.cssClass
})
}
}
})
jQuery(selector).addClass('kpic-legend').show().empty().append('<div class="kpic-legend-items"></div>');
answer.forEach(item => {
var style = (item.color) ? `style="color:${item.color};"` : ""
var elem = `<div class="kpic-legend-item">
<span ${style} class="k-icon k-i-circle ${item.cssClass}"></span>
${item.label}
</div>`;
jQuery(selector).find('.kpic-legend-items').append(elem);
});
}
}
// Private Methods
#updateMonthName() {
var month;
if (this.monthDisplay) {
const date = new Date(this.activeDate);
if (this.monthDisplay.format === 'long' || this.monthDisplay.format === 'short') {
month = date.toLocaleString('default', { month: this.monthDisplay.format });
} else if (typeof kendo === 'object') {
month = kendo.toString(date, this.monthDisplay.format);
} else {
month = date.toLocaleString('default', { month: 'long'});
}
this.monthDisplay.elem.text(month);
}
if (typeof this.monthDisplay?.callback === "function") {
this.monthDisplay.callback(this.activeDate);
}
}
#validMonth(month) {
var d = new Date();
if (typeof month === 'undefined' || parseInt(month) < 0 || parseInt(month) > 11) {
return d.getMonth();
}
return month
}
#validYear(year) {
var d = new Date();
if (typeof year === 'undefined' || parseInt(year) < 2000) {
return d.getFullYear();
}
return year
}
#addKpiDomItem(item) {
var cssClass = "kpic-data-item";
if (item.cssClass) {
cssClass += ` ${item.cssClass}`;
} else {
cssClass += 'kpic-data-item-default';
}
var cssStyle = '';
if (item.color) {
cssStyle = `background-color:${item.color};border-color:${item.color};`
}
var selector = `.kpic-box[data-date="${item.key}"]`;
var itemSelector = `.kpic-data-item[data-uid="${item.uid}"]`;
var $box = this.elem.find(selector);
if ($box.length > 0 && jQuery(itemSelector).length === 0) {
if (this.type === 'fill') {
// add only the last data item for a calendar of type fill.
$box.find('.kpic-data-items .kpic-data-item').remove()
}
var e = `<div data-uid="${item.uid}" class="${cssClass}" title="${item.title}" style="${cssStyle}">
<div class="kpic-data-item-inner">${item.text}</div>
</div>`;
$box.addClass('kpic-has-items').find('.kpic-data-items').append(e);
// calculate whether there are kpi items that are below the bottom (hidden) of the date box.
var boxBottom = $box.offset().top + $box.innerHeight();
var lastItem = $box.find('.kpic-data-item:last-child');
var bottom = lastItem.offset().top + lastItem.innerHeight();
if (bottom > boxBottom) {
// store this in DOM, so that we no exactly how many are *not* shown, as opposed to how many there are.
var count = parseInt($box.find('.kpic-data-items-count').text()) || 0;
$box.find('.kpic-data-items-count').show().text(`+${count + 1}`);
}
this.#buildLegend();
}
}
#buildCalendarElement() {
var today = new Date();
var thisMonth = this.activeDate.getMonth();
// build kpi calendar header row
var headerId = this.#uuid();
this.elem.empty().append(`<div data-uuid="${headerId}" class="kpic-header-row"></div>`);
var $lgcHeader = jQuery(`[data-uuid="${headerId}"]`);
for (var i = 0; i < 7; i++) {
var div = `<div class="kpic-box"><div class="kpic-dow-text">${"SMTWTFS"[i]}</div></div>`;
$lgcHeader.append(div);
}
// month view starts from the first sunday on or before the active date
var startDate = new Date(this.activeDate);
while (startDate.getDay() > 0) {
startDate.setDate(startDate.getDate() - 1);
}
var currentDate = new Date(startDate);
// build a grid for 5 or 6 weeks; no less or more
var gridId = this.#uuid();
this.elem.append(`<div data-uuid="${gridId}" class="kpic-grid"></div>`);
var $grid = jQuery(`[data-uuid="${gridId}"]`);
for (var i = 0; i < 42; i++) {
var dateClass = "kpic-box";
if (currentDate.getMonth() !== thisMonth) {
dateClass += " kpic-date-disabled";
}
if (currentDate.toDateString() === today.toDateString()) {
dateClass += " kpic-date-current";
}
var dateKey = currentDate.toISOString().slice(0, 10);
var dateId = this.#uuid();
var d = `<div class="${dateClass}" data-date="${dateKey}" data-uuid="[${dateId}">
<div class="kpic-date-text">${currentDate.getDate()}</div>
<div class="kpic-data-items"></div>
<div class="kpic-data-items-count"></div>
</div>`;
$grid.append(d);
currentDate.setDate(currentDate.getDate() + 1);
// break on the 6th week if that sunday is not in the current month.
if (this.truncateView && i == 34 && currentDate.getMonth() !== thisMonth) {
break;
}
}
// if there are elements in the kpi, re-add them to the new calendar
// lovely use of arrow function here, since it doesn't jerk around with 'this'
this.kpiItems.forEach(item => {
this.#addKpiDomItem(item);
});
this.#buildLegend();
this.#updateMonthName();
}
#uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
}