add backup/restore select menu for gist

This commit is contained in:
Listen 1 2017-12-26 23:19:08 +08:00
parent fa600e40d0
commit d12ee5d298
5 changed files with 153 additions and 52 deletions

View File

@ -943,11 +943,39 @@ li {
width: 100px;
}
.dialog-playlist li h2{
.dialog-backuplist li h2{
margin-left: 125px;
font-size: 17px;
}
.dialog-backuplist {
padding-left: 0px;
text-align: left;
}
.dialog-backuplist li {
cursor: pointer;
height: 112px;
padding: 6px;
}
.dialog-backuplist li:hover {
background-color: #555555;
}
.dialog-backuplist li img {
float: left;
height: 100px;
width: 100px;
}
.dialog-backuplist li h2{
margin-top: 10px;
margin-left: 125px;
font-size: 15px;
}
.dialog-merge-playlist {
padding-left: 0px;
text-align: left;
@ -983,6 +1011,14 @@ li {
margin-right: 96px;
}
.dialog-newbackup {
text-align: center;
}
.dialog-newbackup .confirm-button {
margin-right: 12px;
}
.dialog-editplaylist .dialog-footer {
position: absolute;
bottom: 20px;
@ -1060,6 +1096,10 @@ li {
}
.settings-content .btn {
margin-right: 10px;
}
.btn-group button,.btn-pagination,.btn-pagination:focus {
background-color: #333333;
color: #ffffff;

View File

@ -252,6 +252,24 @@
$scope.dialog_title = '连接到Github.com';
$scope.dialog_type = 7;
}
if (dialog_type == 8) {
$scope.dialog_title = '导出到Github Gist';
$scope.dialog_type = 8;
gist.listExistBackup().then(function(res){
$scope.myBackup = res;
}, function(err){
$scope.myBackup = [];
});
}
if (dialog_type == 10) {
$scope.dialog_title = '从Github Gist导入';
$scope.dialog_type = 10;
gist.listExistBackup().then(function(res){
$scope.myBackup = res;
}, function(err){
$scope.myBackup = [];
});
}
};
$scope.chooseDialogOption = function(option_id) {
@ -276,12 +294,12 @@
});
};
$scope.newDialogOption = function() {
$scope.dialog_type = 1;
$scope.newDialogOption = function(option) {
$scope.dialog_type = option;
};
$scope.cancelNewDialog = function() {
$scope.dialog_type = 0;
$scope.cancelNewDialog = function(option) {
$scope.dialog_type = option;
};
$scope.createAndAddPlaylist = function() {
@ -501,14 +519,14 @@
var value = data[key];
localStorage.setObject(key, value);
}
Notification.success("恢复我的歌单成功");
Notification.success("成功导入我的歌单");
}
};
reader.readAsText(fileObject);
}
$scope.gistBackupLoading = false;
$scope.backupMySettings2Gist= function(){
$scope.backupMySettings2Gist= function(gistId, isPublic){
var items = {};
for ( var i = 0; i < localStorage.length; i++ ) {
var key = localStorage.key(i);
@ -519,40 +537,40 @@
}
var gistFiles = gist.json2gist(items);
$scope.gistBackupLoading = true;
gist.backupMySettings2Gist(gistFiles).then(function(){
gist.backupMySettings2Gist(gistFiles, gistId, isPublic).then(function(){
Notification.clearAll();
Notification.success("成功导出歌单到Gist");
Notification.success("成功导出我的歌单到Gist");
$scope.gistBackupLoading = false;
},function(err){
Notification.clearAll();
Notification.warning("导出歌单失败,检查后重试");
Notification.warning("导出我的歌单失败,检查后重试");
$scope.gistBackupLoading = false;
});
Notification({message: "正在导出歌单到Gist...", delay: null});
Notification({message: "正在导出我的歌单到Gist...", delay: null});
}
$scope.gistRestoreLoading = false;
$scope.importMySettingsFromGist = function(){
$scope.importMySettingsFromGist = function(gistId){
$scope.gistRestoreLoading = true;
gist.importMySettingsFromGist().then(function(raw){
gist.importMySettingsFromGist(gistId).then(function(raw){
var data = gist.gist2json(raw);
for ( var key in data) {
var value = data[key];
localStorage.setObject(key, value);
}
Notification.clearAll();
Notification.success("导入歌单成功");
Notification.success("导入我的歌单成功");
$scope.gistRestoreLoading = false;
},function(err){
Notification.clearAll();
if(err==404){
Notification.warning("未找到备份歌单,请先备份");
}else{
Notification.warning("导入歌单失败,检查后重试");
Notification.warning("导入我的歌单失败,检查后重试");
}
$scope.gistRestoreLoading = false;
})
Notification({message: "正在从Gist导入歌单...", delay: null});
Notification({message: "正在从Gist导入我的歌单...", delay: null});
}

View File

@ -21,8 +21,6 @@ function($rootScope) {
},
logout: function(){
Github.logout();
// delete gist id info;
localStorage.removeItem("gistid");
}
};
}]);
@ -37,9 +35,11 @@ ngGithub.provider('gist', {
function json2gist(jsonObject) {
var result = {};
result['listen1_backup.json'] = {content: JSON.stringify(jsonObject)};
//var markdown = '# My Listen1 Playlists\n';
var playlistIds = jsonObject['playerlists'];
var songsCount = 0;
for(var i=0; i<playlistIds.length; i++) {
var playlistId = playlistIds[i];
var playlist = jsonObject[playlistId];
@ -55,7 +55,10 @@ ngGithub.provider('gist', {
var content = '<details>\n <summary>' + cover + ' ' + title + '</summary><p>\n' + tableHeader + tableBody +'</p></details>';
var filename = 'listen1_'+playlistId + '.md';
result[filename] = {content: content};
songsCount += playlist.tracks.length;
}
var summary = "本歌单由[Listen1](http://listen1.github.io/listen1/)创建, 歌曲数:" + songsCount + ",歌单数:" + playlistIds.length + ",点击查看更多";
result['listen1_aha_playlist.md'] = {content: summary};
return result;
}
@ -65,9 +68,25 @@ ngGithub.provider('gist', {
return JSON.parse(jsonString);
}
function backup(files) {
function listExistBackup(){
var deferred = $q.defer();
var url = apiUrl;
$http({
method: 'GET',
url: url,
headers:{'Authorization':'token ' + localStorage.getObject("githubOauthAccessKey")},
}).then(function(res) {
var result = res.data;
result = result.filter(function(backup){return backup.description.startsWith('updated by Listen1')});
deferred.resolve(result);
}, function(err) {
deferred.reject(err)
});
return deferred.promise;
}
function backup(files, gistId, isPublic) {
var deferred = $q.defer();
var gistId = _getGistId();
var method = '';
var url = '';
if (gistId != null) {
@ -83,13 +102,11 @@ ngGithub.provider('gist', {
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": files
"description": "updated by Listen1(http://listen1.github.io/listen1/) at " + new Date().toLocaleString(),
"public": isPublic,
"files": files
}
}).then(function(res) {
var newGistId = res.data.id;
localStorage.setObject("gistid", newGistId);
deferred.resolve();
}, function(err) {
deferred.reject(err)
@ -97,11 +114,11 @@ ngGithub.provider('gist', {
return deferred.promise;
}
function restore() {
function restore(gistId) {
var deferred = $q.defer();
$http({
method:'GET',
url:apiUrl + '/' + _getGistId(),
url:apiUrl + '/' + gistId,
}).then(function(res) {
try{
var files = res.data.files
@ -117,6 +134,7 @@ ngGithub.provider('gist', {
var gistApi = {
"gist2json": gist2json,
"json2gist": json2gist,
"listExistBackup": listExistBackup,
"backupMySettings2Gist": backup,
"importMySettingsFromGist": restore,
};

View File

@ -55,7 +55,7 @@
return '连接中';
}
if(this.status == 2) {
return '已连接: ' + this.username;
return this.username + '已登录';
}
return '???'
},

View File

@ -50,24 +50,24 @@
<div class="dialog-header"> <span>{{ dialog_title }}</span> <span class="dialog-close" ng-click="closeDialog()">×</span></div>
<div class="dialog-body">
<!-- choose playlist dialog -->
<ul class="dialog-playlist" ng-show="dialog_type==0">
<li class="detail-add" ng-click="newDialogOption()">
<img src="images/mycover.jpg" />
<h2> 新建歌单 </h2>
</li>
<li ng-repeat="playlist in myplaylist track by $index" ng-class-odd="'odd'" ng-class-even="'even'" ng-click="chooseDialogOption(playlist.info.id)">
<img ng-src="{{ playlist.info.cover_img_url }}" />
<h2> {{ playlist.info.title }} </h2>
</li>
</ul>
<!-- choose playlist dialog -->
<ul class="dialog-playlist" ng-show="dialog_type==0">
<li class="detail-add" ng-click="newDialogOption(1)">
<img src="images/mycover.jpg" />
<h2> 新建歌单 </h2>
</li>
<li ng-repeat="playlist in myplaylist track by $index" ng-class-odd="'odd'" ng-class-even="'even'" ng-click="chooseDialogOption(playlist.info.id)">
<img ng-src="{{ playlist.info.cover_img_url }}" />
<h2> {{ playlist.info.title }} </h2>
</li>
</ul>
<!-- create new playlist dialog -->
<div ng-show="dialog_type==1" class="dialog-newplaylist">
<input type="text" class="form-control" placeholder="输入歌单名称" ng-model="newlist_title"/>
<button class="btn btn-primary confirm-button" ng-click="createAndAddPlaylist()">创建并添加</button>
<button class="btn btn-default" ng-click="cancelNewDialog()">取消</button>
</div>
<!-- create new playlist dialog -->
<div ng-show="dialog_type==1" class="dialog-newplaylist">
<input type="text" class="form-control" placeholder="输入歌单名称" ng-model="newlist_title"/>
<button class="btn btn-primary confirm-button" ng-click="createAndAddPlaylist()">创建并添加</button>
<button class="btn btn-default" ng-click="cancelNewDialog(0)">取消</button>
</div>
<!-- login douban dialog -->
<!-- <div ng-show="dialog_type==2" class="dbimport" ng-controller="ImportController" ng-init="getLoginInfo()" >
@ -141,6 +141,31 @@
</div>
</div>
<ul class="dialog-backuplist" ng-show="dialog_type==8">
<li class="detail-add" ng-click="newDialogOption(9)">
<img src="images/mycover.jpg" />
<h2> 新建歌单备份 </h2>
</li>
<li ng-repeat="backup in myBackup track by $index" ng-class-odd="'odd'" ng-class-even="'even'" ng-click="backupMySettings2Gist(backup.id, backup.public); closeDialog();">
<img ng-src="images/mycover.jpg" />
<h2> {{ backup.id }} {{backup.description}}</h2>
</li>
</ul>
<!-- create new backup dialog -->
<div ng-show="dialog_type==9" class="dialog-newbackup">
<button class="btn btn-primary confirm-button" ng-click="backupMySettings2Gist(null, true);closeDialog();">创建公开备份</button>
<button class="btn btn-primary confirm-button" ng-click="backupMySettings2Gist(null, false);closeDialog();">创建私有备份</button>
<button class="btn btn-default" ng-click="cancelNewDialog(8)">取消</button>
</div>
<ul class="dialog-backuplist" ng-show="dialog_type==10">
<li ng-repeat="backup in myBackup track by $index" ng-class-odd="'odd'" ng-class-even="'even'" ng-click="importMySettingsFromGist(backup.id); closeDialog();">
<img ng-src="images/mycover.jpg" />
<h2> {{ backup.id }} {{backup.description}}</h2>
</li>
</ul>
</div>
</div>
@ -279,21 +304,21 @@
<div ng-show="isDoubanLogin"> 豆瓣:<button class="btn btn-primary confirm-button" ng-click="showDialog(2)">已登录</button></div>
<div ng-hide="isDoubanLogin"> 豆瓣:<button class="btn btn-primary confirm-button" ng-click="showDialog(2)">登录</button> </div>
</div> -->
<div class="settings-title"><span>歌单导出<span></div>
<div class="settings-title"><span>导出我的歌单<span></div>
<div class="settings-content">
<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()">导出歌单到Github Gist</button>
<button class="btn btn-primary confirm-button" ng-click="backupMySettings()">导出到本地文件</button>
<button class="btn btn-primary confirm-button" ng-show="github.getStatus() == 2" ng-click="showDialog(8)">导出到Github Gist</button>
</div>
</div>
<div class="settings-title"><span>歌单导入<span></div>
<div class="settings-title"><span>导入我的歌单<span></div>
<div class="settings-content">
<p>选择备份文件,恢复我的歌单。注意:恢复我的歌单会覆盖现有的歌单。</p>
<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">从本地文件导入歌单
<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()">从Github Gist导入歌单</button>
<button class="btn btn-warning confirm-button" ng-show="github.getStatus() == 2" ng-click="showDialog(10)">从Github Gist导入</button>
</div>
<div class="settings-title"><span>连接到 Github.com<span></div>
@ -354,7 +379,7 @@
<a title="收藏歌单" class="clone" ng-show="playlist_title!='' && !is_mine" ng-click="clonePlaylist(list_id)">收藏</a>
<a title="编辑歌单" class="edit" ng-show="playlist_title!='' && is_mine" ng-click="showDialog(3, {list_id: list_id, playlist_title: playlist_title, cover_img_url: cover_img_url})">编辑</a>
<a title="原始链接" class="link" ng-show="playlist_title!=''" open-url="playlist_source_url">原始链接</a>
<a title="导入合并" class="merge" ng-show="playlist_title!='' && is_mine" ng-click="showDialog(6)">导入合并</a>
<a title="导入合并" class="merge" ng-show="playlist_title!='' && is_mine" ng-click="showDialog(6)">导入合并</a>
</div>
</div>
<ul class="detail-songlist">