UNPKG

12.9 kBJavaScriptView Raw
1import React from 'react'
2import { AppLoader } from 'edf-app-loader'
3import * as common from './common'
4import utils from 'edf-utils'
5import { fromJS } from 'immutable'
6import contextManager from './context'
7import config from './config'
8//import * as Babel from 'babel-standalone'
9
10
11const appInstances = {}
12class action {
13 constructor(option) {
14 this.appInfo = option.appInfo
15 this.meta = fromJS(option.appInfo.meta)
16 this.cache = {}
17
18 common.setMeta(option.appInfo)
19 }
20
21 config = ({ metaHandlers }) => {
22 this.metaHandlers = metaHandlers
23 }
24
25 initView = (component, injections) => {
26 this.component = component
27 this.injections = injections
28
29 appInstances[component.props.appFullName] = {
30 appName: component.props.appName,
31 appQuery: component.props.appQuery,
32 //app: config.getApps()[component.props.appName],
33 instance: component
34 }
35
36 this.metaHandlers && this.metaHandlers['onInit'] && this.metaHandlers['onInit']({ component, injections })
37 }
38
39 unmount = () => {
40 delete appInstances[this.component.appFullName]
41 }
42
43 componentWillMount = () => {
44 this.metaHandlers
45 && this.metaHandlers['componentWillMount']
46 && this.metaHandlers['componentWillMount'] != this.componentWillMount
47 && this.metaHandlers['componentWillMount']()
48 }
49
50 componentDidMount = () => {
51 this.metaHandlers
52 && this.metaHandlers['componentDidMount']
53 && this.metaHandlers['componentDidMount'] != this.componentDidMount
54 && this.metaHandlers['componentDidMount']()
55 }
56
57 shouldComponentUpdate = (nextProps, nextState) => {
58 this.metaHandlers
59 && this.metaHandlers['shouldComponentUpdate']
60 && this.metaHandlers['shouldComponentUpdate'] != this.shouldComponentUpdate
61 && this.metaHandlers['shouldComponentUpdate'](nextProps, nextState)
62 }
63
64 componentWillReceiveProps = (nextProps) => {
65 this.metaHandlers
66 && this.metaHandlers['componentWillReceiveProps']
67 && this.metaHandlers['componentWillReceiveProps'] != this.componentWillReceiveProps
68 && this.metaHandlers['componentWillReceiveProps'](nextProps)
69 }
70
71 componentWillUpdate = (nextProps, nextState) => {
72 this.metaHandlers
73 && this.metaHandlers['componentWillUpdate']
74 && this.metaHandlers['componentWillUpdate'] != this.componentWillUpdate
75 && this.metaHandlers['componentWillUpdate'](nextProps, nextState)
76 }
77
78 componentDidCatch = (error, info) => {
79 this.metaHandlers
80 && this.metaHandlers['componentDidCatch']
81 && this.metaHandlers['componentDidCatch'] != this.componentDidCatch
82 && this.metaHandlers['componentDidCatch'](error, info)
83 }
84
85 componentWillUnmount = () => {
86 this.metaHandlers
87 && this.metaHandlers['componentWillUnmount']
88 && this.metaHandlers['componentWillUnmount'] != this.componentWillUnmount
89 && this.metaHandlers['componentWillUnmount']()
90 }
91
92 componentDidUpdate = () => {
93 this.metaHandlers
94 && this.metaHandlers['componentDidUpdate']
95 && this.metaHandlers['componentDidUpdate'] != this.componentDidUpdate
96 && this.metaHandlers['componentDidUpdate']()
97 }
98
99 getAppInstances = () => {
100 return appInstances
101 }
102
103
104 getField = (fieldPath) => {
105 return common.getField(this.injections.getState(), fieldPath)
106 }
107
108 getFields = (fieldPaths) => {
109 return common.getFields(this.injections.getState(), fieldPaths)
110 }
111
112 setField = (fieldPath, value) => {
113 return this.injections.reduce('setField', fieldPath, value)
114 }
115
116 setFields = (values) => {
117 return this.injections.reduce('setFields', values)
118 }
119
120 parseExpreesion = (v) => {
121 if (!this.cache.expression)
122 this.cache.expression = {}
123
124 if (this.cache.expression[v]) {
125 return this.cache.expression[v]
126 }
127
128 if (!this.cache.expressionParams) {
129 this.cache.expressionParams = ['data']
130 .concat(Object.keys(this.metaHandlers).map(k => "$" + k))
131 .concat(['_path', '_rowIndex', '_vars', '_ctrlPath', '_lastIndex'])
132 }
133
134 var params = this.cache.expressionParams
135
136 var body = utils.expression.getExpressionBody(v)
137
138
139 this.cache.expression[v] = new Function(...params, body)
140 return this.cache.expression[v]
141
142 }
143
144 execExpression = (expressContent, data, path, rowIndex, vars, ctrlPath) => {
145
146 let browserType = utils.environment.getBrowserVersion()
147 if (browserType && browserType.ie) {
148 if (expressContent && expressContent.indexOf('=>') > -1) {
149 console.log('ie兼容exception:' + expressContent)
150 //if (Babel) {
151 // expressContent = Babel.transform(expressContent, { presets: ['es2015'] }).code
152 //expressContent = expressContent.replace("use strict;", "")
153 //v = v.replace("use strict", '')
154 //}
155 }
156 }
157 let _express = this.parseExpreesion(expressContent)
158 let values = [data]
159
160 Object.keys(this.metaHandlers).forEach(k => {
161 values.push((...args) => this.metaHandlers[k](...args, { currentPath: path, rowIndex, vars, lastIndex: vars && vars[vars.length - 1] }))
162 })
163 values = values.concat([path, rowIndex, vars, ctrlPath, vars && vars[vars.length - 1]])
164
165 if (typeof (_express) != 'undefined') {
166 try {
167 return _express.apply(this, values)
168 }
169 catch (e) {
170 this.metaHandlers
171 && this.metaHandlers['componentDidCatch']
172 && this.metaHandlers['componentDidCatch'] != this.componentDidCatch
173 && this.metaHandlers['componentDidCatch'](e)
174 utils.exception.error(e)
175 }
176 }
177
178 }
179
180
181 needUpdate = (meta) => {
182 if (!meta)
183 return false
184
185 const t = typeof meta
186
187 if (t == 'string' && utils.expression.isExpression(meta))
188 return true
189
190 if (t != 'object')
191 return false
192
193 if (meta["_notParse"] === true) {
194 return false
195 }
196
197 return !(t != 'object'
198 || !!meta['$$typeof']
199 || !!meta['_isAMomentObject']
200 || !!meta["_power"]
201 || meta["_visible"] === false)
202 }
203
204 updateMeta = (meta, path, rowIndex, vars, data, ctrlPath) => {
205
206 if (!this.needUpdate(meta))
207 return
208
209 if (meta instanceof Array) {
210 for (let i = 0; i < meta.length; i++) {
211 let sub = meta[i]
212 let currentPath = path
213 if (!sub)
214 continue
215
216 if (sub['_power']) {
217 currentPath = `${path}.${sub.name}`
218 sub.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
219 continue
220 }
221
222 let subType = typeof sub, isExpression = false, isMeta = false
223
224 if (subType == 'string' && utils.expression.isExpression(sub)) {
225 sub = this.execExpression(sub, data, path, rowIndex, vars, ctrlPath)
226 isExpression = true
227 if (sub && sub['_isMeta'] === true)
228 isMeta = true
229
230 if (sub && sub['_isMeta'] === true) {
231 isMeta = true
232 meta[i] = sub.value
233 }
234 else {
235 meta[i] = sub
236 }
237 }
238
239 if (!this.needUpdate(sub))
240 continue
241
242 if (isExpression && !isMeta) {
243 continue
244 }
245
246 subType = typeof sub
247
248 if (sub instanceof Array) {
249 currentPath = `${path}.${i}`
250 sub.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
251 this.updateMeta(sub, currentPath, rowIndex, vars, data, ctrlPath)
252 continue
253 }
254
255 if (sub.name && sub.component) {
256 currentPath = `${path}.${sub.name}`
257 sub.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
258 this.updateMeta(sub, currentPath, rowIndex, vars, data, sub.path)
259 }
260 else {
261 currentPath = `${path}.${i}`
262 sub.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
263 this.updateMeta(sub, currentPath, rowIndex, vars, data, ctrlPath)
264 }
265 }
266 return
267 }
268 var excludeProps = meta["_excludeProps"]
269 if (excludeProps && utils.expression.isExpression(excludeProps)) {
270 excludeProps = this.execExpression(excludeProps, data, path, rowIndex, vars, ctrlPath)
271 }
272
273 //去除meta的排除属性
274 if (excludeProps && excludeProps instanceof Array) {
275 excludeProps.forEach(k => {
276 if (meta[k])
277 delete meta[k]
278 })
279 }
280
281 const keys = Object.keys(meta)
282
283 for (let key of keys) {
284 let v = meta[key],
285 t = typeof v,
286 currentPath = path
287
288 if (!v)
289 continue
290 if (v['_power']) {
291 currentPath = `${path}.${key}.${v.name}`
292 v.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
293 continue
294 }
295
296 let isExpression = false, isMeta = false
297 if (t == 'string' && utils.expression.isExpression(v)) {
298 v = this.execExpression(v, data, `${path}.${key}`, rowIndex, vars, ctrlPath)
299 isExpression = true
300 if (key == '...' && v && typeof v == 'object') {
301 Object.keys(v).forEach(kk => {
302 meta[kk] = v[kk]
303 })
304 delete meta['...']
305 } else {
306 if (v && v['_isMeta'] === true) {
307 isMeta = true
308 meta[key] = v.value
309 }
310 else {
311 meta[key] = v
312 }
313 }
314 }
315
316 t = typeof t
317
318 if (!this.needUpdate(v))
319 continue
320
321 if (isExpression && !isMeta) {
322 continue
323 }
324 if (v instanceof Array) {
325 this.updateMeta(v, `${path}.${key}`, rowIndex, vars, data, ctrlPath)
326 continue
327 }
328
329 if (v.name && v.component) {
330 currentPath = `${path}.${key}.${v.name}`
331 v.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
332 this.updateMeta(v, currentPath, rowIndex, vars, data, v.path)
333 }
334 else {
335 currentPath = `${path}.${key}`
336 v.path = vars ? `${currentPath}, ${vars.join(',')}` : currentPath
337 this.updateMeta(v, currentPath, rowIndex, vars, data, ctrlPath)
338 }
339 }
340 }
341
342 getMeta = (fullPath, propertys, data) => {
343 const meta = common.getMeta(this.appInfo, fullPath, propertys),
344 parsedPath = utils.path.parsePath(fullPath),
345 path = parsedPath.path,
346 rowIndex = parsedPath.vars ? parsedPath.vars[0] : undefined,
347 vars = parsedPath.vars
348
349 if (!data)
350 data = common.getField(this.injections.getState()).toJS()
351
352 meta['_power'] = undefined
353 meta.path = fullPath
354 this.updateMeta(meta, path, rowIndex, vars, data, fullPath)
355 return meta
356 }
357
358 setMetaForce = (appName, meta) => {
359 common.setMetaForce(appName, meta)
360 }
361
362 focus = (path) => {
363 if (this.isFocus(path)) return false
364 this.setField('data.other.focusFieldPath', path)
365 return true
366 }
367
368 focusByEvent = (e) => {
369 const path = utils.path.findPathByEvent(e)
370 return this.focus(path)
371 }
372
373 isGridReadOnly = (path) => {
374 if (!path) return false
375 let gridReadOnly = this.getField(path)
376
377 //this.getField('data.other.isGridReadOnly')
378 if (!gridReadOnly) gridReadOnly = false
379
380 return gridReadOnly
381 }
382
383 isReadOnly = (path) => {
384 if (!path) return false
385 if (path.indexOf('.cell.cell') != -1) {
386 path = path.split('.cell.cell,')[0]
387 }
388 let _isReadOnly = this.getField(path + '.isReadOnly')
389 if (!_isReadOnly) _isReadOnly = false
390
391 return _isReadOnly
392 }
393
394 isFocus = (path) => {
395 if (!path) return false
396 const focusFieldPath = this.getField('data.other.focusFieldPath')
397 if (!focusFieldPath) return false
398 return path.replace(/\s/g, '') == focusFieldPath.replace(/\s/g, '')
399 }
400
401 getDirectFuns = () => {
402 return {
403 getMeta: (...args) => {
404 return this.getMeta(...args)
405 },
406 getField: (fieldPath) => {
407 return this.getField(fieldPath)
408 },
409 gm: (...args) => {
410 return this.getMeta(...args)
411 },
412 gf: (fieldPath) => {
413 return this.getField(fieldPath)
414 },
415 }
416 }
417
418 toast = (...args) => {
419 const Toast = config.getToast()
420 if(Toast){
421 Toast.destroy()
422 }
423 if (!Toast || args.length == 0 || !Toast[args[0]]) return
424 if (Toast) {
425 Toast[args[0]](...args.slice(1))
426 }
427 }
428
429 alert = (...args) => {
430 const Alert = config.getAlert()
431 if (!Alert || args.length == 0 || !Alert[args[0]]) return
432 Alert[args[0]](...args.slice(1))
433 }
434
435 clearToast = (Toast) => {
436 window.setTimeout(function(){
437
438 },0)
439 }
440
441 notification = (...args) => {
442 const Notification = config.getNotification()
443 if (!Notification || args.length == 0 || !Notification[args[0]]) return
444 Notification[args[0]](...args.slice(1))
445 }
446
447 popconfirm = (...args) => {
448 const PopConfirm = config.getPopconfirm()
449 if (!PopConfirm || args.length == 0 || !PopConfirm[args[0]]) return
450 return PopConfirm[args[0]](...args.slice(1))
451 }
452
453 modal = (...args) => {
454 const Modal = config.getModal()
455 if (!Modal || args.length == 0 || !Modal[args[0]]) return
456 return Modal[args[0]](...args.slice(1))
457 }
458
459 loadApp = (name, props) => {
460 return <AppLoader {...props} name={name} />
461 }
462
463 gm = this.getMeta
464
465 sm = this.setMeta
466
467 gf = this.getField
468
469 gfs = this.getFields
470
471 sf = this.setField
472
473 sfs = this.setFields
474
475 findPathByEvent = utils.path.findPathByEvent
476
477 stringToMoment = utils.moment.stringToMoment
478
479 momentToString = utils.moment.momentToString
480
481 fromJS = fromJS
482
483 context = contextManager
484}
485
486export default function creator(option) {
487 return new action(option)
488}
489
\No newline at end of file