You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIUtil.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* global $, config, interfaceConfig */
  2. /**
  3. * Created by hristo on 12/22/14.
  4. */
  5. var UIUtil = {
  6. /**
  7. * Returns the size of the side panel.
  8. */
  9. getSidePanelSize () {
  10. var availableHeight = window.innerHeight;
  11. var availableWidth = window.innerWidth;
  12. var panelWidth = 200;
  13. if (availableWidth * 0.2 < 200) {
  14. panelWidth = availableWidth * 0.2;
  15. }
  16. return [panelWidth, availableHeight];
  17. },
  18. /**
  19. * Returns the available video width.
  20. */
  21. getAvailableVideoWidth: function (isSidePanelVisible) {
  22. let rightPanelWidth = 0;
  23. if (isSidePanelVisible) {
  24. rightPanelWidth = UIUtil.getSidePanelSize()[0];
  25. }
  26. return window.innerWidth - rightPanelWidth;
  27. },
  28. /**
  29. * Changes the style class of the element given by id.
  30. */
  31. buttonClick: function(id, classname) {
  32. $(id).toggleClass(classname); // add the class to the clicked element
  33. },
  34. /**
  35. * Returns the text width for the given element.
  36. *
  37. * @param el the element
  38. */
  39. getTextWidth: function (el) {
  40. return (el.clientWidth + 1);
  41. },
  42. /**
  43. * Returns the text height for the given element.
  44. *
  45. * @param el the element
  46. */
  47. getTextHeight: function (el) {
  48. return (el.clientHeight + 1);
  49. },
  50. /**
  51. * Plays the sound given by id.
  52. *
  53. * @param id the identifier of the audio element.
  54. */
  55. playSoundNotification: function (id) {
  56. document.getElementById(id).play();
  57. },
  58. /**
  59. * Escapes the given text.
  60. */
  61. escapeHtml: function (unsafeText) {
  62. return $('<div/>').text(unsafeText).html();
  63. },
  64. /**
  65. * Unescapes the given text.
  66. *
  67. * @param {string} safe string which contains escaped html
  68. * @returns {string} unescaped html string.
  69. */
  70. unescapeHtml: function (safe) {
  71. return $('<div />').html(safe).text();
  72. },
  73. imageToGrayScale: function (canvas) {
  74. var context = canvas.getContext('2d');
  75. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  76. var pixels = imgData.data;
  77. for (var i = 0, n = pixels.length; i < n; i += 4) {
  78. var grayscale
  79. = pixels[i] * 0.3 + pixels[i+1] * 0.59 + pixels[i+2] * 0.11;
  80. pixels[i ] = grayscale; // red
  81. pixels[i+1] = grayscale; // green
  82. pixels[i+2] = grayscale; // blue
  83. // pixels[i+3] is alpha
  84. }
  85. // redraw the image in black & white
  86. context.putImageData(imgData, 0, 0);
  87. },
  88. setTooltip: function (element, key, position) {
  89. element.setAttribute("data-i18n", "[data-content]" + key);
  90. element.setAttribute("data-toggle", "popover");
  91. element.setAttribute("data-placement", position);
  92. element.setAttribute("data-html", true);
  93. element.setAttribute("data-container", "body");
  94. },
  95. /**
  96. * Inserts given child element as the first one into the container.
  97. * @param container the container to which new child element will be added
  98. * @param newChild the new element that will be inserted into the container
  99. */
  100. prependChild: function (container, newChild) {
  101. var firstChild = container.childNodes[0];
  102. if (firstChild) {
  103. container.insertBefore(newChild, firstChild);
  104. } else {
  105. container.appendChild(newChild);
  106. }
  107. },
  108. isButtonEnabled: function (name) {
  109. var isEnabled = interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1;
  110. if (name === 'recording') {
  111. return isEnabled && config.enableRecording;
  112. }
  113. return isEnabled;
  114. },
  115. hideDisabledButtons: function (mappings) {
  116. var selector = Object.keys(mappings)
  117. .map(function (buttonName) {
  118. return UIUtil.isButtonEnabled(buttonName)
  119. ? null : mappings[buttonName]; })
  120. .filter(function (item) { return item; })
  121. .join(',');
  122. $(selector).hide();
  123. },
  124. redirect (url) {
  125. window.location.href = url;
  126. },
  127. isFullScreen () {
  128. return document.fullScreen
  129. || document.mozFullScreen
  130. || document.webkitIsFullScreen;
  131. },
  132. /**
  133. * Create html attributes string out of object properties.
  134. * @param {Object} attrs object with properties
  135. * @returns {String} string of html element attributes
  136. */
  137. attrsToString: function (attrs) {
  138. return Object.keys(attrs).map(
  139. key => ` ${key}="${attrs[key]}"`
  140. ).join(' ');
  141. }
  142. };
  143. export default UIUtil;