您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

util.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* global $ */
  2. /**
  3. * Utility functions.
  4. */
  5. var Util = (function (my) {
  6. /**
  7. * Returns the text width for the given element.
  8. *
  9. * @param el the element
  10. */
  11. my.getTextWidth = function (el) {
  12. return (el.clientWidth + 1);
  13. };
  14. /**
  15. * Returns the text height for the given element.
  16. *
  17. * @param el the element
  18. */
  19. my.getTextHeight = function (el) {
  20. return (el.clientHeight + 1);
  21. };
  22. /**
  23. * Casts the given number to integer.
  24. *
  25. * @param number the number to cast
  26. */
  27. my.toInteger = function (number) {
  28. return Math.round(Number(number));
  29. };
  30. /**
  31. * Plays the sound given by id.
  32. *
  33. * @param id the identifier of the audio element.
  34. */
  35. my.playSoundNotification = function (id) {
  36. document.getElementById(id).play();
  37. };
  38. /**
  39. * Escapes the given text.
  40. */
  41. my.escapeHtml = function (unsafeText) {
  42. return $('<div/>').text(unsafeText).html();
  43. };
  44. /**
  45. * Returns the available video width.
  46. */
  47. my.getAvailableVideoWidth = function () {
  48. var chatspaceWidth
  49. = (Chat.isVisible() || ContactList.isVisible())
  50. ? $('#chatspace').width()
  51. : 0;
  52. return window.innerWidth - chatspaceWidth;
  53. };
  54. my.imageToGrayScale = function (canvas) {
  55. var context = canvas.getContext('2d');
  56. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  57. var pixels = imgData.data;
  58. for (var i = 0, n = pixels.length; i < n; i += 4) {
  59. var grayscale
  60. = pixels[i] * .3 + pixels[i+1] * .59 + pixels[i+2] * .11;
  61. pixels[i ] = grayscale; // red
  62. pixels[i+1] = grayscale; // green
  63. pixels[i+2] = grayscale; // blue
  64. // pixels[i+3] is alpha
  65. }
  66. // redraw the image in black & white
  67. context.putImageData(imgData, 0, 0);
  68. };
  69. my.setTooltip = function (element, tooltipText, position) {
  70. element.setAttribute("data-content", tooltipText);
  71. element.setAttribute("data-toggle", "popover");
  72. element.setAttribute("data-placement", position);
  73. element.setAttribute("data-html", true);
  74. element.setAttribute("data-container", "body");
  75. };
  76. return my;
  77. }(Util || {}));