Skip to content

Commit 66b2809

Browse files
Merge pull request #6 from pierina-ixpantia/T5
Adds javascript logic for clearing selection given a tableId
2 parents b877306 + 6bf6c9f commit 66b2809

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
^src/\.cargo$
2+
^.*\.Rproj$
3+
^\.Rproj\.user$

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
src/*.o
6+
src/*.so
7+
src/*.dll

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spyctable
22
Title: What the Package Does (One Line, Title Case)
3-
Version: 0.2.0
3+
Version: 0.3.0
44
Authors@R:
55
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
66
comment = c(ORCID = "YOUR-ORCID-ID"))

inst/table/index.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ spyCTableBinding.find = function(scope) {
44
return $(scope).find(".spyctable");
55
}
66

7+
var globalSpyCTableIndex = new Map();
8+
var spyCTableSelectionBuffer = new Array();
9+
710
// Its its true then it's dragging
811
var is_dragging = false;
912

@@ -16,18 +19,23 @@ function enable_dragging() {
1619
}
1720

1821
function selected_deselected(el) {
19-
const selection = el.selection;
22+
// This is a pointer to the selection array
23+
const selection = globalSpyCTableIndex.get(el.tableId);
2024
let is_selected = el.classList.contains('selected');
2125
if (is_selected) {
22-
selection.delete(el.coords);
26+
selection.delete(el);
2327
el.classList.remove("bg-primary");
2428
el.classList.remove("selected");
2529
} else {
26-
selection.add(el.coords);
30+
selection.add(el);
2731
el.classList.add("bg-primary");
2832
el.classList.add("selected");
2933
}
30-
Shiny.setInputValue(el.inputId, Array.from(selection))
34+
spyCTableSelectionBuffer.length = 0;
35+
for (const element of selection) {
36+
spyCTableSelectionBuffer.push(element.coords);
37+
}
38+
Shiny.setInputValue(el.inputId, spyCTableSelectionBuffer);
3139
}
3240

3341
function mouse_over_event() {
@@ -40,13 +48,27 @@ function mouse_down_event() {
4048
selected_deselected(this)
4149
}
4250

51+
// This function is to deselect everything in the table
52+
function spyctable_deselect_all(tableId) {
53+
const selection = globalSpyCTableIndex.get(tableId);
54+
if (selection !== undefined) {
55+
for (const element of selection) {
56+
element.classList.remove("bg-primary");
57+
element.classList.remove("selected");
58+
}
59+
selection.clear();
60+
spyCTableSelectionBuffer.length = 0;
61+
Shiny.setInputValue(el.inputId, spyCTableSelectionBuffer);
62+
}
63+
}
64+
4365
// If anywhere on the page the mouseup event is found
4466
// then we disable dragging
4567
addEventListener("mouseup", (_event) => {
4668
disable_dragging();
4769
});
4870

49-
function build_tbody(selection, inputId, len_x, len_y, data, keys) {
71+
function build_tbody(tableId, inputId, len_x, len_y, data, keys) {
5072
var tbody = document.createElement("tbody");
5173

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

135+
var selection = globalSpyCTableIndex.get(id);
136+
if (selection === undefined) {
137+
selection = new Set();
138+
globalSpyCTableIndex.set(id, selection);
139+
}
112140
let data = msg.data;
113141
let thead_content = msg.thead;
114-
el.selection = new Set();
115142
let keys = Object.keys(data);
116143
let len_x = keys.length;
117144
let len_y = data[keys[0]].length;
118145
var table = document.createElement("table");
119146
table.classList.add("table");
120147
table.id = id + '_inner_table';
121148
table.appendChild(fromHTML(thead_content));
122-
table.appendChild(build_tbody(el.selection, inputId, len_x, len_y, data, keys));
149+
table.appendChild(build_tbody(id, inputId, len_x, len_y, data, keys));
123150
el.appendChild(table);
124151

125152
let scroll_y = el.getAttribute("scroll-y");

spyctable.Rproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
BuildType: Package
16+
PackageUseDevtools: Yes
17+
PackageInstallArgs: --no-multiarch --with-keep.source

0 commit comments

Comments
 (0)