Home Reference Source

cables_dev/cables/src/core/cgl/cgl_utils.js

  1. /** @namespace CGL */
  2.  
  3. /**
  4. * multiply to get radians from degree, e.g. `360 * CGL.DEG2RAD`
  5. * @const {Number}
  6. * @memberof CGL
  7. * @static
  8. */
  9. export const DEG2RAD = Math.PI / 180.0;
  10.  
  11. /**
  12. * to get degrees from radians, e.g. `3.14 * CGL.RAD2DEG`
  13. * @const {number}
  14. * @memberof CGL
  15. */
  16. export const RAD2DEG = 180.0 / Math.PI;
  17.  
  18. export const onLoadingAssetsFinished = null; // deprecated / remove later
  19.  
  20. /**
  21. * get normalized mouse wheel delta (including browser specific adjustment)
  22. * @function getWheelDelta
  23. * @static
  24. * @memberof CGL
  25. * @param {MouseEvent} event
  26. * @return {Number} normalized delta
  27. */
  28. export const isWindows = window.navigator.userAgent.contains("Windows");
  29. const getWheelDelta_ = function (event)
  30. {
  31. let normalized;
  32. if (event.wheelDelta)
  33. {
  34. // chrome
  35. normalized = (event.wheelDelta % 120) - 0 == -0 ? event.wheelDelta / 120 : event.wheelDelta / 30;
  36. normalized *= -1.5;
  37. if (isWindows) normalized *= 2;
  38. }
  39. else
  40. {
  41. // firefox
  42. let d = event.deltaY;
  43. if (event.shiftKey) d = event.deltaX;
  44. const rawAmmount = d || event.detail;
  45. normalized = -(rawAmmount % 3 ? rawAmmount * 10 : rawAmmount / 3);
  46. normalized *= -3;
  47. }
  48.  
  49. if (normalized > 20) normalized = 20;
  50. if (normalized < -20) normalized = -20;
  51.  
  52. return normalized;
  53. };
  54.  
  55. export const getWheelSpeed = getWheelDelta_;
  56. export const getWheelDelta = getWheelDelta_;
  57.  
  58. // from https://github.com/lodash/lodash/blob/master/escape.js
  59.  
  60. const htmlEscapes = {
  61. "&": "&amp;",
  62. "<": "&lt;",
  63. ">": "&gt;",
  64. "\"": "&quot;",
  65. "'": "&#39;",
  66. };
  67.  
  68. /** Used to match HTML entities and HTML characters. */
  69. const reUnescapedHtml = /[&<>"']/g;
  70. const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
  71.  
  72. /* eslint-disable */
  73. export const escapeHTML = function(string)
  74. {
  75. return string && reHasUnescapedHtml.test(string) ?
  76. string.replace(reUnescapedHtml, function(chr) { return htmlEscapes[chr]; })
  77. : string || "";
  78. }
  79. /* eslint-enable */