DayGrid.js
13.6 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
/* A component that renders a grid of whole-days that runs horizontally. There can be multiple rows, one per week.
----------------------------------------------------------------------------------------------------------------------*/
var DayGrid = Grid.extend({
numbersVisible: false, // should render a row for day/week numbers? set by outside view. TODO: make internal
bottomCoordPadding: 0, // hack for extending the hit area for the last row of the coordinate grid
breakOnWeeks: null, // should create a new row for each week? set by outside view
cellDates: null, // flat chronological array of each cell's dates
dayToCellOffsets: null, // maps days offsets from grid's start date, to cell offsets
rowEls: null, // set of fake row elements
dayEls: null, // set of whole-day elements comprising the row's background
helperEls: null, // set of cell skeleton elements for rendering the mock event "helper"
constructor: function() {
Grid.apply(this, arguments);
this.cellDuration = moment.duration(1, 'day'); // for Grid system
},
// Renders the rows and columns into the component's `this.el`, which should already be assigned.
// isRigid determins whether the individual rows should ignore the contents and be a constant height.
// Relies on the view's colCnt and rowCnt. In the future, this component should probably be self-sufficient.
renderDates: function(isRigid) {
var view = this.view;
var rowCnt = this.rowCnt;
var colCnt = this.colCnt;
var cellCnt = rowCnt * colCnt;
var html = '';
var row;
var i, cell;
for (row = 0; row < rowCnt; row++) {
html += this.dayRowHtml(row, isRigid);
}
this.el.html(html);
this.rowEls = this.el.find('.fc-row');
this.dayEls = this.el.find('.fc-day');
// trigger dayRender with each cell's element
for (i = 0; i < cellCnt; i++) {
cell = this.getCell(i);
view.trigger('dayRender', null, cell.start, this.dayEls.eq(i));
}
},
destroyDates: function() {
this.destroySegPopover();
},
renderBusinessHours: function() {
var events = this.view.calendar.getBusinessHoursEvents(true); // wholeDay=true
var segs = this.eventsToSegs(events);
this.renderFill('businessHours', segs, 'bgevent');
},
// Generates the HTML for a single row. `row` is the row number.
dayRowHtml: function(row, isRigid) {
var view = this.view;
var classes = [ 'fc-row', 'fc-week', view.widgetContentClass ];
if (isRigid) {
classes.push('fc-rigid');
}
return '' +
'<div class="' + classes.join(' ') + '">' +
'<div class="fc-bg">' +
'<table>' +
this.rowHtml('day', row) + // leverages RowRenderer. calls dayCellHtml()
'</table>' +
'</div>' +
'<div class="fc-content-skeleton">' +
'<table>' +
(this.numbersVisible ?
'<thead>' +
this.rowHtml('number', row) + // leverages RowRenderer. View will define render method
'</thead>' :
''
) +
'</table>' +
'</div>' +
'</div>';
},
// Renders the HTML for a whole-day cell. Will eventually end up in the day-row's background.
// We go through a 'day' row type instead of just doing a 'bg' row type so that the View can do custom rendering
// specifically for whole-day rows, whereas a 'bg' might also be used for other purposes (TimeGrid bg for example).
dayCellHtml: function(cell) {
return this.bgCellHtml(cell);
},
/* Options
------------------------------------------------------------------------------------------------------------------*/
// Computes a default column header formatting string if `colFormat` is not explicitly defined
computeColHeadFormat: function() {
if (this.rowCnt > 1) { // more than one week row. day numbers will be in each cell
return 'ddd'; // "Sat"
}
else 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('extraSmallTimeFormat'); // like "6p" or "6:30p"
},
// Computes a default `displayEventEnd` value if one is not expliclty defined
computeDisplayEventEnd: function() {
return this.colCnt == 1; // we'll likely have space if there's only one day
},
/* Cell System
------------------------------------------------------------------------------------------------------------------*/
// Initializes row/col information
updateCells: function() {
var cellDates;
var firstDay;
var rowCnt;
var colCnt;
this.updateCellDates(); // populates cellDates and dayToCellOffsets
cellDates = this.cellDates;
if (this.breakOnWeeks) {
// count columns until the day-of-week repeats
firstDay = cellDates[0].day();
for (colCnt = 1; colCnt < cellDates.length; colCnt++) {
if (cellDates[colCnt].day() == firstDay) {
break;
}
}
rowCnt = Math.ceil(cellDates.length / colCnt);
}
else {
rowCnt = 1;
colCnt = cellDates.length;
}
this.rowCnt = rowCnt;
this.colCnt = colCnt;
},
// Populates cellDates and dayToCellOffsets
updateCellDates: function() {
var view = this.view;
var date = this.start.clone();
var dates = [];
var offset = -1;
var offsets = [];
while (date.isBefore(this.end)) { // loop each day from start to end
if (view.isHiddenDay(date)) {
offsets.push(offset + 0.5); // mark that it's between offsets
}
else {
offset++;
offsets.push(offset);
dates.push(date.clone());
}
date.add(1, 'days');
}
this.cellDates = dates;
this.dayToCellOffsets = offsets;
},
// Given a cell object, generates its start date. Returns a reference-free copy.
computeCellDate: function(cell) {
var colCnt = this.colCnt;
var index = cell.row * colCnt + (this.isRTL ? colCnt - cell.col - 1 : cell.col);
return this.cellDates[index].clone();
},
// Retrieves the element representing the given row
getRowEl: function(row) {
return this.rowEls.eq(row);
},
// Retrieves the element representing the given column
getColEl: function(col) {
return this.dayEls.eq(col);
},
// Gets the whole-day element associated with the cell
getCellDayEl: function(cell) {
return this.dayEls.eq(cell.row * this.colCnt + cell.col);
},
// Overrides Grid's method for when row coordinates are computed
computeRowCoords: function() {
var rowCoords = Grid.prototype.computeRowCoords.call(this); // call the super-method
// hack for extending last row (used by AgendaView)
rowCoords[rowCoords.length - 1].bottom += this.bottomCoordPadding;
return rowCoords;
},
/* Dates
------------------------------------------------------------------------------------------------------------------*/
// Slices up a date range by row into an array of segments
rangeToSegs: function(range) {
var isRTL = this.isRTL;
var rowCnt = this.rowCnt;
var colCnt = this.colCnt;
var segs = [];
var first, last; // inclusive cell-offset range for given range
var row;
var rowFirst, rowLast; // inclusive cell-offset range for current row
var isStart, isEnd;
var segFirst, segLast; // inclusive cell-offset range for segment
var seg;
range = this.view.computeDayRange(range); // make whole-day range, considering nextDayThreshold
first = this.dateToCellOffset(range.start);
last = this.dateToCellOffset(range.end.subtract(1, 'days')); // offset of inclusive end date
for (row = 0; row < rowCnt; row++) {
rowFirst = row * colCnt;
rowLast = rowFirst + colCnt - 1;
// intersect segment's offset range with the row's
segFirst = Math.max(rowFirst, first);
segLast = Math.min(rowLast, last);
// deal with in-between indices
segFirst = Math.ceil(segFirst); // in-between starts round to next cell
segLast = Math.floor(segLast); // in-between ends round to prev cell
if (segFirst <= segLast) { // was there any intersection with the current row?
// must be matching integers to be the segment's start/end
isStart = segFirst === first;
isEnd = segLast === last;
// translate offsets to be relative to start-of-row
segFirst -= rowFirst;
segLast -= rowFirst;
seg = { row: row, isStart: isStart, isEnd: isEnd };
if (isRTL) {
seg.leftCol = colCnt - segLast - 1;
seg.rightCol = colCnt - segFirst - 1;
}
else {
seg.leftCol = segFirst;
seg.rightCol = segLast;
}
segs.push(seg);
}
}
return segs;
},
// Given a date, returns its chronolocial cell-offset from the first cell of the grid.
// If the date lies between cells (because of hiddenDays), returns a floating-point value between offsets.
// If before the first offset, returns a negative number.
// If after the last offset, returns an offset past the last cell offset.
// Only works for *start* dates of cells. Will not work for exclusive end dates for cells.
dateToCellOffset: function(date) {
var offsets = this.dayToCellOffsets;
var day = date.diff(this.start, 'days');
if (day < 0) {
return offsets[0] - 1;
}
else if (day >= offsets.length) {
return offsets[offsets.length - 1] + 1;
}
else {
return offsets[day];
}
},
/* Event Drag Visualization
------------------------------------------------------------------------------------------------------------------*/
// TODO: move to DayGrid.event, similar to what we did with Grid's drag methods
// Renders a visual indication of an event or external element being dragged.
// The dropLocation's end can be null. seg can be null. See Grid::renderDrag for more info.
renderDrag: function(dropLocation, seg) {
// always render a highlight underneath
this.renderHighlight(
this.view.calendar.ensureVisibleEventRange(dropLocation) // needs to be a proper range
);
// if a segment from the same calendar but another component is being dragged, render a helper event
if (seg && !seg.el.closest(this.el).length) {
this.renderRangeHelper(dropLocation, seg);
this.applyDragOpacity(this.helperEls);
return true; // a helper has been rendered
}
},
// Unrenders any visual indication of a hovering event
destroyDrag: function() {
this.destroyHighlight();
this.destroyHelper();
},
/* Event Resize Visualization
------------------------------------------------------------------------------------------------------------------*/
// Renders a visual indication of an event being resized
renderEventResize: function(range, seg) {
this.renderHighlight(range);
this.renderRangeHelper(range, seg);
},
// Unrenders a visual indication of an event being resized
destroyEventResize: function() {
this.destroyHighlight();
this.destroyHelper();
},
/* Event Helper
------------------------------------------------------------------------------------------------------------------*/
// Renders a mock "helper" event. `sourceSeg` is the associated internal segment object. It can be null.
renderHelper: function(event, sourceSeg) {
var helperNodes = [];
var segs = this.eventsToSegs([ event ]);
var rowStructs;
segs = this.renderFgSegEls(segs); // assigns each seg's el and returns a subset of segs that were rendered
rowStructs = this.renderSegRows(segs);
// inject each new event skeleton into each associated row
this.rowEls.each(function(row, rowNode) {
var rowEl = $(rowNode); // the .fc-row
var skeletonEl = $('<div class="fc-helper-skeleton"><table/></div>'); // will be absolutely positioned
var skeletonTop;
// If there is an original segment, match the top position. Otherwise, put it at the row's top level
if (sourceSeg && sourceSeg.row === row) {
skeletonTop = sourceSeg.el.position().top;
}
else {
skeletonTop = rowEl.find('.fc-content-skeleton tbody').position().top;
}
skeletonEl.css('top', skeletonTop)
.find('table')
.append(rowStructs[row].tbodyEl);
rowEl.append(skeletonEl);
helperNodes.push(skeletonEl[0]);
});
this.helperEls = $(helperNodes); // array -> jQuery set
},
// Unrenders any visual indication of a mock helper event
destroyHelper: function() {
if (this.helperEls) {
this.helperEls.remove();
this.helperEls = null;
}
},
/* Fill System (highlight, background events, business hours)
------------------------------------------------------------------------------------------------------------------*/
fillSegTag: 'td', // override the default tag name
// Renders a set of rectangles over the given segments of days.
// Only returns segments that successfully rendered.
renderFill: function(type, segs, className) {
var nodes = [];
var i, seg;
var skeletonEl;
segs = this.renderFillSegEls(type, segs); // assignes `.el` to each seg. returns successfully rendered segs
for (i = 0; i < segs.length; i++) {
seg = segs[i];
skeletonEl = this.renderFillRow(type, seg, className);
this.rowEls.eq(seg.row).append(skeletonEl);
nodes.push(skeletonEl[0]);
}
this.elsByFill[type] = $(nodes);
return segs;
},
// Generates the HTML needed for one row of a fill. Requires the seg's el to be rendered.
renderFillRow: function(type, seg, className) {
var colCnt = this.colCnt;
var startCol = seg.leftCol;
var endCol = seg.rightCol + 1;
var skeletonEl;
var trEl;
className = className || type.toLowerCase();
skeletonEl = $(
'<div class="fc-' + className + '-skeleton">' +
'<table><tr/></table>' +
'</div>'
);
trEl = skeletonEl.find('tr');
if (startCol > 0) {
trEl.append('<td colspan="' + startCol + '"/>');
}
trEl.append(
seg.el.attr('colspan', endCol - startCol)
);
if (endCol < colCnt) {
trEl.append('<td colspan="' + (colCnt - endCol) + '"/>');
}
this.bookendCells(trEl, type);
return skeletonEl;
}
});