oldj 8 years ago
parent
commit
a771a14257

File diff suppressed because it is too large
+ 251 - 1
app/build/bundle.js


+ 7 - 7
app/index.html

@@ -7,14 +7,14 @@
 <body>
 <div id="app"></div>
 <script>
-	const IS_DEV = process.env.ENV === 'dev';
-	const SH_event = require('./ui/event').event;
-	const SH_Agent = require('./ui/agent');
-	const {ipcRenderer} = require('electron');
-    const notifier = require('node-notifier');
-	const platform = process.platform;
+  const IS_DEV = process.env.ENV === 'dev'
+  const SH_event = require('./ui/event').event
+  const SH_Agent = require('./ui/agent')
+  const {ipcRenderer} = require('electron')
+  const notifier = require('node-notifier')
+  const platform = process.platform
 </script>
 <!--<script src="build/bundle.js"></script>-->
-<script>require('./build/bundle');</script>
+<script>require('./build/bundle')</script>
 </body>
 </html>

+ 6 - 2
app/package.json

@@ -8,7 +8,10 @@
     "type": "git",
     "url": "git+https://github.com/oldj/SwitchHosts.git"
   },
-  "keywords": ["SwitchHosts!", "host"],
+  "keywords": [
+    "SwitchHosts!",
+    "host"
+  ],
   "author": "oldj",
   "license": "MIT",
   "bugs": {
@@ -26,7 +29,8 @@
     "react-dom": "^15.3.1",
     "request": "^2.79.0",
     "sortablejs": "^1.5.1",
+    "wheel-js": "0.0.2",
     "yargs": "^6.6.0"
   },
   "devDependencies": {}
-}
+}

+ 114 - 105
app/ui/components/app.js

@@ -3,118 +3,127 @@
  * @blog http://oldj.net
  */
 
-'use strict';
+'use strict'
 
-import React from 'react';
-import Panel from './panel/panel';
-import Content from './content/content';
-import SudoPrompt from './frame/sudo';
-import EditPrompt from './frame/edit';
-import PreferencesPrompt from './frame/preferences';
-import './app.less';
+import React from 'react'
+import Panel from './panel/panel'
+import Content from './content/content'
+import SudoPrompt from './frame/sudo'
+import EditPrompt from './frame/edit'
+import PreferencesPrompt from './frame/preferences'
+import util from '../libs/util'
+import './app.less'
 
 class App extends React.Component {
-    constructor(props) {
-        super(props);
-
-        let _data = SH_Agent.getHosts();
-
-        this.state = {
-            hosts: _data,
-            current: _data.sys
-        };
-
-        SH_event.on('after_apply', () => {
-            if (this.state.current.is_sys) {
-                // 重新读取
-                this.setState({
-                    current: SH_Agent.getSysHosts()
-                });
-            }
-        });
-
-        ipcRenderer.on('to_import', (e, fn) => {
-            if (!confirm(SH_Agent.lang.confirm_import)) return;
-
-            SH_Agent.readFile(fn, (err, cnt) => {
-                if (err) {
-                    alert(err.message || 'Import Error!');
-                    return;
-                }
-                let data;
-                try {
-                    data = JSON.parse(cnt);
-                } catch (e) {
-                    console.log(e);
-                    alert(e.message || 'Bad format, the import file should be a JSON file.');
-                    return;
-                }
-
-                if (!data.list || !Array.isArray(data.list)) {
-                    alert('Bad format, the data JSON should have a [list] field.');
-                    return;
-                }
-
-                this.setState({
-                    hosts: Object.assign({}, this.state.hosts, {list: data.list})
-                }, () => {
-                    SH_event.emit('imported');
-                });
-                console.log('imported.');
-            })
-        });
-
-        ipcRenderer.send('reg_renderer');
-    }
+  constructor (props) {
+    super(props)
 
-    setCurrent(host) {
-        this.setState({
-            current: host.is_sys ? SH_Agent.getSysHosts() : host
-        });
-    }
+    let _data = SH_Agent.getHosts()
 
-    static isReadOnly(host) {
-        return host.is_sys || host.where == 'remote';
+    this.state = {
+      hosts: _data,
+      current: _data.sys
     }
 
-    toSave() {
-        clearTimeout(this._t);
-
-        this._t = setTimeout(() => {
-            SH_event.emit('change');
-        }, 1000);
-    }
-
-    setHostContent(v) {
-        if (this.state.current.content == v) return; // not changed
-
-        this.state.current.content = v || '';
-        this.toSave();
-    }
-
-    componentDidMount() {
-        window.addEventListener('keydown', (e) => {
-            if (e.keyCode === 27) {
-                SH_event.emit('esc');
-            }
-        }, false);
-    }
+    SH_event.on('after_apply', () => {
+      if (this.state.current.is_sys) {
+        // 重新读取
+        this.setState({
+          current: SH_Agent.getSysHosts()
+        })
+      }
+    })
+
+    ipcRenderer.on('to_import', (e, fn) => {
+      if (!confirm(SH_Agent.lang.confirm_import)) return
+
+      SH_Agent.readFile(fn, (err, cnt) => {
+        if (err) {
+          alert(err.message || 'Import Error!')
+          return
+        }
+        let data
+        try {
+          data = JSON.parse(cnt)
+        } catch (e) {
+          console.log(e)
+          alert(
+            e.message || 'Bad format, the import file should be a JSON file.')
+          return
+        }
+
+        if (!data.list || !Array.isArray(data.list)) {
+          alert('Bad format, the data JSON should have a [list] field.')
+          return
+        }
+
+        data.list.map(item => {
+          if (!item.id) {
+            item.id = util.makeId()
+          }
+        })
 
-    render() {
-        let current = this.state.current;
-        return (
-            <div id="app" className={'platform-' + platform}>
-                <Panel hosts={this.state.hosts} current={current} setCurrent={this.setCurrent.bind(this)}/>
-                <Content current={current} readonly={App.isReadOnly(current)}
-                         setHostContent={this.setHostContent.bind(this)}/>
-                <div className="frames">
-                    <SudoPrompt/>
-                    <EditPrompt hosts={this.state.hosts}/>
-                    <PreferencesPrompt/>
-                </div>
-            </div>
-        );
-    }
+        this.setState({
+          hosts: Object.assign({}, this.state.hosts, {list: data.list})
+        }, () => {
+          SH_event.emit('imported')
+        })
+        console.log('imported.')
+      })
+    })
+
+    ipcRenderer.send('reg_renderer')
+  }
+
+  setCurrent (host) {
+    this.setState({
+      current: host.is_sys ? SH_Agent.getSysHosts() : host
+    })
+  }
+
+  static isReadOnly (host) {
+    return host.is_sys || host.where === 'remote'
+  }
+
+  toSave () {
+    clearTimeout(this._t)
+
+    this._t = setTimeout(() => {
+      SH_event.emit('change')
+    }, 1000)
+  }
+
+  setHostContent (v) {
+    if (this.state.current.content === v) return // not changed
+
+    this.state.current.content = v || ''
+    this.toSave()
+  }
+
+  componentDidMount () {
+    window.addEventListener('keydown', (e) => {
+      if (e.keyCode === 27) {
+        SH_event.emit('esc')
+      }
+    }, false)
+  }
+
+  render () {
+    let current = this.state.current
+    return (
+      <div id="app" className={'platform-' + platform}>
+        <Panel hosts={this.state.hosts} current={current}
+               setCurrent={this.setCurrent.bind(this)}/>
+        <Content current={current} readonly={App.isReadOnly(current)}
+                 setHostContent={this.setHostContent.bind(this)}/>
+        <div className="frames">
+          <SudoPrompt/>
+          <EditPrompt hosts={this.state.hosts}/>
+          <PreferencesPrompt/>
+        </div>
+      </div>
+    )
+  }
 }
 
-export default App;
+export default App

+ 3 - 0
app/ui/components/frame/edit.js

@@ -9,6 +9,7 @@ import React from 'react'
 import MyFrame from './frame'
 import classnames from 'classnames'
 import Group from './group'
+import util from '../../libs/util'
 import './edit.less'
 
 export default class EditPrompt extends React.Component {
@@ -103,6 +104,8 @@ export default class EditPrompt extends React.Component {
         on: false,
       } : {})
 
+    if (!data.id) data.id = util.makeId()
+
     delete data['add']
     SH_event.emit('host_' + (this.state.add ? 'add' : 'edit') + 'ed', data,
       this.current_host)

+ 32 - 15
app/ui/components/frame/group.js

@@ -8,6 +8,7 @@
 import React from 'react'
 import classnames from 'classnames'
 import Sortable from 'sortablejs'
+import listToArray from 'wheel-js/src/common/list-to-array'
 import './group.less'
 
 export default class Group extends React.Component {
@@ -21,21 +22,28 @@ export default class Group extends React.Component {
     this.current_host = null
   }
 
+  makeItem (item) {
+    let attrs = {
+      'data-id': 'id:' + (item.id || '')
+    }
+    return (
+      <div className="hosts-item" {...attrs}>
+        <i className={classnames({
+          'iconfont': 1
+          , 'item-icon': 1
+          , 'icon-file': item.where !== 'group'
+          , 'icon-files': item.where === 'group'
+        })}
+        />
+        <span>{item.title}</span>
+      </div>
+    )
+  }
+
   makeList () {
-    let items = this.state.list.map(item => {
-      return (
-        <div className="host-item">
-          <i className={classnames({
-            'iconfont': 1
-            , 'item-icon': 1
-            , 'icon-file': item.where !== 'group'
-            , 'icon-files': item.where === 'group'
-          })}
-          />
-          <span>{item.title}</span>
-        </div>
-      )
-    })
+    let items = this.state.list
+      .filter(item => item.where !== 'group')
+      .map(item => this.makeItem(item))
 
     return (
       <div id="hosts-group-valid">
@@ -54,6 +62,12 @@ export default class Group extends React.Component {
     )
   }
 
+  getCurrentListFromDOM () {
+    let nodes = this.refs.group_current.getElementsByClassName('hosts-item')
+    nodes = listToArray(nodes)
+    console.log(nodes)
+  }
+
   componentDidMount () {
     Sortable.create(this.refs.group_valid, {
       group: 'sorting'
@@ -63,6 +77,9 @@ export default class Group extends React.Component {
     Sortable.create(this.refs.group_current, {
       group: 'sorting'
       , sort: true
+      , onSort: evt => {
+        this.getCurrentListFromDOM()
+      }
     })
   }
 
@@ -70,7 +87,7 @@ export default class Group extends React.Component {
     return (
       <div id="hosts-group">
         {this.makeList()}
-        <div className="arrow"></div>
+        <div className="arrow"/>
         {this.currentList()}
       </div>
     )

+ 2 - 2
app/ui/components/frame/group.less

@@ -29,9 +29,9 @@
   .hosts-group-list {
     height: @h;
     overflow: auto;
-    border: solid 1px #999;
+    border: solid 1px #ccc;
 
-    .host-item {
+    .hosts-item {
       cursor: move;
       padding: 2px 6px;
 

+ 7 - 3
app/ui/libs/util.js

@@ -4,8 +4,12 @@
  * @blog http://oldj.net
  */
 
-'use strict';
+'use strict'
 
 exports.formatVersion = (v) => {
-    return 'v' + v.slice(0, 3).join('.') + ` (${v[3]})`;
-};
+  return 'v' + v.slice(0, 3).join('.') + ` (${v[3]})`
+}
+
+exports.makeId = () => {
+  return (new Date()).getTime() + '-' + Math.floor(Math.random() * 1e6)
+}

+ 1 - 1
package.json

@@ -8,7 +8,7 @@
     "start": "electron app",
     "dev": "ENV=dev electron app",
     "build": "gulp ver && webpack -p",
-    "build-w": "webpack -pw",
+    "build-w": "webpack -w",
     "pack": "gulp pack",
     "pack-mac": "gulp pack --platform=macOS",
     "pack-win": "gulp pack --platform=win64",

+ 6 - 6
webpack.config.js

@@ -44,13 +44,13 @@ module.exports = {
             'process.env': {
                 NODE_ENV: JSON.stringify('production')
             }
-        }),
-        new webpack.optimize.UglifyJsPlugin({
-            sourceMap: true,
-            compress: {
-                warnings: false
-            }
         })
+        //, new webpack.optimize.UglifyJsPlugin({
+        //    sourceMap: true,
+        //    compress: {
+        //        warnings: false
+        //    }
+        //})
         , new webpack.IgnorePlugin(new RegExp("^(electron|fs|path)$"))
     ]
 };

Some files were not shown because too many files changed in this diff