| | |
| | | html: '', |
| | | } |
| | | |
| | | loaded = false |
| | | |
| | | UNSAFE_componentWillMount () { |
| | | const { data, initdata } = this.props |
| | | let _config = fromJS(this.props.config).toJS() |
| | |
| | | |
| | | if (_sync && data) { |
| | | _data = data[_config.dataName] || {} |
| | | if (_data && Array.isArray(_data)) { |
| | | _data = _data[0] || {} |
| | | } |
| | | _sync = false |
| | | this.loaded = true |
| | | } else if (_sync && initdata) { |
| | | _data = initdata || {} |
| | | if (_data && Array.isArray(_data)) { |
| | | _data = _data[0] || {} |
| | | } |
| | | _sync = false |
| | | this.loaded = true |
| | | } |
| | | } else { |
| | | _data = {} |
| | | this.loaded = true |
| | | } |
| | | |
| | | if (_config.css) { |
| | | let node = document.getElementById(_config.uuid) |
| | | node && node.remove() |
| | | |
| | | let ele = document.createElement('style') |
| | | ele.id = _config.uuid |
| | | ele.innerHTML = _config.css |
| | | document.getElementsByTagName('head')[0].appendChild(ele) |
| | | } |
| | |
| | | } |
| | | |
| | | componentDidMount () { |
| | | const { config } = this.state |
| | | |
| | | MKEmitter.addListener('reloadData', this.reloadData) |
| | | |
| | | if (config.$cache && !this.loaded) { |
| | | Api.getLCacheConfig(config.uuid).then(res => { |
| | | if (!res || this.loaded) return |
| | | |
| | | this.setState({data: res}, () => { |
| | | this.renderView() |
| | | }) |
| | | }) |
| | | } |
| | | } |
| | | |
| | | shouldComponentUpdate (nextProps, nextState) { |
| | |
| | | let _data = {} |
| | | if (nextProps.data && nextProps.data[config.dataName]) { |
| | | _data = nextProps.data[config.dataName] |
| | | if (_data && Array.isArray(_data)) { |
| | | _data = _data[0] |
| | | } |
| | | } |
| | | |
| | | this.setState({sync: false, data: _data}, () => { |
| | | this.renderView() |
| | | }) |
| | | } else if (config.setting.syncRefresh && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { |
| | | this.loaded = true |
| | | |
| | | if (!is(fromJS(this.state.data), fromJS(_data))) { |
| | | setTimeout(() => { |
| | | this.renderView() |
| | | }, 10) |
| | | } |
| | | |
| | | this.setState({sync: false, data: _data}) |
| | | } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { |
| | | this.setState({}, () => { |
| | | this.loadData() |
| | | }) |
| | |
| | | data: {}, |
| | | loading: false |
| | | }) |
| | | this.loaded = true |
| | | return |
| | | } |
| | | |
| | |
| | | |
| | | let result = await Api.genericInterface(param) |
| | | if (result.status) { |
| | | let _data = result.data && result.data[0] ? result.data[0] : {} |
| | | let _data = result.data || {} |
| | | |
| | | this.loaded = true |
| | | if (config.$cache && config.setting.onload !== 'false') { |
| | | Api.writeCacheConfig(config.uuid, result.data || '') |
| | | } |
| | | |
| | | if (!is(fromJS(this.state.data), fromJS(_data))) { |
| | | setTimeout(() => { |
| | | this.renderView() |
| | | }, 10) |
| | | } |
| | | |
| | | this.setState({ |
| | | data: _data, |
| | | loading: false |
| | | }, () => { |
| | | this.renderView() |
| | | }) |
| | | } else { |
| | | this.setState({ |
| | |
| | | const { html, js, wrap, columns } = this.state.config |
| | | |
| | | let _html = html |
| | | if (_html && wrap.datatype !== 'static') { |
| | | if (_html && wrap.datatype !== 'static' && wrap.compileMode !== 'custom') { |
| | | let _data = data |
| | | if (Array.isArray(_data)) { |
| | | _data = _data[0] || {} |
| | | } |
| | | columns.forEach(col => { |
| | | if (col.field) { |
| | | let val = (data[col.field] || data[col.field] === 0) ? data[col.field] : '' |
| | | let val = (_data[col.field] || _data[col.field] === 0) ? _data[col.field] : '' |
| | | let reg = new RegExp('@' + col.field + '@', 'ig') |
| | | |
| | | _html = _html.replace(reg, val) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | this.setState({html: _html}, () => { |
| | | if (js) { |
| | | try { |
| | | // eslint-disable-next-line no-eval |
| | | eval(js) |
| | | if (wrap.compileMode !== 'custom') { |
| | | // eslint-disable-next-line no-eval |
| | | eval(js) |
| | | } else { |
| | | // eslint-disable-next-line |
| | | let evalfunc = eval('(true && function (data) {' + js + '})') |
| | | evalfunc(data) |
| | | } |
| | | } catch (e) { |
| | | console.warn('JS 执行失败!') |
| | | console.warn(e) |
| | | } |
| | | } |
| | | }) |