const { __, _x, _n, sprintf }=wp.i18n;
function pgcal_resolve_cals(settings){
let calArgs=[];
let identifiers=[];
const cals=settings["gcal"]
.split(",")
.map((cal)=> cal.trim())
.filter((cal)=> cal.length > 0);
let customIds=[];
if(settings["cal_ids"]){
customIds=settings["cal_ids"]
.split(",")
.map((id)=> id.trim())
.filter((id)=> id.length > 0);
}
for (var i=0; i < cals.length; i++){
const identifier=customIds[i]||i;
identifiers.push(identifier);
calArgs.push({
googleCalendarId: cals[i],
className: `pgcal-event-${identifier} pgcal-calendar-${identifier}-event`,
});
}
return { eventSources: calArgs, identifiers: identifiers };}
const pgcal_resolve_views=(settings)=> {
const wantsToEnforceListviewOnMobile=pgcal_is_truthy(
settings["enforce_listview_on_mobile"]
);
initialView=settings["initial_view"];
const viewsArray=pgcal_csv_to_array(settings["views"]);
const viewsIncludesList=pgcal_get_item_by_fuzzy_value(viewsArray, "list");
const listType=pgcal_get_item_by_fuzzy_value(
viewsArray,
settings["list_type"]
);
if(pgcal_is_mobile()&&wantsToEnforceListviewOnMobile){
initialView=listType;
}
const views={
all: viewsArray,
length: viewsArray.length,
hasList: !!viewsIncludesList,
listType,
initial: initialView,
wantsToEnforceListviewOnMobile,
};
return views;
};
const pgcal_csv_to_array=(csv)=> csv.split(",").map((view)=> view.trim());
const pgcal_get_item_by_fuzzy_value=(array, value)=>
array.find((item)=> item.toLowerCase().includes(value.toLowerCase()));
function pgcal_is_truthy(value){
const lowercaseValue =
typeof value==="string" ? value.toLowerCase():value;
return ["true", "1", true, 1].includes(lowercaseValue);
}
function pgcal_is_mobile(width=768){
return window.innerWidth <=width;
}
function pgcal_urlify(text){
const urlRegex=/<a[\s>].*?<\/a>|https?:\/\/[^\s]+[?!.]*\/?\b/g;
if(text){
return text.replace(urlRegex, function (m){
if(m.startsWith("<a")){
return m;
}else{
const urlMatch=m.match(/https?:\/\/[^\s]+[?!.]*\/?\b/);
if(urlMatch){
const url=urlMatch[0];
const punctuation=url.match(/[?!.]*$/);
if(punctuation){
const cleanedURL=url.replace(/[?!.]*$/, "");
const linkText=cleanedURL;
return (
'<a target="_blank" href="' +
cleanedURL +
'">' +
linkText +
"</a>" +
punctuation[0]
);
}else{
return '<a target="_blank" href="' + url + '">' + url + "</a>";
}}
return m;
}});
}
return "";
}
function pgcal_breakify(text){
if(text){
return text.replace(/(?:\r\n|\r|\n)/g, "<br />");
}
return "";
}
function pgcal_mapify(text){
const buttonLabel=__("Map", "pretty-google-calendar");
let footer="";
if(text){
footer +=`<br /><a class="button pgcal-map-button" target="_blank" href="https://www.google.com/maps/search/?api=1&query=${encodeURI(
text
)}">${buttonLabel}</a>`;
}
return footer;
}
function pgcal_addToGoogle(url){
const buttonLabel=__("Add to Google", "pretty-google-calendar");
if(url){
return `<a class="button pgcal-add-to-google-button" href="${url}" target="_blank">${buttonLabel}</a>`;
}}
function pgcal_downloadEventICS(event){
const sanitize=(str)=> {
if(!str) return "";
return String(str)
.replace(/\\/g, "\\\\")
.replace(/;/g, "\\;")
.replace(/,/g, "\\,")
.replace(/\n/g, "\\n");
};
const formatICSDate=(dateStr, allDay)=> {
const date=new Date(dateStr);
if(allDay){
const year=date.getUTCFullYear();
const month=String(date.getUTCMonth() + 1).padStart(2, "0");
const day=String(date.getUTCDate()).padStart(2, "0");
return `${year}${month}${day}`;
}else{
const year=date.getUTCFullYear();
const month=String(date.getUTCMonth() + 1).padStart(2, "0");
const day=String(date.getUTCDate()).padStart(2, "0");
const hours=String(date.getUTCHours()).padStart(2, "0");
const minutes=String(date.getUTCMinutes()).padStart(2, "0");
const seconds=String(date.getUTCSeconds()).padStart(2, "0");
return `${year}${month}${day}T${hours}${minutes}${seconds}Z`;
}};
const startDate=formatICSDate(event.startStr, event.allDay);
const endDate=event.endStr
? formatICSDate(event.endStr, event.allDay)
: startDate;
const uid=`${event.id||"event"}@pretty-google-calendar`;
let ics=`BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Pretty Google Calendar//EN\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\nBEGIN:VEVENT\nUID:${uid}\nDTSTAMP:${formatICSDate(
new Date().toISOString(),
false
)}\nDTSTART${event.allDay ? ";VALUE=DATE":""}:${startDate}\nDTEND${
event.allDay ? ";VALUE=DATE":""
}:${endDate}\nSUMMARY:${sanitize(event.title)}`;
if(event.extendedProps&&event.extendedProps.location){
ics +=`\nLOCATION:${sanitize(event.extendedProps.location)}`;
}
if(event.extendedProps&&event.extendedProps.description){
ics +=`\nDESCRIPTION:${sanitize(event.extendedProps.description)}`;
}
ics +=`\nEND:VEVENT\nEND:VCALENDAR`;
const encodedICS=encodeURIComponent(ics);
const filename=`${event.title||"event"}.ics`;
const downloadLink=`data:text/calendar;charset=utf-8,${encodedICS}`;
const label=__("Download (.ics)", "pretty-google-calendar");
return `<a class="button pgcal-download-ics-button" href="${downloadLink}" download="${filename}">${label}</a>`;
}
function pgcal_argmerge(defaults, override){
const out={};
for (const [name, defaultVal] of Object.entries(defaults)){
if(override.hasOwnProperty(name)){
out[name]=override[name];
}else{
out[name]=defaultVal;
}}
for (const name in override){
if(!out.hasOwnProperty(name)){
out[name]=override[name];
}}
return out;
};