Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

isElementClickable.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function isElementClickable(elem) {
  4. if (!elem.getBoundingClientRect || !elem.scrollIntoView || !elem.contains || !elem.getClientRects || !document.elementFromPoint) {
  5. return false;
  6. }
  7. const isOldEdge = !!window.StyleMedia;
  8. const scrollIntoViewFullSupport = !(window.safari || isOldEdge);
  9. function getOverlappingElement(elem, context) {
  10. context = context || document;
  11. const elemDimension = elem.getBoundingClientRect();
  12. const x = elemDimension.left + (elem.clientWidth / 2);
  13. const y = elemDimension.top + (elem.clientHeight / 2);
  14. return context.elementFromPoint(x, y);
  15. }
  16. function getOverlappingRects(elem, context) {
  17. context = context || document;
  18. const elems = [];
  19. const rects = elem.getClientRects();
  20. const rect = rects[0];
  21. const x = rect.left + (rect.width / 2);
  22. const y = rect.top + (rect.height / 2);
  23. elems.push(context.elementFromPoint(x, y));
  24. return elems;
  25. }
  26. function getOverlappingElements(elem, context) {
  27. return [getOverlappingElement(elem, context)].concat(getOverlappingRects(elem, context));
  28. }
  29. function nodeContains(elem, otherNode) {
  30. if (isOldEdge) {
  31. let tmpElement = otherNode;
  32. while (tmpElement) {
  33. if (tmpElement === elem) {
  34. return true;
  35. }
  36. tmpElement = tmpElement.parentNode;
  37. if (tmpElement && tmpElement.nodeType === 11 && tmpElement.host) {
  38. tmpElement = tmpElement.host;
  39. }
  40. }
  41. return false;
  42. }
  43. return elem.contains(otherNode);
  44. }
  45. function isOverlappingElementMatch(elementsFromPoint, elem) {
  46. if (elementsFromPoint.some(function (elementFromPoint) {
  47. return elementFromPoint === elem || nodeContains(elem, elementFromPoint);
  48. })) {
  49. return true;
  50. }
  51. let elemsWithShadowRoot = [].concat(elementsFromPoint);
  52. elemsWithShadowRoot = elemsWithShadowRoot.filter(function (x) {
  53. return x && x.shadowRoot && x.shadowRoot.elementFromPoint;
  54. });
  55. let shadowElementsFromPoint = [];
  56. for (let i = 0; i < elemsWithShadowRoot.length; ++i) {
  57. let shadowElement = elemsWithShadowRoot[i];
  58. shadowElementsFromPoint = shadowElementsFromPoint.concat(getOverlappingElements(elem, shadowElement.shadowRoot));
  59. }
  60. shadowElementsFromPoint = [].concat(shadowElementsFromPoint);
  61. shadowElementsFromPoint = shadowElementsFromPoint.filter(function (x) {
  62. return !elementsFromPoint.includes(x);
  63. });
  64. if (shadowElementsFromPoint.length === 0) {
  65. return false;
  66. }
  67. return isOverlappingElementMatch(shadowElementsFromPoint, elem);
  68. }
  69. function isElementInViewport(elem) {
  70. if (!elem.getBoundingClientRect) {
  71. return false;
  72. }
  73. const rect = elem.getBoundingClientRect();
  74. const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
  75. const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
  76. const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) > 0);
  77. const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) > 0);
  78. return (vertInView && horInView);
  79. }
  80. function isClickable(elem) {
  81. return (isElementInViewport(elem) && elem.disabled !== true &&
  82. isOverlappingElementMatch(getOverlappingElements(elem), elem));
  83. }
  84. if (!isClickable(elem)) {
  85. elem.scrollIntoView(scrollIntoViewFullSupport ? { block: 'nearest', inline: 'nearest' } : false);
  86. if (!isClickable(elem)) {
  87. elem.scrollIntoView(scrollIntoViewFullSupport ? { block: 'center', inline: 'center' } : true);
  88. return isClickable(elem);
  89. }
  90. }
  91. return true;
  92. }
  93. exports.default = isElementClickable;