diff --git a/js/app.js b/js/app.js index 1e81b28..377e69a 100644 --- a/js/app.js +++ b/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() { diff --git a/js/github.js b/js/github.js index bc7b2fd..2623c76 100644 --- a/js/github.js +++ b/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; + } +}) \ No newline at end of file diff --git a/js/github_api.js b/js/github_api.js index 5c9e1d9..9d70a0f 100644 --- a/js/github_api.js +++ b/js/github_api.js @@ -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) { diff --git a/listen1.html b/listen1.html index 9b429de..010ed4f 100644 --- a/listen1.html +++ b/listen1.html @@ -284,6 +284,7 @@

重装插件或清除缓存数据会导致我的歌单数据丢失,强烈建议在这些操作前,备份我的歌单。

+
数据恢复
@@ -292,6 +293,7 @@ +
连接到 Github.com