diff --git a/korektury/api/urls.py b/korektury/api/urls.py index 6c1551ae..eb74d90f 100644 --- a/korektury/api/urls.py +++ b/korektury/api/urls.py @@ -5,4 +5,5 @@ from . import views urlpatterns = [ path('/stav', org_required(views.korektury_stav_view), name='korektury_api_pdf_stav'), path('oprava/stav', org_required(views.oprava_stav_view), name='korektury_api_oprava_stav'), + path('/opravy_a_komentare', org_required(views.opravy_a_komentare_view), name='korektury_api_opravy_a_komentare'), ] diff --git a/korektury/api/views.py b/korektury/api/views.py index 86149803..8bb610f0 100644 --- a/korektury/api/views.py +++ b/korektury/api/views.py @@ -1,5 +1,6 @@ from django.http import HttpResponseForbidden, JsonResponse from django.shortcuts import get_object_or_404 +from django.utils.html import linebreaks from django.views.decorators.csrf import csrf_exempt from rest_framework import serializers @@ -32,3 +33,33 @@ def oprava_stav_view(request, **kwargs): op.status = status op.save() return JsonResponse({'status': op.status}) + +class KomentarSerializer(serializers.ModelSerializer): + class Meta: + model = Komentar + fields = '__all__' + + def to_representation(self, instance): + ret = super().to_representation(instance) + ret["autor"] = str(instance.autor) + ret["text"] = linebreaks(ret["text"], autoescape=True) # Autora není třeba escapovat, ten se vkládá jako text. + return ret + +class OpravaSerializer(serializers.ModelSerializer): + class Meta: + model = Oprava + fields = '__all__' + + def to_representation(self, instance): + ret = super().to_representation(instance) + ret["komentare"] = [KomentarSerializer(komentar).data for komentar in instance.komentar_set.all()] + return ret + + # komentar_set = serializers.ListField(child=KomentarSerializer()) + +def opravy_a_komentare_view(request, pdf_id: int, **kwargs): + q = request.POST + + opravy = Oprava.objects.filter(pdf=pdf_id).all() + # Serializovat list je prý security vulnerability, tedy je přidán slovník pro bezpečnost + return JsonResponse({"context": [OpravaSerializer(oprava).data for oprava in opravy]}) diff --git a/korektury/static/korektury/opraf.css b/korektury/static/korektury/opraf.css index fd7b6c0a..515415ca 100644 --- a/korektury/static/korektury/opraf.css +++ b/korektury/static/korektury/opraf.css @@ -1,5 +1,6 @@ .textzanaseni { display:none; } .textzastarale { display:none; } +#prekomentar, #preoprava, #prepointer { display: none; } body { &[data-status="pridavani"] { @@ -79,6 +80,7 @@ img{background:white;} padding: 3px; border: 2px solid rgb(var(--rgb)); border-radius: 10px; + position: absolute; &:hover { border-width:3px; @@ -96,6 +98,8 @@ img{background:white;} } } + button img { pointer-events: none; } + .corr-header { overflow: auto; } diff --git a/korektury/static/korektury/opraf.js b/korektury/static/korektury/opraf.js index c2b55970..5677deff 100644 --- a/korektury/static/korektury/opraf.js +++ b/korektury/static/korektury/opraf.js @@ -1,129 +1,46 @@ +const W_SKIP = 10; +const H_SKIP = 5; +const POINTER_MIN_H = 30; + function place_comments_one_div(img_id, comments) { - var img = document.getElementById(img_id); - if( img == null ) { - return; + const img = document.getElementById("img-"+img_id); + if( img == null ) return; + const comments_sorted = comments.sort((a, b) => a.y - b.y); + + const par = img.parentNode; + const w = img.clientWidth; + + let bott_max = 0; + for (const oprava of comments_sorted) { + const x = oprava.x; + const y = oprava.y; + const htmlElement = oprava.htmlElement; + const pointer = oprava.pointer; + + par.appendChild(pointer); + par.appendChild(htmlElement); + + const delta_y = (y > bott_max) ? 0: bott_max - y + H_SKIP; + + pointer.style.left = x; + pointer.style.top = y; + pointer.style.width = w - x + W_SKIP; + pointer.style.height = POINTER_MIN_H + delta_y; + + htmlElement.style.left = w + W_SKIP; + htmlElement.style.top = y + delta_y; + + bott_max = Math.max(bott_max, htmlElement.offsetTop + htmlElement.offsetHeight); } - var par = img.parentNode; - var w = img.clientWidth; - var h = img.clientHeight; - var w_skip = 10; - var h_skip = 5; - var pointer_min_h = 30; - var bott_max = 0; - var comments_sorted = comments.sort(function (a,b) { - return a[2] - b[2]; - //pokus o hezci kladeni poiteru, ale nic moc - if( a[3] < b[3] ) { - return (a[2] + pointer_min_h)- b[2]; - } else { - return (a[2] - pointer_min_h)- b[2]; - } - - }); - for (c in comments_sorted) { - var id = comments_sorted[c][0]; - var x = comments_sorted[c][1]; - var y = comments_sorted[c][2]; - - var el = document.getElementById(id); - var elp = document.getElementById(id + "-pointer"); - - if( el == null || elp == null ) { - continue; - } - - par.appendChild(elp); - par.appendChild(el); - - var delta_y = (y > bott_max) ? 0: bott_max - y + h_skip; - - elp.style.left = x; - elp.style.top = y ; - elp.style.width = w - x + w_skip; - elp.style.height = pointer_min_h + delta_y; - - el.pointer = elp; - el.img_id = img_id; - el.x = x - el.y = y - el.style.position = 'absolute'; - el.style.left = w + w_skip; - el.style.top = y + delta_y; - - var bott = el.offsetTop + el.offsetHeight; - bott_max = ( bott_max > bott ) ? bott_max : bott; - } - if( par.offsetHeight < bott_max ) { - par.style.height = bott_max; - - } + if (par.offsetHeight < bott_max) par.style.height = bott_max; } function place_comments() { - for (var i=0; i < comments.length-1; i++) { - place_comments_one_div(comments[i][0], comments[i][1]) + for (let [img_id, opravy] of Object.entries(comments)) { + place_comments_one_div(img_id, opravy) } } -// show comment form, when clicked to image -function img_click(element, ev) { - switch(document.body.dataset.status){ - case "zanaseni": - if (!confirm("Právě jsou zanášeny korektury, opravdu chcete přidat novou?")) - return; - break; - case "zastarale": - if (!confirm("Toto PDF je již zastaralé, opravdu chcete vytvořit korekturu?")) - return; - break; - } - - var dx, dy; - var par = element.parentNode; - if( ev.pageX != null ) { - dx = ev.pageX - par.offsetLeft; - dy = ev.pageY - par.offsetTop; - } else { //IE - dx = ev.offsetX; - dy = ev.offsetY; - } - var img_id = element.id; - return show_form(img_id, dx, dy, '', '', ''); -} -// hide or show text of correction -function toggle_visibility(oprava){ - oprava.dataset.opravazobrazit = oprava.dataset.opravazobrazit !== 'true'; - for (var i=0;i +
-
{{k.autor}}
-
- -
- {% csrf_token %} - - -
- - + +
+
-
{{k.text|linebreaks}}
+ +
{# {{k.text|linebreaks}} #}
+ + + + diff --git a/korektury/templates/korektury/korekturovatko/__oprava.html b/korektury/templates/korektury/korekturovatko/__oprava.html index 33a47844..442aae06 100644 --- a/korektury/templates/korektury/korekturovatko/__oprava.html +++ b/korektury/templates/korektury/korekturovatko/__oprava.html @@ -1,63 +1,145 @@ {% load static %} -
-
-
+ +
-
- {% for k in o.komentare %} - {% include "korektury/korekturovatko/__komentar.html" %} -
- {% endfor %} + {# {% for k in o.komentare %} {% include "korektury/korekturovatko/__komentar.html" %}
{% endfor %} #}
+
- - -
- {% csrf_token %} - + + + + + + + - - - - - - + + + - - - -
- +
+ + diff --git a/korektury/templates/korektury/korekturovatko/__stranky.html b/korektury/templates/korektury/korekturovatko/__stranky.html new file mode 100644 index 00000000..b6f9932b --- /dev/null +++ b/korektury/templates/korektury/korekturovatko/__stranky.html @@ -0,0 +1,50 @@ +{% for i in img_indexes %} +
+ Strana {{ i|add:1 }} +
+ +
+{% endfor %} + + diff --git a/korektury/templates/korektury/korekturovatko/_main.html b/korektury/templates/korektury/korekturovatko/_main.html index ee22f38b..25ed71fb 100644 --- a/korektury/templates/korektury/korekturovatko/_main.html +++ b/korektury/templates/korektury/korekturovatko/_main.html @@ -1,47 +1,40 @@ {% include "korektury/korekturovatko/__edit_komentar.html" %} -{% for i in img_indexes %} -
- Strana {{ i|add:1 }} -
+{% include "korektury/korekturovatko/__stranky.html" %} -
-{% endfor %} - - -{% for o in opravy %} - {% include "korektury/korekturovatko/__oprava.html" %} -{% endfor %} +{# {% for o in opravy %} {% include "korektury/korekturovatko/__oprava.html" %} {% endfor %} #} +{% include "korektury/korekturovatko/__oprava.html" %} +{% include "korektury/korekturovatko/__komentar.html" %} - - + + + +