From 3e34dfa6ebbc0d70c4e704fd7f57dca25ecb8c63 Mon Sep 17 00:00:00 2001 From: Jan C Date: Sat, 20 Feb 2016 16:29:33 +0100 Subject: =?UTF-8?q?m=C3=B6glichkeit=20grils=20direkt=20=C3=BCber=20die=20g?= =?UTF-8?q?ril=20view=20zu=20einer=20liste=20hinzuzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/assets_src/js/gril.js b/assets_src/js/gril.js new file mode 100644 index 0000000..561c221 --- /dev/null +++ b/assets_src/js/gril.js @@ -0,0 +1,47 @@ +import 'babel-polyfill'; +import * as dom from './lib/dom'; +import * as ajax from './lib/ajax'; + +dom.ready(() => { + $('.modal-trigger').leanModal({ + async ready() { + const el = document.getElementById('modal-add-gril-content'); + + el.innerHTML = ` +
+
+
`; + + let data = {}; + try { + data = JSON.parse(await ajax.get('/api/lists/user')); + } catch(e) { + console.error(e); + } + + el.innerHTML = ''; + + data.forEach(d => { + const element = document.createElement('a'); + element.innerHTML = d.Name; + element.className = 'collection-item purple-text text-lighten-2'; + element.addEventListener('click', async () => { + try { + await ajax.post( + `/list/${d.ID}`, + 'id=' + window.location.href.match(/\/gril\/([0-9]{1,})\//)[1], + { headers: { + 'Content-type': 'application/x-www-form-urlencoded' + } }); + $('#modal_add_gril').closeModal(); + } catch(e) { + console.error(e); + } + + return true; + }); + el.appendChild(element); + }); + }, + }); +}); \ No newline at end of file diff --git a/modules/lists/lists.go b/modules/lists/lists.go index e1b806d..a42c51c 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -2,6 +2,7 @@ package lists import ( "database/sql" + "encoding/json" "errors" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" @@ -72,6 +73,8 @@ func (m *Module) Init(g *grilist.Grilist) { m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) m.g.Router.GET("/new/list", m.displayCreateList) m.g.Router.POST("/new/list", m.createList) + + m.g.Router.GET("/api/lists/user", m.APIgetUserLists) } func (m *Module) getListGrils(list *List) error { @@ -120,11 +123,6 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e continue } - if err := m.getListGrils(list); err != nil { - log.Println(err) - continue - } - // Owner kriegn owner, err := m.g.Charakterin.GetUserByID(ownerID) if err != nil { @@ -140,20 +138,36 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e } // GetUserLists gibt die Listen eines Benutzers zurück. -func (m *Module) GetUserLists(u *charakterin.User) []*List { +func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List { lists, err := m.getLists(`user_id = $1`, u.ID) if err != nil { log.Println(err) } + + if withGrils { + for _, list := range lists { + if err := m.getListGrils(list); err != nil { + log.Println(err) + } + } + } return lists } // FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. -func (m *Module) FromID(id int) (*List, error) { +func (m *Module) FromID(id int, withGrils bool) (*List, error) { lists, err := m.getLists(`id = $1`, id) if err != nil { return nil, err } + + if withGrils { + for _, list := range lists { + if err := m.getListGrils(list); err != nil { + return nil, err + } + } + } if len(lists) == 0 { return nil, errors.New("no list found") @@ -199,6 +213,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar log.Println(err) return categories } + for _, list := range lists { + if err := m.getListGrils(list); err != nil { + log.Println(err) + } + } categories = append(categories, grilist.DashboardCategory{ Title: "Neueste Listen", @@ -210,11 +229,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar } // Listen des Benutzers - lists, err = m.getLists(`user_id = $1`, user.ID) - if err != nil { - log.Println(err) - return categories - } + lists = m.GetUserLists(user, true) categories = append(categories, grilist.DashboardCategory{ Title: "Meine Listen", @@ -234,7 +249,7 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P return } - list, err := m.FromID(id) + list, err := m.FromID(id, true) if err != nil { http.Redirect(w, r, "/", 302) return @@ -274,7 +289,7 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou return } - list, err := m.FromID(listID) + list, err := m.FromID(listID, true) if err != nil { http.Error(w, "invalid list", 404) return @@ -439,6 +454,30 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht return } +func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + user, err := m.g.Charakterin.GetUserFromRequest(r) + if err != nil { + http.Error(w, "403", http.StatusForbidden) + return + } + + lists := m.GetUserLists(user, false) + + for _, list := range lists { + // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16) + list.Owner = nil + } + + data, err := json.Marshal(&lists) + if err != nil { + log.Println(err) + http.Error(w, "500", http.StatusInternalServerError) + return + } + w.WriteHeader(200) + w.Write(data) +} + // New erstellt eine neue Instanz des Modules func New() *Module { return &Module{} diff --git a/modules/user/user.go b/modules/user/user.go index da58382..2bf862b 100644 --- a/modules/user/user.go +++ b/modules/user/user.go @@ -60,7 +60,7 @@ func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.P } } - userLists := m.lists.GetUserLists(user) + userLists := m.lists.GetUserLists(user, true) data := m.g.Renderer.DefaultData() data["user"] = currentUser diff --git a/views/includes/materialize.html b/views/includes/materialize.html index 7c40c0d..9d5d210 100644 --- a/views/includes/materialize.html +++ b/views/includes/materialize.html @@ -1,6 +1,5 @@ {{define "materialize"}} - {{end}} \ No newline at end of file diff --git a/views/includes/materialize_js.html b/views/includes/materialize_js.html new file mode 100644 index 0000000..be6dda6 --- /dev/null +++ b/views/includes/materialize_js.html @@ -0,0 +1,4 @@ +{{ define "materialize_js" }} + + +{{ end }} \ No newline at end of file diff --git a/views/pages/gril.html b/views/pages/gril.html index 776f14f..cd92208 100644 --- a/views/pages/gril.html +++ b/views/pages/gril.html @@ -19,6 +19,10 @@ {{ end }}
+ {{ if .user }} +

Zu Liste hinzufügen + {{ end }} +
{{ range $tag := $gril.Tags }}
- {{ if $gril.Birthday.Valid }} -
-
-
- {{ $gril.Birthday.Value}} -

Geburtstag

-
-
-
- {{ else if $gril.Age.Valid }} -
-
-
- {{ $gril.Age.Value }} -

Jahre alt

-
-
-
- {{ end }} - {{ if $gril.Lists }} -
-
-
-

in

- {{ len $gril.Lists}} -

Liste{{ if ne (len $gril.Lists) 1 }}n{{ end }}

-
-
-
- {{ end }} -
-
-
- {{ $gril.UpdatedAt.Format "Jan 02, 2006" }} -

aktualisiert

-
-
-
+ {{ if $gril.Birthday.Valid }} +
+
+
+ {{ $gril.Birthday.Value}} +

Geburtstag

+
+
+
+ {{ else if $gril.Age.Valid }} +
+
+
+ {{ $gril.Age.Value }} +

Jahre alt

+
+
+
+ {{ end }} + {{ if $gril.Lists }} +
+
+
+

in

+ {{ len $gril.Lists}} +

Liste{{ if ne (len $gril.Lists) 1 }}n{{ end }}

+
+
+
+ {{ end }} +
+
+
+ {{ $gril.UpdatedAt.Format "Jan 02, 2006" }} +

aktualisiert

+
+
+
+ + + {{ if .user }} + + + + {{ template "materialize_js" }} + + {{ end }} {{ end }} -- cgit v0.10.1