All files / dgrid GridWithColumnSetsFromHtml.js

0% Statements 0/53
0% Branches 0/18
0% Functions 0/4
0% Lines 0/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                               
define([
	'./GridFromHtml',
	'./ColumnSet',
	'dojo/_base/declare'
], function (GridFromHtml, ColumnSet, declare) {
	// summary:
	//		This module augments GridFromHtml with additional support for interpreting
	//		ColumnSets from colgroups in table markup.
 
	function getColumnSetsFromDom(domNode) {
		// summary:
		//		Generates ColumnSets from DOM.
 
		var columnsets = [], // to be pushed upon / returned
			cgspans = [], // stores info on columnset sizes (colgroup span)
			rowspans = [], // will store info on any "unexhausted" rowspans
			colgroups = domNode.getElementsByTagName('colgroup'),
			cglen = colgroups.length,
			trs = domNode.getElementsByTagName('tr'),
			trslen = trs.length,
			getNum = GridFromHtml.utils.getNumFromAttr,
			getCol = GridFromHtml.utils.getColumnFromCell,
			// used in loops:
			currcol, // keeps track of what column we're at
			currcg, // and which colgroup
			groupColumns,
			tr,
			ths,
			thslen,
			i,
			j,
			tmp;
 
		function incCurrcol(amount) {
			// Check whether we've passed into the next colgroup within current row.
			// (Used within th loop)
			currcol += amount;
			tmp = cgspans[currcg];
			if (currcol >= tmp) {
				// First, push info for the set we just finished:
				// (i is still the active row index from the for loop)
				columnsets[currcg][i] = groupColumns;
 
				// Now, time to move on to the next columnset for this row.
				currcol -= tmp;
				currcg++;
				groupColumns = [];
			}
		}
 
		// no need for ColumnSet unless there's >1 colgroup
		if (cglen < 2) {
			return false;
		}
 
		// read span from each colgroup (defaults to 1)
		for (i = 0; i < cglen; i++) {
			// store number of cells this column spans
			tmp = getNum(colgroups[i], 'span') || 1;
			cgspans[i] = tmp;
			// add nested array to return value to be populated for this set
			columnsets[i] = [];
			// initialize inner rowspan-tracking array for each
			rowspans[i] = [];
			for (j = 0; j < tmp; j++) {
				rowspans[i][j] = 0;
			}
		}
 
		for (i = 0; i < trslen; i++) {
			currcol = currcg = 0;
			groupColumns = [];
			tr = trs[i];
			ths = tr.getElementsByTagName('th');
			thslen = ths.length;
			for (j = 0; j < thslen; j++) {
				// account for space occupied by previous rowSpans
				while (rowspans[currcg][currcol]) {
					// decrement rowspan "leftover" for next iteration
					rowspans[currcg][currcol]--;
					// skip past this cell for now, and try again w/ updated currcg/col
					incCurrcol(1);
				}
 
				// store cell info
				tmp = getCol(ths[j]);
				groupColumns.push(tmp);
 
				// if this cell has rowspan, keep that in mind for future iterations
				rowspans[currcg][currcol] = tmp.rowSpan ? tmp.rowSpan - 1 : 0;
 
				// increment currcol/currcg appropriately, accounting for cell colSpan
				incCurrcol(tmp.colSpan || 1);
			}
			// At the end of processing each row, there is a chance that the last
			// column set didn't get pushed yet (specifically if there are trailing
			// rowspans - since rowspan "debt" gets iterated at the beginning of each
			// iteration, not the end).  In that case, push the last one now.
			if (groupColumns.length) {
				columnsets[currcg][i] = groupColumns;
			}
		}
		if (tr) {
			domNode.removeChild(tr.parentNode);
		}
		return columnsets;
	}
	return declare([GridFromHtml, ColumnSet], {
		configStructure: function () {
			// summary:
			//		Configure subRows based on HTML originally in srcNodeRef
 
			var tmp;
			if (!this._checkedTrs) {
				tmp = getColumnSetsFromDom(this.srcNodeRef);
				if (tmp) {
					this.columnSets = tmp;
					this._checkedTrs = true;
				}
				else {
					// no reason to worry about ColumnSets, let GridFromHtml do the job
					return this.inherited(arguments);
				}
			}
			return this.inherited(arguments);
		}
	});
});