Merge branch 'metadata'

This commit is contained in:
Martin Müller 2018-01-17 01:28:29 +01:00
commit 629e63c292
3 changed files with 89 additions and 7 deletions

View File

@ -4,8 +4,7 @@ import sys
import subprocess
import sqlite3
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, g
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, g, jsonify
import config
@ -155,3 +154,25 @@ def play_random():
sounds = [os.fsencode(file).decode() for file in os.listdir(config.path)]
random_sound = random.sample(sounds, 1)[0]
subprocess.Popen(["omxplayer", os.path.join(config.path, random_sound).encode("utf-8")], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
@app.route("/tags/<tags>")
def tags(tags):
# Credits: https://stackoverflow.com/a/38955049/1532986
query = """\
SELECT button.file
FROM button
WHERE button.id IN (
SELECT button_tags.fk_button
FROM button_tags
JOIN tag
ON tag.id = button_tags.fk_tag
GROUP BY button_tags.fk_button
HAVING
"""
tagFilter = "\t\tSUM(CASE WHEN tag.name = \"{}\" THEN 1 ELSE 0 END) > 0"
tagFilters = " AND \n".join([tagFilter.format(tag) for tag in tags.split(",")])
query += tagFilters + "\n)"
sounds = queryDB(query)
return jsonify([sound["file"].split(".")[0] for sound in sounds])

View File

@ -103,8 +103,13 @@ button:active {
}
.tags li a:hover {
background-color: #eee;
color: #333;
border-color: #eee;
color: #eee;
}
.tagged {
background-color: #eee !important;
color: #333 !important;
}
label {

View File

@ -1,6 +1,7 @@
/* jshint esversion: 6 */
var localModeEnabled = false;
var selectedTags = [];
// References to howler objects
var howlerSounds = [];
@ -43,7 +44,6 @@ ready(function() {
searchfield.focus();
searchFilter(searchfield, ".sound", "inline-block");
searchFilter(searchfield, ".tag", "block");
var reset = document.querySelector("#reset");
var sounds_nav = document.querySelector("#sounds-nav");
@ -74,6 +74,22 @@ function searchFilter(searchfield, itemSelector, unHideStyle) {
}
}
function tagFilter(sounds) {
var items = document.querySelectorAll(".sound");
items.forEach(function(item) {
item.style.display = "inline-block";
});
items.forEach(function(item) {
var name = item.firstChild.innerHTML;
if (sounds.length > 0 && sounds.indexOf(name.toLowerCase()) === -1) {
item.style.display = "none";
}
});
}
/*
* Adds all necessary KeyListeners to document
*/
@ -113,6 +129,42 @@ function addKeyListeners() {
}
});
});
var tags = document.querySelectorAll(".tags li");
tags.forEach(function(tag) {
tag.addEventListener("click", function(e) {
var className = "tagged";
var el = this.firstChild;
var classes = el.className.split(" ");
var existingIndex = classes.indexOf(className);
if (existingIndex >= 0)
classes.splice(existingIndex, 1);
else
classes.push(className);
el.className = classes.join(" ");
var i = selectedTags.indexOf(el.innerHTML);
if (i == -1)
selectedTags.push(el.innerHTML);
else
selectedTags.splice(i, 1);
console.log(selectedTags);
if (selectedTags.length > 0) {
ajaxRequest("/tags/" + selectedTags.join(), function() {
tagFilter(JSON.parse(this.responseText));
});
} else {
tagFilter([]);
}
});
});
}
@ -145,11 +197,15 @@ function hideSections() {
});
}
function ajaxRequest(url) {
function ajaxRequest(url, callback) {
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("GET", url, true);
if (typeof callback !== 'undefined')
ajaxRequest.onload = callback;
ajaxRequest.send(null);
} catch (e) {
alert("Unfortunately we were unable to handle your request. Please try again later and contact the server administrator if the problem persists.");