@next-libs/cmdb-instances 6.5.1 → 6.6.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.
@@ -1,351 +1,309 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
1
2
  import _extends from "@babel/runtime/helpers/extends";
2
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
3
  import React from "react";
4
- import { DatePicker, Input, InputNumber, Modal, Radio, Select } from "antd";
4
+ import { DatePicker, Input, InputNumber, Modal, Radio, Select, Form } from "antd";
5
+ import { StructDefineType } from "./interfaces";
5
6
  import { boolOptions } from "../model-attribute-form-control/ModelAttributeFormControl";
6
- import styles from "./index.module.css";
7
7
  import i18n from "i18next";
8
8
  import { K, NS_LIBS_CMDB_INSTANCES } from "../i18n/constants";
9
- import { computeDateFormat } from "../processors";
10
- import _ from "lodash";
9
+ import moment from "moment";
10
+ import { keyBy, reduce, isString, isNil } from "lodash";
11
11
  import { CodeEditor } from "@next-libs/code-editor-components";
12
- export class AddStructModal extends React.Component {
13
- constructor(props) {
14
- super(props);
15
-
16
- _defineProperty(this, "hasRegex", regex => !_.isNil(regex) && regex !== "");
17
-
18
- _defineProperty(this, "hasValueWithRegex", (value, regex) => !_.isNil(value) && value !== "" && this.hasRegex(regex));
19
-
20
- _defineProperty(this, "handleValueChange", (e, define) => {
21
- var structData = this.state.structData;
22
- structData[define.id] = e;
23
- this.setState({
24
- structData
25
- });
26
- });
27
-
28
- _defineProperty(this, "handleInputValueChange", (e, define) => {
29
- this.handleValueChange(e.target.value, define);
30
- });
31
-
32
- _defineProperty(this, "validateRegex", (value, define, index) => {
33
- var {
34
- showError
35
- } = this.state;
36
- var regex = new RegExp(define.regex);
37
- var matched;
38
-
39
- if (define.type === "arr") {
40
- matched = this.hasRegex(define.regex) ? value.every(item => regex.test(item)) : true;
41
- } else {
42
- matched = this.hasValueWithRegex(value, define.regex) ? regex.test(value) : true;
43
- }
44
-
45
- showError[index] = !matched;
46
- this.setState({
47
- showError
48
- });
49
- this.handleValueChange(value, define);
50
- });
51
-
52
- _defineProperty(this, "handleIpValueChange", (e, define, index) => {
53
- var value = e.target.value;
54
- var {
55
- showError
56
- } = this.state;
57
- var matched = value === "" || /^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\[^\\],;\s]{1,100}\]|)$/.test(value);
58
- showError[index] = !matched;
59
- this.setState({
60
- showError
61
- });
62
- this.handleValueChange(value, define);
63
- });
64
-
65
- _defineProperty(this, "getEnumForm", (define, value) => {
66
- var enumForm;
67
- var {
68
- regex
69
- } = define;
70
-
71
- if (regex.length < 6) {
72
- var RadioGroup = Radio.Group;
73
- enumForm = /*#__PURE__*/React.createElement(RadioGroup, {
74
- onChange: e => this.handleInputValueChange(e, define),
75
- defaultValue: value
76
- }, regex.map(item => /*#__PURE__*/React.createElement(Radio, {
77
- value: item,
78
- key: item
79
- }, item)));
80
- } else {
81
- var Option = Select.Option;
82
- enumForm = /*#__PURE__*/React.createElement(Select, _extends({}, value !== null ? {
83
- defaultValue: value
84
- } : {}, {
85
- style: {
86
- width: "100%"
87
- },
88
- onChange: e => this.handleValueChange(e, define)
89
- }), regex.map(item => /*#__PURE__*/React.createElement(Option, {
90
- value: item,
91
- key: item
92
- }, item)));
93
- }
94
-
95
- return enumForm;
96
- });
97
-
98
- _defineProperty(this, "validateJson", (err, index) => {
99
- var {
100
- showError
101
- } = this.state;
102
- var error = err.some(v => ["error", "warning"].includes(v.type));
103
- showError[index] = error;
104
- this.setState({
105
- showError
106
- });
107
- });
108
-
109
- _defineProperty(this, "getFormType", (define, value, index) => {
110
- var formType;
111
- var defaultValue = value ? value[define.id] : null; //如果是json类型,数据有可能是字符串、数组或对象等,需要对数据处理
112
-
113
- if (define.type === "json") {
114
- defaultValue = _.isString(defaultValue) || !defaultValue ? defaultValue || "" : JSON.stringify(defaultValue, null, 2);
115
- }
12
+ export function AddStructModal(props) {
13
+ var {
14
+ attribute,
15
+ visible,
16
+ structData,
17
+ handleStoreFunction,
18
+ handleCancelFunction
19
+ } = props;
20
+ var structDefine = attribute.value.struct_define;
21
+ var structDefineMap = keyBy(structDefine, "id");
22
+ var [form] = Form.useForm();
23
+ var structValue = reduce(structData, (pre, value, key) => {
24
+ var _structDefineMap$key;
25
+
26
+ var calcValue;
27
+
28
+ switch ((_structDefineMap$key = structDefineMap[key]) === null || _structDefineMap$key === void 0 ? void 0 : _structDefineMap$key.type) {
29
+ case StructDefineType.DATE:
30
+ case StructDefineType.DATETIME:
31
+ calcValue = value && moment(value);
32
+ break;
33
+
34
+ case StructDefineType.JSON:
35
+ calcValue = isString(value) || !value ? value || "" : JSON.stringify(value, null, 2);
36
+ break;
37
+
38
+ default:
39
+ calcValue = value;
40
+ }
116
41
 
117
- switch (define.type) {
118
- case "int":
119
- {
120
- formType = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(InputNumber, {
121
- defaultValue: defaultValue,
42
+ pre[key] = calcValue;
43
+ return pre;
44
+ }, {});
45
+
46
+ var hasRegex = regex => !isNil(regex) && regex !== "";
47
+
48
+ var hasValue = value => !isNil(value) && value !== "";
49
+
50
+ var handleJsonValidate = (err, define) => {
51
+ var errors = [];
52
+ err.some(v => v.type === "error") && errors.push(i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_JSON)));
53
+ err.some(v => v.type === "warning") && errors.push(i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX)));
54
+ form.setFields([{
55
+ name: define.id,
56
+ errors
57
+ }]);
58
+ };
59
+
60
+ var getFormType = define => {
61
+ switch (define.type) {
62
+ case StructDefineType.INTEGER:
63
+ {
64
+ return /*#__PURE__*/React.createElement(InputNumber, {
65
+ style: {
66
+ width: "100%"
67
+ },
68
+ precision: 0,
69
+ placeholder: hasRegex(define.regex) ? i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.MATCHING_REGULAR), {
70
+ regexp: define.regex
71
+ }) : ""
72
+ });
73
+ }
74
+
75
+ case StructDefineType.FLOAT:
76
+ {
77
+ return /*#__PURE__*/React.createElement(InputNumber, {
78
+ style: {
79
+ width: "100%"
80
+ }
81
+ });
82
+ }
83
+
84
+ case StructDefineType.BOOLEAN:
85
+ {
86
+ return /*#__PURE__*/React.createElement(Radio.Group, null, boolOptions.map(item => /*#__PURE__*/React.createElement(Radio, {
87
+ value: item.id,
88
+ key: item.text
89
+ }, item.text)));
90
+ }
91
+
92
+ case StructDefineType.ARR:
93
+ {
94
+ return /*#__PURE__*/React.createElement(Select, {
95
+ mode: "tags",
96
+ style: {
97
+ width: "100%"
98
+ },
99
+ placeholder: hasRegex(define.regex) ? i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.MATCHING_REGULAR), {
100
+ regexp: define.regex
101
+ }) : ""
102
+ });
103
+ }
104
+
105
+ case StructDefineType.ENUM:
106
+ {
107
+ var {
108
+ regex
109
+ } = define;
110
+
111
+ if (regex.length < 6) {
112
+ return /*#__PURE__*/React.createElement(Radio.Group, null, regex.map(item => /*#__PURE__*/React.createElement(Radio, {
113
+ value: item,
114
+ key: item
115
+ }, item)));
116
+ } else {
117
+ return /*#__PURE__*/React.createElement(Select, {
122
118
  style: {
123
119
  width: "100%"
124
- },
125
- precision: 0,
126
- onChange: e => this.validateRegex(e, define, index),
127
- placeholder: this.hasRegex(define.regex) && i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.MATCHING_REGULAR), {
128
- regexp: define.regex
129
- })
130
- }), /*#__PURE__*/React.createElement("label", {
131
- style: {
132
- display: this.state.showError[index] ? "block" : "none",
133
- color: "#fc5043"
134
120
  }
135
- }, i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX_DETAIL), {
136
- regex: define.regex
137
- })));
138
- break;
139
- }
140
-
141
- case "enum":
142
- {
143
- formType = this.getEnumForm(define, defaultValue);
144
- break;
145
- }
146
-
147
- case "enums":
148
- {
149
- formType = /*#__PURE__*/React.createElement(Select, _extends({
150
- mode: "multiple",
151
- style: {
152
- width: "100%"
153
- },
154
- onChange: e => this.handleValueChange(e, define)
155
- }, defaultValue !== null ? {
156
- defaultValue: defaultValue
157
- } : {}), define.regex.map(item => /*#__PURE__*/React.createElement(Select.Option, {
121
+ }, regex.map(item => /*#__PURE__*/React.createElement(Select.Option, {
158
122
  value: item,
159
123
  key: item
160
124
  }, item)));
161
- break;
162
125
  }
163
-
164
- case "bool":
165
- {
166
- formType = /*#__PURE__*/React.createElement(Radio.Group, {
167
- onChange: e => this.handleInputValueChange(e, define),
168
- defaultValue: defaultValue
169
- }, boolOptions.map(item => /*#__PURE__*/React.createElement(Radio, {
170
- value: item.id,
171
- key: item.text
172
- }, item.text)));
173
- break;
126
+ }
127
+
128
+ case StructDefineType.ENUMS:
129
+ {
130
+ return /*#__PURE__*/React.createElement(Select, {
131
+ mode: "multiple",
132
+ style: {
133
+ width: "100%"
134
+ }
135
+ }, define.regex.map(item => /*#__PURE__*/React.createElement(Select.Option, {
136
+ value: item,
137
+ key: item
138
+ }, item)));
139
+ }
140
+
141
+ case StructDefineType.DATE:
142
+ case StructDefineType.DATETIME:
143
+ {
144
+ return /*#__PURE__*/React.createElement(DatePicker, {
145
+ showTime: define.type === "datetime"
146
+ });
147
+ }
148
+
149
+ case StructDefineType.IP:
150
+ {
151
+ return /*#__PURE__*/React.createElement(Input, null);
152
+ }
153
+
154
+ case StructDefineType.JSON:
155
+ {
156
+ var jsonSchema;
157
+
158
+ if (hasRegex(define.regex)) {
159
+ jsonSchema = JSON.parse(define.regex);
174
160
  }
175
161
 
176
- case "arr":
177
- {
178
- formType = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Select, _extends({
179
- mode: "tags",
180
- style: {
181
- width: "100%"
182
- }
183
- }, defaultValue !== null ? {
184
- defaultValue: defaultValue
185
- } : {}, {
186
- onChange: e => this.validateRegex(e, define, index),
187
- placeholder: this.hasRegex(define.regex) && i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.MATCHING_REGULAR), {
188
- regexp: define.regex
189
- })
190
- })), /*#__PURE__*/React.createElement("label", {
191
- style: {
192
- display: this.state.showError[index] ? "block" : "none",
193
- color: "#fc5043"
194
- }
195
- }, i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX_DETAIL), {
162
+ return /*#__PURE__*/React.createElement(CodeEditor, _extends({
163
+ mode: "json",
164
+ maxLines: "Infinity",
165
+ highlightActiveLine: true,
166
+ minLines: 3,
167
+ showLineNumbers: true,
168
+ showPrintMargin: false,
169
+ validateJsonSchemaMode: "error",
170
+ onValidate: err => handleJsonValidate(err, define)
171
+ }, jsonSchema ? {
172
+ jsonSchema
173
+ } : {}, {
174
+ theme: "tomorrow"
175
+ }));
176
+ }
177
+
178
+ case StructDefineType.STRING:
179
+ default:
180
+ {
181
+ return /*#__PURE__*/React.createElement(Input, {
182
+ placeholder: hasRegex(define.regex) ? i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.MATCHING_REGULAR), {
183
+ regexp: define.regex
184
+ }) : ""
185
+ });
186
+ }
187
+ }
188
+ };
189
+
190
+ var getFormRules = define => {
191
+ var rules = [];
192
+
193
+ switch (define.type) {
194
+ case StructDefineType.STRING:
195
+ case StructDefineType.IP:
196
+ {
197
+ hasRegex(define.regex) && rules.push({
198
+ pattern: define.regex,
199
+ message: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(define.type === "ip" ? K.NOT_MEET_REGEX : K.NOT_MEET_REGEX_DETAIL), {
196
200
  regex: define.regex
197
- })));
198
- break;
199
- }
200
-
201
- case "date":
202
- case "datetime":
203
- {
204
- var formater = computeDateFormat(define.type, defaultValue);
205
- formType = /*#__PURE__*/React.createElement(DatePicker, {
206
- defaultValue: formater.value,
207
- showTime: define.type === "datetime",
208
- onChange: e => this.handleValueChange(e.format(define.type === "datetime" ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD"), define),
209
- format: formater.format
210
- });
211
- break;
212
- }
213
-
214
- case "ip":
215
- {
216
- formType = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
217
- defaultValue: defaultValue,
218
- onChange: e => this.handleIpValueChange(e, define, index)
219
- }), /*#__PURE__*/React.createElement("label", {
220
- style: {
221
- display: this.state.showError[index] ? "block" : "none",
222
- color: "#fc5043"
223
- }
224
- }, i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX))));
225
- break;
226
- }
227
-
228
- case "float":
229
- {
230
- formType = /*#__PURE__*/React.createElement(InputNumber, {
231
- defaultValue: defaultValue,
232
- style: {
233
- width: "100%"
234
- },
235
- onChange: e => this.handleValueChange(e, define)
236
- });
237
- break;
238
- }
239
-
240
- case "json":
241
- {
242
- var jsonSchema;
201
+ })
202
+ });
203
+ break;
204
+ }
205
+
206
+ case StructDefineType.ARR:
207
+ {
208
+ var regex = new RegExp(define.regex);
209
+ hasRegex(define.regex) && rules.push({
210
+ validator: (rule, value, cb) => {
211
+ (value !== null && value !== void 0 ? value : []).every(item => regex.test(item)) ? cb() : cb(rule.message);
212
+ },
213
+ message: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX_DETAIL), {
214
+ regex: define.regex
215
+ })
216
+ });
217
+ break;
218
+ }
219
+
220
+ case StructDefineType.INTEGER:
221
+ {
222
+ var _regex = new RegExp(define.regex);
223
+
224
+ hasRegex(define.regex) && rules.push({
225
+ validator: (rule, value, cb) => {
226
+ !hasValue(value) || _regex.test(value) ? cb() : cb(rule.message);
227
+ },
228
+ message: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX_DETAIL), {
229
+ regex: define.regex
230
+ })
231
+ });
232
+ break;
233
+ }
234
+ }
243
235
 
244
- if (this.hasRegex(define.regex)) {
245
- jsonSchema = JSON.parse(define.regex);
236
+ return rules;
237
+ };
238
+
239
+ var handleStore = /*#__PURE__*/function () {
240
+ var _ref = _asyncToGenerator(function* () {
241
+ // 校验JSON类型
242
+ var hasErrors = !form.getFieldsError().every(v => !v.errors.length);
243
+ if (hasErrors) return;
244
+ var values = yield form.validateFields();
245
+ var newValues = reduce(values, (pre, value, key) => {
246
+ var _structDefineMap$key2;
247
+
248
+ var realValue;
249
+
250
+ switch ((_structDefineMap$key2 = structDefineMap[key]) === null || _structDefineMap$key2 === void 0 ? void 0 : _structDefineMap$key2.type) {
251
+ case StructDefineType.DATE:
252
+ {
253
+ realValue = value === null || value === void 0 ? void 0 : value.format("YYYY-MM-DD");
254
+ break;
246
255
  }
247
256
 
248
- formType = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(CodeEditor, _extends({
249
- value: defaultValue,
250
- mode: "json",
251
- maxLines: "Infinity",
252
- highlightActiveLine: true,
253
- onChange: e => this.handleValueChange(e, define),
254
- minLines: 3,
255
- showLineNumbers: true,
256
- showPrintMargin: false,
257
- onValidate: err => this.validateJson(err, index)
258
- }, jsonSchema ? {
259
- jsonSchema: jsonSchema
260
- } : {}, {
261
- theme: "tomorrow"
262
- })), /*#__PURE__*/React.createElement("label", {
263
- style: {
264
- display: this.state.showError[index] ? "block" : "none",
265
- color: "#fc5043"
266
- }
267
- }, i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_JSON))));
268
- break;
269
- }
270
-
271
- default:
272
- {
273
- formType = /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
274
- defaultValue: defaultValue,
275
- placeholder: this.hasRegex(define.regex) && i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.MATCHING_REGULAR), {
276
- regexp: define.regex
277
- }),
278
- onChange: e => this.validateRegex(e.target.value, define, index)
279
- }), /*#__PURE__*/React.createElement("label", {
280
- style: {
281
- display: this.state.showError[index] ? "block" : "none",
282
- color: "#fc5043"
283
- }
284
- }, i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.NOT_MEET_REGEX_DETAIL), {
285
- regex: define.regex
286
- })));
287
- }
288
- }
289
-
290
- return formType;
291
- });
257
+ case StructDefineType.DATETIME:
258
+ {
259
+ realValue = value === null || value === void 0 ? void 0 : value.format("YYYY-MM-DD HH:mm:ss");
260
+ break;
261
+ }
292
262
 
293
- _defineProperty(this, "handleStore", () => {
294
- this.props.handleStoreFunction(this.state.structData);
295
- });
263
+ default:
264
+ realValue = value;
265
+ }
296
266
 
297
- _defineProperty(this, "handleCloseModal", () => {
298
- this.props.handleCancelFunction();
267
+ pre[key] = realValue;
268
+ return pre;
269
+ }, {});
270
+ handleStoreFunction(newValues);
271
+ form.resetFields();
299
272
  });
300
273
 
301
- var {
302
- attribute
303
- } = props;
304
- this.state = {
305
- showError: new Array(attribute.value.struct_define.length).fill(false)
274
+ return function handleStore() {
275
+ return _ref.apply(this, arguments);
306
276
  };
307
- }
308
-
309
- componentDidUpdate(prevProps) {
310
- if (prevProps.visible !== this.props.visible) {
311
- if (this.props.visible) {
312
- this.setState({
313
- structData: this.props.structData || {},
314
- showError: new Array(this.props.attribute.value.struct_define.length).fill(false)
315
- });
316
- }
317
- }
318
- }
319
-
320
- render() {
321
- var {
322
- attribute,
323
- visible,
324
- structData
325
- } = this.props;
326
- var structDefine = attribute.value.struct_define;
327
- return /*#__PURE__*/React.createElement(Modal, {
328
- visible: visible,
329
- onOk: this.handleStore,
330
- onCancel: this.handleCloseModal,
331
- okButtonProps: {
332
- disabled: this.state.showError.includes(true),
333
- id: "okBtn"
334
- },
335
- okText: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.CONFIRM)),
336
- cancelText: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.CANCEL)),
337
- cancelButtonProps: {
338
- id: "cancelBtn"
339
- },
340
- destroyOnClose: true
341
- }, structDefine.map((define, index) => {
342
- return /*#__PURE__*/React.createElement("div", {
343
- className: styles.formItem,
344
- key: define.id
345
- }, /*#__PURE__*/React.createElement("label", {
346
- className: styles.formLabel
347
- }, define.name, "\uFF1A"), this.getFormType(define, structData, index));
348
- }));
349
- }
350
-
277
+ }();
278
+
279
+ var handleCloseModal = () => {
280
+ handleCancelFunction();
281
+ form.resetFields();
282
+ };
283
+
284
+ return /*#__PURE__*/React.createElement(Modal, {
285
+ visible: visible,
286
+ onOk: handleStore,
287
+ onCancel: handleCloseModal,
288
+ okButtonProps: {
289
+ id: "okBtn"
290
+ },
291
+ okText: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.CONFIRM)),
292
+ cancelText: i18n.t("".concat(NS_LIBS_CMDB_INSTANCES, ":").concat(K.CANCEL)),
293
+ cancelButtonProps: {
294
+ id: "cancelBtn"
295
+ },
296
+ destroyOnClose: true
297
+ }, /*#__PURE__*/React.createElement(Form, {
298
+ form: form,
299
+ layout: "vertical",
300
+ initialValues: structValue
301
+ }, structDefine.map(define => {
302
+ return /*#__PURE__*/React.createElement(Form.Item, {
303
+ key: define.id,
304
+ name: define.id,
305
+ label: define.name + ":",
306
+ rules: getFormRules(define)
307
+ }, getFormType(define));
308
+ })));
351
309
  }
@@ -1 +1,15 @@
1
- export {};
1
+ export var StructDefineType;
2
+
3
+ (function (StructDefineType) {
4
+ StructDefineType["STRING"] = "str";
5
+ StructDefineType["INTEGER"] = "int";
6
+ StructDefineType["ENUM"] = "enum";
7
+ StructDefineType["ENUMS"] = "enums";
8
+ StructDefineType["ARR"] = "arr";
9
+ StructDefineType["DATE"] = "date";
10
+ StructDefineType["DATETIME"] = "datetime";
11
+ StructDefineType["IP"] = "ip";
12
+ StructDefineType["FLOAT"] = "float";
13
+ StructDefineType["BOOLEAN"] = "bool";
14
+ StructDefineType["JSON"] = "json";
15
+ })(StructDefineType || (StructDefineType = {}));