RowRenderer.js
3.38 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
/* A utility class for rendering <tr> rows.
----------------------------------------------------------------------------------------------------------------------*/
// It leverages methods of the subclass and the View to determine custom rendering behavior for each row "type"
// (such as highlight rows, day rows, helper rows, etc).
var RowRenderer = Class.extend({
view: null, // a View object
isRTL: null, // shortcut to the view's isRTL option
cellHtml: '<td/>', // plain default HTML used for a cell when no other is available
constructor: function(view) {
this.view = view;
this.isRTL = view.opt('isRTL');
},
// Renders the HTML for a row, leveraging custom cell-HTML-renderers based on the `rowType`.
// Also applies the "intro" and "outro" cells, which are specified by the subclass and views.
// `row` is an optional row number.
rowHtml: function(rowType, row) {
var renderCell = this.getHtmlRenderer('cell', rowType);
var rowCellHtml = '';
var col;
var cell;
row = row || 0;
for (col = 0; col < this.colCnt; col++) {
cell = this.getCell(row, col);
rowCellHtml += renderCell(cell);
}
rowCellHtml = this.bookendCells(rowCellHtml, rowType, row); // apply intro and outro
return '<tr>' + rowCellHtml + '</tr>';
},
// Applies the "intro" and "outro" HTML to the given cells.
// Intro means the leftmost cell when the calendar is LTR and the rightmost cell when RTL. Vice-versa for outro.
// `cells` can be an HTML string of <td>'s or a jQuery <tr> element
// `row` is an optional row number.
bookendCells: function(cells, rowType, row) {
var intro = this.getHtmlRenderer('intro', rowType)(row || 0);
var outro = this.getHtmlRenderer('outro', rowType)(row || 0);
var prependHtml = this.isRTL ? outro : intro;
var appendHtml = this.isRTL ? intro : outro;
if (typeof cells === 'string') {
return prependHtml + cells + appendHtml;
}
else { // a jQuery <tr> element
return cells.prepend(prependHtml).append(appendHtml);
}
},
// Returns an HTML-rendering function given a specific `rendererName` (like cell, intro, or outro) and a specific
// `rowType` (like day, eventSkeleton, helperSkeleton), which is optional.
// If a renderer for the specific rowType doesn't exist, it will fall back to a generic renderer.
// We will query the View object first for any custom rendering functions, then the methods of the subclass.
getHtmlRenderer: function(rendererName, rowType) {
var view = this.view;
var generalName; // like "cellHtml"
var specificName; // like "dayCellHtml". based on rowType
var provider; // either the View or the RowRenderer subclass, whichever provided the method
var renderer;
generalName = rendererName + 'Html';
if (rowType) {
specificName = rowType + capitaliseFirstLetter(rendererName) + 'Html';
}
if (specificName && (renderer = view[specificName])) {
provider = view;
}
else if (specificName && (renderer = this[specificName])) {
provider = this;
}
else if ((renderer = view[generalName])) {
provider = view;
}
else if ((renderer = this[generalName])) {
provider = this;
}
if (typeof renderer === 'function') {
return function() {
return renderer.apply(provider, arguments) || ''; // use correct `this` and always return a string
};
}
// the rendered can be a plain string as well. if not specified, always an empty string.
return function() {
return renderer || '';
};
}
});