ag-common 0.0.697 → 0.0.699

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,4 +4,4 @@ export declare const ModalItem: import("@emotion/styled").StyledComponent<{
4
4
  theme?: import("@emotion/react").Theme | undefined;
5
5
  as?: React.ElementType<any, keyof React.JSX.IntrinsicElements> | undefined;
6
6
  }, React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, {}>;
7
- export declare const Modal: ({ open, setOpen, children, position, topPosition, showCloseButton, closeOnMoveMouseOutside, className, closeOnClickOutside, portalId: pidraw, style, }: IModal) => React.JSX.Element | null;
7
+ export declare const Modal: (p: IModal) => React.JSX.Element | null;
@@ -31,6 +31,7 @@ exports.Modal = exports.ModalItem = void 0;
31
31
  const styled_1 = __importDefault(require("@emotion/styled"));
32
32
  const react_1 = __importStar(require("react"));
33
33
  const react_dom_1 = require("react-dom");
34
+ const dom_1 = require("../../helpers/dom");
34
35
  const useOnClickOutside_1 = require("../../helpers/useOnClickOutside");
35
36
  const styles_1 = require("../../styles");
36
37
  const Close_1 = require("../Close");
@@ -85,7 +86,8 @@ exports.ModalItem = styled_1.default.div `
85
86
  const CloseStyled = (0, styled_1.default)(Close_1.Close) `
86
87
  z-index: 1;
87
88
  `;
88
- const Modal = ({ open, setOpen, children, position = 'left', topPosition = 'top', showCloseButton = true, closeOnMoveMouseOutside = false, className, closeOnClickOutside = true, portalId: pidraw, style, }) => {
89
+ const Modal = (p) => {
90
+ const { open, setOpen, children, position = 'left', topPosition = 'top', showCloseButton = true, closeOnMoveMouseOutside = false, className, closeOnClickOutside = true, portalId: pidraw, style, } = p;
89
91
  let portalId = pidraw;
90
92
  if (portalId === undefined) {
91
93
  portalId = globalId;
@@ -127,7 +129,7 @@ const Modal = ({ open, setOpen, children, position = 'left', topPosition = 'top'
127
129
  disabled: !open,
128
130
  ref,
129
131
  moveMouseOutside: closeOnMoveMouseOutside,
130
- }, () => {
132
+ }, (e) => {
131
133
  var _a;
132
134
  //there might be multiple models open, only close the last one on the stack
133
135
  if (portalElem) {
@@ -140,7 +142,7 @@ const Modal = ({ open, setOpen, children, position = 'left', topPosition = 'top'
140
142
  }
141
143
  }
142
144
  if (closeOnClickOutside && open) {
143
- setOpen(false);
145
+ setOpen(false, e);
144
146
  setBounced(false);
145
147
  }
146
148
  });
@@ -158,8 +160,8 @@ const Modal = ({ open, setOpen, children, position = 'left', topPosition = 'top'
158
160
  return react_1.default.createElement(react_1.default.Fragment, null);
159
161
  }
160
162
  const Content = (react_1.default.createElement(FixedBackground, null,
161
- react_1.default.createElement(ModalBase, { "data-bounced": bounced, "data-position": position, "data-topposition": topPosition, ref: ref, className: className, style: style },
162
- showCloseButton && (react_1.default.createElement(CloseStyled, { "data-type": "modal-close", onClick: () => setOpen(false) })),
163
+ react_1.default.createElement(ModalBase, Object.assign({}, (0, dom_1.filterDataProps)(p), { "data-bounced": bounced, "data-position": position, "data-topposition": topPosition, ref: ref, className: className, style: style }),
164
+ showCloseButton && (react_1.default.createElement(CloseStyled, { "data-type": "modal-close", onClick: (e) => setOpen(false, e) })),
163
165
  children)));
164
166
  if (portalId && portalElem === undefined) {
165
167
  return null;
@@ -1,7 +1,7 @@
1
1
  import type { CSSProperties } from 'react';
2
2
  export interface IModal {
3
3
  open: boolean;
4
- setOpen: (b: boolean) => void;
4
+ setOpen: (b: boolean, e: Event) => void;
5
5
  children: React.ReactNode;
6
6
  position?: 'left' | 'right' | 'center';
7
7
  topPosition?: 'bottom' | 'top' | 'center';
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import type { TreeNodeData, TreeNodeOut } from './types';
3
+ export declare const TooltipContent: ({ data, node, head, }: {
4
+ data: TreeNodeData;
5
+ node: TreeNodeOut;
6
+ head: TreeNodeOut;
7
+ }) => React.JSX.Element;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TooltipContent = void 0;
7
+ const styled_1 = __importDefault(require("@emotion/styled"));
8
+ const react_1 = __importDefault(require("react"));
9
+ const Base = styled_1.default.div `
10
+ padding: 0.5rem;
11
+ border: solid 1px black;
12
+ background-color: white;
13
+ `;
14
+ const Title = styled_1.default.div `
15
+ font-weight: bold;
16
+ `;
17
+ const TooltipContent = ({ data, node, head, }) => {
18
+ var _a;
19
+ let rows = [];
20
+ let n = node;
21
+ while (n) {
22
+ const t = ((_a = data.titleFn) === null || _a === void 0 ? void 0 : _a.call(data, {
23
+ path: n.name,
24
+ pathCount: n.size,
25
+ fullCount: head.size,
26
+ })) || `${n.name} (${n.size}/${head.size})`;
27
+ rows = [t, ...rows];
28
+ n = n.parent;
29
+ }
30
+ return (react_1.default.createElement(Base, null,
31
+ react_1.default.createElement(Title, null, rows.map((r, i) => (react_1.default.createElement("div", { key: r, style: { marginLeft: `${i * 2}px` } }, r))))));
32
+ };
33
+ exports.TooltipContent = TooltipContent;
@@ -31,9 +31,11 @@ exports.TreeChart = void 0;
31
31
  const styled_1 = __importDefault(require("@emotion/styled"));
32
32
  const react_1 = __importStar(require("react"));
33
33
  const useResize_1 = require("../../helpers/useResize");
34
+ const useTooltip_1 = require("../../helpers/useTooltip");
34
35
  const styles_1 = require("../../styles");
35
36
  const common_1 = require("../../styles/common");
36
37
  const helpers_1 = require("./helpers");
38
+ const TooltipContent_1 = require("./TooltipContent");
37
39
  const Base = styled_1.default.div `
38
40
  border: solid 1px #ccc;
39
41
  max-height: 100%;
@@ -68,28 +70,38 @@ const Title = styled_1.default.div `
68
70
  min-height: 1rem;
69
71
  line-height: 1rem;
70
72
  `;
71
- const render = ({ n, depth, head, headDim, tnd, }) => {
72
- var _a;
73
- const leaf = n.children.length === 0;
73
+ const Render = ({ n, depth, head, headDim, tnd, UT, }) => {
74
+ const children = Object.values(n.children);
75
+ const leaf = children.length === 0;
74
76
  const sizeMult = n.size / head.size;
75
77
  const biggerDim = Math.max(headDim.width, headDim.height);
76
78
  const nodeSize = Math.floor(biggerDim * sizeMult).toString();
77
- const title = ((_a = tnd.titleFn) === null || _a === void 0 ? void 0 : _a.call(tnd, {
78
- path: n.name,
79
- pathCount: n.size,
80
- fullCount: head.size,
81
- })) || `${n.name} (${n.size}/${head.size})`;
82
79
  return (react_1.default.createElement(Node, { "data-treenode": true, "data-leaf": leaf.toString(), style: Object.assign({ backgroundColor: (0, styles_1.getColourWheel)(depth) }, (leaf &&
83
80
  nodeSize && {
84
81
  width: nodeSize + 'px',
85
82
  height: nodeSize + 'px',
86
- })), key: n.name, "data-ch": n.children.length, "data-size": n.size, title: title },
83
+ })), key: n.name, "data-ch": n.children.length, "data-size": n.size, onMouseLeave: () => UT.setPos(undefined), onMouseMove: (element) => {
84
+ UT.setPos({
85
+ element,
86
+ parent: null,
87
+ data: { data: tnd, node: n, head },
88
+ });
89
+ element.preventDefault();
90
+ element.stopPropagation();
91
+ } },
87
92
  n.name && react_1.default.createElement(Title, null, n.name),
88
- n.children.length > 0 && (react_1.default.createElement(NodeChildren, { "data-type": "nc" }, n.children.map((c) => render({ n: c, depth: depth + 1, head, headDim, tnd }))))));
93
+ children.length > 0 && (react_1.default.createElement(NodeChildren, { "data-type": "nc" }, children.map((c) => Render({
94
+ UT,
95
+ n: c,
96
+ depth: depth + 1,
97
+ head,
98
+ headDim,
99
+ tnd,
100
+ }))))));
89
101
  };
90
102
  const TreeChart = (tnd) => {
91
- const raw = (0, helpers_1.convertToRaw)({ tnd });
92
- const head = (0, helpers_1.toArray)('', raw);
103
+ const UT = (0, useTooltip_1.useTooltip)();
104
+ const head = (0, helpers_1.convertToRaw)({ tnd });
93
105
  const pd = (0, useResize_1.useResize)();
94
106
  const [headDim, setHeadDim] = (0, react_1.useState)();
95
107
  const r = (0, react_1.useRef)(null);
@@ -107,6 +119,16 @@ const TreeChart = (tnd) => {
107
119
  if (head.size === 0) {
108
120
  return react_1.default.createElement("div", null);
109
121
  }
110
- return (react_1.default.createElement(Base, { ref: r, className: tnd.className, style: tnd.style }, headDim && render({ tnd, n: head, depth: 0, head, headDim })));
122
+ return (react_1.default.createElement(Base, { ref: r, className: tnd.className, style: tnd.style },
123
+ react_1.default.createElement(UT.Comp, { pos: UT.pos }, (p) => react_1.default.createElement(TooltipContent_1.TooltipContent, Object.assign({}, p))),
124
+ headDim &&
125
+ Render({
126
+ UT,
127
+ tnd,
128
+ n: head,
129
+ depth: 0,
130
+ head,
131
+ headDim,
132
+ })));
111
133
  };
112
134
  exports.TreeChart = TreeChart;
@@ -1,14 +1,4 @@
1
1
  import type { TreeNodeData, TreeNodeOut } from './types';
2
- interface TreeNodeRaw {
3
- name: string;
4
- size: number;
5
- depth: number;
6
- children: {
7
- [P in string]: TreeNodeRaw;
8
- };
9
- }
10
2
  export declare const convertToRaw: ({ tnd: { data, pathDelimiter }, }: {
11
3
  tnd: TreeNodeData;
12
- }) => TreeNodeRaw;
13
- export declare const toArray: (name: string, raw: TreeNodeRaw) => TreeNodeOut;
14
- export {};
4
+ }) => TreeNodeOut;
@@ -1,11 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toArray = exports.convertToRaw = void 0;
3
+ exports.convertToRaw = void 0;
4
4
  const convertToRaw = ({ tnd: { data, pathDelimiter }, }) => {
5
5
  if (data.length === 0) {
6
- return { children: {}, size: 0, name: '', depth: 0 };
6
+ return { children: {}, size: 0, name: '', depth: 0, parent: undefined };
7
7
  }
8
- const dm = { size: 0, children: {}, name: '', depth: 0 };
8
+ const dm = {
9
+ size: 0,
10
+ children: {},
11
+ name: '',
12
+ depth: 0,
13
+ parent: undefined,
14
+ };
9
15
  data.forEach((line) => {
10
16
  const names = line.path.split(pathDelimiter || '/');
11
17
  let node = dm;
@@ -23,6 +29,7 @@ const convertToRaw = ({ tnd: { data, pathDelimiter }, }) => {
23
29
  size: 0,
24
30
  name: names[a],
25
31
  depth: a,
32
+ parent: node,
26
33
  };
27
34
  }
28
35
  node = node.children[names[a]];
@@ -32,20 +39,3 @@ const convertToRaw = ({ tnd: { data, pathDelimiter }, }) => {
32
39
  return dm;
33
40
  };
34
41
  exports.convertToRaw = convertToRaw;
35
- const toArrayAux = (name, n) => ({
36
- name,
37
- size: n.size,
38
- depth: n.depth,
39
- children: Object.entries(n.children).map(([cn, cv]) => toArrayAux(cn, cv)),
40
- });
41
- const toArray = (name, raw) => {
42
- const arr = toArrayAux(name, {
43
- children: raw.children,
44
- size: 0,
45
- name: '',
46
- depth: 0,
47
- });
48
- arr.size = arr.children.map((d) => d.size).reduce((a, b) => a + b, 0);
49
- return arr;
50
- };
51
- exports.toArray = toArray;
@@ -3,7 +3,10 @@ export interface TreeNodeOut {
3
3
  name: string;
4
4
  size: number;
5
5
  depth: number;
6
- children: TreeNodeOut[];
6
+ children: {
7
+ [P in string]: TreeNodeOut;
8
+ };
9
+ parent: TreeNodeOut | undefined;
7
10
  }
8
11
  export interface TreeNodeData {
9
12
  className?: string;
@@ -1,5 +1,4 @@
1
1
  import type { MouseEvent } from 'react';
2
- import React from 'react';
3
2
  interface IPos<T> {
4
3
  cursor: MouseEvent;
5
4
  data: T;
@@ -10,20 +9,21 @@ interface IPos<T> {
10
9
  x: number;
11
10
  y: number;
12
11
  }
13
- type IUT = {
12
+ type ITooltipProps = {
14
13
  /** default 'ag-tooltip-portal' */
15
14
  portalId: string;
16
15
  };
17
- export declare const useTooltip: <T>(p?: IUT) => {
18
- Comp: <T_1>({ pos, children, }: {
19
- pos: IPos<T_1> | undefined;
20
- children: (data: T_1) => JSX.Element;
21
- }) => React.JSX.Element | null;
16
+ export interface IUseTooltip<T> {
17
+ Comp: <T>({ pos, children, }: {
18
+ pos: IPos<T> | undefined;
19
+ children: (data: T) => JSX.Element;
20
+ }) => JSX.Element | null;
22
21
  setPos: (p?: {
23
22
  element: MouseEvent;
24
23
  parent: Element | null;
25
24
  data: T;
26
- }) => void;
25
+ } | undefined) => void;
27
26
  pos: IPos<T> | undefined;
28
- };
27
+ }
28
+ export declare const useTooltip: <T>(p?: ITooltipProps) => IUseTooltip<T>;
29
29
  export {};
@@ -87,7 +87,7 @@ const Comp = ({ pos, children, }) => {
87
87
  top = 0;
88
88
  }
89
89
  }
90
- const Content = (react_1.default.createElement(Base, { ref: ref, style: Object.assign(Object.assign({ left,
90
+ const Content = (react_1.default.createElement(Base, { "data-type": "tooltip-content", ref: ref, style: Object.assign(Object.assign({ left,
91
91
  right,
92
92
  top,
93
93
  bottom, zIndex: 10 }, (pos.usePortal && { position: 'fixed' })), (!(size === null || size === void 0 ? void 0 : size.p) && { zIndex: -1 })) }, children(pos.data)));
@@ -127,6 +127,8 @@ const useTooltip = (p) => {
127
127
  let parentLeft = 0;
128
128
  let parentWidth = document.body.clientWidth;
129
129
  let parentHeight = document.body.clientHeight;
130
+ let x = 0;
131
+ let y = 0;
130
132
  if (p.parent) {
131
133
  ({
132
134
  top: parentTop,
@@ -134,9 +136,15 @@ const useTooltip = (p) => {
134
136
  width: parentWidth,
135
137
  height: parentHeight,
136
138
  } = p.parent.getBoundingClientRect());
139
+ x = p.element.pageX - parentLeft;
140
+ y = p.element.pageY - parentTop;
141
+ }
142
+ else {
143
+ parentWidth = window.innerWidth;
144
+ parentHeight = window.innerHeight;
145
+ x = p.element.clientX;
146
+ y = p.element.clientY;
137
147
  }
138
- const x = p.element.pageX - parentLeft;
139
- const y = p.element.pageY - parentTop;
140
148
  const p2 = {
141
149
  cursor: p.element,
142
150
  data: p.data,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.697",
2
+ "version": "0.0.699",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",