2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2020-12-23 12:45:03 +01:00
|
|
|
* Calendar Util Methods
|
|
|
|
*
|
2023-04-09 12:49:50 +02:00
|
|
|
* By Rejas
|
2020-12-23 12:45:03 +01:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
const CalendarUtils = {
|
|
|
|
/**
|
2023-04-09 12:49:50 +02:00
|
|
|
* Capitalize the first letter of a string
|
2020-12-23 12:45:03 +01:00
|
|
|
*
|
2023-04-09 12:49:50 +02:00
|
|
|
* @param {string} string The string to capitalize
|
|
|
|
* @returns {string} The capitalized string
|
2020-12-23 12:45:03 +01:00
|
|
|
*/
|
2023-04-09 12:49:50 +02:00
|
|
|
capFirst: function (string) {
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
},
|
2020-12-23 12:45:03 +01:00
|
|
|
|
2023-04-09 12:49:50 +02:00
|
|
|
/**
|
|
|
|
* This function accepts a number (either 12 or 24) and returns a moment.js LocaleSpecification with the
|
|
|
|
* corresponding time-format to be used in the calendar display. If no number is given (or otherwise invalid input)
|
|
|
|
* it will a localeSpecification object with the system locale time format.
|
|
|
|
*
|
|
|
|
* @param {number} timeFormat Specifies either 12 or 24-hour time format
|
|
|
|
* @returns {moment.LocaleSpecification} formatted time
|
|
|
|
*/
|
|
|
|
getLocaleSpecification: function (timeFormat) {
|
|
|
|
switch (timeFormat) {
|
|
|
|
case 12: {
|
|
|
|
return { longDateFormat: { LT: "h:mm A" } };
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
2023-04-09 12:49:50 +02:00
|
|
|
case 24: {
|
|
|
|
return { longDateFormat: { LT: "HH:mm" } };
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
2023-04-09 12:49:50 +02:00
|
|
|
default: {
|
|
|
|
return { longDateFormat: { LT: moment.localeData().longDateFormat("LT") } };
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-05-15 14:49:04 +02:00
|
|
|
/**
|
2023-04-09 12:49:50 +02:00
|
|
|
* Shortens a string if it's longer than maxLength and add an ellipsis to the end
|
2021-05-15 14:49:04 +02:00
|
|
|
*
|
2023-04-09 12:49:50 +02:00
|
|
|
* @param {string} string Text string to shorten
|
|
|
|
* @param {number} maxLength The max length of the string
|
|
|
|
* @param {boolean} wrapEvents Wrap the text after the line has reached maxLength
|
|
|
|
* @param {number} maxTitleLines The max number of vertical lines before cutting event title
|
|
|
|
* @returns {string} The shortened string
|
2021-05-15 14:49:04 +02:00
|
|
|
*/
|
2023-04-09 12:49:50 +02:00
|
|
|
shorten: function (string, maxLength, wrapEvents, maxTitleLines) {
|
|
|
|
if (typeof string !== "string") {
|
|
|
|
return "";
|
|
|
|
}
|
2020-12-26 12:12:44 +01:00
|
|
|
|
2023-04-09 12:49:50 +02:00
|
|
|
if (wrapEvents === true) {
|
|
|
|
const words = string.split(" ");
|
|
|
|
let temp = "";
|
|
|
|
let currentLine = "";
|
|
|
|
let line = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < words.length; i++) {
|
|
|
|
const word = words[i];
|
|
|
|
if (currentLine.length + word.length < (typeof maxLength === "number" ? maxLength : 25) - 1) {
|
|
|
|
// max - 1 to account for a space
|
|
|
|
currentLine += `${word} `;
|
2020-12-26 12:12:44 +01:00
|
|
|
} else {
|
2023-04-09 12:49:50 +02:00
|
|
|
line++;
|
|
|
|
if (line > maxTitleLines - 1) {
|
|
|
|
if (i < words.length) {
|
|
|
|
currentLine += "…";
|
2020-12-26 12:12:44 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-09 12:49:50 +02:00
|
|
|
if (currentLine.length > 0) {
|
|
|
|
temp += `${currentLine}<br>${word} `;
|
2020-12-26 12:12:44 +01:00
|
|
|
} else {
|
2023-04-09 12:49:50 +02:00
|
|
|
temp += `${word}<br>`;
|
2020-12-26 12:12:44 +01:00
|
|
|
}
|
2023-04-09 12:49:50 +02:00
|
|
|
currentLine = "";
|
2020-12-26 12:12:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 12:49:50 +02:00
|
|
|
return (temp + currentLine).trim();
|
|
|
|
} else {
|
|
|
|
if (maxLength && typeof maxLength === "number" && string.length > maxLength) {
|
|
|
|
return `${string.trim().slice(0, maxLength)}…`;
|
|
|
|
} else {
|
|
|
|
return string.trim();
|
|
|
|
}
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2023-04-09 12:49:50 +02:00
|
|
|
* Transforms the title of an event for usage.
|
|
|
|
* Replaces parts of the text as defined in config.titleReplace.
|
|
|
|
* Shortens title based on config.maxTitleLength and config.wrapEvents
|
2020-12-23 12:45:03 +01:00
|
|
|
*
|
2023-04-09 12:49:50 +02:00
|
|
|
* @param {string} title The title to transform.
|
|
|
|
* @param {object} titleReplace Pairs of strings to be replaced in the title
|
|
|
|
* @returns {string} The transformed title.
|
2020-12-23 12:45:03 +01:00
|
|
|
*/
|
2023-04-09 12:49:50 +02:00
|
|
|
titleTransform: function (title, titleReplace) {
|
2023-04-16 17:38:39 +02:00
|
|
|
let transformedTitle = title;
|
2023-04-09 12:49:50 +02:00
|
|
|
for (let needle in titleReplace) {
|
|
|
|
const replacement = titleReplace[needle];
|
|
|
|
|
|
|
|
const regParts = needle.match(/^\/(.+)\/([gim]*)$/);
|
|
|
|
if (regParts) {
|
|
|
|
// the parsed pattern is a regexp.
|
|
|
|
needle = new RegExp(regParts[1], regParts[2]);
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
|
|
|
|
2023-04-16 17:38:39 +02:00
|
|
|
transformedTitle = transformedTitle.replace(needle, replacement);
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
2023-04-16 17:38:39 +02:00
|
|
|
return transformedTitle;
|
2020-12-23 12:45:03 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof module !== "undefined") {
|
|
|
|
module.exports = CalendarUtils;
|
|
|
|
}
|