I am trying to implement a function to different modules but while trying to do so the functionality is not working. But if I use the same in a module it's working perfectly.
The R files that I have:
app.R
library(shiny)
purrr::walk(dir('module_R', pattern = '.R', full.names = T), source)
ui <- fluidPage(
tabsetPanel(
tabPanel("Tab1",
tab1_UI("x1")),
tabPanel("Tab2",
tab2_UI("x2"))
)
)
server <- function(input, output, session) {
tab1_Server("x1")
tab2_Server("x2")
}
shinyApp(ui, server)
mod1.R
tab1_UI <- function(id) {
ns <- NS(id)
tagList(
expand_text(text())
)
}
tab1_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
})
}
mod2.R
tab2_UI <- function(id) {
ns <- NS(id)
tagList(
expand_text(text())
)
}
tab2_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
})
}
utils.R
JS <- '
$(document).ready(function() {
// Configure/customize these variables.
var showChar = 128; // How many characters are shown by default
var ellipsestext = "...";
var moretext = "Show more";
var lesstext = "Show less";
$(".more").each(function() {
var content = $(this).html();
if(content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var html = c + "<span class=\\\"moreellipses\\\">" + ellipsestext
+ " </span><span class=\\\"morecontent\\\"><span>" + h
+ "</span> <a href=\\\"\\\" class=\\\"morelink\\\">"
+ moretext + "</a></span>";
$(this).html(html);
}
});
$(".morelink").click(function(){
if($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
} else {
$(this).addClass("less");
$(this).html(lesstext);
}
$(this).parent().prev().toggle();
$(this).prev().toggle();
return false;
});
});
'
CSS <- "
.morecontent span {
display: none;
}
.morelink {
display: block;
}
"
expand_text <- function(text) {
tagList(
tags$head(
tags$script(HTML(JS)),
tags$style(HTML(CSS))
),
tags$div(
tags$span(class = "more",
text)
)
)
}
text <- function(){
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
The 'Show more'/ 'Show less' functionality is not when I am using expand_text() function in both modules but it's working perfectly while I am using in any one of modules.
What am I doing wrong here?
Thanks in advance.