Browse Source

为dashboard.js准备方法

xiaoct 7 years ago
parent
commit
ffe6348eed
3 changed files with 174 additions and 16 deletions
  1. 116 0
      src/demo.jsx
  2. 56 16
      src/models/dashboard.js
  3. 2 0
      src/routes/mainLayout.js

+ 116 - 0
src/demo.jsx

@@ -0,0 +1,116 @@
+import React from 'react'
+import { Tree } from 'antd';
+
+const TreeNode = Tree.TreeNode;
+
+const x = 3;
+const y = 2;
+const z = 1;
+const gData = [];
+
+const generateData = (_level, _preKey, _tns) => {
+  const preKey = _preKey || '0';
+  const tns = _tns || gData;
+
+  const children = [];
+  for (let i = 0; i < x; i++) {
+    const key = `${preKey}-${i}`;
+    tns.push({ title: key, key });
+    if (i < y) {
+      children.push(key);
+    }
+  }
+  if (_level < 0) {
+    return tns;
+  }
+  const level = _level - 1;
+  children.forEach((key, index) => {
+    tns[index].children = [];
+    return generateData(level, key, tns[index].children);
+  });
+};
+generateData(z);
+
+class Demo extends React.Component {
+  state = {
+    gData,
+    expandedKeys: ['0-0', '0-0-0', '0-0-0-0'],
+  }
+
+  onDragEnter = (info) => {
+    console.log(info);
+    // expandedKeys 需要受控时设置
+    // this.setState({
+    //   expandedKeys: info.expandedKeys,
+    // });
+  }
+
+  onDrop = (info) => {
+    console.log(info);
+    const dropKey = info.node.props.eventKey;
+    const dragKey = info.dragNode.props.eventKey;
+    const dropPos = info.node.props.pos.split('-');
+    const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
+    // const dragNodesKeys = info.dragNodesKeys;
+    const loop = (data, key, callback) => {
+      data.forEach((item, index, arr) => {
+        if (item.key === key) {
+          return callback(item, index, arr);
+        }
+        if (item.children) {
+          return loop(item.children, key, callback);
+        }
+      });
+    };
+    const data = [...this.state.gData];
+    let dragObj;
+    loop(data, dragKey, (item, index, arr) => {
+      arr.splice(index, 1);
+      dragObj = item;
+    });
+    if (info.dropToGap) {
+      let ar;
+      let i;
+      loop(data, dropKey, (item, index, arr) => {
+        ar = arr;
+        i = index;
+      });
+      if (dropPosition === -1) {
+        ar.splice(i, 0, dragObj);
+      } else {
+        ar.splice(i + 1, 0, dragObj);
+      }
+    } else {
+      loop(data, dropKey, (item) => {
+        item.children = item.children || [];
+        // where to insert 示例添加到尾部,可以是随意位置
+        item.children.push(dragObj);
+      });
+    }
+    this.setState({
+      gData: data,
+    });
+  }
+
+  render() {
+    const loop = data => data.map((item) => {
+      if (item.children && item.children.length) {
+        return <TreeNode key={item.key} title={item.title}>{loop(item.children)}</TreeNode>;
+      }
+      return <TreeNode draggable="false" key={item.key} title={item.title} />;
+    });
+    return (
+      <Tree
+        className="draggable-tree"
+        defaultExpandedKeys={this.state.expandedKeys}
+        draggable
+        onDragEnter={this.onDragEnter}
+        onDrop={this.onDrop}
+      >
+        {loop(this.state.gData)}
+      </Tree>
+    );
+  }
+}
+
+export default Demo

+ 56 - 16
src/models/dashboard.js

@@ -1,8 +1,12 @@
+import { message } from 'antd'
+import * as service from '../services/index'
+import URLS from '../constants/url'
+
 export default {
     namespace: 'dashboard',
     state: {
         currentDashboard: {},
-        myDynamicDashboardList: [{            //Dynamic Dashboard指看板(动态)
+        dashboardList: [{            //Dynamic Dashboard指看板(动态) Static Dashboard指报告(静态)
             dashboardID: 1,
             type: 'dynamic',
             title: 'Card 1',
@@ -11,9 +15,8 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar: '',
-            dashboardConfig: {}
-            
-    
+            dashboardConfig: {},
+            creator: {}
         },{
             dashboardID: 2,
             type: 'dynamic',
@@ -23,8 +26,8 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar:{},
-            dashboardConfig: {}
-    
+            dashboardConfig: {},
+            creator: {}
         },{
             dashboardID: 3,
             type: 'dynamic',
@@ -34,7 +37,8 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar:{},
-            dashboardConfig: {}
+            dashboardConfig: {},
+            creator: {}
         },{
             dashboardID: 6,
             type: 'dynamic',
@@ -44,7 +48,8 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar:{},
-            dashboardConfig: {}
+            dashboardConfig: {},
+            creator: {}
         },{
             dashboardID: 7,
             type: 'dynamic',
@@ -54,10 +59,9 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar:{},
-            dashboardConfig: {}
-        }
-        ],
-        myStaticDashboardList: [{            //Static Dashboard指报告(静态)
+            dashboardConfig: {},
+            creator: {}
+        },{
             dashboardID: 4,
             type: 'static',
             title: 'Card 4',
@@ -66,7 +70,8 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar:{},
-            dashboardConfig: {}
+            dashboardConfig: {},
+            creator: {}
         },{
             dashboardID: 5,
             type: 'static',
@@ -76,10 +81,45 @@ export default {
             coverImg:'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png',
             coverAlt: '',
             avatar:{},
-            dashboardConfig: {}
-        
+            dashboardConfig: {},
+            creator: {}
         }
         ],
-                    
+    },
+    reducers: {
+        list(state, action) {
+            let data = action.data;
+            return Object.assign({}, state, {dashboardList: data});
+        }
+    },
+    effets: {
+        *fetchList(action, {select, call, put}) {
+            try {
+                const dashboard = yield select(state => state.present.dashboard)
+                if(!action.mandatory && dashboard.list.length > 0) {
+                    return;
+                }
+                const response = yield call(service.fetch, {
+                    url: "127.0.0.1:5000/dashboard",
+                    method: 'GET',
+                    body: {}
+                });
+                if(!response.err && response.data.code > 0) {
+                    let data = response
+                }
+            }catch(e) {
+                message.error('读取报告与看板错误')
+            }
+        }
+    },
+    subscriptions: {
+        setup({ dispatch, history}) {
+            return history.listen(({pathname}) => {
+                if (pathname === '/dashboard') {
+                    dispatch({ type: 'fetchList'});
+                }
+            }
+        )
+        }
     }
 }

+ 2 - 0
src/routes/mainLayout.js

@@ -9,6 +9,7 @@ import DataSourceDetail from '../components/datasource/dataSourceDetail'
 import Dashboard from '../components/dashboard/dashboard'
 import Chart from '../components/chart/list'
 import './mainLayout.less';
+import Demo from '../demo';
 const { Header, Content } = Layout
 
 const MainLayout = (history) => {
@@ -20,6 +21,7 @@ const MainLayout = (history) => {
             </Header>
             <Content className='main-content'>
                 <Switch>
+                    <Route exact path='/demo' component={Demo}/>
                     <Route exact path='/' component={Welcome}/>
                     <Route exact path='/home' component={Welcome}/>
                     <Route exact sensitive path='/datasource' component={DataSource}/>