|
@@ -1,4 +1,5 @@
|
|
|
import _ from 'lodash-es';
|
|
|
+import { BillboardType, HeaderConfig, ListConfig, TableConfig, TitleConfig } from './billboard.data';
|
|
|
|
|
|
/**
|
|
|
* 功能类似 lodash.get 但是当取值为 null 时也直接返回默认值
|
|
@@ -7,3 +8,80 @@ export const get: typeof _.get = (o, p, defaultValue = '/') => {
|
|
|
const d = _.get(o, p, defaultValue);
|
|
|
return d === null ? defaultValue : d;
|
|
|
};
|
|
|
+
|
|
|
+/** 依据配置获取适用于 CommonTitle 的数据 */
|
|
|
+export function parseTitleConfig(data: BillboardType, config: TitleConfig) {
|
|
|
+ const val = get(data, config.prop);
|
|
|
+ return {
|
|
|
+ label: config.label,
|
|
|
+ value: config.translation ? get(config.translation, val) : val,
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+/** 依据配置获取适用于 LargeBoard 的数据 */
|
|
|
+export function parseHeaderConfigs(data: BillboardType, configs: HeaderConfig[]) {
|
|
|
+ const types = ['to-top-right', 'to-bottom-right'];
|
|
|
+ return configs.map((cfg, i) => {
|
|
|
+ const val = get(data, cfg.prop);
|
|
|
+ return {
|
|
|
+ label: cfg.label,
|
|
|
+ value: cfg.translation ? get(cfg.translation, val) : val,
|
|
|
+ type: types[i % configs.length],
|
|
|
+ };
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/** 依据配置获取适用于 CommonTable、CollapseTable 的数据 */
|
|
|
+export function parseTableConfig(data: BillboardType, config: TableConfig) {
|
|
|
+ const arr = get(data, config.prop, []);
|
|
|
+ if (config.translation) {
|
|
|
+ const finalRes = arr.reduce((res: any[], ele) => {
|
|
|
+ // 对于表格里的每条数据,如果需要翻译则翻译之
|
|
|
+ config.columns.forEach((col) => {
|
|
|
+ if (col._t) {
|
|
|
+ const v = get(ele, col.prop);
|
|
|
+ _.set(ele, col.prop, get(config.translation, v));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ config.collapses.forEach((col) => {
|
|
|
+ if (col._t) {
|
|
|
+ const v = get(ele, col.prop);
|
|
|
+ _.set(ele, col.prop, get(config.translation, v));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ res.push(ele);
|
|
|
+ return res;
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ return {
|
|
|
+ data: finalRes,
|
|
|
+ collapses: config.collapses,
|
|
|
+ columns: config.columns,
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ return {
|
|
|
+ data,
|
|
|
+ collapses: config.collapses,
|
|
|
+ columns: config.columns,
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/** 依据配置获取适用于 ListItem 的数据 */
|
|
|
+export function parseListConfigs(data: BillboardType, configs: ListConfig[]) {
|
|
|
+ const types = ['blue', 'green'];
|
|
|
+ return configs.map((cfg, i) => {
|
|
|
+ const val = get(data, cfg.prop);
|
|
|
+ return {
|
|
|
+ icon: cfg.icon,
|
|
|
+ label: cfg.label,
|
|
|
+ value: cfg.translation ? get(cfg.translation, val) : val,
|
|
|
+ type: types[i % configs.length],
|
|
|
+ collapses: cfg.collapses.map((col) => {
|
|
|
+ return {
|
|
|
+ label: col.label,
|
|
|
+ };
|
|
|
+ }),
|
|
|
+ };
|
|
|
+ });
|
|
|
+}
|