meta-util.js 4.2 KB

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