GridWithColumnSetsFromHtml.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. define(["./GridFromHtml", "./ColumnSet", "dojo/_base/declare"],
  2. function(GridFromHtml, ColumnSet, declare){
  3. // summary:
  4. // This module augments GridFromHtml with additional support for interpreting
  5. // ColumnSets from colgroups in table markup.
  6. function getColumnSetsFromDom(domNode){
  7. // summary:
  8. // Generates ColumnSets from DOM.
  9. var
  10. columnsets = [], // to be pushed upon / returned
  11. cgspans = [], // stores info on columnset sizes (colgroup span)
  12. rowspans = [], // will store info on any "unexhausted" rowspans
  13. colgroups = domNode.getElementsByTagName("colgroup"),
  14. cglen = colgroups.length,
  15. trs = domNode.getElementsByTagName("tr"),
  16. trslen = trs.length,
  17. getNum = GridFromHtml.utils.getNumFromAttr,
  18. getCol = GridFromHtml.utils.getColumnFromCell,
  19. // used in loops:
  20. currcol, // keeps track of what column we're at
  21. currcg, // and which colgroup
  22. groupColumns, tr, ths, i, j, tmp;
  23. function incCurrcol(amount){
  24. // Check whether we've passed into the next colgroup within current row.
  25. // (Used within th loop)
  26. currcol += amount;
  27. tmp = cgspans[currcg];
  28. if(currcol >= tmp){
  29. // First, push info for the set we just finished:
  30. // (i is still the active row index from the for loop)
  31. columnsets[currcg][i] = groupColumns;
  32. // Now, time to move on to the next columnset for this row.
  33. currcol -= tmp;
  34. currcg++;
  35. groupColumns = [];
  36. }
  37. }
  38. // no need for ColumnSet unless there's >1 colgroup
  39. if(cglen < 2){ return false; }
  40. // read span from each colgroup (defaults to 1)
  41. for(i = 0; i < cglen; i++){
  42. // store number of cells this column spans
  43. tmp = getNum(colgroups[i], "span") || 1;
  44. cgspans[i] = tmp;
  45. // add nested array to return value to be populated for this set
  46. columnsets[i] = [];
  47. // initialize inner rowspan-tracking array for each
  48. rowspans[i] = [];
  49. for(j = 0; j < tmp; j++){
  50. rowspans[i][j] = 0;
  51. }
  52. }
  53. for(i = 0; i < trslen; i++){
  54. currcol = currcg = 0;
  55. groupColumns = [];
  56. tr = trs[i];
  57. ths = tr.getElementsByTagName("th"), thslen = ths.length;
  58. for(j = 0; j < thslen; j++){
  59. // account for space occupied by previous rowSpans
  60. while(rowspans[currcg][currcol]){
  61. // decrement rowspan "leftover" for next iteration
  62. rowspans[currcg][currcol]--;
  63. // skip past this cell for now, and try again w/ updated currcg/col
  64. incCurrcol(1);
  65. }
  66. // store cell info
  67. tmp = getCol(ths[j]);
  68. groupColumns.push(tmp);
  69. // if this cell has rowspan, keep that in mind for future iterations
  70. rowspans[currcg][currcol] = tmp.rowSpan ? tmp.rowSpan - 1 : 0;
  71. // increment currcol/currcg appropriately, accounting for cell colSpan
  72. incCurrcol(tmp.colSpan || 1);
  73. }
  74. // At the end of processing each row, there is a chance that the last
  75. // column set didn't get pushed yet (specifically if there are trailing
  76. // rowspans - since rowspan "debt" gets iterated at the beginning of each
  77. // iteration, not the end). In that case, push the last one now.
  78. if(groupColumns.length){
  79. columnsets[currcg][i] = groupColumns;
  80. }
  81. }
  82. if(tr){
  83. domNode.removeChild(tr.parentNode);
  84. }
  85. return columnsets;
  86. }
  87. return declare([GridFromHtml, ColumnSet], {
  88. configStructure: function(){
  89. // summary:
  90. // Configure subRows based on HTML originally in srcNodeRef
  91. var tmp;
  92. if(!this._checkedTrs){
  93. tmp = getColumnSetsFromDom(this.srcNodeRef);
  94. if(tmp){
  95. this.columnSets = tmp;
  96. this._checkedTrs = true;
  97. }else{
  98. // no reason to worry about ColumnSets, let GridFromHtml do the job
  99. return this.inherited(arguments);
  100. }
  101. }
  102. return this.inherited(arguments);
  103. }
  104. });
  105. });