From 69b65fb8ec7e436dc11ff2fe8b09876c52097cb2 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Tue, 15 Jan 2019 16:21:33 +0200 Subject: [PATCH] search clear, revert socketio bs --- public/index.css | 13 ++++ public/index.html | 1 + public/index.js | 8 +++ src/server.js | 9 +-- src/server/sockets.js | 141 ------------------------------------------ 5 files changed, 25 insertions(+), 147 deletions(-) delete mode 100644 src/server/sockets.js diff --git a/public/index.css b/public/index.css index 49a1ec7..59b7660 100644 --- a/public/index.css +++ b/public/index.css @@ -181,11 +181,24 @@ tr:nth-child(even) { display: flex; flex-direction: row; min-height: fit-content; + position: relative; } .inline-flex input { flex-grow: 1; min-width: 0; } +#search-clear { + display: none; +} +.btn-clear { + cursor: pointer; + position: absolute; + right: 10px; + top: 12px; + font-size: 2em; + opacity: 0.5; + padding: 0 10px; +} .queue-tag { padding: 10px; background-color: #007d32; diff --git a/public/index.html b/public/index.html index 5648b73..a65c8aa 100644 --- a/public/index.html +++ b/public/index.html @@ -17,6 +17,7 @@ + x
diff --git a/public/index.js b/public/index.js index dc13a19..a119a57 100644 --- a/public/index.js +++ b/public/index.js @@ -1,6 +1,7 @@ (function () { var table = document.getElementById('ttable') var input = document.getElementById('search') + var clear = document.getElementById('search-clear') var audio = document.getElementById('player') var playing = document.getElementById('playing') @@ -307,6 +308,7 @@ searching = false pagePrev = 0 pages = pagesPrev + clear.style.display = 'none' } if (page > pages) return @@ -330,6 +332,7 @@ pagePrev = pageNum pagesPrev = pages searching = true + clear.style.display = 'block' } httpGet('/api/tracks/search?q=' + query + '&page=' + page).then(function (data) { @@ -401,6 +404,11 @@ audio.addEventListener('ended', playNext, false) + clear.addEventListener('click', function () { + input.value = '' + showTracks(pagePrev !== 0 ? pagePrev : 1) + }) + document.getElementById('player-next').addEventListener('click', playNext, false) document.getElementById('player-prev').addEventListener('click', function (e) { diff --git a/src/server.js b/src/server.js index 911e632..8b64d4f 100644 --- a/src/server.js +++ b/src/server.js @@ -2,7 +2,6 @@ import path from 'path' import sqlite from 'sqlite' import Promise from 'bluebird' import express from 'express' -import sockets from './server/sockets' require('express-async-errors') @@ -18,9 +17,6 @@ const port = process.env.PORT || 3000 const router = express.Router() -const http = require('http').Server(app) -const io = require('socket.io')(http) - router.get('/tracks', async (req, res) => { let page = parseInt(req.query.page) || 1 if (isNaN(page)) { @@ -156,7 +152,8 @@ app.use('/api', router) app.use('/file/track', express.static(path.resolve(values.directory))) app.use('/', express.static(path.join(process.cwd(), 'public'))) -http.listen(port, '127.0.0.1', function () { - sockets.start(io) +const host = process.env.NODE_ENV === 'development' ? '0.0.0.0' : '127.0.0.1' + +app.listen(port, host, function () { console.log(`app running on port ${port}`) }) diff --git a/src/server/sockets.js b/src/server/sockets.js deleted file mode 100644 index 2ca1ec1..0000000 --- a/src/server/sockets.js +++ /dev/null @@ -1,141 +0,0 @@ - -let data = { - clients: {}, - - // Key: Controller - // Value: Controlee - // Music is played on Value's device. - control: {} -} - -function getSocketByDevice (devid) { - for (let id in data.clients) { - let sock = data.clients[i] - if (sock.device && sock.device === devid) { - return sock - } - } - - return null -} - -function getControlling (sockid) { - for (let id in data.control) { - let cl = data.control[id] - if (cl === sockid) return id - } - - return null -} - -function start (io) { - io.on('connection', function (socket) { - let sockid = socket.id - - socket.on('auth', function (device) { - if (getSocketByDevice(device) != null) { - return socket.emit('cerr', {error: 'devexists', code: 0}) - } - - socket.device = device - data.clients[sockid] = socket - }) - - socket.on('req:devs', function () { - let devices = [] - - for (let i in data.clients) { - let cl = data.clients[i] - if (cl.device === socket.device) continue - devices.push(devices) - } - - socket.emit('res:devs', devices) - }) - - // Controller wants to take control of Controlee - socket.on('ctrl:connect', function (ctrlee) { - let dev = getSocketByDevice(ctrlee) - if (!dev) return socket.emit('cerr', {error: 'devnull', code: 1}) - - // Device is already controlling/being controlled - if (data.control[sockid] || getControlling(dev.id)) return socket.emit('cerr', {error: 'devctrl', code: 2}) - - dev.emit('ctrl:connect', { device: socket.device }) - socket.emit('targ:success', { device: ctrlee }) - }) - - // Disconnect - socket.on('ctrl:disconnect', function (ctrlee) { - let id, ctrl - if (getControlling(sockid)) { - // If this is a controlee, disconnect from controller - id = getControlling(sockid) - ctrl = data.clients[id] - - delete data.control[id] - } else if (data.control[sockid]) { - // If this is a controller, disconnect from controlee - id = data.control[sockid] - ctrl = data.clients[id] - - delete data.control[sockid] - } - - if (!ctrl) return - - ctrl.emit('targ:disconnect', {}) - }) - - let directpass = ['track', 'pause', 'seek', 'volume', 'mute', 'queue', 'req:queue', 'res:queue'] - for (let i in directpass) { - let syn = 'sync:' + directpass[i] - - // Set pause/play state - // Sent from controlee to controller - socket.on(syn, function (data) { - let ctrl - - if (getControlling(sockid)) { - // If this is a controlee, send to controller - ctrl = data.clients[getControlling(sockid)] - } else if (data.control[sockid]) { - // If this is a controller, send to controlee - ctrl = data.clients[data.control[sockid]] - } - - ctrl && ctrl.emit('set:' + directpass[i], data) - }) - } - - // Synchronize timestamp - socket.on('sync:time', function (data) { - if (!getControlling(sockid)) return - let ctrl = data.clients[getControlling(sockid)] - ctrl && ctrl.emit('sync:time', data) - }) - - socket.on('disconnect', function () { - if (data.clients[sockid]) { - let dev = data.clients[sockid] - - // Controller loses connection - // Emit to controlee - if (data.control[sockid]) { - let clt = data.clients[sockid] - clt && clt.emit('ctrl:disconnect', {}) - - // Controlee loses connection - // Emit to controller - } else if (getControlling(sockid)) { - let clt = data.clients[getControlling(sockid)] - clt && clt.emit('targ:disconnect', {}) - } - - delete data.clients[sockid] - } - }) - }) -} - -module.exports = { start }