Skip to content

Commit

Permalink
updated calendar-pro to 3.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Feb 4, 2025
1 parent 030f87c commit d6f43ad
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 247 deletions.
167 changes: 88 additions & 79 deletions R/calendar-pro-input.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
html_dependency_calendar_pro <- function() {
htmlDependency(
name = "calendar-pro",
version = "2.9.10",
version = "3.0.3",
src = c(file = system.file("packer", package = "shinyWidgets")),
script = "calendar-pro.js",
all_files = FALSE
Expand All @@ -18,33 +18,35 @@ html_dependency_calendar_pro <- function() {
#'
#' @inheritParams shiny::selectInput
#' @param value Initial value.
#' @param type Determines the type of calendar displayed and the selection process: 'default' | 'multiple' | 'range' | 'month' | 'year'.
#' @param min This parameter sets the minimum date that the user can choose. Dates earlier than the specified date will be disabled and not available for selection.
#' @param max This parameter sets the maximum date that the user can choose. Dates later than the specified date will be disabled and not available for selection.
#' @param disablePast This parameter disables all past days.
#' @param disableAllDays This parameter disables all days and can be useful when using `enable` is set.
#' @param disableWeekday This parameter allows you to disable specified weekdays. Specify an array with numbers, where each number represents a day of the week. For example, `0` is Sunday.
#' @param disableGaps This parameter disables the selection of days within a range with disabled dates. It only works when `type = "range"`.
#' @param disabled This parameter allows you to disable specific dates regardless of the specified range.
#' @param enabled This parameter allows you to enable specific dates regardless of the range and disabled dates.
#' @param months The months parameter specifies the number of displayed months when the calendar type is set to 'multiple'.
#' @param jumpMonths The jumpMonths parameter controls the number of months to jump.
#' @param jumpToSelectedDate When the option is enabled and 1 or more selected date(s) are provided but without providing
#' @param mode This parameter determines whether selecting one or multiple days is allowed, or if date selection is completely disabled.
#' Possible values are: 'single' | 'multiple' | 'multiple-ranged' | false.
#' @param type Determines the type of calendar displayed and the selection process: 'default' | 'multiple' | 'month' | 'year'.
#' @param displayDateMin This parameter sets the minimum date that the user can choose. Dates earlier than the specified date will be disabled and not available for selection.
#' @param displayDateMax This parameter sets the maximum date that the user can choose. Dates later than the specified date will be disabled and not available for selection.
#' @param firstWeekday This parameter sets the first day of the week. Specify a number from 0 to 6, where the number represents the day of the week identifier.
#' According to JS standards, the days of the week start with 0, and 0 is Sunday.
#' @param disableDatesPast This parameter disables all past days.
#' @param disableAllDates This parameter disables all days and can be useful when using `enableDates` is set.
#' @param disableWeekdays This parameter allows you to disable specified weekdays. Specify an array with numbers, where each number represents a day of the week. For example, `0` is Sunday.
#' @param disableDatesGaps This parameter disables the selection of days within a range with disabled dates. It only works when `mode = "multiple-ranged"`.
#' @param disableDates This parameter allows you to disable specific dates regardless of the specified range.
#' @param enableDates This parameter allows you to enable specific dates regardless of the range and disabled dates.
#' @param displayMonthsCount The months parameter specifies the number of displayed months when the calendar type is set to 'multiple'.
#' @param enableJumpToSelectedDate When the option is enabled and 1 or more selected date(s) are provided but without providing
#' selected.month and selected.year, it will make the calendar jump to the first selected date. If set to false,
#' the calendar will always open to the current month and year.
#' @param toggleSelected If toggleSelected parameter is true then clicking on the active cell will remove the selection from it.
#' @param weekNumbers With this parameter, you can decide whether to display week numbers in the calendar.
#' @param weekNumbersSelect If `TRUE` select the week when week number is clicked.
#' @param weekend This parameter allows you to highlight weekends in the calendar.
#' @param time This parameter enables time selection. You can also specify the time format using a boolean value or a number: 24-hour or 12-hour format.
#' @param timeValue Initial time value.
#' @param enableDateToggle If true then clicking on a selected date again will deselect it.
#' @param enableWeekNumbers With this parameter, you can decide whether to display week numbers in the calendar.
#' @param selectWeekNumbers If `TRUE` select the week when week number is clicked.
#' @param selectionTimeMode This parameter enables time selection. You can also specify the time format using a boolean value or a number: 24-hour or 12-hour format.
#' @param selectedTime Initial time value.
#' @param ... Other settings passed to Slim Select JAvaScript method.
#' @param format Format to use when displaying date in input field, if an initial value is provided it must be a date so that the format apply.
#' @param positionToInput This parameter specifies the position of the calendar relative to input,
#' if the calendar is initialized with the input parameter. Possible values: 'auto' | 'center' | 'left' | 'right' | c('bottom' | 'top', 'center' | 'left' | 'right')
#' @param theme This parameter determines the theme of the calendar : 'light' or 'dark'.
#' @param selectedTheme This parameter determines the theme of the calendar : 'light' or 'dark'.
#' @param placeholder A character string giving the user a hint as to what can be entered into the control.
#' @param input If `TRUE` (default), use an input and render calendar in a dropdown, otherwise calendar is rendered in the page.
#' @param inputMode If `TRUE` (default), use an input and render calendar in a dropdown, otherwise calendar is rendered in the page.
#' @param inline Display calendar container inline.
#' @param parseValue Convert input value to date/datetime in server or not.
#'
Expand All @@ -60,84 +62,91 @@ html_dependency_calendar_pro <- function() {
calendarProInput <- function(inputId,
label,
value = NULL,
type = c("default", "multiple", "range", "month", "year"),
min = NULL,
max = NULL,
disablePast = FALSE,
disableAllDays = FALSE,
disableWeekday = NULL,
disableGaps = FALSE,
disabled = NULL,
enabled = NULL,
months = 2,
jumpMonths = 1,
jumpToSelectedDate = FALSE,
toggleSelected = TRUE,
weekNumbers = FALSE,
weekNumbersSelect = FALSE,
weekend = TRUE,
time = NULL,
timeValue = NULL,
mode = c("single", "multiple", "multiple-ranged", "false"),
type = c("default", "multiple", "month", "year"),
displayDateMin = NULL,
displayDateMax = NULL,
firstWeekday = NULL,
disableDatesPast = FALSE,
disableAllDates = FALSE,
disableWeekdays = NULL,
disableDatesGaps = FALSE,
disableDates = NULL,
enableDates = NULL,
displayMonthsCount = 1,
enableJumpToSelectedDate = FALSE,
enableDateToggle = TRUE,
enableWeekNumbers = FALSE,
selectWeekNumbers = FALSE,
selectionTimeMode = NULL,
selectedTime = NULL,
...,
format = "%Y-%m-%d",
positionToInput = "auto",
theme = "light",
selectedTheme = "light",
placeholder = NULL,
input = TRUE,
inputMode = TRUE,
inline = FALSE,
parseValue = TRUE,
width = NULL) {
# selected <- restoreInput(id = inputId, default = selected)
mode <- match.arg(mode)
type <- match.arg(type)
if (type == "multiple") {
stopifnot(
"calendarProInput: when type='multiple', displayMonthsCount shoulb be between 2 and 12." = displayMonthsCount >= 2 | displayMonthsCount <= 12
)
}
parseValue <- if (isTRUE(parseValue)) {
if (type %in% c("month", "year")) {
"calendarPro.monthyear"
if (type == "month") {
"calendarPro.month"
} else if (type == "year") {
"calendarPro.year"
} else {
"calendarPro.date"
}
} else {
"calendarPro.raw"
}
if (!is.null(selectionTimeMode)) {
if (is.null(selectedTime)) {
selectedTime <- format(value, format = "%H:%M")
} else {
value <- as.POSIXct(paste(format(value, format = "%Y-%m-%d"), selectedTime))
}
}
options <- dropNulls(list(
selectedDates = list1(format(value, format = "%Y-%m-%d")),
inputMode = inputMode,
selectionDatesMode = mode,
type = type,
displayMonthsCount = displayMonthsCount,
enableJumpToSelectedDate = enableJumpToSelectedDate,
enableDateToggle = enableDateToggle,
positionToInput = positionToInput,
enableWeekNumbers = enableWeekNumbers,
selectedTheme = selectedTheme,
displayDateMin = displayDateMin,
displayDateMax = displayDateMax,
disableDatesPast = disableDatesPast,
disableDatesGaps = disableDatesGaps,
disableWeekdays = list1(disableWeekdays),
disableAllDates = disableAllDates,
enableDates = list1(enableDates),
disableDates = list1(disableDates),
selectionTimeMode = selectionTimeMode,
selectedTime = selectedTime,
firstWeekday = firstWeekday
))
options <- modifyList(options, list(...))
config <- list(
type = if (type == "range") "multiple" else type,
months = months,
jumpMonths = jumpMonths,
jumpToSelectedDate = jumpToSelectedDate,
toggleSelected = toggleSelected,
weekNumbersSelect = weekNumbersSelect,
options = options,
selectWeekNumbers = selectWeekNumbers,
parseValue = parseValue,
format = to_dayjs_fmt(format)
)
config$input <- input
config$settings$selection$time <- time
if (!is.null(time)) {
if (is.null(timeValue)) {
timeValue <- format(value, format = "%H:%M")
} else {
value <- as.POSIXct(paste(format(value, format = "%Y-%m-%d"), timeValue))
}
}
config$settings$selected$time <- timeValue
if (!is.null(value))
config$settings$selected$dates <- list1(format(value, format = "%Y-%m-%d"))
if (type == "multiple")
config$settings$selection$day <- "multiple"
if (type == "range")
config$settings$selection$day <- "multiple-ranged"
config$settings$range$min <- min
config$settings$range$max <- max
config$settings$range$disablePast <- disablePast
config$settings$range$disableAllDays <- disableAllDays
config$settings$range$disableWeekday <- list1(disableWeekday)
config$settings$range$disableGaps <- disableGaps
config$settings$range$disabled <- list1(disabled)
config$settings$range$enabled <- list1(enabled)
config$settings$visibility$theme <- theme
config$settings$visibility$weekNumbers <- weekNumbers
config$settings$visibility$weekend <- weekend
config$settings$visibility$positionToInput <- positionToInput
config <- modifyList(config, list(...))
tag_el <- if (isTRUE(input)) {

tag_el <- if (isTRUE(inputMode)) {
tags$input(
type = "text",
class = "form-control calendar-pro-element",
Expand Down
30 changes: 27 additions & 3 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
shiny::registerInputHandler("calendarPro.date", function(data, ...) {
if (length(data) < 1 || length(data$selectedDates) < 1)
return(NULL)
if (!is.null(data$selectedTime)) {
if (identical(data$selectedDates[[1]], "NULL"))
return(NULL)
if (!is.null(data$selectedTime) & !identical(data$selectedDates[[1]], "NULL")) {
fmt <- if (grepl(pattern = "(A|P)M", x = data$selectedTime)) {
"%Y-%m-%d %I:%M %p"
} else {
Expand All @@ -89,8 +91,10 @@
res
}
}, force = TRUE)
shiny::registerInputHandler("calendarPro.monthyear", function(data, ...) {
if (length(data) < 1)
shiny::registerInputHandler("calendarPro.month", function(data, ...) {
if (!is.list(data))
return(NULL)
if (length(data$selectedMonth) < 1)
return(NULL)
res <- try(as.Date(
paste(
Expand All @@ -107,6 +111,26 @@
res
}
}, force = TRUE)
shiny::registerInputHandler("calendarPro.year", function(data, ...) {
if (!is.list(data))
return(NULL)
if (length(data$selectedYear) < 1)
return(NULL)
res <- try(as.Date(
paste(
data$selectedYear,
"01",
"01",
sep = "-"
)
), silent = TRUE)
if (inherits(res, "try-error")) {
warning("calendarProInput: Failed to parse dates, try using parseValue = FALSE", call. = FALSE)
data
} else {
res
}
}, force = TRUE)
shiny::registerInputHandler("calendarPro.raw", function(data, ...) {
if (length(data) < 1)
return(NULL)
Expand Down
23 changes: 12 additions & 11 deletions examples/calendar-pro.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ ui <- fluidPage(
calendarProInput(
inputId = "cal5",
label = "Calendar without input field:",
input = FALSE,
width = "100%"
inputMode = FALSE,
width = "300px"
),
verbatimTextOutput("res5"),
calendarProInput(
inputId = "cal7",
label = "Calendar with week numbers:",
placeholder = "Select a date",
weekNumbers = TRUE,
enableWeekNumbers = TRUE,
width = "100%"
),
verbatimTextOutput("res7")
Expand All @@ -44,33 +44,34 @@ ui <- fluidPage(
calendarProInput(
inputId = "cal2",
label = "Calendar with multiple selection:",
type = "multiple",
mode = "multiple",
placeholder = "Select multiple dates",
width = "100%"
),
verbatimTextOutput("res2"),
calendarProInput(
inputId = "cal4",
label = "Calendar with range selection:",
type = "range",
mode = "multiple-ranged",
width = "100%"
),
verbatimTextOutput("res4"),
calendarProInput(
inputId = "cal6",
label = "Calendar (range) without input field:",
type = "range",
months = 3,
input = FALSE,
mode = "multiple-ranged",
type = "multiple",
displayMonthsCount = 2,
inputMode = FALSE,
width = "100%"
),
verbatimTextOutput("res6"),
calendarProInput(
inputId = "cal8",
label = "Calendar select week:",
type = "range",
weekNumbers = TRUE,
weekNumbersSelect = TRUE,
mode = "multiple-ranged",
enableWeekNumbers = TRUE,
selectWeekNumbers = TRUE,
width = "100%"
),
verbatimTextOutput("res8")
Expand Down
Loading

0 comments on commit d6f43ad

Please sign in to comment.