@gravity-ui/dashkit 8.10.2 → 8.11.0-beta.0

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.
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import type { Config, ConfigItem, GlobalParams, ItemDropProps, ItemsStateAndParams } from '../../shared';
3
- import { ContextProps, DashKitGroup, MenuItem, Plugin, SetConfigItem, SetNewItemOptions, Settings, SettingsProps } from '../../typings';
3
+ import { AddNewItemOptions, ContextProps, DashKitGroup, MenuItem, Plugin, SetConfigItem, Settings, SettingsProps } from '../../typings';
4
4
  import GridLayout from '../GridLayout/GridLayout';
5
5
  import { OverlayControlItem } from '../OverlayControls/OverlayControls';
6
6
  interface DashKitGeneralProps {
@@ -39,11 +39,12 @@ export declare class DashKit extends React.PureComponent<DashKitInnerProps> {
39
39
  static defaultProps: DashKitDefaultProps;
40
40
  static registerPlugins(...plugins: Plugin[]): void;
41
41
  static setSettings(settings: Settings): void;
42
- static setItem({ item: setItem, namespace, config, options, }: {
42
+ static setItem({ item: setItem, namespace, config, options, groups, }: {
43
43
  item: SetConfigItem;
44
44
  namespace?: string;
45
45
  config: Config;
46
- options?: SetNewItemOptions;
46
+ options?: Omit<AddNewItemOptions, 'reflowLayoutOptions'>;
47
+ groups?: DashKitGroup[];
47
48
  }): Config;
48
49
  static removeItem({ id, config, itemsStateAndParams, }: {
49
50
  id: string;
@@ -4,10 +4,12 @@ exports.DashKit = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const react_1 = tslib_1.__importDefault(require("react"));
6
6
  const noop_1 = tslib_1.__importDefault(require("lodash/noop"));
7
+ const pick_1 = tslib_1.__importDefault(require("lodash/pick"));
7
8
  const constants_1 = require("../../constants");
8
9
  const utils_1 = require("../../utils");
9
10
  const DashKitView_1 = tslib_1.__importDefault(require("../DashKitView/DashKitView"));
10
11
  const registerManager = new utils_1.RegisterManager();
12
+ const getReflowProps = (props) => Object.assign({ compactType: 'vertical', cols: 36 }, (0, pick_1.default)(props, 'cols', 'maxRows', 'compactType'));
11
13
  class DashKit extends react_1.default.PureComponent {
12
14
  constructor() {
13
15
  super(...arguments);
@@ -21,22 +23,43 @@ class DashKit extends react_1.default.PureComponent {
21
23
  static setSettings(settings) {
22
24
  registerManager.setSettings(settings);
23
25
  }
24
- static setItem({ item: setItem, namespace = constants_1.DEFAULT_NAMESPACE, config, options = {}, }) {
26
+ static setItem({ item: setItem, namespace = constants_1.DEFAULT_NAMESPACE, config, options = {}, groups = [], }) {
25
27
  if (setItem.id) {
26
- return utils_1.UpdateManager.editItem({ item: setItem, namespace, config, options });
28
+ return utils_1.UpdateManager.editItem({
29
+ item: setItem,
30
+ namespace,
31
+ config,
32
+ options,
33
+ });
27
34
  }
28
35
  else {
29
36
  const item = setItem;
30
37
  const layout = Object.assign({}, registerManager.getItem(item.type).defaultLayout);
31
- if (item.layout) {
32
- Object.assign(layout, item.layout);
38
+ const defaultGridProps = getReflowProps(registerManager.gridLayout);
39
+ const reflowLayoutOptions = {
40
+ defaultProps: defaultGridProps,
41
+ groups: groups.reduce((memo, g) => {
42
+ const groupId = g.id || constants_1.DEFAULT_GROUP;
43
+ if (g.gridProperties) {
44
+ memo[groupId] = getReflowProps(g.gridProperties(defaultGridProps));
45
+ }
46
+ else {
47
+ memo[groupId] = defaultGridProps;
48
+ }
49
+ return memo;
50
+ }, {}),
51
+ };
52
+ const copyItem = Object.assign({}, item);
53
+ if (copyItem.layout) {
54
+ Object.assign(layout, copyItem.layout);
55
+ delete copyItem.layout;
33
56
  }
34
57
  return utils_1.UpdateManager.addItem({
35
- item,
58
+ item: copyItem,
36
59
  namespace,
37
60
  config,
38
61
  layout,
39
- options,
62
+ options: Object.assign(Object.assign({}, options), { reflowLayoutOptions }),
40
63
  });
41
64
  }
42
65
  }
@@ -78,18 +78,111 @@
78
78
  position: absolute;
79
79
  width: 20px;
80
80
  height: 20px;
81
- bottom: 0;
82
- right: 0;
83
- cursor: se-resize;
84
81
  z-index: 6;
85
82
  }
86
83
 
87
84
  .react-grid-item .react-resizable-handle::after {
88
85
  content: "";
89
86
  position: absolute;
90
- right: 3px;
91
- bottom: 3px;
92
87
  border: 4px solid transparent;
88
+ }
89
+
90
+ .react-grid-item .react-resizable-handle-s {
91
+ cursor: s-resize;
92
+ bottom: 0;
93
+ left: 50%;
94
+ transform: translateX(-50%) translateY(0);
95
+ }
96
+ .react-grid-item .react-resizable-handle-s::after {
97
+ bottom: -3px;
98
+ left: 4px;
99
+ border: 6px solid transparent;
100
+ border-top-color: rgba(0, 0, 0, 0.4);
101
+ }
102
+
103
+ .react-grid-item .react-resizable-handle-w {
104
+ cursor: w-resize;
105
+ top: 50%;
106
+ left: 0;
107
+ transform: translateX(0) translateY(-50%);
108
+ }
109
+ .react-grid-item .react-resizable-handle-w::after {
110
+ top: 4px;
111
+ left: -3px;
112
+ border: 6px solid transparent;
113
+ border-right-color: rgba(0, 0, 0, 0.4);
114
+ }
115
+
116
+ .react-grid-item .react-resizable-handle-e {
117
+ cursor: e-resize;
118
+ top: 50%;
119
+ right: 0;
120
+ transform: translateX(0) translateY(-50%);
121
+ }
122
+ .react-grid-item .react-resizable-handle-e::after {
123
+ top: 4px;
124
+ right: -3px;
125
+ border: 6px solid transparent;
126
+ border-left-color: rgba(0, 0, 0, 0.4);
127
+ }
128
+
129
+ .react-grid-item .react-resizable-handle-n {
130
+ cursor: n-resize;
131
+ top: 0;
132
+ left: 50%;
133
+ transform: translateX(-50%) translateY(0);
134
+ }
135
+ .react-grid-item .react-resizable-handle-n::after {
136
+ top: -3px;
137
+ left: 4px;
138
+ border: 6px solid transparent;
139
+ border-bottom-color: rgba(0, 0, 0, 0.4);
140
+ }
141
+
142
+ .react-grid-item .react-resizable-handle-sw {
143
+ cursor: sw-resize;
144
+ bottom: 0;
145
+ left: 0;
146
+ }
147
+ .react-grid-item .react-resizable-handle-sw::after {
148
+ bottom: 3px;
149
+ left: 3px;
150
+ border-left-color: rgba(0, 0, 0, 0.4);
151
+ border-bottom-color: rgba(0, 0, 0, 0.4);
152
+ }
153
+
154
+ .react-grid-item .react-resizable-handle-nw {
155
+ cursor: nw-resize;
156
+ top: 0;
157
+ left: 0;
158
+ }
159
+ .react-grid-item .react-resizable-handle-nw::after {
160
+ top: 3px;
161
+ left: 3px;
162
+ border-left-color: rgba(0, 0, 0, 0.4);
163
+ border-top-color: rgba(0, 0, 0, 0.4);
164
+ }
165
+
166
+ .react-grid-item .react-resizable-handle-se {
167
+ cursor: se-resize;
168
+ bottom: 0;
169
+ right: 0;
170
+ }
171
+ .react-grid-item .react-resizable-handle-se::after {
172
+ bottom: 3px;
173
+ right: 3px;
93
174
  border-right-color: rgba(0, 0, 0, 0.4);
94
175
  border-bottom-color: rgba(0, 0, 0, 0.4);
176
+ }
177
+
178
+ .react-grid-item .react-resizable-handle-ne {
179
+ cursor: ne-resize;
180
+ top: 0;
181
+ right: 0;
182
+ }
183
+ .react-grid-item .react-resizable-handle-ne::after {
184
+ top: 3px;
185
+ right: 3px;
186
+ border-right-color: rgba(0, 0, 0, 0.4);
187
+ border-top-color: rgba(0, 0, 0, 0.4);
95
188
  }
@@ -128,7 +128,7 @@ function useMemoStateContext(props) {
128
128
  react_1.default.useMemo(() => {
129
129
  const groups = props.groups;
130
130
  const layout = props.layout;
131
- const defaultProps = props.registerManager._gridLayout || {};
131
+ const defaultProps = props.registerManager.gridLayout || {};
132
132
  const nowrapGroups = {};
133
133
  let hasNowrapGroups = false;
134
134
  if (defaultProps.compactType === common_1.COMPACT_TYPE_HORIZONTAL_NOWRAP) {
@@ -4,14 +4,26 @@ import type { Config, ConfigItem, ConfigLayout } from '../shared';
4
4
  export interface AddConfigItem extends Omit<ConfigItem, 'id' | 'namespace'> {
5
5
  id?: null;
6
6
  namespace?: string;
7
- layout?: ConfigLayout;
7
+ layout?: Omit<ConfigLayout, 'i'>;
8
8
  }
9
9
  export type SetConfigItem = ConfigItem | AddConfigItem;
10
10
  export type SetItemOptions = {
11
11
  excludeIds?: string[];
12
+ };
13
+ export type GridReflowOptions = {
14
+ cols: number;
15
+ maxRows?: number;
16
+ compactType?: CompactType;
17
+ };
18
+ export type CompactType = ReactGridLayout.ReactGridLayoutProps['compactType'] | 'horizontal-nowrap';
19
+ export type ReflowLayoutOptions = {
20
+ defaultProps: GridReflowOptions;
21
+ groups?: Record<string, GridReflowOptions>;
22
+ };
23
+ export type AddNewItemOptions = SetItemOptions & {
12
24
  updateLayout?: ConfigLayout[];
25
+ reflowLayoutOptions?: ReflowLayoutOptions;
13
26
  };
14
- export type SetNewItemOptions = SetItemOptions;
15
27
  export interface DashkitGroupRenderProps {
16
28
  config: Config;
17
29
  editMode: boolean;
@@ -19,7 +31,7 @@ export interface DashkitGroupRenderProps {
19
31
  layout: ConfigLayout[];
20
32
  }
21
33
  export type ReactGridLayoutProps = Omit<ReactGridLayout.ReactGridLayoutProps, 'children' | 'compactType' | 'innerRef' | 'key' | 'layout' | 'isDraggable' | 'isResizable' | 'onDragStart' | 'onDragStop' | 'onResizeStart' | 'onResizeStop' | 'draggableHandle' | 'isDroppable' | 'onDropDragOver' | 'onDrop' | 'draggableCancel'> & {
22
- compactType?: ReactGridLayout.ReactGridLayoutProps['compactType'] | 'horizontal-nowrap';
34
+ compactType?: CompactType;
23
35
  };
24
36
  export interface DashKitGroup {
25
37
  id?: string;
@@ -196,7 +196,7 @@ describe('UpdateManager', () => {
196
196
  type: 'widget',
197
197
  },
198
198
  namespace: 'default',
199
- layout: { h: 4, w: 4, x: 0, y: 0 },
199
+ layout: { h: 4, w: 4, x: 0, y: Infinity },
200
200
  config: config,
201
201
  })).toEqual(Object.assign(Object.assign({}, config), { salt: '0.09852189776033704', counter: 15, items: [
202
202
  ...config.items,
@@ -215,7 +215,7 @@ describe('UpdateManager', () => {
215
215
  type: 'widget',
216
216
  },
217
217
  namespace: 'default',
218
- layout: { h: 4, w: 4, x: 0, y: 0 },
218
+ layout: { h: 4, w: 4, x: 0, y: Infinity },
219
219
  config: config,
220
220
  options: {
221
221
  excludeIds: ['EZ'],
@@ -260,6 +260,67 @@ describe('UpdateManager', () => {
260
260
  },
261
261
  ], layout: [...updateLayout, { h: 4, w: 12, x: 0, y: 0, i: 'EZ' }] }));
262
262
  });
263
+ it('prepend new item', () => {
264
+ expect(update_manager_1.UpdateManager.addItem({
265
+ item: {
266
+ data: {},
267
+ type: 'widget',
268
+ },
269
+ namespace: 'default',
270
+ layout: { h: 4, w: 36, x: 0, y: 0 },
271
+ config: config,
272
+ options: {
273
+ reflowLayoutOptions: {
274
+ defaultProps: {
275
+ cols: 36,
276
+ },
277
+ },
278
+ },
279
+ })).toEqual(Object.assign(Object.assign({}, config), { salt: '0.09852189776033704', counter: 15, items: [
280
+ ...config.items,
281
+ {
282
+ id: 'EZ',
283
+ data: {},
284
+ type: 'widget',
285
+ namespace: 'default',
286
+ },
287
+ ], layout: [
288
+ { i: 'al', h: 4, w: 4, x: 8, y: 4 },
289
+ { i: 'Q8', h: 16, w: 12, x: 12, y: 4 },
290
+ { i: 'L5', h: 2, w: 8, x: 0, y: 4 },
291
+ { i: 'C8', h: 2, w: 8, x: 10, y: 30 },
292
+ { i: 'lko', h: 2, w: 8, x: 0, y: 12 },
293
+ { i: 'qY', h: 2, w: 8, x: 0, y: 24 },
294
+ { i: 'EZ', h: 4, w: 36, x: 0, y: 0 },
295
+ ] }));
296
+ });
297
+ it('add item with no compactType', () => {
298
+ expect(update_manager_1.UpdateManager.addItem({
299
+ item: {
300
+ data: {},
301
+ type: 'widget',
302
+ },
303
+ namespace: 'default',
304
+ layout: { h: 4, w: 36, x: 0, y: 0 },
305
+ config: config,
306
+ options: {
307
+ reflowLayoutOptions: {
308
+ defaultProps: {
309
+ cols: 36,
310
+ compactType: null,
311
+ },
312
+ },
313
+ },
314
+ })).toEqual(Object.assign(Object.assign({}, config), { salt: '0.09852189776033704', counter: 15, items: [
315
+ ...config.items,
316
+ {
317
+ id: 'EZ',
318
+ data: {},
319
+ type: 'widget',
320
+ namespace: 'default',
321
+ },
322
+ ], layout: [...config.layout, { i: 'EZ', h: 4, w: 36, x: 0, y: 0 }] }));
323
+ });
263
324
  });
264
325
  describe('removeItem', () => {
265
326
  it('remove item by id', () => {
@@ -0,0 +1,4 @@
1
+ import { Layout } from 'react-grid-layout';
2
+ import type { CompactType } from '../';
3
+ export declare const compact: (layout: Layout[], compactType: CompactType, cols: number) => Layout[];
4
+ export declare const bottom: (layout: Layout[]) => number;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bottom = exports.compact = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_grid_layout_1 = tslib_1.__importDefault(require("react-grid-layout"));
6
+ const { utils } = react_grid_layout_1.default;
7
+ const compact = (layout, compactType, cols) => {
8
+ if (compactType === 'horizontal-nowrap') {
9
+ compactType = 'horizontal';
10
+ }
11
+ return utils.compact(layout, compactType, cols);
12
+ };
13
+ exports.compact = compact;
14
+ const bottom = (layout) => {
15
+ return utils.bottom(layout);
16
+ };
17
+ exports.bottom = bottom;
@@ -1,5 +1,5 @@
1
1
  import type { Config, ConfigItem, ConfigItemGroup, ItemStateAndParams, ItemStateAndParamsChangeOptions, ItemsStateAndParams } from '../shared';
2
- import type { AddConfigItem, SetItemOptions, SetNewItemOptions, WidgetLayout } from '../typings';
2
+ import type { AddConfigItem, AddNewItemOptions, SetItemOptions, WidgetLayout } from '../typings';
3
3
  import { RegisterManagerPluginLayout } from './register-manager';
4
4
  interface RemoveItemArg {
5
5
  id: string;
@@ -26,7 +26,7 @@ export declare class UpdateManager {
26
26
  namespace: string;
27
27
  layout: RegisterManagerPluginLayout;
28
28
  config: Config;
29
- options?: SetNewItemOptions;
29
+ options?: AddNewItemOptions;
30
30
  }): Config;
31
31
  static editItem({ item, namespace, config, options, }: {
32
32
  item: ConfigItem;
@@ -8,6 +8,7 @@ const pick_1 = tslib_1.__importDefault(require("lodash/pick"));
8
8
  const constants_1 = require("../constants");
9
9
  const shared_1 = require("../shared");
10
10
  const get_new_id_1 = require("./get-new-id");
11
+ const grid_layout_1 = require("./grid-layout");
11
12
  (0, immutability_helper_1.extend)('$auto', (value, object) => (object ? (0, immutability_helper_1.default)(object, value) : (0, immutability_helper_1.default)({}, value)));
12
13
  function removeItemVersion1({ id, config, itemsStateAndParams }) {
13
14
  const itemIndex = config.items.findIndex((item) => item.id === id);
@@ -226,6 +227,48 @@ function changeGroupParams({ groupItemIds, initiatorId, initiatorItem, itemsStat
226
227
  };
227
228
  return (0, immutability_helper_1.default)(itemsStateAndParams, obj);
228
229
  }
230
+ function reflowLayout(newLayoutItem, layout, reflowLayoutOptions) {
231
+ const byGroup = {};
232
+ const reducer = (memo, item, i, _items, isNewItem) => {
233
+ memo[item.i] = i;
234
+ const parent = item.parent || constants_1.DEFAULT_GROUP;
235
+ if (byGroup[parent]) {
236
+ if (isNewItem) {
237
+ byGroup[parent].unshift(item);
238
+ }
239
+ else {
240
+ byGroup[parent].push(item);
241
+ }
242
+ }
243
+ else {
244
+ byGroup[parent] = [item];
245
+ }
246
+ return memo;
247
+ };
248
+ const orderById = layout.reduce(reducer, {});
249
+ reducer(orderById, newLayoutItem, layout.length, layout, true);
250
+ const { defaultProps } = reflowLayoutOptions;
251
+ return Object.entries(byGroup)
252
+ .reduce((memo, [groupId, layoutItems]) => {
253
+ var _a;
254
+ let reflowOptions = defaultProps;
255
+ if ((_a = reflowLayoutOptions === null || reflowLayoutOptions === void 0 ? void 0 : reflowLayoutOptions.groups) === null || _a === void 0 ? void 0 : _a[groupId]) {
256
+ reflowOptions = reflowLayoutOptions === null || reflowLayoutOptions === void 0 ? void 0 : reflowLayoutOptions.groups[groupId];
257
+ }
258
+ if (reflowOptions.compactType === null) {
259
+ return memo.concat(layoutItems);
260
+ }
261
+ const compactedList = (0, grid_layout_1.compact)(layoutItems, reflowOptions.compactType, reflowOptions.cols).map((item) => {
262
+ const cleanCopy = (0, pick_1.default)(item, ['i', 'h', 'w', 'x', 'y', 'parent']);
263
+ if (groupId === constants_1.DEFAULT_GROUP) {
264
+ return cleanCopy;
265
+ }
266
+ return Object.assign(Object.assign({}, cleanCopy), { parent: groupId });
267
+ });
268
+ return memo.concat(compactedList);
269
+ }, [])
270
+ .sort((a, b) => orderById[a.i] - orderById[b.i]);
271
+ }
229
272
  class UpdateManager {
230
273
  static addItem({ item, namespace = constants_1.DEFAULT_NAMESPACE, layout, config, options = {}, }) {
231
274
  const salt = config.salt;
@@ -240,10 +283,32 @@ class UpdateManager {
240
283
  memo[t.i] = t;
241
284
  return memo;
242
285
  }, {});
243
- const newLayout = [
244
- ...config.layout.map((t) => (Object.assign(Object.assign({}, t), (byId[t.i] || {})))),
245
- Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id }),
246
- ];
286
+ let newLayout;
287
+ const newLayoutItem = Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id });
288
+ if (options.reflowLayoutOptions) {
289
+ newLayout = reflowLayout(newLayoutItem, config.layout.map((t) => (Object.assign(Object.assign({}, t), (byId[t.i] || {})))), options.reflowLayoutOptions);
290
+ }
291
+ else {
292
+ newLayout = [
293
+ ...config.layout.map((t) => (Object.assign(Object.assign({}, t), (byId[t.i] || {})))),
294
+ Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id }),
295
+ ];
296
+ }
297
+ return (0, immutability_helper_1.default)(config, {
298
+ items: { $push: [newItem] },
299
+ layout: { $set: newLayout },
300
+ counter: { $set: counter },
301
+ });
302
+ }
303
+ else if (isFinite(layout.y)) {
304
+ let newLayout;
305
+ const newLayoutItem = Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id });
306
+ if (options.reflowLayoutOptions) {
307
+ newLayout = reflowLayout(newLayoutItem, config.layout, options.reflowLayoutOptions);
308
+ }
309
+ else {
310
+ newLayout = [...config.layout, newLayoutItem];
311
+ }
247
312
  return (0, immutability_helper_1.default)(config, {
248
313
  items: { $push: [newItem] },
249
314
  layout: { $set: newLayout },
@@ -251,7 +316,7 @@ class UpdateManager {
251
316
  });
252
317
  }
253
318
  else {
254
- const layoutY = Math.max(0, ...config.layout.map(({ h, y }) => h + y));
319
+ const layoutY = (0, grid_layout_1.bottom)(config.layout);
255
320
  const newLayoutItem = Object.assign(Object.assign({}, saveDefaultLayout), { y: layoutY, i: newItem.id });
256
321
  return (0, immutability_helper_1.default)(config, {
257
322
  items: { $push: [newItem] },
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import type { Config, ConfigItem, GlobalParams, ItemDropProps, ItemsStateAndParams } from '../../shared';
3
- import { ContextProps, DashKitGroup, MenuItem, Plugin, SetConfigItem, SetNewItemOptions, Settings, SettingsProps } from '../../typings';
3
+ import { AddNewItemOptions, ContextProps, DashKitGroup, MenuItem, Plugin, SetConfigItem, Settings, SettingsProps } from '../../typings';
4
4
  import GridLayout from '../GridLayout/GridLayout';
5
5
  import { OverlayControlItem } from '../OverlayControls/OverlayControls';
6
6
  interface DashKitGeneralProps {
@@ -39,11 +39,12 @@ export declare class DashKit extends React.PureComponent<DashKitInnerProps> {
39
39
  static defaultProps: DashKitDefaultProps;
40
40
  static registerPlugins(...plugins: Plugin[]): void;
41
41
  static setSettings(settings: Settings): void;
42
- static setItem({ item: setItem, namespace, config, options, }: {
42
+ static setItem({ item: setItem, namespace, config, options, groups, }: {
43
43
  item: SetConfigItem;
44
44
  namespace?: string;
45
45
  config: Config;
46
- options?: SetNewItemOptions;
46
+ options?: Omit<AddNewItemOptions, 'reflowLayoutOptions'>;
47
+ groups?: DashKitGroup[];
47
48
  }): Config;
48
49
  static removeItem({ id, config, itemsStateAndParams, }: {
49
50
  id: string;
@@ -1,9 +1,11 @@
1
1
  import React from 'react';
2
2
  import noop from 'lodash/noop';
3
- import { DEFAULT_NAMESPACE } from '../../constants';
3
+ import pick from 'lodash/pick';
4
+ import { DEFAULT_GROUP, DEFAULT_NAMESPACE } from '../../constants';
4
5
  import { RegisterManager, UpdateManager } from '../../utils';
5
6
  import DashKitView from '../DashKitView/DashKitView';
6
7
  const registerManager = new RegisterManager();
8
+ const getReflowProps = (props) => Object.assign({ compactType: 'vertical', cols: 36 }, pick(props, 'cols', 'maxRows', 'compactType'));
7
9
  export class DashKit extends React.PureComponent {
8
10
  constructor() {
9
11
  super(...arguments);
@@ -17,22 +19,43 @@ export class DashKit extends React.PureComponent {
17
19
  static setSettings(settings) {
18
20
  registerManager.setSettings(settings);
19
21
  }
20
- static setItem({ item: setItem, namespace = DEFAULT_NAMESPACE, config, options = {}, }) {
22
+ static setItem({ item: setItem, namespace = DEFAULT_NAMESPACE, config, options = {}, groups = [], }) {
21
23
  if (setItem.id) {
22
- return UpdateManager.editItem({ item: setItem, namespace, config, options });
24
+ return UpdateManager.editItem({
25
+ item: setItem,
26
+ namespace,
27
+ config,
28
+ options,
29
+ });
23
30
  }
24
31
  else {
25
32
  const item = setItem;
26
33
  const layout = Object.assign({}, registerManager.getItem(item.type).defaultLayout);
27
- if (item.layout) {
28
- Object.assign(layout, item.layout);
34
+ const defaultGridProps = getReflowProps(registerManager.gridLayout);
35
+ const reflowLayoutOptions = {
36
+ defaultProps: defaultGridProps,
37
+ groups: groups.reduce((memo, g) => {
38
+ const groupId = g.id || DEFAULT_GROUP;
39
+ if (g.gridProperties) {
40
+ memo[groupId] = getReflowProps(g.gridProperties(defaultGridProps));
41
+ }
42
+ else {
43
+ memo[groupId] = defaultGridProps;
44
+ }
45
+ return memo;
46
+ }, {}),
47
+ };
48
+ const copyItem = Object.assign({}, item);
49
+ if (copyItem.layout) {
50
+ Object.assign(layout, copyItem.layout);
51
+ delete copyItem.layout;
29
52
  }
30
53
  return UpdateManager.addItem({
31
- item,
54
+ item: copyItem,
32
55
  namespace,
33
56
  config,
34
57
  layout,
35
- options,
58
+ options: Object.assign(Object.assign({}, options), { reflowLayoutOptions }),
36
59
  });
37
60
  }
38
61
  }
@@ -78,18 +78,111 @@
78
78
  position: absolute;
79
79
  width: 20px;
80
80
  height: 20px;
81
- bottom: 0;
82
- right: 0;
83
- cursor: se-resize;
84
81
  z-index: 6;
85
82
  }
86
83
 
87
84
  .react-grid-item .react-resizable-handle::after {
88
85
  content: "";
89
86
  position: absolute;
90
- right: 3px;
91
- bottom: 3px;
92
87
  border: 4px solid transparent;
88
+ }
89
+
90
+ .react-grid-item .react-resizable-handle-s {
91
+ cursor: s-resize;
92
+ bottom: 0;
93
+ left: 50%;
94
+ transform: translateX(-50%) translateY(0);
95
+ }
96
+ .react-grid-item .react-resizable-handle-s::after {
97
+ bottom: -3px;
98
+ left: 4px;
99
+ border: 6px solid transparent;
100
+ border-top-color: rgba(0, 0, 0, 0.4);
101
+ }
102
+
103
+ .react-grid-item .react-resizable-handle-w {
104
+ cursor: w-resize;
105
+ top: 50%;
106
+ left: 0;
107
+ transform: translateX(0) translateY(-50%);
108
+ }
109
+ .react-grid-item .react-resizable-handle-w::after {
110
+ top: 4px;
111
+ left: -3px;
112
+ border: 6px solid transparent;
113
+ border-right-color: rgba(0, 0, 0, 0.4);
114
+ }
115
+
116
+ .react-grid-item .react-resizable-handle-e {
117
+ cursor: e-resize;
118
+ top: 50%;
119
+ right: 0;
120
+ transform: translateX(0) translateY(-50%);
121
+ }
122
+ .react-grid-item .react-resizable-handle-e::after {
123
+ top: 4px;
124
+ right: -3px;
125
+ border: 6px solid transparent;
126
+ border-left-color: rgba(0, 0, 0, 0.4);
127
+ }
128
+
129
+ .react-grid-item .react-resizable-handle-n {
130
+ cursor: n-resize;
131
+ top: 0;
132
+ left: 50%;
133
+ transform: translateX(-50%) translateY(0);
134
+ }
135
+ .react-grid-item .react-resizable-handle-n::after {
136
+ top: -3px;
137
+ left: 4px;
138
+ border: 6px solid transparent;
139
+ border-bottom-color: rgba(0, 0, 0, 0.4);
140
+ }
141
+
142
+ .react-grid-item .react-resizable-handle-sw {
143
+ cursor: sw-resize;
144
+ bottom: 0;
145
+ left: 0;
146
+ }
147
+ .react-grid-item .react-resizable-handle-sw::after {
148
+ bottom: 3px;
149
+ left: 3px;
150
+ border-left-color: rgba(0, 0, 0, 0.4);
151
+ border-bottom-color: rgba(0, 0, 0, 0.4);
152
+ }
153
+
154
+ .react-grid-item .react-resizable-handle-nw {
155
+ cursor: nw-resize;
156
+ top: 0;
157
+ left: 0;
158
+ }
159
+ .react-grid-item .react-resizable-handle-nw::after {
160
+ top: 3px;
161
+ left: 3px;
162
+ border-left-color: rgba(0, 0, 0, 0.4);
163
+ border-top-color: rgba(0, 0, 0, 0.4);
164
+ }
165
+
166
+ .react-grid-item .react-resizable-handle-se {
167
+ cursor: se-resize;
168
+ bottom: 0;
169
+ right: 0;
170
+ }
171
+ .react-grid-item .react-resizable-handle-se::after {
172
+ bottom: 3px;
173
+ right: 3px;
93
174
  border-right-color: rgba(0, 0, 0, 0.4);
94
175
  border-bottom-color: rgba(0, 0, 0, 0.4);
176
+ }
177
+
178
+ .react-grid-item .react-resizable-handle-ne {
179
+ cursor: ne-resize;
180
+ top: 0;
181
+ right: 0;
182
+ }
183
+ .react-grid-item .react-resizable-handle-ne::after {
184
+ top: 3px;
185
+ right: 3px;
186
+ border-right-color: rgba(0, 0, 0, 0.4);
187
+ border-top-color: rgba(0, 0, 0, 0.4);
95
188
  }
@@ -125,7 +125,7 @@ function useMemoStateContext(props) {
125
125
  React.useMemo(() => {
126
126
  const groups = props.groups;
127
127
  const layout = props.layout;
128
- const defaultProps = props.registerManager._gridLayout || {};
128
+ const defaultProps = props.registerManager.gridLayout || {};
129
129
  const nowrapGroups = {};
130
130
  let hasNowrapGroups = false;
131
131
  if (defaultProps.compactType === COMPACT_TYPE_HORIZONTAL_NOWRAP) {
@@ -4,14 +4,26 @@ import type { Config, ConfigItem, ConfigLayout } from '../shared';
4
4
  export interface AddConfigItem extends Omit<ConfigItem, 'id' | 'namespace'> {
5
5
  id?: null;
6
6
  namespace?: string;
7
- layout?: ConfigLayout;
7
+ layout?: Omit<ConfigLayout, 'i'>;
8
8
  }
9
9
  export type SetConfigItem = ConfigItem | AddConfigItem;
10
10
  export type SetItemOptions = {
11
11
  excludeIds?: string[];
12
+ };
13
+ export type GridReflowOptions = {
14
+ cols: number;
15
+ maxRows?: number;
16
+ compactType?: CompactType;
17
+ };
18
+ export type CompactType = ReactGridLayout.ReactGridLayoutProps['compactType'] | 'horizontal-nowrap';
19
+ export type ReflowLayoutOptions = {
20
+ defaultProps: GridReflowOptions;
21
+ groups?: Record<string, GridReflowOptions>;
22
+ };
23
+ export type AddNewItemOptions = SetItemOptions & {
12
24
  updateLayout?: ConfigLayout[];
25
+ reflowLayoutOptions?: ReflowLayoutOptions;
13
26
  };
14
- export type SetNewItemOptions = SetItemOptions;
15
27
  export interface DashkitGroupRenderProps {
16
28
  config: Config;
17
29
  editMode: boolean;
@@ -19,7 +31,7 @@ export interface DashkitGroupRenderProps {
19
31
  layout: ConfigLayout[];
20
32
  }
21
33
  export type ReactGridLayoutProps = Omit<ReactGridLayout.ReactGridLayoutProps, 'children' | 'compactType' | 'innerRef' | 'key' | 'layout' | 'isDraggable' | 'isResizable' | 'onDragStart' | 'onDragStop' | 'onResizeStart' | 'onResizeStop' | 'draggableHandle' | 'isDroppable' | 'onDropDragOver' | 'onDrop' | 'draggableCancel'> & {
22
- compactType?: ReactGridLayout.ReactGridLayoutProps['compactType'] | 'horizontal-nowrap';
34
+ compactType?: CompactType;
23
35
  };
24
36
  export interface DashKitGroup {
25
37
  id?: string;
@@ -194,7 +194,7 @@ describe('UpdateManager', () => {
194
194
  type: 'widget',
195
195
  },
196
196
  namespace: 'default',
197
- layout: { h: 4, w: 4, x: 0, y: 0 },
197
+ layout: { h: 4, w: 4, x: 0, y: Infinity },
198
198
  config: config,
199
199
  })).toEqual(Object.assign(Object.assign({}, config), { salt: '0.09852189776033704', counter: 15, items: [
200
200
  ...config.items,
@@ -213,7 +213,7 @@ describe('UpdateManager', () => {
213
213
  type: 'widget',
214
214
  },
215
215
  namespace: 'default',
216
- layout: { h: 4, w: 4, x: 0, y: 0 },
216
+ layout: { h: 4, w: 4, x: 0, y: Infinity },
217
217
  config: config,
218
218
  options: {
219
219
  excludeIds: ['EZ'],
@@ -258,6 +258,67 @@ describe('UpdateManager', () => {
258
258
  },
259
259
  ], layout: [...updateLayout, { h: 4, w: 12, x: 0, y: 0, i: 'EZ' }] }));
260
260
  });
261
+ it('prepend new item', () => {
262
+ expect(UpdateManager.addItem({
263
+ item: {
264
+ data: {},
265
+ type: 'widget',
266
+ },
267
+ namespace: 'default',
268
+ layout: { h: 4, w: 36, x: 0, y: 0 },
269
+ config: config,
270
+ options: {
271
+ reflowLayoutOptions: {
272
+ defaultProps: {
273
+ cols: 36,
274
+ },
275
+ },
276
+ },
277
+ })).toEqual(Object.assign(Object.assign({}, config), { salt: '0.09852189776033704', counter: 15, items: [
278
+ ...config.items,
279
+ {
280
+ id: 'EZ',
281
+ data: {},
282
+ type: 'widget',
283
+ namespace: 'default',
284
+ },
285
+ ], layout: [
286
+ { i: 'al', h: 4, w: 4, x: 8, y: 4 },
287
+ { i: 'Q8', h: 16, w: 12, x: 12, y: 4 },
288
+ { i: 'L5', h: 2, w: 8, x: 0, y: 4 },
289
+ { i: 'C8', h: 2, w: 8, x: 10, y: 30 },
290
+ { i: 'lko', h: 2, w: 8, x: 0, y: 12 },
291
+ { i: 'qY', h: 2, w: 8, x: 0, y: 24 },
292
+ { i: 'EZ', h: 4, w: 36, x: 0, y: 0 },
293
+ ] }));
294
+ });
295
+ it('add item with no compactType', () => {
296
+ expect(UpdateManager.addItem({
297
+ item: {
298
+ data: {},
299
+ type: 'widget',
300
+ },
301
+ namespace: 'default',
302
+ layout: { h: 4, w: 36, x: 0, y: 0 },
303
+ config: config,
304
+ options: {
305
+ reflowLayoutOptions: {
306
+ defaultProps: {
307
+ cols: 36,
308
+ compactType: null,
309
+ },
310
+ },
311
+ },
312
+ })).toEqual(Object.assign(Object.assign({}, config), { salt: '0.09852189776033704', counter: 15, items: [
313
+ ...config.items,
314
+ {
315
+ id: 'EZ',
316
+ data: {},
317
+ type: 'widget',
318
+ namespace: 'default',
319
+ },
320
+ ], layout: [...config.layout, { i: 'EZ', h: 4, w: 36, x: 0, y: 0 }] }));
321
+ });
261
322
  });
262
323
  describe('removeItem', () => {
263
324
  it('remove item by id', () => {
@@ -0,0 +1,4 @@
1
+ import { Layout } from 'react-grid-layout';
2
+ import type { CompactType } from '../';
3
+ export declare const compact: (layout: Layout[], compactType: CompactType, cols: number) => Layout[];
4
+ export declare const bottom: (layout: Layout[]) => number;
@@ -0,0 +1,11 @@
1
+ import gridLayout from 'react-grid-layout';
2
+ const { utils } = gridLayout;
3
+ export const compact = (layout, compactType, cols) => {
4
+ if (compactType === 'horizontal-nowrap') {
5
+ compactType = 'horizontal';
6
+ }
7
+ return utils.compact(layout, compactType, cols);
8
+ };
9
+ export const bottom = (layout) => {
10
+ return utils.bottom(layout);
11
+ };
@@ -1,5 +1,5 @@
1
1
  import type { Config, ConfigItem, ConfigItemGroup, ItemStateAndParams, ItemStateAndParamsChangeOptions, ItemsStateAndParams } from '../shared';
2
- import type { AddConfigItem, SetItemOptions, SetNewItemOptions, WidgetLayout } from '../typings';
2
+ import type { AddConfigItem, AddNewItemOptions, SetItemOptions, WidgetLayout } from '../typings';
3
3
  import { RegisterManagerPluginLayout } from './register-manager';
4
4
  interface RemoveItemArg {
5
5
  id: string;
@@ -26,7 +26,7 @@ export declare class UpdateManager {
26
26
  namespace: string;
27
27
  layout: RegisterManagerPluginLayout;
28
28
  config: Config;
29
- options?: SetNewItemOptions;
29
+ options?: AddNewItemOptions;
30
30
  }): Config;
31
31
  static editItem({ item, namespace, config, options, }: {
32
32
  item: ConfigItem;
@@ -1,9 +1,10 @@
1
1
  import update, { extend } from 'immutability-helper';
2
2
  import omit from 'lodash/omit';
3
3
  import pick from 'lodash/pick';
4
- import { DEFAULT_NAMESPACE } from '../constants';
4
+ import { DEFAULT_GROUP, DEFAULT_NAMESPACE } from '../constants';
5
5
  import { META_KEY, addGroupToQueue, addToQueue, deleteFromQueue, getCurrentVersion, getInitialItemsStateAndParamsMeta, getItemsStateAndParamsMeta, isItemWithGroup, isItemWithTabs, mergeParamsWithAliases, pickActionParamsFromParams, resolveItemInnerId, transformParamsToActionParams, } from '../shared';
6
6
  import { getNewId } from './get-new-id';
7
+ import { bottom, compact } from './grid-layout';
7
8
  extend('$auto', (value, object) => (object ? update(object, value) : update({}, value)));
8
9
  function removeItemVersion1({ id, config, itemsStateAndParams }) {
9
10
  const itemIndex = config.items.findIndex((item) => item.id === id);
@@ -221,6 +222,48 @@ function changeGroupParams({ groupItemIds, initiatorId, initiatorItem, itemsStat
221
222
  };
222
223
  return update(itemsStateAndParams, obj);
223
224
  }
225
+ function reflowLayout(newLayoutItem, layout, reflowLayoutOptions) {
226
+ const byGroup = {};
227
+ const reducer = (memo, item, i, _items, isNewItem) => {
228
+ memo[item.i] = i;
229
+ const parent = item.parent || DEFAULT_GROUP;
230
+ if (byGroup[parent]) {
231
+ if (isNewItem) {
232
+ byGroup[parent].unshift(item);
233
+ }
234
+ else {
235
+ byGroup[parent].push(item);
236
+ }
237
+ }
238
+ else {
239
+ byGroup[parent] = [item];
240
+ }
241
+ return memo;
242
+ };
243
+ const orderById = layout.reduce(reducer, {});
244
+ reducer(orderById, newLayoutItem, layout.length, layout, true);
245
+ const { defaultProps } = reflowLayoutOptions;
246
+ return Object.entries(byGroup)
247
+ .reduce((memo, [groupId, layoutItems]) => {
248
+ var _a;
249
+ let reflowOptions = defaultProps;
250
+ if ((_a = reflowLayoutOptions === null || reflowLayoutOptions === void 0 ? void 0 : reflowLayoutOptions.groups) === null || _a === void 0 ? void 0 : _a[groupId]) {
251
+ reflowOptions = reflowLayoutOptions === null || reflowLayoutOptions === void 0 ? void 0 : reflowLayoutOptions.groups[groupId];
252
+ }
253
+ if (reflowOptions.compactType === null) {
254
+ return memo.concat(layoutItems);
255
+ }
256
+ const compactedList = compact(layoutItems, reflowOptions.compactType, reflowOptions.cols).map((item) => {
257
+ const cleanCopy = pick(item, ['i', 'h', 'w', 'x', 'y', 'parent']);
258
+ if (groupId === DEFAULT_GROUP) {
259
+ return cleanCopy;
260
+ }
261
+ return Object.assign(Object.assign({}, cleanCopy), { parent: groupId });
262
+ });
263
+ return memo.concat(compactedList);
264
+ }, [])
265
+ .sort((a, b) => orderById[a.i] - orderById[b.i]);
266
+ }
224
267
  export class UpdateManager {
225
268
  static addItem({ item, namespace = DEFAULT_NAMESPACE, layout, config, options = {}, }) {
226
269
  const salt = config.salt;
@@ -235,10 +278,32 @@ export class UpdateManager {
235
278
  memo[t.i] = t;
236
279
  return memo;
237
280
  }, {});
238
- const newLayout = [
239
- ...config.layout.map((t) => (Object.assign(Object.assign({}, t), (byId[t.i] || {})))),
240
- Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id }),
241
- ];
281
+ let newLayout;
282
+ const newLayoutItem = Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id });
283
+ if (options.reflowLayoutOptions) {
284
+ newLayout = reflowLayout(newLayoutItem, config.layout.map((t) => (Object.assign(Object.assign({}, t), (byId[t.i] || {})))), options.reflowLayoutOptions);
285
+ }
286
+ else {
287
+ newLayout = [
288
+ ...config.layout.map((t) => (Object.assign(Object.assign({}, t), (byId[t.i] || {})))),
289
+ Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id }),
290
+ ];
291
+ }
292
+ return update(config, {
293
+ items: { $push: [newItem] },
294
+ layout: { $set: newLayout },
295
+ counter: { $set: counter },
296
+ });
297
+ }
298
+ else if (isFinite(layout.y)) {
299
+ let newLayout;
300
+ const newLayoutItem = Object.assign(Object.assign({}, saveDefaultLayout), { i: newItem.id });
301
+ if (options.reflowLayoutOptions) {
302
+ newLayout = reflowLayout(newLayoutItem, config.layout, options.reflowLayoutOptions);
303
+ }
304
+ else {
305
+ newLayout = [...config.layout, newLayoutItem];
306
+ }
242
307
  return update(config, {
243
308
  items: { $push: [newItem] },
244
309
  layout: { $set: newLayout },
@@ -246,7 +311,7 @@ export class UpdateManager {
246
311
  });
247
312
  }
248
313
  else {
249
- const layoutY = Math.max(0, ...config.layout.map(({ h, y }) => h + y));
314
+ const layoutY = bottom(config.layout);
250
315
  const newLayoutItem = Object.assign(Object.assign({}, saveDefaultLayout), { y: layoutY, i: newItem.id });
251
316
  return update(config, {
252
317
  items: { $push: [newItem] },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/dashkit",
3
- "version": "8.10.2",
3
+ "version": "8.11.0-beta.0",
4
4
  "description": "Library for rendering dashboard grid layout",
5
5
  "license": "MIT",
6
6
  "repository": {