TimeGrid.js
14.2 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* A component that renders one or more columns of vertical time slots
----------------------------------------------------------------------------------------------------------------------*/
var TimeGrid = Grid.extend({
slotDuration: null, // duration of a "slot", a distinct time segment on given day, visualized by lines
snapDuration: null, // granularity of time for dragging and selecting
minTime: null, // Duration object that denotes the first visible time of any given day
maxTime: null, // Duration object that denotes the exclusive visible end time of any given day
axisFormat: null, // formatting string for times running along vertical axis
dayEls: null, // cells elements in the day-row background
slatEls: null, // elements running horizontally across all columns
slatTops: null, // an array of top positions, relative to the container. last item holds bottom of last slot
helperEl: null, // cell skeleton element for rendering the mock event "helper"
businessHourSegs: null,
constructor: function() {
Grid.apply(this, arguments); // call the super-constructor
this.processOptions();
},
// Renders the time grid into `this.el`, which should already be assigned.
// Relies on the view's colCnt. In the future, this component should probably be self-sufficient.
renderDates: function() {
this.el.html(this.renderHtml());
this.dayEls = this.el.find('.fc-day');
this.slatEls = this.el.find('.fc-slats tr');
},
renderBusinessHours: function() {
var events = this.view.calendar.getBusinessHoursEvents();
this.businessHourSegs = this.renderFill('businessHours', this.eventsToSegs(events), 'bgevent');
},
// Renders the basic HTML skeleton for the grid
renderHtml: function() {
return '' +
'<div class="fc-bg">' +
'<table>' +
this.rowHtml('slotBg') + // leverages RowRenderer, which will call slotBgCellHtml
'</table>' +
'</div>' +
'<div class="fc-slats">' +
'<table>' +
this.slatRowHtml() +
'</table>' +
'</div>';
},
// Renders the HTML for a vertical background cell behind the slots.
// This method is distinct from 'bg' because we wanted a new `rowType` so the View could customize the rendering.
slotBgCellHtml: function(cell) {
return this.bgCellHtml(cell);
},
// Generates the HTML for the horizontal "slats" that run width-wise. Has a time axis on a side. Depends on RTL.
slatRowHtml: function() {
var view = this.view;
var isRTL = this.isRTL;
var html = '';
var slotNormal = this.slotDuration.asMinutes() % 15 === 0;
var slotTime = moment.duration(+this.minTime); // wish there was .clone() for durations
var slotDate; // will be on the view's first day, but we only care about its time
var minutes;
var axisHtml;
// Calculate the time for each slot
while (slotTime < this.maxTime) {
slotDate = this.start.clone().time(slotTime); // will be in UTC but that's good. to avoid DST issues
minutes = slotDate.minutes();
axisHtml =
'<td class="fc-axis fc-time ' + view.widgetContentClass + '" ' + view.axisStyleAttr() + '>' +
((!slotNormal || !minutes) ? // if irregular slot duration, or on the hour, then display the time
'<span>' + // for matchCellWidths
htmlEscape(slotDate.format(this.axisFormat)) +
'</span>' :
''
) +
'</td>';
html +=
'<tr ' + (!minutes ? '' : 'class="fc-minor"') + '>' +
(!isRTL ? axisHtml : '') +
'<td class="' + view.widgetContentClass + '"/>' +
(isRTL ? axisHtml : '') +
"</tr>";
slotTime.add(this.slotDuration);
}
return html;
},
/* Options
------------------------------------------------------------------------------------------------------------------*/
// Parses various options into properties of this object
processOptions: function() {
var view = this.view;
var slotDuration = view.opt('slotDuration');
var snapDuration = view.opt('snapDuration');
slotDuration = moment.duration(slotDuration);
snapDuration = snapDuration ? moment.duration(snapDuration) : slotDuration;
this.slotDuration = slotDuration;
this.snapDuration = snapDuration;
this.cellDuration = snapDuration; // for Grid system
this.minTime = moment.duration(view.opt('minTime'));
this.maxTime = moment.duration(view.opt('maxTime'));
this.axisFormat = view.opt('axisFormat') || view.opt('smallTimeFormat');
},
// Computes a default column header formatting string if `colFormat` is not explicitly defined
computeColHeadFormat: function() {
if (this.colCnt > 1) { // multiple days, so full single date string WON'T be in title text
return this.view.opt('dayOfMonthFormat'); // "Sat 12/10"
}
else { // single day, so full single date string will probably be in title text
return 'dddd'; // "Saturday"
}
},
// Computes a default event time formatting string if `timeFormat` is not explicitly defined
computeEventTimeFormat: function() {
return this.view.opt('noMeridiemTimeFormat'); // like "6:30" (no AM/PM)
},
// Computes a default `displayEventEnd` value if one is not expliclty defined
computeDisplayEventEnd: function() {
return true;
},
/* Cell System
------------------------------------------------------------------------------------------------------------------*/
// Initializes row/col information
updateCells: function() {
var view = this.view;
var colData = [];
var date;
date = this.start.clone();
while (date.isBefore(this.end)) {
colData.push({
day: date.clone()
});
date.add(1, 'day');
date = view.skipHiddenDays(date);
}
if (this.isRTL) {
colData.reverse();
}
this.colData = colData;
this.colCnt = colData.length;
this.rowCnt = Math.ceil((this.maxTime - this.minTime) / this.snapDuration); // # of vertical snaps
},
// Given a cell object, generates its start date. Returns a reference-free copy.
computeCellDate: function(cell) {
var time = this.computeSnapTime(cell.row);
return this.view.calendar.rezoneDate(cell.day).time(time);
},
// Retrieves the element representing the given column
getColEl: function(col) {
return this.dayEls.eq(col);
},
/* Dates
------------------------------------------------------------------------------------------------------------------*/
// Given a row number of the grid, representing a "snap", returns a time (Duration) from its start-of-day
computeSnapTime: function(row) {
return moment.duration(this.minTime + this.snapDuration * row);
},
// Slices up a date range by column into an array of segments
rangeToSegs: function(range) {
var colCnt = this.colCnt;
var segs = [];
var seg;
var col;
var colDate;
var colRange;
// normalize :(
range = {
start: range.start.clone().stripZone(),
end: range.end.clone().stripZone()
};
for (col = 0; col < colCnt; col++) {
colDate = this.colData[col].day; // will be ambig time/timezone
colRange = {
start: colDate.clone().time(this.minTime),
end: colDate.clone().time(this.maxTime)
};
seg = intersectionToSeg(range, colRange); // both will be ambig timezone
if (seg) {
seg.col = col;
segs.push(seg);
}
}
return segs;
},
/* Coordinates
------------------------------------------------------------------------------------------------------------------*/
updateSize: function(isResize) { // NOT a standard Grid method
this.computeSlatTops();
if (isResize) {
this.updateSegVerticals();
}
},
// Computes the top/bottom coordinates of each "snap" rows
computeRowCoords: function() {
var originTop = this.el.offset().top;
var items = [];
var i;
var item;
for (i = 0; i < this.rowCnt; i++) {
item = {
top: originTop + this.computeTimeTop(this.computeSnapTime(i))
};
if (i > 0) {
items[i - 1].bottom = item.top;
}
items.push(item);
}
item.bottom = item.top + this.computeTimeTop(this.computeSnapTime(i));
return items;
},
// Computes the top coordinate, relative to the bounds of the grid, of the given date.
// A `startOfDayDate` must be given for avoiding ambiguity over how to treat midnight.
computeDateTop: function(date, startOfDayDate) {
return this.computeTimeTop(
moment.duration(
date.clone().stripZone() - startOfDayDate.clone().stripTime()
)
);
},
// Computes the top coordinate, relative to the bounds of the grid, of the given time (a Duration).
computeTimeTop: function(time) {
var slatCoverage = (time - this.minTime) / this.slotDuration; // floating-point value of # of slots covered
var slatIndex;
var slatRemainder;
var slatTop;
var slatBottom;
// constrain. because minTime/maxTime might be customized
slatCoverage = Math.max(0, slatCoverage);
slatCoverage = Math.min(this.slatEls.length, slatCoverage);
slatIndex = Math.floor(slatCoverage); // an integer index of the furthest whole slot
slatRemainder = slatCoverage - slatIndex;
slatTop = this.slatTops[slatIndex]; // the top position of the furthest whole slot
if (slatRemainder) { // time spans part-way into the slot
slatBottom = this.slatTops[slatIndex + 1];
return slatTop + (slatBottom - slatTop) * slatRemainder; // part-way between slots
}
else {
return slatTop;
}
},
// Queries each `slatEl` for its position relative to the grid's container and stores it in `slatTops`.
// Includes the the bottom of the last slat as the last item in the array.
computeSlatTops: function() {
var tops = [];
var top;
this.slatEls.each(function(i, node) {
top = $(node).position().top;
tops.push(top);
});
tops.push(top + this.slatEls.last().outerHeight()); // bottom of the last slat
this.slatTops = tops;
},
/* Event Drag Visualization
------------------------------------------------------------------------------------------------------------------*/
// Renders a visual indication of an event being dragged over the specified date(s).
// dropLocation's end might be null, as well as `seg`. See Grid::renderDrag for more info.
// A returned value of `true` signals that a mock "helper" event has been rendered.
renderDrag: function(dropLocation, seg) {
if (seg) { // if there is event information for this drag, render a helper event
this.renderRangeHelper(dropLocation, seg);
this.applyDragOpacity(this.helperEl);
return true; // signal that a helper has been rendered
}
else {
// otherwise, just render a highlight
this.renderHighlight(
this.view.calendar.ensureVisibleEventRange(dropLocation) // needs to be a proper range
);
}
},
// Unrenders any visual indication of an event being dragged
destroyDrag: function() {
this.destroyHelper();
this.destroyHighlight();
},
/* Event Resize Visualization
------------------------------------------------------------------------------------------------------------------*/
// Renders a visual indication of an event being resized
renderEventResize: function(range, seg) {
this.renderRangeHelper(range, seg);
},
// Unrenders any visual indication of an event being resized
destroyEventResize: function() {
this.destroyHelper();
},
/* Event Helper
------------------------------------------------------------------------------------------------------------------*/
// Renders a mock "helper" event. `sourceSeg` is the original segment object and might be null (an external drag)
renderHelper: function(event, sourceSeg) {
var segs = this.eventsToSegs([ event ]);
var tableEl;
var i, seg;
var sourceEl;
segs = this.renderFgSegEls(segs); // assigns each seg's el and returns a subset of segs that were rendered
tableEl = this.renderSegTable(segs);
// Try to make the segment that is in the same row as sourceSeg look the same
for (i = 0; i < segs.length; i++) {
seg = segs[i];
if (sourceSeg && sourceSeg.col === seg.col) {
sourceEl = sourceSeg.el;
seg.el.css({
left: sourceEl.css('left'),
right: sourceEl.css('right'),
'margin-left': sourceEl.css('margin-left'),
'margin-right': sourceEl.css('margin-right')
});
}
}
this.helperEl = $('<div class="fc-helper-skeleton"/>')
.append(tableEl)
.appendTo(this.el);
},
// Unrenders any mock helper event
destroyHelper: function() {
if (this.helperEl) {
this.helperEl.remove();
this.helperEl = null;
}
},
/* Selection
------------------------------------------------------------------------------------------------------------------*/
// Renders a visual indication of a selection. Overrides the default, which was to simply render a highlight.
renderSelection: function(range) {
if (this.view.opt('selectHelper')) { // this setting signals that a mock helper event should be rendered
this.renderRangeHelper(range);
}
else {
this.renderHighlight(range);
}
},
// Unrenders any visual indication of a selection
destroySelection: function() {
this.destroyHelper();
this.destroyHighlight();
},
/* Fill System (highlight, background events, business hours)
------------------------------------------------------------------------------------------------------------------*/
// Renders a set of rectangles over the given time segments.
// Only returns segments that successfully rendered.
renderFill: function(type, segs, className) {
var segCols;
var skeletonEl;
var trEl;
var col, colSegs;
var tdEl;
var containerEl;
var dayDate;
var i, seg;
if (segs.length) {
segs = this.renderFillSegEls(type, segs); // assignes `.el` to each seg. returns successfully rendered segs
segCols = this.groupSegCols(segs); // group into sub-arrays, and assigns 'col' to each seg
className = className || type.toLowerCase();
skeletonEl = $(
'<div class="fc-' + className + '-skeleton">' +
'<table><tr/></table>' +
'</div>'
);
trEl = skeletonEl.find('tr');
for (col = 0; col < segCols.length; col++) {
colSegs = segCols[col];
tdEl = $('<td/>').appendTo(trEl);
if (colSegs.length) {
containerEl = $('<div class="fc-' + className + '-container"/>').appendTo(tdEl);
dayDate = this.colData[col].day;
for (i = 0; i < colSegs.length; i++) {
seg = colSegs[i];
containerEl.append(
seg.el.css({
top: this.computeDateTop(seg.start, dayDate),
bottom: -this.computeDateTop(seg.end, dayDate) // the y position of the bottom edge
})
);
}
}
}
this.bookendCells(trEl, type);
this.el.append(skeletonEl);
this.elsByFill[type] = skeletonEl;
}
return segs;
}
});