All files / src/dom dom.ts

8.12% Statements 23/283
3.57% Branches 5/140
10.75% Functions 10/93
8.15% Lines 23/282

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1x                                                                                                                                                                                                             2x 2x 2x 2x 2x 2x           2x                                         2x 2x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         12x 12x                           12x     12x 8x   4x                                                                                                                                                                                                                                                                                                                                                 3x 3x 3x                                                                                                                                                         1x                     1x     1x 1x                                                                                                                                      
import {DomWatcher} from '../dom/dom-watcher';
import {Defer} from '../func/defer';
import * as func from '../func/func';
import * as is from '../is/is';
import * as mathf from '../mathf/mathf';
 
/**
 * Given an element, returns the amount that the element has been scrolled
 * through in the the window.
 *
 * Example:
 * This method is particularly useful when you have a 100vh sticky element
 * and want a relative distance travelled percentage.
 *
 * ```
 * HTML
 * <div class="parent">
 *    <div class="child"><div class="content"></div>
 * </div>
 *
 * SASS
 * .parent
 *   position: relative
 *   height: 300vh  // Should be more than 100vh
 * .child
 *   position: sticky
 *   height: 100vh
 *   width: 100%
 *   top: 0
 * .contents
 *   position: relative
 *   height: 100vh
 *
 * JS
 *
 *  window.addEventListener('scroll', () => {
 *     let progress =
 *             dom.getElementScrolledPercent(this.stickyParent_);
 *     // 0 when the above the element,
 *     // increases as you scroll thorugh
 *     // 1 when the bottom of the element reaches the bottom of the screen.
 *     console.log(progress);
 *   });
 *
 *
 * ```
 *
 * Top and bottom offset.
 * You can offset the top and bottom so that the calculations are shifted.
 *
 * ```ts
 *
 *     let startOffset = 100;
 *     // Now progress will start when the element is 100px inview.
 *     // But still will complete normally at the bottom.
 *     let progress =
 *             mathf.getElementScrolledPercent(this.element, startOffset, 0);
 *
 *     let startOffset = window.innerHeight;
 *     // Now progress when the top of the element hits the top of the screen.
 *     // But still will complete normally at the bottom.
 *     let progress =
 *             mathf.getElementScrolledPercent(this.element, startOffset, 0);
 *
 *     let heightOffset = -100;
 *     // Entry as normal - 0 when the element first comes into view.
 *     // Now progress will complete get to 0 when there is still 100px
 *     // of the element still to be shown since it is virtually 100px shorter.
 *     let progress =
 *             mathf.getElementScrolledPercent(this.element, 0, heightOffset);
 *
 *     // Now progress will complete when the element has scrolled past
 *     // the center of the screen since we virually extend the height by
 *     // 50vh.
 *     let heightOffset = window.innerHeight * 0.5;
 *     let progress =
 *             mathf.getElementScrolledPercent(this.element, 0, heightOffset);
 *
 *     // Now progress will complete when the element has scrolled past
 *     // the very top of the screen.
 *     let heightOffset = window.innerHeight * 1;
 *     let progress =
 *             mathf.getElementScrolledPercent(this.element, 0, heightOffset);
 *
 * ```
 *
 * Provided the element is larger than the viewport height,
 * this will return 0% when the element is above the screen,
 * it increases in value as the user scrolls through the element and
 * finally when the BOTTOM of the element just passes the bottom of the screen
 * it returns 1.
 *
 * This method assumes that the tracked element is atleast more than 100vh.
 *
 * @param {HTMLElement} element The root element.
 * @param {number} startOffset A positive value (pixel) to offset the top position
 *     of the element.
 *     The start offset value should never be negative as technically, offseting
 *     to before the element comes into view doesn't have an effect and will
 *     only complicate things for you.
 *     Changing this value will affect the poitn at which your progress hits
 *     0 (starts).
 * @param {number} heightOffset A value (pixel) to add to the total height of
 *     the element.  Can be positive or negative values.  Think of this,
 *     shortening or growing your element virually.  Changing this value
 *     will affect the point at which your progress hits 1 (ends).
 * @param {boolean} Optionally remove percent clamping.  This means it can return
 *     values outside 0-1.
 * @param {number} Optionally pass the window.innerHeight value.  This is
 *    useful for cases in which you have cached the window.innerHeight value.
 *    One usecase for this is on mobile, the window.innerHeight value can change
 *    as the user scrolls (since the url bar appears and disappears). To avoid
 *    jumps in the return progress percent, you want to cache the
 *    window.innerHeight value and pass it here.
 * @return {number} percent The amount in percentage that the user has scrolled
 *     in the element.
 *
 */
export function getElementScrolledPercent(
  element: HTMLElement,
  startOffset = 0,
  heightOffset = 0,
  noClamp = false,
  innerHeight = 0
): number {
  const box = element.getBoundingClientRect();
  let wh;
 
  if (innerHeight) {
    wh = innerHeight;
  } else {
    wh = window.innerHeight;
  }
 
  // We need to calculate this so that we start the 0% when the element comes
  // in (the top of the element).  But the 100% is marked when the BOTTOM
  // of the element passes the bottom of the screen.
  const current = wh - (box.top + startOffset);
  const percent = current / (box.height - startOffset + heightOffset);
  return noClamp ? percent : mathf.clampAsPercent(percent);
}
 
/**
 * Given a rootElement and targetElement, returns the progress value of the
 * targetElement.
 *
 * The baseLine is a number that is from 0 to 1.  0 is the top of the element,
 * 0.5 would be the middle and 1 would be the bottom of the element.
 *
 * @see cssParallaxer.getElementProgressPoint for an in detail usecase of this
 * method.
 */
export function getElementProgressPoint(
  rootElement: HTMLElement,
  targetElement: HTMLElement,
  baseline = 0,
  startOffset = 0,
  heightOffset = 0,
  noClamp = false
): number {
  const y =
    Math.abs(
      rootElement.getBoundingClientRect().top -
        (targetElement.getBoundingClientRect().top +
          targetElement.offsetHeight * baseline)
    ) - startOffset;
 
  const totalScrollDistance = Math.abs(
    getScrollYAtPercent(rootElement, startOffset, heightOffset, 0, true) -
      getScrollYAtPercent(rootElement, startOffset, heightOffset, 1, true)
  );
  const percent = y / totalScrollDistance;
  return noClamp ? percent : mathf.clampAsPercent(percent);
}
 
/**
 * Inverse of getElementScrolledPercent.
 * Given an element on the page, finds the scrollY value in order
 * to scroll into that element by the provided percent.
 *
 * @param element
 * @param startOffset
 * @param heightOffset
 * @param percent A value between 0-1
 *
 * ```ts
 *
 * // Scroll halfway into the given element.
 * const scrollY = mathf.getScrollYAtPercent(element, 0, 0, 0.5);
 * window.scrollTo(0, scrollY);
 *
 * // Scroll 0.2 percent into the given element.
 * const scrollY = mathf.getScrollYAtPercent(element, 0, 0, 0.2);
 * window.scrollTo(0, scrollY);
 * ```
 */
export function getScrollYAtPercent(
  element: HTMLElement,
  startOffset = 0,
  heightOffset = 0,
  percent: number,
  withTransform = true
) {
  const wh = window.innerHeight;
  const top = withTransform
    ? getScrollTop(element)
    : getScrollTopWithoutTransforms(element);
  const start = top - wh + startOffset;
  const end = top - wh + element.offsetHeight + heightOffset;
  return mathf.lerp(start, end, percent);
}
 
/**
 * Sets a css variable to an element.
 *
 * ```ts
 *   dom.setCssVariable(
 *       myElement, '--chapter-height', '100px');
 * ```
 *
 * @param element The element to set the css variable.
 * @param name The name of the css variable.  Should start with --.  Exampe: --height
 * @param value The value to set.
 */
export function setCssVariable(
  element: HTMLElement,
  name: string,
  value: string | number
) {
  if (!element) {
    throw new Error(
      'You need specificy a valid element to apply a css variable'
    );
  }
  element.style.setProperty(name, String(value));
}
 
/**
 * Batch update css variables on an element.  This updates style in a
 * destructive manner and will override any other style elements.
 *
 * ```ts
 * dom.addVariables(element, { '--height': "200px", '--mycssvariable': '20px'});
 * ```
 * @param element
 * @param style
 */
export function setCssVariables(
  element: HTMLElement,
  variables: Record<string, string | number>
) {
  for (const key in variables) {
    element.style.setProperty(key, String(variables[key]));
  }
}
 
/**
 * Given a set of styles, add it to an element.
 *
 * ```ts
 * dom.addStyles(element, { left: "200px", '--mycssvariable': '20px'});
 * ```
 * @param element
 * @param style
 */
export function addStyles(
  element: HTMLElement,
  styles: {[key: string]: string}
) {
  for (const key in styles) {
    if (key.startsWith('--')) {
      setCssVariable(element, key, styles[key]);
    }
  }
  Object.assign(element.style, styles);
}
 
/**
 * Tests whether a given element is a descendant of another elemenet.
 *
 * Examples:
 * ```ts
 *  var isChild = dom.testDescendant(
 *      document.getElementById('button'),
 *      document.querySelector('.article'));
 *
 * // Whether the active focused element is under the article container.
 *  var isFocused = dom.testDescendant(document.activeElement,
 *      document.querySelector('.article'));
 * ```
 *
 * @param element The child element
 * @param parentElement The possible parent element
 * @return Whether element is a descendant of the provided parent element.
 */
export function testDescendant(
  element: HTMLElement,
  parentElement: HTMLElement
): boolean {
  let currentNode = element.parentNode;
  while (currentNode) {
    if (currentNode === parentElement) {
      return true;
    }
    currentNode = currentNode.parentNode;
  }
  return false;
}
 
/**
 * Tests whether a given video element is playing.
 * @param videoElement
 */
export function testVideoIsPlaying(video: HTMLVideoElement): boolean {
  return !video.paused && !video.ended && video.readyState > 2;
}
 
/**
 * Resets all videos inside of a given element.
 * @param element The element to search videos in for.
 */
export function resetAllVideosInElement(element: HTMLElement) {
  const videos = Array.from(element.querySelectorAll('video'));
  videos.forEach((video) => {
    video.currentTime = 0;
  });
}
 
/**
 * Pauses all videos inside of a given element.
 * @param element The element to search videos in for.
 * @param reset Whether to pause and reset the video to 0 seconds (start).
 */
export function pauseAllVideosInElement(element: HTMLElement, reset = false) {
  const videos = Array.from(element.querySelectorAll('video'));
  videos.forEach((video) => {
    if (reset) {
      video.currentTime = 0;
    }
    video.pause();
  });
}
 
/**
 * Plays all videos inside of a given element.
 * @param element The element to search videos in for.
 * @param reset Whether to start playing from currentTime 0.
 */
export function playAllVideosInElement(element: HTMLElement, reset = false) {
  const videos = Array.from(element.querySelectorAll('video'));
  videos.forEach((video) => {
    if (reset) {
      video.currentTime = 0;
    }
    if (!testVideoIsPlaying(video)) {
      const playPromise = video.play();
      if (playPromise !== undefined) {
        playPromise.then(() => {}).catch(() => {});
      }
    }
  });
}
 
/**
 * Allows you to make sure that all videos are loaded (readyState 4) in a
 * given element before executing callback.
 *
 * Note that this method executes polling in the background
 * to check for video state so use carefully.  Defaults to timeout after
 * 10000ms
 *
 * ```ts
 * // Wait for all videos to be loaded.
 * dom.whenVideosLoadedInElement(element).then(()=> {
 *   // Play all videos for example.
 *   dom.playAllVideosInElement(element);
 * })
 *
 * ```
 * @param element
 * @param timeout The amount of time in which to give up polling.
 * @return Promise
 */
export function whenVideosLoadedInElement(
  element: HTMLElement,
  timeout = 10000
): Promise<void> {
  const videos = Array.from(element.querySelectorAll('video'));
  return whenVideosLoaded(videos, timeout);
}
 
/**
 * Allows you to make sure that all videos are loaded (readyState 4)
 * before executing callback.
 *
 * Note that this method executes polling in the background
 * to check for video state so use carefully.  Defaults to timeout after
 * 10000ms
 *
 * ```ts
 * // Wait for all videos to be loaded.
 * const videos = [videoElement1, videoElement2];
 * dom.whenVideosLoaded(videos).then(()=> {
 *   //
 * })
 *
 * ```
 * @param element
 * @param timeout The amount of time in which to give up polling.
 * @return Promise
 */
export function whenVideosLoaded(
  videos: Array<HTMLVideoElement>,
  timeout = 10000
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const promises: Array<Promise<any>> = [];
  videos.forEach((video) => {
    const defer = new Defer();
    promises.push(defer.getPromise());
    func
      .waitUntil(() => video.readyState === 4, timeout, 10)
      .then(() => {
        defer.resolve();
      });
  });
 
  return Promise.all(promises);
}
 
/**
 * Fires a event on the element.
 *
 * ```ts
 * dom.event(document, 'myCustomEvent', { message: hello});
 *
 * // Data gets passed on the event.detail.
 * document.addEventListener('myCustomEvent', (e)=> {
 *   console.log(e.detail.message); // Hello
 * }, false)
 * ```
 *
 * @param element
 * @param name
 * @param data
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function event(element: HTMLElement, name: string, data: any) {
  const event = new CustomEvent(name, {detail: data});
  element.dispatchEvent(event);
}
 
/**
 * Creates an html element from a string.
 * ```
 * dom.createElementFromString("<div>Hohohoho</div>");
 * ```
 * @param htmlString
 * @param fragMethod Uses a method to create dom from string WITHOUT using innerHTML
 */
export function createElementFromString(
  htmlString: string,
  fragMethod = false
): HTMLElement {
  if (fragMethod) {
    const holder = document.createElement('div') as HTMLElement;
    const frag = document.createRange().createContextualFragment(htmlString);
    holder.appendChild(frag);
    return holder.firstElementChild as HTMLElement;
  } else {
    const div = document.createElement('div');
    div.innerHTML = htmlString.trim();
 
    return div.firstChild as HTMLElement;
  }
}
 
/**
 * Removes an element from the dom.
 * @param element
 */
export function removeElement(element: HTMLElement) {
  element.parentNode!.removeChild(element);
}
 
/**
 * Removes all children of a given element.  This can be prefereable over
 * `element.innerHTML = ''` since that does not remove listeners.
 */
export function removeAllChildren(element: HTMLElement) {
  while (element.firstChild) {
    element.removeChild(element.firstChild);
  }
}
 
/**
 * Executes a callback after window has loaded.
 * This isn't a simple window load event listening but
 * first makes a check if the window has already loaded.
 * If it has, the callback will immediately get executed,
 * otherwise, it will wait until the load event completes.
 * @param callback
 */
export function runAfterWindowLoad(callback: Function) {
  // Make sure we are at the bottom of the event
  // stack (hence setTimeout) - this avoids
  // edge cases in which windowLoaded is misevaluated.
  window.setTimeout(() => {
    if (is.windowLoaded()) {
      callback();
    } else {
      window.addEventListener(
        'load',
        () => {
          callback();
        },
        {once: true}
      );
    }
  });
}
 
/**
 * Executes a callback as soon as the user is not at the top of the screen.
 *
 * If the user is at the top of the screen when this is called, it will
 * wait until the user has scrolled down to execute.  If the user is already
 * not at the top of the screen, the callback will immediately execute.
 *
 * Note that if your document is less than 100vh, your callback will
 * never get executed because the user can't scroll and will always be at
 * the top of the screen.
 *
 * @param callback
 */
export function runAfterNotTopOfScreen(callback: Function) {
  if (window.scrollY !== 0) {
    callback();
  } else {
    window.addEventListener(
      'scroll',
      () => {
        callback();
      },
      {once: true, passive: true}
    );
  }
}
 
/**
 * Gets the current browser aspect ratio.
 * 2 --> wide rectangle
 * 1 --> squre
 * 0.5 --> tall rectangle
 */
export function aspect() {
  return window.innerWidth / window.innerHeight;
}
 
/**
 * Gets the computed style of a given element .
 * @param element
 * @return CSSStyleDeclartion
 */
export function getComputedStyle(element: HTMLElement): CSSStyleDeclaration {
  return window.getComputedStyle(element);
}
 
/**
 * Creates an image in memory that is later deletable so it can
 * be released from native memory AND image cache.
 *
 * This method can be paired with deleteImage.
 *
 * // Consider the following:
 * new Image();
 * image.src = 'hohoho.jpg';
 * image.decode().then(()=> {
 *   // Do something.
 *   myCanvas.drawImage(image);
 *   image = null;
 * })
 *
 * This does appear to get GCed and removed from image cache but it is
 * never flushed from native memory.
 *
 * You can observe this by going to Chrome -> Task manager.  Make sure
 * you have the Memory and also the Image Cache columns.
 *
 * Theefore, usage of image.decode() for now, can lead to memory leaks that
 * can't be cured with image=null.
 *
 * The same is observed with ImageBitmaps.
 *
 * Related.
 * https://bugs.webkit.org/show_bug.cgi?id=31253
 *
 *
 * The solution is to use createObjectURL and later revoke that
 * so memory can be released.
 *
 * ```
 * const imageSource = 'public/image/cat.jpg';
 *
 * // Cache the image in browser memory.
 *    dom.fetchAndMakeImage(imageSource).then((image)=> {
 *
 *       // Do something with image.
 *       myCanvas.drawImage(image);
 *
 *       // Delete from memory.
 *       dom.deleteImage(image);
 *    })
 *
 * ```
 */
export function fetchAndMakeImage(source: string): Promise<HTMLImageElement> {
  return new Promise((resolve) => {
    fetch(source)
      .then((response) => {
        return response.blob();
      })
      .then((response) => {
        const blob = response;
        const img = document.createElement('img');
        img.decoding = 'async';
        img.onload = () => {
          resolve(img);
        };
        img.src = URL.createObjectURL(blob);
      });
  });
}
 
/**
 * Makes images from a blob source.  Can be coupled with
 * delete image to quickly release memory.
 * @param blob
 */
export function makeImageFromBlob(blob: Blob): Promise<HTMLImageElement> {
  return new Promise((resolve) => {
    const img = document.createElement('img');
    img.decoding = 'async';
    img.onload = () => {
      img.onload = null;
      resolve(img);
    };
    img.src = URL.createObjectURL(blob);
  });
}
 
/**
 * Makes an image from a blob source.  This is an alternative to
 * URL.createObjectURL is to convert a Blob into a base64-encoded string.
 *
 *
 * Return somethig like:
 * <img src="data:image/png;base64,R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAA">
 *
 * @param blob
 */
export function makeBase64ImageFromBlob(blob: Blob): Promise<HTMLImageElement> {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob); // converts the blob to base64 and calls onload
 
    reader.addEventListener(
      'load',
      () => {
        const img = document.createElement('img');
        img.src = reader.result as string;
        img.onload = () => {
          img.onload = null;
          resolve(img);
        };
      },
      {once: true}
    );
  });
}
 
export function copyBase64Image(
  image: HTMLImageElement
): Promise<HTMLImageElement> {
  return new Promise((resolve) => {
    const img = document.createElement('img');
    img.src = image.src;
    img.onload = () => {
      img.onload = null;
      resolve(img);
    };
  });
}
 
/**
 * Deletes an image from memory.  Inverse action of
 * dom.makeBase64ImageFromBlob or dom.makeImageFromBlob.
 */
export function deleteImage(image: HTMLImageElement | null) {
  if (image) {
    // Delete ObjectURLs or base64
    if (
      image.src &&
      (image.src.startsWith('blob:') || image.src.startsWith('data:'))
    ) {
      URL.revokeObjectURL(image.src);
      image.src = '';
    }
    image = null;
  }
}
 
/**
 * Flushes all video sources in a given element.  This allow the VRAM to
 * flush.  Use unflushVideos to put it back.
 *
 * This is particularly useful to combine with inview.
 * Where normally, it would take Chrome a couple of seconds
 * to flush memory of an outview video, this would do it
 * explicitly release memory much quicker.
 *
 *
 * Current will only work with video with <source> not video with
 * src.
 * ```
 *  <video>
 *    <source src="xxx.mp4"></source>
 *    <source src="xxx.webm"></source>
 * </video>
 * ```
 *
 * Basically, this works by removig the src attribute from each video
 * and then force reloading it - flushing it from memory.
 *
 * ```ts
 *    let ev = elementVisibility.inview(
 *       el, {rootMargin: '100px 100px 100px 100px'}, () => {
 *       if (ev.state().ready) {
 *         if (ev.state().inview) {
 *           dom.unflushVideos(el);
 *         } else {
 *           dom.flushVideos(el);
 *         }
 *       }
 *     });
 *
 * ```
 *
 *
 * You may also have a case where you are lazyloading videos as such:
 *
 * ```
 * <video>
 *    <source lazyvideo-src="xxx.mp4"></source>
 * </video>
 * ```
 *
 * In this case, you can use the source attribute to specify the data-attritube
 * that contains the path to the source.
 *
 * ```ts
 *     dom.flushVideos(el, 'lazyvideo-src');
 * ```
 *
 * https://html.spec.whatwg.org/multipage/media.html#best-practices-for-authors-using-media-elements
 *
 * @param el The html element to search
 * @param sourceAttribute An optional attribute value to acquire the
 *   video source from.
 */
export function flushVideos(el: HTMLElement, sourceAttribute = '') {
  const videos = Array.from(el.querySelectorAll('video'));
  videos.forEach((video) => {
    const sources = Array.from(video.querySelectorAll('source'));
    sources.forEach((source: HTMLSourceElement | null) => {
      const src = source!.getAttribute(sourceAttribute) || source!.src;
      source!.setAttribute('data-video-src', src);
      source!.removeAttribute('src');
      source = null;
    });
 
    video.load();
  });
}
 
/**
 * Puts back video sources and undoes the effect of dom.flushVideos.
 * @param el The root element.
 * @param noPlay Whether to suppress autoplaying of videos when videos are
 *   unflushed.
 */
export function unflushVideos(el: HTMLElement, noPlay = false) {
  const videos = Array.from(el.querySelectorAll('video'));
  videos.forEach((video) => {
    const sources = Array.from(video.querySelectorAll('source'));
    sources.forEach((source) => {
      if (!source.hasAttribute('data-video-src')) {
        return;
      }
      const src = source.getAttribute('data-video-src')!;
      source.setAttribute('src', src);
    });
 
    video.load();
  });
 
  if (!noPlay) {
    playAllVideosInElement(el);
  }
}
 
/**
 * Gets the Y distance of the element from the top of the document.
 *
 * ```ts
 *   const top = dom.getScrollTop(el);
 *   window.scroll(0, top);
 * ```
 *
 */
export function getScrollTop(el: HTMLElement, includeParent = false): number {
  // Safe guard.
  if (!el) {
    return 0;
  }
 
  if (includeParent && el.offsetParent) {
    return (
      el.getBoundingClientRect().top -
      el.offsetParent.getBoundingClientRect().top +
      window.scrollY
    );
  } else {
    return el.getBoundingClientRect().top + window.scrollY;
  }
}
 
/**
 * Gets the Y distance of the element from the top of the document without
 * regard to any transforms applied.
 *
 * ```ts
 *   const top = dom.getScrollTopWithoutTransforms(el);
 *   window.scroll(0, top);
 * ```
 *
 */
export function getScrollTopWithoutTransforms(el: HTMLElement): number {
  let offsetTop = 0;
  do {
    offsetTop += el.offsetTop;
    el = el.offsetParent as HTMLElement;
  } while (el);
  return offsetTop;
}
 
/**
 * An attempt at override VO focus.
 * Requires the element to have a tabindex="-1" and then focusing.
 * To fully reinstante the original state prior to running force focus,
 * run resetForceFocus().
 *
 *
 * ---
 *
 * dom.forceFocus(myElement);
 *
 * window.setTimeout(()=> {
 *
 *   dom.forceFocus(myElement2);
 *
 * }, 2000)
 *
 *
 *
 * //  Some given time later, undo everything if needed.
 * dom.resetForceFocus();
 * ---
 */
export function forceFocus(el: HTMLElement) {
  // Check if we previously forced focused element in which case,
  // revert that to it's previously state.
  resetForceFocus();
 
  const currentIndex = el.getAttribute('tabindex');
  if (is.defined(currentIndex) && !is.nullLike(currentIndex)) {
    el.setAttribute('forcetabindex', currentIndex!);
  } else {
    el.setAttribute('forcetabindex', 'none');
  }
 
  el.setAttribute('tabindex', '-1');
  el.focus();
}
 
/**
 * Undos the effects of forceFocus which overrides the tabindex of focused
 * elements.
 */
export function resetForceFocus() {
  const previouslyFocusedElement = Array.from(
    document.querySelectorAll('[forcetabindex]')
  );
  previouslyFocusedElement.forEach((element) => {
    const tabIndex = element.getAttribute('forcetabindex');
    if (is.defined(tabIndex) && !is.nullLike(tabIndex) && tabIndex !== 'none') {
      element.setAttribute('tabindex', tabIndex!);
    } else {
      element.removeAttribute('tabindex');
    }
    element.removeAttribute('forcetabindex');
  });
}
 
/**
 * Apart from .forceFocus, another alternative workaround for
 * implementing forces VO focus.
 * Thank you to: https://silvantroxler.ch/2016/setting-voiceover-focus-with-javascript/
 * @param el
 */
export function forceVOFocus(
  element: HTMLElement,
  interval = 10,
  repetition = 10
) {
  const focusInterval = interval; // ms, time between function calls
  const focusTotalRepetitions = repetition; // number of repetitions
 
  element.setAttribute('tabindex', '0');
  element.blur();
 
  let focusRepetitions = 0;
  const int = window.setInterval(() => {
    element.focus();
    focusRepetitions++;
    if (focusRepetitions >= focusTotalRepetitions) {
      window.clearInterval(int);
    }
  }, focusInterval);
}
 
/**
 * Returns a list of focusable child elements from a parent element.
 */
export function getFocusableElements(element: HTMLElement): HTMLElement[] {
  return Array.from(
    element.querySelectorAll(
      'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
    )
  );
}
 
/**
 * Gets all focusable elements within an element and sets it to the given tabindex.
 * This method is paired with "unsetFocusableElementsToTabIndex" to undo this
 * action.
 *
 * ```
 * # Set all focusable elements to -1 tabindex.
 *   dom.setFocusableElementsToTabIndex(parentElement, -1);
 *
 * # Later undo this effect.  Focusable elements revert to their original tab index.
 *   dom.unsetFocusableElementsToTabIndex(parentElement);
 * ```
 */
export function setFocusableElementsToTabIndex(
  element: HTMLElement,
  tabindex: number
) {
  const elements: HTMLElement[] = getFocusableElements(element);
  elements.forEach((element) => {
    const currentTabIndex = element.tabIndex;
    if (!element.hasAttribute('originalTabIndex')) {
      element.setAttribute('originalTabIndex', currentTabIndex + '');
    }
    element.tabIndex = tabindex;
  });
}
 
/**
 * Reverts all focusable elements within an element to its original tabindex
 * after the 'dom.setFocusableElementsToTabIndex()` method has been executed.
 */
export function unsetFocusableElementsToTabIndex(element: HTMLElement) {
  const elements: HTMLElement[] = Array.from(
    element.querySelectorAll('[originalTabIndex]')
  );
 
  elements.forEach((element) => {
    const originalTabIndex: string =
      element.getAttribute('originalTabIndex') || '';
    element.tabIndex = +originalTabIndex;
  });
}
 
/**
 * Gets the current styles on a given element.
 *
 * ```
 * var style = dom.getStyle(el);
 *
 * style.marginTop;
 * style.paddingLeft;
 *
 * ```
 */
export function getStyle(el: Element): CSSStyleDeclaration {
  return window.getComputedStyle(el);
}
 
/**
 * Tests whether the provided element is set to display none.
 */
export function isDisplayNone(el: Element): boolean {
  const style = window.getComputedStyle(el).display;
  return style === 'none';
}
 
/**
 * Tests whether a given element and it's ancestors have
 * a display:none.  This is useful to see if a given element
 * is on the screen (based on whether if the element or it's ancestors have
 * display none.)
 *
 * ```
 * const elementIsVisibleOnScreen = !dom.isDisplayNoneWithAncestors(element);
 * ```
 */
export function isDisplayNoneWithAncestors(el: Element): boolean {
  Iif (isDisplayNone(el)) {
    return true;
  }
  if (el.parentElement) {
    return isDisplayNoneWithAncestors(el.parentElement);
  }
  return false;
}
 
/**
 * Tries to determine if the element is currently visible on the
 * screen.  Note this is an attempt and is not a guarantee as is based
 * on whether the element or its parents have opacity, visibility or
 * display set to a hidden state.
 * @param el
 */
export function isVisibleOnScreen(el: HTMLElement): boolean {
  const checkVisibility = (el: HTMLElement): boolean => {
    const styles = getComputedStyle(el as HTMLElement);
    return !(
      styles.opacity !== '1' ||
      styles.visibility === 'hidden' ||
      styles.display === 'none'
    );
  };
 
  const isVisible = checkVisibility(el);
  if (!isVisible) {
    return false;
  }
  if (el.parentElement) {
    return isVisibleOnScreen(el.parentElement);
  }
  return true;
}
 
/**
 * Removes all classes from an element that starts with a given prefix.
 * https://stackoverflow.com/questions/28608587/how-to-remove-a-class-that-starts-with
 * @param el
 * @param prefix
 */
export function removeClassByPrefix(el: HTMLElement, prefix: string) {
  const reg = new RegExp('\\b' + prefix + '.*?\\b', 'g');
  el.className = el.className.replace(reg, '');
  return el;
}
 
/**
 * Returns all text nodes under a given element.
 * https://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
 * @param el
 */
export function getAllTextNodes(el: HTMLElement): Array<Node> {
  let n;
  const a = [];
  const walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
  while ((n = walk.nextNode())) a.push(n);
  return a;
}
 
export function appendAfter(
  nodeToAdd: HTMLElement,
  nodeToAddAfter: HTMLElement
) {
  nodeToAddAfter.parentNode!.insertBefore(
    nodeToAdd,
    nodeToAddAfter.nextSibling
  );
}
 
/**
 * Used for cases in which you want to prevent scroll on the page.
 *
 * Technically, this prevents wheel and touchmove events.
 *
 * ```
 * // Disable scrolling.
 * const reenable = dom.domDisableScrolling();
 *
 * // Renable.
 * reenable();
 * ```
 */
export function disableScrolling() {
  const disabler = (e: Event) => {
    e.preventDefault();
  };
 
  const domWatcher = new DomWatcher();
  domWatcher.add({
    element: window,
    on: 'wheel',
    callback: disabler,
    eventOptions: {
      passive: false,
    },
  });
  domWatcher.add({
    element: window,
    on: 'touchmove',
    callback: disabler,
    eventOptions: {
      passive: false,
    },
  });
 
  return () => {
    domWatcher.dispose();
  };
}
 
/**
 * Adds &nbsps to between the second to last and last word
 * to avoid unorphanization.
 *
 * ```
 *       // Unorphanize all text nodes.
 *       dom.unorphan(element as HTMLElement);
 *
 *       // No nesting (just the last text node)
 *       dom.unorphan(element as HTMLElement, true);
 * ```
 * @param el
 */
export function unorphan(el: HTMLElement, lastOnly = false): void {
  let allTextNodes = getAllTextNodes(el as HTMLElement);
  if (lastOnly) {
    allTextNodes = [allTextNodes[allTextNodes.length - 1]];
  }
 
  const nbsp = '\xA0';
  allTextNodes.forEach((node) => {
    node.nodeValue = node.nodeValue!.replace(/\s+([^\s]*)\s*$/, nbsp + '$1');
  });
}
 
export function getScrollElement(): Element {
  return document.scrollingElement || document.documentElement;
}
 
/**
 * Determines if two elements are overlapping or not.
 * @param aElement
 * @param bElement
 * https://stackoverflow.com/questions/12066870/how-to-check-if-an-element-is-overlapping-other-elements
 */
export function isOverlapping(
  aElement: HTMLElement,
  bElement: HTMLElement
): boolean {
  // Element Can't overlap with itself.
  if (aElement === bElement) {
    return false;
  }
 
  const rect1 = aElement.getBoundingClientRect();
  const rect2 = bElement.getBoundingClientRect();
  return !(
    rect1.right < rect2.left ||
    rect1.left > rect2.right ||
    rect1.bottom < rect2.top ||
    rect1.top > rect2.bottom
  );
}
 
/**
 * Adds the given cssStyle to the page.
 * @param cssStyle
 *
 * ```
 * dom.addStylesToPage(".myclass { background: blue}");
 * ```
 */
export function addStylesToPage(cssStyle: string) {
  const style = document.createElement('style');
  style.textContent = cssStyle;
  document.head.appendChild(style);
}
 
/**
 * https://stackoverflow.com/questions/54520554/custom-element-getrootnode-closest-function-crossing-multiple-parent-shadowd
 *
 * Looks for the closest element that matches the selector across the shadow DOM
 * and standard DOM boundaries.
 *
 * ```
 * dom.closestElementComposed(".my-shadow-dom-parent", element);
 * ```
 */
export function closestElementComposed(selector, baseElement) {
  function closestFrom(baseElement) {
    if (!baseElement || baseElement === document || baseElement === window)
      return null;
    const found = baseElement.closest(selector);
    return found ? found : closestFrom(baseElement.getRootNode().host);
  }
  return closestFrom(baseElement);
}
 
/**
 * Finds all loading="lazy" within an element and removes the attribute.
 * The context and reason why you may want to use this is because of how
 * native lazy loading works.  Native lazy image loading will only load an image
 * when it is rendered and painted on the screen.  Say you have
 * a carousel module with many slides.  Even if the user has scrolled into
 * the module, carousel image in slides 2,3 etc with lazy loading will
 * not load until the user clicks into that slide and the image is painted.
 *
 * To avoid this, you can run this when the module comes into view.
 */
export function removeNativeLazyImage(rootElement: HTMLElement) {
  const lazyElements: HTMLElement[] = Array.from(
    rootElement.querySelectorAll('[loading=lazy]')
  );
 
  lazyElements.forEach((element) => {
    element.removeAttribute('loading');
  });
}
 
/**
 * Listens for a specified event on any element within the provided element and
 * then recursively calls the listener on all ancestors of the originating
 * element.
 *
 * For example a given module,  when any click event happens within the module,
 * call your listener repeatedly starting from the originating element and all
 * of its ancestors (bubble up) up to the document root.
 *
 * ```ts
 *   dom.addDelegatedListener(
 *     myModuleElement || document.documentElement,
 *     'click',
 *     (e: HTMLElement, event: any) => {
 *       if (!e) {
 *         return;
 *       }
 *        // Called on the element that was clicked and it's parent
 *        // recursively until the document root.
 *        ...
 *     }
 *   );
 * ```
 *
 * @param {Element} el Element to host the listener.
 * @param {string} type Listener type.
 * @param {function} listener Listener function.
 */
export function addDelegatedListener(
  el: HTMLElement,
  type: string,
  listener: Function
) {
  const handler = (e: any) => {
    e = e || window.event;
    let target = e.target || e.srcElement;
    target = target.nodeType === 3 ? target.parentNode : target;
    do {
      listener(target, e);
      if (target.parentNode) {
        target = target.parentNode;
      }
    } while (target.parentNode);
  };
  el.addEventListener(type, handler);
 
  // Return a function to remove the listener.
  return () => {
    el.removeEventListener(type, handler);
  };
}
 
/**
 * Smoothly scrolls the window vertically to `top` over `duration` ms using
 * requestAnimationFrame (swing easing), cancelling automatically on user
 * interaction (wheel, mousedown, touchmove, keyup).
 *
 * Returns a cancel function that stops the animation and cleans up listeners.
 */
export function smoothScrollTo(
  top: number,
  duration = 500,
  onComplete?: () => void
): () => void {
  const startY = window.scrollY;
  const distance = top - startY;
 
  if (duration <= 0 || distance === 0) {
    window.scrollTo(window.scrollX, top);
    onComplete && onComplete();
    return () => {};
  }
 
  let rafId: number | null = null;
  let finished = false;
  const startTime = performance.now();
  const cancelEvents = ['wheel', 'mousedown', 'touchmove', 'keyup'];
 
  const finish = () => {
    if (finished) {
      return;
    }
    finished = true;
    if (rafId !== null) {
      cancelAnimationFrame(rafId);
      rafId = null;
    }
    for (const ev of cancelEvents) {
      window.removeEventListener(ev, finish);
    }
    onComplete && onComplete();
  };
 
  for (const ev of cancelEvents) {
    window.addEventListener(ev, finish, {passive: true});
  }
 
  const step = (now: number) => {
    if (finished) {
      return;
    }
    const progress = Math.min(1, (now - startTime) / duration);
    // Swing easing (matches jQuery's default easing).
    const eased = 0.5 - Math.cos(progress * Math.PI) / 2;
    window.scrollTo(window.scrollX, startY + distance * eased);
    if (progress < 1) {
      rafId = requestAnimationFrame(step);
    } else {
      finish();
    }
  };
 
  rafId = requestAnimationFrame(step);
  return finish;
}