Skip to content

Commit

Permalink
Add r-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 6, 2024
1 parent 25aefbe commit 9f953da
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build": "esbuild srcjs/index.js --bundle --minify --outfile=pytabulator/srcjs/tabulator-bindings.js",
"build-dev": "esbuild srcjs/index.js --bundle --outfile=pytabulator/srcjs/tabulator-bindings.js",
"prettier": "prettier srcjs --write",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build-r-dev": "esbuild srcjs/index-r.js --bundle --outfile=r-bindings/rtabulator.js"
},
"author": "Stefan Kuethe",
"license": "MIT"
Expand Down
94 changes: 94 additions & 0 deletions r-bindings/rtabulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
(() => {
// srcjs/events.js
function addEventListeners(table, el) {
table.on("rowClick", function(e, row) {
const inputName = `${el.id}_row_clicked`;
console.log(inputName, row.getData());
Shiny.onInputChange(inputName, row.getData());
});
table.on("rowClick", (e, row) => {
const inputName = `${el.id}_rows_selected`;
const data = table.getSelectedRows().map((row2) => row2.getData());
console.log(inputName, data);
Shiny.onInputChange(inputName, data);
});
table.on("cellEdited", function(cell) {
const inputName = `${el.id}_row_edited`;
console.log(inputName, cell.getData());
Shiny.onInputChange(inputName, cell.getData());
});
table.on("dataFiltered", function(filters, rows) {
const inputName = `${el.id}_data_filtered`;
const data = rows.map((row) => row.getData());
console.log(inputName, data);
Shiny.onInputChange(inputName, data);
});
}

// srcjs/widget.js
function run_calls(el, table, calls) {
calls.forEach(([method_name, options]) => {
if (method_name === "getData") {
console.log("custom call");
Shiny.onInputChange(`${el.id}_data`, table.getData());
return;
}
if (method_name === "deleteSelectedRows") {
console.log("custom call");
const rows = table.getSelectedRows();
rows.forEach((row) => {
console.log(row.getIndex());
table.deleteRow(row.getIndex());
});
return;
}
console.log(method_name, options);
table[method_name](...options);
});
}
var TabulatorWidget = class {
constructor(container, data, options) {
options.data = data;
this._container = container;
console.log("columns", options.columns);
if (options.columns == null)
options.autoColumns = true;
this._table = new Tabulator(this._container, options);
if (typeof Shiny === "object") {
addEventListeners(this._table, this._container);
this._addShinyMessageHandler();
}
}
_addShinyMessageHandler() {
const messageHandlerName = `tabulator-${this._container.id}`;
Shiny.addCustomMessageHandler(messageHandlerName, (payload) => {
console.log(payload);
run_calls(this._container, this._table, payload.calls);
});
}
getTable() {
return this._table;
}
};

// srcjs/index-r.js
function tabulatorFactory(widgetElement, width, height) {
let table = null;
function renderValue(payload) {
console.log(payload);
if (payload.options === null) {
payload.options = {};
}
const widget = new TabulatorWidget(widgetElement, payload.data, payload.options);
table = widget.getTable();
}
function resize(width2, height2) {
}
return { renderValue, resize };
}
HTMLWidgets.widget({
name: "rtabulator",
type: "output",
factory: tabulatorFactory
});
})();
26 changes: 26 additions & 0 deletions srcjs/index-r.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TabulatorWidget } from "./widget";

function tabulatorFactory(widgetElement, width, height) {
let table = null;

function renderValue(payload) {
console.log(payload);
if (payload.options === null) {
payload.options = {};
}
const widget = new TabulatorWidget(widgetElement, payload.data, payload.options);
table = widget.getTable();
}

function resize(width, height) {
// not implemented yet
}

return { renderValue, resize };
}

HTMLWidgets.widget({
name: "rtabulator",
type: "output",
factory: tabulatorFactory,
});

0 comments on commit 9f953da

Please sign in to comment.