2021-08-04 22:28:07 +08:00
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
2020-04-08 14:53:21 +08:00
|
|
|
import LocalDateBuilder from "../lib/local-date-builder";
|
2018-05-04 14:51:41 +08:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2021-08-04 22:28:07 +08:00
|
|
|
import showModal from "discourse/lib/show-modal";
|
2021-10-06 11:11:52 +08:00
|
|
|
import { downloadCalendar } from "discourse/lib/download-calendar";
|
|
|
|
import { renderIcon } from "discourse-common/lib/icon-library";
|
2021-10-09 03:22:52 +08:00
|
|
|
import I18n from "I18n";
|
2022-05-02 23:10:26 +08:00
|
|
|
import { hidePopover, showPopover } from "discourse/lib/d-popover";
|
2021-08-04 22:28:07 +08:00
|
|
|
|
2022-03-31 08:58:38 +08:00
|
|
|
// Import applyLocalDates from discourse/lib/local-dates instead
|
2021-08-04 22:28:07 +08:00
|
|
|
export function applyLocalDates(dates, siteSettings) {
|
|
|
|
if (!siteSettings.discourse_local_dates_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentUserTZ = moment.tz.guess();
|
|
|
|
|
|
|
|
dates.forEach((element) => {
|
|
|
|
const opts = buildOptionsFromElement(element, siteSettings);
|
|
|
|
|
|
|
|
const localDateBuilder = new LocalDateBuilder(opts, currentUserTZ).build();
|
|
|
|
element.innerText = "";
|
|
|
|
element.insertAdjacentHTML(
|
|
|
|
"beforeend",
|
|
|
|
`
|
|
|
|
<svg class="fa d-icon d-icon-globe-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
|
2021-11-25 12:22:43 +08:00
|
|
|
<use href="#globe-americas"></use>
|
2021-08-04 22:28:07 +08:00
|
|
|
</svg>
|
|
|
|
<span class="relative-time">${localDateBuilder.formated}</span>
|
|
|
|
`
|
|
|
|
);
|
|
|
|
element.setAttribute("aria-label", localDateBuilder.textPreview);
|
|
|
|
|
|
|
|
const classes = ["cooked-date"];
|
|
|
|
if (localDateBuilder.pastEvent) {
|
|
|
|
classes.push("past");
|
|
|
|
}
|
|
|
|
element.classList.add(...classes);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildOptionsFromElement(element, siteSettings) {
|
|
|
|
const opts = {};
|
|
|
|
const dataset = element.dataset;
|
2021-09-20 07:23:18 +08:00
|
|
|
|
|
|
|
if (_rangeElements(element).length === 2) {
|
|
|
|
opts.duration = _calculateDuration(element);
|
|
|
|
}
|
|
|
|
|
2021-08-04 22:28:07 +08:00
|
|
|
opts.time = dataset.time;
|
|
|
|
opts.date = dataset.date;
|
|
|
|
opts.recurring = dataset.recurring;
|
|
|
|
opts.timezones = (
|
|
|
|
dataset.timezones ||
|
|
|
|
siteSettings.discourse_local_dates_default_timezones ||
|
|
|
|
"Etc/UTC"
|
|
|
|
)
|
|
|
|
.split("|")
|
|
|
|
.filter(Boolean);
|
|
|
|
opts.timezone = dataset.timezone;
|
|
|
|
opts.calendar = (dataset.calendar || "on") === "on";
|
|
|
|
opts.displayedTimezone = dataset.displayedTimezone;
|
|
|
|
opts.format = dataset.format || (opts.time ? "LLL" : "LL");
|
|
|
|
opts.countdown = dataset.countdown;
|
|
|
|
return opts;
|
|
|
|
}
|
2020-04-08 14:53:21 +08:00
|
|
|
|
2021-09-20 07:23:18 +08:00
|
|
|
function _rangeElements(element) {
|
2021-09-20 14:03:02 +08:00
|
|
|
if (!element.parentElement) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-01-10 15:02:36 +08:00
|
|
|
|
|
|
|
// TODO: element.parentElement.children.length !== 2 is a fallback to old solution for ranges
|
|
|
|
// Condition can be removed after migration to [date-range]
|
|
|
|
if (
|
|
|
|
element.dataset.range !== "true" &&
|
|
|
|
element.parentElement.children.length !== 2
|
|
|
|
) {
|
|
|
|
return [element];
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:23:18 +08:00
|
|
|
return Array.from(element.parentElement.children).filter(
|
|
|
|
(span) => span.dataset.date
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
function initializeDiscourseLocalDates(api) {
|
2021-08-04 22:28:07 +08:00
|
|
|
const siteSettings = api.container.lookup("site-settings:main");
|
2021-10-09 03:22:52 +08:00
|
|
|
const defaultTitle = I18n.t("discourse_local_dates.default_title", {
|
|
|
|
site_name: siteSettings.title,
|
|
|
|
});
|
2021-09-23 02:46:06 +08:00
|
|
|
|
2021-08-04 22:28:07 +08:00
|
|
|
api.decorateCookedElement(
|
2021-10-09 03:22:52 +08:00
|
|
|
(elem, helper) => {
|
|
|
|
const dates = elem.querySelectorAll(".discourse-local-date");
|
|
|
|
|
|
|
|
applyLocalDates(dates, siteSettings);
|
|
|
|
|
|
|
|
const topicTitle = helper?.getModel()?.topic?.title;
|
|
|
|
dates.forEach((date) => {
|
|
|
|
date.dataset.title = date.dataset.title || topicTitle || defaultTitle;
|
|
|
|
});
|
2021-08-04 22:28:07 +08:00
|
|
|
},
|
2019-06-11 23:21:23 +08:00
|
|
|
{ id: "discourse-local-date" }
|
|
|
|
);
|
2018-05-04 14:51:41 +08:00
|
|
|
|
2019-04-08 21:52:09 +08:00
|
|
|
api.onToolbarCreate((toolbar) => {
|
|
|
|
toolbar.addButton({
|
|
|
|
title: "discourse_local_dates.title",
|
|
|
|
id: "local-dates",
|
|
|
|
group: "extras",
|
|
|
|
icon: "calendar-alt",
|
|
|
|
sendAction: (event) =>
|
|
|
|
toolbar.context.send("insertDiscourseLocalDate", event),
|
|
|
|
});
|
2018-05-04 14:51:41 +08:00
|
|
|
});
|
|
|
|
|
2019-04-08 21:52:09 +08:00
|
|
|
api.modifyClass("component:d-editor", {
|
2021-09-02 02:29:07 +08:00
|
|
|
pluginId: "discourse-local-dates",
|
2018-05-04 14:51:41 +08:00
|
|
|
actions: {
|
2019-04-08 21:52:09 +08:00
|
|
|
insertDiscourseLocalDate(toolbarEvent) {
|
2018-05-04 14:51:41 +08:00
|
|
|
showModal("discourse-local-dates-create-modal").setProperties({
|
2021-09-23 02:46:06 +08:00
|
|
|
insertDate: (markup) => {
|
|
|
|
toolbarEvent.addText(markup);
|
|
|
|
},
|
2018-05-04 14:51:41 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-04 22:28:07 +08:00
|
|
|
function buildHtmlPreview(element, siteSettings) {
|
|
|
|
const opts = buildOptionsFromElement(element, siteSettings);
|
|
|
|
const localDateBuilder = new LocalDateBuilder(
|
|
|
|
opts,
|
|
|
|
moment.tz.guess()
|
|
|
|
).build();
|
|
|
|
|
|
|
|
const htmlPreviews = localDateBuilder.previews.map((preview) => {
|
|
|
|
const previewNode = document.createElement("div");
|
|
|
|
previewNode.classList.add("preview");
|
|
|
|
if (preview.current) {
|
|
|
|
previewNode.classList.add("current");
|
|
|
|
}
|
|
|
|
|
|
|
|
const timezoneNode = document.createElement("span");
|
|
|
|
timezoneNode.classList.add("timezone");
|
|
|
|
timezoneNode.innerText = preview.timezone;
|
|
|
|
previewNode.appendChild(timezoneNode);
|
|
|
|
|
|
|
|
const dateTimeNode = document.createElement("span");
|
|
|
|
dateTimeNode.classList.add("date-time");
|
2021-09-20 07:23:18 +08:00
|
|
|
dateTimeNode.innerHTML = preview.formated;
|
2021-08-04 22:28:07 +08:00
|
|
|
previewNode.appendChild(dateTimeNode);
|
|
|
|
|
|
|
|
return previewNode;
|
|
|
|
});
|
|
|
|
|
|
|
|
const previewsNode = document.createElement("div");
|
|
|
|
previewsNode.classList.add("locale-dates-previews");
|
|
|
|
htmlPreviews.forEach((htmlPreview) => previewsNode.appendChild(htmlPreview));
|
|
|
|
|
2021-10-14 06:22:44 +08:00
|
|
|
const calendarNode = _downloadCalendarNode(element);
|
|
|
|
if (calendarNode) {
|
|
|
|
previewsNode.appendChild(calendarNode);
|
|
|
|
}
|
2021-10-06 11:11:52 +08:00
|
|
|
|
2021-08-04 22:28:07 +08:00
|
|
|
return previewsNode.outerHTML;
|
|
|
|
}
|
|
|
|
|
2021-10-14 06:22:44 +08:00
|
|
|
function calculateStartAndEndDate(startDataset, endDataset) {
|
|
|
|
let startDate, endDate;
|
|
|
|
startDate = moment.tz(
|
|
|
|
`${startDataset.date} ${startDataset.time || ""}`.trim(),
|
|
|
|
startDataset.timezone
|
|
|
|
);
|
|
|
|
if (endDataset) {
|
|
|
|
endDate = moment.tz(
|
|
|
|
`${endDataset.date} ${endDataset.time || ""}`.trim(),
|
|
|
|
endDataset.timezone
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return [startDate, endDate];
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:11:52 +08:00
|
|
|
function _downloadCalendarNode(element) {
|
2021-10-14 06:22:44 +08:00
|
|
|
const [startDataset, endDataset] = _rangeElements(element).map(
|
|
|
|
(dateElement) => dateElement.dataset
|
|
|
|
);
|
|
|
|
const [startDate, endDate] = calculateStartAndEndDate(
|
|
|
|
startDataset,
|
|
|
|
endDataset
|
|
|
|
);
|
|
|
|
|
|
|
|
if (startDate < moment().tz(startDataset.timezone)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:11:52 +08:00
|
|
|
const node = document.createElement("div");
|
|
|
|
node.classList.add("download-calendar");
|
|
|
|
node.innerHTML = `${renderIcon("string", "file")} ${I18n.t(
|
|
|
|
"download_calendar.add_to_calendar"
|
|
|
|
)}`;
|
2021-10-14 06:22:44 +08:00
|
|
|
node.setAttribute("data-starts-at", startDate.toISOString());
|
2021-10-06 11:11:52 +08:00
|
|
|
if (endDataset) {
|
2021-10-14 06:22:44 +08:00
|
|
|
node.setAttribute("data-ends-at", endDate.toISOString());
|
2021-10-06 11:11:52 +08:00
|
|
|
}
|
|
|
|
if (!startDataset.time && !endDataset) {
|
2021-10-14 06:22:44 +08:00
|
|
|
node.setAttribute("data-ends-at", startDate.add(24, "hours").toISOString());
|
2021-10-06 11:11:52 +08:00
|
|
|
}
|
2021-10-09 03:22:52 +08:00
|
|
|
node.setAttribute("data-title", startDataset.title);
|
2021-10-06 11:11:52 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:23:18 +08:00
|
|
|
function _calculateDuration(element) {
|
|
|
|
const [startDataset, endDataset] = _rangeElements(element).map(
|
|
|
|
(dateElement) => dateElement.dataset
|
|
|
|
);
|
|
|
|
const startDateTime = moment(
|
2021-10-01 04:07:48 +08:00
|
|
|
`${startDataset.date} ${startDataset.time || ""}`.trim()
|
|
|
|
);
|
|
|
|
const endDateTime = moment(
|
|
|
|
`${endDataset.date} ${endDataset.time || ""}`.trim()
|
2021-09-20 07:23:18 +08:00
|
|
|
);
|
|
|
|
const duration = endDateTime.diff(startDateTime, "minutes");
|
|
|
|
|
|
|
|
// negative duration is used when we calculate difference for end date from range
|
|
|
|
return element.dataset === startDataset ? duration : -duration;
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
export default {
|
|
|
|
name: "discourse-local-dates",
|
|
|
|
|
2021-08-04 22:28:07 +08:00
|
|
|
showDatePopover(event) {
|
2021-08-10 17:48:31 +08:00
|
|
|
const owner = getOwner(this);
|
|
|
|
if (owner.isDestroyed || owner.isDestroying) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-04 22:28:07 +08:00
|
|
|
|
2022-05-02 23:10:26 +08:00
|
|
|
if (event?.target?.classList?.contains("download-calendar")) {
|
2021-10-06 11:11:52 +08:00
|
|
|
const dataset = event.target.dataset;
|
2021-10-14 06:22:44 +08:00
|
|
|
downloadCalendar(dataset.title, [
|
2021-10-06 11:11:52 +08:00
|
|
|
{
|
|
|
|
startsAt: dataset.startsAt,
|
|
|
|
endsAt: dataset.endsAt,
|
|
|
|
},
|
|
|
|
]);
|
2022-05-03 02:35:15 +08:00
|
|
|
|
|
|
|
// TODO: remove this when rewriting preview as a component
|
|
|
|
const parentPopover = event.target.closest("[data-tippy-root]");
|
|
|
|
if (parentPopover?._tippy) {
|
|
|
|
parentPopover._tippy.hide();
|
|
|
|
}
|
|
|
|
|
2022-05-02 23:10:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!event?.target?.classList?.contains("discourse-local-date")) {
|
|
|
|
return;
|
2021-08-04 22:28:07 +08:00
|
|
|
}
|
2022-05-02 23:10:26 +08:00
|
|
|
|
|
|
|
const siteSettings = owner.lookup("site-settings:main");
|
|
|
|
|
|
|
|
showPopover(event, {
|
|
|
|
trigger: "click",
|
|
|
|
content: buildHtmlPreview(event.target, siteSettings),
|
|
|
|
allowHTML: true,
|
|
|
|
interactive: true,
|
|
|
|
appendTo: "parent",
|
|
|
|
});
|
2021-08-04 22:28:07 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
hideDatePopover(event) {
|
2022-05-02 23:10:26 +08:00
|
|
|
hidePopover(event);
|
2021-08-04 22:28:07 +08:00
|
|
|
},
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
initialize(container) {
|
2021-08-04 22:28:07 +08:00
|
|
|
window.addEventListener("click", this.showDatePopover);
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
const siteSettings = container.lookup("site-settings:main");
|
|
|
|
if (siteSettings.discourse_local_dates_enabled) {
|
2020-04-08 14:53:21 +08:00
|
|
|
$.fn.applyLocalDates = function () {
|
2021-08-04 22:28:07 +08:00
|
|
|
deprecated(
|
|
|
|
"`$.applyLocalDates()` is deprecated, import and use `applyLocalDates()` instead."
|
|
|
|
);
|
|
|
|
|
|
|
|
return applyLocalDates(this.toArray(), siteSettings);
|
2020-04-08 14:53:21 +08:00
|
|
|
};
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
withPluginApi("0.8.8", initializeDiscourseLocalDates);
|
|
|
|
}
|
|
|
|
},
|
2021-08-04 22:28:07 +08:00
|
|
|
|
|
|
|
teardown() {
|
|
|
|
window.removeEventListener("click", this.showDatePopover);
|
|
|
|
},
|
2018-05-04 14:51:41 +08:00
|
|
|
};
|