File size: 4,280 Bytes
c9d0005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { get, set, unset } from 'lodash-es';
import {
  _cloneDeep,
  isArray,
  isObject,
  safeGet
} from '../utils/index';

const isMultiBind = (array: string[]) => isArray(array) && array.every(item => typeof item === 'string');

// Need to consider list nested controls
const transformPath = (path: string) => {
  const result: string[] = [];

  const recursion = (str: string) => {
    const index = str.indexOf('[]');
    if (index === -1) {
      result.push(str);
      return;
    }
    result.push(str.substring(0, index));
    recursion(str.substring(index+3))
  };

  recursion(path);

  if (result.length === 1) {
    return result[0];
  }
  return result;
};

const transformValueToBind = (data: any, path: any, bind: false | string | string[]) => {
  if (bind === false) {
    unset(data, path);
    return;
  }

  if (typeof bind === 'string') {
    let value = get(data, path);
    const preValue = get(data, bind);
    if (isObject(preValue)) {
      value = { ...preValue, ...value };
    }
    set(data, bind, value);
    unset(data, path);
    return;
  }

  // The array is converted to multiple fields.
  if (isMultiBind(bind)) {
    const value = get(data, path);
    unset(data, path);

    if (Array.isArray(value)) {
      value.forEach((item, index) => {
        const bindPath = bind[index];
        bindPath && set(data, bindPath, item);
      });
    }
  }
}

const transformBindToValue = (data: any, path: any, bind: any) => {
  if (typeof bind === 'string') {
    let value = get(data, bind);
    const preValue = get(data, path);
    if (isObject(preValue)) {
      value = { ...preValue, ...value };
    }
    set(data, path, value);
    unset(data, bind);
    return;
  }

  // The array is converted to multiple fields.
  if (isMultiBind(bind)) {
    const value = [];
    bind.forEach(key => {
      const bindValue = get(data, key);
      // if (bindValue != undefined) {
      //   value.push(bindValue);
      // }
      value.push(bindValue);
      unset(data, key);
    });

    if (value.length > 0) {
      set(data, path, value);
    }
  }
}


export const parseValuesToBind = (values: any, flatten: any) => {
  // No bind field exists, no processing
  if (!JSON.stringify(flatten).includes('bind')) {
    return values;
  }

  const data = _cloneDeep(values);

  const dealFieldList = (obj: any, [path, ...rest]: any, bind: any) => {
    if (rest.length === 1) {
      const list = get(obj, path, [])||[];
      list.forEach((item: any, index: number) => {
        const value = get(item, rest[0]);
        if (bind === 'root') {
          list[index] = value;
          return;
        }
        transformValueToBind(item, rest[0], bind);
      });
    }

    if (isArray(obj)) {
      obj.forEach((item: any) => dealFieldList(item, [path, ...rest], bind));
    } else if (isObject(obj)) {
      const value = get(obj, path);
      dealFieldList(value, rest, bind);
    }
  };

  Object.keys(flatten).forEach(key => {
    const bind = flatten[key]?.schema?.bind;
    if (bind === undefined) {
      return;
    }
    const path = transformPath(key);
    isArray(path) ? dealFieldList(data, path, bind) : transformValueToBind(data, path, bind);
  });

  return data;
};

export const parseBindToValues = (values: any, flatten: any) => {
  if (!JSON.stringify(flatten).includes('bind')) {
    return values;
  }

  const data = _cloneDeep(values);
  const dealFieldList = (obj: any, [path, ...rest]: any, bind: any) => {
    if (rest.length === 1) {
      const list = safeGet(obj, path, []);
      list.forEach((item: any, index: number) => {
        if (bind === 'root') {
          list[index] = { [rest[0]] : item };
          return;
        }
        transformBindToValue(item, rest[0], bind);
      });
    }

    if (isArray(obj)) {
      obj.forEach((item: any) => dealFieldList(item, [path, ...rest], bind));
    } else if (isObject(obj)) {
      const value = get(obj, path);
      dealFieldList(value, rest, bind);
    }
  };

  Object.keys(flatten).forEach(key => {
    const bind = flatten[key]?.schema?.bind;
    if (bind === undefined) {
      return;
    }
    const path = transformPath(key);

    isArray(path) ? dealFieldList(data, path, bind) : transformBindToValue(data, path, bind);
  });

  return data;
};