Commit af4632b9 authored by onny's avatar onny

split up javascript files

parent f3f316bf
......@@ -4,7 +4,7 @@
<id>radio</id>
<name>Radio</name>
<description>Listening to your favorite radio stations in Nextcloud</description>
<version>0.6.2</version>
<version>0.6.3</version>
<licence>agpl</licence>
<author>Jonas Heinrich</author>
<documentation>
......
$(function(){
var MODULE = (function (radio) {
var last_volume = 0;
......@@ -23,14 +23,14 @@ $(function(){
if(!station_list.data("didfire")) {
station_list.data("didfire", true);
if ($('li.nav-files').hasClass('active')){
radio_query(1, "");
radio.radio_query(1, "");
} else if ($('li.nav-recent').hasClass('active')) {
radio_query(2, "");
radio.radio_query(2, "");
} else if ($('li.nav-favorites').hasClass('active')) {
console.log("todo");
} else {
var query = $('#searchbox').val();
radio_query(0, query);
radio.radio_query(0, query);
};
var ref = station_list;
setTimeout(function(){
......@@ -45,14 +45,14 @@ $(function(){
searchTimeout = null;
var scheduled;
function isElementOverflowing(element) {
radio.isElementOverflowing = function(element) {
var overflowX = element.offsetWidth < element.scrollWidth,
overflowY = element.offsetHeight < element.scrollHeight;
return (overflowX || overflowY);
}
function wrapContentsInMarquee(element) {
radio.wrapContentsInMarquee = function(element) {
var marquee = document.createElement('marquee'),
contents = element.innerText;
......@@ -71,7 +71,7 @@ $(function(){
$('#playbutton').attr("class", "buffering");
var stationid = $(this).parent().attr('data-id');
var stationname = $(this).parent().find('.nametext').text();
action_play(stationid);
radio.action_play(stationid);
$('#player').attr('data-id',stationid);
$("#station_metadata").html("Playing: "+stationname);
......@@ -80,18 +80,18 @@ $(function(){
$(this).parent().css("background-color", "#f8f8f8");
var element = document.getElementById('station_metadata');
if (isElementOverflowing(element)) {
wrapContentsInMarquee(element);
if (radio.isElementOverflowing(element)) {
radio.wrapContentsInMarquee(element);
};
if (!scheduled){
schedule_get_metadata(stationid);
radio.schedule_get_metadata(stationid);
};
});
/* Save station to favorites */
$('body').on('click', '.favorite', function() {
var stationid = $(this).parent().parent().attr('data-id');
action_favorite(stationid);
radio.action_favorite(stationid);
});
/* 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 //
/===============*/
function schedule_get_metadata(stationid){
radio.schedule_get_metadata = function(stationid){
scheduled = true;
var stationid = $('#player').attr('data-id');
req = $.get('https://onny.project-insanity.org/getmetadata/'+stationid, function(data) {
......@@ -133,11 +117,11 @@ $(function(){
if (metadata != $("#station_metadata").text()) {
$("#station_metadata").html(metadata);
var element = document.getElementById('station_metadata');
if (isElementOverflowing(element)) {
wrapContentsInMarquee(element);
if (radio.isElementOverflowing(element)) {
radio.wrapContentsInMarquee(element);
};
};
setTimeout(function(){schedule_get_metadata(stationid);}, 10000);
setTimeout(function(){radio.schedule_get_metadata(stationid);}, 10000);
} else {
scheduled = false;
};
......@@ -146,7 +130,7 @@ $(function(){
});
};
function action_favorite(stationid){
radio.action_favorite = function(stationid){
var removed = false;
var stations = [];
......@@ -183,7 +167,7 @@ $(function(){
});
};
function action_play(stationid){
radio.action_play = function(stationid){
$.ajax({
method: "GET",
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');
$.get(baseUrl + '/stations', function ( fav_stations ) {
......@@ -245,7 +229,7 @@ $(function(){
});
}
function action_load_favorites(){
radio.action_load_favorites = function(){
var stations = []
var baseUrl = OC.generateUrl('/apps/radio');
......@@ -258,12 +242,12 @@ $(function(){
$('#emptycontent').removeClass('hidden');
$('.loading').addClass('hidden');
} else {
query_stations(stations);
radio.query_stations(stations);
};
});
};
function query_stations(station_ids){
radio.query_stations = function(station_ids){
var station_array = [];
station_ids.forEach(function (station_id, idx) {
......@@ -275,7 +259,7 @@ $(function(){
}).success( function(data){
station_array = station_array.concat(data);
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;
switch (type) {
case 0:
......@@ -306,81 +290,20 @@ $(function(){
},
dataType: 'json',
}).success( function(data){
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;
radio.render_results(data);
});
}
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
new OCA.Search(function (query) {
$('#app-navigation').find('li').removeClass("active");
$("tbody > tr").remove();
radio_query(0, query);
radio.radio_query(0, query);
}, function (arg) {
switch_menu(0);
radio.switch_menu(0);
});
function debounce(func, wait, immediate) {
radio.debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this,
......@@ -422,8 +345,8 @@ $(function(){
}
});
$("#volumeslider").on("slidestop", debounce(function(e, ui) {
save_volume_state(ui.value);
$("#volumeslider").on("slidestop", radio.debounce(function(e, ui) {
radio.save_volume_state(ui.value);
}, 700)
);
......@@ -432,16 +355,16 @@ $("#volumeicon").on("click", function(e) {
last_volume = player.volume;
player.volume = 0;
$('#volumeslider').slider('value',0);
save_volume_state(0);
radio.save_volume_state(0);
} else {
if (!last_volume == 0) {
player.volume = last_volume;
$('#volumeslider').slider('value',last_volume);
save_volume_state(last_volume);
radio.save_volume_state(last_volume);
} else {
player.volume = 1;
$('#volumeslider').slider('value',1);
save_volume_state(1);
radio.save_volume_state(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 baseUrl = OC.generateUrl('/apps/radio/getVolumeState');
$.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 settings = {
"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();
radio.load_volume_state();
});
return radio;
}(MODULE || {}));
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 || {}));
<?php
style('radio', 'main');
script('radio', 'main');
script('radio', 'navigation');
?>
<div id="content" class="app-files" role="main">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment