|
| 1 | +// dd-utils.ts 2.0.2 @preserve |
| 2 | + |
| 3 | +/** |
| 4 | + * https://gridstackjs.com/ |
| 5 | + * (c) 2020 Alain Dumesny, rhlin |
| 6 | + * gridstack.js may be freely distributed under the MIT license. |
| 7 | +*/ |
| 8 | +export class DDUtils { |
| 9 | + static clone(el: HTMLElement): HTMLElement { |
| 10 | + const node = el.cloneNode(true) as HTMLElement; |
| 11 | + node.removeAttribute('id'); |
| 12 | + return node; |
| 13 | + } |
| 14 | + |
| 15 | + static appendTo(el: HTMLElement, parent: string | HTMLElement | Node) { |
| 16 | + let parentNode: HTMLElement; |
| 17 | + if (typeof parent === 'string') { |
| 18 | + parentNode = document.querySelector(parent as string); |
| 19 | + } else { |
| 20 | + parentNode = parent as HTMLElement; |
| 21 | + } |
| 22 | + if (parentNode) { |
| 23 | + parentNode.append(el); |
| 24 | + } |
| 25 | + } |
| 26 | + static setPositionRelative(el) { |
| 27 | + if (!(/^(?:r|a|f)/).test(window.getComputedStyle(el).position)) { |
| 28 | + el.style.position = "relative"; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static throttle(callback: (...args) => void, delay: number) { |
| 33 | + let isWaiting = false; |
| 34 | + |
| 35 | + return (...args) => { |
| 36 | + if (!isWaiting) { |
| 37 | + callback(...args); |
| 38 | + isWaiting = true; |
| 39 | + setTimeout(() => isWaiting = false, delay); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + static addElStyles(el: HTMLElement, styles: { [prop: string]: string | string[] }) { |
| 44 | + if (styles instanceof Object) { |
| 45 | + for (const s in styles) { |
| 46 | + if (styles.hasOwnProperty(s)) { |
| 47 | + if (Array.isArray(styles[s])) { |
| 48 | + // support fallback value |
| 49 | + (styles[s] as string[]).forEach(val => { |
| 50 | + el.style[s] = val; |
| 51 | + }); |
| 52 | + } else { |
| 53 | + el.style[s] = styles[s]; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + static copyProps(dst, src, props) { |
| 60 | + for (let i = 0; i < props.length; i++) { |
| 61 | + const p = props[i]; |
| 62 | + dst[p] = src[p]; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static initEvent<T>(e: DragEvent|MouseEvent, info: {type: string; target?: EventTarget}) { |
| 67 | + const kbdProps = 'altKey,ctrlKey,metaKey,shiftKey'.split(','); |
| 68 | + const ptProps = 'pageX,pageY,clientX,clientY,screenX,screenY'.split(','); |
| 69 | + const evt = {type: info.type}; |
| 70 | + const obj = { |
| 71 | + button: 0, |
| 72 | + which: 0, |
| 73 | + buttons: 1, |
| 74 | + bubbles: true, |
| 75 | + cancelable: true, |
| 76 | + originEvent: e, |
| 77 | + target: info.target? info.target : e.target |
| 78 | + } |
| 79 | + if (e instanceof DragEvent) { |
| 80 | + Object.assign(obj, {dataTransfer: e.dataTransfer}); |
| 81 | + } |
| 82 | + DDUtils.copyProps(evt, e, kbdProps); |
| 83 | + DDUtils.copyProps(evt, e, ptProps); |
| 84 | + DDUtils.copyProps(evt, obj, Object.keys(obj)); |
| 85 | + return evt as unknown as T; |
| 86 | + } |
| 87 | +} |
0 commit comments