gitea源码

observer.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {isDocumentFragmentOrElementNode} from '../utils/dom.ts';
  2. import type {Promisable} from '../types.ts';
  3. import type {InitPerformanceTracer} from './init.ts';
  4. let globalSelectorObserverInited = false;
  5. type SelectorHandler = {selector: string, handler: (el: HTMLElement) => void};
  6. const selectorHandlers: SelectorHandler[] = [];
  7. type GlobalEventFunc<T extends HTMLElement, E extends Event> = (el: T, e: E) => Promisable<void>;
  8. const globalEventFuncs: Record<string, GlobalEventFunc<HTMLElement, Event>> = {};
  9. type GlobalInitFunc<T extends HTMLElement> = (el: T) => Promisable<void>;
  10. const globalInitFuncs: Record<string, GlobalInitFunc<HTMLElement>> = {};
  11. // It handles the global events for all `<div data-global-click="onSomeElemClick"></div>` elements.
  12. export function registerGlobalEventFunc<T extends HTMLElement, E extends Event>(event: string, name: string, func: GlobalEventFunc<T, E>) {
  13. globalEventFuncs[`${event}:${name}`] = func as GlobalEventFunc<HTMLElement, Event>;
  14. }
  15. // It handles the global init functions by a selector, for example:
  16. // > registerGlobalSelectorObserver('.ui.dropdown:not(.custom)', (el) => { initDropdown(el, ...) });
  17. // ATTENTION: For most cases, it's recommended to use registerGlobalInitFunc instead,
  18. // Because this selector-based approach is less efficient and less maintainable.
  19. // But if there are already a lot of elements on many pages, this selector-based approach is more convenient for exiting code.
  20. export function registerGlobalSelectorFunc(selector: string, handler: (el: HTMLElement) => void) {
  21. selectorHandlers.push({selector, handler});
  22. // Then initAddedElementObserver will call this handler for all existing elements after all handlers are added.
  23. // This approach makes the init stage only need to do one "querySelectorAll".
  24. if (!globalSelectorObserverInited) return;
  25. for (const el of document.querySelectorAll<HTMLElement>(selector)) {
  26. handler(el);
  27. }
  28. }
  29. // It handles the global init functions for all `<div data-global-int="initSomeElem"></div>` elements.
  30. export function registerGlobalInitFunc<T extends HTMLElement>(name: string, handler: GlobalInitFunc<T>) {
  31. globalInitFuncs[name] = handler as GlobalInitFunc<HTMLElement>;
  32. // The "global init" functions are managed internally and called by callGlobalInitFunc
  33. // They must be ready before initGlobalSelectorObserver is called.
  34. if (globalSelectorObserverInited) throw new Error('registerGlobalInitFunc() must be called before initGlobalSelectorObserver()');
  35. }
  36. function callGlobalInitFunc(el: HTMLElement) {
  37. const initFunc = el.getAttribute('data-global-init');
  38. const func = globalInitFuncs[initFunc];
  39. if (!func) throw new Error(`Global init function "${initFunc}" not found`);
  40. // when an element node is removed and added again, it should not be re-initialized again.
  41. type GiteaGlobalInitElement = Partial<HTMLElement> & {_giteaGlobalInited: boolean};
  42. if ((el as GiteaGlobalInitElement)._giteaGlobalInited) return;
  43. (el as GiteaGlobalInitElement)._giteaGlobalInited = true;
  44. func(el);
  45. }
  46. function attachGlobalEvents() {
  47. // add global "[data-global-click]" event handler
  48. document.addEventListener('click', (e) => {
  49. const elem = (e.target as HTMLElement).closest<HTMLElement>('[data-global-click]');
  50. if (!elem) return;
  51. const funcName = elem.getAttribute('data-global-click');
  52. const func = globalEventFuncs[`click:${funcName}`];
  53. if (!func) throw new Error(`Global event function "click:${funcName}" not found`);
  54. func(elem, e);
  55. });
  56. }
  57. export function initGlobalSelectorObserver(perfTracer?: InitPerformanceTracer): void {
  58. if (globalSelectorObserverInited) throw new Error('initGlobalSelectorObserver() already called');
  59. globalSelectorObserverInited = true;
  60. attachGlobalEvents();
  61. selectorHandlers.push({selector: '[data-global-init]', handler: callGlobalInitFunc});
  62. const observer = new MutationObserver((mutationList) => {
  63. const len = mutationList.length;
  64. for (let i = 0; i < len; i++) {
  65. const mutation = mutationList[i];
  66. const len = mutation.addedNodes.length;
  67. for (let i = 0; i < len; i++) {
  68. const addedNode = mutation.addedNodes[i] as HTMLElement;
  69. if (!isDocumentFragmentOrElementNode(addedNode)) continue;
  70. for (const {selector, handler} of selectorHandlers) {
  71. if (addedNode.matches(selector)) {
  72. handler(addedNode);
  73. }
  74. for (const el of addedNode.querySelectorAll<HTMLElement>(selector)) {
  75. handler(el);
  76. }
  77. }
  78. }
  79. }
  80. });
  81. if (perfTracer) {
  82. for (const {selector, handler} of selectorHandlers) {
  83. perfTracer.recordCall(`initGlobalSelectorObserver ${selector}`, () => {
  84. for (const el of document.querySelectorAll<HTMLElement>(selector)) {
  85. handler(el);
  86. }
  87. });
  88. }
  89. } else {
  90. for (const {selector, handler} of selectorHandlers) {
  91. for (const el of document.querySelectorAll<HTMLElement>(selector)) {
  92. handler(el);
  93. }
  94. }
  95. }
  96. observer.observe(document, {subtree: true, childList: true});
  97. }