1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
<!doctype html>
<html lang="de">
<head>
<title>Lehrerspruch-Wahlplattform</title>
<link rel="stylesheet" type="text/css" href="/static/materialize.min.css">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0" />
</head>
<body>
<script type="text/javascript" src="/static/jquery.min.js"></script>
<script type="text/javascript" src="/static/materialize.min.js"></script>
<div class="container">
<div class="row">
<h3>Lehrersprüche</h3>
<div class="section">
{% if user.loggedin %}
<form action="/logout" method="post">
<input type="hidden" name="redirpage" value="{{ currentpage }}">
<button class="btn" type="submit">Logout ({{ user.name }})</button>
</form>
{% else %}
<form action="/login" method="post">
<input type="hidden" name="redirpage" value="{{ currentpage }}">
<div class="col s6 m4 l2 input-field">
<label for="user">Benutzer</label>
<input type="text" name="user">
</div>
<div class="col s6 m4 l2 input-field">
<label for="user">Passwort</label>
<input type="password" name="password">
</div>
<div class="col s6 m4 l2">
<button class="btn col s12" type="submit" name="action" value="login">Login</button>
</div>
<div class="col s6 m4 l2">
<button class="btn col s12" type="submit" name="action" value="register">Anmelden</button>
</div>
</form>
{% endif %}
</div>
</div>
{% with msgs = get_flashed_messages() %}
{% if msgs %}
<div>
{% for msg in msgs %}
<p>{{ msg }}</p>
{% endfor %}
</div>
{% endif %}
{% endwith %}
<ul class="collection">
{% for joke in jokes %}
<li class="collection-item row valign-wrapper">
<div class="col s3 m2 valign">
<form action="/vote" method="post">
<input type="hidden" name="id" value="{{ joke.id }}">
<input type="hidden" name="redirpage" value="{{ currentpage }}">
<button class="col s12 upvotebtn btn{% if joke.reports>0 %}-flat{% endif %}{% if joke.upvoted %} disabled{% endif %}" type="submit" name="vote" value="upvote">
<i class="material-icons">thumb_up</i> <span class="btnval" data-original="{% if joke.upvoted %}{{ joke.upvotes-1 }}{% else %}{{ joke.upvotes }}{% endif %}">{{ joke.upvotes }}</span>
</button>
<button class="col s12 downvotebtn btn{% if joke.reports>0 %}-flat{% endif %}{% if joke.downvoted %} disabled{% endif %}" type="submit" name="vote" value="downvote">
<i class="material-icons">thumb_down</i> <span class="btnval" data-original="{% if joke.downvoted %}{{ joke.downvotes-1 }}{% else %}{{ joke.downvotes }}{% endif %}">{{ joke.downvotes }}</span>
</button>
</form>
<form action="/report" method="post">
<input type="hidden" name="id" value="{{ joke.id }}">
<input type="hidden" name="redirpage" value="{{ currentpage }}">
<button class="reportbtn btn-flat{% if joke.reported %} disabled{% endif %}" type="submit">
<i class="material-icons left">report_problem</i>
<span class="btnval red-text" data-original="{% if joke.reported %}{{ joke.reports-1 }}{% else %}{{ joke.reports }}{% endif %}">{{ joke.reports }}</span>
</button>
</form>
{% if joke.mine %}
<form action="/delete" method="post">
<input type="hidden" name="id" value="{{ joke.id }}">
<input type="hidden" name="redirpage" value="{{ currentpage }}">
<button class="deletebtn btn-flat" type="submit"><i class="material-icons">delete</i></button>
</form>
{% endif %}
</div>
<div class="flow-text col s9 m10 {% if joke.reports>0 %}grey-text{% endif %}" style="line-height: 1.5">
{{ joke.text|safe }}
</div>
</li>
{% endfor %}
</ul>
<div class="section center">
<ul class="pagination">
<li{% if currentpage==0 %} class="disabled"{% endif %}><a href="/page/{{ currentpage-1 }}"><i class="material-icons">chevron_left</i></a></li>
{% for page in pages %}
<li{% if currentpage==loop.index0 %} class="active"{% endif %}><a href="/page/{{ loop.index0 }}">{{ loop.index }}</a></li>
{% endfor %}
<li{% if currentpage==loop.length-1 %} class="disabled"{% endif %}><a href="/page/{{ currentpage+1 }}"><i class="material-icons">chevron_right</i></a></li>
</ul>
</div>
<div class="divider"></div>
<div class="section">
<form action="/submit" method="post">
<input type="hidden" name="redirpage" value="{{ currentpage }}">
<div class="row">
<div class="col s12 m9 input-field">
<label for="text">Neuen Spruch hinzufügen</label>
<textarea class="materialize-textarea" name="text"></textarea>
</div>
<div class="col s12 m3">
<input type="submit" class="btn" value="hinzufügen">
</div>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('.upvotebtn').click(function(e) { submitAjaxer.call(this, e, 'upvote'); });
$('.downvotebtn').click(function(e) { submitAjaxer.call(this, e, 'downvote'); });
$('.reportbtn').click(function(e) { submitAjaxer.call(this, e, 'report'); });
$('.deletebtn').click(function(e) { submitAjaxer.call(this, e, 'delete'); });
function submitAjaxer(e, type) {
e.preventDefault();
var form = $(this).closest('form');
var container = $(this).closest('div');
var data;
if (type == 'upvote' || type == 'downvote') {
senddata = form.serialize() + '&vote=' + type;
} else {
senddata = form.serialize();
}
$.ajax({
url: form.attr('action'),
type: "POST",
data: senddata,
success: function(data) {
if (type == 'delete') {
var deletebtn = $('.deletebtn', container);
var id = deletebtn.data('id');
var undobtn = $('<button class="btn col s12"><i class="material-icons">delete undo</i></button>');
undobtn.click(function() {
$.ajax({
url: form.attr('action').replace(/\/delete$/, '/undelete'),
type: "POST",
data: senddata,
success: function() {
window.location.reload(false); // TODO rebuild DOM
}
})
});
container.html(undobtn);
return;
}
var upvotebtn = $('.upvotebtn', container);
var downvotebtn = $('.downvotebtn', container);
var reportbtn = $('.reportbtn', container);
var upvotes = parseInt($('.btnval', upvotebtn).data('original'));
var downvotes = parseInt($('.btnval', downvotebtn).data('original'));
var reports = parseInt($('.btnval', reportbtn).data('original'));
$('.btnval', upvotebtn).text(upvotes);
$('.btnval', downvotebtn).text(downvotes);
$('.btnval', reportbtn).text(reports);
upvotebtn.removeClass('disabled');
upvotebtn.removeClass('btn-flat');
upvotebtn.addClass('btn');
downvotebtn.removeClass('disabled');
downvotebtn.removeClass('btn-flat');
downvotebtn.addClass('btn');
reportbtn.removeClass('disabled');
if (type == "upvote") {
$('.btnval', upvotebtn).text(upvotes+1);
upvotebtn.addClass('disabled');
} else if (type == "downvote") {
$('.btnval', downvotebtn).text(downvotes+1);
downvotebtn.addClass('disabled');
} else if (type == "report") {
$('.btnval', reportbtn).text(reports+1);
reportbtn.addClass('disabled');
downvotebtn.addClass('btn-flat');
downvotebtn.removeClass('btn');
upvotebtn.addClass('btn-flat');
upvotebtn.removeClass('btn');
}
}
});
}
});
</script>
</body>
</html>
|