/* * Copyright (C) 2016-2020 phantombot.github.io/PhantomBot * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * @author ScaniaTV */ /* * Main player functions and listeners. * * Every global functions should be added under the "player" object. * The iframe API from YouTube can be accessed from "player.API". * Basic information about the current song can be access with "player.temp.song", "player.temp.title", and "player.temp.requester". * For more functions under the "player" object, take a look at the socket.js script. * You can also generate modals with jQuery, see util/helpers.js for more information. */ $(function() { var cluster = null, timer = null; /* * @function Loads the player page. * * @param {boolean} hasPlaylistData - If a playlist is loaded. */ const openPlayer = hasPlaylistData => { $('.loader').fadeOut(6e2, () => { $(this).remove(); }); // Show the page. $('#main').fadeIn(5e2); if (!hasPlaylistData && player.hasAPIKey() && !player.secondConnection) { toastr.error('Failed to load a playlist with songs.'); // Create a fake progress slider. player.progressSlider = $('#progress-slider').slider({ 'value': 0, 'min': 0, 'step': 0.1, 'tooltip': 'hide', 'selection': 'none' }); // Error the to user. helpers.getErrorModal('Playlist Error', 'Failed to load a playlist with songs, please load a playlist.', () => { player.dbQuery('get_playlists', 'yt_playlists_registry', (results) => { // Get the keys. results = Object.keys(results); const playlists = []; for (let i = 0; i < results.length; i++) { if (results[i].indexOf('ytPlaylist_') !== -1) { playlists.push(results[i].substr(results[i].indexOf('_') + 1, results[i].length)); } } helpers.getPlaylistModal('Load Playlist', 'Playlist Name', 'Load', 'Playlist', playlists, () => { let playlist = $('#playlist-load').find(':selected').text(); if (playlist === 'Select a playlist') { toastr.error('Please select a valid playlist.'); } else { if (playlist.length > 0) { player.loadPlaylist(playlist); toastr.success('Loading playlist: ' + playlist); if (player.firstLoad === true) { player.ready(); } } } }).modal('toggle'); }); }).modal('toggle'); } }; /* * Global function that is called once the socket is connected and that the YouTube iframe is loaded. */ window.onYouTubeIframeAPIReady = () => { // Set a timer in case no songs load to send a error. timer = setTimeout(openPlayer, 5e3, false); // Set a var for the slider. player.canSlide = true; // Set a var for the first load. player.firstLoad = true; // Check if the player is disabled right away. player.dbQuery('get_module_status', 'modules', (data) => { if (data['./systems/youtubePlayer.js'] == 'false') { helpers.getErrorModal('Module Disabled', 'The YouTube player module is disabled, please go and enable it.', () => { window.location.reload(); }).modal('toggle'); openPlayer(true); clearTimeout(timer); } }); // Add a listener to load the main playlist. player.addListener('playlist', (e) => { let table = [], playlist = e.playlist; // Set the playlist name. $('#playlist-name').html('(' + e.playlistname + ')'); // Table header. table.push(($('').append($('', { 'style': 'width: 5%;', 'html': '#' })).append($('', { 'style': 'width: 70%;', 'html': 'Song' })).append($('', { 'style': 'width: 15%;', 'html': 'Duration' })).append($('', { 'style': 'width: 10%; text-align: right; padding-right: 16px;', 'html': 'Actions' }))).html()); for (let i = 0; i < playlist.length; i++) { let row = $(''); // Add position. row.append($('', { 'text': i, 'style': 'width: 5%;' })); // Add song name. row.append($('', { 'text': playlist[i].title, 'style': 'width: 70%;' })); // Add duration. row.append($('', { 'text': playlist[i].duration, 'style': 'width: 15%;' })); // Add buttons. row.append($('', { 'html': $('
', { 'class': 'btn-group btn-group-justified header-button' }).append($('