107 lines
3.3 KiB
JavaScript
107 lines
3.3 KiB
JavaScript
/**
|
||
* 枚举创建工厂(构造函数),扩展枚举对象:keys、values(含key值的[{key,text,type}])、formatter。
|
||
* @param {*} enumObj 枚举值,支持标准模式{key:{text,type},},简单模式{key:text,}(会自动转换为标准模式)
|
||
* @param {*} keyParseFunc key的转换函数,默认null,如果key为整数则传 parseInt
|
||
*/
|
||
export default function EnumFactory(enumObj, keyParseFunc = null) {
|
||
//复制(继承)enumObj
|
||
Object.assign(this, enumObj)
|
||
|
||
// keys:枚举的key集合[key]
|
||
Object.defineProperty(this, 'keys', {
|
||
value: keyParseFunc ? Object.keys(enumObj).map(s => keyParseFunc(s)) : Object.keys(enumObj)
|
||
})
|
||
|
||
// 处理 values
|
||
let values = []
|
||
const ovalues = Object.values(enumObj)
|
||
// 主要区分下value是简单类型(字符串)还是对象类型
|
||
if (typeof ovalues[0] === 'string') {
|
||
ovalues.forEach((text, index) => {
|
||
const obj = { key: this.keys[index], text }
|
||
values.push(obj)
|
||
this[this.keys[index]] = obj
|
||
})
|
||
}
|
||
else {
|
||
ovalues.forEach((item, index) => {
|
||
item.key = this.keys[index]
|
||
values.push(item)
|
||
})
|
||
}
|
||
// 设置values属性
|
||
Object.defineProperty(this, 'values', { value: values })
|
||
|
||
// formatter:element中表格绑定枚举数据文本的formatter函数
|
||
// r、c为行列,可传入null
|
||
Object.defineProperty(this, 'formatter', {
|
||
value: function(r, c, value) {
|
||
return values.filter(v => v.key == value || v.text == value)[0]?.text || 'notfound'
|
||
}
|
||
})
|
||
|
||
//枚举定义的数据都是常量,不可修改,冻结一下
|
||
Object.freeze(this)
|
||
}
|
||
|
||
export const runningStateEnum = new EnumFactory({
|
||
10: { text: '运行', type: 'success' },
|
||
20: { text: '待机', type: 'primary' },
|
||
30: { text: '停机', type: 'warning' },
|
||
40: { text: '故障', type: 'danger' },
|
||
50: { text: '离线', type: 'info' },
|
||
}, parseInt)
|
||
|
||
export const onlingEnum = new EnumFactory({
|
||
1: { text: '在线', type: 'success' },
|
||
0: { text: '离线', type: 'info' },
|
||
}, parseInt)
|
||
|
||
export const drainTypeEnum = new EnumFactory({
|
||
'product': '生产工艺',
|
||
'mtrans': '物料输送',
|
||
'mstore':'物料储存'
|
||
})
|
||
export const mioTypeEnum = new EnumFactory({
|
||
'do_out': '生产领料',
|
||
'sale_out': '销售发货',
|
||
'pur_in': '采购入库',
|
||
'do_in': '生产入库',
|
||
'other_in': '其他入库',
|
||
'other_out': '其他出库'
|
||
})
|
||
export const productTypeEnum = new EnumFactory({
|
||
0: '电/水/气',
|
||
10: '成品',
|
||
20: '半成品',
|
||
30: '主要原料',
|
||
40: '辅助材料',
|
||
50: '加工工具',
|
||
60: '辅助工装',
|
||
70: '办公用品',
|
||
}, parseInt)
|
||
export const eqTypeEnum = new EnumFactory({
|
||
10: { text: '生产设备', type: 'success' },
|
||
20: { text: '计量设备', type: 'primary' },
|
||
30: { text: '治理设备', type: 'warning' },
|
||
40: { text: '监测设备', type: 'danger' },
|
||
50: { text: '监控设备', type: 'info' },
|
||
}, parseInt)
|
||
|
||
export const valTypeEnum = new EnumFactory({
|
||
'int': '整型',
|
||
'float': '浮点型',
|
||
'str': '字符串型',
|
||
'bool': '布尔型',
|
||
})
|
||
|
||
export const boolEnum = new EnumFactory({
|
||
true: '是',
|
||
false: '否',
|
||
})
|
||
|
||
export const iMEnum = new EnumFactory({
|
||
10: '单开为开/单停为停',
|
||
20: '单开为开/都停为停',
|
||
30: '都开为开/单停为停',
|
||
}, parseInt) |