Skip to content

Commit 3011e53

Browse files
extended API: callbacks, controls, event replacement, etc
1 parent f20322c commit 3011e53

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed

example/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@
9292
//[ , namespace: "SAMPLES" ] // current namespace : default namespace
9393
//[ , username: "USER" ] // user name : default user
9494
//[ , password: "" ] // user password : default password
95+
},
96+
triggers: { //
97+
// onDrillDown: function ({Object { level: {number}, mdx: {string} }}) {}
98+
//, onDrillThrough: function ({Object { level: {number}, mdx: {string} }}) {}
99+
//, back: function ({Object { level: {number} }}) {}
95100
}
101+
//, hideButtons: true // hides "back" and "drillThrough" buttons
102+
//, triggerEvent: "touchstart" // all "click" events will be replaced by this event
96103
//, caption: "My table" // if set, table basic caption will be replaced by this text
97104
//, DrillDownExpression: "<spec>" // @deprecated drillDown expression split by "^"
98105
//, showSummary: true // show summary by columns

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": "0.7.0",
4+
"version": "0.8.0",
55
"description": "A lightweight pivot table for MDX2JSON source for InterSystems Cache",
66
"main": "test/testServer.js",
77
"directories": {

readme.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,26 @@ Build the project, and then include <code>build/css/lightPivotTable.css</code> a
3434
Then use global object <i>LightPivotTable</i>:
3535
```js
3636
var setup = { // Object that contain settings. Any setting may be missed.
37-
container: document.getElementById("pivot"), // HTMLElement on DOM which will contain table.
38-
dataSource: {
39-
MDX2JSONSource: "http://localhost:57772/SAMPLES", // MDX2JSON source server address
40-
basicMDX: "SELECT NON EMPTY [Product].[P1].[Product Category].Members ON 0, NON EMPTY [Outlet].[H1].[Region].Members ON 1 FROM [HoleFoods]" // basic MDX which are going to be rendered when widget loads
37+
container: document.getElementById("pivot") // HTMLElement which will contain table.
38+
, dataSource: {
39+
MDX2JSONSource: "http://localhost:57772/SAMPLES", // MDX2JSON server address
40+
basicMDX: typeof req === "object" ? req.basicMDX : req
41+
[ , namespace: "SAMPLES" ] // current namespace : default namespace
42+
[ , username: "USER" ] // user name : default user
43+
[ , password: "" ] // user password : default password
4144
}
42-
, caption: "My table" // if set, table basic caption will be replaced by this text
43-
, showSummary: true // show summary by columns
44-
, formatNumbers: "#,###.##" // number formatting mask // @deprecated
45-
, drillDownTarget: "dashboard name.dashboard" // custom drilldown target, DeepSee only.
45+
[ , triggers: { // provide your functions here to handle certain events
46+
onDrillDown: function ({Object { level: {number}, mdx: {string} }}) {}
47+
, onDrillThrough: function ({Object { level: {number}, mdx: {string} }}) {}
48+
, back: function ({Object { level: {number} }}) {}
49+
} ]
50+
[ , hideButtons: true // hides "back" and "drillThrough" buttons ]
51+
[ , triggerEvent: "touchstart" // all "click" events will be replaced by this event ]
52+
[ , caption: "My table" // if set, table basic caption will be replaced by this text ]
53+
[ , DrillDownExpression: "<spec>" // @deprecated drillDown expression split by "^" ]
54+
[ , showSummary: true // show summary by columns ]
55+
[ , formatNumbers: "#,###.##" // @deprecated ]
56+
[ , drillDownTarget: "<dashboard name>" // deepSee only - dashboard to open ]
4657
},
4758
lp = new LightPivotTable(setup);
4859

source/js/LightPivotTable.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ var LightPivotTable = function (configuration) {
99
var _ = this;
1010

1111
if (typeof configuration !== "object") configuration = {};
12+
this.normalizeConfiguration(configuration);
1213

1314
this._dataSourcesStack = [];
1415

1516
this.DRILL_LEVEL = -1;
1617
this.CONFIG = configuration;
1718

19+
/**
20+
* @see this.init
21+
* @type {object}
22+
*/
23+
this.CONTROLS = {};
24+
1825
this.mdxParser = new MDXParser();
1926
this.pivotView = new PivotView(this, configuration.container);
2027
this.dataSource = this.pushDataSource(configuration.dataSource);
@@ -151,6 +158,12 @@ LightPivotTable.prototype.tryDrillDown = function (filter) {
151158
_.pivotView.pushTable();
152159
_.dataController.pushData();
153160
_.dataController.setData(data);
161+
if (typeof _.CONFIG.triggers["drillDown"] === "function") {
162+
_.CONFIG.triggers["drillDown"].call(_, {
163+
level: _.DRILL_LEVEL,
164+
mdx: ds.basicMDX
165+
});
166+
}
154167
} else {
155168
_.popDataSource();
156169
}
@@ -185,6 +198,12 @@ LightPivotTable.prototype.tryDrillThrough = function (filters) {
185198
_.pivotView.pushTable();
186199
_.dataController.pushData();
187200
_.dataController.setData(data);
201+
if (typeof _.CONFIG.triggers["drillThrough"] === "function") {
202+
_.CONFIG.triggers["drillThrough"].call(_, {
203+
level: _.DRILL_LEVEL,
204+
mdx: ds.basicMDX
205+
});
206+
}
188207
} else {
189208
_.popDataSource();
190209
}
@@ -209,8 +228,28 @@ LightPivotTable.prototype.getPivotProperty = function (path) {
209228
return obj;
210229
};
211230

231+
/**
232+
* Fill up to normal config structure to avoid additional checks and issues.
233+
*
234+
* @param config
235+
*/
236+
LightPivotTable.prototype.normalizeConfiguration = function (config) {
237+
if (!config["triggers"]) config.triggers = {};
238+
if (!config["dataSource"]) config.dataSource = {};
239+
};
240+
212241
LightPivotTable.prototype.init = function () {
213242

243+
var _ = this;
244+
245+
this.CONTROLS.drillThrough = function () {
246+
_.pivotView._drillThroughClickHandler.call(_.pivotView);
247+
};
248+
249+
this.CONTROLS.back = function () {
250+
_.pivotView._backClickHandler.call(_.pivotView);
251+
};
252+
214253
this.refresh();
215254

216255
};

source/js/PivotView.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,30 @@ PivotView.prototype._rowClickHandler = function (rowIndex, cellData) {
144144

145145
PivotView.prototype._backClickHandler = function (event) {
146146

147-
event.cancelBubble = true;
148-
event.stopPropagation();
147+
if (event) {
148+
event.cancelBubble = true;
149+
event.stopPropagation();
150+
}
149151

150152
this.popTable();
151153
this.controller.popDataSource();
152154

155+
if (typeof this.controller.CONFIG.triggers["back"] === "function") {
156+
this.controller.CONFIG.triggers["back"].call(this.controller, {
157+
level: this.controller.DRILL_LEVEL
158+
});
159+
}
160+
153161
};
154162

155163
PivotView.prototype._drillThroughClickHandler = function (event) {
156164

157165
this.controller.tryDrillThrough();
158166

159-
event.cancelBubble = true;
160-
event.stopPropagation();
167+
if (event) {
168+
event.cancelBubble = true;
169+
event.stopPropagation();
170+
}
161171

162172
};
163173

@@ -376,6 +386,8 @@ PivotView.prototype.formatNumber = function (mask, value) {
376386
*/
377387
PivotView.prototype.renderRawData = function (data) {
378388

389+
var clickEvent = this.controller.CONFIG["triggerEvent"] || "click";
390+
379391
if (!data || !data[0] || !data[0][0]) {
380392
this.elements.tableContainer.innerHTML = "<h1>Unable to render data</h1><p>"
381393
+ JSON.stringify(data) + "</p>";
@@ -459,10 +471,11 @@ PivotView.prototype.renderRawData = function (data) {
459471
return i - y;
460472
})(data[y][x].group);
461473

462-
if (x === 0 && y === 0 && _.tablesStack.length > 1) {
474+
if (!_.controller.CONFIG["hideButtons"] && x === 0 && y === 0
475+
&& _.tablesStack.length > 1) {
463476
var elt = document.createElement("div");
464477
elt.className = "backButton";
465-
addTrigger(elt, "click", function (event) {
478+
addTrigger(elt, clickEvent, function (event) {
466479
_._backClickHandler.call(_, event);
467480
});
468481
td.insertBefore(elt, td.childNodes[td.childNodes.length - 1] || null);
@@ -477,7 +490,7 @@ PivotView.prototype.renderRawData = function (data) {
477490
if (td && x >= headLeftColsNum && y === headColsNum - 1) {
478491
// clickable cells (sort option)
479492
(function (x) {
480-
addTrigger(td, "click", function () {
493+
addTrigger(td, clickEvent, function () {
481494
var colNum = x - headLeftColsNum;
482495
_._columnClickHandler.call(_, colNum);
483496
});
@@ -487,7 +500,7 @@ PivotView.prototype.renderRawData = function (data) {
487500
// add _rowClickHandler to th's last column
488501
if (td && x === headLeftColsNum - 1 && y >= headRowsNum) {
489502
(function (y, x) {
490-
addTrigger(td, "click", function () {
503+
addTrigger(td, clickEvent, function () {
491504
var rowNum = y - headRowsNum;
492505
_._rowClickHandler.call(_, rowNum, data[y][x]);
493506
});
@@ -522,18 +535,19 @@ PivotView.prototype.renderRawData = function (data) {
522535
span.textContent = data[y][x].value;
523536
}
524537

525-
(function (x, y) {addTrigger(td, "click", function () {
538+
(function (x, y) {addTrigger(td, clickEvent, function () {
526539
_._cellClickHandler.call(_, x, y);
527540
})})(x, y);
528541
} else {
529542
span.textContent = data[y][x].value;
530543
}
531544
}
532545

533-
if (x === 0 && y === 0 && _.controller.dataController.getData().info.action === "MDX") {
546+
if (!_.controller.CONFIG["hideButtons"] && x === 0 && y === 0
547+
&& _.controller.dataController.getData().info.action === "MDX") {
534548
var element = document.createElement("div");
535549
element.className = "drillDownIcon";
536-
addTrigger(element, "click", function (event) {
550+
addTrigger(element, clickEvent, function (event) {
537551
_._drillThroughClickHandler.call(_, event);
538552
});
539553
td.insertBefore(element, td.childNodes[td.childNodes.length - 1] || null);

0 commit comments

Comments
 (0)