$(function(){ searchTimeout = null; var scheduled; function isElementOverflowing(element) { var overflowX = element.offsetWidth < element.scrollWidth, overflowY = element.offsetHeight < element.scrollHeight; return (overflowX || overflowY); } function wrapContentsInMarquee(element) { var marquee = document.createElement('marquee'), contents = element.innerText; marquee.innerText = contents; element.innerHTML = ''; element.appendChild(marquee); } /* ============== // EVENTS // /===============*/ /* Click on a radio station (table entry) and play it */ $('body').on('click', '.filename', function() { var stationid = $(this).parent().attr('data-id'); var stationname = $(this).parent().find('.nametext').text(); action_play(stationid); $('#player').attr('data-id',stationid); $("#station_metadata").html("Playing: "+stationname); var element = document.getElementById('station_metadata'); if (isElementOverflowing(element)) { wrapContentsInMarquee(element); }; if (!scheduled){ schedule_get_metadata(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); if ($('li.nav-files').hasClass('active')){ radio_query(1, ""); } else if ($('li.nav-recent').hasClass('active')) { radio_query(2, ""); } else if ($('li.nav-favorites').hasClass('active')) { console.log("todo"); } else { var query = $('#searchbox').val(); radio_query(0, query); }; var ref = $(this); setTimeout(function(){ ref.data("didfire", false); }, 500); }; }; }); /* ============== // ACTIONS // /===============*/ function schedule_get_metadata(stationid){ scheduled = true; var stationid = $('#player').attr('data-id'); req = $.get('https://onny.project-insanity.org/getmetadata/'+stationid, function(data) { var metadata = data['metadata']; if (metadata != $("#station_metadata").text()) { $("#station_metadata").html(metadata); var element = document.getElementById('station_metadata'); if (isElementOverflowing(element)) { wrapContentsInMarquee(element); }; }; setTimeout(function(){schedule_get_metadata(stationid);}, 10000); }).fail(function(){ scheduled = false; }); }; 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://www.radio-browser.info/webservice/v2/json/url/"+stationid, dataType: 'json', success: function(data){ var streamurl = data['url']; $("#player").attr("src", streamurl); $('#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('\ \ \ \ Favorite\ \ \ \ \ \ '+station['name']+'\ \ \ '); }); }); } function action_load_favorites(){ var stations = [] var baseUrl = OC.generateUrl('/apps/radio'); $.get(baseUrl + '/stations', function ( data ) { for (var station in data) { stations.push(data[station]["stationid"]); }; render_stations(stations); }); }; function render_stations(station_ids){ for (stationid in station_ids){ $.ajax({ method: "GET", url: "https://www.radio-browser.info/webservice/json/stations/byid/"+station_ids[stationid], dataType: 'json', }).success( function(data){ render_result(data); }); }; }; function radio_query(type, query){ var offset = $('tbody > tr').length; switch (type) { case 0: var url = "https://www.radio-browser.info/webservice/json/stations/search"; break; case 1: var url = "https://www.radio-browser.info/webservice/json/stations/topclick"; break; case 2: var url = "https://www.radio-browser.info/webservice/json/stations/lastchange"; break; }; $.ajax({ method: "POST", url: url, data: { name: query, limit: 20, offset: offset }, dataType: 'json', }).success( function(data){ render_result(data); }); }; function switch_menu(type) { clearTimeout(searchTimeout); searchTimeout = setTimeout(function(){ $('#app-navigation').find('li').removeClass("active"); $("tbody > tr").remove(); switch (type) { case 0: $('li.nav-files').addClass('active'); radio_query(1); break; case 1: $('li.nav-recent').addClass('active'); radio_query(2); break; case 2: $('li.nav-favorites').addClass('active'); action_load_favorites(); break; } }, 500); } $('a.nav-icon-files').click(function() { switch_menu(0); }); $('a.nav-icon-recent').click(function() { switch_menu(1); }); $('a.nav-icon-favorites').click(function() { switch_menu(2); }); function mySearch(query){ if (query != "") { clearTimeout(searchTimeout); searchTimeout = setTimeout(function(){ $('#app-navigation').find('li').removeClass("active"); $("tbody > tr").remove(); radio_query(0, query); }, 500); }; }; OC.Plugins.register('OCA.Search', { attach: function(search) { search.setFilter('radio', mySearch); } }); $('#playbutton').click(function() { var music = document.getElementById('player'); if (music.paused && $("#player").attr("src") != "") { music.play(); playbutton.className = ""; playbutton.className = "pause"; } else { music.pause(); playbutton.className = ""; playbutton.className = "play"; } }); $('#volumeslider').slider({ orientation: "horizontal", value: player.volume, min: 0, max: 1, range: 'min', animate: true, step: .1, slide: function(e, ui) { player.volume = ui.value; } }); switch_menu(0); });