aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets_src/js/list.js24
-rw-r--r--modules/lists/lists.go80
-rw-r--r--views/includes/list_gril.html2
3 files changed, 102 insertions, 4 deletions
diff --git a/assets_src/js/list.js b/assets_src/js/list.js
index 8246950..b3541e0 100644
--- a/assets_src/js/list.js
+++ b/assets_src/js/list.js
@@ -19,18 +19,36 @@ async function resultClicked(grilId) {
19 document.getElementById('gril-list').innerHTML += response; 19 document.getElementById('gril-list').innerHTML += response;
20} 20}
21 21
22function listButtonHandler(e) { 22async function updateGrilOrder(gril, pos) {
23 return await ajax.post(
24 `${window.location}/order`,
25 `gril=${gril}&pos=${pos}`,
26 { headers: {
27 'Content-Type': 'application/x-www-form-urlencoded',
28 } });
29}
30
31async function listButtonHandler(e) {
23 let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li'; 32 let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li';
24 let item = dom.closest(e.target, liFilter); 33 let item = dom.closest(e.target, liFilter);
25 let pItem = dom.previous(item, liFilter), 34 let pItem = dom.previous(item, liFilter),
26 nItem = dom.next(dom.next(item, liFilter), liFilter); 35 nItem = dom.next(dom.next(item, liFilter), liFilter);
27 switch (e.target.classList[0]) { 36 switch (e.target.classList[0]) {
28 case 'gril-sort-up': 37 case 'gril-sort-up':
29 //TODO: backend 38 try {
39 await updateGrilOrder(item.id, -1);
40 } catch(e) {
41 alert(e.message);
42 }
30 item.parentNode.insertBefore(item, pItem); 43 item.parentNode.insertBefore(item, pItem);
31 break; 44 break;
32 case 'gril-sort-down': 45 case 'gril-sort-down':
33 //TODO: backend 46 try {
47 await updateGrilOrder(item.id, 1);
48 } catch(e) {
49 alert(e.message);
50 }
51
34 if (nItem) { 52 if (nItem) {
35 item.parentNode.insertBefore(item, nItem); 53 item.parentNode.insertBefore(item, nItem);
36 } else { 54 } else {
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index ec29ea8..b40ee18 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -67,6 +67,7 @@ func (m *Module) Init(g *grilist.Grilist) {
67 } 67 }
68 m.grils = grilsModule 68 m.grils = grilsModule
69 m.g.Router.GET("/list/:id", m.viewList) 69 m.g.Router.GET("/list/:id", m.viewList)
70 m.g.Router.POST("/list/:id/order", m.updateGrilOrder)
70 m.g.Router.POST("/list/:id", m.addGrilToList) 71 m.g.Router.POST("/list/:id", m.addGrilToList)
71 m.g.Router.GET("/new/list", m.displayCreateList) 72 m.g.Router.GET("/new/list", m.displayCreateList)
72 m.g.Router.POST("/new/list", m.createList) 73 m.g.Router.POST("/new/list", m.createList)
@@ -341,6 +342,85 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter
341 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) 342 http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302)
342} 343}
343 344
345func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
346 slistID := p.ByName("id")
347
348 user, err := m.g.Charakterin.GetUserFromRequest(r)
349 if err != nil {
350 http.Error(w, "403", http.StatusForbidden)
351 return
352 }
353
354 listID, err := strconv.Atoi(slistID)
355 if err != nil {
356 http.Error(w, "invalid list ID (type mismatch)", 400)
357 return
358 }
359
360 values, err := readBody(r)
361 if err != nil {
362 http.Error(w, "invalid POST data", 400)
363 return
364 }
365
366 grilID, err := strconv.Atoi(values.Get("gril"))
367 if err != nil {
368 http.Error(w, "invalid gril ID", 404)
369 return
370 }
371
372 diff, err := strconv.Atoi(values.Get("pos"))
373 if err != nil {
374 http.Error(w, "invalid position diff", 400)
375 return
376 }
377
378 list, err := m.FromID(listID)
379 if err != nil {
380 http.Error(w, "invalid list", 404)
381 return
382 }
383
384 // find the gril in our list
385 var lgril *ListGril = nil
386 log.Println(list.Grils)
387 for _, g := range list.Grils {
388 if g.Gril.ID == grilID {
389 lgril = g
390 break
391 }
392 }
393 if lgril == nil {
394 http.Error(w, "gril not found in the list", 404)
395 return
396 }
397
398 newPos := lgril.Order + diff
399
400 if newPos < 0 {
401 newPos = 0
402 } else if newPos >= len(list.Grils) {
403 newPos = len(list.Grils) - 1
404 }
405
406 if newPos == lgril.Order {
407 http.Error(w, "no change in order", 400)
408 return
409 }
410
411 // rein in die DB damit
412 _, err = m.g.DB.Query(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, newPos)
413 if err != nil {
414 log.Println("error reordering gril:", err)
415 http.Error(w, "could not update gril order", 500)
416 return
417 }
418
419 w.WriteHeader(200)
420 w.Write([]byte("ok"))
421 return
422}
423
344// New erstellt eine neue Instanz des Modules 424// New erstellt eine neue Instanz des Modules
345func New() *Module { 425func New() *Module {
346 return &Module{} 426 return &Module{}
diff --git a/views/includes/list_gril.html b/views/includes/list_gril.html
index 57694aa..4b5e55c 100644
--- a/views/includes/list_gril.html
+++ b/views/includes/list_gril.html
@@ -1,5 +1,5 @@
1{{ define "list_gril" }} 1{{ define "list_gril" }}
2<li id="{{ .Index }}"> 2<li id="{{ .Value.Gril.Gril.ID }}">
3 <div class="card-panel hoverable list-entry"> 3 <div class="card-panel hoverable list-entry">
4 <div class="row valign-wrapper"> 4 <div class="row valign-wrapper">
5 <div class="col s3 m2 {{ if not .Value.IsListOwner }}offset-s1 offset-m1{{ end }}"> 5 <div class="col s3 m2 {{ if not .Value.IsListOwner }}offset-s1 offset-m1{{ end }}">