Browse Source

Merge branch 'feature/auto-refresh' into develop

oldj 10 years ago
parent
commit
7a3d92c0a9

+ 1 - 0
app/SH3/MacGap/Classes/Commands/App.h

@@ -23,6 +23,7 @@
 JSExportAs(setUserAgent, - (void) setCustomUserAgent:(NSString *)userAgentString);
 - (void) openURL:(NSString*)url;
 - (void) launch:(NSString *)name;
+- (void) log:(NSString *)msg;
 @property (readonly) NSNumber* idleTime;
 @property (readonly) NSString* applicationPath;
 @property (readonly) NSString* resourcePath;

+ 3 - 0
app/SH3/MacGap/Classes/Commands/App.m

@@ -177,5 +177,8 @@
     return [NSNumber numberWithDouble:timeSinceLastEvent];
 }
 
+- (void)log:(NSString *)msg {
+    NSLog(@"%@", msg);
+}
 
 @end

+ 1 - 1
app/SH3/MacGap/SwitchHosts!-Info.plist

@@ -21,7 +21,7 @@
 	<key>CFBundleSignature</key>
 	<string>????</string>
 	<key>CFBundleVersion</key>
-	<string>2890</string>
+	<string>2950</string>
 	<key>LSApplicationCategoryType</key>
 	<string>public.app-category.developer-tools</string>
 	<key>LSMinimumSystemVersion</key>

+ 6 - 2
app/SH3/public/css/style.css

@@ -130,7 +130,7 @@ a:hover {
   left: 50%;
   background: #fff;
   z-index: 100;
-  width: 400px;
+  width: 420px;
   margin-left: -200px;
 }
 #edit-form h2,
@@ -156,7 +156,7 @@ a:hover {
 #pswd-form .body .ln input[type=text],
 #edit-form .body .ln input[type=password],
 #pswd-form .body .ln input[type=password] {
-  width: 200px;
+  width: 240px;
   padding: 6px 10px;
   outline: none;
 }
@@ -164,6 +164,10 @@ a:hover {
 #pswd-form .body .ln input[type=password] {
   letter-spacing: 8px;
 }
+#edit-form .body .ln .info,
+#pswd-form .body .ln .info {
+  color: #aaa;
+}
 #edit-form .body .ln .inform,
 #pswd-form .body .ln .inform {
   color: #f03;

+ 4 - 2
app/SH3/public/css/style.styl

@@ -160,7 +160,7 @@ unselectable()
   left 50%
   background #fff
   z-index 100
-  width 400px
+  width 420px
   margin-left -200px
 
   h2
@@ -182,13 +182,15 @@ unselectable()
         width 80px
       input[type=text],
       input[type=password]
-        width 200px
+        width 240px
         padding 6px 10px
         outline none
 
       input[type=password]
         letter-spacing 8px
 
+      .info
+        color #aaa
       .inform
         color color_danger
 

+ 20 - 6
app/SH3/public/index.html

@@ -27,7 +27,8 @@
 					v-dropzone="sort(hosts.list, $index, $droptag, $dropdata)"
 					:class="{selected:host==current_host}">
 					<!--<i class="switch" @click="switchHost(host)" :class="{'on':host.on}"></i>-->
-					<i class="iconfont switch" @click="switchHost(host)" :class="{'icon-on':host.on, 'icon-off':!host.on}"></i>
+					<i class="iconfont switch" @click="switchHost(host)"
+					   :class="{'icon-on':host.on, 'icon-off':!host.on}"></i>
 					<i class="iconfont icon-doc i-h"></i>
 					<span class="title">{{ host.title }}</span>
 					<i class="iconfont icon-edit btn-edit" @click="edit(host)"></i>
@@ -43,11 +44,14 @@
 					<a href="#" @click="add()" title="{{ lang.add_host }}"><i class="iconfont icon-add-s"></i></a>
 				</li>
 				<li class="right">
-					<a href="#" @click="tmpClean()" v-cloak v-if="could_tmp_clean_on" title="{{ lang.tmp_clean }}"><i class="iconfont icon-switchon"></i></a>
-					<a href="#" @click="tmpRecover()" v-cloak v-if="!could_tmp_clean_on" title="{{ lang.tmp_recover }}"><i class="iconfont icon-switchoff"></i></a>
+					<a href="#" @click="tmpClean()" v-cloak v-if="could_tmp_clean_on" title="{{ lang.tmp_clean }}"><i
+							class="iconfont icon-switchon"></i></a>
+					<a href="#" @click="tmpRecover()" v-cloak v-if="!could_tmp_clean_on" title="{{ lang.tmp_recover }}"><i
+							class="iconfont icon-switchoff"></i></a>
 				</li>
 				<li class="right">
-					<a href="#" id="btn-toggle-search" @click="toggleSearch()" :class="{active:is_search_bar_show}"><i class="iconfont icon-search"></i></a>
+					<a href="#" id="btn-toggle-search" @click="toggleSearch()" :class="{active:is_search_bar_show}"><i
+							class="iconfont icon-search"></i></a>
 				</li>
 			</ul>
 		</div>
@@ -84,7 +88,7 @@
 			</div>
 			<div class="ln" v-if="current_edit_host.where=='remote'">
 				<label for="ipt-host-url">{{ lang.url }}</label>
-				<input type="text" id="ipt-host-url" name="host_title"
+				<input type="text" id="ipt-host-url" name="host_url"
 					   placeholder="http://"
 					   maxlength="1024"
 					   v-model="current_edit_host.url"
@@ -94,8 +98,18 @@
 				>
 				<div class="inform">{{ inform.url }}</div>
 			</div>
+			<div class="ln" v-if="current_edit_host.where=='remote'">
+				<label for="ipt-refresh-interval">{{ lang.auto_refresh }}</label>
+				<select name="host_refresh_interval" id="ipt-refresh-interval" v-model="current_edit_host.refresh_interval">
+					<option value="{{ opt[0] }}" v-for="opt in refresh_options">{{ opt[1] }}</option>
+				</select>
+				<span class="info">
+					{{ lang.last_refresh }}: {{ _current_edit_host.last_refresh || 'N/A' }}
+				</span>
+			</div>
 			<div class="ln" v-if="add_or_edit=='edit'">
-				<a href="#" class="delete-host" @click="delHost(current_edit_host)"><i class="iconfont icon-delete"></i> {{ lang.del_host }}</a>
+				<a href="#" class="delete-host" @click="delHost(current_edit_host)"><i class="iconfont icon-delete"></i>
+					{{ lang.del_host }}</a>
 			</div>
 		</div>
 		<div class="foot">

File diff suppressed because it is too large
+ 0 - 0
app/SH3/public/js/main.js


+ 7 - 0
app/src/agent.js

@@ -194,6 +194,7 @@ function setPreference(key, value) {
 }
 
 function getURL(url, data, success, fail) {
+    data = data || {};
     if (!data._r) {
         data._r = Math.random();
     }
@@ -274,7 +275,13 @@ function afterSetHosts(sudo_pswd, callback) {
     task.launch();
 }
 
+function log(msg) {
+    console.log(msg);
+    MacGap.log(msg.toString());
+}
+
 module.exports = {
+    log: log,
     readFile: readFile,
     writeFile: writeFile,
     existPath: existPath,

+ 14 - 0
app/src/lang.js

@@ -28,6 +28,13 @@ var languages = {
         , where_remote: 'remote'
         , url: 'URL'
         , bad_url: 'URL is not valid.'
+        , auto_refresh: 'Auto refresh'
+        , last_refresh: 'Last refresh'
+        , never: 'never'
+        , hour: 'hour'
+        , hours: 'hours'
+        , day: 'day'
+        , days: 'days'
         , hide_dock_icon: 'Hide Dock Icon'
         , show_dock_icon: 'Show Dock Icon'
         , toggle_dock_icon: 'Toggle Dock Icon'
@@ -56,6 +63,13 @@ var languages = {
         , where_remote: '远程'
         , url: 'URL 地址'
         , bad_url: 'URL 地址有误。'
+        , auto_refresh: '自动更新'
+        , last_refresh: '上次更新'
+        , never: '从不'
+        , hour: '小时'
+        , hours: '小时'
+        , day: '天'
+        , days: '天'
         , hide_dock_icon: '隐藏 Dock 图标'
         , show_dock_icon: '显示 Dock 图标'
         , toggle_dock_icon: '显示/隐藏 Dock 图标'

+ 31 - 27
app/src/main.js

@@ -10,6 +10,7 @@ var config = require('./config');
 Vue.use(require('./vue_dnd'));
 var util = require('./util');
 var agent = require('./agent');
+var refresh = require('./refresh');
 var lang = require('./lang').getLang(navigator.language);
 var tray_obj;
 
@@ -31,8 +32,17 @@ var app = new Vue({
             on: true,
             is_editable: false,
             where: 'sys',
-            url: ''
+            url: '',
+            refresh_interval: 24,
+            last_refresh: null
         },
+        refresh_options: [
+            [0, lang.never],
+            //[0.005, '0.005 ' + lang.hour],
+            [1, '1 ' + lang.hour],
+            [24, '1 ' + lang.day],
+            [24 * 7, '7 ' + lang.days]
+        ],
         inform: {
             title: '',
             url: ''
@@ -139,30 +149,7 @@ var app = new Vue({
             this.current_host.content = v;
         },
         getRemoteHost: function (host) {
-            if (host.where !== 'remote' || !host.url) return;
-            var tpl = [
-                '# REMOTE: ' + host.title,
-                '# URL: ' + host.url,
-                '# UPDATE: ' + util.now()
-            ];
-
-            host.content = '# loading...';
-            this.onCurrentHostChange(host);
-
-            var _this = this;
-            agent.getURL(host.url, {}, function (s) {
-                // success
-                host.content = tpl.concat(['', s]).join('\n');
-                _this.onCurrentHostChange(host);
-                _this.doSave();
-            }, function (xhr, status) {
-                // fail
-                host.content = tpl.concat(['', 'FAIL to get!', status]).join('\n');
-                _this.onCurrentHostChange(host);
-                _this.doSave();
-            });
-
-            this.doSave();
+            refresh.getRemoteHost(this, host);
         },
         toSave: function () {
             if (!this.chkHostTitle() || !this.chkHostUrl()) {
@@ -182,7 +169,9 @@ var app = new Vue({
                     content: '# ' + this.current_edit_host.title,
                     on: false,
                     where: this.current_edit_host.where,
-                    url: this.current_edit_host.url
+                    url: this.current_edit_host.url,
+                    refresh_interval: this.current_edit_host.refresh_interval,
+                    last_refresh: null
                 };
                 this.hosts.list.push(host);
                 this.selectHost(host);
@@ -382,8 +371,19 @@ var app = new Vue({
                 item.content.indexOf(this.search_keyword) > -1;
         },
 
+        checkRefresh: function () {
+            var _this = this;
+            var t = 60 * 5 * 1000;
+            //var t = 1000;
+            refresh.checkRefresh(this);
+
+            setTimeout(function () {
+                _this.checkRefresh();
+            }, t);
+        },
+
         log: function (obj) {
-            console.log(obj);
+            agent.log(obj);
             return 1;
         }
     }
@@ -396,3 +396,7 @@ tray_obj.updateTrayMenu(app.hosts);
 
 var ui = require('./ui');
 ui.init(app);
+
+setTimeout(function () {
+    app.checkRefresh();
+}, 1000);

+ 72 - 0
app/src/refresh.js

@@ -0,0 +1,72 @@
+/**
+ * @author oldj
+ * @blog http://oldj.net
+ */
+
+'use strict';
+
+var agent = require('./agent');
+var moment = require('moment');
+
+function checkRefresh(app) {
+    var to_refresh_list = [];
+
+    app.hosts.list.map(function (host) {
+        if (typeof host.refresh_interval != 'number') {
+            host.refresh_interval = 0;
+        }
+
+        if (host.where != 'remote' || !host.refresh_interval) {
+            return;
+        }
+
+        var last_refresh = host.last_refresh;
+        if (typeof last_refresh == 'string') {
+            last_refresh = moment(last_refresh);
+        }
+
+        if (last_refresh && last_refresh.isValid && last_refresh.isValid()) {
+            last_refresh.add(host.refresh_interval, 'h');
+            if (last_refresh.diff(moment()) > 0) {
+                return;
+            }
+        }
+
+        to_refresh_list.push(host);
+    });
+
+    to_refresh_list.map(function (host) {
+        getRemoteHost(app, host);
+    });
+}
+exports.checkRefresh = checkRefresh;
+
+function getRemoteHost(app, host) {
+    if (host.where !== 'remote' || !host.url) return;
+    var tpl = [
+        '# REMOTE: ' + host.title,
+        '# URL: ' + host.url
+    ];
+
+    host.content = '# loading...';
+    app.onCurrentHostChange(host);
+
+    agent.getURL(host.url, {}, function (s) {
+        // success
+        var now = moment().format('YYYY-MM-DD HH:mm:ss');
+        host.content = tpl.concat(['# UPDATE: ' + now, '', s]).join('\n');
+        host.last_refresh = now;
+        app.onCurrentHostChange(host);
+        app.doSave();
+    }, function (xhr, status) {
+        // fail
+        var now = moment().format('YYYY-MM-DD HH:mm:ss');
+        host.content = tpl.concat(['# UPDATE: ' + now, '', 'FAIL to get!', status]).join('\n');
+        host.last_refresh = now;
+        app.onCurrentHostChange(host);
+        app.doSave();
+    });
+
+    app.doSave();
+}
+exports.getRemoteHost = getRemoteHost;

+ 0 - 8
app/src/util.js

@@ -5,14 +5,6 @@
 
 'use strict';
 
-exports.now = function () {
-    var dt = new Date();
-    return [
-        dt.getFullYear(), '-', dt.getMonth() + 1, '-', dt.getDay(), ' ',
-        dt.getHours(), ':', dt.getMinutes(), ':', dt.getSeconds()
-    ].join('');
-};
-
 exports.trim = function (s) {
     if (!s) return '';
     return (typeof s === 'string' ? s : s.toString()).replace(/^\s+|\s+$/g, '');

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
     "gulp-stylus": "~2.1.1",
     "gulp-uglify": "~1.5.1",
     "jquery": "~2.1.4",
+    "moment": "^2.11.1",
     "vue": "~1.0.12",
     "yargs": "~3.31.0"
   },

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