diff --git a/htdocs/ajax.loadControls.php b/htdocs/ajax.loadControls.php deleted file mode 100755 index 74243910e..000000000 --- a/htdocs/ajax.loadControls.php +++ /dev/null @@ -1,61 +0,0 @@ - -
- '> - '> - -'; - } - else { - print ''; - } -?> - - '> - '> - -
-
-
- - '> - - - - '.$lang['globalOff'].''; - } elseif ($playerStatus['single'] == "1") { - print ' '.$lang['globalTrack'].''; - } else { - print ' '.$lang['globalList'].''; - } - if (array_key_exists('state', $playerStatus)) { - print ' '; - } -?> -
- - - \ No newline at end of file diff --git a/htdocs/ajax.loadCover.php b/htdocs/ajax.loadCover.php deleted file mode 100755 index 738714dad..000000000 --- a/htdocs/ajax.loadCover.php +++ /dev/null @@ -1,52 +0,0 @@ -'; - print ""; - print ''; -} elseif (file_exists($Audio_Folders_Path.'/'.$Latest_Folder_Played.'/spotify.txt')) { - print '
'; - print ""; - print '
'; -} else { - print '
'; - print ""; - print '
'; -} -?> diff --git a/htdocs/ajax.loadPlaylist.php b/htdocs/ajax.loadPlaylist.php deleted file mode 100755 index 656ac35f6..000000000 --- a/htdocs/ajax.loadPlaylist.php +++ /dev/null @@ -1,89 +0,0 @@ -'; -$i=0; -foreach($plFile[1] AS $file) { - print ' - - - - - '; - if(trim($plTitle['1'][$i]) != "") { - print ' - '.$plTitle['1'][$i].''; - } else { - print ' - '.basename($file).''; - } - print ' -
'.str_replace(";", " and ", $plArtist['1'][$i]).''; - if (empty($plAlbum['1'][$i]) != true) { - print "
".$plAlbum['1'][$i]; - if (empty($plDate['1'][$i]) != true) { - print " (".$plDate['1'][$i].")"; - } - print ""; - } - print ' - - '; - // Livestreams and podcasts have no time length, check to suppress badge - if ( $plTime['1'][$i] > 0 && $plTime['1'][$i] < 3600 ) { - print ''.date("i:s",$plTime['1'][$i]).''; - } elseif ( $plTime['1'][$i] > 0 ) { - print ''.gmdate("H:i:s",$plTime['1'][$i]).''; - } - print' - - - '; - $i++; -} -print ''; -/* -$i=0; -foreach($plFile[1] AS $file) { - print ' -
  • -
    -
    - -
    -
    '; - if(trim($plTitle['1'][$i]) != "") { - print ' - '.$plTitle['1'][$i].''; - } else { - print ' - '.basename($file).''; - } - print ' -
    '.str_replace(";", " and ", $plArtist['1'][$i]).''; - if (empty($plAlbum['1'][$i]) != true) { - print "
    ".$plAlbum['1'][$i]; - if (empty($plDate['1'][$i]) != true) { - print " (".$plDate['1'][$i].")"; - } - print ""; - } - print ' -
    -
    '; - // Livestreams and podcasts have no time length, check to suppress badge - if ( $plTime['1'][$i] > 0 && $plTime['1'][$i] < 3600 ) { - print ''.date("i:s",$plTime['1'][$i]).''; - } elseif ( $plTime['1'][$i] > 0 ) { - print ''.gmdate("H:i:s",$plTime['1'][$i]).''; - } - print' -
    -
    -
  • - '; - $i++; -} -*/ -?> \ No newline at end of file diff --git a/htdocs/ajax.loadTime.php b/htdocs/ajax.loadTime.php deleted file mode 100755 index e72ff78ae..000000000 --- a/htdocs/ajax.loadTime.php +++ /dev/null @@ -1,18 +0,0 @@ -'; -if ( $plTime['1'][$playerStatus['pos']] < 3600 ) { - print date("i:s",$playerStatus['elapsed']); - // Livestream and podcasts have no time length, show only elapsed time - if ( $plTime['1'][$playerStatus['pos']] > 0 ) { - print ' / '.date("i:s",$plTime['1'][$playerStatus['pos']]); - } -} else { - print date("H:i:s",$playerStatus['elapsed']); - // Livestream and podcasts have no time length, show only elapsed time - if ( $plTime['1'][$playerStatus['pos']] > 0 ) { - print ' / '.gmdate("H:i:s",$plTime['1'][$playerStatus['pos']]); - } -} -print ''; -?> \ No newline at end of file diff --git a/htdocs/ajax.loadVolume.php b/htdocs/ajax.loadVolume.php deleted file mode 100755 index b5ace5d26..000000000 --- a/htdocs/ajax.loadVolume.php +++ /dev/null @@ -1,15 +0,0 @@ - -
    -
    - % -
    -
    -
    -
    -
    -
    diff --git a/htdocs/api/common.php b/htdocs/api/common.php new file mode 100644 index 000000000..7562c58b2 --- /dev/null +++ b/htdocs/api/common.php @@ -0,0 +1,29 @@ + diff --git a/htdocs/api/cover.php b/htdocs/api/cover.php new file mode 100644 index 000000000..c9bdbd113 --- /dev/null +++ b/htdocs/api/cover.php @@ -0,0 +1,30 @@ + diff --git a/htdocs/api/latest.php b/htdocs/api/latest.php new file mode 100644 index 000000000..1f949fa50 --- /dev/null +++ b/htdocs/api/latest.php @@ -0,0 +1,13 @@ + diff --git a/htdocs/api/player.php b/htdocs/api/player.php new file mode 100644 index 000000000..407822cce --- /dev/null +++ b/htdocs/api/player.php @@ -0,0 +1,81 @@ + diff --git a/htdocs/api/playlist.php b/htdocs/api/playlist.php new file mode 100644 index 000000000..d39155e30 --- /dev/null +++ b/htdocs/api/playlist.php @@ -0,0 +1,69 @@ += 2) { + $key = substr(trim($exploded[0]), 0, -1); + $value = $exploded[1]; + if ($key === 'file') { + // next track + if ($index != -1) { + $result[$index] = $currentEntry; + } + $index++; + $currentEntry = array(); + } + $currentEntry[$key] = $value; + } + } + header('Content-Type: application/json'); + echo json_encode($result); +} + +function handlePut() { + $body = file_get_contents('php://input'); + $json = json_decode(trim($body), TRUE); + if (validateRequest($json)) { + $playlist = $json['playlist']; + if($json['recursive'] === "true") { + execScript("rfid_trigger_play.sh -d={$playlist} -v=\"recursive\""); + } else { + execScript("rfid_trigger_play.sh -d={$playlist}"); + } + } +} + +function validateRequest($json) { + if ($json['playlist'] == null) { + http_response_code(400); + echo "playlist attribute missing"; + return false; + } else if ($json['recursive'] == null) { + http_response_code(400); + echo "recursive attribute missing"; + return false; + } + return true; +} + +?> diff --git a/htdocs/api/playlist/resume.php b/htdocs/api/playlist/resume.php new file mode 100644 index 000000000..d5c0b2a10 --- /dev/null +++ b/htdocs/api/playlist/resume.php @@ -0,0 +1,37 @@ + diff --git a/htdocs/api/playlist/shuffle.php b/htdocs/api/playlist/shuffle.php new file mode 100644 index 000000000..a21049130 --- /dev/null +++ b/htdocs/api/playlist/shuffle.php @@ -0,0 +1,37 @@ + diff --git a/htdocs/api/playlist/single.php b/htdocs/api/playlist/single.php new file mode 100644 index 000000000..52e6b2393 --- /dev/null +++ b/htdocs/api/playlist/single.php @@ -0,0 +1,37 @@ + diff --git a/htdocs/api/playlist/song.php b/htdocs/api/playlist/song.php new file mode 100644 index 000000000..00a2a644e --- /dev/null +++ b/htdocs/api/playlist/song.php @@ -0,0 +1,19 @@ + diff --git a/htdocs/api/volume.php b/htdocs/api/volume.php new file mode 100644 index 000000000..5e97bb6f3 --- /dev/null +++ b/htdocs/api/volume.php @@ -0,0 +1,23 @@ + diff --git a/htdocs/func.php b/htdocs/func.php index b13f46f0e..6c4f5096c 100755 --- a/htdocs/func.php +++ b/htdocs/func.php @@ -20,15 +20,15 @@ function html_bootstrap3_createHeader($lang="en",$title="Welcome",$url_absolute= - + ".$title." - + - + @@ -37,21 +37,21 @@ function html_bootstrap3_createHeader($lang="en",$title="Welcome",$url_absolute= - + - - + + - + @@ -149,7 +149,7 @@ function html_bootstrap3_createHeader($lang="en",$title="Welcome",$url_absolute= color: #999!important; } .btn-panel-big { - font-size: 3em!important; + font-size: 3em!important; margin-right: 0.1em; } .btn-panel-col { @@ -173,13 +173,13 @@ function html_bootstrap3_createHeader($lang="en",$title="Welcome",$url_absolute= margin-right: 1em } - + \n"; } function arrayPregDiff($a, $p) { - # added function to use regular expressions to remove multiple files - # e.g. all of the same file type from the array forming the later playlist. + # added function to use regular expressions to remove multiple files + # e.g. all of the same file type from the array forming the later playlist. # Idea originates from http://php.net/manual/en/function.array-diff.php#117219 foreach ($a as $key => $value) { if (preg_match($p, $value)) { @@ -235,7 +235,7 @@ function dir_list_recursively($rootdir = "") { * Get directory tree recursively. * The dir path will end without '/'. */ - + $iter = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($rootdir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, @@ -246,7 +246,7 @@ function dir_list_recursively($rootdir = "") { foreach ($iter as $path => $dir) { if ($dir->isDir()) { $paths[] = $path; - } + } } return $paths; @@ -260,9 +260,9 @@ function index_folders_print($item, $key) global $debugcol; /**/ // get files from array - foreach($contentTree as $tempkey => $values) { - $allFiles = $values['files']; - } + foreach($contentTree as $tempkey => $values) { + $allFiles = $values['files']; + } //print_r($contentTree);//??? // get mp3 files from files list $filesMp3 = array(); @@ -272,6 +272,7 @@ function index_folders_print($item, $key) $filesMp3[] = $allFile; } } + $playlist = urlencode($contentTree[$key]['path_rel']); /**/ //print "
    \nkey:".$key." id:".$contentTree[$key]['id']." path_rel:".$contentTree[$key]['path_rel']; print_r($contentTree); print "
    "; //??? //print "
    \nfiles:"; print_r($files); print "
    "; //??? @@ -285,7 +286,7 @@ function index_folders_print($item, $key) $panelStyle = "panel-default"; } /* - if(file_exists($contentTree[$key]['path_abs'].'/cover.jpg')) { + if(file_exists($contentTree[$key]['path_abs'].'/cover.jpg')) { print ''; } else { print ''; @@ -309,11 +310,11 @@ function index_folders_print($item, $key) */ if($contentTree[$key]['count_files'] > 0) { print " - "; + "; } if($contentTree[$key]['count_subdirs'] > 0) { print " - "; + "; } if (!in_array($contentTree[$key]['path_abs']."/livestream.txt", $contentTree[$key]['files']) && !in_array($contentTree[$key]['path_abs']."/spotify.txt", $contentTree[$key]['files']) && !in_array($contentTree[$key]['path_abs']."/podcast.txt", $contentTree[$key]['files']) ) { print " @@ -339,11 +340,11 @@ function index_folders_print($item, $key) } elseif (in_array($contentTree[$key]['path_abs']."/livestream.txt", $contentTree[$key]['files'])) { print " "; print $contentTree[$key]['basename']; - + } elseif (in_array($contentTree[$key]['path_abs']."/podcast.txt", $contentTree[$key]['files'])) { print " "; print $contentTree[$key]['basename']; - + } else { print $contentTree[$key]['basename']; } @@ -360,8 +361,8 @@ function index_folders_print($item, $key) // do not show any if there is a live stream in the folder if (!in_array($contentTree[$key]['path_abs']."/livestream.txt", $contentTree[$key]['files']) ) { $foundResume = "OFF"; - if( - file_exists($contentTree[$key]['path_abs']."/folder.conf") + if( + file_exists($contentTree[$key]['path_abs']."/folder.conf") && strpos(file_get_contents($contentTree[$key]['path_abs']."/folder.conf"),'RESUME="ON"') !== false ) { $foundResume = "ON"; @@ -369,27 +370,27 @@ function index_folders_print($item, $key) } if( $foundResume == "OFF" ) { // do stuff - print "".$lang['globalResume'].": ".$lang['globalOff']." "; + print "".$lang['globalResume'].": ".$lang['globalOff']." "; } elseif($foundResume == "ON") { - print "".$lang['globalResume'].": ".$lang['globalOn']." "; + print "".$lang['globalResume'].": ".$lang['globalOn']." "; } } - + // SHUFFLE BUTTON // do not show any if there is a live stream in the folder if (!in_array($contentTree[$key]['path_abs']."/livestream.txt", $contentTree[$key]['files']) ) { $foundShuffle = "OFF"; - if( - file_exists($contentTree[$key]['path_abs']."/folder.conf") + if( + file_exists($contentTree[$key]['path_abs']."/folder.conf") && strpos(file_get_contents($contentTree[$key]['path_abs']."/folder.conf"),'SHUFFLE="ON"') !== false ) { $foundShuffle = "ON"; } if( $foundShuffle == "OFF" ) { // do stuff - print "".$lang['globalShuffle'].": ".$lang['globalOff']." "; + print "".$lang['globalShuffle'].": ".$lang['globalOff']." "; } elseif($foundShuffle == "ON") { - print "".$lang['globalShuffle'].": ".$lang['globalOn']." "; + print "".$lang['globalShuffle'].": ".$lang['globalOn']." "; } } @@ -397,30 +398,31 @@ function index_folders_print($item, $key) // do not show any if there is a live stream in the folder if (!in_array($contentTree[$key]['path_abs']."/livestream.txt", $contentTree[$key]['files']) ) { $foundSinglePlay = "OFF"; - if( - file_exists($contentTree[$key]['path_abs']."/folder.conf") + if( + file_exists($contentTree[$key]['path_abs']."/folder.conf") && strpos(file_get_contents($contentTree[$key]['path_abs']."/folder.conf"),'SINGLE="ON"') !== false ) { $foundSinglePlay = "ON"; } if( $foundSinglePlay == "OFF" ) { // do stuff - print "".$lang['globalSingle'].": ".$lang['globalOff']." "; + + print "".$lang['globalSingle'].": ".$lang['globalOff']." "; } elseif($foundSinglePlay == "ON") { - print "".$lang['globalSingle'].": ".$lang['globalOn']." "; + print "".$lang['globalSingle'].": ".$lang['globalOn']." "; } } // RSS link if (count($filesMp3) > 0) { print ""; - print "Podcast RSS "; + print "Podcast RSS "; print ""; - } + } print "
    "; } - + // get all IDs that match this folder $IDchips = ""; // print later @@ -441,9 +443,9 @@ function index_folders_print($item, $key)
    "; //print $contentTree[$key]['id']; //??? - + printPlaylistHtml($contentTree[$key]['files']); - + if(is_array($item)) { array_walk($item, 'index_folders_print'); } @@ -451,53 +453,53 @@ function index_folders_print($item, $key)
    "; - + } -function getSubDirectories( $path = '.', $level = 0, $showfiles = 0 ){ +function getSubDirectories( $path = '.', $level = 0, $showfiles = 0 ){ $return = array(); - // Directories to ignore when listing output. Many hosts - // will deny PHP access to the cgi-bin. - $ignore = array( '.', '..', '.git', '.github' ); + // Directories to ignore when listing output. Many hosts + // will deny PHP access to the cgi-bin. + $ignore = array( '.', '..', '.git', '.github' ); - // Open the directory to the handle $dh + // Open the directory to the handle $dh $dh = @opendir($path); - - while( false !== ( $file = readdir( $dh ) ) ){ - // Loop through the directory - - if( !in_array( $file, $ignore ) ){ - // Check that this file is not to be ignored - - //$spaces = str_repeat( ' ', ( $level * 4 ) ); - // Just to add spacing to the list, to better - // show the directory tree. - - if(is_dir("$path/$file") ){ - // Its a directory, so we need to keep reading down... + + while( false !== ( $file = readdir( $dh ) ) ){ + // Loop through the directory + + if( !in_array( $file, $ignore ) ){ + // Check that this file is not to be ignored + + //$spaces = str_repeat( ' ', ( $level * 4 ) ); + // Just to add spacing to the list, to better + // show the directory tree. + + if(is_dir("$path/$file") ){ + // Its a directory, so we need to keep reading down... $return[$path."/".$file] = getSubDirectories("$path/$file",($level+1)); - - } elseif($showfiles == "1") { + + } elseif($showfiles == "1") { //$return[$path."/".$file]['files'] = $file; - } - } - } + } + } + } + + closedir( $dh ); - closedir( $dh ); - uksort($return, 'strnatcasecmp'); return $return; } function printPlaylistHtml($files) -{ +{ global $lang; $counter = 1; - -/* + +/* print " -
    "; +
    "; foreach($files as $file) { print "
    ".$counter++."
    @@ -510,14 +512,14 @@ function printPlaylistHtml($files) "; } print " -
    "; -*/ - +
    "; +*/ + print " -
      "; +
        "; foreach($files as $file) { print " -
      1. ".$counter++." : +
      2. ".$counter++." : ".basename($file).""; if(basename($file) != "livestream.txt" && basename($file) != "podcast.txt" && basename($file) != "spotify.txt") { print" @@ -527,5 +529,5 @@ function printPlaylistHtml($files)
      3. "; } print " -
      "; +
    "; }?> diff --git a/htdocs/inc.controlPlayer.php b/htdocs/inc.controlPlayer.php old mode 100755 new mode 100644 index 0d12dce2c..9da451e30 --- a/htdocs/inc.controlPlayer.php +++ b/htdocs/inc.controlPlayer.php @@ -1,30 +1,13 @@ '; + echo '
    '; + include('inc.loadCover.php'); + echo '
    '; } ?> -
    - - + diff --git a/htdocs/inc.loadControls.php b/htdocs/inc.loadControls.php new file mode 100644 index 000000000..b14174d95 --- /dev/null +++ b/htdocs/inc.loadControls.php @@ -0,0 +1,175 @@ + + +
    + '> + '> + +'; +print ''; + +?> + +'> '> + +
    + +
    +
    + + '> + + + + ' . $lang['globalOff'] . ''; + print ''; + print ''; + print ''; + ?> +
    + + + * + * + + diff --git a/htdocs/inc.loadCover.php b/htdocs/inc.loadCover.php new file mode 100644 index 000000000..60c1fd4f1 --- /dev/null +++ b/htdocs/inc.loadCover.php @@ -0,0 +1,12 @@ +
    + Cover +
    + + diff --git a/htdocs/inc.loadedPlaylist.php b/htdocs/inc.loadedPlaylist.php old mode 100755 new mode 100644 index 22179afb3..bdd7c79df --- a/htdocs/inc.loadedPlaylist.php +++ b/htdocs/inc.loadedPlaylist.php @@ -1,117 +1,142 @@ - - - - - - - - - - - - - - - - - -
    1The title of the track - it could be long03:12
    2 - - This is the tenth album by - This is the tenth album by - This is the tenth album by - This is the tenth album by - This is the tenth album by - This is the tenth album by - This is the tenth album by - This is the tenth album by - - 23:42
    -
    - -*/ -?> - - - -
    + + + + + + +
    + +
    + - Show playlist +
    -'; - -/* -print ' -
    -
    -
    -

    -
    -
    - -
    -
    -
    - -
    -
    -
    -
    -
    -
    - -
    - -
    -
    -

    -
    -
    -
      -
      -
    -
    -
    -
    -'; -*/ -?> diff --git a/htdocs/inc.setVolume.php b/htdocs/inc.setVolume.php old mode 100755 new mode 100644 index ea3ab973d..9b34003e1 --- a/htdocs/inc.setVolume.php +++ b/htdocs/inc.setVolume.php @@ -1,62 +1,73 @@ - +

    -
    '> +
    - = 0) { - print " - "; - $i = $i - 5; - }; - print "\n"; + for ($i = $maxvalueselect; $i >= 0; $i -= 5) { + print ""; + } ?> - + '/>
    - -
    - +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    + +
    diff --git a/htdocs/inc.viewFolderWell.php b/htdocs/inc.viewFolderWell.php deleted file mode 100755 index c7f7263a1..000000000 --- a/htdocs/inc.viewFolderWell.php +++ /dev/null @@ -1,94 +0,0 @@ -".$lang['indexContainsFiles'].""; - -// get all IDs that match this folder -$ids = ""; // print later -$audiofolderbasename = trim(basename($audiofolder)); -if(in_array($audiofolderbasename, $shortcuts)) { - foreach ($shortcuts as $key => $value) { - if($value == $audiofolderbasename) { - $ids .= " ".$key." | "; - } - } - $ids = rtrim($ids, "| "); // get rid of trailing slash -} -//parse_str($Audio_Folders_Path.'/'.$audiofolder.'/folder.conf', $folderConf); -$folderConfRaw = file_get_contents($audiofolder.'/folder.conf'); -// if folder not empty, display play button and content -if ($accordion != "

    ".$lang['indexContainsFiles']."

    ") { - print " -
    -
    "; - print " -

    - ".str_replace($Audio_Folders_Path.'/', '', $audiofolder)." -

    "; - print " - ".$lang['globalPlay']." "; - // Adds a button to enable/disable resume play. Checks if lastplayed.dat exists and livestream.txt not (no resume for livestreams) - - // RESUME BUTTON - // do not show any if there is a live stream in the folder - if (!in_array("livestream.txt", $files) ) { - $foundResume = "OFF"; - if( file_exists($audiofolder."/folder.conf") && strpos($folderConfRaw),'RESUME="ON"') !== false) { - $foundResume = "ON"; - } else { - } - } - if( $foundResume == "OFF" ) { - // do stuff - print "".$lang['globalResume'].": ".$lang['globalOff']." "; - } elseif($foundResume == "ON") { - print "".$lang['globalResume'].": ".$lang['globalOn']." "; - } - - // SHUFFLE BUTTON - // do not show any if there is a live stream in the folder - if (!in_array("livestream.txt", $files) ) { - $foundShuffle = "OFF"; - if( file_exists($audiofolder."/folder.conf") && strpos($folderConfRaw),'SHUFFLE="ON"') !== false) { - $foundShuffle = "ON"; - } - } - if( $foundShuffle == "OFF" ) { - // do stuff - print "".$lang['globalShuffle'].": ".$lang['globalOff']." "; - } elseif($foundShuffle == "ON") { - print "".$lang['globalShuffle'].": ".$lang['globalOn']." "; - } - - - print " - ".$lang['indexShowFiles']." "; - print " -
    - ".$accordion." -
    - "; - // print ID if any found - if($ids != "") { - print " -
    ".$lang['globalCardId'].": ".$ids; - } else { - print " -
     "; - } - print " -
    -
    - "; -} -?> \ No newline at end of file diff --git a/htdocs/index.php b/htdocs/index.php index 2122f0e42..244e60682 100755 --- a/htdocs/index.php +++ b/htdocs/index.php @@ -12,10 +12,6 @@
    - - @@ -30,6 +26,7 @@ print '
    '.$warning.'
    '; } +print ''; include("inc.controlPlayer.php"); ?> @@ -40,7 +37,6 @@
    '; @@ -48,7 +44,6 @@ print '
    '; -} ?>
    @@ -87,7 +82,6 @@ $idcounter++; include('inc.viewFolderTree.php'); - //include('inc.viewFolderWell.php'); } @@ -136,4 +130,10 @@
    + + + diff --git a/htdocs/js/jukebox.js b/htdocs/js/jukebox.js new file mode 100644 index 000000000..9528dbfb1 --- /dev/null +++ b/htdocs/js/jukebox.js @@ -0,0 +1,244 @@ +function init() { + JUKEBOX = {}; + // Allow observers to react to changes to playlist or songs. + // New playlist is selected + JUKEBOX.playlistChangedListener = []; + // Playlist data is downloaded + JUKEBOX.playlistDataChangedListener = []; + JUKEBOX.songChangedListener = []; + JUKEBOX.folderChangedListener = []; + + // Allow observers to react to player changes + JUKEBOX.playStateListener = []; + JUKEBOX.timeListener = []; + JUKEBOX.volumeChangedListener = []; + JUKEBOX.repeatChangedListener = []; + JUKEBOX.singleChangedListener = []; + + // Track which song is currently played. + JUKEBOX.latestInfo = {}; + // Track the current status of the player + JUKEBOX.playerInfo = {}; + // information about the current playlist + JUKEBOX.playlistInfo = []; +} + +function loadStatusRepeat() { + loadStatus(() => { + setTimeout(loadStatusRepeat, 5000); + }); +} + + +function loadStatus(completion) { + $.ajax({ + url: 'api/player.php', + datatype: 'json' + }).success(data => { + notifyOnDataChange(data); + }).complete(() => { + if (completion != null) { + completion(); + } + }); +} + +function notifyOnDataChange(data) { + const oldPlayerInfo = JUKEBOX.playerInfo; + JUKEBOX.playerInfo = data; + if (data.state !== oldPlayerInfo.state) { + notify(JUKEBOX.playStateListener, data.state); + } + + if (data.playlist !== oldPlayerInfo.playlist) { + notify(JUKEBOX.playlistChangedListener, data.playlist); + } else { + if (data.songid !== oldPlayerInfo.songid) { + notify(JUKEBOX.songChangedListener, data.songid); + } + } + + if (data.volume !== oldPlayerInfo.volume) { + notify(JUKEBOX.volumeChangedListener, data.volume); + } + if (data.repeat != oldPlayerInfo.repeat) { + notify(JUKEBOX.repeatChangedListener, data.repeat); + } + if (data.single != oldPlayerInfo.single) { + notify(JUKEBOX.singleChangedListener, data.single); + } + notify(JUKEBOX.timeListener, data.time); +} + +function notify(listeners, data) { + listeners.forEach(listener => listener(data)); +} + +function playPlaylist(playlist, recursive) { + const json = { + playlist, + recursive + }; + $.ajax({ + url: `api/playlist.php`, + method: 'PUT', + data: JSON.stringify(json) + }).success(data => { + loadStatus(); + }); +} + +function loadPlaylist() { + $.ajax({ + url: `api/playlist.php` + }).success(data => { + JUKEBOX.playlistInfo = data; + notify(JUKEBOX.playlistDataChangedListener, data); + }); +} + +function playSongInPlaylist(song) { + $.ajax({ + url: `api/playlist/song.php`, + method: 'PUT', + data: song.toString() + }).success(data => { + loadStatus(); + }); +} + +function executePlayerCommand(command, completion) { + hideApiError(); + $.ajax({ + url: 'api/player.php', + method: 'PUT', + contentType: "application/json; charset=utf-8", + data: JSON.stringify({ 'command': command }) + }).success(data => { + if (completion != null) { + completion(data); + } + }).error(error => { + $("#api-alert").html(`An error occorured: ${error.responseText}`); + showApiError(); + }); +} + + +function volumeUp() { + executePlayerCommand('volumeup', () => { + loadVolume() + }); +} + +function volumeDown() { + executePlayerCommand('volumedown', () => { + loadVolume() + }); +} + +function loadVolume() { + $.ajax({ + url: 'api/volume.php', + method: 'GET' + }).success(data => { + displayVolume(data); + }); +} + +function stop() { + pause(); + executePlayerCommand('stop'); +} + +function toggleShuffle(element) { + togglePlaylistCommand(element, 'shuffle') +} + +function toggleResume(element) { + togglePlaylistCommand(element, 'resume') +} + +function toggleSingle(element) { + togglePlaylistCommand(element, 'single') +} + +function togglePlaylistCommand(element, toggleAttributeName) { + const attribute = $(element).attr(toggleAttributeName); + const newAttributeValue = attribute === 'true' ? 'false' : 'true'; + const playlist = $(element).attr('playlist'); + hideApiError(); + const jsonData = { playlist }; + jsonData[toggleAttributeName] = newAttributeValue; + + $.ajax({ + url: 'api/playlist/' + toggleAttributeName + '.php', + method: 'PUT', + contentType: "application/json; charset=utf-8", + data: JSON.stringify(jsonData) + }).success(data => { + $(element).attr(toggleAttributeName, newAttributeValue); + if (newAttributeValue == 'true') { + $(element).html(JUKEBOX.lang['global' + capitalizeFirstLetter(toggleAttributeName)] + ": " + JUKEBOX.lang['globalOn'] + "