From 3e1331b0e3024abe6cdbe9d389571788465d44d7 Mon Sep 17 00:00:00 2001 From: Krateng Date: Thu, 30 Jul 2020 15:31:29 +0200 Subject: [PATCH] Reorganized backend --- maloja/static/js/manualscrobble.js | 123 ++++++++++++ maloja/web/jinja/admin.jinja | 105 +++------- .../{issues.jinja => admin_issues.jinja} | 20 +- maloja/web/jinja/admin_manual.jinja | 46 +++++ maloja/web/jinja/admin_overview.jinja | 77 ++++++++ .../jinja/{setup.jinja => admin_setup.jinja} | 25 +-- maloja/web/jinja/base.jinja | 2 +- maloja/web/jinja/manual.jinja | 186 ------------------ 8 files changed, 285 insertions(+), 299 deletions(-) create mode 100644 maloja/static/js/manualscrobble.js rename maloja/web/jinja/{issues.jinja => admin_issues.jinja} (78%) create mode 100644 maloja/web/jinja/admin_manual.jinja create mode 100644 maloja/web/jinja/admin_overview.jinja rename maloja/web/jinja/{setup.jinja => admin_setup.jinja} (85%) delete mode 100644 maloja/web/jinja/manual.jinja diff --git a/maloja/static/js/manualscrobble.js b/maloja/static/js/manualscrobble.js new file mode 100644 index 0000000..8b24c30 --- /dev/null +++ b/maloja/static/js/manualscrobble.js @@ -0,0 +1,123 @@ +function keyDetect(event) { + if (event.key === "Enter" || event.key === "Tab") { addArtist() } + if (event.key === "Backspace" && document.getElementById("artists").value == "") { removeArtist() } +} + +function addArtist() { + element = document.getElementById("artists"); + newartist = element.value.trim(); + element.value = ""; + if (newartist != "") { + artist = document.createElement("span"); + artist.innerHTML = newartist; + artist.style = "padding:5px;"; + document.getElementById("artists_td").insertBefore(artist,element); + + element.placeholder = "Backspace to remove last" + } +} +function removeArtist() { + artists = document.getElementById("artists_td").getElementsByTagName("span") + lastartist = artists[artists.length-1] + document.getElementById("artists_td").removeChild(lastartist); + if (artists.length < 1) { + document.getElementById("artists").placeholder = "Separate with Enter" + } +} + + +function scrobbleIfEnter(event) { + if (event.key === "Enter") { + scrobbleNew() + } +} + +function scrobbleNew() { + artistnodes = document.getElementById("artists_td").getElementsByTagName("span"); + artists = []; + for (let node of artistnodes) { + artists.push(node.innerHTML); + } + title = document.getElementById("title").value; + scrobble(artists,title); +} + +function scrobble(artists,title) { + + + artist = artists.join(";"); + + if (title != "" && artists.length > 0) { + xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = scrobbledone + xhttp.open("GET","/api/newscrobble?artist=" + encodeURIComponent(artist) + + "&title=" + encodeURIComponent(title), true); + xhttp.send(); + } + + document.getElementById("title").value = ""; + document.getElementById("artists").value = ""; + parent = document.getElementById("artists_td"); + artists = document.getElementById("artists_td").getElementsByTagName("span") + while (artists.length > 0) { + removeArtist(); + } +} + +function scrobbledone() { + if (this.readyState == 4 && this.status == 200) { + result = JSON.parse(this.responseText); + txt = result["track"]["title"] + " by " + result["track"]["artists"][0]; + if (result["track"]["artists"].length > 1) { + txt += " et al."; + } + document.getElementById("notification").innerHTML = "Scrobbled " + txt + "!"; + } + +} + + + + +/// +// SEARCH +/// + +function search_manualscrobbling(searchfield) { + txt = searchfield.value; + if (txt == "") { + + } + else { + xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = searchresult_manualscrobbling; + xhttp.open("GET","/api/search?max=5&query=" + encodeURIComponent(txt), true); + xhttp.send(); + } +} +function searchresult_manualscrobbling() { + if (this.readyState == 4 && this.status == 200) { + document.getElementById("searchresults").innerHTML = ""; + result = JSON.parse(this.responseText); + tracks = result["tracks"].slice(0,10); + console.log(tracks); + for (let t of tracks) { + track = document.createElement("span"); + trackstr = t["artists"].join(", ") + " - " + t["title"]; + tracklink = t["link"]; + track.innerHTML = "" + trackstr + ""; + row = document.createElement("tr") + col1 = document.createElement("td") + col1.className = "button" + col1.innerHTML = "Scrobble!" + col1.onclick = function(){ scrobble(t["artists"],t["title"])}; + col2 = document.createElement("td") + row.appendChild(col1) + row.appendChild(col2) + col2.appendChild(track) + document.getElementById("searchresults").appendChild(row); + } + + + } +} diff --git a/maloja/web/jinja/admin.jinja b/maloja/web/jinja/admin.jinja index 5499184..7e211fb 100644 --- a/maloja/web/jinja/admin.jinja +++ b/maloja/web/jinja/admin.jinja @@ -1,54 +1,5 @@ {% extends "base.jinja" %} -{% block title %}Maloja - Admin Panel{% endblock %} - -{% block scripts %} - - -{% endblock %} +{% block title %}Maloja - Backend{% endblock %} {% block content %} @@ -63,36 +14,36 @@

Admin Panel

+
+ + {% if page=='admin_overview' %} + Overview + {% else %} + Overview + {% endif %} | + {% if page=='admin_setup' %} + Server Setup + {% else %} + Server Setup + {% endif %} | + {% if page=='admin_manual' %} + Manual Scrobbling + {% else %} + Manual Scrobbling + {% endif %} | + {% if page=='admin_issues' %} + Database Maintenance + {% else %} + Database Maintenance + {% endif %} + +

+ -

Update

- - Currently installed Maloja version: Loading...
- Latest recommended Maloja version: Loading...
- - - -

Admin Mode

- - Admin Mode allows you to manually scrobble from various places on the web interface instead of just the dedicated page.

- {% if adminmode %} - Deactivate - {% else %} - Activate - {% endif %} - -

Links

- - Server Setup
- Manual Scrobbling
- Database Maintenance - -

External

- - Report Issue
- - + {% block maincontent %} + {% endblock %} {% endblock %} diff --git a/maloja/web/jinja/issues.jinja b/maloja/web/jinja/admin_issues.jinja similarity index 78% rename from maloja/web/jinja/issues.jinja rename to maloja/web/jinja/admin_issues.jinja index b1672e4..d078a31 100644 --- a/maloja/web/jinja/issues.jinja +++ b/maloja/web/jinja/admin_issues.jinja @@ -1,9 +1,9 @@ -{% extends "base.jinja" %} +{% set page ='admin_issues' %} +{% extends "admin.jinja" %} {% block title %}Maloja - Issues{% endblock %} {% block scripts %} - +{% endblock %} + + +{% block maincontent %} + +

Scrobble new discovery

+ + + + + + + + +
+ Artists: + + +
+ Title: + + +
+ +
+ + Scrobble! + +
+ + + +

Search

+ + +

+
+ + +{% endblock %} diff --git a/maloja/web/jinja/admin_overview.jinja b/maloja/web/jinja/admin_overview.jinja new file mode 100644 index 0000000..2b4ece2 --- /dev/null +++ b/maloja/web/jinja/admin_overview.jinja @@ -0,0 +1,77 @@ +{% set page ='admin_overview' %} +{% extends "admin.jinja" %} +{% block title %}Maloja - Admin Panel{% endblock %} + +{% block scripts %} + +{% endblock %} + + + {% block maincontent %} + +

Update

+ + Currently installed Maloja version: Loading...
+ Latest recommended Maloja version: Loading...
+ + + +

Admin Mode

+ + Admin Mode allows you to manually scrobble from various places on the web interface instead of just the dedicated page.

+ {% if adminmode %} + Deactivate + {% else %} + Activate + {% endif %} + +

Links

+ + Report Issue
+ Readme
+ PyPi
+ {% endblock %} diff --git a/maloja/web/jinja/setup.jinja b/maloja/web/jinja/admin_setup.jinja similarity index 85% rename from maloja/web/jinja/setup.jinja rename to maloja/web/jinja/admin_setup.jinja index 0b93561..f7ec84e 100644 --- a/maloja/web/jinja/setup.jinja +++ b/maloja/web/jinja/admin_setup.jinja @@ -1,8 +1,8 @@ -{% extends "base.jinja" %} +{% set page ='admin_setup' %} +{% extends "admin.jinja" %} {% block title %}Maloja - Setup{% endblock %} {% block scripts %} - - - -{% endblock %} - - - -{% block content %} - - - - - - -
-
-
-

Manual Scrobbling


-

- - -
- -

Scrobble new discovery

- - - - - - - - -
- Artists: - - -
- Title: - - -
- -
- - Scrobble! - -
- - - -

Search

- - -

-
- - -{% endblock %}