Skip to content

Commit

Permalink
Simplify DT callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jthompson-arcus committed Dec 13, 2024
1 parent 0bb7ee6 commit 05084db
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
53 changes: 3 additions & 50 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,9 @@ shiny::registerInputHandler('CS.reviewInfo', function(val, ...) {
))
}, TRUE)

checkbox_callback <- DT::JS(
"table.on('column-reorder', function() {",
"table.rows().every(function() {",
"if (this.data()[0].reviewed == null) {",
"$(':checkbox', this.node()).",
"addClass('indeterminate').",
"prop('indeterminate', this.data()[0].updated == null).",
"prop('readOnly', this.data()[0].updated == false)",
"}",
"})",
"});",
"table.on('click', 'input[type=\"checkbox\"]', function(){",
"var tblId = $(this).closest('.datatables').attr('id');",
"var cell = table.cell($(this).closest('td'));",
"var review = $(this).is(':indeterminate') ? null : $(this).is(':checked');",
"cell.data().updated = review;",
"var info = {review: review, ids: cell.data().ids, row_id: cell.data().row_id};",
"Shiny.setInputValue(tblId + '_review_selection:CS.reviewInfo', info, {priority: 'event'});",
"});"
)

checkbox_render <- DT::JS(
"function(data, type, row, meta) {",
"var reviewed = data.reviewed;",
"var updated = data.updated;",
"var disabled = data.disabled;",
"var cb_class = ''",
"if (reviewed == null) {",
"cb_class = updated == null ? '' : 'indeterminate'",
"} else {",
"cb_class = reviewed ? 'checked' : 'unchecked'",
"}",
"return `<input type='checkbox' ",
"${disabled ? 'disabled ' : ''}",
"class='${cb_class}' ",
"${updated == null ? (reviewed ? 'checked' : '') : (updated ? 'checked' : '')} ",
"${reviewed == null ? 'onclick=\"ts(this)\"' : ''}/>`;",
"}"
)

row_callback <- DT::JS(
"function(row, data) {",
"if (data[0].reviewed == null) {",
"$(':checkbox', row).",
"addClass('indeterminate').",
"prop('indeterminate', data[0].updated == null).",
"prop('readOnly', data[0].updated == false)",
"}",
"}"
)
checkbox_callback <- DT::JS("checkboxCallback(table);")
checkbox_render <- DT::JS("checkboxRender")
row_callback <- DT::JS("rowCallback")

progress_bar <- function(outputId) {
div(
Expand Down
48 changes: 48 additions & 0 deletions inst/app/www/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,54 @@ function ts(cb) {
}
}

function checkboxCallback(table) {
table.on('column-reorder', function() {
table.rows().every(function() {
if (this.data()[0].reviewed == null) {
$(':checkbox', this.node())
.addClass('indeterminate')
.prop('indeterminate', this.data()[0].updated == null)
.prop('readOnly', this.data()[0].updated == false)
}
})
});
table.on('click', 'input[type="checkbox"]', function(){
var tblId = $(this).closest('.datatables').attr('id');
var cell = table.cell($(this).closest('td'));
var review = $(this).is(':indeterminate') ? null : $(this).is(':checked');
cell.data().updated = review;
var info = {review: review, ids: cell.data().ids, row_id: cell.data().row_id};
Shiny.setInputValue(tblId + '_review_selection:CS.reviewInfo', info, {priority: 'event'});
})
return table;
}

function checkboxRender(data, type, row, meta) {
var reviewed = data.reviewed;
var updated = data.updated;
var disabled = data.disabled;
var cb_class = ''
if (reviewed == null) {
cb_class = updated == null ? '' : 'indeterminate'
} else {
cb_class = reviewed ? 'checked' : 'unchecked'
}
return `<input type='checkbox'
${disabled ? 'disabled ' : ''}
class='${cb_class}'
${updated == null ? (reviewed ? 'checked' : '') : (updated ? 'checked' : '')}
${reviewed == null ? 'onclick="ts(this)"' : ''}/>`;
}

function rowCallback(row, data) {
if (data[0].reviewed == null) {
$(':checkbox', row)
.addClass('indeterminate')
.prop('indeterminate', data[0].updated == null)
.prop('readOnly', data[0].updated == false)
}
}

$(document).ready(function() {

/* Define custom Shiny input binding for overall review checkbox.
Expand Down

0 comments on commit 05084db

Please sign in to comment.