var MODULE = (function (radio) { $(document).ready(function () { var version = "0.7.0"; var current_stationid = ""; var searchTimeout = null; var scheduled = false; // 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); var hashurl = window.location.hash; radio.radio_query(hashurl); var ref = station_list; setTimeout(function(){ ref.data("didfire", false); }, 500); }; }; }); }); /* ============== // ACTIONS // /===============*/ radio.schedule_get_metadata = function(stationid){ radio.scheduled = true; var stationid = radio.current_stationid; req = $.get('https://onny.project-insanity.org/getmetadata/'+stationid, function(data) { if (data.hasOwnProperty('metadata')) { var metadata = data['metadata'].toString(); if (metadata != $("#station_metadata").text()) { $("#station_metadata").html(metadata); var element = document.getElementById('station_metadata'); if (radio.isElementOverflowing(element)) { radio.wrapContentsInMarquee(element); }; }; setTimeout(function(){radio.schedule_get_metadata(stationid);}, 10000); } else { radio.scheduled = false; }; }).fail(function(){ radio.scheduled = false; }); }; radio.action_favorite = function(stationid){ var exists = false; var baseUrl = OC.generateUrl('/apps/radio'); $.get(baseUrl + '/stations', function ( data ) { for (var station in data) { if (data[station]["stationid"] == stationid){ exists = true; }; }; 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'); }); } else { var station = { "stationid": stationid }; $.ajax({ url: baseUrl + '/stations', method: 'POST', contentType: 'application/json', data: JSON.stringify(station) }).done(function(){ $( "tr[data-src='#station/"+stationid+"']" ).find('.icon-stationfav').addClass('starred'); }); }; }); }; radio.parse_results = function(data){ var baseUrl = OC.generateUrl('/apps/radio'); $.get(baseUrl + '/stations', function ( fav_stations ) { 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'); }; $.each(data, function(i, station) { if (typeof station['stationuuid'] == "undefined") { 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']) { isstarred = true; break; } }; var hashurl = '#station/'+station['stationuuid']; } radio.render_entry(entry_type, isstarred, hashurl, station['favicon'], station['name']); }); }); } radio.action_load_favorites = function(){ var stations = [] var baseUrl = OC.generateUrl('/apps/radio'); $.get(baseUrl + '/stations', function ( data ) { for (var station in data) { stations.push(data[station]["stationid"]); }; if (stations.length == 0) { $('#filestable').hide(); $('#emptycontent').removeClass('hidden'); $('.loading').addClass('hidden'); } else { console.log(stations); radio.query_stations(stations); }; }); }; radio.query_stations = function(station_ids){ var station_array = []; var station_array_new = []; station_ids.forEach(function (station_id, idx) { var url = "https://de1.api.radio-browser.info/json/stations/byid/"+station_ids[idx]; $.getJSON( url , {"User-Agent": "Nextcloud Radio/"+radio.version }, function( data ) { 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); }; }); }); radio.parse_results(station_array_new); }; }); }); }; radio.radio_query = function(hashurl){ var offset = $('tbody > tr').length; var query = ""; var api_baseurl = "https://de1.api.radio-browser.info/json/"; switch (hashurl) { case "#top": var query_url = "stations/topclick"; break; case "#recent": var query_url = "stations/lastchange"; break; 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; } } break; case (hashurl.match(/^#search/) || {}).input: var query_url = "stations/search"; var query = hashurl.substring(8); break; }; console.log(query_url); $.ajax({ method: "POST", url: api_baseurl + query_url, data: { name: query, limit: 20, "User-Agent": "Nextcloud Radio/"+radio.version, offset: offset }, dataType: 'json', }).success( function(data){ if (data.length) { radio.parse_results(data); } }); }; // Search function new OCA.Search(function (query) { $('#app-navigation').find('li').removeClass("active"); $("tbody > tr").remove(); var hashurl = "#search/"+query; history.pushState("", "", hashurl); radio.radio_query(hashurl); }, function (arg) { console.log("quit search dialog"); }); 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('