Browse Source

实现自动刷新逻辑。

oldj 10 years ago
parent
commit
da604d7471

+ 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>2906</string>
+	<string>2941</string>
 	<key>LSApplicationCategoryType</key>
 	<string>public.app-category.developer-tools</string>
 	<key>LSMinimumSystemVersion</key>

+ 2 - 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;
 }

+ 2 - 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,7 +182,7 @@ unselectable()
         width 80px
       input[type=text],
       input[type=password]
-        width 200px
+        width 240px
         padding 6px 10px
         outline none
 

+ 2 - 3
app/SH3/public/index.html

@@ -104,13 +104,12 @@
 					<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' }}
+					{{ 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>
+					{{ 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,

+ 15 - 1
app/src/main.js

@@ -37,6 +37,7 @@ var app = new Vue({
         },
         refresh_options: [
             [0, lang.never],
+            //[0.005, '0.005 ' + lang.hour],
             [1, '1 ' + lang.hour],
             [24, '1 ' + lang.day],
             [24 * 7, '7 ' + lang.days]
@@ -392,8 +393,17 @@ var app = new Vue({
                 item.content.indexOf(this.search_keyword) > -1;
         },
 
+        checkRefresh: function () {
+            var _this = this;
+            require('./refresh').checkRefresh(this);
+
+            setTimeout(function () {
+                _this.checkRefresh();
+            }, 60 * 5 * 1000);
+        },
+
         log: function (obj) {
-            console.log(obj);
+            agent.log(obj);
             return 1;
         }
     }
@@ -406,3 +416,7 @@ tray_obj.updateTrayMenu(app.hosts);
 
 var ui = require('./ui');
 ui.init(app);
+
+setTimeout(function () {
+    app.checkRefresh();
+}, 1000);

+ 52 - 0
app/src/refresh.js

@@ -0,0 +1,52 @@
+/**
+ * @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) {
+        agent.getURL(host.url, {}, function (c) {
+            host.content = c;
+            host.last_refresh = moment().format('YYYY-MM-DD HH:mm:ss');
+
+            app.onCurrentHostChange(host);
+            app.doSave();
+        });
+    });
+}
+exports.checkRefresh = checkRefresh;
+
+function getRemoteHosts(app, host) {
+
+}

+ 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