@vue-start/pro 0.1.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.
- package/CHANGELOG.md +19 -0
- package/README.md +11 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.es.js +728 -0
- package/dist/index.js +748 -0
- package/package.json +28 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var vue = require('vue');
|
|
6
|
+
var lodash = require('lodash');
|
|
7
|
+
var rxjs = require('rxjs');
|
|
8
|
+
var hooks = require('@vue-start/hooks');
|
|
9
|
+
var request = require('@vue-start/request');
|
|
10
|
+
|
|
11
|
+
const createUseRequestActor = filterFun => (actors, callback) => {
|
|
12
|
+
const {
|
|
13
|
+
requestSubject$
|
|
14
|
+
} = request.useRequestProvide();
|
|
15
|
+
const nameSet = new Set(lodash.map(actors, actor => lodash.isString(actor) ? actor : actor.name));
|
|
16
|
+
hooks.useEffect(() => {
|
|
17
|
+
const sub = requestSubject$.pipe(rxjs.filter(filterFun), rxjs.tap(actor => {
|
|
18
|
+
if (nameSet.has(actor.name)) {
|
|
19
|
+
callback(actor);
|
|
20
|
+
}
|
|
21
|
+
})).subscribe();
|
|
22
|
+
return () => {
|
|
23
|
+
sub.unsubscribe();
|
|
24
|
+
};
|
|
25
|
+
}, []);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const useDoneRequestActor = createUseRequestActor(request.isDoneRequestActor);
|
|
29
|
+
const useFailedRequestActor = createUseRequestActor(request.isFailedRequestActor);
|
|
30
|
+
const useComposeRequestActor = (actors, options, cancelWhileUnmount) => {
|
|
31
|
+
const {
|
|
32
|
+
requestSubject$,
|
|
33
|
+
dispatchRequest
|
|
34
|
+
} = request.useRequestProvide();
|
|
35
|
+
const nameSet = new Set(lodash.map(actors, actor => lodash.isString(actor) ? actor : actor.name));
|
|
36
|
+
const lastRequestActors = {};
|
|
37
|
+
hooks.useEffect(() => {
|
|
38
|
+
const sub = rxjs.merge(requestSubject$.pipe(rxjs.filter(request.isPreRequestActor), rxjs.tap(actor => {
|
|
39
|
+
if (nameSet.has(actor.name)) {
|
|
40
|
+
var _options$onStart;
|
|
41
|
+
|
|
42
|
+
(_options$onStart = options.onStart) === null || _options$onStart === void 0 ? void 0 : _options$onStart.call(options, actor);
|
|
43
|
+
lastRequestActors[actor.name] = actor;
|
|
44
|
+
}
|
|
45
|
+
})), requestSubject$.pipe(rxjs.filter(request.isDoneRequestActor), rxjs.tap(actor => {
|
|
46
|
+
if (nameSet.has(actor.name)) {
|
|
47
|
+
var _options$onSuccess, _options$onFinish;
|
|
48
|
+
|
|
49
|
+
(_options$onSuccess = options.onSuccess) === null || _options$onSuccess === void 0 ? void 0 : _options$onSuccess.call(options, actor);
|
|
50
|
+
(_options$onFinish = options.onFinish) === null || _options$onFinish === void 0 ? void 0 : _options$onFinish.call(options, actor);
|
|
51
|
+
lastRequestActors[actor.name] = undefined;
|
|
52
|
+
}
|
|
53
|
+
})), requestSubject$.pipe(rxjs.filter(request.isFailedRequestActor), rxjs.tap(actor => {
|
|
54
|
+
if (nameSet.has(actor.name)) {
|
|
55
|
+
var _options$onFailed, _options$onFinish2;
|
|
56
|
+
|
|
57
|
+
(_options$onFailed = options.onFailed) === null || _options$onFailed === void 0 ? void 0 : _options$onFailed.call(options, actor);
|
|
58
|
+
(_options$onFinish2 = options.onFinish) === null || _options$onFinish2 === void 0 ? void 0 : _options$onFinish2.call(options, actor);
|
|
59
|
+
lastRequestActors[actor.name] = undefined;
|
|
60
|
+
}
|
|
61
|
+
}))).subscribe();
|
|
62
|
+
return () => {
|
|
63
|
+
sub.unsubscribe();
|
|
64
|
+
|
|
65
|
+
if (cancelWhileUnmount) {
|
|
66
|
+
//组件销毁的时候cancel请求
|
|
67
|
+
lodash.forEach(lastRequestActors, actor => {
|
|
68
|
+
actor && dispatchRequest({ ...actor,
|
|
69
|
+
stage: "CANCEL"
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}, []);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 获取Column的valueType,默认"text"
|
|
79
|
+
* @param column
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
const getColumnValueType = column => {
|
|
83
|
+
return column.formValueType || column.valueType || "text";
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
*获取Column的FormItem name
|
|
87
|
+
* @param column
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
const getColumnFormItemName = column => {
|
|
91
|
+
var _column$formItemProps;
|
|
92
|
+
|
|
93
|
+
return ((_column$formItemProps = column.formItemProps) === null || _column$formItemProps === void 0 ? void 0 : _column$formItemProps.name) || column.dataIndex;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* 根据Column生成FormItem VNode
|
|
97
|
+
* formFieldProps中的slots参数会以v-slots的形式传递到FormItem的录入组件(子组件)中
|
|
98
|
+
* @param formElementMap
|
|
99
|
+
* @param column
|
|
100
|
+
* @param needRules
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
const getFormItemEl = (formElementMap, column, needRules = true) => {
|
|
104
|
+
var _column$formFieldProp;
|
|
105
|
+
|
|
106
|
+
const valueType = getColumnValueType(column);
|
|
107
|
+
const Comp = lodash.get(formElementMap, valueType);
|
|
108
|
+
|
|
109
|
+
if (!Comp) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const name = getColumnFormItemName(column);
|
|
114
|
+
const itemProps = needRules ? column.formItemProps : lodash.omit(column.formItemProps, "rules");
|
|
115
|
+
return vue.h(Comp, {
|
|
116
|
+
key: name,
|
|
117
|
+
name,
|
|
118
|
+
label: column.title,
|
|
119
|
+
...itemProps,
|
|
120
|
+
fieldProps: lodash.omit(column.formFieldProps, "slots"),
|
|
121
|
+
showProps: column.showProps
|
|
122
|
+
}, (_column$formFieldProp = column.formFieldProps) === null || _column$formFieldProp === void 0 ? void 0 : _column$formFieldProp.slots);
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* 根据Column生成Item VNode
|
|
126
|
+
* @param elementMap
|
|
127
|
+
* @param column
|
|
128
|
+
* @param value
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
const getItemEl = (elementMap, column, value) => {
|
|
132
|
+
var _column$formFieldProp2;
|
|
133
|
+
|
|
134
|
+
const valueType = column.valueType || "text";
|
|
135
|
+
const Comp = lodash.get(elementMap, valueType);
|
|
136
|
+
|
|
137
|
+
if (!Comp) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return vue.h(Comp, { ...lodash.omit(column.formFieldProps, "slots"),
|
|
142
|
+
showProps: column.showProps,
|
|
143
|
+
value
|
|
144
|
+
}, (_column$formFieldProp2 = column.formFieldProps) === null || _column$formFieldProp2 === void 0 ? void 0 : _column$formFieldProp2.slots);
|
|
145
|
+
};
|
|
146
|
+
const ProModuleKey = Symbol("pro-module");
|
|
147
|
+
const useProModule = () => vue.inject(ProModuleKey);
|
|
148
|
+
const provideProModule = ctx => {
|
|
149
|
+
vue.provide(ProModuleKey, ctx);
|
|
150
|
+
};
|
|
151
|
+
const RequestAction = {
|
|
152
|
+
Success: "request-success$",
|
|
153
|
+
Fail: "request-fail$"
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const proModuleProps = () => ({
|
|
157
|
+
/**
|
|
158
|
+
* module状态
|
|
159
|
+
*/
|
|
160
|
+
state: {
|
|
161
|
+
type: Object
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 配置(静态)
|
|
166
|
+
*/
|
|
167
|
+
columns: {
|
|
168
|
+
type: Array
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 配置(动态)
|
|
173
|
+
* columns动态属性兼容
|
|
174
|
+
*/
|
|
175
|
+
columnState: {
|
|
176
|
+
type: Object
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 展示组件集
|
|
181
|
+
*/
|
|
182
|
+
elementMap: {
|
|
183
|
+
type: Object
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 录入组件集
|
|
188
|
+
*/
|
|
189
|
+
formElementMap: {
|
|
190
|
+
type: Object
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* requests
|
|
195
|
+
*/
|
|
196
|
+
requests: {
|
|
197
|
+
type: Array
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const ProModule = vue.defineComponent({
|
|
202
|
+
props: { ...proModuleProps()
|
|
203
|
+
},
|
|
204
|
+
setup: (props, {
|
|
205
|
+
slots
|
|
206
|
+
}) => {
|
|
207
|
+
/**
|
|
208
|
+
* columns columnState 合并
|
|
209
|
+
*/
|
|
210
|
+
const columns = vue.computed(() => {
|
|
211
|
+
return lodash.map(props.columns, item => {
|
|
212
|
+
//如果columnState中有值,merge处理
|
|
213
|
+
const mapData = lodash.get(props.columnState, getColumnFormItemName(item));
|
|
214
|
+
|
|
215
|
+
if (lodash.isObject(mapData) && !lodash.isEmpty(mapData) && !lodash.isArray(mapData) && !lodash.isFunction(mapData)) {
|
|
216
|
+
//合并
|
|
217
|
+
return lodash.mergeWith(item, mapData, (objValue, srcValue) => {
|
|
218
|
+
//如果是数组,替换
|
|
219
|
+
if (lodash.isArray(objValue) || lodash.isArray(srcValue)) {
|
|
220
|
+
return srcValue;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return item;
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
/*********************************** 渲染组件 ***************************************/
|
|
229
|
+
// 获取FormItem VNode
|
|
230
|
+
|
|
231
|
+
const getFormItemVNode = (column, needRules = true) => {
|
|
232
|
+
return getFormItemEl(props.formElementMap, column, needRules);
|
|
233
|
+
}; // 获取Item VNode
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
const getItemVNode = (column, value) => {
|
|
237
|
+
return getItemEl(props.elementMap, column, value);
|
|
238
|
+
};
|
|
239
|
+
/*********************************** 事件处理 ***************************************/
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
const subject$ = new rxjs.Subject(); //发送Module事件
|
|
243
|
+
|
|
244
|
+
const sendEvent = action => {
|
|
245
|
+
subject$.next(action);
|
|
246
|
+
};
|
|
247
|
+
/*********************************** 页面状态 ***************************************/
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
const state = props.state || vue.reactive({});
|
|
251
|
+
|
|
252
|
+
const dispatch = action => {
|
|
253
|
+
//如果要更新的属性值是 object ,执行覆盖操作
|
|
254
|
+
if (lodash.isObject(state[action.type])) {
|
|
255
|
+
hooks.setReactiveValue(state[action.type], action.payload);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
state[action.type] = action.payload;
|
|
260
|
+
};
|
|
261
|
+
/*********************************** request ***************************************/
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
const {
|
|
265
|
+
dispatchRequest
|
|
266
|
+
} = request.useRequestProvide();
|
|
267
|
+
const requestMap = lodash.reduce(props.requests, (pair, item) => ({ ...pair,
|
|
268
|
+
[item.actor.name]: item
|
|
269
|
+
}), {});
|
|
270
|
+
const actionMap = lodash.reduce(props.requests, (pair, item) => ({ ...pair,
|
|
271
|
+
[item.action]: item
|
|
272
|
+
}), {}); //发送请求
|
|
273
|
+
|
|
274
|
+
const sendRequest = (requestNameOrAction, ...params) => {
|
|
275
|
+
const requestOpts = lodash.get(requestMap, requestNameOrAction) || lodash.get(actionMap, requestNameOrAction);
|
|
276
|
+
|
|
277
|
+
if (!requestOpts) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let nextParams;
|
|
282
|
+
|
|
283
|
+
if (requestOpts.convertParams) {
|
|
284
|
+
nextParams = requestOpts.convertParams(...params);
|
|
285
|
+
} else {
|
|
286
|
+
nextParams = lodash.get(params, 0);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
dispatchRequest(requestOpts.actor, nextParams);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
useComposeRequestActor(lodash.keys(requestMap), {
|
|
293
|
+
onStart: actor => {
|
|
294
|
+
//如果设置了loading,将请求状态维护到state中
|
|
295
|
+
const loadingName = lodash.get(requestMap, [actor.name, "loadingName"]);
|
|
296
|
+
|
|
297
|
+
if (loadingName) {
|
|
298
|
+
dispatch({
|
|
299
|
+
type: loadingName,
|
|
300
|
+
payload: true
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
onSuccess: actor => {
|
|
305
|
+
var _requestOpts$onSucces;
|
|
306
|
+
|
|
307
|
+
const requestOpts = lodash.get(requestMap, actor.name); //如果设置了stateName,将结果维护到state中
|
|
308
|
+
|
|
309
|
+
if (requestOpts !== null && requestOpts !== void 0 && requestOpts.stateName) {
|
|
310
|
+
var _actor$res;
|
|
311
|
+
|
|
312
|
+
const data = requestOpts.convertData ? requestOpts.convertData(actor) : (_actor$res = actor.res) === null || _actor$res === void 0 ? void 0 : _actor$res.data;
|
|
313
|
+
dispatch({
|
|
314
|
+
type: requestOpts.stateName,
|
|
315
|
+
payload: data
|
|
316
|
+
});
|
|
317
|
+
} //发送成功事件
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
sendEvent({
|
|
321
|
+
type: RequestAction.Success,
|
|
322
|
+
payload: {
|
|
323
|
+
actor
|
|
324
|
+
}
|
|
325
|
+
}); //回调事件
|
|
326
|
+
|
|
327
|
+
(_requestOpts$onSucces = requestOpts.onSuccess) === null || _requestOpts$onSucces === void 0 ? void 0 : _requestOpts$onSucces.call(requestOpts, actor);
|
|
328
|
+
},
|
|
329
|
+
onFailed: actor => {
|
|
330
|
+
var _requestOpts$onFailed;
|
|
331
|
+
|
|
332
|
+
const requestOpts = lodash.get(requestMap, actor.name); //发送失败事件
|
|
333
|
+
|
|
334
|
+
sendEvent({
|
|
335
|
+
type: RequestAction.Fail,
|
|
336
|
+
payload: {
|
|
337
|
+
actor
|
|
338
|
+
}
|
|
339
|
+
}); //回调事件
|
|
340
|
+
|
|
341
|
+
(_requestOpts$onFailed = requestOpts.onFailed) === null || _requestOpts$onFailed === void 0 ? void 0 : _requestOpts$onFailed.call(requestOpts, actor);
|
|
342
|
+
},
|
|
343
|
+
onFinish: actor => {
|
|
344
|
+
const loadingName = lodash.get(requestMap, [actor.name, "loadingName"]);
|
|
345
|
+
|
|
346
|
+
if (loadingName) {
|
|
347
|
+
dispatch({
|
|
348
|
+
type: loadingName,
|
|
349
|
+
payload: false
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}, true);
|
|
354
|
+
provideProModule({
|
|
355
|
+
columns,
|
|
356
|
+
getFormItemVNode,
|
|
357
|
+
getItemVNode,
|
|
358
|
+
elementMap: props.elementMap,
|
|
359
|
+
formElementMap: props.formElementMap,
|
|
360
|
+
//
|
|
361
|
+
subject$,
|
|
362
|
+
sendEvent,
|
|
363
|
+
//
|
|
364
|
+
state,
|
|
365
|
+
dispatch,
|
|
366
|
+
//
|
|
367
|
+
requests: props.requests,
|
|
368
|
+
sendRequest
|
|
369
|
+
});
|
|
370
|
+
return () => {
|
|
371
|
+
var _slots$default;
|
|
372
|
+
|
|
373
|
+
return (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots);
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
//订阅module事件
|
|
379
|
+
const useModuleEvent = cb => {
|
|
380
|
+
const {
|
|
381
|
+
subject$
|
|
382
|
+
} = useProModule();
|
|
383
|
+
hooks.useEffect(() => {
|
|
384
|
+
const sub = subject$.subscribe({
|
|
385
|
+
next: action => {
|
|
386
|
+
cb(action);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
return () => {
|
|
390
|
+
return sub.unsubscribe();
|
|
391
|
+
};
|
|
392
|
+
}, []);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const ProCurdKey = Symbol("pro-curd");
|
|
396
|
+
const useProCurd = () => vue.inject(ProCurdKey);
|
|
397
|
+
const provideProCurd = ctx => vue.provide(ProCurdKey, ctx);
|
|
398
|
+
/************************************ 常量 *************************************/
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* curd 5种action
|
|
402
|
+
*/
|
|
403
|
+
|
|
404
|
+
exports.CurdAction = void 0;
|
|
405
|
+
|
|
406
|
+
(function (CurdAction) {
|
|
407
|
+
CurdAction["LIST"] = "LIST";
|
|
408
|
+
CurdAction["DETAIL"] = "DETAIL";
|
|
409
|
+
CurdAction["ADD"] = "ADD";
|
|
410
|
+
CurdAction["EDIT"] = "EDIT";
|
|
411
|
+
CurdAction["DELETE"] = "DELETE";
|
|
412
|
+
})(exports.CurdAction || (exports.CurdAction = {}));
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* curd 操作模式
|
|
416
|
+
*/
|
|
417
|
+
exports.CurdCurrentMode = void 0;
|
|
418
|
+
|
|
419
|
+
(function (CurdCurrentMode) {
|
|
420
|
+
CurdCurrentMode["ADD"] = "ADD";
|
|
421
|
+
CurdCurrentMode["EDIT"] = "EDIT";
|
|
422
|
+
CurdCurrentMode["DETAIL"] = "DETAIL";
|
|
423
|
+
})(exports.CurdCurrentMode || (exports.CurdCurrentMode = {}));
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* curd add 模式下 标记 "确定" "确定并继续" 触发
|
|
427
|
+
*/
|
|
428
|
+
exports.CurdAddAction = void 0;
|
|
429
|
+
|
|
430
|
+
(function (CurdAddAction) {
|
|
431
|
+
CurdAddAction["NORMAL"] = "NORMAL";
|
|
432
|
+
CurdAddAction["CONTINUE"] = "CONTINUE";
|
|
433
|
+
})(exports.CurdAddAction || (exports.CurdAddAction = {}));
|
|
434
|
+
|
|
435
|
+
const proCurdProps = () => ({
|
|
436
|
+
/**
|
|
437
|
+
* 列表 或 详情 的唯一标识
|
|
438
|
+
*/
|
|
439
|
+
rowKey: {
|
|
440
|
+
type: String,
|
|
441
|
+
default: "id"
|
|
442
|
+
},
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* operates
|
|
446
|
+
*/
|
|
447
|
+
operates: {
|
|
448
|
+
type: Array
|
|
449
|
+
},
|
|
450
|
+
|
|
451
|
+
/************************* 子组件props *******************************/
|
|
452
|
+
listProps: {
|
|
453
|
+
type: Object
|
|
454
|
+
},
|
|
455
|
+
formProps: {
|
|
456
|
+
type: Object
|
|
457
|
+
},
|
|
458
|
+
descProps: {
|
|
459
|
+
type: Object
|
|
460
|
+
},
|
|
461
|
+
modalProps: {
|
|
462
|
+
type: Object
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const Curd = vue.defineComponent({
|
|
467
|
+
props: { ...proCurdProps()
|
|
468
|
+
},
|
|
469
|
+
setup: (props, {
|
|
470
|
+
slots
|
|
471
|
+
}) => {
|
|
472
|
+
const {
|
|
473
|
+
columns,
|
|
474
|
+
state,
|
|
475
|
+
sendEvent,
|
|
476
|
+
sendRequest
|
|
477
|
+
} = useProModule();
|
|
478
|
+
/**
|
|
479
|
+
* 排序
|
|
480
|
+
* @param list
|
|
481
|
+
* @param propName
|
|
482
|
+
*/
|
|
483
|
+
|
|
484
|
+
const dealSort = (list, propName) => {
|
|
485
|
+
return lodash.sortBy(list, item => lodash.get(item, propName));
|
|
486
|
+
};
|
|
487
|
+
/**
|
|
488
|
+
* 非 hideInForm columns
|
|
489
|
+
*/
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
const formColumns = vue.computed(() => {
|
|
493
|
+
return dealSort(lodash.filter(columns.value, item => !item.hideInForm), "formSort");
|
|
494
|
+
});
|
|
495
|
+
/**
|
|
496
|
+
* 非 hideInDetail columns
|
|
497
|
+
*/
|
|
498
|
+
|
|
499
|
+
const descColumns = vue.computed(() => {
|
|
500
|
+
return dealSort(lodash.filter(columns.value, item => !item.hideInDetail), "descSort");
|
|
501
|
+
});
|
|
502
|
+
/**
|
|
503
|
+
* 非 hideInTable columns
|
|
504
|
+
*/
|
|
505
|
+
|
|
506
|
+
const tableColumns = vue.computed(() => {
|
|
507
|
+
return dealSort(lodash.filter(columns.value, item => !item.hideInTable), "tableSort");
|
|
508
|
+
});
|
|
509
|
+
/**
|
|
510
|
+
* search columns
|
|
511
|
+
*/
|
|
512
|
+
|
|
513
|
+
const searchColumns = vue.computed(() => {
|
|
514
|
+
return dealSort(lodash.filter(columns.value, item => !!item.search), "searchSort");
|
|
515
|
+
});
|
|
516
|
+
/******************************** 逻辑 *************************************/
|
|
517
|
+
//上一次发起列表请求的参数
|
|
518
|
+
|
|
519
|
+
let prevListParams; //刷新列表
|
|
520
|
+
|
|
521
|
+
const handleSearch = extra => {
|
|
522
|
+
sendRequest(exports.CurdAction.LIST, { ...prevListParams,
|
|
523
|
+
...extra
|
|
524
|
+
});
|
|
525
|
+
}; //发送事件
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
const sendCurdEvent = event => {
|
|
529
|
+
sendEvent({
|
|
530
|
+
type: event.action,
|
|
531
|
+
payload: lodash.omit(event, "action")
|
|
532
|
+
});
|
|
533
|
+
}; //事件订阅
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
useModuleEvent(event => {
|
|
537
|
+
if (event.type === RequestAction.Success) {
|
|
538
|
+
return;
|
|
539
|
+
} else if (event.type === RequestAction.Fail) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const action = event.type;
|
|
544
|
+
const {
|
|
545
|
+
type,
|
|
546
|
+
values,
|
|
547
|
+
record
|
|
548
|
+
} = event.payload;
|
|
549
|
+
|
|
550
|
+
switch (action) {
|
|
551
|
+
case exports.CurdAction.LIST:
|
|
552
|
+
if (type === "emit") {
|
|
553
|
+
prevListParams = values;
|
|
554
|
+
handleSearch();
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return;
|
|
558
|
+
|
|
559
|
+
case exports.CurdAction.ADD:
|
|
560
|
+
if (type === "execute") {
|
|
561
|
+
sendRequest(exports.CurdAction.ADD, values, state.detailData);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
return;
|
|
565
|
+
|
|
566
|
+
case exports.CurdAction.EDIT:
|
|
567
|
+
if (type === "execute") {
|
|
568
|
+
sendRequest(exports.CurdAction.EDIT, values, state.detailData);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return;
|
|
572
|
+
|
|
573
|
+
case exports.CurdAction.DELETE:
|
|
574
|
+
if (type === "emit") {
|
|
575
|
+
sendRequest(exports.CurdAction.DELETE, record, props.rowKey);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
const operateMap = lodash.reduce(props.operates, (pair, item) => ({ ...pair,
|
|
582
|
+
[item.action]: item
|
|
583
|
+
}), {}); //根据Action获取ICurdOperateOpts
|
|
584
|
+
|
|
585
|
+
const getOperate = action => {
|
|
586
|
+
return lodash.get(operateMap, action);
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
provideProCurd({
|
|
590
|
+
rowKey: props.rowKey,
|
|
591
|
+
curdState: state,
|
|
592
|
+
formColumns,
|
|
593
|
+
descColumns,
|
|
594
|
+
tableColumns,
|
|
595
|
+
searchColumns,
|
|
596
|
+
//
|
|
597
|
+
sendCurdEvent,
|
|
598
|
+
//
|
|
599
|
+
getOperate,
|
|
600
|
+
//
|
|
601
|
+
listProps: props.listProps,
|
|
602
|
+
formProps: props.formProps,
|
|
603
|
+
descProps: props.descProps,
|
|
604
|
+
modalProps: props.modalProps
|
|
605
|
+
});
|
|
606
|
+
return () => {
|
|
607
|
+
var _slots$default;
|
|
608
|
+
|
|
609
|
+
return (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots);
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
const ProCurd = vue.defineComponent({
|
|
614
|
+
props: { ...ProModule.props,
|
|
615
|
+
...Curd.props
|
|
616
|
+
},
|
|
617
|
+
setup: (props, {
|
|
618
|
+
slots
|
|
619
|
+
}) => {
|
|
620
|
+
const curdState = props.curdState || vue.reactive({
|
|
621
|
+
detailData: {}
|
|
622
|
+
});
|
|
623
|
+
/****************** 请求处理 **********************/
|
|
624
|
+
//curd默认网络属性
|
|
625
|
+
|
|
626
|
+
const curdOperateOpts = {
|
|
627
|
+
[exports.CurdAction.LIST]: {
|
|
628
|
+
convertParams: values => values,
|
|
629
|
+
convertData: actor => {
|
|
630
|
+
var _actor$res;
|
|
631
|
+
|
|
632
|
+
return (_actor$res = actor.res) === null || _actor$res === void 0 ? void 0 : _actor$res.data;
|
|
633
|
+
},
|
|
634
|
+
loadingName: "listLoading",
|
|
635
|
+
stateName: "listData"
|
|
636
|
+
},
|
|
637
|
+
[exports.CurdAction.DETAIL]: {
|
|
638
|
+
convertParams: (record, rowKey) => lodash.pick(record, rowKey),
|
|
639
|
+
convertData: actor => {
|
|
640
|
+
var _actor$res2;
|
|
641
|
+
|
|
642
|
+
return (_actor$res2 = actor.res) === null || _actor$res2 === void 0 ? void 0 : _actor$res2.data;
|
|
643
|
+
},
|
|
644
|
+
loadingName: "detailLoading",
|
|
645
|
+
stateName: "detailData",
|
|
646
|
+
label: "详情"
|
|
647
|
+
},
|
|
648
|
+
[exports.CurdAction.ADD]: {
|
|
649
|
+
convertParams: (values, record) => ({
|
|
650
|
+
body: { ...record,
|
|
651
|
+
...values
|
|
652
|
+
}
|
|
653
|
+
}),
|
|
654
|
+
loadingName: "operateLoading",
|
|
655
|
+
label: "添加"
|
|
656
|
+
},
|
|
657
|
+
[exports.CurdAction.EDIT]: {
|
|
658
|
+
convertParams: (values, record) => ({
|
|
659
|
+
body: { ...record,
|
|
660
|
+
...values
|
|
661
|
+
}
|
|
662
|
+
}),
|
|
663
|
+
loadingName: "operateLoading",
|
|
664
|
+
label: "编辑"
|
|
665
|
+
},
|
|
666
|
+
[exports.CurdAction.DELETE]: {
|
|
667
|
+
convertParams: (values, record) => ({
|
|
668
|
+
body: { ...record,
|
|
669
|
+
...values
|
|
670
|
+
}
|
|
671
|
+
}),
|
|
672
|
+
label: "删除"
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
const requests = lodash.map(props.operates, item => {
|
|
676
|
+
const curdOpts = lodash.get(curdOperateOpts, item.action);
|
|
677
|
+
return { ...curdOpts,
|
|
678
|
+
...item
|
|
679
|
+
};
|
|
680
|
+
});
|
|
681
|
+
const moduleKeys = lodash.keys(ProModule.props);
|
|
682
|
+
return () => {
|
|
683
|
+
return vue.h(ProModule, { ...lodash.pick(props, moduleKeys),
|
|
684
|
+
state: curdState,
|
|
685
|
+
requests
|
|
686
|
+
}, vue.h(Curd, { ...lodash.omit(props, ...moduleKeys, "curdState")
|
|
687
|
+
}, slots));
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* 剔除showState或showStateRules规则为!true的值
|
|
694
|
+
* @param values
|
|
695
|
+
* @param showState
|
|
696
|
+
* @param showStateRules
|
|
697
|
+
*/
|
|
698
|
+
const getValidValues = (values, showState, showStateRules) => {
|
|
699
|
+
if (showState) {
|
|
700
|
+
const invalidKeys = lodash.filter(lodash.keys(showState), key => !showState[key]);
|
|
701
|
+
return lodash.omit(values, invalidKeys);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
if (showStateRules) {
|
|
705
|
+
const invalidKeys = lodash.filter(lodash.keys(showStateRules), key => !showStateRules[key](values));
|
|
706
|
+
return lodash.omit(values, invalidKeys);
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return values;
|
|
710
|
+
};
|
|
711
|
+
/**
|
|
712
|
+
* string类型的path转为arr
|
|
713
|
+
* @param path
|
|
714
|
+
*/
|
|
715
|
+
|
|
716
|
+
const convertPathToList = path => {
|
|
717
|
+
if (!path) {
|
|
718
|
+
return undefined;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
if (lodash.isArray(path)) {
|
|
722
|
+
return path;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (path && lodash.isString(path) && path.indexOf(".") > 0) {
|
|
726
|
+
return lodash.split(path, ".");
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
return [path];
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
exports.ProCurd = ProCurd;
|
|
733
|
+
exports.ProModule = ProModule;
|
|
734
|
+
exports.RequestAction = RequestAction;
|
|
735
|
+
exports.convertPathToList = convertPathToList;
|
|
736
|
+
exports.getColumnFormItemName = getColumnFormItemName;
|
|
737
|
+
exports.getColumnValueType = getColumnValueType;
|
|
738
|
+
exports.getFormItemEl = getFormItemEl;
|
|
739
|
+
exports.getItemEl = getItemEl;
|
|
740
|
+
exports.getValidValues = getValidValues;
|
|
741
|
+
exports.provideProCurd = provideProCurd;
|
|
742
|
+
exports.provideProModule = provideProModule;
|
|
743
|
+
exports.useComposeRequestActor = useComposeRequestActor;
|
|
744
|
+
exports.useDoneRequestActor = useDoneRequestActor;
|
|
745
|
+
exports.useFailedRequestActor = useFailedRequestActor;
|
|
746
|
+
exports.useModuleEvent = useModuleEvent;
|
|
747
|
+
exports.useProCurd = useProCurd;
|
|
748
|
+
exports.useProModule = useProModule;
|