domutils.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ArrayExt } from '@phosphor/algorithm';
  4. import { ElementExt } from '@phosphor/domutils';
  5. import { UUID } from '@phosphor/coreutils';
  6. /**
  7. * The namespace for DOM utilities.
  8. */
  9. export namespace DOMUtils {
  10. /**
  11. * Get the index of the node at a client position, or `-1`.
  12. */
  13. export function hitTestNodes(
  14. nodes: HTMLElement[] | HTMLCollection,
  15. x: number,
  16. y: number
  17. ): number {
  18. return ArrayExt.findFirstIndex(nodes, node => {
  19. return ElementExt.hitTest(node, x, y);
  20. });
  21. }
  22. /**
  23. * Find the first element matching a class name.
  24. */
  25. export function findElement(
  26. parent: HTMLElement,
  27. className: string
  28. ): HTMLElement {
  29. return parent.querySelector(`.${className}`) as HTMLElement;
  30. }
  31. /**
  32. * Create a DOM id with prefix "id-" to solve bug for UUIDs beginning with numbers.
  33. */
  34. export function createDomID(): string {
  35. return `id-${UUID.uuid4()}`;
  36. }
  37. }