Initial implementation of inventree
A small IT asset inventory system built around a nested folder tree, matching the project brief: Go + html/template + Bootstrap 5 + vanilla JS, SQLite storage (WAL, FKs, migrations), embedded templates/static assets, single compiled binary. - AJAX-driven inventory tree: lazy-loaded folder expansion with per-user persisted expand state, drag-and-drop move for folders/assets, safe delete-with-relocate-or-cascade confirmation, search with breadcrumbs. - Assets: CRUD, duplicate, move, auto-generated IT-000001-style IDs, CSV import (with optional folder auto-create) and export. - Admin-defined item types with custom fields (text/long_text/number/date/ boolean/dropdown/url/ip_address). - Configurable statuses, session-based auth (bcrypt, secure cookies), admin/user roles, user management. - CLI: serve, version, database migrate, database backup; /healthz; graceful shutdown; Dockerfile/docker-compose/systemd unit; Go test suite.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package web
|
||||
|
||||
import "context"
|
||||
|
||||
// FolderOption is a flattened, indented folder choice for <select> dropdowns
|
||||
// (asset create/edit, move-to pickers).
|
||||
type FolderOption struct {
|
||||
ID int64
|
||||
Path string
|
||||
Depth int
|
||||
}
|
||||
|
||||
// folderOptions returns every folder as a flat, depth-first ordered list
|
||||
// with indentable depth, suitable for a folder-picker <select>.
|
||||
func (a *App) folderOptions(ctx context.Context) ([]FolderOption, error) {
|
||||
all, err := a.Folders.All(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
byParent := map[int64][]struct {
|
||||
ID int64
|
||||
Name string
|
||||
}{}
|
||||
var top []struct {
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
for _, f := range all {
|
||||
item := struct {
|
||||
ID int64
|
||||
Name string
|
||||
}{f.ID, f.Name}
|
||||
if f.ParentID == nil {
|
||||
top = append(top, item)
|
||||
} else {
|
||||
byParent[*f.ParentID] = append(byParent[*f.ParentID], item)
|
||||
}
|
||||
}
|
||||
|
||||
var out []FolderOption
|
||||
var walk func(id int64, name, prefix string, depth int)
|
||||
walk = func(id int64, name, prefix string, depth int) {
|
||||
path := name
|
||||
if prefix != "" {
|
||||
path = prefix + " / " + name
|
||||
}
|
||||
out = append(out, FolderOption{ID: id, Path: path, Depth: depth})
|
||||
for _, child := range byParent[id] {
|
||||
walk(child.ID, child.Name, path, depth+1)
|
||||
}
|
||||
}
|
||||
for _, t := range top {
|
||||
walk(t.ID, t.Name, "", 0)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user