factory.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import Container from './Layout.js';
  3. import Title from '../src/Title/Title.jsx';
  4. import MessageBox from '../src/MsgBox/MessageBox.jsx';
  5. import { converter } from './converter.js';
  6. import URL from '../constants/url.json';
  7. class Factory extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.index = 0;
  11. this.state = {
  12. titleHeight: 0,
  13. error: null
  14. };
  15. window.forceUpdate = this.forceUpdate.bind(this);
  16. }
  17. getModelConfig(mid) {
  18. let me = this;
  19. fetch(URL.path + mid, {
  20. method: 'POST',
  21. credentials: 'include'
  22. }).then(function (response) {
  23. return (response.json())
  24. }).then((json) => {
  25. if(!json.instance) {
  26. throw {message: json.message};
  27. }
  28. let instance = json.instance;
  29. if (!me.state.instance) {
  30. me.setState({
  31. instance: instance
  32. }, me.setRefresh);
  33. }
  34. return json.data[0];
  35. }).then(function (modelconfig) {
  36. if(!modelconfig.content) {
  37. throw {message: '该看板内容为空'}
  38. }
  39. me.setState({
  40. error: null,
  41. model: converter(modelconfig),
  42. });
  43. }).catch(function (ex) {
  44. me.setState({
  45. error: {message: ex.message}
  46. });
  47. console.log('parsing failed', ex);
  48. });
  49. }
  50. getTitleHeight() {
  51. let titleEl = document.getElementsByClassName('rc-title');
  52. let titleHeight = titleEl.length > 0 ? titleEl[0].offsetHeight : 0;
  53. titleHeight = titleHeight || 0;
  54. return titleHeight;
  55. }
  56. setTitleHeight(height) {
  57. this.setState({
  58. titleHeight: height
  59. });
  60. }
  61. setRefresh() {
  62. let { instance } = this.state;
  63. if (!instance) { return; }
  64. let codes = instance.enabledKanbanCodes;
  65. let display = instance.display;
  66. let next = {
  67. enable: instance.switchFrequency > 0 ? true : false,
  68. interval: instance.switchFrequency
  69. };
  70. let current = {
  71. enable: instance.refreshFrequency > 0 ? true : false,
  72. interval: instance.refreshFrequency
  73. };
  74. let refresh = {
  75. current: current,
  76. next: next
  77. };
  78. if (refresh.current) {
  79. if (refresh.current.enable) {
  80. this.refreshThis = setInterval(function () {
  81. if (this.index == codes.length - 1) {
  82. this.index = 0;
  83. } else {
  84. this.index++;
  85. }
  86. this.getModelConfig(this.props.code[0] + '?kanbanCode=' + codes[this.index]);
  87. }.bind(this), refresh.current.interval * 1000 || 10000)
  88. }
  89. }
  90. }
  91. onWindowResize() {
  92. this.forceUpdate();
  93. }
  94. componentDidUpdate() {
  95. }
  96. componentWillMount() {
  97. let { code } = this.props;
  98. this.getModelConfig(code[0]);
  99. }
  100. componentDidMount() {
  101. window.addEventListener('resize', this.onWindowResize);
  102. this.setState({
  103. titleHeight: this.getTitleHeight()
  104. });
  105. }
  106. componentWillUnmount() {
  107. window.removeEventListener('resize', this.onWindowResize);
  108. if (this.refreshThis) {
  109. if (this.refreshThis.interval > 0) {
  110. window.clearInterval(this.refreshThis);
  111. }
  112. }
  113. if (this.refreshNext) {
  114. if (this.refreshNext.interval > 0) {
  115. window.clearInterval(this.refreshNext);
  116. }
  117. }
  118. }
  119. componentWillReceiveProps(nextProps) {
  120. this.setState({
  121. titleHeight: this.getTitleHeight()
  122. });
  123. }
  124. render() {
  125. let { titleHeight, model, error } = this.state;
  126. if (this.state.error) {
  127. return <MessageBox static={this.props.static} titleHeight={titleHeight} error={error} />
  128. }
  129. if (!this.state.model) {
  130. return <div style={{color: 'white'}}>loading...</div>
  131. }
  132. const { title, content, fixedbox } = model;
  133. let titleConfig = title;
  134. let items = [];
  135. if (fixedbox) {
  136. items = fixedbox.items || [];
  137. }
  138. return (
  139. <div>
  140. <Title static={this.props.static} setTitleHeight={this.setTitleHeight.bind(this)} {...titleConfig} />
  141. <Container static={this.props.static} items={content.items} rowHeight={(window.innerHeight - titleHeight) / 10} />
  142. </div>
  143. );
  144. }
  145. };
  146. module.exports = Factory;