| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import ResizeObserver from 'resize-observer-polyfill';
- const isServer = typeof window === 'undefined';
- /* istanbul ignore next */
- function resizeHandler(entries: any[]) {
- for (const entry of entries) {
- const listeners = entry.target.__resizeListeners__ || [];
- if (listeners.length) {
- listeners.forEach((fn: () => any) => {
- fn();
- });
- }
- }
- }
- /* istanbul ignore next */
- export function addResizeListener(element: any, fn: () => any) {
- if (isServer) return;
- if (!element.__resizeListeners__) {
- element.__resizeListeners__ = [];
- element.__ro__ = new ResizeObserver(resizeHandler);
- element.__ro__.observe(element);
- }
- element.__resizeListeners__.push(fn);
- }
- /* istanbul ignore next */
- export function removeResizeListener(element: any, fn: () => any) {
- if (!element || !element.__resizeListeners__) return;
- element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
- if (!element.__resizeListeners__.length) {
- element.__ro__.disconnect();
- }
- }
- export function triggerWindowResize() {
- const event = document.createEvent('HTMLEvents');
- event.initEvent('resize', true, true);
- (event as any).eventType = 'message';
- window.dispatchEvent(event);
- }
- export function setDivHeight(e: MouseEvent, minHeight, scroll, scrollClientMaxHeight = 175, scrollClientHeight = 100) {
- if (e && e.target && e.target['className'] && e.target['className'].includes('input')) {
- return;
- }
- e.preventDefault();
- e.stopPropagation();
- const divObject = e.currentTarget as HTMLElement;
- if (divObject) {
- const divHeight = divObject.offsetHeight;
- let height = divHeight;
- const startY = e.clientY;
- let isMove = false;
- document.onmousemove = function (res) {
- res.preventDefault();
- isMove = true;
- const distY = Math.abs(res.clientY - startY);
- if (res.clientY > startY) {
- if (divHeight - distY > minHeight) {
- height = divHeight - distY;
- } else {
- height = minHeight;
- }
- }
- if (res.clientY < startY) {
- height = divHeight + distY;
- }
- if (height > document.body.clientHeight - scrollClientMaxHeight) {
- height = document.body.clientHeight - scrollClientMaxHeight;
- }
- divObject.style.height = height + 'px';
- };
- document.onmouseup = function () {
- document.onmousemove = null;
- if (isMove && scroll.y != height - scrollClientHeight) {
- scroll.y = height - scrollClientHeight - 20;
- }
- };
- }
- }
|