1 if(!dojo._hasResource["dojox.grid._grid.builder"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.grid._grid.builder"] = true;
3 dojo.provide("dojox.grid._grid.builder");
4 dojo.require("dojox.grid._grid.drag");
6 dojo.declare("dojox.grid.Builder",
10 // Base class to produce html for grid content.
11 // Also provide event decoration, providing grid related information inside the event object
12 // passed to grid events.
13 constructor: function(inView){
15 this.grid = inView.grid;
20 _table: '<table class="dojoxGrid-row-table" border="0" cellspacing="0" cellpadding="0" role="wairole:presentation">',
22 // generate starting tags for a cell
23 generateCellMarkup: function(inCell, inMoreStyles, inMoreClasses, isHeader){
24 var result = [], html;
26 html = [ '<th tabIndex="-1" role="wairole:columnheader"' ];
28 html = [ '<td tabIndex="-1" role="wairole:gridcell"' ];
30 inCell.colSpan && html.push(' colspan="', inCell.colSpan, '"');
31 inCell.rowSpan && html.push(' rowspan="', inCell.rowSpan, '"');
32 html.push(' class="dojoxGrid-cell ');
33 inCell.classes && html.push(inCell.classes, ' ');
34 inMoreClasses && html.push(inMoreClasses, ' ');
35 // result[0] => td opener, style
36 result.push(html.join(''));
37 // SLOT: result[1] => td classes
39 html = ['" idx="', inCell.index, '" style="'];
40 html.push(inCell.styles, inMoreStyles||'');
41 inCell.unitWidth && html.push('width:', inCell.unitWidth, ';');
42 // result[2] => markup
43 result.push(html.join(''));
44 // SLOT: result[3] => td style
47 inCell.attrs && html.push(" ", inCell.attrs);
49 // result[4] => td postfix
50 result.push(html.join(''));
51 // SLOT: result[5] => content
53 // result[6] => td closes
55 return result; // Array
59 isCellNode: function(inNode){
60 return Boolean(inNode && inNode.getAttribute && inNode.getAttribute("idx"));
63 getCellNodeIndex: function(inCellNode){
64 return inCellNode ? Number(inCellNode.getAttribute("idx")) : -1;
67 getCellNode: function(inRowNode, inCellIndex){
68 for(var i=0, row; row=dojox.grid.getTr(inRowNode.firstChild, i); i++){
69 for(var j=0, cell; cell=row.cells[j]; j++){
70 if(this.getCellNodeIndex(cell) == inCellIndex){
77 findCellTarget: function(inSourceNode, inTopNode){
79 while(n && (!this.isCellNode(n) || (dojox.grid.gridViewTag in n.offsetParent.parentNode && n.offsetParent.parentNode[dojox.grid.gridViewTag] != this.view.id)) && (n!=inTopNode)){
82 return n!=inTopNode ? n : null
86 baseDecorateEvent: function(e){
87 e.dispatch = 'do' + e.type;
89 e.sourceView = this.view;
90 e.cellNode = this.findCellTarget(e.target, e.rowNode);
91 e.cellIndex = this.getCellNodeIndex(e.cellNode);
92 e.cell = (e.cellIndex >= 0 ? this.grid.getCell(e.cellIndex) : null);
96 findTarget: function(inSource, inTag){
98 while(n && (n!=this.domNode) && (!(inTag in n) || (dojox.grid.gridViewTag in n && n[dojox.grid.gridViewTag] != this.view.id))){
101 return (n != this.domNode) ? n : null;
104 findRowTarget: function(inSource){
105 return this.findTarget(inSource, dojox.grid.rowIndexTag);
108 isIntraNodeEvent: function(e){
110 return (e.cellNode && e.relatedTarget && dojo.isDescendant(e.relatedTarget, e.cellNode));
112 // e.relatedTarget has permission problem in FF if it's an input: https://bugzilla.mozilla.org/show_bug.cgi?id=208427
117 isIntraRowEvent: function(e){
119 var row = e.relatedTarget && this.findRowTarget(e.relatedTarget);
120 return !row && (e.rowIndex==-1) || row && (e.rowIndex==row.gridRowIndex);
122 // e.relatedTarget on INPUT has permission problem in FF: https://bugzilla.mozilla.org/show_bug.cgi?id=208427
127 dispatchEvent: function(e){
128 if(e.dispatch in this){
129 return this[e.dispatch](e);
133 // dispatched event handlers
134 domouseover: function(e){
135 if(e.cellNode && (e.cellNode!=this.lastOverCellNode)){
136 this.lastOverCellNode = e.cellNode;
137 this.grid.onMouseOver(e);
139 this.grid.onMouseOverRow(e);
142 domouseout: function(e){
143 if(e.cellNode && (e.cellNode==this.lastOverCellNode) && !this.isIntraNodeEvent(e, this.lastOverCellNode)){
144 this.lastOverCellNode = null;
145 this.grid.onMouseOut(e);
146 if(!this.isIntraRowEvent(e)){
147 this.grid.onMouseOutRow(e);
152 domousedown: function(e){
154 this.grid.onMouseDown(e);
155 this.grid.onMouseDownRow(e)
160 dojo.declare("dojox.grid.contentBuilder",
164 // Produces html for grid data content. Owned by grid and used internally
165 // for rendering data. Override to implement custom rendering.
170 // cache html for rendering data rows
171 prepareHtml: function(){
172 var defaultGet=this.grid.get, rows=this.view.structure.rows;
173 for(var j=0, row; (row=rows[j]); j++){
174 for(var i=0, cell; (cell=row[i]); i++){
175 cell.get = cell.get || (cell.value == undefined) && defaultGet;
176 cell.markup = this.generateCellMarkup(cell, cell.cellStyles, cell.cellClasses, false);
181 // time critical: generate html using cache and data source
182 generateHtml: function(inDataIndex, inRowIndex){
184 html = [ this._table ],
187 rows = v.structure.rows;
189 obr && obr(inRowIndex, rows);
190 for(var j=0, row; (row=rows[j]); j++){
191 if(row.hidden || row.header){
194 html.push(!row.invisible ? '<tr>' : '<tr class="dojoxGrid-invisible">');
195 for(var i=0, cell, m, cc, cs; (cell=row[i]); i++){
196 m = cell.markup, cc = cell.customClasses = [], cs = cell.customStyles = [];
197 // content (format can fill in cc and cs as side-effects)
198 m[5] = cell.format(inDataIndex);
204 html.push.apply(html, m);
208 html.push('</table>');
209 return html.join(''); // String
212 decorateEvent: function(e){
213 e.rowNode = this.findRowTarget(e.target);
214 if(!e.rowNode){return false};
215 e.rowIndex = e.rowNode[dojox.grid.rowIndexTag];
216 this.baseDecorateEvent(e);
217 e.cell = this.grid.getCell(e.cellIndex);
218 return true; // Boolean
223 dojo.declare("dojox.grid.headerBuilder",
227 // Produces html for grid header content. Owned by grid and used internally
228 // for rendering data. Override to implement custom rendering.
234 // FIXME: isn't this getting mixed from dojox.grid.Builder, -1 character?
235 _table: '<table class="dojoxGrid-row-table" border="0" cellspacing="0" cellpadding="0" role="wairole:presentation"',
238 this.tableMap = new dojox.grid.tableMap(this.view.structure.rows);
241 generateHtml: function(inGetValue, inValue){
242 var html = [this._table], rows = this.view.structure.rows;
244 // render header with appropriate width, if possible so that views with flex columns are correct height
245 if(this.view.viewWidth){
246 html.push([' style="width:', this.view.viewWidth, ';"'].join(''));
249 dojox.grid.fire(this.view, "onBeforeRow", [-1, rows]);
250 for(var j=0, row; (row=rows[j]); j++){
254 html.push(!row.invisible ? '<tr>' : '<tr class="dojoxGrid-invisible">');
255 for(var i=0, cell, markup; (cell=row[i]); i++){
256 cell.customClasses = [];
257 cell.customStyles = [];
258 markup = this.generateCellMarkup(cell, cell.headerStyles, cell.headerClasses, true);
260 markup[5] = (inValue != undefined ? inValue : inGetValue(cell));
262 markup[3] = cell.customStyles.join(';');
264 markup[1] = cell.customClasses.join(' '); //(cell.customClasses ? ' ' + cell.customClasses : '');
265 html.push(markup.join(''));
269 html.push('</table>');
270 return html.join('');
274 getCellX: function(e){
277 var n = dojox.grid.ascendDom(e.target, dojox.grid.makeNotTagName("th"));
278 x -= (n && n.offsetLeft) || 0;
279 var t = e.sourceView.getScrollbarWidth();
280 if(!dojo._isBodyLtr() && e.sourceView.headerNode.scrollLeft < t)
282 //x -= getProp(ascendDom(e.target, mkNotTagName("td")), "offsetLeft") || 0;
284 var n = dojox.grid.ascendDom(e.target, function(){
285 if(!n || n == e.cellNode){
288 // Mozilla 1.8 (FF 1.5) has a bug that makes offsetLeft = -parent border width
289 // when parent has border, overflow: hidden, and is positioned
290 // handle this problem here ... not a general solution!
291 x += (n.offsetLeft < 0 ? 0 : n.offsetLeft);
298 decorateEvent: function(e){
299 this.baseDecorateEvent(e);
301 e.cellX = this.getCellX(e);
307 prepareResize: function(e, mod){
308 var i = dojox.grid.getTdIndex(e.cellNode);
309 e.cellNode = (i ? e.cellNode.parentNode.cells[i+mod] : null);
310 e.cellIndex = (e.cellNode ? this.getCellNodeIndex(e.cellNode) : -1);
311 return Boolean(e.cellNode);
314 canResize: function(e){
315 if(!e.cellNode || e.cellNode.colSpan > 1){
318 var cell = this.grid.getCell(e.cellIndex);
319 return !cell.noresize && !cell.isFlex();
322 overLeftResizeArea: function(e){
323 if(dojo._isBodyLtr()){
324 return (e.cellIndex>0) && (e.cellX < this.overResizeWidth) && this.prepareResize(e, -1);
326 return t = e.cellNode && (e.cellX < this.overResizeWidth);
329 overRightResizeArea: function(e){
330 if(dojo._isBodyLtr()){
331 return e.cellNode && (e.cellX >= e.cellNode.offsetWidth - this.overResizeWidth);
333 return (e.cellIndex>0) && (e.cellX >= e.cellNode.offsetWidth - this.overResizeWidth) && this.prepareResize(e, -1);
336 domousemove: function(e){
337 //console.log(e.cellIndex, e.cellX, e.cellNode.offsetWidth);
338 var c = (this.overRightResizeArea(e) ? 'e-resize' : (this.overLeftResizeArea(e) ? 'w-resize' : ''));
339 if(c && !this.canResize(e)){
342 e.sourceView.headerNode.style.cursor = c || ''; //'default';
347 domousedown: function(e){
348 if(!dojox.grid.drag.dragging){
349 if((this.overRightResizeArea(e) || this.overLeftResizeArea(e)) && this.canResize(e)){
350 this.beginColumnResize(e);
352 this.grid.onMouseDown(e);
353 this.grid.onMouseOverRow(e);
356 // this.beginMoveColumn(e);
361 doclick: function(e) {
362 if (new Date().getTime() < this.bogusClickTime) {
369 beginColumnResize: function(e){
371 var spanners = [], nodes = this.tableMap.findOverlappingNodes(e.cellNode);
372 for(var i=0, cell; (cell=nodes[i]); i++){
373 spanners.push({ node: cell, index: this.getCellNodeIndex(cell), width: cell.offsetWidth });
374 //console.log("spanner: " + this.getCellNodeIndex(cell));
377 scrollLeft: e.sourceView.headerNode.scrollLeft,
381 w: e.cellNode.clientWidth,
384 //console.log(drag.index, drag.w);
385 dojox.grid.drag.start(e.cellNode, dojo.hitch(this, 'doResizeColumn', drag), dojo.hitch(this, 'endResizeColumn', drag), e);
388 doResizeColumn: function(inDrag, inEvent){
389 var isLtr = dojo._isBodyLtr();
391 var w = inDrag.w + inEvent.deltaX;
393 var w = inDrag.w - inEvent.deltaX;
395 if(w >= this.minColWidth){
396 for(var i=0, s, sw; (s=inDrag.spanners[i]); i++){
398 sw = s.width + inEvent.deltaX;
400 sw = s.width - inEvent.deltaX;
402 s.node.style.width = sw + 'px';
403 inDrag.view.setColWidth(s.index, sw);
404 //console.log('setColWidth', '#' + s.index, sw + 'px');
406 inDrag.node.style.width = w + 'px';
407 inDrag.view.setColWidth(inDrag.index, w);
409 inDrag.view.headerNode.scrollLeft = (inDrag.scrollLeft - inEvent.deltaX);
412 if(inDrag.view.flexCells && !inDrag.view.testFlexCells()){
413 var t = dojox.grid.findTable(inDrag.node);
414 t && (t.style.width = '');
418 endResizeColumn: function(inDrag){
419 this.bogusClickTime = new Date().getTime() + 30;
420 setTimeout(dojo.hitch(inDrag.view, "update"), 50);
425 dojo.declare("dojox.grid.tableMap",
429 // Maps an html table into a structure parsable for information about cell row and col spanning.
430 // Used by headerBuilder
431 constructor: function(inRows){
432 this.mapRows(inRows);
437 mapRows: function(inRows){
438 // summary: Map table topography
440 //console.log('mapRows');
442 var rowCount = inRows.length;
446 // map which columns and rows fill which cells
448 for(var j=0, row; (row=inRows[j]); j++){
451 for(var j=0, row; (row=inRows[j]); j++){
452 for(var i=0, x=0, cell, colSpan, rowSpan; (cell=row[i]); i++){
453 while (this.map[j][x]){x++};
454 this.map[j][x] = { c: i, r: j };
455 rowSpan = cell.rowSpan || 1;
456 colSpan = cell.colSpan || 1;
457 for(var y=0; y<rowSpan; y++){
458 for(var s=0; s<colSpan; s++){
459 this.map[j+y][x+s] = this.map[j][x];
469 for(var j=0, row, h=''; (row=this.map[j]); j++,h=''){
470 for(var i=0, cell; (cell=row[i]); i++){
471 h += cell.r + ',' + cell.c + ' ';
477 getMapCoords: function(inRow, inCol){
478 // summary: Find node's map coords by it's structure coords
479 for(var j=0, row; (row=this.map[j]); j++){
480 for(var i=0, cell; (cell=row[i]); i++){
481 if(cell.c==inCol && cell.r == inRow){
482 return { j: j, i: i };
484 //else{console.log(inRow, inCol, ' : ', i, j, " : ", cell.r, cell.c); };
487 return { j: -1, i: -1 };
490 getNode: function(inTable, inRow, inCol){
491 // summary: Find a node in inNode's table with the given structure coords
492 var row = inTable && inTable.rows[inRow];
493 return row && row.cells[inCol];
496 _findOverlappingNodes: function(inTable, inRow, inCol){
498 var m = this.getMapCoords(inRow, inCol);
499 //console.log("node j: %d, i: %d", m.j, m.i);
500 var row = this.map[m.j];
501 for(var j=0, row; (row=this.map[j]); j++){
502 if(j == m.j){ continue; }
504 //console.log("overlaps: r: %d, c: %d", r, c);
505 var n = this.getNode(inTable, r, c);
506 if(n){ nodes.push(n); }
509 //console.log(nodes);
513 findOverlappingNodes: function(inNode){
514 return this._findOverlappingNodes(dojox.grid.findTable(inNode), dojox.grid.getTrIndex(inNode.parentNode), dojox.grid.getTdIndex(inNode));
519 dojox.grid.rowIndexTag = "gridRowIndex";
520 dojox.grid.gridViewTag = "gridView";