mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 05:34:03 +08:00
DEV: adds details/location options to downloadCalendar (#26177)
This will now only be used for google to pre-fill the associated fields in: https://calendar.google.com/calendar/u/0/r/eventedit
This commit is contained in:
parent
a71f68afdd
commit
c986f9a947
|
@ -3,7 +3,7 @@ import User from "discourse/models/user";
|
||||||
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
|
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
|
||||||
import getURL from "discourse-common/lib/get-url";
|
import getURL from "discourse-common/lib/get-url";
|
||||||
|
|
||||||
export function downloadCalendar(title, dates, recurrenceRule = null) {
|
export function downloadCalendar(title, dates, options = {}) {
|
||||||
const currentUser = User.current();
|
const currentUser = User.current();
|
||||||
|
|
||||||
const formattedDates = formatDates(dates);
|
const formattedDates = formatDates(dates);
|
||||||
|
@ -11,20 +11,20 @@ export function downloadCalendar(title, dates, recurrenceRule = null) {
|
||||||
|
|
||||||
switch (currentUser.user_option.default_calendar) {
|
switch (currentUser.user_option.default_calendar) {
|
||||||
case "none_selected":
|
case "none_selected":
|
||||||
_displayModal(title, formattedDates, recurrenceRule);
|
_displayModal(title, formattedDates, options);
|
||||||
break;
|
break;
|
||||||
case "ics":
|
case "ics":
|
||||||
downloadIcs(title, formattedDates, recurrenceRule);
|
downloadIcs(title, formattedDates, options);
|
||||||
break;
|
break;
|
||||||
case "google":
|
case "google":
|
||||||
downloadGoogle(title, formattedDates, recurrenceRule);
|
downloadGoogle(title, formattedDates, options);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function downloadIcs(title, dates, recurrenceRule) {
|
export function downloadIcs(title, dates, options = {}) {
|
||||||
const REMOVE_FILE_AFTER = 20_000;
|
const REMOVE_FILE_AFTER = 20_000;
|
||||||
const file = new File([generateIcsData(title, dates, recurrenceRule)], {
|
const file = new File([generateIcsData(title, dates, options)], {
|
||||||
type: "text/plain",
|
type: "text/plain",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ export function downloadIcs(title, dates, recurrenceRule) {
|
||||||
setTimeout(() => window.URL.revokeObjectURL(file), REMOVE_FILE_AFTER); //remove file to avoid memory leaks
|
setTimeout(() => window.URL.revokeObjectURL(file), REMOVE_FILE_AFTER); //remove file to avoid memory leaks
|
||||||
}
|
}
|
||||||
|
|
||||||
export function downloadGoogle(title, dates, recurrenceRule) {
|
export function downloadGoogle(title, dates, options = {}) {
|
||||||
dates.forEach((date) => {
|
dates.forEach((date) => {
|
||||||
const link = new URL("https://www.google.com/calendar/event");
|
const link = new URL("https://www.google.com/calendar/event");
|
||||||
link.searchParams.append("action", "TEMPLATE");
|
link.searchParams.append("action", "TEMPLATE");
|
||||||
|
@ -49,8 +49,16 @@ export function downloadGoogle(title, dates, recurrenceRule) {
|
||||||
)}`
|
)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (recurrenceRule) {
|
if (options.recurrenceRule) {
|
||||||
link.searchParams.append("recur", `RRULE:${recurrenceRule}`);
|
link.searchParams.append("recur", `RRULE:${options.recurrenceRule}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.location) {
|
||||||
|
link.searchParams.append("location", options.location);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.details) {
|
||||||
|
link.searchParams.append("details", options.details);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.open(getURL(link.href).trim(), "_blank", "noopener", "noreferrer");
|
window.open(getURL(link.href).trim(), "_blank", "noopener", "noreferrer");
|
||||||
|
@ -68,7 +76,7 @@ export function formatDates(dates) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateIcsData(title, dates, recurrenceRule) {
|
export function generateIcsData(title, dates, options = {}) {
|
||||||
let data = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Discourse//EN\n";
|
let data = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Discourse//EN\n";
|
||||||
dates.forEach((date) => {
|
dates.forEach((date) => {
|
||||||
const startDate = moment(date.startsAt);
|
const startDate = moment(date.startsAt);
|
||||||
|
@ -80,7 +88,7 @@ export function generateIcsData(title, dates, recurrenceRule) {
|
||||||
`DTSTAMP:${moment().utc().format("YMMDDTHHmmss")}Z\n` +
|
`DTSTAMP:${moment().utc().format("YMMDDTHHmmss")}Z\n` +
|
||||||
`DTSTART:${startDate.utc().format("YMMDDTHHmmss")}Z\n` +
|
`DTSTART:${startDate.utc().format("YMMDDTHHmmss")}Z\n` +
|
||||||
`DTEND:${endDate.utc().format("YMMDDTHHmmss")}Z\n` +
|
`DTEND:${endDate.utc().format("YMMDDTHHmmss")}Z\n` +
|
||||||
(recurrenceRule ? `RRULE:${recurrenceRule}\n` : ``) +
|
(options.recurrenceRule ? `RRULE:${options.recurrenceRule}\n` : ``) +
|
||||||
`SUMMARY:${title}\n` +
|
`SUMMARY:${title}\n` +
|
||||||
"END:VEVENT\n"
|
"END:VEVENT\n"
|
||||||
);
|
);
|
||||||
|
@ -89,10 +97,12 @@ export function generateIcsData(title, dates, recurrenceRule) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _displayModal(title, dates, recurrenceRule) {
|
function _displayModal(title, dates, options = {}) {
|
||||||
const modal = getOwnerWithFallback(this).lookup("service:modal");
|
const modal = getOwnerWithFallback(this).lookup("service:modal");
|
||||||
modal.show(downloadCalendarModal, {
|
modal.show(downloadCalendarModal, {
|
||||||
model: { calendar: { title, dates, recurrenceRule } },
|
model: {
|
||||||
|
calendar: { title, dates, recurrenceRule: options.recurrenceRule },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2116,12 +2116,12 @@ class PluginApi {
|
||||||
* endsAt: "2021-10-12T16:00:00.000Z",
|
* endsAt: "2021-10-12T16:00:00.000Z",
|
||||||
* },
|
* },
|
||||||
* ],
|
* ],
|
||||||
* "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"
|
* { recurrenceRule: "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR", location: "Paris", details: "Foo" }
|
||||||
* );
|
* );
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
downloadCalendar(title, dates, recurrenceRule = null) {
|
downloadCalendar(title, dates, options = {}) {
|
||||||
downloadCalendar(title, dates, recurrenceRule);
|
downloadCalendar(title, dates, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -62,7 +62,7 @@ END:VCALENDAR`
|
||||||
endsAt: "2021-10-12T16:00:00.000Z",
|
endsAt: "2021-10-12T16:00:00.000Z",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"
|
{ recurrenceRule: "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR" }
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
data,
|
data,
|
||||||
|
@ -109,7 +109,7 @@ END:VCALENDAR`
|
||||||
endsAt: "2021-10-12T16:00:00.000Z",
|
endsAt: "2021-10-12T16:00:00.000Z",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
"FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"
|
{ recurrenceRule: "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR" }
|
||||||
);
|
);
|
||||||
assert.ok(
|
assert.ok(
|
||||||
window.open.calledWith(
|
window.open.calledWith(
|
||||||
|
@ -121,6 +121,48 @@ END:VCALENDAR`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("correct location for Google when location given", function (assert) {
|
||||||
|
downloadGoogle(
|
||||||
|
"event",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startsAt: "2021-10-12T15:00:00.000Z",
|
||||||
|
endsAt: "2021-10-12T16:00:00.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{ location: "Paris" }
|
||||||
|
);
|
||||||
|
assert.ok(
|
||||||
|
window.open.calledWith(
|
||||||
|
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z%2F20211012T160000Z&location=Paris",
|
||||||
|
"_blank",
|
||||||
|
"noopener",
|
||||||
|
"noreferrer"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("correct details for Google when details given", function (assert) {
|
||||||
|
downloadGoogle(
|
||||||
|
"event",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startsAt: "2021-10-12T15:00:00.000Z",
|
||||||
|
endsAt: "2021-10-12T16:00:00.000Z",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{ details: "Cool" }
|
||||||
|
);
|
||||||
|
assert.ok(
|
||||||
|
window.open.calledWith(
|
||||||
|
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z%2F20211012T160000Z&details=Cool",
|
||||||
|
"_blank",
|
||||||
|
"noopener",
|
||||||
|
"noreferrer"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("calculates end date when none given", function (assert) {
|
test("calculates end date when none given", function (assert) {
|
||||||
let dates = formatDates([{ startsAt: "2021-10-12T15:00:00.000Z" }]);
|
let dates = formatDates([{ startsAt: "2021-10-12T15:00:00.000Z" }]);
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user