@gravity-ui/dashkit 3.0.1

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.
Files changed (232) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/LICENSE +21 -0
  3. package/README.md +364 -0
  4. package/build/cjs/components/DashKit/DashKit.d.ts +50 -0
  5. package/build/cjs/components/DashKit/DashKit.js +70 -0
  6. package/build/cjs/components/DashKit/index.d.ts +1 -0
  7. package/build/cjs/components/DashKit/index.js +4 -0
  8. package/build/cjs/components/DashKitView/DashKitView.css +4 -0
  9. package/build/cjs/components/DashKitView/DashKitView.d.ts +3 -0
  10. package/build/cjs/components/DashKitView/DashKitView.js +23 -0
  11. package/build/cjs/components/GridItem/GridItem.css +83 -0
  12. package/build/cjs/components/GridItem/GridItem.d.ts +3 -0
  13. package/build/cjs/components/GridItem/GridItem.js +70 -0
  14. package/build/cjs/components/GridLayout/GridLayout.d.ts +25 -0
  15. package/build/cjs/components/GridLayout/GridLayout.js +89 -0
  16. package/build/cjs/components/Item/Item.css +19 -0
  17. package/build/cjs/components/Item/Item.d.ts +67 -0
  18. package/build/cjs/components/Item/Item.js +23 -0
  19. package/build/cjs/components/MobileLayout/MobileLayout.css +10 -0
  20. package/build/cjs/components/MobileLayout/MobileLayout.d.ts +16 -0
  21. package/build/cjs/components/MobileLayout/MobileLayout.js +54 -0
  22. package/build/cjs/components/MobileLayout/helpers.d.ts +3 -0
  23. package/build/cjs/components/MobileLayout/helpers.js +32 -0
  24. package/build/cjs/components/OverlayControls/OverlayControls.css +28 -0
  25. package/build/cjs/components/OverlayControls/OverlayControls.d.ts +54 -0
  26. package/build/cjs/components/OverlayControls/OverlayControls.js +212 -0
  27. package/build/cjs/components/index.d.ts +1 -0
  28. package/build/cjs/components/index.js +4 -0
  29. package/build/cjs/constants/common.d.ts +7 -0
  30. package/build/cjs/constants/common.js +10 -0
  31. package/build/cjs/constants/index.d.ts +1 -0
  32. package/build/cjs/constants/index.js +4 -0
  33. package/build/cjs/context/DashKitContext.d.ts +2 -0
  34. package/build/cjs/context/DashKitContext.js +7 -0
  35. package/build/cjs/hocs/prepareItem.d.ts +66 -0
  36. package/build/cjs/hocs/prepareItem.js +66 -0
  37. package/build/cjs/hocs/withContext.d.ts +165 -0
  38. package/build/cjs/hocs/withContext.js +203 -0
  39. package/build/cjs/hooks/useCalcLayout.d.ts +13 -0
  40. package/build/cjs/hooks/useCalcLayout.js +31 -0
  41. package/build/cjs/i18n/en.json +8 -0
  42. package/build/cjs/i18n/index.d.ts +4 -0
  43. package/build/cjs/i18n/index.js +19 -0
  44. package/build/cjs/i18n/ru.json +8 -0
  45. package/build/cjs/icons/CloseIcon.d.ts +2 -0
  46. package/build/cjs/icons/CloseIcon.js +10 -0
  47. package/build/cjs/icons/CogIcon.d.ts +2 -0
  48. package/build/cjs/icons/CogIcon.js +10 -0
  49. package/build/cjs/icons/CopyIcon.d.ts +2 -0
  50. package/build/cjs/icons/CopyIcon.js +10 -0
  51. package/build/cjs/icons/DeleteIcon.d.ts +2 -0
  52. package/build/cjs/icons/DeleteIcon.js +12 -0
  53. package/build/cjs/icons/DotsIcon.d.ts +2 -0
  54. package/build/cjs/icons/DotsIcon.js +10 -0
  55. package/build/cjs/icons/TickIcon.d.ts +2 -0
  56. package/build/cjs/icons/TickIcon.js +10 -0
  57. package/build/cjs/icons/WarningIcon.d.ts +2 -0
  58. package/build/cjs/icons/WarningIcon.js +10 -0
  59. package/build/cjs/index.d.ts +5 -0
  60. package/build/cjs/index.js +8 -0
  61. package/build/cjs/plugins/Text/Text.css +34 -0
  62. package/build/cjs/plugins/Text/Text.d.ts +52 -0
  63. package/build/cjs/plugins/Text/Text.js +119 -0
  64. package/build/cjs/plugins/Title/Title.css +24 -0
  65. package/build/cjs/plugins/Title/Title.d.ts +14 -0
  66. package/build/cjs/plugins/Title/Title.js +25 -0
  67. package/build/cjs/shared/constants/common.d.ts +2 -0
  68. package/build/cjs/shared/constants/common.js +5 -0
  69. package/build/cjs/shared/constants/index.d.ts +1 -0
  70. package/build/cjs/shared/constants/index.js +4 -0
  71. package/build/cjs/shared/index.d.ts +4 -0
  72. package/build/cjs/shared/index.js +7 -0
  73. package/build/cjs/shared/modules/__tests__/helpers.test.d.ts +1 -0
  74. package/build/cjs/shared/modules/__tests__/helpers.test.js +217 -0
  75. package/build/cjs/shared/modules/helpers.d.ts +42 -0
  76. package/build/cjs/shared/modules/helpers.js +172 -0
  77. package/build/cjs/shared/modules/index.d.ts +2 -0
  78. package/build/cjs/shared/modules/index.js +5 -0
  79. package/build/cjs/shared/modules/state-and-params.d.ts +16 -0
  80. package/build/cjs/shared/modules/state-and-params.js +94 -0
  81. package/build/cjs/shared/types/common.d.ts +5 -0
  82. package/build/cjs/shared/types/common.js +2 -0
  83. package/build/cjs/shared/types/config.d.ts +49 -0
  84. package/build/cjs/shared/types/config.js +2 -0
  85. package/build/cjs/shared/types/index.d.ts +4 -0
  86. package/build/cjs/shared/types/index.js +7 -0
  87. package/build/cjs/shared/types/plugin.d.ts +8 -0
  88. package/build/cjs/shared/types/plugin.js +2 -0
  89. package/build/cjs/shared/types/state-and-params.d.ts +22 -0
  90. package/build/cjs/shared/types/state-and-params.js +3 -0
  91. package/build/cjs/shared/units/__tests__/state-and-params.test.d.ts +241 -0
  92. package/build/cjs/shared/units/__tests__/state-and-params.test.js +299 -0
  93. package/build/cjs/shared/units/datalens/index.d.ts +5 -0
  94. package/build/cjs/shared/units/datalens/index.js +25 -0
  95. package/build/cjs/shared/units/index.d.ts +1 -0
  96. package/build/cjs/shared/units/index.js +4 -0
  97. package/build/cjs/typings/common.d.ts +32 -0
  98. package/build/cjs/typings/common.js +2 -0
  99. package/build/cjs/typings/config.d.ts +7 -0
  100. package/build/cjs/typings/config.js +2 -0
  101. package/build/cjs/typings/global.d.ts +7 -0
  102. package/build/cjs/typings/index.d.ts +3 -0
  103. package/build/cjs/typings/index.js +6 -0
  104. package/build/cjs/typings/plugin.d.ts +39 -0
  105. package/build/cjs/typings/plugin.js +2 -0
  106. package/build/cjs/utils/__tests__/mock.test.d.ts +1 -0
  107. package/build/cjs/utils/__tests__/mock.test.js +8 -0
  108. package/build/cjs/utils/configure.d.ts +9 -0
  109. package/build/cjs/utils/configure.js +21 -0
  110. package/build/cjs/utils/i18n.d.ts +7 -0
  111. package/build/cjs/utils/i18n.js +13 -0
  112. package/build/cjs/utils/index.d.ts +3 -0
  113. package/build/cjs/utils/index.js +6 -0
  114. package/build/cjs/utils/register-manager.d.ts +46 -0
  115. package/build/cjs/utils/register-manager.js +58 -0
  116. package/build/cjs/utils/update-manager.d.ts +37 -0
  117. package/build/cjs/utils/update-manager.js +274 -0
  118. package/build/esm/components/DashKit/DashKit.d.ts +50 -0
  119. package/build/esm/components/DashKit/DashKit.js +65 -0
  120. package/build/esm/components/DashKit/index.d.ts +1 -0
  121. package/build/esm/components/DashKit/index.js +1 -0
  122. package/build/esm/components/DashKitView/DashKitView.css +4 -0
  123. package/build/esm/components/DashKitView/DashKitView.d.ts +3 -0
  124. package/build/esm/components/DashKitView/DashKitView.js +21 -0
  125. package/build/esm/components/GridItem/GridItem.css +83 -0
  126. package/build/esm/components/GridItem/GridItem.d.ts +3 -0
  127. package/build/esm/components/GridItem/GridItem.js +68 -0
  128. package/build/esm/components/GridLayout/GridLayout.d.ts +25 -0
  129. package/build/esm/components/GridLayout/GridLayout.js +85 -0
  130. package/build/esm/components/Item/Item.css +19 -0
  131. package/build/esm/components/Item/Item.d.ts +67 -0
  132. package/build/esm/components/Item/Item.js +21 -0
  133. package/build/esm/components/MobileLayout/MobileLayout.css +10 -0
  134. package/build/esm/components/MobileLayout/MobileLayout.d.ts +16 -0
  135. package/build/esm/components/MobileLayout/MobileLayout.js +51 -0
  136. package/build/esm/components/MobileLayout/helpers.d.ts +3 -0
  137. package/build/esm/components/MobileLayout/helpers.js +28 -0
  138. package/build/esm/components/OverlayControls/OverlayControls.css +28 -0
  139. package/build/esm/components/OverlayControls/OverlayControls.d.ts +55 -0
  140. package/build/esm/components/OverlayControls/OverlayControls.js +209 -0
  141. package/build/esm/components/index.d.ts +1 -0
  142. package/build/esm/components/index.js +1 -0
  143. package/build/esm/constants/common.d.ts +7 -0
  144. package/build/esm/constants/common.js +7 -0
  145. package/build/esm/constants/index.d.ts +1 -0
  146. package/build/esm/constants/index.js +1 -0
  147. package/build/esm/context/DashKitContext.d.ts +2 -0
  148. package/build/esm/context/DashKitContext.js +3 -0
  149. package/build/esm/hocs/prepareItem.d.ts +66 -0
  150. package/build/esm/hocs/prepareItem.js +61 -0
  151. package/build/esm/hocs/withContext.d.ts +165 -0
  152. package/build/esm/hocs/withContext.js +198 -0
  153. package/build/esm/hooks/useCalcLayout.d.ts +13 -0
  154. package/build/esm/hooks/useCalcLayout.js +26 -0
  155. package/build/esm/i18n/en.json +8 -0
  156. package/build/esm/i18n/index.d.ts +4 -0
  157. package/build/esm/i18n/index.js +15 -0
  158. package/build/esm/i18n/ru.json +8 -0
  159. package/build/esm/icons/CloseIcon.d.ts +2 -0
  160. package/build/esm/icons/CloseIcon.js +5 -0
  161. package/build/esm/icons/CogIcon.d.ts +2 -0
  162. package/build/esm/icons/CogIcon.js +5 -0
  163. package/build/esm/icons/CopyIcon.d.ts +2 -0
  164. package/build/esm/icons/CopyIcon.js +5 -0
  165. package/build/esm/icons/DeleteIcon.d.ts +2 -0
  166. package/build/esm/icons/DeleteIcon.js +7 -0
  167. package/build/esm/icons/DotsIcon.d.ts +2 -0
  168. package/build/esm/icons/DotsIcon.js +5 -0
  169. package/build/esm/icons/TickIcon.d.ts +2 -0
  170. package/build/esm/icons/TickIcon.js +5 -0
  171. package/build/esm/icons/WarningIcon.d.ts +2 -0
  172. package/build/esm/icons/WarningIcon.js +5 -0
  173. package/build/esm/index.d.ts +5 -0
  174. package/build/esm/index.js +5 -0
  175. package/build/esm/plugins/Text/Text.css +34 -0
  176. package/build/esm/plugins/Text/Text.d.ts +53 -0
  177. package/build/esm/plugins/Text/Text.js +115 -0
  178. package/build/esm/plugins/Title/Title.css +24 -0
  179. package/build/esm/plugins/Title/Title.d.ts +15 -0
  180. package/build/esm/plugins/Title/Title.js +21 -0
  181. package/build/esm/shared/constants/common.d.ts +2 -0
  182. package/build/esm/shared/constants/common.js +2 -0
  183. package/build/esm/shared/constants/index.d.ts +1 -0
  184. package/build/esm/shared/constants/index.js +1 -0
  185. package/build/esm/shared/index.d.ts +4 -0
  186. package/build/esm/shared/index.js +4 -0
  187. package/build/esm/shared/modules/__tests__/helpers.test.d.ts +1 -0
  188. package/build/esm/shared/modules/__tests__/helpers.test.js +215 -0
  189. package/build/esm/shared/modules/helpers.d.ts +42 -0
  190. package/build/esm/shared/modules/helpers.js +157 -0
  191. package/build/esm/shared/modules/index.d.ts +2 -0
  192. package/build/esm/shared/modules/index.js +2 -0
  193. package/build/esm/shared/modules/state-and-params.d.ts +16 -0
  194. package/build/esm/shared/modules/state-and-params.js +87 -0
  195. package/build/esm/shared/types/common.d.ts +5 -0
  196. package/build/esm/shared/types/common.js +1 -0
  197. package/build/esm/shared/types/config.d.ts +49 -0
  198. package/build/esm/shared/types/config.js +1 -0
  199. package/build/esm/shared/types/index.d.ts +4 -0
  200. package/build/esm/shared/types/index.js +4 -0
  201. package/build/esm/shared/types/plugin.d.ts +8 -0
  202. package/build/esm/shared/types/plugin.js +1 -0
  203. package/build/esm/shared/types/state-and-params.d.ts +22 -0
  204. package/build/esm/shared/types/state-and-params.js +1 -0
  205. package/build/esm/shared/units/__tests__/state-and-params.test.d.ts +241 -0
  206. package/build/esm/shared/units/__tests__/state-and-params.test.js +295 -0
  207. package/build/esm/shared/units/datalens/index.d.ts +5 -0
  208. package/build/esm/shared/units/datalens/index.js +20 -0
  209. package/build/esm/shared/units/index.d.ts +1 -0
  210. package/build/esm/shared/units/index.js +1 -0
  211. package/build/esm/typings/common.d.ts +32 -0
  212. package/build/esm/typings/common.js +1 -0
  213. package/build/esm/typings/config.d.ts +7 -0
  214. package/build/esm/typings/config.js +1 -0
  215. package/build/esm/typings/global.d.ts +7 -0
  216. package/build/esm/typings/index.d.ts +3 -0
  217. package/build/esm/typings/index.js +3 -0
  218. package/build/esm/typings/plugin.d.ts +39 -0
  219. package/build/esm/typings/plugin.js +1 -0
  220. package/build/esm/utils/__tests__/mock.test.d.ts +1 -0
  221. package/build/esm/utils/__tests__/mock.test.js +8 -0
  222. package/build/esm/utils/configure.d.ts +9 -0
  223. package/build/esm/utils/configure.js +15 -0
  224. package/build/esm/utils/i18n.d.ts +7 -0
  225. package/build/esm/utils/i18n.js +9 -0
  226. package/build/esm/utils/index.d.ts +3 -0
  227. package/build/esm/utils/index.js +3 -0
  228. package/build/esm/utils/register-manager.d.ts +46 -0
  229. package/build/esm/utils/register-manager.js +54 -0
  230. package/build/esm/utils/update-manager.d.ts +37 -0
  231. package/build/esm/utils/update-manager.js +269 -0
  232. package/package.json +107 -0
@@ -0,0 +1,15 @@
1
+ let subs = [];
2
+ const config = {};
3
+ export const configure = (newConfig) => {
4
+ Object.assign(config, newConfig);
5
+ subs.forEach((sub) => {
6
+ sub(config);
7
+ });
8
+ };
9
+ export const subscribeConfigure = (sub) => {
10
+ subs.push(sub);
11
+ return () => {
12
+ subs = subs.filter((item) => item !== sub);
13
+ };
14
+ };
15
+ export const getConfig = () => config;
@@ -0,0 +1,7 @@
1
+ import { Lang } from './configure';
2
+ /**
3
+ * Preferred method to set language.
4
+ * @param lang
5
+ * @return {void}
6
+ */
7
+ export declare const setLang: (lang: Lang) => void;
@@ -0,0 +1,9 @@
1
+ import { configure } from './configure';
2
+ /**
3
+ * Preferred method to set language.
4
+ * @param lang
5
+ * @return {void}
6
+ */
7
+ export const setLang = (lang) => {
8
+ configure({ lang: lang });
9
+ };
@@ -0,0 +1,3 @@
1
+ export * from './i18n';
2
+ export * from './register-manager';
3
+ export * from './update-manager';
@@ -0,0 +1,3 @@
1
+ export * from './i18n';
2
+ export * from './register-manager';
3
+ export * from './update-manager';
@@ -0,0 +1,46 @@
1
+ import { Plugin, PluginDefaultLayout, Settings } from '../typings';
2
+ interface RegisterManagerDefaultLayout {
3
+ x: number;
4
+ y: number;
5
+ w: number;
6
+ h: number;
7
+ minW: number;
8
+ minH: number;
9
+ }
10
+ export declare type RegisterManagerPluginLayout = RegisterManagerDefaultLayout & PluginDefaultLayout;
11
+ export interface RegisterManagerPlugin extends Omit<Plugin, 'defaultLayout'> {
12
+ defaultLayout: RegisterManagerDefaultLayout & Plugin['defaultLayout'];
13
+ }
14
+ export declare type RegisterManagerGridLayout = {
15
+ margin: [number, number];
16
+ rowHeight: number;
17
+ cols: number;
18
+ containerPadding: [number, number];
19
+ } & Settings['gridLayout'];
20
+ export declare type RegisterManagerSettings = {
21
+ theme: string;
22
+ } & Settings;
23
+ export declare class RegisterManager {
24
+ private _items;
25
+ private _defaultLayout;
26
+ private _gridLayout;
27
+ private _settings;
28
+ registerPlugin(plugin: Plugin): void;
29
+ setSettings(settings?: Settings): void;
30
+ get settings(): RegisterManagerSettings;
31
+ get gridLayout(): {
32
+ margin: [number, number];
33
+ rowHeight: number;
34
+ cols: number;
35
+ containerPadding: [number, number];
36
+ } & {
37
+ margin?: [number, number] | undefined;
38
+ rowHeight?: number | undefined;
39
+ cols?: number | undefined;
40
+ containerPadding?: [number, number] | undefined;
41
+ };
42
+ getPlugins(): RegisterManagerPlugin[];
43
+ getItem(type: string): RegisterManagerPlugin;
44
+ check(type: string): boolean;
45
+ }
46
+ export {};
@@ -0,0 +1,54 @@
1
+ import { __rest } from "tslib";
2
+ export class RegisterManager {
3
+ constructor() {
4
+ this._items = {};
5
+ this._defaultLayout = {
6
+ x: 0,
7
+ y: Infinity,
8
+ w: Infinity,
9
+ h: 4,
10
+ minW: 4,
11
+ minH: 2,
12
+ };
13
+ this._gridLayout = {
14
+ rowHeight: 18,
15
+ cols: 36,
16
+ margin: [2, 2],
17
+ containerPadding: [0, 0],
18
+ };
19
+ this._settings = {
20
+ theme: 'default',
21
+ };
22
+ }
23
+ registerPlugin(plugin) {
24
+ const { type, defaultLayout = {} } = plugin, item = __rest(plugin, ["type", "defaultLayout"]);
25
+ if (type in this._items) {
26
+ throw new Error(`DashKit.registerPlugins: type ${type} уже был зарегистрирован`);
27
+ }
28
+ if (typeof plugin.renderer !== 'function') {
29
+ throw new Error('DashKit.registerPlugins: renderer должна быть функцией');
30
+ }
31
+ this._items[type] = Object.assign(Object.assign({}, item), { type, defaultLayout: Object.assign(Object.assign({}, this._defaultLayout), defaultLayout) });
32
+ }
33
+ setSettings(settings = {}) {
34
+ Object.assign(this._settings, settings);
35
+ if (settings.gridLayout) {
36
+ this._gridLayout = Object.assign(Object.assign({}, this._gridLayout), settings.gridLayout);
37
+ }
38
+ }
39
+ get settings() {
40
+ return this._settings;
41
+ }
42
+ get gridLayout() {
43
+ return this._gridLayout;
44
+ }
45
+ getPlugins() {
46
+ return Object.values(this._items);
47
+ }
48
+ getItem(type) {
49
+ return this._items[type];
50
+ }
51
+ check(type) {
52
+ return type in this._items;
53
+ }
54
+ }
@@ -0,0 +1,37 @@
1
+ import { Config, ConfigItem, ItemsStateAndParams, ItemStateAndParams } from '../shared';
2
+ import { AddConfigItem, WidgetLayout } from '../typings';
3
+ import { RegisterManagerPluginLayout } from './register-manager';
4
+ interface RemoveItemArg {
5
+ id: string;
6
+ config: Config;
7
+ itemsStateAndParams: ItemsStateAndParams;
8
+ }
9
+ interface ChangeStateAndParamsArg {
10
+ id: string;
11
+ config: Config;
12
+ itemsStateAndParams: ItemsStateAndParams;
13
+ stateAndParams: ItemStateAndParams;
14
+ }
15
+ export declare class UpdateManager {
16
+ static addItem({ item, namespace, layout, config, }: {
17
+ item: AddConfigItem;
18
+ namespace: string;
19
+ layout: RegisterManagerPluginLayout;
20
+ config: Config;
21
+ }): Config;
22
+ static editItem({ item, namespace, config, }: {
23
+ item: ConfigItem;
24
+ namespace: string;
25
+ config: Config;
26
+ }): Config;
27
+ static removeItem({ id, config, itemsStateAndParams }: RemoveItemArg): {
28
+ config: Config;
29
+ itemsStateAndParams: ItemsStateAndParams;
30
+ };
31
+ static updateLayout({ layout, config }: {
32
+ layout: WidgetLayout[];
33
+ config: Config;
34
+ }): Config;
35
+ static changeStateAndParams({ id: initiatorId, config, stateAndParams, itemsStateAndParams, }: ChangeStateAndParamsArg): ItemsStateAndParams;
36
+ }
37
+ export {};
@@ -0,0 +1,269 @@
1
+ import update, { extend } from 'immutability-helper';
2
+ import Hashids from 'hashids';
3
+ import omit from 'lodash/omit';
4
+ import pick from 'lodash/pick';
5
+ import { mergeParamsWithAliases, isItemWithTabs, getCurrentVersion, META_KEY, deleteFromQueue, addToQueue, getInitialItemsStateAndParamsMeta, resolveItemInnerId, getItemsStateAndParamsMeta, } from '../shared';
6
+ import { DEFAULT_NAMESPACE } from '../constants';
7
+ extend('$auto', (value, object) => (object ? update(object, value) : update({}, value)));
8
+ function removeItemVersion1({ id, config, itemsStateAndParams }) {
9
+ const itemIndex = config.items.findIndex((item) => item.id === id);
10
+ const removeItem = config.items[itemIndex];
11
+ const { defaults = {} } = removeItem;
12
+ const itemParamsKeys = Object.keys(defaults);
13
+ const getParams = (excludeId, items) => {
14
+ return Object.keys(items.reduce((acc, item) => {
15
+ if (item.id !== excludeId) {
16
+ Object.assign(acc, item.defaults);
17
+ }
18
+ return acc;
19
+ }, {}));
20
+ };
21
+ const allParamsKeys = getParams(id, config.items);
22
+ const allNamespaceParamsKeys = getParams(id, config.items.filter((item) => item.namespace === removeItem.namespace));
23
+ const uniqParamsKeys = itemParamsKeys.filter((key) => !allParamsKeys.includes(key));
24
+ const uniqNamespaceParamsKeys = itemParamsKeys.filter((key) => !allNamespaceParamsKeys.includes(key));
25
+ const withoutUniqItemsParams = Object.keys(itemsStateAndParams)
26
+ .filter((key) => key !== id)
27
+ .reduce((acc, key) => {
28
+ const { params } = itemsStateAndParams[key];
29
+ // в state из урла могут быть элементы, которых нет в config.items
30
+ const item = config.items.find((configItem) => configItem.id === key);
31
+ if (params && item) {
32
+ const { namespace } = item;
33
+ const currentUniqParamsKeys = namespace === removeItem.namespace ? uniqNamespaceParamsKeys : uniqParamsKeys;
34
+ const resultParams = omit(params, currentUniqParamsKeys);
35
+ if (Object.keys(params).length !== Object.keys(resultParams).length) {
36
+ acc[key] = {
37
+ params: {
38
+ $set: resultParams,
39
+ },
40
+ };
41
+ }
42
+ }
43
+ return acc;
44
+ }, {});
45
+ const newItemsStateAndParams = update(itemsStateAndParams, Object.assign({ $unset: [id] }, withoutUniqItemsParams));
46
+ const connections = config.connections.filter(({ from, to }) => id !== from && id !== to);
47
+ const newConfig = update(config, {
48
+ items: {
49
+ $splice: [[itemIndex, 1]],
50
+ },
51
+ layout: {
52
+ $splice: [[itemIndex, 1]],
53
+ },
54
+ connections: {
55
+ $set: connections,
56
+ },
57
+ });
58
+ return {
59
+ config: newConfig,
60
+ itemsStateAndParams: newItemsStateAndParams,
61
+ };
62
+ }
63
+ function getAllowableChangedParams(item, stateAndParams, itemsStateAndParams) {
64
+ var _a;
65
+ let allowedParams = {};
66
+ if (isItemWithTabs(item)) {
67
+ let tab;
68
+ if ('state' in stateAndParams && ((_a = stateAndParams.state) === null || _a === void 0 ? void 0 : _a.tabId)) {
69
+ tab = item.data.tabs.find(({ id }) => { var _a; return id === ((_a = stateAndParams.state) === null || _a === void 0 ? void 0 : _a.tabId); });
70
+ }
71
+ if (!tab) {
72
+ const tabId = resolveItemInnerId({ item, itemsStateAndParams });
73
+ tab = item.data.tabs.find(({ id }) => id === tabId);
74
+ }
75
+ allowedParams = pick(stateAndParams.params, Object.keys((tab === null || tab === void 0 ? void 0 : tab.params) || {}));
76
+ }
77
+ else {
78
+ allowedParams = pick(stateAndParams.params, Object.keys(item.defaults || {}));
79
+ }
80
+ if (Object.keys(allowedParams).length !== Object.keys(stateAndParams.params || {}).length) {
81
+ console.warn('Параметры, которых нет в defaults, будут проигнорированы!');
82
+ }
83
+ return allowedParams;
84
+ }
85
+ function changeStateAndParamsVersion1({ id: initiatorId, config, stateAndParams, itemsStateAndParams, }) {
86
+ const hasState = 'state' in stateAndParams;
87
+ const { aliases } = config;
88
+ if ('params' in stateAndParams) {
89
+ const initiatorItem = config.items.find(({ id }) => id === initiatorId);
90
+ const allowableParams = getAllowableChangedParams(initiatorItem, stateAndParams, itemsStateAndParams);
91
+ const allowableParamsWithAliases = mergeParamsWithAliases({
92
+ aliases,
93
+ namespace: initiatorItem.namespace,
94
+ params: allowableParams,
95
+ });
96
+ const ignoresInitiatorIds = config.connections
97
+ .filter(({ to }) => to === initiatorId)
98
+ .map(({ from }) => from);
99
+ const updateIds = config.items
100
+ .filter((item) => item.namespace === initiatorItem.namespace &&
101
+ !ignoresInitiatorIds.includes(item.id) &&
102
+ (!isItemWithTabs(item) ||
103
+ (isItemWithTabs(item) &&
104
+ item.data.tabs.every(({ id }) => !ignoresInitiatorIds.includes(id)))))
105
+ .map(({ id }) => id);
106
+ return update(itemsStateAndParams, updateIds.reduce((acc, currentId) => {
107
+ var _a;
108
+ acc[currentId] = {
109
+ $auto: Object.assign({ params: {
110
+ [((_a = itemsStateAndParams[currentId]) === null || _a === void 0 ? void 0 : _a.params)
111
+ ? '$merge'
112
+ : '$set']: allowableParamsWithAliases,
113
+ } }, (hasState && currentId === initiatorId
114
+ ? { state: { $set: stateAndParams.state } }
115
+ : {})),
116
+ };
117
+ return acc;
118
+ }, {}));
119
+ }
120
+ else if (hasState) {
121
+ return update(itemsStateAndParams, {
122
+ [initiatorId]: {
123
+ $auto: {
124
+ state: { $set: stateAndParams.state },
125
+ },
126
+ },
127
+ });
128
+ }
129
+ else {
130
+ return itemsStateAndParams;
131
+ }
132
+ }
133
+ export class UpdateManager {
134
+ // TODO: проверять, что id нового элемента уникальный
135
+ static addItem({ item, namespace = DEFAULT_NAMESPACE, layout, config, }) {
136
+ const layoutY = Math.max(0, ...config.layout.map(({ h, y }) => h + y));
137
+ const saveDefaultLayout = pick(layout, ['h', 'w', 'x', 'y']);
138
+ const hashids = new Hashids(config.salt);
139
+ let counter = config.counter;
140
+ const resultData = isItemWithTabs(item)
141
+ ? Object.assign(Object.assign({}, item.data), { tabs: item.data.tabs.map((tab) => tab.id ? tab : Object.assign(Object.assign({}, tab), { id: hashids.encode(++counter) })) }) : item.data;
142
+ const newItem = Object.assign(Object.assign({}, item), { id: hashids.encode(++counter), data: resultData, namespace });
143
+ return update(config, {
144
+ items: { $push: [newItem] },
145
+ layout: { $push: [Object.assign(Object.assign({}, saveDefaultLayout), { y: layoutY, i: newItem.id })] },
146
+ counter: { $set: counter },
147
+ });
148
+ }
149
+ static editItem({ item, namespace = DEFAULT_NAMESPACE, config, }) {
150
+ const hashids = new Hashids(config.salt);
151
+ let counter = config.counter;
152
+ const itemIndex = config.items.findIndex(({ id }) => item.id === id);
153
+ const resultData = isItemWithTabs(item)
154
+ ? Object.assign(Object.assign({}, item.data), { tabs: item.data.tabs.map((tab) => tab.id ? tab : Object.assign(Object.assign({}, tab), { id: hashids.encode(++counter) })) }) : item.data;
155
+ return update(config, {
156
+ items: { [itemIndex]: { $set: Object.assign(Object.assign({}, item), { data: resultData, namespace }) } },
157
+ counter: { $set: counter },
158
+ });
159
+ }
160
+ static removeItem({ id, config, itemsStateAndParams = {} }) {
161
+ if (getCurrentVersion(itemsStateAndParams) === 1) {
162
+ return removeItemVersion1({ id, config, itemsStateAndParams });
163
+ }
164
+ const itemIndex = config.items.findIndex((item) => item.id === id);
165
+ const item = config.items[itemIndex];
166
+ const itemIds = isItemWithTabs(item)
167
+ ? [id].concat(item.data.tabs.map((tab) => tab.id))
168
+ : [id];
169
+ const connections = config.connections.filter(({ from, to }) => !itemIds.includes(from) && !itemIds.includes(to));
170
+ return {
171
+ config: update(config, {
172
+ items: {
173
+ $splice: [[itemIndex, 1]],
174
+ },
175
+ layout: {
176
+ $splice: [[itemIndex, 1]],
177
+ },
178
+ connections: {
179
+ $set: connections,
180
+ },
181
+ }),
182
+ itemsStateAndParams: update(itemsStateAndParams, {
183
+ $unset: [id],
184
+ [META_KEY]: {
185
+ $set: deleteFromQueue({
186
+ id,
187
+ itemsStateAndParams,
188
+ config,
189
+ }),
190
+ },
191
+ }),
192
+ };
193
+ }
194
+ static updateLayout({ layout, config }) {
195
+ return update(config, {
196
+ layout: {
197
+ $set: layout.map(({ x, y, w, h, i }) => ({ x, y, w, h, i })),
198
+ },
199
+ });
200
+ }
201
+ static changeStateAndParams({ id: initiatorId, config, stateAndParams, itemsStateAndParams, }) {
202
+ var _a, _b;
203
+ if (getCurrentVersion(itemsStateAndParams) === 1) {
204
+ return changeStateAndParamsVersion1({
205
+ id: initiatorId,
206
+ config,
207
+ stateAndParams,
208
+ itemsStateAndParams,
209
+ });
210
+ }
211
+ const hasState = 'state' in stateAndParams;
212
+ const { items } = config;
213
+ const itemsIds = items.map(({ id: itemId }) => itemId);
214
+ const itemsStateAndParamsIds = Object.keys(omit(itemsStateAndParams, [META_KEY]));
215
+ const unusedIds = itemsStateAndParamsIds.filter((id) => !itemsIds.includes(id));
216
+ const initiatorItem = items.find(({ id }) => id === initiatorId);
217
+ const newTabId = (_a = stateAndParams.state) === null || _a === void 0 ? void 0 : _a.tabId;
218
+ const isTabSwitched = isItemWithTabs(initiatorItem) && Boolean(newTabId);
219
+ const currentMeta = getItemsStateAndParamsMeta(itemsStateAndParams);
220
+ if ('params' in stateAndParams) {
221
+ const allowableParams = getAllowableChangedParams(initiatorItem, stateAndParams, itemsStateAndParams);
222
+ const tabId = isItemWithTabs(initiatorItem)
223
+ ? newTabId || resolveItemInnerId({ item: initiatorItem, itemsStateAndParams })
224
+ : undefined;
225
+ const meta = addToQueue({ id: initiatorId, tabId, config, itemsStateAndParams });
226
+ let commandUpdateParams = ((_b = itemsStateAndParams[initiatorId]) === null || _b === void 0 ? void 0 : _b.params)
227
+ ? '$merge'
228
+ : '$set';
229
+ if (isTabSwitched) {
230
+ commandUpdateParams = '$set';
231
+ }
232
+ return update(itemsStateAndParams, {
233
+ $unset: unusedIds,
234
+ [initiatorId]: {
235
+ $auto: Object.assign({ params: {
236
+ [commandUpdateParams]: allowableParams,
237
+ } }, (hasState ? { state: { $set: stateAndParams.state } } : {})),
238
+ },
239
+ [META_KEY]: { $set: meta },
240
+ });
241
+ }
242
+ else if (hasState) {
243
+ let metaSpec = {};
244
+ if (currentMeta && isTabSwitched) {
245
+ metaSpec = {
246
+ [META_KEY]: {
247
+ $set: deleteFromQueue({
248
+ id: initiatorId,
249
+ itemsStateAndParams,
250
+ config,
251
+ }),
252
+ },
253
+ };
254
+ }
255
+ return update(itemsStateAndParams, Object.assign({ $unset: unusedIds, [initiatorId]: {
256
+ $auto: Object.assign({ state: { $set: stateAndParams.state } }, (isTabSwitched ? { $unset: ['params'] } : {})),
257
+ } }, (currentMeta
258
+ ? metaSpec
259
+ : { [META_KEY]: { $set: getInitialItemsStateAndParamsMeta() } })));
260
+ }
261
+ else {
262
+ return update(itemsStateAndParams, Object.assign({ $unset: unusedIds }, (currentMeta
263
+ ? {}
264
+ : {
265
+ [META_KEY]: { $set: getInitialItemsStateAndParamsMeta() },
266
+ })));
267
+ }
268
+ }
269
+ }
package/package.json ADDED
@@ -0,0 +1,107 @@
1
+ {
2
+ "name": "@gravity-ui/dashkit",
3
+ "version": "3.0.1",
4
+ "description": "Library for rendering dashboard grid layout",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/gravity-ui/dashkit"
9
+ },
10
+ "files": [
11
+ "build"
12
+ ],
13
+ "sideEffects": [
14
+ "*.scss",
15
+ "*.css"
16
+ ],
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "main": "build/cjs/index.js",
21
+ "module": "build/esm/index.js",
22
+ "types": "build/esm/index.d.ts",
23
+ "scripts": {
24
+ "lint:prettier": "prettier --check --loglevel=error '**/*.{js,jsx,ts,tsx,scss}'",
25
+ "lint:js": "eslint src --ext .js,.jsx,.ts,.tsx",
26
+ "lint:styles": "stylelint 'src/**/*.scss'",
27
+ "lint": "run-p lint:*",
28
+ "test": "jest",
29
+ "clean": "gulp clean",
30
+ "build": "gulp",
31
+ "start": "start-storybook -p 7120",
32
+ "typecheck": "tsc --noEmit",
33
+ "build-storybook": "build-storybook -c .storybook -o storybook-static",
34
+ "prepare": "husky install",
35
+ "prepublishOnly": "npm run lint && npm run test && npm run build"
36
+ },
37
+ "dependencies": {
38
+ "hashids": "^2.2.8",
39
+ "immutability-helper": "^3.1.1",
40
+ "react-grid-layout": "^0.18.3"
41
+ },
42
+ "peerDependencies": {
43
+ "@gravity-ui/uikit": "^3.0.0",
44
+ "@gravity-ui/i18n": "^1.0.0",
45
+ "bem-cn-lite": "^4.0.0",
46
+ "react": "^16.0.0 || ^17.0.0"
47
+ },
48
+ "devDependencies": {
49
+ "@commitlint/cli": "^17.0.3",
50
+ "@commitlint/config-conventional": "^17.0.3",
51
+ "@storybook/addon-essentials": "^6.5.9",
52
+ "@storybook/preset-scss": "^1.0.3",
53
+ "@storybook/react": "^6.5.9",
54
+ "@types/enzyme": "^3.10.8",
55
+ "@types/jest": "^26.0.20",
56
+ "@types/lodash": "^4.14.170",
57
+ "@types/react": "^17.0.47",
58
+ "@gravity-ui/eslint-config": "^1.0.2",
59
+ "@gravity-ui/i18n": "^1.0.0",
60
+ "@gravity-ui/prettier-config": "^1.0.1",
61
+ "@gravity-ui/stylelint-config": "^1.0.1",
62
+ "@gravity-ui/tsconfig": "^1.0.0",
63
+ "@gravity-ui/uikit": "^3.0.0",
64
+ "babel-jest": "^26.6.3",
65
+ "bem-cn-lite": "^4.0.0",
66
+ "copyfiles": "^2.4.1",
67
+ "enzyme": "^3.11.0",
68
+ "enzyme-adapter-react-16": "^1.15.6",
69
+ "enzyme-to-json": "^3.6.1",
70
+ "eslint": "^8.11.0",
71
+ "gulp": "^4.0.2",
72
+ "gulp-cli": "^2.3.0",
73
+ "gulp-dart-sass": "^1.0.2",
74
+ "gulp-replace": "^1.1.3",
75
+ "gulp-typescript": "^6.0.0-alpha.1",
76
+ "husky": "^8.0.1",
77
+ "jest": "^26.6.3",
78
+ "lint-staged": "^13.0.3",
79
+ "npm-run-all": "^4.1.5",
80
+ "postcss": "^8.4.12",
81
+ "prettier": "^2.6.0",
82
+ "react": "^17.0.2",
83
+ "react-docgen-typescript": "^2.2.2",
84
+ "react-dom": "^17.0.2",
85
+ "rimraf": "^3.0.2",
86
+ "sass": "^1.53.0",
87
+ "sass-loader": "^10.3.1",
88
+ "stylelint": "^14.6.0",
89
+ "ts-jest": "^26.5.3",
90
+ "typescript": "^4.7.4"
91
+ },
92
+ "husky": {
93
+ "hooks": {
94
+ "pre-commit": "lint-staged"
95
+ }
96
+ },
97
+ "lint-staged": {
98
+ "*.{css,scss}": [
99
+ "stylelint --fix --quiet",
100
+ "prettier --write"
101
+ ],
102
+ "*.{js,jsx,ts,tsx}": [
103
+ "eslint --fix --quiet",
104
+ "prettier --write"
105
+ ]
106
+ }
107
+ }