Skip to content

Commit

Permalink
DEV: Extract common calendar options (#604)
Browse files Browse the repository at this point in the history
Extract common calendar options
  • Loading branch information
nattsw authored Aug 28, 2024
1 parent 57fa918 commit 66259cd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import loadScript from "discourse/lib/load-script";
import Category from "discourse/models/category";
import getURL from "discourse-common/lib/get-url";
import { formatEventName } from "../helpers/format-event-name";
import {
getCalendarButtonsText,
getCurrentBcp47Locale,
} from "../lib/calendar-locale";
import fullCalendarDefaultOptions from "../lib/full-calendar-default-options";
import { isNotFullDayEvent } from "../lib/guess-best-date-format";
import { buildPopover, destroyPopover } from "../lib/popover";

export default Component.extend({
tagName: "",
Expand Down Expand Up @@ -63,11 +59,7 @@ export default Component.extend({

this._loadCalendar().then(() => {
const fullCalendar = new window.FullCalendar.Calendar(calendarNode, {
locale: getCurrentBcp47Locale(),
buttonText: getCalendarButtonsText(),
eventClick: function () {
destroyPopover();
},
...fullCalendarDefaultOptions(),
eventPositioned: (info) => {
if (siteSettings.events_max_rows === 0) {
return;
Expand All @@ -93,14 +85,6 @@ export default Component.extend({
}
fullCalendar.updateSize();
},
eventMouseEnter: function ({ event, jsEvent }) {
destroyPopover();
const htmlContent = event.title;
buildPopover(jsEvent, htmlContent);
},
eventMouseLeave: function () {
destroyPopover();
},
});
this._calendar = fullCalendar;

Expand Down
23 changes: 3 additions & 20 deletions assets/javascripts/discourse/initializers/discourse-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import getURL from "discourse-common/lib/get-url";
import { iconHTML } from "discourse-common/lib/icon-library";
import I18n from "I18n";
import { formatEventName } from "../helpers/format-event-name";
import {
getCalendarButtonsText,
getCurrentBcp47Locale,
} from "../lib/calendar-locale";
import { colorToHex, contrastColor, stringToColor } from "../lib/colors";
import fullCalendarDefaultOptions from "../lib/full-calendar-default-options";
import { isNotFullDayEvent } from "../lib/guess-best-date-format";
import { buildPopover, destroyPopover } from "../lib/popover";

Expand Down Expand Up @@ -127,11 +124,7 @@ function initializeDiscourseCalendar(api) {
let fullCalendar = new window.FullCalendar.Calendar(
categoryEventNode,
{
eventClick: function () {
destroyPopover();
},
locale: getCurrentBcp47Locale(),
buttonText: getCalendarButtonsText(),
...fullCalendarDefaultOptions(),
eventPositioned: (info) => {
if (siteSettings.events_max_rows === 0) {
return;
Expand All @@ -157,14 +150,6 @@ function initializeDiscourseCalendar(api) {
}
fullCalendar.updateSize();
},
eventMouseEnter: function ({ event, jsEvent }) {
destroyPopover();
const htmlContent = event.title;
buildPopover(jsEvent, htmlContent);
},
eventMouseLeave: function () {
destroyPopover();
},
}
);
const params = {
Expand Down Expand Up @@ -360,10 +345,9 @@ function initializeDiscourseCalendar(api) {
$calendar.attr("data-calendar-show-add-to-calendar") !== "false";

return new window.FullCalendar.Calendar($calendar[0], {
...fullCalendarDefaultOptions(),
timeZone,
timeZoneImpl: "moment-timezone",
locale: getCurrentBcp47Locale(),
buttonText: getCalendarButtonsText(),
nextDayThreshold: "06:00:00",
displayEventEnd: true,
height: 650,
Expand Down Expand Up @@ -395,7 +379,6 @@ function initializeDiscourseCalendar(api) {

$calendarTitle.innerText = info.view.title;
},

eventPositioned: (info) => {
_setTimezoneOffset(info);
},
Expand Down
2 changes: 1 addition & 1 deletion assets/javascripts/discourse/lib/calendar-locale.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import I18n from "I18n";

export function getCurrentBcp47Locale() {
return I18n.currentLocale().replace("_", "-");
return I18n.currentLocale().replace("_", "-").toLowerCase();
}

export function getCalendarButtonsText() {
Expand Down
23 changes: 23 additions & 0 deletions assets/javascripts/discourse/lib/full-calendar-default-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
getCalendarButtonsText,
getCurrentBcp47Locale,
} from "./calendar-locale";
import { buildPopover, destroyPopover } from "./popover";

export default function fullCalendarDefaultOptions() {
return {
eventClick: function () {
destroyPopover();
},
locale: getCurrentBcp47Locale(),
buttonText: getCalendarButtonsText(),
eventMouseEnter: function ({ event, jsEvent }) {
destroyPopover();
const htmlContent = event.title;
buildPopover(jsEvent, htmlContent);
},
eventMouseLeave: function () {
destroyPopover();
},
};
}

0 comments on commit 66259cd

Please sign in to comment.