Browse Source

group 内容更新初步完成。

oldj 8 years ago
parent
commit
aebf250817

+ 105 - 12
app/bundle.js

@@ -3453,6 +3453,10 @@ var _Agent = __webpack_require__(5);
 
 var _Agent2 = _interopRequireDefault(_Agent);
 
+var _cleanData = __webpack_require__(235);
+
+var _cleanData2 = _interopRequireDefault(_cleanData);
+
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
 module.exports = function (app, hosts) {
@@ -3467,8 +3471,19 @@ module.exports = function (app, hosts) {
     list.splice(idx, 1, Object.assign({}, old_hosts, hosts));
   }
 
+  list = (0, _cleanData2.default)(list);
+
   _Agent2.default.pact('saveHosts', list).then(function () {
-    app.setState({ list: list }, function () {
+    var state = { list: list };
+    var current = app.state.current;
+    var item = list.find(function (i) {
+      return i.id === current.id;
+    });
+    if (item) {
+      state.current = item;
+    }
+
+    app.setState(state, function () {
       _Agent2.default.emit('select', hosts.id);
     });
   }).catch(function (e) {
@@ -21595,7 +21610,8 @@ var EditPrompt = function (_React$Component) {
       url: '',
       last_refresh: null,
       refresh_interval: 0,
-      is_loading: false
+      is_loading: false,
+      include: []
     };
 
     _this.current_hosts = null;
@@ -21636,6 +21652,9 @@ var EditPrompt = function (_React$Component) {
 
       _Agent2.default.on('edit_hosts', function (hosts) {
         _this2.current_hosts = hosts;
+        var include = hosts.include || [];
+        include = Array.from(new Set(include));
+
         _this2.setState({
           show: true,
           is_add: false,
@@ -21643,7 +21662,8 @@ var EditPrompt = function (_React$Component) {
           title: hosts.title || '',
           url: hosts.url || '',
           last_refresh: hosts.last_refresh || null,
-          refresh_interval: hosts.refresh_interval || 0
+          refresh_interval: hosts.refresh_interval || 0,
+          include: include
         });
         setTimeout(function () {
           _this2.tryToFocus();
@@ -21714,6 +21734,11 @@ var EditPrompt = function (_React$Component) {
       });
       this.clear();
     }
+  }, {
+    key: 'updateInclude',
+    value: function updateInclude(include) {
+      this.setState({ include: include });
+    }
   }, {
     key: 'getRefreshOptions',
     value: function getRefreshOptions() {
@@ -21787,7 +21812,11 @@ var EditPrompt = function (_React$Component) {
     value: function renderGroup() {
       if (this.state.where !== 'group') return null;
 
-      return _react2.default.createElement(_group2.default, { list: this.props.list });
+      return _react2.default.createElement(_group2.default, {
+        list: this.props.list,
+        include: this.state.include,
+        updateInclude: this.updateInclude.bind(this)
+      });
     }
   }, {
     key: 'renderRemoteInputs',
@@ -22030,10 +22059,12 @@ var Group = function (_React$Component) {
     var _this = _possibleConstructorReturn(this, (Group.__proto__ || Object.getPrototypeOf(Group)).call(this, props));
 
     _this.state = {
-      list: []
+      list: [],
+      include: []
     };
 
     _this.current_hosts = null;
+    _this.ids = [];
     return _this;
   }
 
@@ -22065,8 +22096,9 @@ var Group = function (_React$Component) {
     value: function makeList() {
       var _this2 = this;
 
+      var include = this.state.include;
       var items = this.state.list.filter(function (item) {
-        return item.where !== 'group';
+        return item.where !== 'group' && !include.includes(item.id);
       }).map(function (item) {
         return _this2.makeItem(item);
       });
@@ -22084,30 +22116,52 @@ var Group = function (_React$Component) {
   }, {
     key: 'currentList',
     value: function currentList() {
+      var _this3 = this;
+
+      var list = this.state.list;
+      var items = this.state.include.map(function (id) {
+        return list.find(function (item) {
+          return item.id === id;
+        });
+      }).map(function (item) {
+        return _this3.makeItem(item);
+      });
+
       return _react2.default.createElement(
         'div',
         { id: 'hosts-group-current' },
-        _react2.default.createElement('div', { ref: 'group_current', className: 'hosts-group-list' })
+        _react2.default.createElement(
+          'div',
+          { ref: 'group_current', className: 'hosts-group-list' },
+          items
+        )
       );
     }
   }, {
     key: 'getCurrentListFromDOM',
     value: function getCurrentListFromDOM() {
+      var updateInclude = this.props.updateInclude;
+
       var nodes = this.refs.group_current.getElementsByClassName('hosts-item');
       nodes = (0, _listToArray2.default)(nodes);
-      console.log(nodes);
+      var ids = nodes.map(function (item) {
+        return item.getAttribute('data-id');
+      });
+      this.ids = ids;
+      updateInclude(ids);
     }
   }, {
     key: 'componentWillMount',
     value: function componentWillMount() {
       this.setState({
-        list: this.props.list
+        list: this.props.list,
+        include: this.props.include
       });
     }
   }, {
     key: 'componentDidMount',
     value: function componentDidMount() {
-      var _this3 = this;
+      var _this4 = this;
 
       _sortablejs2.default.create(this.refs.group_valid, {
         group: 'sorting',
@@ -22117,8 +22171,8 @@ var Group = function (_React$Component) {
       _sortablejs2.default.create(this.refs.group_current, {
         group: 'sorting',
         sort: true,
-        onSort: function onSort(evt) {
-          _this3.getCurrentListFromDOM();
+        onSort: function onSort() {
+          _this4.getCurrentListFromDOM();
         }
       });
     }
@@ -35294,6 +35348,45 @@ module.exports = function (app, on, on_ids, callback) {
   });
 };
 
+/***/ }),
+/* 235 */
+/***/ (function(module, exports, __webpack_require__) {
+
+"use strict";
+/**
+ * @author oldj
+ * @blog https://oldj.net
+ */
+
+
+
+function makeGroupContent(item, list) {
+  return (item.include || []).map(function (id) {
+    return (list.find(function (i) {
+      return i.id === id;
+    }) || {}).content || '';
+  }).join('\n\n');
+}
+
+module.exports = function (list) {
+  return list.map(function (item) {
+    var new_item = {};
+
+    var valid_keys = ['id', 'title', 'content', 'on', 'where', 'last_refresh', 'url', 'include'];
+    valid_keys.map(function (k) {
+      if (item.hasOwnProperty(k)) {
+        new_item[k] = item[k];
+      }
+    });
+
+    if (new_item.where === 'group') {
+      new_item.content = makeGroupContent(new_item, list);
+    }
+
+    return new_item;
+  });
+};
+
 /***/ })
 /******/ ]);
 //# sourceMappingURL=bundle.js.map

+ 1 - 1
app/server/actions/saveHosts.js

@@ -36,7 +36,7 @@ function tryToApply (svr, cnt, pswd) {
 }
 
 module.exports = (svr, list) => {
-  list = cleanData(list)
+  //list = cleanData(list)
 
   let fn = paths.data_path
   let data = {

+ 11 - 1
app/server/cleanData.js

@@ -5,12 +5,18 @@
 
 'use strict'
 
+function makeGroupContent(item, list) {
+  return (item.include || []).map(id => {
+    return (list.find(i => i.id === id) || {}).content || ''
+  }).join('\n\n')
+}
+
 module.exports = (list) => {
   return list.map(item => {
     let new_item = {}
 
     let valid_keys = [
-      'id', 'title', 'content', 'on', 'where', 'last_refresh', 'url'
+      'id', 'title', 'content', 'on', 'where', 'last_refresh', 'url', 'include'
     ]
     valid_keys.map(k => {
       if (item.hasOwnProperty(k)) {
@@ -18,6 +24,10 @@ module.exports = (list) => {
       }
     })
 
+    if (new_item.where === 'group') {
+      new_item.content = makeGroupContent(new_item, list)
+    }
+
     return new_item
   })
 }

+ 3 - 1
app/server/makeOutHosts.js

@@ -1,11 +1,13 @@
 /**
  * @author oldj
  * @blog https://oldj.net
+ *
+ * 输出 hosts,提供给系统等应用
  */
 
 'use strict'
 
-function getHostsContent(item, list) {
+function getHostsContent(item) {
   return item.content || ''
 }
 

+ 1 - 1
app/version.js

@@ -1 +1 @@
-exports.version = [3,3,0,4399];
+exports.version = [3,3,0,4412];

+ 11 - 1
ui/events/update_hosts.js

@@ -6,6 +6,7 @@
 'use strict'
 
 import Agent from '../Agent'
+import cleanData from '../../app/server/cleanData'
 
 module.exports = (app, hosts) => {
   let list = app.state.list.slice(0)
@@ -17,9 +18,18 @@ module.exports = (app, hosts) => {
     list.splice(idx, 1, Object.assign({}, old_hosts, hosts))
   }
 
+  list = cleanData(list)
+
   Agent.pact('saveHosts', list)
     .then(() => {
-      app.setState({list}, () => {
+      let state = {list}
+      let current = app.state.current
+      let item = list.find(i => i.id === current.id)
+      if (item) {
+        state.current = item
+      }
+
+      app.setState(state, () => {
         Agent.emit('select', hosts.id)
       })
     })

+ 16 - 3
ui/frame/edit.js

@@ -25,7 +25,8 @@ export default class EditPrompt extends React.Component {
       url: '',
       last_refresh: null,
       refresh_interval: 0,
-      is_loading: false
+      is_loading: false,
+      include: []
     }
 
     this.current_hosts = null
@@ -59,6 +60,9 @@ export default class EditPrompt extends React.Component {
 
     Agent.on('edit_hosts', (hosts) => {
       this.current_hosts = hosts
+      let include = hosts.include || []
+      include = Array.from(new Set(include))
+
       this.setState({
         show: true,
         is_add: false,
@@ -66,7 +70,8 @@ export default class EditPrompt extends React.Component {
         title: hosts.title || '',
         url: hosts.url || '',
         last_refresh: hosts.last_refresh || null,
-        refresh_interval: hosts.refresh_interval || 0
+        refresh_interval: hosts.refresh_interval || 0,
+        include
       })
       setTimeout(() => {
         this.tryToFocus()
@@ -135,6 +140,10 @@ export default class EditPrompt extends React.Component {
     this.clear()
   }
 
+  updateInclude (include) {
+    this.setState({include})
+  }
+
   getRefreshOptions () {
     let {lang} = this.props
     let k = [
@@ -191,7 +200,11 @@ export default class EditPrompt extends React.Component {
   renderGroup () {
     if (this.state.where !== 'group') return null
 
-    return <Group list={this.props.list}/>
+    return <Group
+      list={this.props.list}
+      include={this.state.include}
+      updateInclude={this.updateInclude.bind(this)}
+    />
   }
 
   renderRemoteInputs () {

+ 20 - 6
ui/frame/group.js

@@ -16,10 +16,12 @@ export default class Group extends React.Component {
     super(props)
 
     this.state = {
-      list: []
+      list: [],
+      include: []
     }
 
     this.current_hosts = null
+    this.ids = []
   }
 
   makeItem (item) {
@@ -41,8 +43,9 @@ export default class Group extends React.Component {
   }
 
   makeList () {
+    let include = this.state.include
     let items = this.state.list
-      .filter(item => item.where !== 'group')
+      .filter(item => item.where !== 'group' && !include.includes(item.id))
       .map(item => this.makeItem(item))
 
     return (
@@ -55,22 +58,33 @@ export default class Group extends React.Component {
   }
 
   currentList () {
+    let list = this.state.list
+    let items = this.state.include
+      .map(id => list.find(item => item.id === id))
+      .map(item => this.makeItem(item))
+
     return (
       <div id="hosts-group-current">
-        <div ref="group_current" className="hosts-group-list"></div>
+        <div ref="group_current" className="hosts-group-list">
+          {items}
+        </div>
       </div>
     )
   }
 
   getCurrentListFromDOM () {
+    let {updateInclude} = this.props
     let nodes = this.refs.group_current.getElementsByClassName('hosts-item')
     nodes = listToArray(nodes)
-    console.log(nodes)
+    let ids = nodes.map(item => item.getAttribute('data-id'))
+    this.ids = ids
+    updateInclude(ids)
   }
 
   componentWillMount () {
     this.setState({
-      list: this.props.list
+      list: this.props.list,
+      include: this.props.include
     })
   }
 
@@ -83,7 +97,7 @@ export default class Group extends React.Component {
     Sortable.create(this.refs.group_current, {
       group: 'sorting'
       , sort: true
-      , onSort: evt => {
+      , onSort: () => {
         this.getCurrentListFromDOM()
       }
     })

+ 2 - 3
ui/package.json

@@ -14,7 +14,6 @@
     "react": "^15.4.2",
     "react-dom": "^15.4.2",
     "sortablejs": "^1.5.1",
-    "wheel-js": "0.0.3"
-  },
-  "dependencies": {}
+    "wheel-js": "0.0.4"
+  }
 }