Browse Source

Modules, cleanup, move some code from backup

Vova Tkach 6 years ago
parent
commit
2ec87cd6b6

+ 5 - 0
consts/consts.go

@@ -25,6 +25,11 @@ const AssetsSysLogoPng = AssetsPath + "/sys/logo.png"
 const AssetsSysLogoSvg = AssetsPath + "/sys/logo.svg"
 const AssetsSysStylesCss = AssetsPath + "/sys/styles.css"
 
+type BreadCrumb struct {
+	Name string
+	Link string
+}
+
 // Template data
 type TmplSystem struct {
 	PathIcoFav       string

+ 1 - 1
engine/html/html.go → engine/builder/builder.go

@@ -1,4 +1,4 @@
-package html
+package builder
 
 // TODO: Return table builder
 // TODO: Return forms builder

+ 15 - 13
engine/wrapper/wrapper.go

@@ -37,6 +37,7 @@ type Wrapper struct {
 	ConfMysqlExists bool
 	UrlArgs         []string
 	CurrModule      string
+	CurrSubModule   string
 
 	DB   *sql.DB
 	User *utils.MySql_user
@@ -44,19 +45,20 @@ type Wrapper struct {
 
 func New(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) *Wrapper {
 	return &Wrapper{
-		l:          l,
-		W:          w,
-		R:          r,
-		S:          s,
-		Host:       host,
-		Port:       port,
-		DConfig:    dirConfig,
-		DHtdocs:    dirHtdocs,
-		DLogs:      dirLogs,
-		DTemplate:  dirTemplate,
-		DTmp:       dirTmp,
-		UrlArgs:    []string{},
-		CurrModule: "",
+		l:             l,
+		W:             w,
+		R:             r,
+		S:             s,
+		Host:          host,
+		Port:          port,
+		DConfig:       dirConfig,
+		DHtdocs:       dirHtdocs,
+		DLogs:         dirLogs,
+		DTemplate:     dirTemplate,
+		DTmp:          dirTmp,
+		UrlArgs:       []string{},
+		CurrModule:    "",
+		CurrSubModule: "",
 	}
 }
 

+ 14 - 4
modules/module_index.go

@@ -22,8 +22,8 @@ func (this *Modules) RegisterModule_Index() *Module {
 		Order:  0,
 		Icon:   assets.SysSvgIconPage,
 		Sub: &[]MISub{
-			{Mount: "default", Name: "List of pages", Icon: assets.SysSvgIconList},
-			{Mount: "add", Name: "Add new page", Icon: assets.SysSvgIconPlus},
+			{Mount: "default", Name: "List of Pages", Icon: assets.SysSvgIconList},
+			{Mount: "add", Name: "Add New Page", Icon: assets.SysSvgIconPlus},
 		},
 	}, func(wrap *wrapper.Wrapper) {
 		// Front-end
@@ -40,8 +40,18 @@ func (this *Modules) RegisterModule_Index() *Module {
 			},
 		})
 	}, func(wrap *wrapper.Wrapper) (string, string, string) {
-		// Back-end
-		return this.getSidebarModules(wrap), "Index", "Index Sidebar"
+		content := ""
+		sidebar := ""
+		if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
+			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
+				{Name: "List of Pages"},
+			})
+		} else if wrap.CurrSubModule == "add" {
+			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
+				{Name: "Add New Page"},
+			})
+		}
+		return this.getSidebarModules(wrap), content, sidebar
 	})
 }
 

+ 30 - 0
modules/module_settings.go

@@ -0,0 +1,30 @@
+package modules
+
+import (
+	"golang-fave/assets"
+	"golang-fave/consts"
+	"golang-fave/engine/wrapper"
+)
+
+func (this *Modules) RegisterModule_Settings() *Module {
+	return this.newModule(MInfo{
+		WantDB: true,
+		Mount:  "settings",
+		Name:   "Settings",
+		Order:  801,
+		System: true,
+		Icon:   assets.SysSvgIconGear,
+		Sub: &[]MISub{
+			{Mount: "default", Name: "Main Settings", Icon: assets.SysSvgIconList},
+		},
+	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
+		content := ""
+		sidebar := ""
+		if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
+			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
+				{Name: "Main Settings"},
+			})
+		}
+		return this.getSidebarModules(wrap), content, sidebar
+	})
+}

+ 0 - 42
modules/module_some.go

@@ -1,42 +0,0 @@
-package modules
-
-import (
-	"golang-fave/engine/wrapper"
-)
-
-func (this *Modules) RegisterModule_Some() *Module {
-	return this.newModule(MInfo{
-		WantDB: true,
-		Mount:  "some",
-		Name:   "Some Module",
-		Order:  1,
-	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
-		// Back-end
-		return this.getSidebarModules(wrap), "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 this.getSidebarModules(wrap), "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
-		return this.getSidebarModules(wrap), "System", "System Sidebar"
-	})
-}

+ 35 - 0
modules/module_users.go

@@ -0,0 +1,35 @@
+package modules
+
+import (
+	"golang-fave/assets"
+	"golang-fave/consts"
+	"golang-fave/engine/wrapper"
+)
+
+func (this *Modules) RegisterModule_Users() *Module {
+	return this.newModule(MInfo{
+		WantDB: true,
+		Mount:  "users",
+		Name:   "Users",
+		Order:  800,
+		System: true,
+		Icon:   assets.SysSvgIconUser,
+		Sub: &[]MISub{
+			{Mount: "default", Name: "List of Users", Icon: assets.SysSvgIconList},
+			{Mount: "add", Name: "Add New User", Icon: assets.SysSvgIconPlus},
+		},
+	}, nil, func(wrap *wrapper.Wrapper) (string, string, string) {
+		content := ""
+		sidebar := ""
+		if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
+			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
+				{Name: "List of Users"},
+			})
+		} else if wrap.CurrSubModule == "add" {
+			content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
+				{Name: "Add New User"},
+			})
+		}
+		return this.getSidebarModules(wrap), content, sidebar
+	})
+}

+ 22 - 0
modules/modules.go

@@ -1,6 +1,7 @@
 package modules
 
 import (
+	"html"
 	"html/template"
 	"net/http"
 	"reflect"
@@ -148,6 +149,8 @@ func (this *Modules) getSidebarModuleSubMenu(wrap *wrapper.Wrapper, mod *MInfo)
 			href := "/cp/" + mod.Mount + "/" + item.Mount + "/"
 			if mod.Mount == "index" && item.Mount == "default" {
 				href = "/cp/"
+			} else if item.Mount == "default" {
+				href = "/cp/" + mod.Mount + "/"
 			}
 			html += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + item.Name + `</a></li>`
 		}
@@ -208,6 +211,22 @@ func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
 	return html_def + html_sys
 }
 
+func (this *Modules) getBreadCrumbs(wrap *wrapper.Wrapper, data *[]consts.BreadCrumb) string {
+	res := `<nav aria-label="breadcrumb">`
+	res += `<ol class="breadcrumb">`
+	res += `<li class="breadcrumb-item"><a href="/cp/` + this.mods[wrap.CurrModule].Info.Mount + `/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
+	for _, item := range *data {
+		if item.Link == "" {
+			res += `<li class="breadcrumb-item active" aria-current="page">` + html.EscapeString(item.Name) + `</li>`
+		} else {
+			res += `<li class="breadcrumb-item"><a href="` + item.Link + `">` + html.EscapeString(item.Name) + `</a></li>`
+		}
+	}
+	res += `</ol>`
+	res += `</nav>`
+	return res
+}
+
 func New() *Modules {
 	m := Modules{
 		mods: map[string]*Module{},
@@ -276,6 +295,9 @@ func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
 	mod, cm := this.getCurrentModule(wrap, true)
 	if mod != nil {
 		wrap.CurrModule = cm
+		if len(wrap.UrlArgs) >= 2 && wrap.UrlArgs[1] != "" {
+			wrap.CurrSubModule = wrap.UrlArgs[1]
+		}
 		if mod.Back != nil {
 			sidebar_left, content, sidebar_right := mod.Back(wrap)