|
| 1 | +var PivotView = function (container) { |
| 2 | + |
| 3 | + if (!(container instanceof HTMLElement)) throw new Error("Please, provide HTMLElement " + |
| 4 | + "instance \"container\" into pivot table configuration."); |
| 5 | + |
| 6 | + this.elements = { |
| 7 | + container: container, |
| 8 | + base: document.createElement("div"), |
| 9 | + tableContainer: document.createElement("div"), |
| 10 | + controlsContainer: document.createElement("div") |
| 11 | + }; |
| 12 | + |
| 13 | + this.init(); |
| 14 | + |
| 15 | +}; |
| 16 | + |
| 17 | +PivotView.prototype.init = function () { |
| 18 | + |
| 19 | + var els = this.elements; |
| 20 | + |
| 21 | + els.base.className = "lpt"; |
| 22 | + els.tableContainer.textContent = "Loading..."; |
| 23 | + els.tableContainer.className = "tableContainer"; |
| 24 | + els.base.appendChild(els.tableContainer); |
| 25 | + els.container.appendChild(els.base); |
| 26 | + |
| 27 | +}; |
| 28 | + |
| 29 | +PivotView.prototype.render = function (data) { |
| 30 | + |
| 31 | + if (data.error) { |
| 32 | + this.elements.tableContainer.innerHTML = "<h1>Unable to render data</h1><p>" |
| 33 | + + data.error + "</p>"; |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + this.elements.tableContainer.textContent = ""; |
| 38 | + |
| 39 | + var vTree, horDimArr = [], tableWidth, td, extraTh, tr, i, |
| 40 | + table = document.createElement("table"), |
| 41 | + thead = document.createElement("thead"), |
| 42 | + tbody = document.createElement("tbody"); |
| 43 | + |
| 44 | + var build = function (tree, barr, dim) { |
| 45 | + |
| 46 | + var d = [], ch, n, o, nn = 0; |
| 47 | + |
| 48 | + if (barr) { |
| 49 | + if (!dim) dim = 0; |
| 50 | + if (!horDimArr[dim]) horDimArr[dim] = []; |
| 51 | + } |
| 52 | + |
| 53 | + for (var i in tree) { |
| 54 | + ch = tree[i].children ? build(tree[i].children, barr, barr ? dim + 1 : dim) : null; |
| 55 | + n = ch ? ch.numOfChildren : 1; |
| 56 | + nn += n; |
| 57 | + d.push(o = { |
| 58 | + caption: tree[i].caption, |
| 59 | + dimension: tree[i].dimension, |
| 60 | + path: tree[i].path, |
| 61 | + children: ch ? ch.children : ch, |
| 62 | + numOfChildren: n |
| 63 | + }); |
| 64 | + if (barr) horDimArr[dim].push(o); |
| 65 | + } |
| 66 | + |
| 67 | + return { |
| 68 | + children: d, |
| 69 | + numOfChildren: nn |
| 70 | + } |
| 71 | + |
| 72 | + }; |
| 73 | + |
| 74 | + var prepend = function (cont, el) { |
| 75 | + cont.insertBefore(el, cont.firstChild); |
| 76 | + }; |
| 77 | + |
| 78 | + var lastTr, |
| 79 | + trs = [], |
| 80 | + maxLev = 1; |
| 81 | + |
| 82 | + var verticalTree = function (children, sti, lev) { |
| 83 | + |
| 84 | + if (!sti) sti = 0; |
| 85 | + if (!lev) lev = 1; |
| 86 | + if (lev > maxLev) maxLev = lev; |
| 87 | + |
| 88 | + for (var i in children) { |
| 89 | + |
| 90 | + var ch = children[i], |
| 91 | + td; |
| 92 | + |
| 93 | + if (ch.children) { |
| 94 | + verticalTree(ch.children, sti, lev + 1); |
| 95 | + td = document.createElement("th"); |
| 96 | + td.setAttribute("rowspan", ch.numOfChildren); |
| 97 | + td.textContent = ch.caption || ""; |
| 98 | + prepend(trs[sti], td); |
| 99 | + sti += ch.numOfChildren; |
| 100 | + } else { |
| 101 | + trs.push(lastTr = document.createElement("tr")); |
| 102 | + tbody.appendChild(lastTr); |
| 103 | + td = document.createElement("th"); |
| 104 | + td.textContent = ch.caption || ""; |
| 105 | + prepend(lastTr, td); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + }; |
| 111 | + |
| 112 | + build(data.dimensions[0], 1); |
| 113 | + tableWidth = horDimArr[horDimArr.length - 1].length; |
| 114 | + vTree = build(data.dimensions[1]); |
| 115 | + |
| 116 | + verticalTree(vTree.children); |
| 117 | + |
| 118 | + for (var u = 0; u < horDimArr.length; u++) { |
| 119 | + |
| 120 | + tr = document.createElement("tr"); |
| 121 | + |
| 122 | + if (u == 0) { |
| 123 | + var cornerTd = document.createElement("th"); |
| 124 | + //cornerTd.innerHTML = ""; |
| 125 | + cornerTd.setAttribute("rowspan", horDimArr.length.toString()); |
| 126 | + cornerTd.setAttribute("colspan", maxLev.toString()); |
| 127 | + tr.appendChild(cornerTd); |
| 128 | + } |
| 129 | + |
| 130 | + for (i in horDimArr[u]) { |
| 131 | + |
| 132 | + var ch = horDimArr[u][i]; |
| 133 | + |
| 134 | + td = document.createElement("th"); |
| 135 | + |
| 136 | + td.textContent = ch.caption || ""; |
| 137 | + td.setAttribute("colspan", ch.numOfChildren); |
| 138 | + tr.appendChild(td); |
| 139 | + } |
| 140 | + |
| 141 | + thead.appendChild(tr); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + for (i = 0; i < data.dataArray.length; i++) { |
| 146 | + td = document.createElement("td"); |
| 147 | + td.textContent = data.dataArray[i]; |
| 148 | + tr = trs[Math.floor(i / tableWidth)]; |
| 149 | + if (!tr) { |
| 150 | + trs[Math.floor(i / tableWidth)] = tr = document.createElement("tr"); |
| 151 | + extraTh = document.createElement("th"); |
| 152 | + tr.appendChild(extraTh); |
| 153 | + tbody.appendChild(tr); |
| 154 | + } |
| 155 | + tr.appendChild(td); |
| 156 | + } |
| 157 | + |
| 158 | + table.appendChild(thead); |
| 159 | + table.appendChild(tbody); |
| 160 | + this.elements.tableContainer.appendChild(table); |
| 161 | + |
| 162 | +}; |
0 commit comments