-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
176 lines (149 loc) · 5.17 KB
/
action.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
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 React from 'react'
import { action as MetaAction, AppLoader } from 'mk-meta-engine'
import config from './config'
import utils from 'mk-utils'
import common from './common'
import beautify from 'mk-utils/lib/beautify'
import { fromJS } from 'immutable'
class action {
constructor(option) {
this.metaAction = option.metaAction
this.config = config.current
}
onInit = ({ component, injections }) => {
this.component = component
this.injections = injections
injections.reduce('init', { apps: this.getApps() })
}
getApps = (search) => {
const apps = this.config.apps
const keys = Object.keys(apps)
const ret = []
keys.forEach(k => {
if (k != 'config') {
ret.push({
name: apps[k].name,
meta: apps[k].meta
})
}
})
if (!search)
return ret
return ret.filter(o => o.name.indexOf(search) != -1)
}
appSelected = (selectedKeys) => {
if(!selectedKeys && selectedKeys.length == 0)
return
const apps = this.metaAction.gf('data.apps')
const hit = apps.find(o => o.get('name') == selectedKeys[0])
if (hit) {
this.metaAction.sfs({
'data.selectApp': hit,
'data.currentJson':
this.metaAction.gf('data.selectType') == 'meta'
? beautify.beautifyJS(JSON.stringify(hit.get('meta').toJS()))
: beautify.beautifyJS(this.getAppState(hit.get('name')))
})
}
}
searchChange = (e) => {
this.metaAction.sfs({
'data.search': e.target.value,
'data.apps': fromJS(this.getApps(e.target.value))
})
}
typeChange = (e) => {
var currentJson = ''
if (e.target.value == 'meta') {
currentJson = beautify.beautifyJS(JSON.stringify(this.metaAction.gf('data.selectApp.meta').toJS()))
} else {
const appName = this.metaAction.gf('data.selectApp.name')
currentJson = this.getAppState(appName)
}
this.metaAction.sfs({
'data.selectType': e.target.value,
'data.currentJson': beautify.beautifyJS(currentJson)
})
}
getAppState = (appName) => {
var state = window.__mk_store__.getState().get(appName)
if (!state)
return '该应用还未加载'
state = state.toJS()
return beautify.beautifyJS(JSON.stringify({ data: state.data }))
}
jsonChange = (e, d, v) => {
try {
const json = utils.string.toJson(v)
const appName = this.metaAction.gf('data.selectApp.name')
const type = this.metaAction.gf('data.selectType')
if(type == 'meta'){
this.metaAction.sfs({
'data.currentJson': v,
'data.selectApp.meta': fromJS(json)
})
}
else{
this.metaAction.sf('data.currentJson', v)
}
if (type == 'meta'){
this.forceUpdateMeta(appName, json)
}
else if (type == 'state')
this.forceUpdateState(appName, json)
}
catch (e) {
console.error(e)
}
}
forceUpdateMeta = (appName, meta) => {
setTimeout(() => {
this.metaAction.setMetaForce(appName, meta)
const apps = this.metaAction.getAppInstances()
const keys = Object.keys(apps)
keys.forEach(k => {
if (apps[k].appName == appName) {
const inst = apps[k].instance
inst.forceUpdate()
}
})
}, 0)
}
forceUpdateState = (appName, state) => {
setTimeout(() => {
const apps = this.metaAction.getAppInstances()
const keys = Object.keys(apps)
keys.forEach(k => {
if (apps[k].appName == appName) {
const inst = apps[k].instance
inst.props.setField('data', fromJS(state.data))
}
})
}, 0)
}
formatJson = () => {
const formated = beautify.beautifyJS(this.metaAction.gf('data.currentJson'))
this.metaAction.sf('data.currentJson', formated)
}
reset = () => {
const apps = this.config.apps
const keys = Object.keys(apps)
const selectApp = this.metaAction.gf('data.selectApp')
keys.forEach(k => {
if (apps[k].name == selectApp.get('name')) {
this.metaAction.sfs({
'data.selectApp.meta': fromJS(apps[k].meta),
'data.currentJson': beautify.beautifyJS(JSON.stringify(apps[k].meta))
})
this.forceUpdateMeta(selectApp.get('name'), apps[k].meta)
}
})
}
}
export default function creator(option) {
const metaAction = new MetaAction(option),
o = new action({ ...option, metaAction }),
ret = { ...metaAction, ...o }
metaAction.config({ metaHandlers: ret })
return ret
}