2018-05-11 05:41:51 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:53:13 +00:00
|
|
|
"database/sql"
|
|
|
|
"strconv"
|
|
|
|
"sync/atomic"
|
2018-05-11 05:41:51 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
qgen "git.tuxpa.in/a/gosora/query_gen"
|
2018-05-11 05:41:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Menus *DefaultMenuStore
|
|
|
|
|
|
|
|
type DefaultMenuStore struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
menus map[int]*atomic.Value
|
|
|
|
itemStore *DefaultMenuItemStore
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultMenuStore() *DefaultMenuStore {
|
2022-02-21 03:32:53 +00:00
|
|
|
return &DefaultMenuStore{
|
|
|
|
make(map[int]*atomic.Value),
|
|
|
|
NewDefaultMenuItemStore(),
|
|
|
|
}
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add actual support for multiple menus
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) GetAllMap() (out map[int]*MenuListHolder) {
|
2022-02-21 03:32:53 +00:00
|
|
|
out = make(map[int]*MenuListHolder)
|
|
|
|
for mid, atom := range s.menus {
|
|
|
|
out[mid] = atom.Load().(*MenuListHolder)
|
|
|
|
}
|
|
|
|
return out
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) Get(mid int) (*MenuListHolder, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
aStore, ok := s.menus[mid]
|
|
|
|
if ok {
|
|
|
|
return aStore.Load().(*MenuListHolder), nil
|
|
|
|
}
|
|
|
|
return nil, ErrNoRows
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) Items(mid int) (mlist MenuItemList, err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
err = qgen.NewAcc().Select("menu_items").Columns("miid,name,htmlID,cssClass,position,path,aria,tooltip,order,tmplName,guestOnly,memberOnly,staffOnly,adminOnly").Where("mid=" + strconv.Itoa(mid)).Orderby("order ASC").Each(func(rows *sql.Rows) error {
|
|
|
|
i := MenuItem{MenuID: mid}
|
|
|
|
err := rows.Scan(&i.ID, &i.Name, &i.HTMLID, &i.CSSClass, &i.Position, &i.Path, &i.Aria, &i.Tooltip, &i.Order, &i.TmplName, &i.GuestOnly, &i.MemberOnly, &i.SuperModOnly, &i.AdminOnly)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.itemStore.Add(i)
|
|
|
|
mlist = append(mlist, i)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return mlist, err
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) Load(mid int) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
mlist, err := s.Items(mid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hold := &MenuListHolder{mid, mlist, make(map[int]menuTmpl)}
|
|
|
|
err = hold.Preparse()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-11 05:41:51 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
aStore := &atomic.Value{}
|
|
|
|
aStore.Store(hold)
|
|
|
|
s.menus[mid] = aStore
|
|
|
|
return nil
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *DefaultMenuStore) ItemStore() *DefaultMenuItemStore {
|
2022-02-21 03:32:53 +00:00
|
|
|
return s.itemStore
|
2018-05-11 05:41:51 +00:00
|
|
|
}
|