2017-12-05 20:34:24 +00:00
|
|
|
// @flow
|
|
|
|
|
2020-05-18 12:07:09 +00:00
|
|
|
/**
|
|
|
|
* A helper function that behaves similar to Object.assign, but only reassigns a
|
|
|
|
* property in target if it's defined in source.
|
|
|
|
*
|
|
|
|
* @param {Object} target - The target object to assign the values into.
|
|
|
|
* @param {Object} source - The source object.
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function assignIfDefined(target: Object, source: Object) {
|
|
|
|
const to = Object(target);
|
|
|
|
|
|
|
|
for (const nextKey in source) {
|
|
|
|
if (source.hasOwnProperty(nextKey)) {
|
|
|
|
const value = source[nextKey];
|
|
|
|
|
|
|
|
if (typeof value !== 'undefined') {
|
|
|
|
to[nextKey] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
2020-06-24 07:34:00 +00:00
|
|
|
/**
|
|
|
|
* Tries to copy a given text to the clipboard.
|
|
|
|
* Returns true if the action succeeds.
|
|
|
|
*
|
|
|
|
* @param {string} textToCopy - Text to be copied.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function copyText(textToCopy: string) {
|
|
|
|
const fakeTextArea = document.createElement('textarea');
|
|
|
|
let result;
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
document.body.appendChild(fakeTextArea);
|
|
|
|
fakeTextArea.value = textToCopy;
|
|
|
|
fakeTextArea.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = document.execCommand('copy');
|
|
|
|
} catch (err) {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
document.body.removeChild(fakeTextArea);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a deferred object.
|
|
|
|
*
|
|
|
|
* @returns {{promise, resolve, reject}}
|
|
|
|
*/
|
|
|
|
export function createDeferred(): Object {
|
|
|
|
const deferred = {};
|
|
|
|
|
|
|
|
deferred.promise = new Promise((resolve, reject) => {
|
|
|
|
deferred.resolve = resolve;
|
|
|
|
deferred.reject = reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
}
|
|
|
|
|
2019-08-23 14:41:10 +00:00
|
|
|
const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape RegExp special characters.
|
|
|
|
*
|
|
|
|
* Based on https://github.com/sindresorhus/escape-string-regexp.
|
|
|
|
*
|
|
|
|
* @param {string} s - The regexp string to escape.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function escapeRegexp(s: string) {
|
|
|
|
if (typeof s !== 'string') {
|
|
|
|
throw new TypeError('Expected a string');
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
|
|
|
|
}
|
|
|
|
|
2019-07-03 15:39:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the base URL of the app.
|
|
|
|
*
|
|
|
|
* @param {Object} w - Window object to use instead of the built in one.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function getBaseUrl(w: Object = window) {
|
|
|
|
const doc = w.document;
|
|
|
|
const base = doc.querySelector('base');
|
|
|
|
|
|
|
|
if (base && base.href) {
|
|
|
|
return base.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { protocol, host } = w.location;
|
|
|
|
|
|
|
|
return `${protocol}//${host}`;
|
|
|
|
}
|
|
|
|
|
2017-09-01 19:14:03 +00:00
|
|
|
/**
|
|
|
|
* Returns the namespace for all global variables, functions, etc that we need.
|
|
|
|
*
|
|
|
|
* @returns {Object} The namespace.
|
|
|
|
*
|
|
|
|
* NOTE: After React-ifying everything this should be the only global.
|
|
|
|
*/
|
|
|
|
export function getJitsiMeetGlobalNS() {
|
|
|
|
if (!window.JitsiMeetJS) {
|
|
|
|
window.JitsiMeetJS = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window.JitsiMeetJS.app) {
|
|
|
|
window.JitsiMeetJS.app = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return window.JitsiMeetJS.app;
|
|
|
|
}
|
2018-04-12 19:58:20 +00:00
|
|
|
|
2018-07-21 15:16:32 +00:00
|
|
|
/**
|
|
|
|
* Prints the error and reports it to the global error handler.
|
|
|
|
*
|
|
|
|
* @param {Error} e - The error object.
|
|
|
|
* @param {string} msg - A custom message to print in addition to the error.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export function reportError(e: Object, msg: string = '') {
|
2019-09-06 11:02:39 +00:00
|
|
|
console.error(msg, e);
|
2018-07-21 15:16:32 +00:00
|
|
|
window.onerror && window.onerror(msg, null, null, null, e);
|
|
|
|
}
|