Skip to content

Commit

Permalink
Merge pull request #6 from pierina-ixpantia/T5
Browse files Browse the repository at this point in the history
Adds javascript logic for clearing selection given a tableId
  • Loading branch information
andyquinterom authored Jul 16, 2024
2 parents b877306 + 6bf6c9f commit 66b2809
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
^src/\.cargo$
^.*\.Rproj$
^\.Rproj\.user$
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
src/*.o
src/*.so
src/*.dll
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: spyctable
Title: What the Package Does (One Line, Title Case)
Version: 0.2.0
Version: 0.3.0
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Expand Down
43 changes: 35 additions & 8 deletions inst/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ spyCTableBinding.find = function(scope) {
return $(scope).find(".spyctable");
}

var globalSpyCTableIndex = new Map();
var spyCTableSelectionBuffer = new Array();

// Its its true then it's dragging
var is_dragging = false;

Expand All @@ -16,18 +19,23 @@ function enable_dragging() {
}

function selected_deselected(el) {
const selection = el.selection;
// This is a pointer to the selection array
const selection = globalSpyCTableIndex.get(el.tableId);
let is_selected = el.classList.contains('selected');
if (is_selected) {
selection.delete(el.coords);
selection.delete(el);
el.classList.remove("bg-primary");
el.classList.remove("selected");
} else {
selection.add(el.coords);
selection.add(el);
el.classList.add("bg-primary");
el.classList.add("selected");
}
Shiny.setInputValue(el.inputId, Array.from(selection))
spyCTableSelectionBuffer.length = 0;
for (const element of selection) {
spyCTableSelectionBuffer.push(element.coords);
}
Shiny.setInputValue(el.inputId, spyCTableSelectionBuffer);
}

function mouse_over_event() {
Expand All @@ -40,13 +48,27 @@ function mouse_down_event() {
selected_deselected(this)
}

// This function is to deselect everything in the table
function spyctable_deselect_all(tableId) {
const selection = globalSpyCTableIndex.get(tableId);
if (selection !== undefined) {
for (const element of selection) {
element.classList.remove("bg-primary");
element.classList.remove("selected");
}
selection.clear();
spyCTableSelectionBuffer.length = 0;
Shiny.setInputValue(el.inputId, spyCTableSelectionBuffer);
}
}

// If anywhere on the page the mouseup event is found
// then we disable dragging
addEventListener("mouseup", (_event) => {
disable_dragging();
});

function build_tbody(selection, inputId, len_x, len_y, data, keys) {
function build_tbody(tableId, inputId, len_x, len_y, data, keys) {
var tbody = document.createElement("tbody");

// If the user clicks then we enable dragging
Expand All @@ -68,7 +90,8 @@ function build_tbody(selection, inputId, len_x, len_y, data, keys) {
current_cel.coords = [c, i];
current_cel.innerText = data[keys[c]][i];
current_cel.classList.add("user-select-none");
current_cel.selection = selection;
//We passed the pointer to every single cell
current_cel.tableId = tableId;
current_cel.onmouseover = mouse_over_event;
current_cel.onmousedown = mouse_down_event;
current_cel.inputId = inputId;
Expand Down Expand Up @@ -109,17 +132,21 @@ spyCTableBinding.renderValue = function(el, msg) {
Shiny.setInputValue(inputId, new Array())
}

var selection = globalSpyCTableIndex.get(id);
if (selection === undefined) {
selection = new Set();
globalSpyCTableIndex.set(id, selection);
}
let data = msg.data;
let thead_content = msg.thead;
el.selection = new Set();
let keys = Object.keys(data);
let len_x = keys.length;
let len_y = data[keys[0]].length;
var table = document.createElement("table");
table.classList.add("table");
table.id = id + '_inner_table';
table.appendChild(fromHTML(thead_content));
table.appendChild(build_tbody(el.selection, inputId, len_x, len_y, data, keys));
table.appendChild(build_tbody(id, inputId, len_x, len_y, data, keys));
el.appendChild(table);

let scroll_y = el.getAttribute("scroll-y");
Expand Down
17 changes: 17 additions & 0 deletions spyctable.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source

0 comments on commit 66b2809

Please sign in to comment.