gitea源码

dom.ts 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import {debounce} from 'throttle-debounce';
  2. import type {Promisable} from '../types.ts';
  3. import type $ from 'jquery';
  4. import {isInFrontendUnitTest} from './testhelper.ts';
  5. type ArrayLikeIterable<T> = ArrayLike<T> & Iterable<T>; // for NodeListOf and Array
  6. type ElementArg = Element | string | ArrayLikeIterable<Element> | ReturnType<typeof $>;
  7. type ElementsCallback<T extends Element> = (el: T) => Promisable<any>;
  8. type ElementsCallbackWithArgs = (el: Element, ...args: any[]) => Promisable<any>;
  9. export type DOMEvent<E extends Event, T extends Element = HTMLElement> = E & {target: Partial<T>;};
  10. function elementsCall(el: ElementArg, func: ElementsCallbackWithArgs, ...args: any[]): ArrayLikeIterable<Element> {
  11. if (typeof el === 'string' || el instanceof String) {
  12. el = document.querySelectorAll(el as string);
  13. }
  14. if (el instanceof Node) {
  15. func(el, ...args);
  16. return [el];
  17. } else if (el.length !== undefined) {
  18. // this works for: NodeList, HTMLCollection, Array, jQuery
  19. const elems = el as ArrayLikeIterable<Element>;
  20. for (const elem of elems) func(elem, ...args);
  21. return elems;
  22. }
  23. throw new Error('invalid argument to be shown/hidden');
  24. }
  25. export function toggleElemClass(el: ElementArg, className: string, force?: boolean): ArrayLikeIterable<Element> {
  26. return elementsCall(el, (e: Element) => {
  27. if (force === true) {
  28. e.classList.add(className);
  29. } else if (force === false) {
  30. e.classList.remove(className);
  31. } else if (force === undefined) {
  32. e.classList.toggle(className);
  33. } else {
  34. throw new Error('invalid force argument');
  35. }
  36. });
  37. }
  38. /**
  39. * @param el ElementArg
  40. * @param force force=true to show or force=false to hide, undefined to toggle
  41. */
  42. export function toggleElem(el: ElementArg, force?: boolean): ArrayLikeIterable<Element> {
  43. return toggleElemClass(el, 'tw-hidden', force === undefined ? force : !force);
  44. }
  45. export function showElem(el: ElementArg): ArrayLikeIterable<Element> {
  46. return toggleElem(el, true);
  47. }
  48. export function hideElem(el: ElementArg): ArrayLikeIterable<Element> {
  49. return toggleElem(el, false);
  50. }
  51. function applyElemsCallback<T extends Element>(elems: ArrayLikeIterable<T>, fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
  52. if (fn) {
  53. for (const el of elems) {
  54. fn(el);
  55. }
  56. }
  57. return elems;
  58. }
  59. export function queryElemSiblings<T extends Element>(el: Element, selector = '*', fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
  60. const elems = Array.from(el.parentNode.children) as T[];
  61. return applyElemsCallback<T>(elems.filter((child: Element) => {
  62. return child !== el && child.matches(selector);
  63. }), fn);
  64. }
  65. /** it works like jQuery.children: only the direct children are selected */
  66. export function queryElemChildren<T extends Element>(parent: Element | ParentNode, selector = '*', fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
  67. if (isInFrontendUnitTest()) {
  68. // https://github.com/capricorn86/happy-dom/issues/1620 : ":scope" doesn't work
  69. const selected = Array.from<T>(parent.children as any).filter((child) => child.matches(selector));
  70. return applyElemsCallback<T>(selected, fn);
  71. }
  72. return applyElemsCallback<T>(parent.querySelectorAll(`:scope > ${selector}`), fn);
  73. }
  74. /** it works like parent.querySelectorAll: all descendants are selected */
  75. // in the future, all "queryElems(document, ...)" should be refactored to use a more specific parent if the targets are not for page-level components.
  76. export function queryElems<T extends HTMLElement>(parent: Element | ParentNode, selector: string, fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
  77. return applyElemsCallback<T>(parent.querySelectorAll(selector), fn);
  78. }
  79. export function onDomReady(cb: () => Promisable<void>) {
  80. if (document.readyState === 'loading') {
  81. document.addEventListener('DOMContentLoaded', cb);
  82. } else {
  83. cb();
  84. }
  85. }
  86. /** checks whether an element is owned by the current document, and whether it is a document fragment or element node
  87. * if it is, it means it is a "normal" element managed by us, which can be modified safely. */
  88. export function isDocumentFragmentOrElementNode(el: Node) {
  89. try {
  90. return el.ownerDocument === document && el.nodeType === Node.ELEMENT_NODE || el.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
  91. } catch {
  92. // in case the el is not in the same origin, then the access to nodeType would fail
  93. return false;
  94. }
  95. }
  96. /** autosize a textarea to fit content. */
  97. // Based on https://github.com/github/textarea-autosize
  98. // ---------------------------------------------------------------------
  99. // Copyright (c) 2018 GitHub, Inc.
  100. //
  101. // Permission is hereby granted, free of charge, to any person obtaining
  102. // a copy of this software and associated documentation files (the
  103. // "Software"), to deal in the Software without restriction, including
  104. // without limitation the rights to use, copy, modify, merge, publish,
  105. // distribute, sublicense, and/or sell copies of the Software, and to
  106. // permit persons to whom the Software is furnished to do so, subject to
  107. // the following conditions:
  108. //
  109. // The above copyright notice and this permission notice shall be
  110. // included in all copies or substantial portions of the Software.
  111. // ---------------------------------------------------------------------
  112. export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom = 0}: {viewportMarginBottom?: number} = {}) {
  113. let isUserResized = false;
  114. // lastStyleHeight and initialStyleHeight are CSS values like '100px'
  115. let lastMouseX: number;
  116. let lastMouseY: number;
  117. let lastStyleHeight: string;
  118. let initialStyleHeight: string;
  119. function onUserResize(event: MouseEvent) {
  120. if (isUserResized) return;
  121. if (lastMouseX !== event.clientX || lastMouseY !== event.clientY) {
  122. const newStyleHeight = textarea.style.height;
  123. if (lastStyleHeight && lastStyleHeight !== newStyleHeight) {
  124. isUserResized = true;
  125. }
  126. lastStyleHeight = newStyleHeight;
  127. }
  128. lastMouseX = event.clientX;
  129. lastMouseY = event.clientY;
  130. }
  131. function overflowOffset() {
  132. let offsetTop = 0;
  133. let el = textarea;
  134. while (el !== document.body && el !== null) {
  135. offsetTop += el.offsetTop || 0;
  136. el = el.offsetParent as HTMLTextAreaElement;
  137. }
  138. const top = offsetTop - document.defaultView.scrollY;
  139. const bottom = document.documentElement.clientHeight - (top + textarea.offsetHeight);
  140. return {top, bottom};
  141. }
  142. function resizeToFit() {
  143. if (isUserResized) return;
  144. if (textarea.offsetWidth <= 0 && textarea.offsetHeight <= 0) return;
  145. const previousMargin = textarea.style.marginBottom;
  146. try {
  147. const {top, bottom} = overflowOffset();
  148. const isOutOfViewport = top < 0 || bottom < 0;
  149. const computedStyle = getComputedStyle(textarea);
  150. const topBorderWidth = parseFloat(computedStyle.borderTopWidth);
  151. const bottomBorderWidth = parseFloat(computedStyle.borderBottomWidth);
  152. const isBorderBox = computedStyle.boxSizing === 'border-box';
  153. const borderAddOn = isBorderBox ? topBorderWidth + bottomBorderWidth : 0;
  154. const adjustedViewportMarginBottom = Math.min(bottom, viewportMarginBottom);
  155. const curHeight = parseFloat(computedStyle.height);
  156. const maxHeight = curHeight + bottom - adjustedViewportMarginBottom;
  157. // In Firefox, setting auto height momentarily may cause the page to scroll up
  158. // unexpectedly, prevent this by setting a temporary margin.
  159. textarea.style.marginBottom = `${textarea.clientHeight}px`;
  160. textarea.style.height = 'auto';
  161. let newHeight = textarea.scrollHeight + borderAddOn;
  162. if (isOutOfViewport) {
  163. // it is already out of the viewport:
  164. // * if the textarea is expanding: do not resize it
  165. if (newHeight > curHeight) {
  166. newHeight = curHeight;
  167. }
  168. // * if the textarea is shrinking, shrink line by line (just use the
  169. // scrollHeight). do not apply max-height limit, otherwise the page
  170. // flickers and the textarea jumps
  171. } else {
  172. // * if it is in the viewport, apply the max-height limit
  173. newHeight = Math.min(maxHeight, newHeight);
  174. }
  175. textarea.style.height = `${newHeight}px`;
  176. lastStyleHeight = textarea.style.height;
  177. } finally {
  178. // restore previous margin
  179. if (previousMargin) {
  180. textarea.style.marginBottom = previousMargin;
  181. } else {
  182. textarea.style.removeProperty('margin-bottom');
  183. }
  184. // ensure that the textarea is fully scrolled to the end, when the cursor
  185. // is at the end during an input event
  186. if (textarea.selectionStart === textarea.selectionEnd &&
  187. textarea.selectionStart === textarea.value.length) {
  188. textarea.scrollTop = textarea.scrollHeight;
  189. }
  190. }
  191. }
  192. function onFormReset() {
  193. isUserResized = false;
  194. if (initialStyleHeight !== undefined) {
  195. textarea.style.height = initialStyleHeight;
  196. } else {
  197. textarea.style.removeProperty('height');
  198. }
  199. }
  200. textarea.addEventListener('mousemove', onUserResize);
  201. textarea.addEventListener('input', resizeToFit);
  202. textarea.form?.addEventListener('reset', onFormReset);
  203. initialStyleHeight = textarea.style.height ?? undefined;
  204. if (textarea.value) resizeToFit();
  205. return {
  206. resizeToFit,
  207. destroy() {
  208. textarea.removeEventListener('mousemove', onUserResize);
  209. textarea.removeEventListener('input', resizeToFit);
  210. textarea.form?.removeEventListener('reset', onFormReset);
  211. },
  212. };
  213. }
  214. export function onInputDebounce(fn: () => Promisable<any>) {
  215. return debounce(300, fn);
  216. }
  217. type LoadableElement = HTMLEmbedElement | HTMLIFrameElement | HTMLImageElement | HTMLScriptElement | HTMLTrackElement;
  218. /** Set the `src` attribute on an element and returns a promise that resolves once the element
  219. * has loaded or errored. */
  220. export function loadElem(el: LoadableElement, src: string) {
  221. return new Promise((resolve) => {
  222. el.addEventListener('load', () => resolve(true), {once: true});
  223. el.addEventListener('error', () => resolve(false), {once: true});
  224. el.src = src;
  225. });
  226. }
  227. // some browsers like PaleMoon don't have "SubmitEvent" support, so polyfill it by a tricky method: use the last clicked button as submitter
  228. // it can't use other transparent polyfill patches because PaleMoon also doesn't support "addEventListener(capture)"
  229. const needSubmitEventPolyfill = typeof SubmitEvent === 'undefined';
  230. export function submitEventSubmitter(e: any) {
  231. e = e.originalEvent ?? e; // if the event is wrapped by jQuery, use "originalEvent", otherwise, use the event itself
  232. return needSubmitEventPolyfill ? (e.target._submitter || null) : e.submitter;
  233. }
  234. function submitEventPolyfillListener(e: DOMEvent<Event>) {
  235. const form = e.target.closest('form');
  236. if (!form) return;
  237. form._submitter = e.target.closest('button:not([type]), button[type="submit"], input[type="submit"]');
  238. }
  239. export function initSubmitEventPolyfill() {
  240. if (!needSubmitEventPolyfill) return;
  241. console.warn(`This browser doesn't have "SubmitEvent" support, use a tricky method to polyfill`);
  242. document.body.addEventListener('click', submitEventPolyfillListener);
  243. document.body.addEventListener('focus', submitEventPolyfillListener);
  244. }
  245. export function isElemVisible(el: HTMLElement): boolean {
  246. // Check if an element is visible, equivalent to jQuery's `:visible` pseudo.
  247. // This function DOESN'T account for all possible visibility scenarios, its behavior is covered by the tests of "querySingleVisibleElem"
  248. if (!el) return false;
  249. // checking el.style.display is not necessary for browsers, but it is required by some tests with happy-dom because happy-dom doesn't really do layout
  250. return !el.classList.contains('tw-hidden') && (el.offsetWidth || el.offsetHeight || el.getClientRects().length) && el.style.display !== 'none';
  251. }
  252. export function createElementFromHTML<T extends HTMLElement>(htmlString: string): T {
  253. htmlString = htmlString.trim();
  254. // There is no way to create some elements without a proper parent, jQuery's approach: https://github.com/jquery/jquery/blob/main/src/manipulation/wrapMap.js
  255. // eslint-disable-next-line github/unescaped-html-literal
  256. if (htmlString.startsWith('<tr')) {
  257. const container = document.createElement('table');
  258. container.innerHTML = htmlString;
  259. return container.querySelector<T>('tr');
  260. }
  261. const div = document.createElement('div');
  262. div.innerHTML = htmlString;
  263. return div.firstChild as T;
  264. }
  265. export function createElementFromAttrs(tagName: string, attrs: Record<string, any>, ...children: (Node | string)[]): HTMLElement {
  266. const el = document.createElement(tagName);
  267. for (const [key, value] of Object.entries(attrs || {})) {
  268. if (value === undefined || value === null) continue;
  269. if (typeof value === 'boolean') {
  270. el.toggleAttribute(key, value);
  271. } else {
  272. el.setAttribute(key, String(value));
  273. }
  274. }
  275. for (const child of children) {
  276. el.append(child instanceof Node ? child : document.createTextNode(child));
  277. }
  278. return el;
  279. }
  280. export function animateOnce(el: Element, animationClassName: string): Promise<void> {
  281. return new Promise((resolve) => {
  282. el.addEventListener('animationend', function onAnimationEnd() {
  283. el.classList.remove(animationClassName);
  284. el.removeEventListener('animationend', onAnimationEnd);
  285. resolve();
  286. }, {once: true});
  287. el.classList.add(animationClassName);
  288. });
  289. }
  290. export function querySingleVisibleElem<T extends HTMLElement>(parent: Element, selector: string): T | null {
  291. const elems = parent.querySelectorAll<HTMLElement>(selector);
  292. const candidates = Array.from(elems).filter(isElemVisible);
  293. if (candidates.length > 1) throw new Error(`Expected exactly one visible element matching selector "${selector}", but found ${candidates.length}`);
  294. return candidates.length ? candidates[0] as T : null;
  295. }
  296. export function addDelegatedEventListener<T extends HTMLElement, E extends Event>(parent: Node, type: string, selector: string, listener: (elem: T, e: E) => Promisable<void>, options?: boolean | AddEventListenerOptions) {
  297. parent.addEventListener(type, (e: Event) => {
  298. const elem = (e.target as HTMLElement).closest(selector);
  299. // It strictly checks "parent contains the target elem" to avoid side effects of selector running on outside the parent.
  300. // Keep in mind that the elem could have been removed from parent by other event handlers before this event handler is called.
  301. // For example, tippy popup item, the tippy popup could be hidden and removed from DOM before this.
  302. // It is the caller's responsibility to make sure the elem is still in parent's DOM when this event handler is called.
  303. if (!elem || (parent !== document && !parent.contains(elem))) return;
  304. listener(elem as T, e as E);
  305. }, options);
  306. }
  307. /** Returns whether a click event is a left-click without any modifiers held */
  308. export function isPlainClick(e: MouseEvent) {
  309. return e.button === 0 && !e.ctrlKey && !e.metaKey && !e.altKey && !e.shiftKey;
  310. }
  311. let elemIdCounter = 0;
  312. export function generateElemId(prefix: string = ''): string {
  313. return `${prefix}${elemIdCounter++}`;
  314. }