Browse Source

Modules ordering, version--, cp module wrap

Vova Tkach 6 years ago
parent
commit
2bd25ee6e5
5 changed files with 86 additions and 68 deletions
  1. 1 1
      Makefile
  2. 1 1
      consts/consts.go
  3. 3 42
      modules/module_index.go
  4. 28 9
      modules/module_some.go
  5. 53 15
      modules/modules.go

+ 1 - 1
Makefile

@@ -1,4 +1,4 @@
-VERSION="1.0.1"
+VERSION="1.0.0"
 
 default: debug
 

+ 1 - 1
consts/consts.go

@@ -5,7 +5,7 @@ import (
 )
 
 const Debug = true
-const ServerVersion = "1.0.1"
+const ServerVersion = "1.0.0"
 const AssetsVersion = "1"
 const AssetsPath = "assets"
 const DirIndexFile = "index.html"

+ 3 - 42
modules/module_index.go

@@ -5,11 +5,9 @@ import (
 	_ "github.com/go-sql-driver/mysql"
 
 	"fmt"
-	"html/template"
 	"os"
 	"strconv"
 
-	"golang-fave/assets"
 	"golang-fave/consts"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
@@ -20,6 +18,7 @@ func (this *Modules) RegisterModule_Index() *Module {
 		WantDB: true,
 		Mount:  "index",
 		Name:   "Pages",
+		Order:  0,
 	}, func(wrap *wrapper.Wrapper) {
 		// Front-end
 		wrap.RenderFrontEnd("index", consts.TmplDataModIndex{
@@ -34,47 +33,9 @@ func (this *Modules) RegisterModule_Index() *Module {
 				{Name: "Item 3", Link: "/#3", Active: false},
 			},
 		})
-	}, func(wrap *wrapper.Wrapper) {
+	}, func(wrap *wrapper.Wrapper) (string, string, string) {
 		// Back-end
-
-		//page_sb_left := mdl.GetSidebarLeft()
-		//nav_bar_modules_all := mdl.GetNavMenuModules()
-		//nav_bar_modules_sys := mdl.GetNavMenuModulesSys()
-		// If right sidebar and content need to show
-		/*
-			if page_sb_left != "" {
-				body_class = body_class + " cp-sidebar-left"
-			}
-			if page_content == "" {
-				body_class = body_class + " cp-404"
-				page_content = "Panel 404"
-			}
-			if page_sb_right != "" {
-				body_class = body_class + " cp-sidebar-right"
-			}
-		*/
-
-		body_class := "cp"
-		page_sb_left := "Left"
-		page_content := "Content"
-		page_sb_right := "Right"
-
-		wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
-			Title:              "Fave " + consts.ServerVersion,
-			BodyClasses:        body_class,
-			UserId:             wrap.User.A_id,
-			UserFirstName:      wrap.User.A_first_name,
-			UserLastName:       wrap.User.A_last_name,
-			UserEmail:          wrap.User.A_email,
-			UserPassword:       "",
-			UserAvatarLink:     "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
-			NavBarModules:      template.HTML(this.getNavMenuModules(wrap)),
-			NavBarModulesSys:   template.HTML(this.getNavMenuModulesSys(wrap)),
-			ModuleCurrentAlias: "index",
-			SidebarLeft:        template.HTML(page_sb_left),
-			Content:            template.HTML(page_content),
-			SidebarRight:       template.HTML(page_sb_right),
-		})
+		return "1", "Index", "Index Sidebar"
 	})
 }
 

+ 28 - 9
modules/module_some.go

@@ -1,9 +1,6 @@
 package modules
 
 import (
-	"fmt"
-	"net/http"
-
 	"golang-fave/engine/wrapper"
 )
 
@@ -12,12 +9,34 @@ func (this *Modules) RegisterModule_Some() *Module {
 		WantDB: true,
 		Mount:  "some",
 		Name:   "Some Module",
-	}, nil, func(wrap *wrapper.Wrapper) {
+		Order:  1,
+	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
+		// Back-end
+		return "2", "Some", "Some Sidebar"
+	})
+}
+
+func (this *Modules) RegisterModule_More() *Module {
+	return this.newModule(MInfo{
+		WantDB: true,
+		Mount:  "more",
+		Name:   "More Module",
+		Order:  2,
+	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
+		// Back-end
+		return "3", "More", "More Sidebar"
+	})
+}
+
+func (this *Modules) RegisterModule_System() *Module {
+	return this.newModule(MInfo{
+		WantDB: true,
+		Mount:  "system",
+		Name:   "System Module",
+		Order:  800,
+		System: true,
+	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
 		// Back-end
-		fmt.Printf("SOME BackEnd func call\n")
-		wrap.W.WriteHeader(http.StatusOK)
-		wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-		wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
-		wrap.W.Write([]byte(`SOME BackEnd func call (` + wrap.CurrModule + `)`))
+		return "4", "System", "System Sidebar"
 	})
 }

+ 53 - 15
modules/modules.go

@@ -1,10 +1,14 @@
 package modules
 
 import (
+	"html/template"
 	"net/http"
 	"reflect"
+	"sort"
 	"strings"
 
+	"golang-fave/assets"
+	"golang-fave/consts"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -14,12 +18,14 @@ type MInfo struct {
 	WantDB bool
 	Mount  string
 	Name   string
+	Order  int
+	System bool
 }
 
 type Module struct {
 	Info  MInfo
 	Front func(wrap *wrapper.Wrapper)
-	Back  func(wrap *wrapper.Wrapper)
+	Back  func(wrap *wrapper.Wrapper) (string, string, string)
 }
 
 type AInfo struct {
@@ -71,7 +77,7 @@ func (this *Modules) load() {
 	}
 }
 
-func (this *Modules) newModule(info MInfo, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper)) *Module {
+func (this *Modules) newModule(info MInfo, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper) (string, string, string)) *Module {
 	return &Module{Info: info, Front: ff, Back: bf}
 }
 
@@ -104,24 +110,26 @@ func (this *Modules) getCurrentModule(wrap *wrapper.Wrapper, backend bool) (*Mod
 	return mod, modCurr
 }
 
-// TODO: add module ordering
-func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper) string {
-	// TODO: generate list, reorder, build html
-	html := ""
+func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
+	list := make([]*MInfo, 0)
 	for _, mod := range this.mods {
 		if mod.Back != nil {
-			class := ""
-			if mod.Info.Mount == wrap.CurrModule {
-				class = " active"
+			if mod.Info.System == sys {
+				list = append(list, &mod.Info)
 			}
-			html += `<a class="dropdown-item` + class + `" href="/cp/` + mod.Info.Mount + `/">` + mod.Info.Name + `</a>`
 		}
 	}
-	return html
-}
-
-func (this *Modules) getNavMenuModulesSys(wrap *wrapper.Wrapper) string {
+	sort.Slice(list, func(i, j int) bool {
+		return list[i].Order < list[j].Order
+	})
 	html := ""
+	for _, mod := range list {
+		class := ""
+		if mod.Mount == wrap.CurrModule {
+			class = " active"
+		}
+		html += `<a class="dropdown-item` + class + `" href="/cp/` + mod.Mount + `/">` + mod.Name + `</a>`
+	}
 	return html
 }
 
@@ -194,7 +202,37 @@ func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
 	if mod != nil {
 		wrap.CurrModule = cm
 		if mod.Back != nil {
-			mod.Back(wrap)
+			sidebar_left, content, sidebar_right := mod.Back(wrap)
+
+			body_class := "cp"
+			if sidebar_left != "" {
+				body_class = body_class + " cp-sidebar-left"
+			}
+			if content == "" {
+				body_class = body_class + " cp-404"
+				content = "Panel 404"
+			}
+			if sidebar_right != "" {
+				body_class = body_class + " cp-sidebar-right"
+			}
+
+			wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
+				Title:              "Fave " + consts.ServerVersion,
+				BodyClasses:        body_class,
+				UserId:             wrap.User.A_id,
+				UserFirstName:      wrap.User.A_first_name,
+				UserLastName:       wrap.User.A_last_name,
+				UserEmail:          wrap.User.A_email,
+				UserPassword:       "",
+				UserAvatarLink:     "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
+				NavBarModules:      template.HTML(this.getNavMenuModules(wrap, false)),
+				NavBarModulesSys:   template.HTML(this.getNavMenuModules(wrap, true)),
+				ModuleCurrentAlias: wrap.CurrModule,
+				SidebarLeft:        template.HTML(sidebar_left),
+				Content:            template.HTML(content),
+				SidebarRight:       template.HTML(sidebar_right),
+			})
+
 			return true
 		}
 	}