aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan C <jan@ruken.pw>2016-02-20 15:29:33 (UTC)
committerJan C <jan@ruken.pw>2016-02-20 15:29:33 (UTC)
commit3e34dfa6ebbc0d70c4e704fd7f57dca25ecb8c63 (patch)
tree351d02b87d5f6c285b537a1e178b1528f6e5d3f4
parent0d76691705dbbaa8b3972e09ed8df1e590b38da5 (diff)
möglichkeit grils direkt über die gril view zu einer liste hinzuzufügen
-rw-r--r--assets_src/js/gril.js47
-rw-r--r--modules/lists/lists.go67
-rw-r--r--modules/user/user.go2
-rw-r--r--views/includes/materialize.html1
-rw-r--r--views/includes/materialize_js.html4
-rw-r--r--views/pages/gril.html94
6 files changed, 161 insertions, 54 deletions
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 @@
1import 'babel-polyfill';
2import * as dom from './lib/dom';
3import * as ajax from './lib/ajax';
4
5dom.ready(() => {
6 $('.modal-trigger').leanModal({
7 async ready() {
8 const el = document.getElementById('modal-add-gril-content');
9
10 el.innerHTML = `
11 <div class="progress">
12 <div class="indeterminate"></div>
13 </div>`;
14
15 let data = {};
16 try {
17 data = JSON.parse(await ajax.get('/api/lists/user'));
18 } catch(e) {
19 console.error(e);
20 }
21
22 el.innerHTML = '';
23
24 data.forEach(d => {
25 const element = document.createElement('a');
26 element.innerHTML = d.Name;
27 element.className = 'collection-item purple-text text-lighten-2';
28 element.addEventListener('click', async () => {
29 try {
30 await ajax.post(
31 `/list/${d.ID}`,
32 'id=' + window.location.href.match(/\/gril\/([0-9]{1,})\//)[1],
33 { headers: {
34 'Content-type': 'application/x-www-form-urlencoded'
35 } });
36 $('#modal_add_gril').closeModal();
37 } catch(e) {
38 console.error(e);
39 }
40
41 return true;
42 });
43 el.appendChild(element);
44 });
45 },
46 });
47}); \ 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
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "encoding/json"
5 "errors" 6 "errors"
6 "fagott.pw/charakterin" 7 "fagott.pw/charakterin"
7 "fagott.pw/grilist/frontend" 8 "fagott.pw/grilist/frontend"
@@ -72,6 +73,8 @@ func (m *Module) Init(g *grilist.Grilist) {
72 m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) 73 m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList)
73 m.g.Router.GET("/new/list", m.displayCreateList) 74 m.g.Router.GET("/new/list", m.displayCreateList)
74 m.g.Router.POST("/new/list", m.createList) 75 m.g.Router.POST("/new/list", m.createList)
76
77 m.g.Router.GET("/api/lists/user", m.APIgetUserLists)
75} 78}
76 79
77func (m *Module) getListGrils(list *List) error { 80func (m *Module) getListGrils(list *List) error {
@@ -120,11 +123,6 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
120 continue 123 continue
121 } 124 }
122 125
123 if err := m.getListGrils(list); err != nil {
124 log.Println(err)
125 continue
126 }
127
128 // Owner kriegn 126 // Owner kriegn
129 owner, err := m.g.Charakterin.GetUserByID(ownerID) 127 owner, err := m.g.Charakterin.GetUserByID(ownerID)
130 if err != nil { 128 if err != nil {
@@ -140,20 +138,36 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e
140} 138}
141 139
142// GetUserLists gibt die Listen eines Benutzers zurück. 140// GetUserLists gibt die Listen eines Benutzers zurück.
143func (m *Module) GetUserLists(u *charakterin.User) []*List { 141func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List {
144 lists, err := m.getLists(`user_id = $1`, u.ID) 142 lists, err := m.getLists(`user_id = $1`, u.ID)
145 if err != nil { 143 if err != nil {
146 log.Println(err) 144 log.Println(err)
147 } 145 }
146
147 if withGrils {
148 for _, list := range lists {
149 if err := m.getListGrils(list); err != nil {
150 log.Println(err)
151 }
152 }
153 }
148 return lists 154 return lists
149} 155}
150 156
151// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. 157// FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück.
152func (m *Module) FromID(id int) (*List, error) { 158func (m *Module) FromID(id int, withGrils bool) (*List, error) {
153 lists, err := m.getLists(`id = $1`, id) 159 lists, err := m.getLists(`id = $1`, id)
154 if err != nil { 160 if err != nil {
155 return nil, err 161 return nil, err
156 } 162 }
163
164 if withGrils {
165 for _, list := range lists {
166 if err := m.getListGrils(list); err != nil {
167 return nil, err
168 }
169 }
170 }
157 171
158 if len(lists) == 0 { 172 if len(lists) == 0 {
159 return nil, errors.New("no list found") 173 return nil, errors.New("no list found")
@@ -199,6 +213,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
199 log.Println(err) 213 log.Println(err)
200 return categories 214 return categories
201 } 215 }
216 for _, list := range lists {
217 if err := m.getListGrils(list); err != nil {
218 log.Println(err)
219 }
220 }
202 221
203 categories = append(categories, grilist.DashboardCategory{ 222 categories = append(categories, grilist.DashboardCategory{
204 Title: "Neueste Listen", 223 Title: "Neueste Listen",
@@ -210,11 +229,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar
210 } 229 }
211 230
212 // Listen des Benutzers 231 // Listen des Benutzers
213 lists, err = m.getLists(`user_id = $1`, user.ID) 232 lists = m.GetUserLists(user, true)
214 if err != nil {
215 log.Println(err)
216 return categories
217 }
218 233
219 categories = append(categories, grilist.DashboardCategory{ 234 categories = append(categories, grilist.DashboardCategory{
220 Title: "Meine Listen", 235 Title: "Meine Listen",
@@ -234,7 +249,7 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P
234 return 249 return
235 } 250 }
236 251
237 list, err := m.FromID(id) 252 list, err := m.FromID(id, true)
238 if err != nil { 253 if err != nil {
239 http.Redirect(w, r, "/", 302) 254 http.Redirect(w, r, "/", 302)
240 return 255 return
@@ -274,7 +289,7 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou
274 return 289 return
275 } 290 }
276 291
277 list, err := m.FromID(listID) 292 list, err := m.FromID(listID, true)
278 if err != nil { 293 if err != nil {
279 http.Error(w, "invalid list", 404) 294 http.Error(w, "invalid list", 404)
280 return 295 return
@@ -439,6 +454,30 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht
439 return 454 return
440} 455}
441 456
457func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
458 user, err := m.g.Charakterin.GetUserFromRequest(r)
459 if err != nil {
460 http.Error(w, "403", http.StatusForbidden)
461 return
462 }
463
464 lists := m.GetUserLists(user, false)
465
466 for _, list := range lists {
467 // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16)
468 list.Owner = nil
469 }
470
471 data, err := json.Marshal(&lists)
472 if err != nil {
473 log.Println(err)
474 http.Error(w, "500", http.StatusInternalServerError)
475 return
476 }
477 w.WriteHeader(200)
478 w.Write(data)
479}
480
442// New erstellt eine neue Instanz des Modules 481// New erstellt eine neue Instanz des Modules
443func New() *Module { 482func New() *Module {
444 return &Module{} 483 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
60 } 60 }
61 } 61 }
62 62
63 userLists := m.lists.GetUserLists(user) 63 userLists := m.lists.GetUserLists(user, true)
64 64
65 data := m.g.Renderer.DefaultData() 65 data := m.g.Renderer.DefaultData()
66 data["user"] = currentUser 66 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 @@
1{{define "materialize"}} 1{{define "materialize"}}
2 <link rel="stylesheet" href="/assets/css/materialize.css"> 2 <link rel="stylesheet" href="/assets/css/materialize.css">
3 <script src="/assets/js/materialize.min.js"></script>
4 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 3 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
5 <meta name="viewport" content="width=device_width, initial-scale=1.0" /> 4 <meta name="viewport" content="width=device_width, initial-scale=1.0" />
6{{end}} \ No newline at end of file 5{{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 @@
1{{ define "materialize_js" }}
2<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
3<script src="/assets/js/materialize.min.js"></script>
4{{ 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 @@
19 {{ end }} 19 {{ end }}
20 <div class="row center-align"> 20 <div class="row center-align">
21 <img src="/{{ $gril.ImagePath false }}" /> 21 <img src="/{{ $gril.ImagePath false }}" />
22 {{ if .user }}
23 <br /><br /><a class="waves-effect waves-light btn-large modal-trigger purple lighten-2" href="#modal_add_gril">Zu Liste hinzufügen</a>
24 {{ end }}
25
22 </div> 26 </div>
23 {{ range $tag := $gril.Tags }} 27 {{ range $tag := $gril.Tags }}
24 <a href="/tag/{{ $tag }}" 28 <a href="/tag/{{ $tag }}"
@@ -32,46 +36,60 @@
32 <br /> 36 <br />
33 <br /> 37 <br />
34 <div class="row"> 38 <div class="row">
35 {{ if $gril.Birthday.Valid }} 39 {{ if $gril.Birthday.Valid }}
36 <div class="col s6 m2 l2"> 40 <div class="col s6 m2 l2">
37 <div class="card"> 41 <div class="card">
38 <div class="card-content purple-text text-lighten-2 center-align"> 42 <div class="card-content purple-text text-lighten-2 center-align">
39 <span class="card-title big">{{ $gril.Birthday.Value}}</span> 43 <span class="card-title big">{{ $gril.Birthday.Value}}</span>
40 <p>Geburtstag</p> 44 <p>Geburtstag</p>
41 </div> 45 </div>
42 </div> 46 </div>
43 </div> 47 </div>
44 {{ else if $gril.Age.Valid }} 48 {{ else if $gril.Age.Valid }}
45 <div class="col s6 m2 l2"> 49 <div class="col s6 m2 l2">
46 <div class="card"> 50 <div class="card">
47 <div class="card-content purple-text text-lighten-2 center-align"> 51 <div class="card-content purple-text text-lighten-2 center-align">
48 <span class="card-title big">{{ $gril.Age.Value }}</span> 52 <span class="card-title big">{{ $gril.Age.Value }}</span>
49 <p>Jahre alt</p> 53 <p>Jahre alt</p>
50 </div> 54 </div>
51 </div> 55 </div>
52 </div> 56 </div>
53 {{ end }} 57 {{ end }}
54 {{ if $gril.Lists }} 58 {{ if $gril.Lists }}
55 <div class="col s6 m2 l2"> 59 <div class="col s6 m2 l2">
56 <div class="card"> 60 <div class="card">
57 <div class="card-content center-big purple-text text-lighten-2 center-align"> 61 <div class="card-content center-big purple-text text-lighten-2 center-align">
58 <p>in</p> 62 <p>in</p>
59 <span class="card-title big">{{ len $gril.Lists}}</span> 63 <span class="card-title big">{{ len $gril.Lists}}</span>
60 <p>Liste{{ if ne (len $gril.Lists) 1 }}n{{ end }}</p> 64 <p>Liste{{ if ne (len $gril.Lists) 1 }}n{{ end }}</p>
61 </div> 65 </div>
62 </div> 66 </div>
63 </div> 67 </div>
64 {{ end }} 68 {{ end }}
65 <div class="col s12 m4 l4"> 69 <div class="col s12 m4 l4">
66 <div class="card"> 70 <div class="card">
67 <div class="card-content purple-text text-lighten-2 center-align"> 71 <div class="card-content purple-text text-lighten-2 center-align">
68 <span class="card-title big">{{ $gril.UpdatedAt.Format "Jan 02, 2006" }}</span> 72 <span class="card-title big">{{ $gril.UpdatedAt.Format "Jan 02, 2006" }}</span>
69 <p>aktualisiert</p> 73 <p>aktualisiert</p>
70 </div> 74 </div>
71 </div> 75 </div>
72 </div> 76 </div>
73 </div> 77 </div>
74 </div> 78 </div>
79
80
81 {{ if .user }}
82 <!-- Modal Structure -->
83 <div id="modal_add_gril" class="modal bottom-sheet">
84 <div class="modal-content">
85 <h4>Zu Liste hinzufügen</h4>
86 <div class="collection" id="modal-add-gril-content"></div>
87 </div>
88 </div>
89
90 {{ template "materialize_js" }}
91 <script src="/assets/js/gril.js"></script>
92 {{ end }}
75 </body> 93 </body>
76</html> 94</html>
77{{ end }} 95{{ end }}