dashboard

package module
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2025 License: AGPL-3.0 Imports: 13 Imported by: 0

README

Dashboard Open in Gitpod

tests Go Report Card PkgGoDev

This is a project for quickly building a dashboard.

Out of the box it provides a general layout, main menu, user dropdown menu, quick access menu, and theme switcher.

The content of the page itself is left blank. It is up to the developer to customize the pages as needed by the application he is building.

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/agpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Features

  • Uses the latest Bootstrap (v5.3.3)
  • Uses the latest Bootstrap Icons (v1.11.3)
  • Preset sidebar menu
  • Preset user dropdown menu
  • Preset quick access menu
  • Preset menu switcher
  • Supported all Bootswatch themes (26 total)
  • Flexible component system for building UI elements
  • Consistent functional API for all components

Components

The dashboard includes a flexible component system that allows you to build custom UI elements. All components follow a consistent functional pattern that returns hb.TagInterface directly, making them easy to compose and combine.

ShadowBoxComponent

The ShadowBoxComponent adds shadow, padding, and margin to your content, giving it a clean, separated look.

shadowBox := components.NewShadowBoxComponent(components.ShadowBoxConfig{
    Content: "<h1>Hello World</h1>",
    Padding: 15,
    Margin: 10,
})
Card Component

The Card component creates a Bootstrap card with an optional title and customizable styling.

card := components.NewCard(components.CardConfig{
    Title:   "Card Title",
    Content: "<p>This is content inside a Bootstrap card component.</p>",
    Margin:  10,
})
Tab Layout Component

The TabLayout component organizes content into tabs that users can switch between.

tabLayout := components.NewTabLayout(components.TabLayoutConfig{
    Items: []components.TabItem{
        {
            ID:      "tab1",
            Title:   "First Tab",
            Content: "<p>Content of the first tab.</p>",
            Active:  true,
        },
        {
            ID:      "tab2",
            Title:   "Second Tab",
            Content: "<p>Content of the second tab.</p>",
        },
    },
    Margin: 10,
})
Grid Component

The Grid component creates a responsive Bootstrap grid system with rows and columns.

// Create a grid layout with two rows
row1 := []components.GridItem{
    {Content: "<div class='p-3 bg-light'>Column 1</div>", ColumnClass: "col-md-6"},
    {Content: "<div class='p-3 bg-light'>Column 2</div>", ColumnClass: "col-md-6"},
}

// Second row with three columns of different widths
row2 := []components.GridItem{
    {Content: "<div class='p-3 bg-light'>Column 1</div>", ColumnClass: "col-md-3"},
    {Content: "<div class='p-3 bg-light'>Column 2</div>", ColumnClass: "col-md-6"},
    {Content: "<div class='p-3 bg-light'>Column 3</div>", ColumnClass: "col-md-3"},
}

grid := components.NewGridComponent(components.GridLayoutConfig{
    Rows:    [][]components.GridItem{row1, row2},
    Gutters: 3,
    Margin:  10,
})
Combining Components

Components can be easily combined by using the .ToHTML() method:

// Create tabs
tabs := components.NewTabLayout(components.TabLayoutConfig{
    Items: []components.TabItem{
        {
            ID:      "tab1",
            Title:   "First Tab",
            Content: "<p>Content of the first tab.</p>",
            Active:  true,
        },
        {
            ID:      "tab2",
            Title:   "Second Tab",
            Content: "<p>Content of the second tab.</p>",
        },
    },
})

// Put the tabs inside a card
card := components.NewCard(components.CardConfig{
    Title:   "Card with Tabs",
    Content: tabs.ToHTML(),
})

// Put the card inside a shadow box
shadowBox := components.NewShadowBoxComponent(components.ShadowBoxConfig{
    Content: card.ToHTML(),
    Padding: 15,
    Margin:  10,
})

Running the Examples

The dashboard includes a runnable example that demonstrates all the components in action:

cd examples
go run component_examples.go

Then open your browser to http://localhost:8080 to see the examples.

Example

  • Adding to an HTTP handler
func dashboard(w http.ResponseWriter, r *http.Request) {
	dashboard := dashboard.NewDashboard(dashboard.Config{
		Menu: []dashboard.MenuItem{
			{
				Title: "Home",
				URL:   "/",
			},
			{
				Title: "Logout",
				URL:   "/auth/logout",
			},
		},
	})

	w.Write([]byte(dashboard.ToHTML()))
}
  • Adding to a layout function, to reuse on multiple places
func layout(r *http.Request, opts AdminDashboardOptions) string {
    authUser := helpers.GetAuthUser(r)

    logoImageURL = "YOUR_IMAGE_URL.png"
	logoRedirectURL = "/"

    dashboardMenuItems := []dashboard.MenuItem{
            {
                Title: "Home",
                URL:   links.NewAdminLinks().Home(map[string]string{}),
            },
            {
                Title: "Blog Manager",
                URL:   links.NewAdminLinks().Blog(map[string]string{}),
            },
            {
                Title: "Website Manager",
                URL:   links.NewAdminLinks().Cms(map[string]string{}),
            },
            {
                Title: "User Manager",
                URL:   links.NewAdminLinks().Users(map[string]string{}),
            },
        }

    dashboardUser := dashboard.User{
            FirstName: authUser.FirstName(),
            LastName:  authUser.LastName(),
    }

    dashboardQuickAccessMenuItems := []dashboard.MenuItem {
        {
            Title: "New post",
            URL: "/post-create",
        },
        {
            Title: "New page",
            URL: "/page-create",
        }
    }
        
    dashboardUserMenuItems := []dashboard.MenuItem {
        {
            Title: "Profile",
            URL: "/account/profile",
        },
        {
            Title: "Logout",
            URL: "/auth/logout",
        }
    }
        
    dashboard := dashboard.NewDashboard(dashboard.Config{
        HTTPRequest:                r,
        Content:                    opts.Content,
        Title:                      opts.Title,
        LogoImageURL                logoImageURL,
        LogoRedirectURL             logoRedirectURL,
        MenuItems:                  dashboardMenuItems,
        User:                       dashboardUser,
        UserMenuItems:              dashboardUserMenuItems,
        QuickAccessMenuItems:       dashboardQuickAccessMenuItems,
        Scripts:                    opts.Scripts,
        ScriptURLs:                 opts.ScriptURLs,
        Styles:                     opts.Styles,
        StyleURLs:                  opts.StyleURLs,
        // optional, defaults to dark
        // NavbarBackgroundColorMode: "light"
        // optional, defaults to the default Bootstrap theme
        // ThemeName:                 dashboard.THEME_MINTY,
        // ThemeHandlerUrl:      links.NewAdminLinks().Theme(map[string]string{"redirect": links.NewAdminLinks().Home(map[string]string{})}),   // Optional (Advanced)
        // UncdnHandlerEndpoint: links.NewAdminLinks().Uncdn(map[string]string{}),                                                              // Optional (Advanced)
    })

    return dashboard.ToHTML()
}

Screenshots

  • Main View
  • Main Menu
  • Quick Access Menu
  • User Menu

Development

For working on this package:

  • Open in Gitpod (use the button provided)
  • Run these commands sequentially
  • Open the browser URL displayed in the terminal
task dev:init
task dev

Stargazers over time

Stargazers over time

Noteworthy

Similar Golang Projects

Documentation

Index

Constants

View Source
const MENU_TYPE_MODAL = "modal"
View Source
const MENU_TYPE_OFFCANVAS = "offcanvas"
View Source
const THEME_CERULEAN = "cerulean"
View Source
const THEME_COSMO = "cosmo"
View Source
const THEME_CYBORG = "cyborg"
View Source
const THEME_DARKLY = "darkly"
View Source
const THEME_DEFAULT = "bootstrap"
View Source
const THEME_FLATLY = "flatly"
View Source
const THEME_JOURNAL = "journal"
View Source
const THEME_LITERA = "litera"
View Source
const THEME_LUMEN = "lumen"
View Source
const THEME_LUX = "lux"
View Source
const THEME_MATERIA = "materia"
View Source
const THEME_MINTY = "minty"
View Source
const THEME_MORPH = "morph"
View Source
const THEME_PULSE = "pulse"
View Source
const THEME_QUARTZ = "quartz"
View Source
const THEME_SANDSTONE = "sandstone"
View Source
const THEME_SIMPLEX = "simplex"
View Source
const THEME_SKETCHY = "sketchy"
View Source
const THEME_SLATE = "slate"
View Source
const THEME_SOLAR = "solar"
View Source
const THEME_SPACELAB = "spacelab"
View Source
const THEME_SUPERHERO = "superhero"
View Source
const THEME_UNITED = "united"
View Source
const THEME_VAPOR = "vapor"
View Source
const THEME_YETI = "yeti"
View Source
const THEME_ZEPHYR = "zephyr"

Variables

View Source
var THEME_COOKIE_KEY = "theme"

Functions

func ThemeHandler added in v0.0.12

func ThemeHandler(w http.ResponseWriter, r *http.Request)

ThemeHandler checks for the supplied theme and sets the theme name in the session

func ThemeMiddleware added in v0.0.12

func ThemeMiddleware(next http.Handler) http.Handler

func ThemeNameRetrieveFromCookie added in v0.0.12

func ThemeNameRetrieveFromCookie(r *http.Request) string

func UncdnHandler added in v0.0.12

func UncdnHandler(w http.ResponseWriter, r *http.Request)

Types

type Config

type Config struct {
	Content     string
	FaviconURL  string
	HTTPRequest *http.Request
	LogoURL     string

	// Should the menu text be shown
	MenuShowText bool

	// The menu items to display in the main menu
	MenuItems []MenuItem

	// MenuType can be MENU_TYPE_OFFCANVAS or MENU_TYPE_MODAL
	MenuType string

	// Optional. The URL of the logo image
	LogoImageURL string

	// Optional. Raw HTML of the logo, if set will be used instead of logoImageURL
	LogoRawHtml string

	// Optional. The redirect URL of the logo image
	LogoRedirectURL string

	// Optional The background color for the navbar: light, dark (default),  primary, secondary, success, warning, info, danger
	NavbarBackgroundColorMode string

	// Optional. The background color for the navbar (default none)
	NavbarBackgroundColor string

	// Optional. The text color for the navbar (default light)
	NavbarTextColor string

	// Optional. The URL of the login page to use (if user is not provided)
	LoginURL string

	// Optional. The URL of the register page to use (if user is not provided)
	RegisterURL string

	// Optional. Menu for Quick Access
	QuickAccessMenu []MenuItem
	RedirectTime    string
	RedirectUrl     string
	Scripts         []string
	ScriptURLs      []string
	Styles          []string
	StyleURLs       []string

	// Optional. The theme to be activated on the dashboard (default will be used otherwise)
	Theme string

	// Optional. The URL of the theme switcher endpoint to use
	ThemeHandlerUrl string

	// Optional. The theme names to be visible in the theme switcher, the key is the theme, the value is the name (can be customized, default will be used otherwise)
	ThemesRestrict map[string]string

	Title                string
	UncdnHandlerEndpoint string
	User                 User
	UserMenu             []MenuItem
}

type Dashboard

type Dashboard struct {
	// contains filtered or unexported fields
}

func NewDashboard

func NewDashboard(config Config) *Dashboard

NewDashboard creates a new Dashboard instance based on the given configuration.

The function takes a Config struct as its parameter and returns a pointer to a Dashboard struct.

Parameters: - config: A Config struct containing the configuration for the dashboard.

Returns: - A pointer to a Dashboard struct.

func (*Dashboard) DashboardLayoutMenu added in v0.4.0

func (d *Dashboard) DashboardLayoutMenu() string

func (*Dashboard) SetMenuItems added in v1.6.1

func (d *Dashboard) SetMenuItems(menuItems []MenuItem) *Dashboard

func (*Dashboard) SetUser

func (d *Dashboard) SetUser(user User) *Dashboard

func (*Dashboard) SetUserMenuItems added in v1.6.1

func (d *Dashboard) SetUserMenuItems(menuItems []MenuItem) *Dashboard

func (*Dashboard) ToHTML

func (d *Dashboard) ToHTML() string

ToHTML returns the HTML representation of the Dashboard.

It does not take any parameters. It returns a string.

type MenuItem struct {
	Title    string
	URL      string
	Target   string
	Icon     string
	Sequence int
	Children []MenuItem
}

MenuItem is a menu item for the dashboard.

type ThemeNameContextKey added in v0.0.12

type ThemeNameContextKey struct{}

type User

type User struct {
	FirstName string
	LastName  string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL