Added instructions dropdown if downloaded

This commit is contained in:
FrederikBaerentsen 2024-11-20 21:57:56 +01:00
parent 665649a33c
commit b357e08421
2 changed files with 212 additions and 26 deletions

26
app.py
View File

@ -1,4 +1,4 @@
from flask import Flask, request, redirect, jsonify, render_template, Response,url_for
from flask import Flask, request, redirect, jsonify, render_template, Response,url_for, send_from_directory
import os
import json
from flask_socketio import SocketIO
@ -22,6 +22,9 @@ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1, x_
socketio = SocketIO(app,cors_allowed_origins=os.getenv("DOMAIN_NAME"))
count = 0
DIRECTORY = os.path.join(os.getcwd(), 'static', 'instructions')
@app.route('/favicon.ico')
# SocketIO event handler for client connection
@ -67,6 +70,11 @@ def start_task(data):
#return redirect('/')
def hyphen_split(a):
if a.count("-") == 1:
return a.split("-")[0]
return "-".join(a.split("-", 2)[:2])
@app.route('/delete/<tmp>',methods=['POST', 'GET'])
def delete(tmp):
@ -525,7 +533,13 @@ def index():
cursor.close()
conn.close()
return render_template('index.html',set_list=set_list,themes_list=theme_file,missing_list=missing_list)
files = [f for f in os.listdir(DIRECTORY) if f.endswith('.pdf')]
#files = [re.match(r'^([\w]+-[\w]+)', f).group() for f in os.listdir(DIRECTORY) if f.endswith('.pdf')]
print(files.sort())
return render_template('index.html',set_list=set_list,themes_list=theme_file,missing_list=missing_list,files=files)
if request.method == 'POST':
set_num = request.form.get('set_num')
@ -579,6 +593,14 @@ def index():
return ('', 204)
# Route to serve individual files
@app.route('/files/<path:filename>', methods=['GET'])
def serve_file(filename):
try:
return send_from_directory(DIRECTORY, filename)
except Exception as e:
return jsonify({'error': str(e)}), 404
@app.route('/<tmp>/<u_id>', methods=['GET', 'POST'])
def inventory(tmp,u_id):

View File

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html data-theme="light" class="has-navbar-fixed-top">
ns.<html data-theme="light" class="has-navbar-fixed-top">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@ -82,7 +82,6 @@
</style>
</head>
<body>
<nav class="navbar is-dark is-fixed-top" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">
@ -276,29 +275,111 @@
<p class="card-footer-item">
<span>
<a class="is-size-6" style="color: #363636;" href="/{{ i[0] }}/{{ i[11] }}">Inv.</a>
<a class="is-size-6" style="color: #363636;" href="/{{ i[0] }}/{{ i[11] }}">Inv.</a>
</span>
</p>
<p class="card-footer-item">
<span>
<a class="inst is-size-6" style="color: #363636;" href="/static/instructions/{{ i[0] }}.pdf">Inst.</a>
</span>
</p>
{% set ns = namespace(found=false) %}
{% for file in files %}
{% if ns.found is sameas false and file.startswith(i[0]) %}
<div class="card-footer-item dropdown" style="width:100%">
<div class="dropdown-trigger">
<button class="is-size-6" aria-haspopup="true" aria-controls="dropdown-menu3">
<span >Instructions</span>
<span class="icon is-small">
<i class="fas fa-angle-down" aria-hidden="true"></i>
</span>
</button>
</div>
<div class="dropdown-menu" id="dropdown-menu3" role="menu">
<div class="dropdown-content">
<!-- <a class="js-modal-trigger is-size-6" style="color: #363636;" data-id="{{ i[0] }}" data-target="modal-inst">Inst.</a> -->
{% set ns1 = namespace(count=0) %}
{% for x in files %}
{% if x.startswith(i[0]) %}
<a href="/files/{{ x }}" class="dropdown-item">{{ x }}</a>
{% endif %}
{% endfor %}
</div>
</div>
</div>
{% set ns.found = true %}
{% endif %}
{% endfor %}
</footer>
</div>
</div>
<div class="modal" id="modal-inst">
<div class="modal-background"></div>
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title">Instructions</p>
<button class="delete" aria-label="close"></button>
</header>
<section class="modal-card-body">
<span>
{% for file in files %}
<span>
<a> {{ file }} </a></span>
{% if file.startswith(i[0]) %}
<a class="inst is-size-6" style="color: #363636;" href="/files/{{ file }}.pdf">{{ file }}</a>
{% endif %}
{% endfor %}
</span>
</section>
<footer class="modal-card-foot">
<div class="buttons">
<button class="button">Cancel</button>
</div>
</footer>
</div>
</div>
{% endfor %}
</div>
<script>
window.addEventListener('DOMContentLoaded', function() {
var observer = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
var link = entry.target;
var pdfUrl = link.getAttribute('href');
//var observer = new IntersectionObserver(function(entries, observer) {
// entries.forEach(function(entry) {
// if (entry.isIntersecting) {
// var link = entry.target;
// var pdfUrl = link.getAttribute('href');
// var xhr = new XMLHttpRequest();
// xhr.open('HEAD', pdfUrl, true);
// xhr.onload = function() {
// if (xhr.status !== 200) {
// If PDF is not found, disable the link
// link.style.pointerEvents = 'none';
// link.style.color = '#ccc'; // Optionally, change link color to indicate it's disabled
// } else {
// link.style.pinterEvents = 'auto';
// }
// };
// xhr.send();
// observer.unobserve(link);
// }
// });
// });
// var links = document.querySelectorAll('.inst');
// links.forEach(function(link) {
// observer.observe(link);
// });
function testURL(pdfUrl,link){
var xhr = new XMLHttpRequest();
xhr.open('HEAD', pdfUrl, true);
@ -306,20 +387,17 @@ window.addEventListener('DOMContentLoaded', function() {
if (xhr.status !== 200) {
// If PDF is not found, disable the link
link.style.pointerEvents = 'none';
console.log('not')
link.style.color = '#ccc'; // Optionally, change link color to indicate it's disabled
}
} else {
link.style.pinterEvents = 'auto';
}
};
xhr.send();
observer.unobserve(link);
}
});
});
var links = document.querySelectorAll('.inst');
links.forEach(function(link) {
observer.observe(link);
});
}
window.addEventListener('DOMContentLoaded', function() {
});
function searchFunction() {
@ -685,6 +763,92 @@ $(".navbar-item").click(function() {
});
});
document.addEventListener('DOMContentLoaded', () => {
// Functions to open and close a modal
function openModal($el) {
$el.classList.add('is-active');
}
function closeModal($el) {
$el.classList.remove('is-active');
}
function closeAllModals() {
(document.querySelectorAll('.modal') || []).forEach(($modal) => {
closeModal($modal);
});
}
// Add a click event on buttons to open a specific modal
(document.querySelectorAll('.js-modal-trigger') || []).forEach(($trigger) => {
const modal = $trigger.dataset.target;
const $target = document.getElementById(modal);
$trigger.addEventListener('click', () => {
openModal($target);
});
});
// Add a click event on various child elements to close the parent modal
(document.querySelectorAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button') || []).forEach(($close) => {
const $target = $close.closest('.modal');
$close.addEventListener('click', () => {
closeModal($target);
});
});
// Add a keyboard event to close all modals
document.addEventListener('keydown', (event) => {
if(event.key === "Escape") {
closeAllModals();
}
});
});
//var dropdown = document.querySelector('.dropdown');
//dropdown.addEventListener('click', function(event) {
// event.stopPropagation();
// dropdown.classList.toggle('is-active');
//});
// Get all dropdowns on the page that aren't hoverable.
const dropdowns = document.querySelectorAll('.dropdown:not(.is-hoverable)');
if (dropdowns.length > 0) {
// For each dropdown, add event handler to open on click.
dropdowns.forEach(function(el) {
el.addEventListener('click', function(e) {
closeDropdowns();
e.stopPropagation();
el.classList.toggle('is-active');
});
});
// If user clicks outside dropdown, close it.
document.addEventListener('click', function(e) {
closeDropdowns();
});
}
/*
* Close dropdowns by removing `is-active` class.
*/
function closeDropdowns() {
dropdowns.forEach(function(el) {
el.classList.remove('is-active');
});
}
// Close dropdowns if ESC pressed
document.addEventListener('keydown', function (event) {
let e = event || window.event;
if (e.key === 'Esc' || e.key === 'Escape') {
closeDropdowns();
}
});