$(function(){ searchTimeout = null; /* ============== // EVENTS // /===============*/ /* Click on a radio station (table entry) and play it */ $('body').on('click', '.filename', function() { var stationid = $(this).parent().attr('data-id'); action_play(stationid); }); /* Save station to favorites */ $('body').on('click', '.favpls', function() { var stationid = $(this).parent().parent().attr('data-id'); action_favorite(stationid); }); /* Scroll event handler for continious scrolling */ $('#app-content').data("didfire", false).scroll(function() { if ($(this).height() == ($(this).get(0).scrollHeight - $(this).scrollTop())) { if(!$(this).data("didfire")) { $(this).data("didfire", true); radio_query(1, ""); var ref = $(this); setTimeout(function(){ ref.data("didfire", false); }, 500); }; }; }); /* ============== // ACTIONS // /===============*/ function action_favorite(stationid){ var removed = false; var stations = [] var baseUrl = OC.generateUrl('/apps/radio'); $.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(){ $( "tr[data-id='"+data[station]["stationid"]+"']" ).find('.icon-stationfav').removeClass('starred'); }); }; }; if (removed == true) { return true; } else { var station = { "stationid": stationid }; $.ajax({ url: baseUrl + '/stations', method: 'POST', contentType: 'application/json', data: JSON.stringify(station) }).done(function(){ $( "tr[data-id='"+stationid+"']" ).find('.icon-stationfav').addClass('starred'); }); }; }); }; function action_play(stationid){ $.ajax({ method: "GET", url: "https://onny.project-insanity.org/getmetadata/"+stationid, dataType: 'json', success: function(data){ var title = data['metadata']; $("#station_metadata").html(title); } }); $.ajax({ method: "GET", url: "https://www.radio-browser.info/webservice/v2/json/url/"+stationid, dataType: 'json', success: function(data){ var sourceUrl = data['url']; $("#player").attr("src", sourceUrl); $('#player').trigger('play'); $('#playbutton').attr("class", "pause"); } }); }; function render_result(data){ var baseUrl = OC.generateUrl('/apps/radio'); $.get(baseUrl + '/stations', function ( fav_stations ) { $.each(data, function(i, station) { var isstarred = "" for (var fav_station in fav_stations) { if (fav_stations[fav_station]["stationid"] == station['id']) { isstarred = "starred"; break; } }; $('tbody').append('