owncast/webroot/js/utils.js

136 lines
4.0 KiB
JavaScript
Raw Normal View History

export const URL_STATUS = `/status`;
export const URL_CHAT_HISTORY = `/chat`;
export const URL_CUSTOM_EMOJIS = `/emoji`;
export const URL_CONFIG = `/config`;
Merge of emoji + autolink + embed + etc (#108) * Add an emoji picker to chat * Update to the custom emoji picker and add first pass at using custom emoji in text box * Add custom emoji endpoint and use it in the app * Position the emoji picker * Handle events from the text input * pair down the number of party parrots * Size emoji in chat and input * Add new custom emoji * Add OMQ stickers as custom emoji * Show custom category for emoji picker by default * update omq emojis * Document basic supported markdown syntax. Closes #95 * Websocket refactor: Pull it out of the UI and support callbacks (#104) * Websocket refactor: Pull it out of the UI and support listeners * Changes required for Safari to be happy with modules * Move to explicit ad-hoc callback registration * Add an emoji picker to chat * Update to the custom emoji picker and add first pass at using custom emoji in text box * Handle events from the text input * Rebuild autolinking + embed handling for #93 * Re-enable disabling chat * Document basic supported markdown syntax. Closes #95 * Document basic supported markdown syntax. Closes #95 * Add an emoji picker to chat * Merge emoji and embeds. * Merge emoji + embed branches. Rework autolink +embeds. WIP for username highlighting for #100 * More updates to chat text formatting/embedding/linking * Fix username autocomplete to work with div instead of form elements * Post-rebase fixes + tweaks * Disable text input by setting contentEditable = false * Remove test that hardcodes pointing to public test server * Fix re-enable chat with the contentEditable input div * Style and fix the fake placeholder text in the input div * Missing file. Were did it go? * Set a height for instagram embeds * Cleanup Co-authored-by: Ginger Wong <omqmail@gmail.com>
2020-08-13 06:56:41 +02:00
// TODO: This directory is customizable in the config. So we should expose this via the config API.
export const URL_STREAM = `/hls/stream.m3u8`;
export const URL_WEBSOCKET = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/entry`;
2020-07-05 10:08:22 +02:00
2020-08-21 00:29:15 +02:00
export const TIMER_STATUS_UPDATE = 5000; // ms
export const TIMER_DISABLE_CHAT_AFTER_OFFLINE = 5 * 60 * 1000; // 5 mins
export const TIMER_STREAM_DURATION_COUNTER = 1000;
export const TEMP_IMAGE = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
export const MESSAGE_OFFLINE = 'Stream is offline.';
export const MESSAGE_ONLINE = 'Stream is online';
export const URL_OWNCAST = 'https://github.com/gabek/owncast'; // used in footer
export function getLocalStorage(key) {
2020-06-14 09:24:26 +02:00
try {
return localStorage.getItem(key);
} catch (e) {
}
return null;
}
export function setLocalStorage(key, value) {
2020-06-14 09:24:26 +02:00
try {
2020-06-14 10:10:26 +02:00
if (value !== "" && value !== null) {
localStorage.setItem(key, value);
} else {
localStorage.removeItem(key);
}
2020-06-14 09:24:26 +02:00
return true;
} catch (e) {}
return false;
}
export function clearLocalStorage(key) {
2020-06-14 10:10:26 +02:00
localStorage.removeItem(key);
}
// jump down to the max height of a div, with a slight delay
export function jumpToBottom(element) {
if (!element) return;
setTimeout(() => {
element.scrollTo({
top: element.scrollHeight,
left: 0,
behavior: 'smooth'
});
}, 50, element);
2020-06-14 09:24:26 +02:00
}
// convert newlines to <br>s
export function addNewlines(str) {
return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
2020-06-15 06:14:42 +02:00
}
export function pluralize(string, count) {
2020-06-18 19:24:54 +02:00
if (count === 1) {
return string;
} else {
return string + "s";
}
}
2020-06-16 00:45:55 +02:00
// Trying to determine if browser is mobile/tablet.
// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
export function hasTouchScreen() {
2020-08-21 00:29:15 +02:00
let hasTouch = false;
if ("maxTouchPoints" in navigator) {
2020-08-21 00:29:15 +02:00
hasTouch = navigator.maxTouchPoints > 0;
2020-06-16 00:45:55 +02:00
} else if ("msMaxTouchPoints" in navigator) {
2020-08-21 00:29:15 +02:00
hasTouch = navigator.msMaxTouchPoints > 0;
2020-06-16 00:45:55 +02:00
} else {
var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
if (mQ && mQ.media === "(pointer:coarse)") {
2020-08-21 00:29:15 +02:00
hasTouch = !!mQ.matches;
2020-06-16 00:45:55 +02:00
} else if ('orientation' in window) {
2020-08-21 00:29:15 +02:00
hasTouch = true; // deprecated, but good fallback
2020-06-16 00:45:55 +02:00
} else {
// Only as a last resort, fall back to user agent sniffing
var UA = navigator.userAgent;
2020-08-21 00:29:15 +02:00
hasTouch = (
2020-06-16 00:45:55 +02:00
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
);
}
}
2020-08-21 00:29:15 +02:00
return hasTouch;
2020-06-16 00:45:55 +02:00
}
2020-07-05 07:34:00 +02:00
// generate random avatar from https://robohash.org
export function generateAvatar(hash) {
2020-07-05 07:34:00 +02:00
const avatarSource = 'https://robohash.org/';
const optionSize = '?size=80x80';
const optionSet = '&set=set3';
2020-07-05 07:34:00 +02:00
const optionBg = ''; // or &bgset=bg1 or bg2
return avatarSource + hash + optionSize + optionSet + optionBg;
}
export function generateUsername() {
2020-07-05 07:34:00 +02:00
return `User ${(Math.floor(Math.random() * 42) + 1)}`;
2020-07-05 10:08:22 +02:00
}
export function secondsToHMMSS(seconds = 0) {
const finiteSeconds = Number.isFinite(+seconds) ? Math.abs(seconds) : 0;
const hours = Math.floor(finiteSeconds / 3600);
const hoursString = hours ? `${hours}:` : '';
const mins = Math.floor((finiteSeconds / 60) % 60);
const minString = mins < 10 ? `0${mins}:` : `${mins}:`;
const secs = Math.floor(finiteSeconds % 60);
const secsString = secs < 10 ? `0${secs}` : `${secs}`;
return hoursString + minString + secsString;
}
export function setVHvar() {
var vh = window.innerHeight * 0.01;
// Then we set the value in the --vh custom property to the root of the document
document.documentElement.style.setProperty('--vh', `${vh}px`);
console.log("== new vh", vh)
}
export function doesObjectSupportFunction(object, functionName) {
return typeof object[functionName] === "function";
}