Skip to content

Commit ae2399b

Browse files
committed
refactor(block-mixin): 重构block-mixin 相关初始化field代码
1 parent 64db2b1 commit ae2399b

File tree

1 file changed

+80
-98
lines changed

1 file changed

+80
-98
lines changed

src/ams/mixins/block-mixin.js

Lines changed: 80 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue';
2+
import lodashGet from 'lodash.get'
23
import ams from '../index';
34
import { listStringHasValue, get, getByOrder, deepExtend, getType, watermark } from '../../utils';
45
import { getRouter } from './router';
@@ -158,10 +159,7 @@ export default {
158159
async initBlock() {
159160
this.block = await ams.getBlock(this.name);
160161
if (this.block) {
161-
this.resource =
162-
typeof this.block.resource === 'string'
163-
? ams.resources[this.block.resource]
164-
: ams.resource('', this.block.resource);
162+
this.initResource();
165163
ams.$blocks[this.name] = this;
166164
this.initFields();
167165
this.getOperationsCounts();
@@ -215,94 +213,79 @@ export default {
215213
field.ctx = this.block.ctx || 'view';
216214
}
217215
},
216+
/**
217+
* 初始化 列表/表格 字段信息
218+
* @param {String} fieldKey 字段key
219+
* @param {Object} field 字段对应的对象,即 { type, props, width }
220+
* @returns
221+
*/
222+
initListField(fieldKey, field) {
223+
const blockType = ams.configs.baseBlockType[this.block.type]
224+
if (blockType !== 'list') return
225+
// 排序
226+
if (this.block.sorts[fieldKey]) {
227+
field.props.sortable = 'custom';
228+
}
229+
// 过滤
230+
const filter = this.block.filters[fieldKey];
231+
if (!filter) return
232+
let filterOptions = filter.options || field.props.options;
233+
if (!filterOptions) return
234+
if (Array.isArray(filterOptions)) {
235+
filterOptions = filterOptions.map(option => ({
236+
text: option.label,
237+
value: option.value,
238+
}));
239+
} else {
240+
filterOptions = Object.keys(filterOptions).map(key => ({
241+
text: filterOptions[key],
242+
value: key
243+
}));
244+
}
245+
field.props.filters = filterOptions;
246+
field.props['filter-multiple'] = !!filter.multiple;
247+
if (filter.remote) return
248+
field.props['filter-method'] = (value, row, column) => listStringHasValue(
249+
row[fieldKey],
250+
value
251+
);
252+
},
218253
initFields() {
254+
const resourceFields = this.resources && this.resources.fields;
255+
const blockFields = this.block.fields || {};
256+
if (!resourceFields) {
257+
this.fields = null;
258+
return;
259+
}
219260
// 为了可以做过滤、合并props等功能、通过computed属性重新处理field列表
220261
// 为了上层可以快速定位修改
221-
if (this.resource && this.resource.fields) {
222-
const fields = {};
223-
let fieldKeys = Object.keys(this.resource.fields);
224-
fieldKeys.forEach(name => {
225-
const resourceField = this.resource.fields[name];
226-
let blockField =
227-
this.block.fields && this.block.fields[name];
228-
// 字段隐藏,bock内可以通过 fields: {testMarkdown: false} 隐藏字段
229-
if (blockField === false) {
230-
return;
231-
}
232-
blockField = blockField || {};
233-
234-
const field = {
235-
// 默认block级ctx
236-
name,
237-
ctx: this.block.ctx || 'view',
238-
props: {},
239-
on: {}
240-
};
241-
// 按优先级测试合并
242-
// deepExtend(field, defaultFieldConfig[blockField.type || resourceField.type]);
243-
deepExtend(field, resourceField);
244-
deepExtend(field, blockField);
245-
this.initDefaultField(field);
246-
// console.log(field);
247-
// 处理列表
248-
if (ams.configs.baseBlockType[this.block.type] === 'list') {
249-
// 排序
250-
if (this.block.sorts[name]) {
251-
field.props.sortable = 'custom';
252-
}
253-
// 过滤
254-
const filter = this.block.filters[name];
255-
if (filter) {
256-
let filterOptions =
257-
filter.options || field.props.options;
262+
const fields = {};
263+
let fieldKeys = Object.keys(resourceFields);
264+
fieldKeys.forEach(fieldKey => {
265+
const resourceField = resourceFields[fieldKey];
266+
let blockField = blockFields[fieldKey];
267+
// 字段隐藏,bock内可以通过 fields: {[fieldKey]: false} 隐藏字段
268+
if (blockField === false) return;
269+
blockField = blockField || {};
258270

259-
if (filterOptions) {
260-
if (Array.isArray(filterOptions)) {
261-
filterOptions = filterOptions.map(option => {
262-
return {
263-
text: option.label,
264-
value: option.value,
265-
};
266-
});
267-
} else {
268-
filterOptions = Object.keys(filterOptions).map(
269-
key => {
270-
const label = filterOptions[key];
271-
return {
272-
text: label,
273-
value: key
274-
};
275-
}
276-
);
277-
}
278-
field.props.filters = filterOptions;
279-
field.props[
280-
'filter-multiple'
281-
] = !!filter.multiple;
282-
if (!filter.remote) {
283-
field.props['filter-method'] = (
284-
value,
285-
row,
286-
column
287-
) => {
288-
// console.log('filter-method', value, row, column);
289-
return listStringHasValue(
290-
row[name],
291-
value
292-
);
293-
};
294-
}
295-
}
296-
}
297-
}
298-
fields[name] = field;
299-
});
300-
this.fields = fields;
301-
this.layout = this.getFieldsLayout(fields, this.block.layout);
302-
// console.log(this.layout)
303-
} else {
304-
this.fields = null;
305-
}
271+
const field = {
272+
// 默认block级ctx
273+
name: fieldKey,
274+
ctx: this.block.ctx || 'view',
275+
props: {},
276+
on: {}
277+
};
278+
// 按优先级测试合并
279+
// deepExtend(field, defaultFieldConfig[blockField.type || resourceField.type]);
280+
deepExtend(field, resourceField);
281+
deepExtend(field, blockField);
282+
this.initDefaultField(field);
283+
this.initListField(fieldKey, field) // 处理列表
284+
fields[fieldKey] = field;
285+
});
286+
this.fields = fields;
287+
this.layout = this.getFieldsLayout(fields, this.block.layout);
288+
// console.log(this.layout)
306289
},
307290
getFieldsLayout(fields, layout) {
308291
let la = {};
@@ -431,16 +414,15 @@ export default {
431414
},
432415
async emitEvent(name, args) {
433416
// console.log('emitEvent', name, args)
434-
if (name) {
435-
const action = this.block.events[name];
436-
// 保证传入action的都是一个对象
437-
args = args || {};
438-
if (action) {
439-
await this.callAction(action, args);
440-
} else {
441-
// 如果没有绑定event、默认调用同名action、这样可以简化减少如 evnets:{list:'@list'} 的配置
442-
await this.callAction(`@${name}`, args);
443-
}
417+
if (!name) return;
418+
const action = this.block.events[name];
419+
// 保证传入action的都是一个对象
420+
args = args || {};
421+
if (action) {
422+
await this.callAction(action, args);
423+
} else {
424+
// 如果没有绑定event、默认调用同名action、这样可以简化减少如 events:{list:'@list'} 的配置
425+
await this.callAction(`@${name}`, args);
444426
}
445427
},
446428
async callAction(...args) {

0 commit comments

Comments
 (0)