1 |
wakaba |
1.4 |
function tableToCanvas (table, parent, idPrefix) { |
2 |
wakaba |
1.1 |
var canvas = document.createElement ('canvas'); |
3 |
wakaba |
1.3 |
parent.appendChild (canvas); |
4 |
wakaba |
1.2 |
if (window.G_vmlCanvasManager) { |
5 |
|
|
canvas = G_vmlCanvasManager.initElement (canvas); |
6 |
|
|
} |
7 |
wakaba |
1.1 |
var c2d = canvas.getContext ('2d'); |
8 |
|
|
|
9 |
|
|
var param = { |
10 |
|
|
columnLeft: 20, |
11 |
|
|
columnWidth: 20, |
12 |
|
|
columnSpacing: 5, |
13 |
|
|
columnGroupTop: 10, |
14 |
|
|
columnTop: 15, |
15 |
|
|
rowTop: 20, |
16 |
|
|
rowHeight: 20, |
17 |
|
|
rowSpacing: 5, |
18 |
|
|
rowGroupLeft: 10, |
19 |
wakaba |
1.6 |
rowGroupFillLeft: 20, /* Must be same as columnLeft */ |
20 |
wakaba |
1.1 |
rowLeft: 15, |
21 |
|
|
cellTop: 20, |
22 |
wakaba |
1.6 |
cellLeft: 20, /* Must be same as columnLeft */ |
23 |
wakaba |
1.1 |
cellBottom: 20, |
24 |
|
|
cellRight: 20, |
25 |
|
|
explicitColumnGroupStrokeStyle: 'black', |
26 |
|
|
explicitColumnStrokeStyle: 'black', |
27 |
|
|
impliedColumnStrokeStyle: '#C0C0C0', |
28 |
|
|
explicitHeaderRowGroupStrokeStyle: 'black', |
29 |
|
|
explicitHeaderRowGroupFillStyle: 'rgba(220, 220, 220, 0.3)', |
30 |
|
|
explicitBodyRowGroupStrokeStyle: 'black', |
31 |
|
|
explicitBodyRowGroupFillStyle: 'rgba(0, 0, 0, 0)', |
32 |
|
|
explicitFooterRowGroupStrokeStyle: 'black', |
33 |
|
|
explicitFooterRowGroupFillStyle: 'rgba(220, 220, 220, 0.3)', |
34 |
|
|
explicitRowStrokeStyle: 'black', |
35 |
|
|
impliedRowStrokeStyle: '#C0C0C0', |
36 |
|
|
headerCellFillStyle: 'rgba(192, 192, 192, 0.5)', |
37 |
|
|
headerCellStrokeStyle: 'black', |
38 |
|
|
dataCellFillStyle: 'rgba(0, 0, 0, 0)', |
39 |
|
|
dataCellStrokeStyle: 'black', |
40 |
wakaba |
1.8 |
emptyDataCellStrokeStyle: '#C0C0C0', |
41 |
wakaba |
1.1 |
overlappingCellFillStyle: 'red', |
42 |
wakaba |
1.7 |
overlappingCellStrokeStyle: 'rgba(0, 0, 0, 0)', |
43 |
|
|
highlightCellFillStyle: 'yellow' |
44 |
wakaba |
1.1 |
}; |
45 |
|
|
|
46 |
wakaba |
1.7 |
canvas.drawTable = function () { |
47 |
|
|
|
48 |
wakaba |
1.1 |
var columnNumber = table.column.length; |
49 |
|
|
if (columnNumber < table.cell.length) columnNumber = table.cell.length; |
50 |
|
|
var rowNumber = 0; |
51 |
wakaba |
1.5 |
for (var i = 0; i < table.cell.length; i++) { |
52 |
wakaba |
1.1 |
if (table.cell[i] && rowNumber < table.cell[i].length) { |
53 |
|
|
rowNumber = table.cell[i].length; |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
wakaba |
1.2 |
canvas.style.width = 'auto'; // NOTE: Opera9 has default style="" |
58 |
|
|
canvas.style.height = 'auto'; |
59 |
|
|
// NOTE: Set style="" before width/height="" for ExplorerCanvas compatibility |
60 |
wakaba |
1.1 |
canvas.width = param.cellLeft |
61 |
|
|
+ (param.columnWidth + param.columnSpacing) * columnNumber |
62 |
|
|
+ param.cellRight; |
63 |
|
|
canvas.height = param.cellTop |
64 |
|
|
+ (param.rowHeight + param.rowSpacing) * rowNumber |
65 |
|
|
+ param.cellBottom; |
66 |
|
|
|
67 |
|
|
var y = param.rowTop; |
68 |
wakaba |
1.5 |
for (var i = 0; i < table.row_group.length; i++) { |
69 |
wakaba |
1.1 |
var rg = table.row_group[i]; |
70 |
|
|
c2d.beginPath (); |
71 |
|
|
if (rg.type == 'thead') { |
72 |
|
|
c2d.strokeStyle = param.explicitHeaderRowGroupStrokeStyle; |
73 |
|
|
c2d.fillStyle = param.explicitHeaderRowGroupFillStyle; |
74 |
|
|
} else if (rg.type == 'tfoot') { |
75 |
|
|
c2d.strokeStyle = param.explicitFooterRowGroupStrokeStyle; |
76 |
|
|
c2d.fillStyle = param.explicitFooterRowGroupFillStyle; |
77 |
|
|
} else { |
78 |
|
|
c2d.strokeStyle = param.explicitBodyRowGroupStrokeStyle; |
79 |
|
|
c2d.fillStyle = param.explicitBodyRowGroupFillStyle; |
80 |
|
|
} |
81 |
|
|
var dy = (param.rowHeight + param.rowSpacing) * rg.height; |
82 |
|
|
c2d.moveTo (param.rowGroupLeft, y); |
83 |
|
|
c2d.lineTo (param.rowGroupLeft, y + dy - param.rowSpacing); |
84 |
|
|
c2d.stroke (); |
85 |
|
|
c2d.closePath (); |
86 |
|
|
c2d.beginPath (); |
87 |
wakaba |
1.6 |
c2d.rect (param.rowGroupFillLeft, |
88 |
wakaba |
1.1 |
y, |
89 |
|
|
(param.columnWidth + param.columnSpacing) * columnNumber - param.columnSpacing, |
90 |
|
|
dy - param.rowSpacing); |
91 |
|
|
c2d.fill (); |
92 |
|
|
c2d.closePath (); |
93 |
|
|
y += dy; |
94 |
|
|
i += rg.height - 1; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
c2d.beginPath (); |
98 |
|
|
c2d.strokeStyle = param.explicitColumnGroupStrokeStyle; |
99 |
|
|
var x = param.columnLeft; |
100 |
wakaba |
1.5 |
for (var i = 0; i < table.column_group.length; i++) { |
101 |
wakaba |
1.1 |
var cg = table.column_group[i]; |
102 |
|
|
c2d.moveTo (x, param.columnGroupTop); |
103 |
|
|
x += (param.columnWidth + param.columnSpacing) * cg.width; |
104 |
|
|
c2d.lineTo (x - param.columnSpacing, param.columnGroupTop); |
105 |
|
|
i += cg.width - 1; |
106 |
|
|
} |
107 |
|
|
c2d.stroke (); |
108 |
|
|
c2d.closePath (); |
109 |
|
|
|
110 |
|
|
var x = param.columnLeft; |
111 |
wakaba |
1.5 |
for (var i = 0; i < columnNumber; i++) { |
112 |
wakaba |
1.1 |
var c = table.column[i]; |
113 |
|
|
c2d.beginPath (); |
114 |
|
|
c2d.moveTo (x, param.columnTop); |
115 |
|
|
x += param.columnWidth + param.columnSpacing; |
116 |
|
|
c2d.lineTo (x - param.columnSpacing, param.columnTop); |
117 |
|
|
if (c) { |
118 |
|
|
c2d.strokeStyle = param.explicitColumnStrokeStyle; |
119 |
|
|
} else { |
120 |
|
|
c2d.strokeStyle = param.impliedColumnStrokeStyle; |
121 |
|
|
} |
122 |
|
|
c2d.stroke (); |
123 |
|
|
c2d.closePath (); |
124 |
|
|
} |
125 |
|
|
|
126 |
wakaba |
1.3 |
var map = document.createElement ('map'); |
127 |
wakaba |
1.1 |
var x = param.cellLeft; |
128 |
wakaba |
1.5 |
for (var i = 0; i < table.cell.length; i++) { |
129 |
wakaba |
1.1 |
var y = param.cellTop; |
130 |
|
|
if (!table.cell[i]) continue; |
131 |
wakaba |
1.5 |
for (var j = 0; j < table.cell[i].length; j++) { |
132 |
wakaba |
1.1 |
var c = table.cell[i][j]; |
133 |
|
|
if (c && ((c[0].x == i && c[0].y == j) || c.length > 1)) { |
134 |
|
|
c2d.beginPath (); |
135 |
|
|
var width = (param.columnWidth + param.columnSpacing) * c[0].width |
136 |
|
|
- param.columnSpacing; |
137 |
|
|
var height = (param.rowHeight + param.rowSpacing) * c[0].height |
138 |
|
|
- param.rowSpacing; |
139 |
|
|
if (c.length == 1) { |
140 |
|
|
c2d.rect (x, y, width, height); |
141 |
|
|
c2d.fillStyle = c[0].is_header |
142 |
|
|
? param.headerCellFillStyle : param.dataCellFillStyle; |
143 |
|
|
c2d.strokeStyle = c[0].is_header |
144 |
wakaba |
1.8 |
? param.headerCellStrokeStyle |
145 |
|
|
: c[0].is_empty |
146 |
|
|
? param.emptyDataCellStrokeStyle |
147 |
|
|
: param.dataCellStrokeStyle; |
148 |
wakaba |
1.3 |
if (c[0].id) { |
149 |
|
|
var area = document.createElement ('area'); |
150 |
|
|
area.shape = 'rect'; |
151 |
|
|
area.coords = [x, y, x + width, y + height].join (','); |
152 |
|
|
area.alt = 'Cell (' + c[0].x + ', ' + c[0].y + ')'; |
153 |
wakaba |
1.4 |
area.href = '#' + idPrefix + 'node-' + c[0].id; |
154 |
|
|
area.id = idPrefix + 'cell-' + c[0].id; |
155 |
wakaba |
1.7 |
area.onmouseover = (function (v) { |
156 |
|
|
return function () { |
157 |
|
|
canvas.highlightCells (v); |
158 |
|
|
}; |
159 |
|
|
}) (c[0].header); |
160 |
wakaba |
1.3 |
map.appendChild (area); |
161 |
|
|
} |
162 |
wakaba |
1.1 |
} else { |
163 |
|
|
c2d.rect (x, y, param.columnWidth, param.rowHeight); |
164 |
|
|
c2d.fillStyle = param.overlappingCellFillStyle; |
165 |
|
|
c2d.strokeStyle = param.overlappingCellStrokeStyle; |
166 |
|
|
} |
167 |
|
|
c2d.fill (); |
168 |
|
|
c2d.stroke (); |
169 |
|
|
c2d.closePath (); |
170 |
|
|
} |
171 |
|
|
y += param.rowHeight + param.rowSpacing; |
172 |
|
|
} |
173 |
|
|
x += param.columnWidth + param.columnSpacing; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
var y = param.rowTop; |
177 |
wakaba |
1.5 |
for (var i = 0; i < rowNumber; i++) { |
178 |
wakaba |
1.1 |
c2d.beginPath (); |
179 |
|
|
c2d.moveTo (param.rowLeft, y); |
180 |
|
|
y += param.rowHeight + param.rowSpacing; |
181 |
|
|
c2d.lineTo (param.rowLeft, y - param.rowSpacing); |
182 |
|
|
//if (true) { |
183 |
|
|
c2d.strokeStyle = param.explicitRowStrokeStyle; |
184 |
|
|
//} else { |
185 |
|
|
// c2d.strokeStyle = param.impliedRowStrokeStyle; |
186 |
|
|
//} |
187 |
|
|
c2d.stroke (); |
188 |
|
|
c2d.closePath (); |
189 |
|
|
} |
190 |
wakaba |
1.3 |
|
191 |
wakaba |
1.7 |
return map; |
192 |
|
|
}; // canvas.drawTable |
193 |
|
|
|
194 |
|
|
canvas.highlightCells = function (cells) { |
195 |
|
|
var c2d = this.getContext ('2d'); |
196 |
|
|
|
197 |
|
|
for (var x in cells) { |
198 |
|
|
for (var y in cells[x]) { |
199 |
|
|
if (cells[x][y]) { |
200 |
|
|
var cell = table.cell[x][y][0]; |
201 |
|
|
c2d.beginPath (); |
202 |
|
|
c2d.rect |
203 |
|
|
(param.cellLeft + (param.columnWidth + param.columnSpacing) * x, |
204 |
|
|
param.cellTop + (param.rowHeight + param.rowSpacing) * y, |
205 |
|
|
(param.columnWidth + param.columnSpacing) * cell.width - param.columnSpacing, |
206 |
|
|
(param.rowHeight + param.rowSpacing) * cell.height - param.rowSpacing); |
207 |
|
|
c2d.fillStyle = param.highlightCellFillStyle; |
208 |
|
|
c2d.fill (); |
209 |
|
|
c2d.stroke (); |
210 |
|
|
c2d.closePath (); |
211 |
|
|
} |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
this.updateImgElement (); |
215 |
|
|
} // canvas.highlightCells |
216 |
|
|
|
217 |
|
|
var map = canvas.drawTable (); |
218 |
wakaba |
1.3 |
if (map.hasChildNodes ()) { |
219 |
wakaba |
1.4 |
var mapid = /* idPrefix + */ 'table-map-' + ++document.TableMapId; |
220 |
wakaba |
1.3 |
map.name = mapid; |
221 |
|
|
parent.appendChild (map); |
222 |
|
|
var img = document.createElement ('img'); |
223 |
|
|
img.useMap = '#' + mapid; |
224 |
wakaba |
1.7 |
canvas.updateImgElement = function () { |
225 |
|
|
img.src = this.toDataURL (); |
226 |
|
|
}; |
227 |
|
|
img.onmouseover = function (e) { |
228 |
|
|
if (e.target == e.currentTarget) { |
229 |
|
|
canvas.drawTable (); |
230 |
|
|
canvas.updateImgElement (); |
231 |
|
|
} |
232 |
|
|
}; |
233 |
|
|
canvas.updateImgElement (); |
234 |
wakaba |
1.3 |
parent.appendChild (img); |
235 |
|
|
canvas.style.display = 'none'; |
236 |
wakaba |
1.7 |
} else { |
237 |
|
|
canvas.updateImgElement = function () {}; |
238 |
wakaba |
1.3 |
} |
239 |
wakaba |
1.1 |
} // tableToCanvas |
240 |
|
|
|
241 |
wakaba |
1.3 |
if (!document.TableMapId) document.TableMapId = 0; |
242 |
|
|
|
243 |
wakaba |
1.1 |
/* |
244 |
|
|
|
245 |
wakaba |
1.4 |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
246 |
wakaba |
1.1 |
|
247 |
|
|
This library is free software; you can redistribute it |
248 |
|
|
and/or modify it under the same terms as Perl itself. |
249 |
|
|
|
250 |
|
|
*/ |
251 |
wakaba |
1.8 |
/* $Date: 2008/05/06 07:50:28 $ */ |