split up javascript files

This commit is contained in:
Jonas Heinrich 2018-08-20 16:20:03 +02:00
parent f3f316bfe1
commit af4632b991
4 changed files with 141 additions and 135 deletions

View File

@ -4,7 +4,7 @@
<id>radio</id> <id>radio</id>
<name>Radio</name> <name>Radio</name>
<description>Listening to your favorite radio stations in Nextcloud</description> <description>Listening to your favorite radio stations in Nextcloud</description>
<version>0.6.2</version> <version>0.6.3</version>
<licence>agpl</licence> <licence>agpl</licence>
<author>Jonas Heinrich</author> <author>Jonas Heinrich</author>
<documentation> <documentation>

View File

@ -1,4 +1,4 @@
$(function(){ var MODULE = (function (radio) {
var last_volume = 0; var last_volume = 0;
@ -23,14 +23,14 @@ $(function(){
if(!station_list.data("didfire")) { if(!station_list.data("didfire")) {
station_list.data("didfire", true); station_list.data("didfire", true);
if ($('li.nav-files').hasClass('active')){ if ($('li.nav-files').hasClass('active')){
radio_query(1, ""); radio.radio_query(1, "");
} else if ($('li.nav-recent').hasClass('active')) { } else if ($('li.nav-recent').hasClass('active')) {
radio_query(2, ""); radio.radio_query(2, "");
} else if ($('li.nav-favorites').hasClass('active')) { } else if ($('li.nav-favorites').hasClass('active')) {
console.log("todo"); console.log("todo");
} else { } else {
var query = $('#searchbox').val(); var query = $('#searchbox').val();
radio_query(0, query); radio.radio_query(0, query);
}; };
var ref = station_list; var ref = station_list;
setTimeout(function(){ setTimeout(function(){
@ -45,14 +45,14 @@ $(function(){
searchTimeout = null; searchTimeout = null;
var scheduled; var scheduled;
function isElementOverflowing(element) { radio.isElementOverflowing = function(element) {
var overflowX = element.offsetWidth < element.scrollWidth, var overflowX = element.offsetWidth < element.scrollWidth,
overflowY = element.offsetHeight < element.scrollHeight; overflowY = element.offsetHeight < element.scrollHeight;
return (overflowX || overflowY); return (overflowX || overflowY);
} }
function wrapContentsInMarquee(element) { radio.wrapContentsInMarquee = function(element) {
var marquee = document.createElement('marquee'), var marquee = document.createElement('marquee'),
contents = element.innerText; contents = element.innerText;
@ -71,7 +71,7 @@ $(function(){
$('#playbutton').attr("class", "buffering"); $('#playbutton').attr("class", "buffering");
var stationid = $(this).parent().attr('data-id'); var stationid = $(this).parent().attr('data-id');
var stationname = $(this).parent().find('.nametext').text(); var stationname = $(this).parent().find('.nametext').text();
action_play(stationid); radio.action_play(stationid);
$('#player').attr('data-id',stationid); $('#player').attr('data-id',stationid);
$("#station_metadata").html("Playing: "+stationname); $("#station_metadata").html("Playing: "+stationname);
@ -80,18 +80,18 @@ $(function(){
$(this).parent().css("background-color", "#f8f8f8"); $(this).parent().css("background-color", "#f8f8f8");
var element = document.getElementById('station_metadata'); var element = document.getElementById('station_metadata');
if (isElementOverflowing(element)) { if (radio.isElementOverflowing(element)) {
wrapContentsInMarquee(element); radio.wrapContentsInMarquee(element);
}; };
if (!scheduled){ if (!scheduled){
schedule_get_metadata(stationid); radio.schedule_get_metadata(stationid);
}; };
}); });
/* Save station to favorites */ /* Save station to favorites */
$('body').on('click', '.favorite', function() { $('body').on('click', '.favorite', function() {
var stationid = $(this).parent().parent().attr('data-id'); var stationid = $(this).parent().parent().attr('data-id');
action_favorite(stationid); radio.action_favorite(stationid);
}); });
/* Playbutton click */ /* Playbutton click */
@ -104,27 +104,11 @@ $(function(){
} }
}); });
/* Click on menus */
$('a.nav-icon-files').click(function(e) {
e.preventDefault();
switch_menu(0);
});
$('a.nav-icon-recent').click(function(e) {
e.preventDefault();
switch_menu(1);
});
$('a.nav-icon-favorites').click(function(e) {
e.preventDefault();
switch_menu(2);
});
/* ============== /* ==============
// ACTIONS // // ACTIONS //
/===============*/ /===============*/
function schedule_get_metadata(stationid){ radio.schedule_get_metadata = function(stationid){
scheduled = true; scheduled = true;
var stationid = $('#player').attr('data-id'); var stationid = $('#player').attr('data-id');
req = $.get('https://onny.project-insanity.org/getmetadata/'+stationid, function(data) { req = $.get('https://onny.project-insanity.org/getmetadata/'+stationid, function(data) {
@ -133,11 +117,11 @@ $(function(){
if (metadata != $("#station_metadata").text()) { if (metadata != $("#station_metadata").text()) {
$("#station_metadata").html(metadata); $("#station_metadata").html(metadata);
var element = document.getElementById('station_metadata'); var element = document.getElementById('station_metadata');
if (isElementOverflowing(element)) { if (radio.isElementOverflowing(element)) {
wrapContentsInMarquee(element); radio.wrapContentsInMarquee(element);
}; };
}; };
setTimeout(function(){schedule_get_metadata(stationid);}, 10000); setTimeout(function(){radio.schedule_get_metadata(stationid);}, 10000);
} else { } else {
scheduled = false; scheduled = false;
}; };
@ -146,7 +130,7 @@ $(function(){
}); });
}; };
function action_favorite(stationid){ radio.action_favorite = function(stationid){
var removed = false; var removed = false;
var stations = []; var stations = [];
@ -183,7 +167,7 @@ $(function(){
}); });
}; };
function action_play(stationid){ radio.action_play = function(stationid){
$.ajax({ $.ajax({
method: "GET", method: "GET",
url: "https://www.radio-browser.info/webservice/v2/json/url/"+stationid, url: "https://www.radio-browser.info/webservice/v2/json/url/"+stationid,
@ -196,7 +180,7 @@ $(function(){
}); });
}; };
function render_results(data){ radio.render_results = function(data){
var baseUrl = OC.generateUrl('/apps/radio'); var baseUrl = OC.generateUrl('/apps/radio');
$.get(baseUrl + '/stations', function ( fav_stations ) { $.get(baseUrl + '/stations', function ( fav_stations ) {
@ -245,7 +229,7 @@ $(function(){
}); });
} }
function action_load_favorites(){ radio.action_load_favorites = function(){
var stations = [] var stations = []
var baseUrl = OC.generateUrl('/apps/radio'); var baseUrl = OC.generateUrl('/apps/radio');
@ -258,12 +242,12 @@ $(function(){
$('#emptycontent').removeClass('hidden'); $('#emptycontent').removeClass('hidden');
$('.loading').addClass('hidden'); $('.loading').addClass('hidden');
} else { } else {
query_stations(stations); radio.query_stations(stations);
}; };
}); });
}; };
function query_stations(station_ids){ radio.query_stations = function(station_ids){
var station_array = []; var station_array = [];
station_ids.forEach(function (station_id, idx) { station_ids.forEach(function (station_id, idx) {
@ -275,7 +259,7 @@ $(function(){
}).success( function(data){ }).success( function(data){
station_array = station_array.concat(data); station_array = station_array.concat(data);
if (station_ids.length == (idx+1)){ if (station_ids.length == (idx+1)){
render_results(station_array); radio.render_results(station_array);
}; };
}); });
@ -283,7 +267,7 @@ $(function(){
}; };
function radio_query(type, query){ radio.radio_query = function(type, query){
var offset = $('tbody > tr').length; var offset = $('tbody > tr').length;
switch (type) { switch (type) {
case 0: case 0:
@ -306,81 +290,20 @@ $(function(){
}, },
dataType: 'json', dataType: 'json',
}).success( function(data){ }).success( function(data){
render_results(data); radio.render_results(data);
}); });
}; };
function load_menu_state() {
var menu_state = 0;
var baseUrl = OC.generateUrl('/apps/radio/getMenuState');
$.get(baseUrl, function ( data ) {
if ("menu_state" in data) {
menu_state = data["menu_state"];
}
switch_menu(menu_state);
return true;
});
}
function save_menu_state(menu_state) {
var baseUrl = OC.generateUrl('/apps/radio/saveMenuState');
var settings = {
"menu_state": menu_state
};
$.ajax({
url: baseUrl,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(settings)
}).done(function(data){
return true;
});
}
function switch_menu(type) {
var state = Number(type);
$('#filestable').hide();
$('#emptycontent').addClass('hidden');
$('.nofilterresults').addClass('hidden');
$('.loading').removeClass('hidden');
clearTimeout(searchTimeout);
searchTimeout = setTimeout(function(){
$('#app-navigation').find('li').removeClass("active");
$("tbody > tr").remove();
save_menu_state(state)
switch (state) {
case 0:
history.pushState("", "", "#top");
$('li.nav-files').addClass('active');
radio_query(1);
break;
case 1:
history.pushState("", "", "#recent");
$('li.nav-recent').addClass('active');
radio_query(2);
break;
case 2:
history.pushState("", "", "#favorites");
$('li.nav-favorites').addClass('active');
action_load_favorites();
break;
}
}, 500);
};
// Search function // Search function
new OCA.Search(function (query) { new OCA.Search(function (query) {
$('#app-navigation').find('li').removeClass("active"); $('#app-navigation').find('li').removeClass("active");
$("tbody > tr").remove(); $("tbody > tr").remove();
radio_query(0, query); radio.radio_query(0, query);
}, function (arg) { }, function (arg) {
switch_menu(0); radio.switch_menu(0);
}); });
function debounce(func, wait, immediate) { radio.debounce = function(func, wait, immediate) {
var timeout; var timeout;
return function() { return function() {
var context = this, var context = this,
@ -422,8 +345,8 @@ $(function(){
} }
}); });
$("#volumeslider").on("slidestop", debounce(function(e, ui) { $("#volumeslider").on("slidestop", radio.debounce(function(e, ui) {
save_volume_state(ui.value); radio.save_volume_state(ui.value);
}, 700) }, 700)
); );
@ -432,16 +355,16 @@ $("#volumeicon").on("click", function(e) {
last_volume = player.volume; last_volume = player.volume;
player.volume = 0; player.volume = 0;
$('#volumeslider').slider('value',0); $('#volumeslider').slider('value',0);
save_volume_state(0); radio.save_volume_state(0);
} else { } else {
if (!last_volume == 0) { if (!last_volume == 0) {
player.volume = last_volume; player.volume = last_volume;
$('#volumeslider').slider('value',last_volume); $('#volumeslider').slider('value',last_volume);
save_volume_state(last_volume); radio.save_volume_state(last_volume);
} else { } else {
player.volume = 1; player.volume = 1;
$('#volumeslider').slider('value',1); $('#volumeslider').slider('value',1);
save_volume_state(1); radio.save_volume_state(1);
} }
} }
if (player.volume < 0.1) { if (player.volume < 0.1) {
@ -458,7 +381,7 @@ $("#volumeicon").on("click", function(e) {
} }
}); });
function load_volume_state() { radio.load_volume_state = function(){
var volume_state = 0; var volume_state = 0;
var baseUrl = OC.generateUrl('/apps/radio/getVolumeState'); var baseUrl = OC.generateUrl('/apps/radio/getVolumeState');
$.get(baseUrl, function ( data ) { $.get(baseUrl, function ( data ) {
@ -490,7 +413,7 @@ $("#volumeicon").on("click", function(e) {
}); });
} }
function save_volume_state(volume_state) { radio.save_volume_state = function(volume_state) {
var baseUrl = OC.generateUrl('/apps/radio/saveVolumeState'); var baseUrl = OC.generateUrl('/apps/radio/saveVolumeState');
var settings = { var settings = {
"volume_state": volume_state "volume_state": volume_state
@ -505,28 +428,8 @@ $("#volumeicon").on("click", function(e) {
}); });
} }
// On app start, load top list
if(window.location.hash) {
var hash = String(window.location.hash.replace('#',''));
switch(hash) {
case "top":
switch_menu(0);
break;
case "recent":
switch_menu(1);
break;
case "favorites":
switch_menu(2);
break;
default:
switch_menu(0);
break;
}
} else {
load_menu_state();
};
// Load volume state // Load volume state
load_volume_state(); radio.load_volume_state();
}); return radio;
}(MODULE || {}));

102
js/navigation.js Normal file
View File

@ -0,0 +1,102 @@
var MODULE = (function (radio) {
/* Click on menus */
$('a.nav-icon-files').click(function(e) {
e.preventDefault();
radio.switch_menu(0);
});
$('a.nav-icon-recent').click(function(e) {
e.preventDefault();
radio.switch_menu(1);
});
$('a.nav-icon-favorites').click(function(e) {
e.preventDefault();
radio.switch_menu(2);
});
radio.load_menu_state = function() {
var menu_state = 0;
var baseUrl = OC.generateUrl('/apps/radio/getMenuState');
$.get(baseUrl, function ( data ) {
if ("menu_state" in data) {
menu_state = data["menu_state"];
}
radio.switch_menu(menu_state);
return true;
});
}
radio.save_menu_state = function(menu_state) {
var baseUrl = OC.generateUrl('/apps/radio/saveMenuState');
var settings = {
"menu_state": menu_state
};
$.ajax({
url: baseUrl,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(settings)
}).done(function(data){
return true;
});
}
radio.switch_menu = function(type) {
var state = Number(type);
$('#filestable').hide();
$('#emptycontent').addClass('hidden');
$('.nofilterresults').addClass('hidden');
$('.loading').removeClass('hidden');
clearTimeout(searchTimeout);
searchTimeout = setTimeout(function(){
$('#app-navigation').find('li').removeClass("active");
$("tbody > tr").remove();
radio.save_menu_state(state)
switch (state) {
case 0:
history.pushState("", "", "#top");
$('li.nav-files').addClass('active');
radio.radio_query(1);
break;
case 1:
history.pushState("", "", "#recent");
$('li.nav-recent').addClass('active');
radio.radio_query(2);
break;
case 2:
history.pushState("", "", "#favorites");
$('li.nav-favorites').addClass('active');
radio.action_load_favorites();
break;
}
}, 500);
};
// On app start, load top list
if(window.location.hash) {
var hash = String(window.location.hash.replace('#',''));
switch(hash) {
case "top":
radio.switch_menu(0);
break;
case "recent":
radio.switch_menu(1);
break;
case "favorites":
radio.switch_menu(2);
break;
default:
radio.switch_menu(0);
break;
}
} else {
radio.load_menu_state();
};
return radio;
}(MODULE || {}));

View File

@ -1,6 +1,7 @@
<?php <?php
style('radio', 'main'); style('radio', 'main');
script('radio', 'main'); script('radio', 'main');
script('radio', 'navigation');
?> ?>
<div id="content" class="app-files" role="main"> <div id="content" class="app-files" role="main">