Skip to content

Commit

Permalink
calendar: replaced moment by daysjs
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Aug 22, 2021
1 parent f7b5b08 commit d71190a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 54 deletions.
12 changes: 6 additions & 6 deletions R/calendar-demo-data.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

#' @title Calendar demo data
#'
#'
#' @description Create calendar demo data for schedules and properties
#'
#' @param view Calendar view for which to use the data.
#'
#' @return a \code{data.frame}.
#' @export
#'
#'
#' @name cal-demo-data
#'
#' @examples
#'
#' # Monthly schedule
#' cal_demo_data("month")
#'
#'
#' #' # Weekly schedule
#' cal_demo_data("week")
cal_demo_data <- function(view = c("month", "week", "day")) {
Expand Down Expand Up @@ -45,7 +45,7 @@ cal_demo_data <- function(view = c("month", "week", "day")) {
list(
calendarId = 1, title = "Week-end", body = "Week-end with friends", recurrenceRule = NA,
start = as.character(get_day_month("6")[2]),
end = as.character(get_day_month("7")[2]),
end = as.character(get_day_month("6")[2] + 1),
category = "allday", location = NA,
bgColor = NA, color = NA, borderColor = NA
),
Expand Down Expand Up @@ -196,7 +196,7 @@ get_day_week <- function(day) {


#' @export
#'
#'
#' @rdname cal-demo-data
cal_demo_props <- function() {
props <- list(
Expand All @@ -218,7 +218,7 @@ cal_demo_props <- function() {
id = 3,
name = "COURSES",
color = "#000",
bgColor = "#c6e2f6",
bgColor = "#c6e2f6",
borderColor = "#3b7cc6"
)
)
Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/calendar.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions inst/htmlwidgets/calendar.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,3 @@
/*!*********************************************************************************************************************************!*\
!*** external {"commonjs":"tui-date-picker","commonjs2":"tui-date-picker","amd":"tui-date-picker","root":["tui","DatePicker"]} ***!
\*********************************************************************************************************************************/

//! moment.js

//! moment.js locale configuration
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"devDependencies": {
"@toast-ui/chart": "^4.3.6",
"css-loader": "^6.2.0",
"dayjs": "^1.10.6",
"moment": "^2.29.1",
"prettier": "^2.3.2",
"prettier-webpack-plugin": "^1.2.0",
Expand Down
13 changes: 13 additions & 0 deletions srcjs/modules/calendar-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dayjs from "dayjs";

export function formatDateNav(calendar, fmt = "YYYY-MM-DD", sep = " - ") {
var start = calendar.getDateRangeStart()._date;
start = dayjs(start).subtract(7, "day").endOf("month");
start = start.add(1, "day");
start = start.format(fmt);
var end = calendar.getDateRangeEnd()._date;
end = dayjs(end).add(7, "day").startOf("month");
end = end.subtract(1, "day");
end = end.format(fmt);
return start + sep + end;
}
8 changes: 4 additions & 4 deletions srcjs/modules/proxy-calendar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "widgets";
import moment from "moment";
import dayjs from "dayjs";
import * as utils from "./utils";

export function ProxyCalendar() {
Expand All @@ -20,9 +20,9 @@ export function ProxyCalendar() {
cal.setDate(obj.data.date);
}
Shiny.setInputValue(obj.id + "_dates", {
current: moment(cal.getDate()._date).format(),
start: moment(cal.getDateRangeStart()._date).format(),
end: moment(cal.getDateRangeEnd()._date).format(),
current: dayjs(cal.getDate()._date).format(),
start: dayjs(cal.getDateRangeStart()._date).format(),
end: dayjs(cal.getDateRangeEnd()._date).format(),
});
}
});
Expand Down
68 changes: 29 additions & 39 deletions srcjs/widgets/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import Calendar from "tui-calendar";
import "tui-calendar/dist/tui-calendar.css";
import "tui-date-picker/dist/tui-date-picker.css";
import "tui-time-picker/dist/tui-time-picker.css";
import moment from "moment";
import dayjs from "dayjs";

import { ProxyCalendar } from "../modules/proxy-calendar";

import { formatDateNav } from "../modules/calendar-utils";

HTMLWidgets.widget({
name: "calendar",

Expand Down Expand Up @@ -45,10 +47,7 @@ HTMLWidgets.widget({
if (x.useNav) {
formatNav = x.bttnOpts.fmt_date;
renderRange = document.getElementById(el.id + "_renderRange");
renderRange.innerHTML =
moment(cal.getDateRangeStart()._date).format(formatNav) +
" - " +
moment(cal.getDateRangeEnd()._date).format(formatNav);
renderRange.innerHTML = formatDateNav(cal, formatNav);

var prev = document.getElementById(el.id + "_prev");
prev.className += x.bttnOpts.class;
Expand Down Expand Up @@ -90,8 +89,8 @@ HTMLWidgets.widget({
{
title: event.title,
location: event.location,
start: moment(event.start._date).format(),
end: moment(event.end._date).format(),
start: dayjs(event.start._date).format(),
end: dayjs(event.end._date).format(),
isAllDay: event.isAllDay,
category: event.isAllDay ? "allday" : "time",
calendarId: event.calendarId,
Expand All @@ -107,8 +106,8 @@ HTMLWidgets.widget({
Shiny.setInputValue(el.id + "_add", {
title: event.title,
location: event.location,
start: moment(event.start._date).format(),
end: moment(event.end._date).format(),
start: dayjs(event.start._date).format(),
end: dayjs(event.end._date).format(),
isAllDay: event.isAllDay,
category: event.isAllDay ? "allday" : "time",
calendarId: event.calendarId,
Expand Down Expand Up @@ -146,8 +145,8 @@ HTMLWidgets.widget({
id: schedule.id,
title: schedule.title,
location: schedule.location,
start: moment(schedule.start._date).format(),
end: moment(schedule.end._date).format(),
start: dayjs(schedule.start._date).format(),
end: dayjs(schedule.end._date).format(),
isAllDay: schedule.isAllDay,
category: schedule.isAllDay ? "allday" : "time",
calendarId: schedule.calendarId,
Expand All @@ -164,18 +163,18 @@ HTMLWidgets.widget({
var changes = event.changes;
//cal.updateSchedule(schedule.id, schedule.calendarId, changes);
if (changes.hasOwnProperty("end")) {
changes.end = moment(changes.end._date).format();
changes.end = dayjs(changes.end._date).format();
}
if (changes.hasOwnProperty("start")) {
changes.start = moment(changes.start._date).format();
changes.start = dayjs(changes.start._date).format();
}
Shiny.setInputValue(el.id + "_update", {
schedule: {
id: schedule.id,
title: schedule.title,
location: schedule.location,
start: moment(schedule.start._date).format(),
end: moment(schedule.end._date).format(),
start: dayjs(schedule.start._date).format(),
end: dayjs(schedule.end._date).format(),
isAllDay: schedule.isAllDay,
category: schedule.isAllDay ? "allday" : "time",
calendarId: schedule.calendarId,
Expand All @@ -202,9 +201,9 @@ HTMLWidgets.widget({

if (HTMLWidgets.shinyMode) {
Shiny.setInputValue(el.id + "_dates", {
current: moment(cal.getDate()._date).format(),
start: moment(cal.getDateRangeStart()._date).format(),
end: moment(cal.getDateRangeEnd()._date).format(),
current: dayjs(cal.getDate()._date).format(),
start: dayjs(cal.getDateRangeStart()._date).format(),
end: dayjs(cal.getDateRangeEnd()._date).format(),
});
}
},
Expand All @@ -216,44 +215,35 @@ HTMLWidgets.widget({
clickPrev: function (event) {
if (cal !== null) {
cal.prev();
renderRange.innerHTML =
moment(cal.getDateRangeStart()._date).format(formatNav) +
" - " +
moment(cal.getDateRangeEnd()._date).format(formatNav);
renderRange.innerHTML = formatDateNav(cal, formatNav);
Shiny.setInputValue(el.id + "_dates", {
current: moment(cal.getDate()._date).format(),
start: moment(cal.getDateRangeStart()._date).format(),
end: moment(cal.getDateRangeEnd()._date).format(),
current: dayjs(cal.getDate()._date).format(),
start: dayjs(cal.getDateRangeStart()._date).format(),
end: dayjs(cal.getDateRangeEnd()._date).format(),
});
}
},

clickNext: function (event) {
if (cal !== null) {
cal.next();
renderRange.innerHTML =
moment(cal.getDateRangeStart()._date).format(formatNav) +
" - " +
moment(cal.getDateRangeEnd()._date).format(formatNav);
renderRange.innerHTML = formatDateNav(cal, formatNav);
Shiny.setInputValue(el.id + "_dates", {
current: moment(cal.getDate()._date).format(),
start: moment(cal.getDateRangeStart()._date).format(),
end: moment(cal.getDateRangeEnd()._date).format(),
current: dayjs(cal.getDate()._date).format(),
start: dayjs(cal.getDateRangeStart()._date).format(),
end: dayjs(cal.getDateRangeEnd()._date).format(),
});
}
},

clickToday: function (event) {
if (cal !== null) {
cal.today();
renderRange.innerHTML =
moment(cal.getDateRangeStart()._date).format(formatNav) +
" - " +
moment(cal.getDateRangeEnd()._date).format(formatNav);
renderRange.innerHTML = formatDateNav(cal, formatNav);
Shiny.setInputValue(el.id + "_dates", {
current: moment(cal.getDate()._date).format(),
start: moment(cal.getDateRangeStart()._date).format(),
end: moment(cal.getDateRangeEnd()._date).format(),
current: dayjs(cal.getDate()._date).format(),
start: dayjs(cal.getDateRangeStart()._date).format(),
end: dayjs(cal.getDateRangeEnd()._date).format(),
});
}
},
Expand Down

0 comments on commit d71190a

Please sign in to comment.