forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.jsx
More file actions
263 lines (247 loc) · 13 KB
/
common.jsx
File metadata and controls
263 lines (247 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
console.log( "===== simpread option common load =====" )
import Button from 'button';
import Notify from 'notify';
import * as ss from 'stylesheet';
import { storage, Now } from 'storage';
import * as ver from 'version';
import * as menu from 'menu';
import * as watch from 'watch';
import * as exp from 'export';
import {br} from 'browser';
export default class CommonOpt extends React.Component {
static propTypes = {
sync : React.PropTypes.func,
}
state = {
sync: ( sync => !sync ? "从未同步过,建议同步一次!" : `上次同步时间: ${ sync }` ) ( storage.option.sync )
};
sync() {
let notify;
const dbx = exp.dropbox,
read = () => {
notify = new Notify().Render({ content: "数据同步中,请稍等...", state: "loading" });
dbx.Exist( dbx.config_name, ( result, error ) => {
if ( result == -1 ) {
storage.option.sync = Now();
storage.Write( () => {
dbx.Write( dbx.config_name, storage.Export(), callback );
});
} else {
dbx.Read( dbx.config_name, callback );
}
});
},
callback = ( type, result, error ) => {
notify.complete();
switch ( type ) {
case "write":
!error ? ( location.href = location.origin + location.pathname + "?simpread_mode=sync" ) :
new Notify().Render( 2, error == "error" ? "远程数据同步失败,请稍后再试!" : error );
break;
case "read":
const json = JSON.parse( result ),
local = storage.option.update ? new Date( storage.option.update.replace( /年|月/ig, "-" ).replace( "日", "" )) : new Date( "1999-01-01 12:12:12" ),
remote = new Date( json.option.update.replace( /年|月/ig, "-" ).replace( "日", "" ));
if ( ver.Compare( json.version ) == 1 ) {
new Notify().Render( "本地版本与远程版本不一致,且本地版本较新,是否覆盖远程版本?", "覆盖", () => {
storage.option.sync = Now();
storage.Write( () => {
watch.SendMessage( "import", true );
dbx.Write( dbx.config_name, storage.Export(), callback );
}, storage.simpread );
});
}
else if ( local < remote ) {
new Notify().Render( "远程备份配置文件较新,是否覆盖本地文件?", "覆盖", () => {
this.importsecret( json.option.secret, { ...json.secret }, () => {
delete json.secret;
storage.Write( () => {
watch.SendMessage( "import", true );
location.href = location.origin + location.pathname + "?simpread_mode=reload";
}, json );
});
});
} else if ( local > remote ) {
new Notify().Render( "本地配置文件较新,是否覆盖远程备份文件?", "覆盖", () => {
storage.option.sync = Now();
storage.Write( () => {
watch.SendMessage( "import", true );
dbx.Write( dbx.config_name, storage.Export(), callback );
}, storage.simpread );
});
} else {
new Notify().Render( "本地与远程数据相同,无需重复同步。" );
}
break;
}
};
storage.Safe( ()=> {
const sec_dbx = storage.secret.dropbox;
!sec_dbx.access_token ?
new Notify().Render( `未对 ${ dbx.name } 授权,请先进行授权操作。`, "授权", () => {
dbx.New().Auth();
dbx.dtd
.done( () => {
sec_dbx.access_token = dbx.access_token;
storage.Safe( () => {
new Notify().Render( "授权成功!" );
read();
}, storage.secret );
})
.fail( error => {
console.error( error )
new Notify().Render( 2, `获取 ${ dbx.name } 授权失败,请重新获取。` );
});
}) : ( () => {
dbx.access_token = sec_dbx.access_token;
read();
})();
});
}
import() {
const input = document.createElement( "input" ),
$input = $(input),
onload = event => {
if ( event && event.target && event.target.result ) {
try {
let json = JSON.parse( event.target.result );
const result = ver.Compare( json.version );
if ( result < 0 ) {
result == -1 && new Notify().Render( 2, "上传失败,当前版本太低,请升级简悦。" );
result == -2 && new Notify().Render( 2, "上传失败,配置文件版本不存在。" );
} else {
if ( result == 0 ) {
const obj = storage.Verify( json );
if ( obj.option.code != 0 || obj.focus.code != 0 || obj.read.code != 0 || obj.stat.code != 0 ) {
new Notify().Render( 2, "上传失败,配置项不匹配,请重新上传。" );
return;
}
} else if ( result == 1 ) {
storage.version != json.version &&
storage.Fix( json.read.sites, json.version, storage.version, json.focus.sites );
json = ver.Verify( json.version, json );
new Notify().Render( "上传版本太低,已自动转换为最新版本。" );
}
menu.Refresh( json.option.menu );
json.option.origins && json.option.origins.length > 0 &&
new Notify().Render( "导入的配置文件包含了第三方源,请通过手动导入。" );
json.option.plugins && json.option.plugins.length > 0 &&
new Notify().Render( "导入的配置文件包含了插件,请通过手动导入。" );
this.importsecret( json.option.secret, { ...json.secret }, () => {
delete json.secret;
storage.Write( ()=> {
storage.Plugins( () => {
watch.SendMessage( "import", true );
new Notify().Render( "snackbar", "导入成功,请刷新当前页面,以便新配置文件生效。", "刷新", () => {
location.href = location.origin + location.pathname + "?simpread_mode=reload";
});
}, {} );
}, json );
});
}
} catch ( error ) {
new Notify().Render( 2, "上传失败,配置文件解析失败,请重新确认。" );
}
}
};
$input.attr({ type : "file", multiple : "false" })
.one( "change", event => {
const reader = new FileReader();
reader.onload = onload;
reader.readAsText( event.target.files[0] );
});
$input.trigger( "click" );
}
export() {
if ( br.isFirefox() ) {
exp.PrDownload( storage.Export(), `simpread-config.json` );
} else {
const data = "data:text/json;charset=utf-8," + encodeURIComponent( storage.Export() );
exp.Download( data, `simpread-config-${Now()}.json` );
}
}
newsites() {
const notify = new Notify().Render({ content: "数据同步中,请稍等...", state: "loading" });
storage.GetRemote( "remote", ( result, error ) => {
notify.complete();
if ( !error ) {
const count = storage.pr.Addsites( result );
storage.Writesite( storage.pr.sites, () => {
watch.SendMessage( "site", true );
count == 0 ? new Notify().Render( "适配列表已同步至最新版本。" ) : new Notify().Render( 0, `适配列表已同步成功,本次新增 ${ count } 个站点。` );
});
} else {
new Notify().Render( 3, `同步时发生了一些问题,并不会影响本地配置文件,请稍后再试!` );
}
});
}
clear() {
new Notify().Render( "snackbar", "是否清除掉(已包含账户信息)本地配置文件?", "同意 ", () => {
storage.Clear( "local", () => {
new Notify().Render( "snackbar", "清除成功,此页面需刷新后才能生效!", "刷新 ", ()=>{
location.href = location.origin + location.pathname + "?simpread_mode=clear";
});
});
});
}
/**
* Import to secret storage
*
* @param {boole} option.secret value
* @param {object} simpread.secret
* @param {function} callback
*/
importsecret( state, secret, callback ) {
state && !$.isEmptyObject( secret ) ? storage.Safe( ()=>{
callback();
}, secret ): callback();
}
componentDidMount() {
if ( this.props.website_sync ) {
storage.GetRemote( "versions", ( result, error ) => {
if ( !error && result.website == true ) {
new Notify().Render( "正在获取最新的适配列表,请稍等..." );
this.newsites();
}
});
}
}
render() {
return(
<div style={{ width: '100%' }}>
<Button type="raised" text="同步到你的 Dropbox 账户"
icon={ ss.IconPath( "sync_icon" ) }
color="#fff" backgroundColor="#1976D2"
waves="md-waves-effect md-waves-button"
tooltip={{ text: this.state.sync }}
onClick={ ()=>this.sync() } />
<div style={{ display: 'inline-flex', width: '100%' }}>
<Button type="raised" text="从本地导入配置文件" width="100%"
icon={ ss.IconPath( "import_icon" ) }
color="#fff" backgroundColor="#FF5252"
waves="md-waves-effect md-waves-button"
tooltip={{ text: "上传后的配置将覆盖掉当前,请注意确认!" }}
onClick={ ()=>this.import() } />
<Button type="raised" text="导出配置文件到本地" width="100%"
icon={ ss.IconPath( "export_icon" ) }
color="#fff" backgroundColor="#2196F3"
waves="md-waves-effect md-waves-button"
onClick={ ()=>this.export() } />
</div>
<div style={{ display: 'inline-flex', width: '100%' }}>
<Button type="raised" text="手动同步适配列表" width="100%"
icon={ ss.IconPath( "update_icon" ) }
color="#fff" backgroundColor="#2196F3"
waves="md-waves-effect md-waves-button"
onClick={ ()=>this.newsites() } />
<Button type="raised" text="清除数据" width="100%"
icon={ ss.IconPath( "clear_icon" ) }
tooltip={{ text: "清除掉本地配置文件,需谨慎!" }}
color="#fff" backgroundColor="#757575"
waves="md-waves-effect md-waves-button"
onClick={ ()=>this.clear() } />
</div>
</div>
)
}
}