vue-ant-patterns
Version:
vue-ant-patterns
364 lines (313 loc) • 11.1 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
var _antDesignVue = require('ant-design-vue');
var _en_US = require('./locale/en_US');
var _en_US2 = _interopRequireDefault(_en_US);
var _zh_CN = require('./locale/zh_CN');
var _zh_CN2 = _interopRequireDefault(_zh_CN);
var _bizUtils = require('../_biz-utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var dateFormatName = 'dateFormat'; /*
* @Author: w.pengfei
* @Date: 2020-06-22 11:18:04
* @LastEditTime: 2021-04-20 16:31:18
* @Description: ''
* @LastEditors: w.pengfei
*/
var langType = {
'zh-cn': _zh_CN2['default'],
'en-us': _en_US2['default']
};
exports['default'] = {
created: function created() {
this.getDateFormat();
},
data: function data() {
var _$props = this.$props,
vmStatus = _$props.vmStatus,
dateFormat = _$props.dateFormat;
var isEdit = vmStatus === 'edit';
return {
langType: langType,
v_status: vmStatus,
isEdit: isEdit,
strConnector: '_#',
vFormat: dateFormat
};
},
watch: {
vmStatus: function vmStatus(val) {
this.isEdit = val === 'edit';
this.v_status = val;
},
dateFormat: function dateFormat(v) {
this.vFormat = v;
}
},
methods: {
getDateFormat: function getDateFormat() {
var dateFormat = this.$props.dateFormat;
var systemFromat = (0, _bizUtils.getCache)(dateFormatName);
this.vFormat = dateFormat || systemFromat;
},
/**
* 获取默认语言
*/
getDefaultLocale: function getDefaultLocale() {
var result = {
lang: (0, _extends3['default'])({}, _zh_CN2['default'])
};
return result;
},
/**
* 根据规则拼装, 分割字段
* 分别取到 panel code, section id, field name
* 拼装相同规则
*/
combinationFieldName: function combinationFieldName(panelCode, sectionCode, fieldName) {
var strConnector = this.strConnector;
var nFieldName = '' + panelCode + strConnector + sectionCode + strConnector + fieldName;
return nFieldName;
},
// 分割字段
separationFieldName: function separationFieldName(str) {
if (!str) return null;
var strConnector = this.strConnector;
var _str$split = str.split('' + strConnector),
_str$split2 = (0, _slicedToArray3['default'])(_str$split, 3),
panelCode = _str$split2[0],
sectionCode = _str$split2[1],
fieldName = _str$split2[2];
return {
panelCode: panelCode,
sectionCode: sectionCode,
fieldName: fieldName
};
},
/**
* 获取区段数据, 暂时应对 FORM, 列表类型还需要梳理下
* @param { String } sectionId 区段 code 或 id
*/
getSectionData: function getSectionData(sectionId) {
var dataSource = this.$props.dataSource;
var sectionData = dataSource[sectionId];
return sectionData;
},
/**
*
* 编辑状态下的 section, 这里主要处理列表情况下 section DATA 为选中一行的数据
*/
getEditSectionData: function getEditSectionData(sectionData) {
return sectionData ? sectionData.length > 0 ? this.selectTableRow : sectionData : '';
},
/**
* 获取数据中对应字段的值
* 编辑与查看状态返回值不同
* 查看状态直接返回值
* 编辑状态需要对应控件类型的选项
* @param { String } controlType 控件类型
* @param { String } fieldName 字段名
* @param { String } tempDefaultValue 默认值
* @param { Object } data 数据源
*/
getFieldValue: function getFieldValue(controlType, fieldName, data, tempDefaultValue) {
var uData = data;
if (!data || Array.isArray(data) && data.length < 1) {
return null;
}
if (Array.isArray(data)) {
uData = data[0];
}
var o = {
view: 'ViewValue',
edit: 'EditValue'
};
var v_status = this.v_status;
return this['getField' + o[v_status]](controlType, uData[fieldName], tempDefaultValue);
},
/**
* 获取查看状态下对应字段的数据
* 根据字段类型, 查找所对应的值
* @param { String } controlType 控件类型
* @param { any } val 字段值
*/
getFieldViewValue: function getFieldViewValue(controlType, val, tempDefaultValue) {
var value = val || tempDefaultValue || undefined;
/*
之前的date 类型直接取值, 之后后端调整了数据为
{
time: ....
}
取值为 value.time
*/
switch (controlType) {
case 'DATE':
if (value) {
value = (0, _moment2['default'])(value.time).format(this.vFormat);
}
break;
case 'NUMBER':
if (value && typeof value === 'string') {
value = +value;
}
break;
case 'TIME':
if (value) {
value = (0, _moment2['default'])(value.time).format('hh:mm:ss');
} else {
value = undefined;
}
break;
}
return value;
},
// dataStructure在常数代码时候才会传,有tree和list的两个值
/**
* 根据控件类型, 获取编辑状态下字段所对应的值
* @param { String } controlType 控件类型
* @param { any } val 字段值
* @param {*} dataStructure
*/
getFieldEditValue: function getFieldEditValue(controlType, val, tempDefaultValue) {
var value = val ? typeof val === 'string' ? val.trim() : val : tempDefaultValue || undefined;
switch (controlType) {
case 'DATE':
if (value) {
// 不能直接使用, 需要转换
value = (0, _moment2['default'])(value.time);
}
break;
case 'NUMBER':
if (value && typeof value === 'string') {
value = +value;
}
break;
case 'TIME':
if (value) {
value = (0, _moment2['default'])(value, 'HH:mm:ss');
} else {
value = undefined;
}
break;
}
return value;
},
renderEmpty: function renderEmpty() {
var h = this.$createElement;
return h(_antDesignVue.Empty, { style: 'margin: 80px 0 0' });
},
/**
* 获取某个字段的常用属性
*/
getFieldProperty: function getFieldProperty(itemObj) {
var _$props2 = this.$props,
sectionInfo = _$props2.sectionInfo,
fieldInfo = _$props2.fieldInfo,
panelInfo = _$props2.panelInfo,
maxLen = _$props2.maxLen,
forceDisableField = _$props2.forceDisableField,
forceEnableField = _$props2.forceEnableField;
var fieldItem = itemObj.field,
sectionItem = itemObj.section,
panelItem = itemObj.panel;
var _fieldItem$isDisplay = fieldItem.isDisplay,
isDisplay = _fieldItem$isDisplay === undefined ? false : _fieldItem$isDisplay,
isReadOnly = fieldItem.isReadOnly,
isRequired = fieldItem.isRequired,
isDisabled = fieldItem.isDisabled,
controlType = fieldItem.controlType,
constantType = fieldItem.constantType,
dataStructure = fieldItem.dataStructure,
searchHelperCode = fieldItem.searchHelperCode,
numFieldMaxValue = fieldItem.numFieldMaxValue,
tempDefaultValue = fieldItem.defaultValue,
_fieldItem$isDisplayI = fieldItem.isDisplayInGrid,
isDisplayInGrid = _fieldItem$isDisplayI === undefined ? false : _fieldItem$isDisplayI;
// 各个区域的 key
var _sectionInfo = (0, _slicedToArray3['default'])(sectionInfo, 2),
vSectionIdKey = _sectionInfo[0],
vSectionCodeKey = _sectionInfo[1];
var _fieldInfo = (0, _slicedToArray3['default'])(fieldInfo, 3),
vIdKey = _fieldInfo[0],
vFieldNameKey = _fieldInfo[1],
vDispalyNameKey = _fieldInfo[2];
var _panelInfo = (0, _slicedToArray3['default'])(panelInfo, 2),
_ = _panelInfo[0],
vPanelCodeKey = _panelInfo[1];
var fieldName = fieldItem[vFieldNameKey];
// 区段 id code
var sectionId = sectionItem[vSectionIdKey],
sectionCode = sectionItem[vSectionCodeKey];
// 面板 code
var panelCode = panelItem[vPanelCodeKey];
/**
* 字段名用拼接
*/
var nFieldName = this.combinationFieldName(panelCode, sectionCode, fieldName);
var style = sectionItem.style;
var tmpMaxLen = +numFieldMaxValue;
if (tmpMaxLen > maxLen) {
tmpMaxLen = maxLen;
}
return (0, _extends3['default'])({}, itemObj, {
fieldId: fieldItem[vIdKey],
label: fieldItem[vDispalyNameKey],
fieldName: fieldName,
nFieldName: nFieldName,
isDisplay: isDisplay,
isReadOnly: isReadOnly,
isRequired: isRequired,
isDisabled: isDisabled,
controlType: controlType,
constantType: constantType,
dataStructure: dataStructure,
sectionId: sectionId,
sectionCode: sectionCode,
searchHelperCode: searchHelperCode,
isDisplayInGrid: isDisplayInGrid,
style: style,
forceDisableField: forceDisableField,
forceEnableField: forceEnableField,
numFieldMaxValue: tmpMaxLen,
fieldIdKey: vIdKey,
fieldNameKey: vFieldNameKey,
fieldLabelKey: vDispalyNameKey,
tempDefaultValue: tempDefaultValue
});
},
getSlots: function getSlots(prefix) {
var $scopedSlots = this.$scopedSlots;
// 首尾插槽
var firstSlot = $scopedSlots[prefix + '-first'];
var lastSlot = $scopedSlots[prefix + '-last'];
var afterSlot = $scopedSlots[prefix + '-after'];
var customSlot = $scopedSlots[prefix + '-custom'];
return {
firstSlot: firstSlot,
lastSlot: lastSlot,
afterSlot: afterSlot,
customSlot: customSlot
};
},
getPanelSlots: function getPanelSlots() {
var prefix = 'panel';
return this.getSlots(prefix);
},
getSectionSlots: function getSectionSlots() {
var prefix = 'section';
return this.getSlots(prefix);
},
getFieldSlots: function getFieldSlots() {
var prefix = 'field';
return this.getSlots(prefix);
}
}
};