factory.dev.js 5.4 KB

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