nextcloud-app-radio/js/main.js

436 lines
13 KiB
JavaScript
Raw Normal View History

2018-08-20 14:20:03 +00:00
var MODULE = (function (radio) {
2016-11-17 22:07:22 +00:00
var last_volume = 0;
2017-08-09 15:30:13 +00:00
$(document).ready(function () {
2018-08-06 15:07:52 +00:00
// Player icon pause/stop
2017-08-09 15:30:13 +00:00
var vid = document.getElementById("player");
vid.onwaiting = function () {
$('#playbutton').attr("class", "buffering");
};
vid.onplaying = function () {
$('#playbutton').attr("class", "pause");
};
2018-08-06 15:07:52 +00:00
vid.onpause = function () {
$('#playbutton').attr("class", "play");
};
// Scroll event handler
$(window).scroll(function(){
var station_list = $("#app-content-files");
if ( ( $(window).scrollTop() + $(window).height() ) == $(document).height() ) {
if(!station_list.data("didfire")) {
station_list.data("didfire", true);
if ($('li.nav-files').hasClass('active')){
2018-08-20 14:20:03 +00:00
radio.radio_query(1, "");
2018-08-06 15:07:52 +00:00
} else if ($('li.nav-recent').hasClass('active')) {
2018-08-20 14:20:03 +00:00
radio.radio_query(2, "");
2018-08-06 15:07:52 +00:00
} else if ($('li.nav-favorites').hasClass('active')) {
console.log("todo");
} else {
var query = $('#searchbox').val();
2018-08-20 14:20:03 +00:00
radio.radio_query(0, query);
2018-08-06 15:07:52 +00:00
};
var ref = station_list;
setTimeout(function(){
ref.data("didfire", false);
}, 500);
};
};
});
2017-08-09 15:30:13 +00:00
});
searchTimeout = null;
var scheduled;
2018-08-20 14:20:03 +00:00
radio.isElementOverflowing = function(element) {
var overflowX = element.offsetWidth < element.scrollWidth,
overflowY = element.offsetHeight < element.scrollHeight;
return (overflowX || overflowY);
}
2018-08-20 14:20:03 +00:00
radio.wrapContentsInMarquee = function(element) {
var marquee = document.createElement('marquee'),
contents = element.innerText;
marquee.innerText = contents;
element.innerHTML = '';
element.appendChild(marquee);
}
2017-08-05 11:21:11 +00:00
/* ==============
// EVENTS //
/===============*/
/* Click on a radio station (table entry) and play it */
2018-08-07 15:00:10 +00:00
$('body').on('click', '.filename', function(e) {
e.preventDefault();
2018-08-15 15:55:05 +00:00
$('#playbutton').attr("class", "buffering");
2017-08-05 11:21:11 +00:00
var stationid = $(this).parent().attr('data-id');
var stationname = $(this).parent().find('.nametext').text();
2018-08-20 14:20:03 +00:00
radio.action_play(stationid);
$('#player').attr('data-id',stationid);
$("#station_metadata").html("Playing: "+stationname);
2017-08-08 22:26:10 +00:00
/* dirty fix missing background color for first td */
$('tbody tr').css("background-color", "white");
$(this).parent().css("background-color", "#f8f8f8");
var element = document.getElementById('station_metadata');
2018-08-20 14:20:03 +00:00
if (radio.isElementOverflowing(element)) {
radio.wrapContentsInMarquee(element);
};
if (!scheduled){
2018-08-20 14:20:03 +00:00
radio.schedule_get_metadata(stationid);
};
});
/* Save station to favorites */
2017-08-09 15:30:13 +00:00
$('body').on('click', '.favorite', function() {
2017-08-05 11:21:11 +00:00
var stationid = $(this).parent().parent().attr('data-id');
2018-08-20 14:20:03 +00:00
radio.action_favorite(stationid);
});
2017-08-11 16:06:01 +00:00
/* Playbutton click */
$('#playbutton').click(function() {
var music = document.getElementById('player');
if (music.paused && $("#player").attr("src") != "") {
music.play();
} else {
music.pause();
}
});
2017-08-05 11:21:11 +00:00
/* ==============
// ACTIONS //
/===============*/
2018-08-20 14:20:03 +00:00
radio.schedule_get_metadata = function(stationid){
scheduled = true;
var stationid = $('#player').attr('data-id');
req = $.get('https://onny.project-insanity.org/getmetadata/'+stationid, function(data) {
2018-08-04 11:31:29 +00:00
if (data.hasOwnProperty('metadata')) {
var metadata = data['metadata'].toString();
if (metadata != $("#station_metadata").text()) {
$("#station_metadata").html(metadata);
var element = document.getElementById('station_metadata');
2018-08-20 14:20:03 +00:00
if (radio.isElementOverflowing(element)) {
radio.wrapContentsInMarquee(element);
2018-08-04 11:31:29 +00:00
};
};
2018-08-20 14:20:03 +00:00
setTimeout(function(){radio.schedule_get_metadata(stationid);}, 10000);
2018-08-04 11:31:29 +00:00
} else {
scheduled = false;
};
}).fail(function(){
scheduled = false;
});
};
2018-08-20 14:20:03 +00:00
radio.action_favorite = function(stationid){
2017-08-05 11:21:11 +00:00
2017-08-06 21:23:26 +00:00
var removed = false;
var stations = [];
2017-08-05 11:21:11 +00:00
var baseUrl = OC.generateUrl('/apps/radio');
2017-08-06 21:23:26 +00:00
2017-08-05 11:21:11 +00:00
$.get(baseUrl + '/stations', function ( data ) {
for (var station in data) {
if (data[station]["stationid"] == stationid){
removed = true;
$.ajax({
url: baseUrl + '/stations/' + data[station]["id"],
method: 'DELETE'
}).done(function(){
2017-08-09 15:30:13 +00:00
$( "tr[data-id='" + stationid + "']" ).find('.icon-stationfav').removeClass('starred');
2017-08-05 11:21:11 +00:00
});
};
};
if (removed == true) {
return true;
} else {
var station = {
"stationid": stationid
};
$.ajax({
url: baseUrl + '/stations',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(station)
2017-08-05 17:41:29 +00:00
}).done(function(){
$( "tr[data-id='"+stationid+"']" ).find('.icon-stationfav').addClass('starred');
2017-08-05 11:21:11 +00:00
});
};
});
};
2018-08-20 14:20:03 +00:00
radio.action_play = function(stationid){
2017-08-06 21:23:26 +00:00
$.ajax({
method: "GET",
url: "https://www.radio-browser.info/webservice/v2/json/url/"+stationid,
2017-08-06 21:23:26 +00:00
dataType: 'json',
success: function(data){
var streamurl = data['url'];
$("#player").attr("src", streamurl);
$('#player').trigger('play');
2017-08-06 21:23:26 +00:00
}
});
2017-01-08 08:36:13 +00:00
};
2018-08-20 14:20:03 +00:00
radio.render_results = function(data){
2017-08-05 17:41:29 +00:00
var baseUrl = OC.generateUrl('/apps/radio');
$.get(baseUrl + '/stations', function ( fav_stations ) {
2017-08-11 17:05:39 +00:00
if (data.length == 0) {
$('#filestable').hide();
$('#emptycontent').addClass('hidden');
$('.nofilterresults').removeClass('hidden');
$('.loading').addClass('hidden');
} else {
$('#emptycontent').addClass('hidden');
$('#filestable').show();
$('.nofilterresults').addClass('hidden');
$('.loading').addClass('hidden');
};
2017-08-05 17:41:29 +00:00
$.each(data, function(i, station) {
var isstarred = ""
2017-08-11 16:06:01 +00:00
2017-08-05 17:41:29 +00:00
for (var fav_station in fav_stations) {
if (fav_stations[fav_station]["stationid"] == station['id']) {
isstarred = "starred";
break;
}
};
$('tbody').append('<tr data-src='+station['url']+' data-id='+station['id']+'>\
<td class="favcolumn">\
2017-08-09 15:30:13 +00:00
<a href="#" class="favorite">\
2017-08-05 17:41:29 +00:00
<span class="icon-stationfav ' + isstarred + '"></span>\
<span class="hidden-visually">Favorite</span>\
</a>\
</td>\
<td class="filename">\
<label for="select-files-3">\
<div class="thumbnail" style="background-image:url('+station['favicon']+'); background-size: 32px;"></div>\
</label>\
<a class="name" href="#">\
<span class="nametext"><span class="innernametext">'+station['name']+'</span></span>\
</a>\
</td>\
</tr>');
});
2016-11-17 22:07:22 +00:00
});
}
2018-08-20 14:20:03 +00:00
radio.action_load_favorites = function(){
var stations = []
2017-02-21 16:57:30 +00:00
var baseUrl = OC.generateUrl('/apps/radio');
2017-08-25 20:16:36 +00:00
2017-02-21 16:57:30 +00:00
$.get(baseUrl + '/stations', function ( data ) {
for (var station in data) {
stations.push(data[station]["stationid"]);
};
2017-08-11 16:06:01 +00:00
if (stations.length == 0) {
$('#filestable').hide();
$('#emptycontent').removeClass('hidden');
2017-08-11 17:05:39 +00:00
$('.loading').addClass('hidden');
2017-08-11 16:06:01 +00:00
} else {
2018-08-20 14:20:03 +00:00
radio.query_stations(stations);
2017-08-11 16:06:01 +00:00
};
2017-02-16 12:00:43 +00:00
});
2017-01-08 08:36:13 +00:00
};
2018-08-20 14:20:03 +00:00
radio.query_stations = function(station_ids){
2017-08-09 22:24:46 +00:00
var station_array = [];
2017-08-25 20:16:36 +00:00
station_ids.forEach(function (station_id, idx) {
2017-01-08 08:36:13 +00:00
$.ajax({
method: "GET",
2017-08-25 20:16:36 +00:00
url: "https://www.radio-browser.info/webservice/json/stations/byid/"+station_ids[idx],
2017-01-08 08:36:13 +00:00
dataType: 'json',
2017-08-08 17:47:45 +00:00
}).success( function(data){
2017-08-09 22:24:46 +00:00
station_array = station_array.concat(data);
2017-08-25 20:16:36 +00:00
if (station_ids.length == (idx+1)){
2018-08-20 14:20:03 +00:00
radio.render_results(station_array);
2017-08-09 22:24:46 +00:00
};
2017-01-08 08:36:13 +00:00
});
2017-08-25 20:16:36 +00:00
});
2017-01-08 08:36:13 +00:00
};
2018-08-20 14:20:03 +00:00
radio.radio_query = function(type, query){
2017-08-08 17:47:45 +00:00
var offset = $('tbody > tr').length;
2017-01-06 09:52:12 +00:00
switch (type) {
case 0:
var url = "https://www.radio-browser.info/webservice/json/stations/search";
break;
case 1:
2017-08-08 17:47:45 +00:00
var url = "https://www.radio-browser.info/webservice/json/stations/topclick";
2017-01-06 09:52:12 +00:00
break;
case 2:
2017-08-08 17:47:45 +00:00
var url = "https://www.radio-browser.info/webservice/json/stations/lastchange";
2017-01-06 09:52:12 +00:00
break;
};
2016-11-17 22:07:22 +00:00
$.ajax({
method: "POST",
2017-01-06 09:52:12 +00:00
url: url,
2016-11-17 22:07:22 +00:00
data: {
2017-01-09 14:43:56 +00:00
name: query,
2017-08-08 17:47:45 +00:00
limit: 20,
offset: offset
2016-11-17 22:07:22 +00:00
},
dataType: 'json',
2017-08-08 17:47:45 +00:00
}).success( function(data){
2018-08-20 14:20:03 +00:00
radio.render_results(data);
2018-08-11 12:53:01 +00:00
});
2017-08-11 16:06:01 +00:00
};
2017-01-08 08:36:13 +00:00
2018-08-07 14:48:50 +00:00
// Search function
new OCA.Search(function (query) {
$('#app-navigation').find('li').removeClass("active");
$("tbody > tr").remove();
2018-08-20 14:20:03 +00:00
radio.radio_query(0, query);
2018-08-07 14:48:50 +00:00
}, function (arg) {
2018-08-20 14:20:03 +00:00
radio.switch_menu(0);
2018-08-07 14:48:50 +00:00
});
2017-01-09 13:36:07 +00:00
2018-08-20 14:20:03 +00:00
radio.debounce = function(func, wait, immediate) {
2018-08-15 15:34:20 +00:00
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
2018-08-07 14:48:50 +00:00
// Volume slider
$('#volumeslider').slider({
orientation: "horizontal",
value: player.volume,
min: 0,
max: 1,
range: 'min',
animate: true,
step: .1,
slide: function(e, ui) {
2018-08-15 15:55:05 +00:00
player.volume = ui.value;
2018-08-16 21:13:18 +00:00
if (ui.value < 0.1) {
2018-08-14 14:05:20 +00:00
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","silent");
}
if (ui.value > 0.5) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","full");
}
2018-08-16 21:13:18 +00:00
if (ui.value <= 0.5 && ui.value >= 0.1) {
2018-08-14 14:05:20 +00:00
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","mid");
}
}
});
2018-08-20 14:20:03 +00:00
$("#volumeslider").on("slidestop", radio.debounce(function(e, ui) {
radio.save_volume_state(ui.value);
2018-08-15 15:34:20 +00:00
}, 700)
);
$("#volumeicon").on("click", function(e) {
if (player.volume > 0) {
last_volume = player.volume;
player.volume = 0;
$('#volumeslider').slider('value',0);
2018-08-20 14:20:03 +00:00
radio.save_volume_state(0);
} else {
if (!last_volume == 0) {
player.volume = last_volume;
$('#volumeslider').slider('value',last_volume);
2018-08-20 14:20:03 +00:00
radio.save_volume_state(last_volume);
} else {
player.volume = 1;
$('#volumeslider').slider('value',1);
2018-08-20 14:20:03 +00:00
radio.save_volume_state(1);
}
}
2018-08-16 21:13:18 +00:00
if (player.volume < 0.1) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","silent");
}
if (player.volume > 0.5) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","full");
}
2018-08-16 21:13:18 +00:00
if (player.volume <= 0.5 && player.volume >= 0.1) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","mid");
}
});
2018-08-20 14:20:03 +00:00
radio.load_volume_state = function(){
2018-08-15 15:34:20 +00:00
var volume_state = 0;
var baseUrl = OC.generateUrl('/apps/radio/getVolumeState');
$.get(baseUrl, function ( data ) {
if ("volume_state" in data) {
volume_state = data["volume_state"];
if (volume_state == "") {
player.volume = 1;
last_volume = 1;
2018-08-15 15:34:20 +00:00
$('#volumeslider').slider('value',1);
} else {
last_volume = volume_state;
2018-08-15 15:34:20 +00:00
player.volume = Number(volume_state);
$('#volumeslider').slider('value',Number(volume_state));
2018-08-16 21:13:18 +00:00
if (volume_state < 0.1) {
2018-08-15 15:34:20 +00:00
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","silent");
}
if (volume_state > 0.5) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","full");
}
2018-08-16 21:13:18 +00:00
if (volume_state <= 0.5 && volume_state >= 0.1) {
2018-08-15 15:34:20 +00:00
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","mid");
}
}
}
return true;
});
}
2018-08-20 14:20:03 +00:00
radio.save_volume_state = function(volume_state) {
2018-08-15 15:34:20 +00:00
var baseUrl = OC.generateUrl('/apps/radio/saveVolumeState');
var settings = {
"volume_state": volume_state
};
$.ajax({
url: baseUrl,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(settings)
}).done(function(data){
return true;
});
}
// Load volume state
2018-08-20 14:20:03 +00:00
radio.load_volume_state();
2018-08-15 15:34:20 +00:00
2018-08-20 14:20:03 +00:00
return radio;
}(MODULE || {}));