DAVIDS/public/index.html

222 lines
7.6 KiB
HTML
Raw Normal View History

2016-04-17 11:21:26 +02:00
<!doctype html>
<html>
<head>
<title>Spotify Duplicate Finder</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
2016-04-17 15:35:54 +02:00
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css">
2016-04-17 16:29:37 +02:00
<link rel="stylesheet" href="./style.css">
2016-04-17 11:21:26 +02:00
</head>
<body>
<div class="container">
<div id="login">
<h1>This is an example of the Authorization Code flow</h1>
<a href="/login" class="btn btn-primary">Log in with Spotify</a>
</div>
<div id="loggedin">
2016-04-17 16:29:37 +02:00
<h1>Duplicates finder</h1>
2016-04-17 11:21:26 +02:00
<div id="user-profile">
</div>
<div class="media">
<div class="pull-left">
2016-04-17 16:29:37 +02:00
<h2 id="playlists-title">Playlists
<button class="btn btn-default has-spinner" id="get-playlists">
Refresh <span class="spinner"><i class="icon-spin icon-refresh"></i></span>
</button>
</h2>
<div id="playlists">
</div>
</div>
<div class="media-body">
<div id="dups">
</div>
2016-04-17 11:21:26 +02:00
</div>
</div>
</div>
</div>
2016-04-17 16:29:37 +02:00
<script id="playlists-template" type="text/x-handlebars-template">
{{#list playlists}}
<a class="pl_item list-group-item has-spinner" href="/pl/{{pl_uid}}/{{pl_id}}">
{{pl_name}}
<span class="spinner"><i class="icon-spin icon-refresh"></i></span>
<i class="glyphicon glyphicon-chevron-right"></i>
</a>
{{/list}}
</script>
2016-04-17 11:21:26 +02:00
<script id="user-profile-template" type="text/x-handlebars-template">
2016-04-17 16:29:37 +02:00
Logged in as {{display_name}}
2016-04-17 16:07:11 +02:00
<button class="btn btn-default has-spinner" id="obtain-new-token">
2016-04-17 16:29:37 +02:00
Refresh token <span class="spinner"><i class="icon-spin icon-refresh"></i></span>
2016-04-17 16:07:11 +02:00
</button>
2016-04-17 11:21:26 +02:00
</script>
<script id="dups-template" type="text/x-handlebars-template">
<h2>Duplicates in {{pl_name}}</h2>
{{message}}
2016-04-17 16:07:11 +02:00
{{#list dups}}
<div class="list-group-item">{{dup_trackname}} - {{dup_artist}}</div>
{{/list}}
2016-04-17 11:21:26 +02:00
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.1/handlebars.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script>
2016-04-17 16:45:15 +02:00
var access_token;
var refresh_token;
var error;
2016-04-17 11:21:26 +02:00
(function () {
/**
* Obtains parameters from the hash of the URL
* @return Object
*/
function getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
while (e = r.exec(q)) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
return hashParams;
}
Handlebars.registerHelper('list', function (items, options) {
2016-04-17 12:55:53 +02:00
var out = "<div class='list-group'>";
2016-04-17 11:21:26 +02:00
for (var i = 0, l = items.length; i < l; i++) {
2016-04-17 12:55:53 +02:00
out = out + options.fn(items[i]);
2016-04-17 11:21:26 +02:00
}
2016-04-17 12:55:53 +02:00
return out + "</div>";
2016-04-17 11:21:26 +02:00
});
var userProfileSource = document.getElementById('user-profile-template').innerHTML,
userProfileTemplate = Handlebars.compile(userProfileSource),
userProfilePlaceholder = document.getElementById('user-profile');
var playlistsSource = document.getElementById('playlists-template').innerHTML,
playlistsTemplate = Handlebars.compile(playlistsSource),
playlistsPlaceholder = document.getElementById('playlists');
var dupsSource = document.getElementById('dups-template').innerHTML,
dupsTemplate = Handlebars.compile(dupsSource),
dupsPlaceholder = document.getElementById('dups');
var params = getHashParams();
2016-04-17 16:45:15 +02:00
access_token = params.access_token;
refresh_token = params.refresh_token;
error = params.error;
2016-04-17 11:21:26 +02:00
if (error) {
alert('There was an error during the authentication');
} else {
if (access_token) {
$.ajax({
url: 'https://api.spotify.com/v1/me',
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function (response) {
userProfilePlaceholder.innerHTML = userProfileTemplate(response);
$('#login').hide();
$('#loggedin').show();
}
});
} else {
// render initial screen
$('#login').show();
$('#loggedin').hide();
}
document.getElementById('get-playlists').addEventListener('click', function () {
2016-04-17 15:35:54 +02:00
var currentElement = $(this);
currentElement.addClass('loading');
2016-04-17 11:21:26 +02:00
$.ajax({
url: '/get_playlists',
data: {
'access_token': access_token
}
}).done(function (data) {
2016-04-17 16:29:37 +02:00
var pl = data.data.map(function (item) {
2016-04-17 11:21:26 +02:00
return {
pl_uid: item.owner.id,
pl_name: item.name,
pl_id: item.id
2016-04-17 11:21:26 +02:00
}
});
2016-04-17 15:35:54 +02:00
currentElement.removeClass('loading');
2016-04-17 11:21:26 +02:00
playlistsPlaceholder.innerHTML = playlistsTemplate({
playlists: pl
});
})
}, false);
$(document).on('click', '.pl_item', function (e) {
e.preventDefault();
var pl_name = $(this).text();
2016-04-17 12:59:12 +02:00
$('.pl_item').removeClass('active');
2016-04-17 15:35:54 +02:00
var currentElement = $(this);
currentElement.addClass('active');
currentElement.addClass('loading');
2016-04-17 16:45:15 +02:00
$('#dups').hide();
2016-04-17 11:21:26 +02:00
$.ajax({
url: $(this).attr('href'),
data: {
'access_token': access_token
}
}).done(function (data) {
var dups = data.data.map(function (item) {
return {
dup_trackname: item.track.name,
dup_artist: item.track.artists[0].name
}
});
2016-04-17 15:35:54 +02:00
currentElement.removeClass('loading');
2016-04-17 11:21:26 +02:00
if (data.data.length > 0) {
dupsPlaceholder.innerHTML = dupsTemplate({
dups: dups,
pl_name: pl_name
2016-04-17 11:21:26 +02:00
});
}
else {
dupsPlaceholder.innerHTML = dupsTemplate({
dups: [],
2016-04-17 11:21:26 +02:00
message: "No duplicate found.",
pl_name: pl_name
});
}
2016-04-17 16:45:15 +02:00
$('#dups').show();
2016-04-17 11:21:26 +02:00
})
});
$(document).on('click', '#obtain-new-token', function () {
2016-04-17 16:45:15 +02:00
refreshToken();
});
2016-04-17 11:21:26 +02:00
}
})();
2016-04-17 16:45:15 +02:00
function refreshToken() {
var button = $('#obtain-new-token');
button.addClass('loading');
$.ajax({
url: '/refresh_token',
data: {
'refresh_token': refresh_token
}
}).done(function (data) {
access_token = data.access_token;
button.removeClass('loading');
});
}
2016-04-17 11:21:26 +02:00
</script>
</body>
</html>