UNPKG

4.23 kBJavaScriptView Raw
1import Immutable, { Map, List, fromJS } from 'immutable'
2import utils, { path } from 'edf-utils'
3
4const { existsParamsInPath, parsePath } = path
5
6
7const cache = { meta: Map() }
8
9window['__getCache'] = () => cache
10
11export function setMeta(appInfo) {
12 if (!appInfo || !appInfo.meta) return
13 const appName = appInfo.name
14 if (cache.meta.has(appName))
15 return
16 setMetaForce(appName, appInfo.meta)
17}
18
19export function setMetaForce(appName, meta) {
20 if (!appName || !meta)
21 return
22
23 meta = fromJS(meta)
24
25 cache.meta = cache.meta
26 .setIn([appName, 'meta'], meta)
27 .setIn([appName, 'metaMap'], parseMeta(meta))
28}
29
30export function getMeta(appInfo, fullpath, propertys) {
31 //path空取整个meta
32 if (appInfo && !appInfo.name && this.appInfo.name) {
33 appInfo.name = this.appInfo.name
34 }
35 if (!fullpath)
36 return cache.meta.getIn([appInfo.name, 'meta']).toJS()
37 const parsedPath = parsePath(fullpath),
38 vars = parsedPath.vars,
39 metaMap = cache.meta.getIn([appInfo.name, 'metaMap']),
40 meta = cache.meta.getIn([appInfo.name, 'meta'])
41
42 const path = metaMap.get(parsedPath.path)
43
44 const currentMeta = path ? meta.getIn(path.split('.')) : meta
45
46 //属性空,获取该路径下所有属性
47 if (!propertys)
48 return currentMeta.toJS()
49
50 const ret = {}
51
52 //属性为数组,遍历获取
53 if (propertys instanceof Array) {
54 propertys.forEach(p => {
55 let val = currentMeta.getIn(p.split('.'))
56 //immutable值,直接toJS()
57 ret[p] = (val && val.toJS) ? val.toJS() : val
58 })
59
60 return ret
61 }
62
63 //属性为字符串,直接获取
64 if (typeof propertys == 'string') {
65 let val = currentMeta.getIn(propertys.split('.'))
66 return (val && val.toJS) ? val.toJS() : val
67 }
68}
69
70export function getField(state, fieldPath) {
71 if (!fieldPath) {
72 return state.get('data')
73 }
74
75 if (fieldPath instanceof Array) {
76 return state.getIn(fieldPath)
77 } else {
78 return state.getIn(fieldPath.split('.'))
79 }
80}
81
82export function getFields(state, fieldPaths) {
83 var ret = {}
84 fieldPaths.forEach(o => ret[o] = getField(state, o))
85 return ret
86}
87
88export function setField(state, fieldPath, value) {
89 if (fieldPath instanceof Array) {
90 return state.setIn(fieldPath, value)
91 } else {
92 return state.setIn(fieldPath.split('.'), value)
93 }
94}
95
96export function setFields(state, values) {
97 Object.keys(values).forEach(k => {
98 state = setField(state, k, values[k])
99 })
100
101 return state
102}
103
104export function updateField(state, fieldPath, fn) {
105 if (fieldPath instanceof Array) {
106 return state.updateIn(fieldPath, fn)
107 } else {
108 return state.updateIn(fieldPath.split('.'), fn)
109 }
110}
111
112function isComponent(meta) {
113 return typeof meta == 'object' && !!meta.name && !!meta.component
114}
115
116function parseMeta(meta) {
117 let ret = Map()
118 const parseProp = (propValue, parentPath, parentRealPath) => {
119 if (!(propValue instanceof Immutable.Map)) {
120 return
121 }
122 if (propValue.get('name') && propValue.get('component')) {
123 const name = utils.string.trim(propValue.get('name'))
124 parentPath = parentPath ? `${parentPath}.${name}` : name
125 ret = ret.set(parentPath, parentRealPath)
126 }
127
128 propValue.keySeq().toArray().forEach(p => {
129
130 let v = propValue.get(p),
131 currentPath = parentPath ? `${parentPath}.${p}` : p
132 if (v instanceof Immutable.List) {
133 v.forEach((c, index) => {
134 let currentRealPath = parentRealPath ? `${parentRealPath}.${p}.${index}` : `${p}.${index}`
135 parseProp(c, `${currentPath}`, currentRealPath)
136 })
137 } else {
138 let currentRealPath = parentRealPath ? `${parentRealPath}.${p}` : p
139 parseProp(v, `${currentPath}`, currentRealPath)
140 }
141 })
142 }
143
144 parseProp(meta, '', '')
145 return ret
146}