Skip to content

Commit 3d55595

Browse files
extending API - add get cell call and get w/h call
1 parent 8ad4726 commit 3d55595

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "LightPivotTable",
33
"author": "ZitRo",
4-
"version": "1.2.12",
4+
"version": "1.2.13",
55
"description": "A lightweight pivot table for MDX2JSON source for InterSystems Cache",
66
"main": "test/testServer.js",
77
"repository": {

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ lp.changeBasicMDX("..."); // change mdx for LPT
9191
lp.getActualMDX(); // returns currently displayed MDX
9292
lp.getSelectedRows(); // returns array with selected rows indexes. First row have index 1.
9393
lp.attachTrigger("contentRendered", function (lpInstance) { }); // attaches trigger during runtime
94+
95+
// Additional calls:
96+
lp.pivotView.getCellElement(x, y, considerHeaders); // returns cell element by given coordinates
97+
lp.pivotView.getTableSize(considerHeaders); // returns { width: N, height: M }
9498
```
9599

96100
#### Caché DeepSee

source/js/PivotView.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,102 @@ PivotView.prototype.init = function () {
109109

110110
};
111111

112+
/**
113+
* Return cell element which contains table data.
114+
* @param {number} x
115+
* @param {number} y
116+
* @param {boolean} [considerHeaders] - With this flag origin will be set to actual table look
117+
* origin. If false, only table body's first cell will become
118+
* as origin.
119+
* @return {HTMLElement}
120+
*/
121+
PivotView.prototype.getCellElement = function (x, y, considerHeaders) {
122+
123+
var element = this.tablesStack[this.tablesStack.length - 1].element,
124+
table, hh, hw, table2;
125+
126+
var getTableCell = function (table, x, y) {
127+
var m = [], row, cell, xx, tx, ty, xxx, yyy;
128+
for(yyy = 0; yyy < table.rows.length; yyy++) {
129+
row = table.rows[yyy];
130+
for(xxx = 0; xxx < row.cells.length; xxx++) {
131+
cell = row.cells[xxx];
132+
xx = xxx;
133+
for(; m[yyy] && m[yyy][xx]; ++xx) {}
134+
for(tx = xx; tx < xx + cell.colSpan; ++tx) {
135+
for(ty = yyy; ty < yyy + cell.rowSpan; ++ty) {
136+
if (!m[ty])
137+
m[ty] = [];
138+
m[ty][tx] = true;
139+
}
140+
}
141+
if (xx <= x && x < xx + cell.colSpan && yyy <= y && y < yyy + cell.rowSpan)
142+
return cell;
143+
}
144+
}
145+
return null;
146+
};
147+
148+
if (considerHeaders) {
149+
table = element.getElementsByClassName("lpt-topHeader")[0]; if (!table) return null;
150+
table = table.getElementsByTagName("table")[0]; if (!table) return null;
151+
hh = 0; [].slice.call(table.rows).forEach(function (e) {
152+
hh += e.rowSpan || 1;
153+
});
154+
table2 = element.getElementsByClassName("lpt-leftHeader")[0]; if (!table) return null;
155+
table2 = table2.getElementsByTagName("table")[0]; if (!table) return null;
156+
hw = 0; [].slice.call((table2.rows[0] || { cells: [] }).cells).forEach(function (e) {
157+
hw += e.colSpan || 1;
158+
});
159+
if (x < hw && y < hh)
160+
return element.getElementsByClassName("lpt-headerValue")[0] || null;
161+
if (x >= hw && y < hh)
162+
return (getTableCell(table, x - hw, y) || { childNodes: [null] }).childNodes[0];
163+
if (x < hw && y >= hh)
164+
return (getTableCell(table2, x, y - hh) || { childNodes: [null] }).childNodes[0];
165+
x -= hw; y -= hh;
166+
}
167+
168+
table = element.getElementsByClassName("lpt-tableBlock")[0]; if (!table) return null;
169+
table = table.getElementsByTagName("table")[0]; if (!table) return null;
170+
return ((table.rows[y] || { cells: [] }).cells[x] || { childNodes: [null] }).childNodes[0];
171+
172+
};
173+
174+
/**
175+
* @see getCellElement
176+
* @param {boolean} [considerHeaders]
177+
*/
178+
PivotView.prototype.getTableSize = function (considerHeaders) {
179+
180+
var table, hw = 0, hh = 0, element = this.tablesStack[this.tablesStack.length - 1].element;
181+
182+
table = element.getElementsByClassName("lpt-tableBlock")[0]; if (!table) return 0;
183+
table = table.getElementsByTagName("table")[0]; if (!table) return 0;
184+
[].slice.call(table.rows).forEach(function (e) {
185+
hh += e.rowSpan || 1;
186+
});
187+
[].slice.call((table.rows[0] || { cells: [] }).cells).forEach(function (e) {
188+
hw += e.colSpan || 1;
189+
});
190+
191+
if (!considerHeaders) return { width: hw, height: hh };
192+
193+
table = element.getElementsByClassName("lpt-topHeader")[0]; if (!table) return 0;
194+
table = table.getElementsByTagName("table")[0]; if (!table) return 0;
195+
[].slice.call(table.rows).forEach(function (e) {
196+
hh += e.rowSpan || 1;
197+
});
198+
table = element.getElementsByClassName("lpt-leftHeader")[0]; if (!table) return 0;
199+
table = table.getElementsByTagName("table")[0]; if (!table) return 0;
200+
[].slice.call((table.rows[0] || { cells: [] }).cells).forEach(function (e) {
201+
hw += e.colSpan || 1;
202+
});
203+
204+
return { width: hw, height: hh };
205+
206+
};
207+
112208
PivotView.prototype.displayLoading = function () {
113209

114210
this.displayMessage(

0 commit comments

Comments
 (0)