nextcloud-app-radio/js/main.js

307 lines
9.4 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
2017-08-09 15:30:13 +00:00
$(document).ready(function () {
var version = "0.7.0";
2019-05-11 15:03:20 +00:00
var current_stationid = "";
var searchTimeout = null;
var scheduled = false;
2018-08-06 15:07:52 +00:00
// 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);
2019-05-12 11:16:32 +00:00
var hashurl = window.location.hash;
radio.radio_query(hashurl);
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
});
2017-08-05 11:21:11 +00:00
/* ==============
// ACTIONS //
/===============*/
2018-08-20 14:20:03 +00:00
radio.schedule_get_metadata = function(stationid){
2019-05-11 15:03:20 +00:00
radio.scheduled = true;
var stationid = radio.current_stationid;
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 {
2019-05-11 15:03:20 +00:00
radio.scheduled = false;
};
}).fail(function(){
2019-05-11 15:03:20 +00:00
radio.scheduled = false;
});
};
2018-08-20 14:20:03 +00:00
radio.action_favorite = function(stationid){
2019-05-11 15:03:20 +00:00
var exists = false;
2017-08-05 11:21:11 +00:00
var baseUrl = OC.generateUrl('/apps/radio');
$.get(baseUrl + '/stations', function ( data ) {
for (var station in data) {
if (data[station]["stationid"] == stationid){
2019-05-11 15:03:20 +00:00
exists = true;
2017-08-05 11:21:11 +00:00
};
};
2019-05-11 15:03:20 +00:00
if (exists == true) {
$.ajax({
url: baseUrl + '/stations/' + data[station]["id"],
method: 'DELETE'
}).done(function(){
console.log("delete ", stationid);
$( "tr[data-src='#station/"+stationid+"']").find('.icon-stationfav').removeClass('starred');
});
2017-08-05 11:21:11 +00:00
} 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(){
2019-05-11 15:03:20 +00:00
$( "tr[data-src='#station/"+stationid+"']" ).find('.icon-stationfav').addClass('starred');
2017-08-05 11:21:11 +00:00
});
};
});
};
2019-05-11 15:03:20 +00:00
radio.parse_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) {
if (typeof station['stationuuid'] == "undefined") {
2019-05-12 11:16:32 +00:00
var entry_type = "folder";
var isstarred = false;
var hashurl = window.location.hash + "/" + station['name'];
} else {
var entry_type = "station";
var isstarred = false;
for (var fav_station in fav_stations) {
if (fav_stations[fav_station]["stationid"] == station['stationuuid']) {
2019-05-12 11:16:32 +00:00
isstarred = true;
break;
}
};
var hashurl = '#station/'+station['stationuuid'];
2019-05-12 11:16:32 +00:00
}
radio.render_entry(entry_type, isstarred, hashurl, station['favicon'], station['name']);
2017-08-05 17:41:29 +00:00
});
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 {
console.log(stations);
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 = [];
var station_array_new = [];
2017-08-25 20:16:36 +00:00
station_ids.forEach(function (station_id, idx) {
var url = "https://de1.api.radio-browser.info/json/stations/byid/"+station_ids[idx];
2019-05-11 15:03:20 +00:00
$.getJSON( url , {"User-Agent": "Nextcloud Radio/"+radio.version }, function( data ) {
2017-08-25 20:16:36 +00:00
if (data.length === 0) {
station_array = station_array.concat([{'id': station_id, 'name': 'Broken radio station entry :(', 'favicon': '', 'url': ''}])
} else {
station_array = station_array.concat(data);
};
if (station_ids.length == station_array.length){
// sorting results array
station_ids.forEach( function (station_id) {
station_array.forEach( function (station) {
if (station_id === station['id']) {
station_array_new = station_array_new.concat(station);
};
});
});
2019-05-11 15:03:20 +00:00
radio.parse_results(station_array_new);
2017-08-09 22:24:46 +00:00
};
2017-08-25 20:16:36 +00:00
});
2017-08-25 20:16:36 +00:00
});
2017-01-08 08:36:13 +00:00
};
2019-05-11 15:03:20 +00:00
radio.radio_query = function(hashurl){
2017-08-08 17:47:45 +00:00
var offset = $('tbody > tr').length;
2019-05-11 15:03:20 +00:00
var query = "";
var api_baseurl = "https://de1.api.radio-browser.info/json/";
2019-05-11 15:03:20 +00:00
switch (hashurl) {
case "#top":
var query_url = "stations/topclick";
break;
case "#recent":
var query_url = "stations/lastchange";
2017-01-06 09:52:12 +00:00
break;
2019-05-12 11:16:32 +00:00
case (hashurl.match(/^#categories/) || {}).input:
var query_url = hashurl.substring(12);
if (query_url.split("/").length == 2) {
var subcategory = query_url.split("/")[1];
switch (query_url.split("/")[0]) {
case "countries":
var query_url = "stations/bycountryexact/"+subcategory;
break;
case "states":
var query_url = "stations/bystateexact/"+subcategory;
break;
case "languages":
var query_url = "stations/bylanguageexact/"+subcategory;
break;
case "tags":
var query_url = "stations/bytagsexact/"+subcategory;
break;
}
}
2019-05-11 15:03:20 +00:00
break;
case (hashurl.match(/^#search/) || {}).input:
var query_url = "stations/search";
var query = hashurl.substring(8);
2017-01-06 09:52:12 +00:00
break;
};
2019-05-12 11:16:32 +00:00
console.log(query_url);
2016-11-17 22:07:22 +00:00
$.ajax({
method: "POST",
2019-05-11 15:03:20 +00:00
url: api_baseurl + query_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,
2019-05-11 15:03:20 +00:00
"User-Agent": "Nextcloud Radio/"+radio.version,
2017-08-08 17:47:45 +00:00
offset: offset
2016-11-17 22:07:22 +00:00
},
dataType: 'json',
2017-08-08 17:47:45 +00:00
}).success( function(data){
2019-05-12 11:16:32 +00:00
if (data.length) {
radio.parse_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();
2019-05-12 11:16:32 +00:00
var hashurl = "#search/"+query;
history.pushState("", "", hashurl);
radio.radio_query(hashurl);
2018-08-07 14:48:50 +00:00
}, function (arg) {
2019-05-12 11:16:32 +00:00
console.log("quit search dialog");
2018-08-07 14:48:50 +00:00
});
2017-01-09 13:36:07 +00:00
2019-05-11 15:03:20 +00:00
radio.render_entry = function(entry_type, starred, hashurl, iconurl, title){
if (entry_type == "folder") {
var icon_class = "icon-stationfav-disabled";
var icon = "icon-filetype-folder";
} else {
var icon_class = "icon-stationfav";
var icon = '" style="background-image:url('+iconurl+'); background-size: 32px;';
}
if (starred) {
var starred = "starred";
} else {
var starred = "";
}
$('tbody').append('<tr data-src="'+hashurl+'">\
<td class="favcolumn">\
<a href="#" class="favorite">\
2019-05-11 15:03:20 +00:00
<span class="'+icon_class+' '+starred+'"></span>\
<span class="hidden-visually">Favorite</span>\
</a>\
</td>\
<td class="filename">\
<label for="select-files-3">\
2019-05-11 15:03:20 +00:00
<div class="thumbnail '+icon+'"></div>\
</label>\
<a class="name" href="#">\
2019-05-11 15:03:20 +00:00
<span class="nametext"><span class="innernametext">'+title+'</span></span>\
</a>\
</td>\
2019-05-11 15:03:20 +00:00
</tr>')
}
radio.action_load_categories = function (){
$('#emptycontent').addClass('hidden');
$('#filestable').show();
$('.nofilterresults').addClass('hidden');
$('.loading').addClass('hidden');
var categories = [{
'hashurl': '#categories/countries',
'name': 'Countries'
2019-05-11 15:03:20 +00:00
},{
'hashurl': '#categories/states',
'name': 'States'
},{
'hashurl': '#categories/languages',
'name': 'Languages'
},{
'hashurl': '#categories/tags',
'name': 'Tags'
}];
categories.forEach(function(category_entry) {
radio.render_entry("folder", false, category_entry["hashurl"], false, category_entry["name"]);
})
}
2018-08-20 14:20:03 +00:00
return radio;
}(MODULE || {}));