Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

helpers.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * A helper function that behaves similar to Object.assign, but only reassigns a
  3. * property in target if it's defined in source.
  4. *
  5. * @param {Object} target - The target object to assign the values into.
  6. * @param {Object} source - The source object.
  7. * @returns {Object}
  8. */
  9. export function assignIfDefined(target: Object, source: Object) {
  10. const to = Object(target);
  11. for (const nextKey in source) {
  12. if (source.hasOwnProperty(nextKey)) {
  13. const value = source[nextKey as keyof typeof source];
  14. if (typeof value !== 'undefined') {
  15. to[nextKey] = value;
  16. }
  17. }
  18. }
  19. return to;
  20. }
  21. /**
  22. * Creates a deferred object.
  23. *
  24. * @returns {{promise, resolve, reject}}
  25. */
  26. export function createDeferred() {
  27. const deferred: any = {};
  28. deferred.promise = new Promise((resolve, reject) => {
  29. deferred.resolve = resolve;
  30. deferred.reject = reject;
  31. });
  32. return deferred;
  33. }
  34. const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
  35. /**
  36. * Escape RegExp special characters.
  37. *
  38. * Based on https://github.com/sindresorhus/escape-string-regexp.
  39. *
  40. * @param {string} s - The regexp string to escape.
  41. * @returns {string}
  42. */
  43. export function escapeRegexp(s: string) {
  44. if (typeof s !== 'string') {
  45. throw new TypeError('Expected a string');
  46. }
  47. return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
  48. }
  49. /**
  50. * Returns the base URL of the app.
  51. *
  52. * @param {Object} w - Window object to use instead of the built in one.
  53. * @returns {string}
  54. */
  55. export function getBaseUrl(w: typeof window = window) {
  56. const doc = w.document;
  57. const base = doc.querySelector('base');
  58. if (base?.href) {
  59. return base.href;
  60. }
  61. const { protocol, host } = w.location;
  62. return `${protocol}//${host}`;
  63. }
  64. /**
  65. * Returns the namespace for all global variables, functions, etc that we need.
  66. *
  67. * @returns {Object} The namespace.
  68. *
  69. * NOTE: After React-ifying everything this should be the only global.
  70. */
  71. export function getJitsiMeetGlobalNS() {
  72. if (!window.JitsiMeetJS) {
  73. window.JitsiMeetJS = {};
  74. }
  75. if (!window.JitsiMeetJS.app) {
  76. window.JitsiMeetJS.app = {};
  77. }
  78. return window.JitsiMeetJS.app;
  79. }
  80. /**
  81. * Prints the error and reports it to the global error handler.
  82. *
  83. * @param {Error} e - The error object.
  84. * @param {string} msg - A custom message to print in addition to the error.
  85. * @returns {void}
  86. */
  87. export function reportError(e: Error, msg = '') {
  88. console.error(msg, e);
  89. window.onerror?.(msg, undefined, undefined, undefined, e);
  90. }
  91. /**
  92. * Adds alpha to a color css string.
  93. *
  94. * @param {string} color - The color string either in rgb... Or #... Format.
  95. * @param {number} opacity -The opacity(alpha) to apply to the color. Can take a value between 0 and 1, including.
  96. * @returns {string} - The color with applied alpha.
  97. */
  98. export function setColorAlpha(color: string, opacity: number) {
  99. if (!color) {
  100. return `rgba(0, 0, 0, ${opacity})`;
  101. }
  102. let b, g, r;
  103. try {
  104. if (color.startsWith('rgb')) {
  105. [ r, g, b ] = color.split('(')[1].split(')')[0].split(',').map(c => c.trim());
  106. } else if (color.startsWith('#')) {
  107. if (color.length === 4) {
  108. [ r, g, b ] = parseShorthandColor(color);
  109. } else {
  110. r = parseInt(color.substring(1, 3), 16);
  111. g = parseInt(color.substring(3, 5), 16);
  112. b = parseInt(color.substring(5, 7), 16);
  113. }
  114. } else {
  115. return color;
  116. }
  117. return `rgba(${r}, ${g}, ${b}, ${opacity})`;
  118. } catch {
  119. return color;
  120. }
  121. }
  122. /**
  123. * Gets the hexa rgb values for a shorthand css color.
  124. *
  125. * @param {string} color -
  126. * @returns {Array<number>} - Array containing parsed r, g, b values of the color.
  127. */
  128. function parseShorthandColor(color: string) {
  129. let b, g, r;
  130. r = color.substring(1, 2);
  131. r += r;
  132. r = parseInt(r, 16);
  133. g = color.substring(2, 3);
  134. g += g;
  135. g = parseInt(g, 16);
  136. b = color.substring(3, 4);
  137. b += b;
  138. b = parseInt(b, 16);
  139. return [ r, g, b ];
  140. }
  141. /**
  142. * Sorts an object by a sort function, same functionality as array.sort().
  143. *
  144. * @param {Object} object - The data object.
  145. * @param {Function} callback - The sort function.
  146. * @returns {void}
  147. */
  148. export function objectSort(object: Object, callback: Function) {
  149. return Object.entries(object)
  150. .sort(([ , a ], [ , b ]) => callback(a, b))
  151. .reduce((row, [ key, value ]) => {
  152. return { ...row,
  153. [key]: value };
  154. }, {});
  155. }