add gist sync functions
This commit is contained in:
parent
72c6c540d4
commit
ec98406914
55
js/app.js
55
js/app.js
@ -87,10 +87,10 @@
|
||||
app.controller('NavigationController', ['$scope', '$http',
|
||||
'$httpParamSerializerJQLike', '$timeout',
|
||||
'angularPlayer', 'Notification', '$rootScope', 'loWeb',
|
||||
'hotkeys', 'lastfm', 'github',
|
||||
'hotkeys', 'lastfm', 'github', 'gist',
|
||||
function($scope, $http, $httpParamSerializerJQLike,
|
||||
$timeout, angularPlayer, Notification, $rootScope,
|
||||
loWeb, hotkeys, lastfm, github) {
|
||||
loWeb, hotkeys, lastfm, github, gist) {
|
||||
|
||||
$rootScope.page_title = "Listen 1";
|
||||
$scope.window_url_stack = [];
|
||||
@ -507,6 +507,55 @@
|
||||
reader.readAsText(fileObject);
|
||||
}
|
||||
|
||||
$scope.gistBackupLoading = false;
|
||||
$scope.backupMySettings2Gist= function(){
|
||||
var items = {};
|
||||
for ( var i = 0; i < localStorage.length; i++ ) {
|
||||
var key = localStorage.key(i);
|
||||
if(key!=="gistid" && key !== 'githubOauthAccessKey'){ // avoid token leak
|
||||
var value = localStorage.getObject(key);
|
||||
items[key] = value;
|
||||
}
|
||||
}
|
||||
var content = JSON.stringify(items);
|
||||
$scope.gistBackupLoading = true;
|
||||
gist.backupMySettings2Gist(content).then(function(){
|
||||
Notification.clearAll();
|
||||
Notification.success("成功备份歌单到Gist");
|
||||
$scope.gistBackupLoading = false;
|
||||
},function(err){
|
||||
Notification.clearAll();
|
||||
Notification.warning("备份歌单失败,检查后重试");
|
||||
$scope.gistBackupLoading = false;
|
||||
});
|
||||
Notification({message: "正在备份歌单到Gist...", delay: null});
|
||||
}
|
||||
|
||||
$scope.gistRestoreLoading = false;
|
||||
$scope.importMySettingsFromGist = function(){
|
||||
$scope.gistRestoreLoading = true;
|
||||
gist.importMySettingsFromGist().then(function(raw){
|
||||
var data = JSON.parse(raw);
|
||||
for ( var key in data) {
|
||||
var value = data[key];
|
||||
localStorage.setObject(key, value);
|
||||
}
|
||||
Notification.clearAll();
|
||||
Notification.success("恢复我的歌单成功");
|
||||
$scope.gistRestoreLoading = false;
|
||||
},function(err){
|
||||
Notification.clearAll();
|
||||
if(err==404){
|
||||
Notification.warning("未找到备份歌单,请先备份");
|
||||
}else{
|
||||
Notification.warning("恢复歌单失败,检查后重试");
|
||||
}
|
||||
$scope.gistRestoreLoading = false;
|
||||
})
|
||||
Notification({message: "正在从Gist恢复我的歌单...", delay: null});
|
||||
}
|
||||
|
||||
|
||||
$scope.showShortcuts = function() {
|
||||
hotkeys.toggleCheatSheet();
|
||||
}
|
||||
@ -634,7 +683,7 @@
|
||||
}
|
||||
|
||||
$scope.saveLocalCurrentPlaying = function() {
|
||||
localStorage.setObjct('current-playing', angularPlayer.playlist)
|
||||
localStorage.setObject('current-playing', angularPlayer.playlist)
|
||||
}
|
||||
|
||||
$scope.changePlaymode = function() {
|
||||
|
72
js/github.js
72
js/github.js
@ -8,7 +8,6 @@ function($rootScope) {
|
||||
window.open(Github.getOAuthUrl(), '_blank');
|
||||
},
|
||||
getStatusText: function(){
|
||||
console.log('getStatusText');
|
||||
return Github.getStatusText();
|
||||
},
|
||||
getStatus: function(){
|
||||
@ -22,6 +21,77 @@ function($rootScope) {
|
||||
},
|
||||
logout: function(){
|
||||
Github.logout();
|
||||
// delete gist id info;
|
||||
localStorage.removeItem("gistid");
|
||||
}
|
||||
};
|
||||
}]);
|
||||
|
||||
ngGithub.provider('gist', {
|
||||
$get: function($http, $q) {
|
||||
var apiUrl = 'https://api.github.com/gists';
|
||||
|
||||
function _getGistId() {
|
||||
return localStorage.getObject("gistid");
|
||||
}
|
||||
|
||||
function backup(filecontent) {
|
||||
var deferred = $q.defer();
|
||||
var gistId = _getGistId();
|
||||
var method = '';
|
||||
var url = '';
|
||||
if (gistId != null) {
|
||||
method = 'PATCH';
|
||||
url = apiUrl + '/' + gistId;
|
||||
}
|
||||
else {
|
||||
method = 'POST';
|
||||
url = apiUrl;
|
||||
}
|
||||
$http({
|
||||
method: method,
|
||||
url: url,
|
||||
headers:{'Authorization':'token ' + localStorage.getObject("githubOauthAccessKey")},
|
||||
data:{
|
||||
"description": "updated by Listen1(http://listen1.github.io/listen1/) at " + new Date().toLocaleString(),
|
||||
"public": false,
|
||||
"files": {
|
||||
"listen1_backup.json": {
|
||||
"content": filecontent
|
||||
}
|
||||
}
|
||||
}
|
||||
}).then(function(res) {
|
||||
var newGistId = res.data.id;
|
||||
localStorage.setObject("gistid", newGistId);
|
||||
deferred.resolve();
|
||||
}, function(err) {
|
||||
deferred.reject(err)
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
function restore() {
|
||||
var deferred = $q.defer();
|
||||
$http({
|
||||
method:'GET',
|
||||
url:apiUrl + '/' + _getGistId(),
|
||||
}).then(function(res) {
|
||||
try{
|
||||
var backupcontent = res.data.files["listen1_backup.json"].content;
|
||||
deferred.resolve(backupcontent);
|
||||
}catch(e){
|
||||
deferred.reject(404);
|
||||
}
|
||||
}, function(err) {
|
||||
deferred.reject(err);
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
var gistApi = {
|
||||
"backupMySettings2Gist": backup,
|
||||
"importMySettingsFromGist": restore,
|
||||
};
|
||||
return gistApi;
|
||||
}
|
||||
})
|
@ -30,17 +30,12 @@
|
||||
}
|
||||
var self = this;
|
||||
this.api('/user', function(data){
|
||||
console.log(data.login);
|
||||
if (data.login == undefined) {
|
||||
console.log('before setting:', self.status);
|
||||
self.status = 1;
|
||||
console.log('after setting:', self.status);
|
||||
}
|
||||
else {
|
||||
console.log('before setting:', self.status);
|
||||
self.status = 2;
|
||||
self.username = data.login;
|
||||
console.log('after setting:', self.status);
|
||||
}
|
||||
if(callback != null) {
|
||||
callback(self.status);
|
||||
@ -53,7 +48,6 @@
|
||||
},
|
||||
|
||||
getStatusText: function(){
|
||||
console.log(this.status);
|
||||
if(this.status == 0) {
|
||||
return '未连接';
|
||||
}
|
||||
@ -86,7 +80,6 @@
|
||||
dataType: "json",
|
||||
data: data,
|
||||
success: function(response) {
|
||||
console.log(response);
|
||||
var ak = response.access_token;
|
||||
localStorage.setObject('githubOauthAccessKey', ak);
|
||||
if(cb != undefined) {
|
||||
|
@ -284,6 +284,7 @@
|
||||
<p>重装插件或清除缓存数据会导致我的歌单数据丢失,强烈建议在这些操作前,备份我的歌单。</p>
|
||||
<div>
|
||||
<button class="btn btn-primary confirm-button" ng-click="backupMySettings()">下载备份文件</button>
|
||||
<button class="btn btn-primary confirm-button" ng-disabled="gistBackupLoading" ng-show="github.getStatus() == 2" ng-click="backupMySettings2Gist()">备份到Gist</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-title"><span>数据恢复<span></div>
|
||||
@ -292,6 +293,7 @@
|
||||
<label class="btn btn-warning" for="my-file-selector">
|
||||
<input id="my-file-selector" type="file" style="display:none;" ng-model="myuploadfiles" custom-on-change="importMySettings">上传备份文件
|
||||
</label>
|
||||
<button class="btn btn-warning confirm-button" ng-disabled="gistRestoreLoading" ng-show="github.getStatus() == 2" ng-click="importMySettingsFromGist()">从Gist恢复</button>
|
||||
</div>
|
||||
|
||||
<div class="settings-title"><span>连接到 Github.com<span></div>
|
||||
|
Loading…
Reference in New Issue
Block a user