Browse Source

Stable frontend

Moritz Schmidt 9 years ago
parent
commit
56ed0521b1
4 changed files with 63 additions and 48 deletions
  1. 34 41
      main.go
  2. 10 4
      templates/account_box.html
  3. 19 3
      templates/layouts/main.html
  4. 0 0
      templates/partials/notification_box.html

+ 34 - 41
main.go

@@ -2,13 +2,27 @@ package main
 
 
 import (
 import (
   "github.com/kataras/iris"
   "github.com/kataras/iris"
-  "git.mmnx.de/Moe/go-template/html"
+  "github.com/kataras/go-template/html"
   "fmt"
   "fmt"
   "git.mmnx.de/Moe/databaseutils"
   "git.mmnx.de/Moe/databaseutils"
   "git.mmnx.de/Moe/usermanager"
   "git.mmnx.de/Moe/usermanager"
   "git.mmnx.de/Moe/configutils"
   "git.mmnx.de/Moe/configutils"
 )
 )
 
 
+type pageParams struct{
+  HasError string
+  Error string
+  ReqDir string
+  } // {Error: ""}
+
+type pageUserParams struct{
+  HasError string
+  Error string
+  ReqDir string
+  Username string
+  Email string
+  } // {Error: ""}
+
 func main() {
 func main() {
 
 
   conf := configutils.ReadConfig("config.json") // read config
   conf := configutils.ReadConfig("config.json") // read config
@@ -23,6 +37,14 @@ func main() {
   fmt.Print("") // for not needing to remove fmt ...
   fmt.Print("") // for not needing to remove fmt ...
 
 
   iris.Config.IsDevelopment = true
   iris.Config.IsDevelopment = true
+  //iris.Config.Render.Template.Gzip = true
+
+  /** HELPER FUNCTION EXAMPLE **/
+    /*config := html.DefaultConfig()
+    config.Layout = "layouts/main.html"
+    config.Helpers["boldme"] = func(input string) raymond.SafeString {
+      return raymond.SafeString("<b> " + input + "</b>")
+    }*/
 
 
   /** ROUTING **/
   /** ROUTING **/
 
 
@@ -30,53 +52,25 @@ func main() {
   iris.Static("/css", "./static/css", 1)
   iris.Static("/css", "./static/css", 1)
   iris.Static("/img", "./static/img", 1)
   iris.Static("/img", "./static/img", 1)
 
 
-  //iris.Get("/test", usermanager.AuthHandler, testHandler)
-
   iris.Post("/login", loginHandler) // login form handler
   iris.Post("/login", loginHandler) // login form handler
 
 
-
-
-  iris.UseTemplate(html.New(html.Config{
-    Layout: "layouts/main.html",
-  }))
-
-  /*config := html.DefaultConfig()
-  config.Layout = "layouts/main.html"
-  config.Helpers["boldme"] = func(input string) raymond.SafeString {
-    return raymond.SafeString("<b> " + input + "</b>")
-  }
-
-    // set the template engine
-    iris.UseTemplate(handlebars.New(config)).Directory("./templates", ".html")*/
-
-	//iris.Config.Render.Template.Gzip = true
-
 	iris.Get("/", usermanager.AuthHandler, func(ctx *iris.Context) {
 	iris.Get("/", usermanager.AuthHandler, func(ctx *iris.Context) {
-    if err := ctx.Render("notification_box.html", nil); err != nil {
-			println(err.Error())
-		}
-
-		if err := ctx.Render("home_boxes.html", nil); err != nil {
+    params := pageParams{"0", "", ""}
+		if err := ctx.Render("home_boxes.html", params); err != nil {
 			println(err.Error())
 			println(err.Error())
 		}
 		}
 	})
 	})
 
 
   iris.Get("/account", usermanager.AuthHandler, func(ctx *iris.Context) {
   iris.Get("/account", usermanager.AuthHandler, func(ctx *iris.Context) {
-    if err := ctx.Render("notification_box.html", nil); err != nil {
-			println(err.Error())
-		}
-
-		if err := ctx.Render("account_box.html", nil); err != nil {
+    params := pageUserParams{"0", "", "account", "moe", "moritz@mmnx.de"}
+		if err := ctx.Render("account_box.html", params); err != nil {
 			println(err.Error())
 			println(err.Error())
 		}
 		}
 	})
 	})
 
 
   iris.Get("/login", func(ctx *iris.Context) {
   iris.Get("/login", func(ctx *iris.Context) {
-    if err := ctx.Render("notification_box.html", struct{ Error string }{Error: ""}); err != nil { // TODO: do this only when there is a notification
-			println(err.Error())
-		}
-
-    if err := ctx.Render("login.html", struct{ Error string }{Error: ""}); err != nil { // no error for normal login screen
+    params := pageParams{"0", "", "login"}
+    if err := ctx.Render("login.html", params); err != nil { // no error for normal login screen, struct{ Error string }{Error: ""}
       println(err.Error())
       println(err.Error())
     }
     }
   })
   })
@@ -90,10 +84,9 @@ func main() {
 		}
 		}
 	})
 	})
 
 
-
-
-
-
+  iris.UseTemplate(html.New(html.Config{
+    Layout: "layouts/main.html",
+  }))
 
 
   /** OTHER **/
   /** OTHER **/
 
 
@@ -108,11 +101,11 @@ func loginHandler(ctx *iris.Context) {
   tokenString, err := user.Login(username, password) // try to login
   tokenString, err := user.Login(username, password) // try to login
 
 
   if err != nil { // TODO: template compatible error handling
   if err != nil { // TODO: template compatible error handling
-    ctx.Render("login.html", struct{ Error string }{Error: err.Error()})
+    ctx.Render("login.html", pageParams{"1", err.Error(), "login"})
   } else {
   } else {
     ctx.SetCookieKV("token", tokenString)
     ctx.SetCookieKV("token", tokenString)
     ctx.Redirect("/")
     ctx.Redirect("/")
-    //ctx.Render("home.html", nil)
+    //ctx.Render("home.html", nil) // TODO: error-alternative success (main.html)
   }
   }
 }
 }
 
 

+ 10 - 4
templates/account_box.html

@@ -1,15 +1,21 @@
 <!--<div class="container">-->
 <!--<div class="container">-->
   <form>
   <form>
     <div class="form-group row">
     <div class="form-group row">
-      <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
+      <label for="inputUsername3" class="col-sm-2 col-form-label">username</label>
       <div class="col-sm-10">
       <div class="col-sm-10">
-        <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
+        <input type="text" class="form-control" id="inputUsername3" placeholder="{{ .Username}}">
       </div>
       </div>
     </div>
     </div>
     <div class="form-group row">
     <div class="form-group row">
-      <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
+      <label for="inputEmail3" class="col-sm-2 col-form-label">email</label>
       <div class="col-sm-10">
       <div class="col-sm-10">
-        <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
+        <input type="email" class="form-control" id="inputEmail3" placeholder="{{ .Email}}">
+      </div>
+    </div>
+    <div class="form-group row">
+      <label for="inputPassword3" class="col-sm-2 col-form-label">passwort</label>
+      <div class="col-sm-10">
+        <input type="password" class="form-control" id="inputPassword3" placeholder="passwort (leer lassen um nicht zu ändern)">
       </div>
       </div>
     </div>
     </div>
     <fieldset class="form-group row">
     <fieldset class="form-group row">

+ 19 - 3
templates/layouts/main.html

@@ -24,19 +24,35 @@
         <nav>
         <nav>
           <ul class="nav nav-pills float-xs-right">
           <ul class="nav nav-pills float-xs-right">
             <li class="nav-item">
             <li class="nav-item">
-              <a class="nav-link active" href="#">home <span class="sr-only">(current)</span></a>
+              {{ if eq .ReqDir ""}}
+              <a class="nav-link active" href="/">home <span class="sr-only">(current)</span></a>
+              {{ else }}
+              <a class="nav-link" href="/">home</a>
+              {{ end }}
             </li>
             </li>
             <li class="nav-item">
             <li class="nav-item">
-              <a class="nav-link" href="#">account</a>
+              {{ if eq .ReqDir "account"}}
+              <a class="nav-link active" href="/account">account <span class="sr-only">(current)</span></a>
+              {{ else }}
+              <a class="nav-link" href="/account">account</a>
+              {{ end }}
             </li>
             </li>
             <li class="nav-item">
             <li class="nav-item">
-              <a class="nav-link" href="#">help</a>
+              {{ if eq .ReqDir "help"}}
+              <a class="nav-link active" href="/help">help <span class="sr-only">(current)</span></a>
+              {{ else }}
+              <a class="nav-link" href="/help">help</a>
+              {{ end }}
             </li>
             </li>
           </ul>
           </ul>
         </nav>
         </nav>
         <h3 class="text-muted">mmnx<sub>vpn</sub></h3>
         <h3 class="text-muted">mmnx<sub>vpn</sub></h3>
       </div>
       </div>
 
 
+      {{ if eq .HasError "1" }}
+        {{ render "partials/notification_box.html" }}
+      {{ end }}
+
       {{ yield }}
       {{ yield }}
 
 
       <footer class="footer">
       <footer class="footer">

+ 0 - 0
templates/notification_box.html → templates/partials/notification_box.html