meta-util.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. /* 字段定义 */
  4. export const FieldMeta = meta => {
  5. let { field, slots, listOpts } = meta;
  6. const { rules, formOpts, order = 0 } = meta;
  7. if (field === undefined) {
  8. const { name, label, required = false, readonly = false, editable = true } = meta;
  9. field = { name, label, required, readonly, editable };
  10. }
  11. if (_.isArray(field)) {
  12. const [name, label, required = false, readonly = false, editable = true] = field;
  13. field = { name, label, required, readonly, editable };
  14. }
  15. if (slots === undefined) {
  16. // 从meta对象中提取slots属性
  17. const { filter = false, list = true, form = true } = meta;
  18. slots = { filter, list, form };
  19. } else if (_.isObject(slots)) {
  20. // 处理slots默认值
  21. const { filter = false, list = true, form = true } = slots;
  22. slots = { filter, list, form };
  23. } else if (_.isArray(slots)) {
  24. slots = { filter: slots.includes('filter'), list: slots.includes('list'), form: slots.includes('form') };
  25. }
  26. // 处理formatter
  27. let { formatter } = meta;
  28. if (!_.isFunction(formatter) && (_.isObject(formatter) || _.isString(formatter) || _.isArray(formatter))) {
  29. let { name, param } = _.isObject(formatter) ? formatter : {};
  30. if (_.isString(formatter)) {
  31. [name, param] = formatter.split(':', 2);
  32. } else if (_.isArray(formatter)) {
  33. [name, param] = formatter;
  34. }
  35. if (name === undefined) {
  36. formatter = undefined;
  37. } else if (name === 'dict' && param === undefined) {
  38. console.warn(`use ${formatter} formatter must set param,example: 'dict:status' `);
  39. formatter = undefined;
  40. } else {
  41. formatter = { name, param };
  42. }
  43. }
  44. return { field, rules, slots, order, formOpts, listOpts, formatter };
  45. };
  46. /* 列表操作列定义 */
  47. export const Operation = meta => {
  48. let items = Object.entries(meta);
  49. if (_.isArray(meta)) {
  50. items = Object.values(meta);
  51. }
  52. return items.map(item => {
  53. if (_.isArray(item)) {
  54. let [event, label, icon, confirm = false] = item;
  55. if (_.isBoolean(icon)) {
  56. confirm = icon;
  57. icon = undefined;
  58. }
  59. return { event, label, icon, confirm };
  60. }
  61. return item;
  62. });
  63. };
  64. // 预置formatter函数
  65. export const formatters = {
  66. date: param => (row, column, cellValue, index) => {
  67. if (cellValue) {
  68. return moment(cellValue).format(param || 'YYYY-MM-DD');
  69. }
  70. return cellValue;
  71. },
  72. dict: param => {
  73. return function(row, column, cellValue, index) {
  74. if (_.isString(cellValue)) {
  75. return this.$dict(param, cellValue);
  76. }
  77. return cellValue;
  78. };
  79. },
  80. bool: param => (row, column, cellValue, index) => {
  81. if (cellValue === undefined) {
  82. return '';
  83. }
  84. return cellValue ? '是' : '否';
  85. },
  86. };
  87. export const Formatter = (meta, _this) => {
  88. // 处理formatter
  89. let { formatter } = meta;
  90. if (!_.isFunction(formatter) && _.isObject(formatter)) {
  91. const { name, param } = formatter;
  92. formatter = formatters[name](param);
  93. if (_this !== undefined) {
  94. formatter = formatter.bind(_this);
  95. }
  96. }
  97. if (!_.isFunction(formatter)) {
  98. formatter = undefined;
  99. }
  100. return formatter;
  101. };
  102. export const MergeFilters = (meta, _this) => {
  103. // 生成column filters
  104. let { formatter, listOpts } = meta;
  105. if (listOpts && listOpts.filterable && _.isObject(formatter) && formatter.name === 'dict') {
  106. let items = _this.$dict(formatter.param);
  107. if (!items || items.length > 20) return undefined;
  108. const filterMethod = (value, row, column) => {
  109. const property = column['property'];
  110. return row[property] === value;
  111. };
  112. const filters = items.map(p => ({ text: p.name, value: p.code }));
  113. return { filters, filterMethod, ...listOpts };
  114. }
  115. return listOpts;
  116. };