Skip to content

Commit 3beff2b

Browse files
complete conditional formatting
1 parent 6566b53 commit 3beff2b

File tree

7 files changed

+91
-4
lines changed

7 files changed

+91
-4
lines changed

example/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
//, triggerEvent: "touchstart" // all "click" events will be replaced by this event
105105
//, caption: "My table" // if set, table basic caption will be replaced by this text
106106
, showSummary: true // show summary by columns
107+
//, conditionalFormattingOn: true // enable conditional formatting rules
107108
//, drillDownTarget: "<dashboard name>" // undocumented, deepSee only
108109
};
109110

gulpfile.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ gulp.task("gatherScripts", ["clean"], function () {
3535
.pipe(wrap("LightPivotTable = (function(){<%= contents %> return LightPivotTable;}());"))
3636
.pipe(uglify({
3737
output: {
38-
ascii_only: true
38+
ascii_only: true,
39+
width: 30000,
40+
max_line_len: 30000
3941
}
4042
}))
4143
.pipe(header(banner, { pkg: pkg }))

package.json

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

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var setup = { // Object that contain settings. Any setting may be missed.
5555
[ , triggerEvent: "touchstart" // all "click" events will be replaced by this event ]
5656
[ , caption: "My table" // if set, table basic caption will be replaced by this text ]
5757
[ , showSummary: true // show summary by columns ]
58+
[ , conditionalFormattingOn: true // pass false to turn off conditional formatting ]
5859
[ , drillDownTarget: "<dashboard name>" // deepSee only - dashboard to open ]
5960
},
6061
lp = new LightPivotTable(setup);

source/js/DataController.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ DataController.prototype.setData = function (data) {
9090
this.resetDimensionProps();
9191
this.resetConditionalFormatting();
9292
this.resetRawData();
93-
console.log("Data:", data);
9493

9594
this._trigger();
9695
return data;
@@ -159,6 +158,9 @@ DataController.prototype.resetConditionalFormatting = function () {
159158
}
160159

161160
ocfArr = this.controller.CONFIG.pivotProperties["formatRules"] || [];
161+
if (ocfArr.length && typeof this.controller.CONFIG.conditionalFormattingOn === "undefined") {
162+
this.controller.CONFIG.conditionalFormattingOn = true;
163+
}
162164
for (var i in ocfArr) {
163165
// Warn: range ",2-3" is valid for standard pivot as ",2".
164166
// LPT will parse ",2-3" range as invalid.
@@ -426,6 +428,7 @@ DataController.prototype.resetRawData = function () {
426428

427429
if (this.controller.CONFIG["showSummary"] && rawData.length - xh > 1 // xh - see above
428430
&& (rawData[rawData.length - 1][0] || {})["isCaption"]) {
431+
data.info.SUMMARY_SHOWN = true;
429432
this.SUMMARY_SHOWN = true;
430433
this._dataStack[this._dataStack.length - 1].SUMMARY_SHOWN = true;
431434
rawData.push(summary = []);

source/js/DataSource.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ DataSource.prototype.getCurrentData = function (callback) {
162162

163163
var data = ready.data;
164164

165-
console.log("Retrieved data:", ready);
165+
//console.log("Retrieved data:", ready);
166166

167167
(data.Info || {}).action = _.ACTION;
168168
if (_.ACTION === "MDXDrillthrough") {

source/js/PivotView.js

+80
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,75 @@ PivotView.prototype.removeMessage = function () {
266266

267267
};
268268

269+
/**
270+
* @param {*} value1
271+
* @param {string} operator
272+
* @param {*} value2 - fixed value
273+
* @private
274+
* @return {boolean}
275+
*/
276+
PivotView.prototype._matchCondition = function (value1, operator, value2) {
277+
278+
switch (operator) {
279+
case "=": return value1 == value2;
280+
case "<>": return value1 != value2;
281+
case ">": return value1 > value2;
282+
case ">=": return value1 >= value2;
283+
case "<": return value1 < value2;
284+
case "<=": return value1 <= value2;
285+
case "IN": return value2.toString().indexOf(value1) !== -1; // how does it normally work?
286+
case "BETWEEN": return value1 >= value2.split(",")[0] && value1 <= value2.split(",")[1];
287+
case "IS NULL": return !value1;
288+
default: {
289+
console.error("Formatting error: unknown format operator \"" + operator + "\"");
290+
return false;
291+
}
292+
}
293+
294+
};
295+
296+
/**
297+
* Applies conditional formatting for element.
298+
*
299+
* @param {object} rules - Special object that contain formatting rules.
300+
* @param {string} key - Position y,x separated by comma or empty string for global.
301+
* @param {*} value - Original value to format (comparator).
302+
* @param {HTMLElement} element - element to format.
303+
*/
304+
PivotView.prototype.applyConditionalFormatting = function (rules, key, value, element) {
305+
306+
var actualRules = rules[""] || [],
307+
p, i, rule, html, xs, num;
308+
actualRules = actualRules.concat(rules[key] || []);
309+
if ((xs = key.split(",")).length === 2) {
310+
actualRules = actualRules.concat(rules[xs[0] + ","] || [], rules["," + xs[1]] || []);
311+
}
312+
313+
for (p in actualRules) {
314+
315+
rule = actualRules[p];
316+
if (!this._matchCondition(value, rule["operator"], rule["value"])) continue;
317+
318+
// apply formatting
319+
if (rule["style"])
320+
element.setAttribute("style", (element.getAttribute("style") || "") + rule["style"]);
321+
if (rule["icon"]) {
322+
element.textContent = ""; html = "<div style=\"overflow: hidden; height: 16px;\">";
323+
num = parseInt(rule["iconCount"]) || 1;
324+
for (i = 0; i < num; i++) {
325+
html += "<img alt=\"*\" style=\"padding-right:2px; height: 100%;\" " +
326+
"src=\"" + rule["icon"] + "\"/>";
327+
}
328+
// LPT won't change default format (f.e. text-align) for content.
329+
// element.className = (element.className || "") + " formatLeft";
330+
element.innerHTML = html + "</div>";
331+
}
332+
if (rule["text"]) element.textContent = rule["text"];
333+
334+
}
335+
336+
};
337+
269338
/**
270339
* @param container
271340
*/
@@ -534,6 +603,17 @@ PivotView.prototype.renderRawData = function (data) {
534603
}
535604
}
536605
if (rawData[y][x].style) td.setAttribute("style", rawData[y][x].style);
606+
if (
607+
this.controller.CONFIG.conditionalFormattingOn // totals formatting present
608+
&& !(info.SUMMARY_SHOWN && rawData.length - 1 === y) // exclude totals formatting
609+
) {
610+
this.applyConditionalFormatting(
611+
data["conditionalFormatting"],
612+
(y - info.topHeaderRowsNumber + 1) + "," + (x - info.leftHeaderColumnsNumber + 1),
613+
rawData[y][x].value,
614+
td
615+
);
616+
}
537617

538618
// add handlers
539619
td.addEventListener(CLICK_EVENT, (function (x, y, cell) {

0 commit comments

Comments
 (0)