Skip to content

Commit

Permalink
enable ui-table to be configured via ui_control (node-red#21)
Browse files Browse the repository at this point in the history
* enable ui-table to be configured via ui_control

* changed to ui_control.columns and other changes.

* $scope.columns and $scope.ui_control.colums

* Work in progress ...

* locale for feedback out

* code cleanup, removed config changes and callbacks
  • Loading branch information
Christian-Me authored and dceejay committed Nov 29, 2019
1 parent ac1e441 commit d2b00fd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
6 changes: 4 additions & 2 deletions node-red-node-ui-table/node.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
</div>
<div class="form-row node-input-column-container-row">
<label for="node-input-width" style="vertical-align:top"><i class="fa fa-list-alt"></i> Columns</label>
<span style="float:right"><span data-i18n='table.label.send'>&nbsp;</span><input type="checkbox" id="node-input-cts" style="display:inline-block; width:auto; vertical-align:top;"></span>
<span style="float:right">
<span data-i18n='table.label.send'>&nbsp;</span><input type="checkbox" id="node-input-cts" style="display:inline-block; width:auto; vertical-align:top;">
</span>
<ol id="node-input-column-container"></ol>
</div>
</script>
Expand Down Expand Up @@ -183,7 +185,7 @@
});
var that = this;
if ($("#node-input-cts").is(":checked")) { that.outputs = 1; }
else { that.outputs = 0; }
else that.outputs = 0;
},
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-input-column-container-row)");
Expand Down
95 changes: 77 additions & 18 deletions node-red-node-ui-table/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ module.exports = function (RED) {
order: config.order,
group: config.group,
forwardInputMessages: false,

beforeEmit: function (msg, value) {
return { msg: { payload: value } };
return {msg: {
payload: value,
ui_control: (msg.hasOwnProperty("ui_control")) ? msg.ui_control : undefined,
}};
},
beforeSend: function (msg, orig) {
if (orig) { return orig.msg; }
Expand All @@ -73,21 +77,35 @@ module.exports = function (RED) {
$scope.inited = false;
$scope.tabledata = [];
var tablediv;
var createTable = function(basediv, tabledata, columndata, outputs) {
var y = (columndata.length === 0) ? 25 : 32;
var opts = {
data: tabledata,
layout: 'fitColumns',
columns: columndata,
autoColumns: columndata.length == 0,
movableColumns: true,
height: tabledata.length * y + 26
}
var createTable = function(basediv, tabledata, columndata, outputs, ui_control) {
if (!ui_control || !ui_control.tabulator) {
var y = (columndata.length === 0) ? 25 : 32;
var opts = {
data: tabledata,
layout: 'fitColumns',
columns: columndata,
autoColumns: columndata.length == 0,
movableColumns: true,
height: tabledata.length * y + 26
}
} else { // configuration via ui_control
var y = (ui_control.tabulator.columns.length > 0) ? 32 : 25;
var opts = ui_control.tabulator;
opts.data = tabledata;
if (!ui_control.tabulator.layout) opts.layout = 'fitColumns';
if (!ui_control.tabulator.movableColumns) opts.movableColumns = true;
if (!ui_control.tabulator.columns) opts.columns = columndata;
if (!ui_control.tabulator.autoColumns) autoColumns = columndata.length == 0;
if (!ui_control.customHeight) opts.height= tabledata.length * y + 26;
else opts.height= ui_control.customHeight * y + 26;
} // end of configuration via ui_control

if (outputs > 0) {
opts.cellClick = function(e, cell) {
$scope.send({topic:cell.getField(),payload:cell.getData()});
};
}

var table = new Tabulator(basediv, opts);
};
$scope.init = function (config) {
Expand All @@ -97,18 +115,59 @@ module.exports = function (RED) {
if (document.querySelector(tablediv) && $scope.tabledata) {
clearInterval(stateCheck);
$scope.inited = true;
createTable(tablediv,$scope.tabledata,$scope.config.columns,$scope.config.outputs);
createTable(tablediv,$scope.tabledata,$scope.config.columns,$scope.config.outputs,$scope.config.ui_control);
$scope.tabledata = [];
}
}, 40);
}, 200); // lowest setting on my side ... still fails sometimes ;)
};
$scope.$watch('msg', function (msg) {
if (msg && msg.hasOwnProperty("ui_control") && msg.ui_control.hasOwnProperty("callback")) return; // to avoid loopback from callbacks. No better solution jet. Help needed.
if (msg && msg.hasOwnProperty("payload") && Array.isArray(msg.payload)) {
if ($scope.inited == false) {
$scope.tabledata = msg.payload;
return;
$scope.tabledata = msg.payload;
}

// configuration via ui_control
if (msg && msg.hasOwnProperty("ui_control")) {

var addValueOrFunction = function (config,param,value) {
if (typeof String.prototype.parseFunction != 'function') {
String.prototype.parseFunction = function () {
var funcReg = /function *\(([^()]*)\)[ \n\t]*{(.*)}/gmi;
var match = funcReg.exec(this.replace(/\n/g, ' '));
if(match) {
return new Function(match[1].split(','), match[2]);
}
return null;
};
}
var valueFunction;
if (typeof value === "string" && (valueFunction = value.parseFunction())) {
config[param]=valueFunction.bind($scope); // to enable this.send() for callback functions.
}
else config[param]= value;
}
createTable(tablediv,msg.payload,$scope.config.columns,$scope.config.outputs);

var addObject = function (destinationObject,sourceObject) {
for (var element in sourceObject) {
if (!destinationObject[element]) destinationObject[element]=(Array.isArray(sourceObject[element]))? [] : {};
if (typeof sourceObject[element] === "object") {
addObject(destinationObject[element],sourceObject[element])
} else {
addValueOrFunction(destinationObject,element,sourceObject[element]);
}
}
}

if (!$scope.config.ui_control) { $scope.config.ui_control={}; }

addObject($scope.config.ui_control,msg.ui_control);

} // end off configuration via ui_control

if ($scope.inited == false) {
return;
} else {
createTable(tablediv, $scope.tabledata, $scope.config.columns, $scope.config.outputs, $scope.config.ui_control);
}
});
}
Expand All @@ -134,4 +193,4 @@ module.exports = function (RED) {
};
res.sendFile(req.params[0], options)
});
};
};

0 comments on commit d2b00fd

Please sign in to comment.