From 11be63c110fc9a67f9094f03c4e57671d6db47f4 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Sun, 1 Sep 2013 13:30:06 +0200 Subject: [PATCH] last used radio stations are saved now --- webclient/index.html | 7 +-- webclient/js/controls.js | 50 +++++++++++++----- webclient/js/functionsvars.js | 19 +++---- webclient/js/gui.js | 26 ++++++++-- webclient/js/jquery.cookie.js | 96 +++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+), 30 deletions(-) create mode 100644 webclient/js/jquery.cookie.js diff --git a/webclient/index.html b/webclient/index.html index 5c33e9b..e488017 100755 --- a/webclient/index.html +++ b/webclient/index.html @@ -237,9 +237,10 @@

Radio

- + +
@@ -271,7 +272,7 @@ - + diff --git a/webclient/js/controls.js b/webclient/js/controls.js index 228900b..606ae74 100755 --- a/webclient/js/controls.js +++ b/webclient/js/controls.js @@ -297,9 +297,17 @@ function radioPressed(key) { return true; } -function addRadioUri(value) { - var value = value || $('#radioinput').val(); - if (validUrl(value)) { +function addRadioUri(name, uri) { + //value of name is based on the passing of an uri as a parameter or not + var name = ''; + if (!uri) { + name = $('#radionameinput').val(); + } else { + $('#radionameinput').val(''); + } + uri = uri || $('#radiouriinput').val(); +// console.log( name , uri); + if (validUri(uri)) { showLoading(true); //stop directly, for user feedback mopidy.playback.stop(true); @@ -307,15 +315,26 @@ function addRadioUri(value) { document.activeElement.blur(); $("input").blur(); clearQueue(); - mopidy.tracklist.add(null,null, value ); - //add station to list and check for doubles + mopidy.tracklist.add(null,null, uri ); + var tmpname = name || ''; + var i = 0; + //add station to list and check for doubles and add no more than 25 for (var key in radioStations) { rs = radioStations[key]; - if (rs[1] == value) { + if (i > 25) { + delete radioStations[key]; + continue; + } + i++; + if (rs && rs[1] == uri) { + tmpname = name || radioStations[key][0]; delete radioStations[key]; } }; - radioStations.unshift(['', value]); + $('#radionameinput').val(tmpname); + $('#radiouriinput').val(uri); + radioStations.unshift([tmpname, uri]); + saveRadioStations(); mopidy.playback.play(); updateRadioStations(); showLoading(false); @@ -333,7 +352,7 @@ function updateRadioStations() { var rs = radioStations[key]; if(rs) { name = rs[0] || rs[1]; - child = '
  • '; + child = '
  • '; child += '

    ' + name + '

  • '; tmp += child; } @@ -342,9 +361,14 @@ function updateRadioStations() { } function initRadio() { - radioStations.push(['3FM', 'http://icecast.omroep.nl/3fm-bb-mp3']); - radioStations.push(['', 'http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_128_03']); - radioStations.push(['Arrow', 'http://81.173.3.132:8082']); - radioStations.push(['', 'http://icecast.omroep.nl/radio1-bb-mp3']); + $.cookie.json = true; + tmpRS = $.cookie('radioStations'); + radioStations = tmpRS || radioStations; updateRadioStations(); -} \ No newline at end of file +} + +function saveRadioStations() { + $.cookie.json = true; + $.cookie('radioStations', radioStations); +} + diff --git a/webclient/js/functionsvars.js b/webclient/js/functionsvars.js index 16e09cb..19e8ca3 100755 --- a/webclient/js/functionsvars.js +++ b/webclient/js/functionsvars.js @@ -73,6 +73,12 @@ TRACK_TIMER = 1000; STATUS_TIMER = 10000; var radioStations = []; +//fill with defaults + radioStations.push(['NPR 24', 'http://nprdmp.ic.llnwd.net/stream/nprdmp_live01_mp3']); + radioStations.push(['3FM Dutch', 'http://icecast.omroep.nl/3fm-bb-mp3']); + radioStations.push(['BBC WorldService', 'http://vprbbc.streamguys.net:8000/vprbbc24.mp3']); + radioStations.push(['Arrow Jazz', 'http://81.173.3.132:8082']); + radioStations.push(['PBS Australia', 'http://eno.emit.com:8000/pbsfm_live_64.mp3']); /******* * @@ -309,13 +315,8 @@ function toast (message, delay) { } } -/* from stackoverflow */ -function validUrl(str) { - var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name - '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path - '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string - '(\\#[-a-z\\d_]*)?$','i'); // fragment locator - return ( pattern.test(str) ); +// from http://dzone.com/snippets/validate-url-regexp +function validUri(str) { + var regexp = /(mms|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ + return regexp.test(str); } \ No newline at end of file diff --git a/webclient/js/gui.js b/webclient/js/gui.js index 12a0d2f..3182a82 100755 --- a/webclient/js/gui.js +++ b/webclient/js/gui.js @@ -126,11 +126,22 @@ function setSongInfo(data) { artistshtml = ''; artiststext = ''; - + if (validUri(data.name)) { + for (var key in radioStations) { + rs = radioStations[key]; + if (rs && rs[1] == data.name) { + data.name = (rs[0] || rs[1]) + ' [Stream]'; + } + }; + } + if (data.album) { $("#modalalbum").html('Album: ' + data.album.name + ''); getCover(artiststext, data.album.name, '#infocover, #controlspopupimage', 'extralarge'); + } else { + $("#modalalbum").html(''); } + $("#modalname").html(data.name); if (!data.length || data.length == 0) { @@ -144,6 +155,8 @@ function setSongInfo(data) { $('#trackslider').slider('enable'); } + var arttmp = ''; + if(data.artists) { for (var j = 0; j < data.artists.length; j++) { artistshtml += '' + data.artists[j].name + ''; @@ -153,14 +166,14 @@ function setSongInfo(data) { artiststext += ', '; } } - var arttmp = 'Artist'; - + arttmp = 'Artist'; if (data.artists.length > 1) { - arttmp += 's'; + arttmp += 's'; } + arttmp += ': ' + artistshtml; } - $("#modalartist").html(arttmp + ': ' + artistshtml); + $("#modalartist").html(arttmp); $("#trackslider").attr("min", 0); $("#trackslider").attr("max", data.length); @@ -448,6 +461,9 @@ function locationHashChanged() { initSearch($('#searchinput').val()); } break; + case 'radio': + $('#navradio a').addClass('ui-state-active ui-state-persist ui-btn-active'); + break; case 'artists': if (uri != '') { showArtist(uri); diff --git a/webclient/js/jquery.cookie.js b/webclient/js/jquery.cookie.js new file mode 100644 index 0000000..f8f852c --- /dev/null +++ b/webclient/js/jquery.cookie.js @@ -0,0 +1,96 @@ +/*! + * jQuery Cookie Plugin v1.3.1 + * https://github.com/carhartl/jquery-cookie + * + * Copyright 2013 Klaus Hartl + * Released under the MIT license + */ +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as anonymous module. + define(['jquery'], factory); + } else { + // Browser globals. + factory(jQuery); + } +}(function ($) { + + var pluses = /\+/g; + + function decode(s) { + if (config.raw) { + return s; + } + return decodeURIComponent(s.replace(pluses, ' ')); + } + + function decodeAndParse(s) { + if (s.indexOf('"') === 0) { + // This is a quoted cookie as according to RFC2068, unescape... + s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); + } + + s = decode(s); + + try { + return config.json ? JSON.parse(s) : s; + } catch(e) {} + } + + var config = $.cookie = function (key, value, options) { + + // Write + if (value !== undefined) { + options = $.extend({}, config.defaults, options); + + if (typeof options.expires === 'number') { + var days = options.expires, t = options.expires = new Date(); + t.setDate(t.getDate() + days); + } + + value = config.json ? JSON.stringify(value) : String(value); + + return (document.cookie = [ + config.raw ? key : encodeURIComponent(key), + '=', + config.raw ? value : encodeURIComponent(value), + options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE + options.path ? '; path=' + options.path : '', + options.domain ? '; domain=' + options.domain : '', + options.secure ? '; secure' : '' + ].join('')); + } + + // Read + var cookies = document.cookie.split('; '); + var result = key ? undefined : {}; + for (var i = 0, l = cookies.length; i < l; i++) { + var parts = cookies[i].split('='); + var name = decode(parts.shift()); + var cookie = parts.join('='); + + if (key && key === name) { + result = decodeAndParse(cookie); + break; + } + + if (!key) { + result[name] = decodeAndParse(cookie); + } + } + + return result; + }; + + config.defaults = {}; + + $.removeCookie = function (key, options) { + if ($.cookie(key) !== undefined) { + // Must not alter options, thus extending a fresh object... + $.cookie(key, '', $.extend({}, options, { expires: -1 })); + return true; + } + return false; + }; + +}));