-
Notifications
You must be signed in to change notification settings - Fork 3
/
reducer.js
146 lines (118 loc) · 5.16 KB
/
reducer.js
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
import { Map, List, fromJS } from 'immutable'
import { reducer as MetaReducer } from 'mk-meta-engine'
import config from './config'
import { getInitState } from './data'
import { history } from 'mk-utils'
class reducer {
constructor(option) {
this.metaReducer = option.metaReducer
this.config = config.current
}
init = (state, option) => {
state = this.metaReducer.init(state, getInitState())
if (this.config.menu && !this.config.webapi.getMenu) {
return this.load(state, { menu: this.config.menu })
}
return state
}
load = (state, { menu }) => {
if (!menu || menu.lenght == 0)
return state
var defaultMenuItem, firstMenuItem, defaultOpens = [], menuKeyNameMap = {}, menuAppNameMap = {}
const loop = (children) => {
const ret = []
children.forEach(child => {
menuKeyNameMap[child.name] = child.key
//history增加
if (child.appName) {
menuAppNameMap[child.appName] = {
name: child.name,
props: child.appParams || {}
}
}
if (!child.children) {
if (!firstMenuItem) {
firstMenuItem = child
}
if (child.isDefault) {
defaultMenuItem = child
}
}
else {
if (child.isExpand) {
defaultOpens.push(child)
}
loop(child.children)
}
})
return ret
}
loop(menu)
defaultMenuItem = defaultMenuItem || firstMenuItem
const menuSelectedKeys = fromJS(defaultMenuItem ? [defaultMenuItem.key] : [])
const menuDefaultOpenKeys = fromJS(defaultOpens.map(o => o.key))
const defaultContent = defaultMenuItem ? defaultMenuItem : {}
state = this.metaReducer.sf(state, 'data.menu', fromJS(menu))
state = this.metaReducer.sf(state, 'data.menuKeyNameMap', fromJS(menuKeyNameMap))
state = this.metaReducer.sf(state, 'data.menuAppNameMap', fromJS(menuAppNameMap))
state = this.metaReducer.sf(state, 'data.menuSelectedKeys', menuSelectedKeys)
state = this.metaReducer.sf(state, 'data.menuDefaultOpenKeys', menuDefaultOpenKeys)
const childApp = history.getChildApp('mk-app-portal')
if (childApp)
return this.setContent(state, '', childApp)
else
return this.setContent(state, defaultContent.name, defaultContent.appName, defaultContent.appProps)
}
setContent = (state, name, appName, appProps) => {
//判断当前显示页签appName和要新打开的是否一致
const currContent = this.metaReducer.gf(state, 'data.content')
if (currContent && appName == currContent.get('appName'))
return state
//history增加
let menuAppNameMap = this.metaReducer.gf(state, 'data.menuAppNameMap')
if (name && appName && menuAppNameMap.getIn([appName, 'name']) != name) {
menuAppNameMap = menuAppNameMap.set(appName, fromJS({ name, props: appProps }))
state = this.metaReducer.sf(state, 'data.menuAppNameMap', menuAppNameMap)
}
name = menuAppNameMap.getIn([appName, 'name'])
appProps = appProps || menuAppNameMap.getIn([appName, 'props'])
const content = fromJS({ name, appName, appProps })
state = this.metaReducer.sf(state, 'data.content', content)
var openTabs = this.metaReducer.gf(state, 'data.openTabs') || List()
var hit = openTabs.findIndex(o => o.get('name') == name || o.get('appName') == appName) != -1
const isTabsStyle = this.metaReducer.gf(state, 'data.isTabsStyle')
if (!hit) {
if (isTabsStyle)
openTabs = openTabs.push(content)
else
openTabs = List().push(content)
state = this.metaReducer.sf(state, 'data.openTabs', openTabs)
}
else {
if (!isTabsStyle) {
openTabs = List().push(content)
state = this.metaReducer.sf(state, 'data.openTabs', openTabs)
}
}
setTimeout(() => {
history.pushChildApp('mk-app-portal', content.get('appName'))
}, 0)
return state
}
closeContent = (state, name) => {
var openTabs = this.metaReducer.gf(state, 'data.openTabs') || List()
var hitIndex = openTabs.findIndex(o => o.get('name') == name)
openTabs = openTabs.remove(hitIndex)
state = this.metaReducer.sf(state, 'data.openTabs', openTabs)
return this.metaReducer.sf(state, 'data.content', openTabs.get(openTabs.size - 1))
}
closeAll = (state) => {
state = this.metaReducer.sf(state, 'data.openTabs', new List())
return this.metaReducer.sf(state, 'data.content', new Map())
}
}
export default function creator(option) {
const metaReducer = new MetaReducer(option),
o = new reducer({ ...option, metaReducer })
return { ...metaReducer, ...o }
}