2019-10-27 23:13:24 +00:00
|
|
|
/* Copyright Azareal 2017 - 2020 */
|
2017-11-11 04:06:16 +00:00
|
|
|
package common
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2017-11-11 23:34:27 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-01-21 12:27:59 +00:00
|
|
|
"fmt"
|
2017-11-29 02:34:02 +00:00
|
|
|
"html/template"
|
|
|
|
"strings"
|
2017-11-11 23:34:27 +00:00
|
|
|
"sync"
|
2019-01-21 12:27:59 +00:00
|
|
|
"sync/atomic"
|
2019-02-28 07:28:17 +00:00
|
|
|
|
|
|
|
min "github.com/Azareal/Gosora/common/templates"
|
2019-07-26 23:29:42 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-11-11 23:34:27 +00:00
|
|
|
)
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
// TODO: Clean this file up
|
2017-11-11 04:06:16 +00:00
|
|
|
var Docks WidgetDocks
|
2017-09-03 04:50:31 +00:00
|
|
|
var widgetUpdateMutex sync.RWMutex
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
type WidgetDock struct {
|
|
|
|
Items []*Widget
|
|
|
|
Scheduler *WidgetScheduler
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type WidgetDocks struct {
|
2018-03-17 08:16:43 +00:00
|
|
|
LeftOfNav []*Widget
|
|
|
|
RightOfNav []*Widget
|
2019-01-21 12:27:59 +00:00
|
|
|
LeftSidebar WidgetDock
|
|
|
|
RightSidebar WidgetDock
|
2017-06-19 08:06:54 +00:00
|
|
|
//PanelLeft []Menus
|
2019-01-21 12:27:59 +00:00
|
|
|
Footer WidgetDock
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type WidgetMenu struct {
|
|
|
|
Name string
|
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
|
|
|
MenuList []WidgetMenuItem
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type WidgetMenuItem struct {
|
|
|
|
Text string
|
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
|
|
|
Location string
|
2017-09-03 04:50:31 +00:00
|
|
|
Compact bool
|
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
type NameTextPair struct {
|
2017-06-19 08:06:54 +00:00
|
|
|
Name string
|
2017-11-29 02:34:02 +00:00
|
|
|
Text template.HTML
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 01:49:31 +00:00
|
|
|
func preparseWidget(w *Widget, wdata string) (err error) {
|
2017-11-29 02:34:02 +00:00
|
|
|
prebuildWidget := func(name string, data interface{}) (string, error) {
|
|
|
|
var b bytes.Buffer
|
2019-02-10 05:52:26 +00:00
|
|
|
err := DefaultTemplates.ExecuteTemplate(&b, name+".html", data)
|
2019-02-28 07:28:17 +00:00
|
|
|
content := string(b.Bytes())
|
|
|
|
if Config.MinifyTemplates {
|
|
|
|
content = min.Minify(content)
|
|
|
|
}
|
|
|
|
return content, err
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sbytes := []byte(wdata)
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Literal = true
|
2019-01-21 12:27:59 +00:00
|
|
|
// TODO: Split these hard-coded items out of this file and into the files for the individual widget types
|
2019-10-06 01:49:31 +00:00
|
|
|
switch w.Type {
|
2019-01-21 12:27:59 +00:00
|
|
|
case "simple", "about":
|
2017-11-29 02:34:02 +00:00
|
|
|
var tmp NameTextPair
|
|
|
|
err = json.Unmarshal(sbytes, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Body, err = prebuildWidget("widget_"+w.Type, tmp)
|
2019-01-21 12:27:59 +00:00
|
|
|
case "search_and_filter":
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Literal = false
|
|
|
|
w.BuildFunc = widgetSearchAndFilter
|
2019-01-21 12:27:59 +00:00
|
|
|
case "wol":
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Literal = false
|
|
|
|
w.InitFunc = wolInit
|
|
|
|
w.BuildFunc = wolRender
|
|
|
|
w.TickFunc = wolTick
|
2019-01-21 12:27:59 +00:00
|
|
|
case "wol_context":
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Literal = false
|
|
|
|
w.BuildFunc = wolContextRender
|
2017-11-29 02:34:02 +00:00
|
|
|
default:
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Body = wdata
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Test this
|
|
|
|
// TODO: Should we toss this through a proper parser rather than crudely replacing it?
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Location = strings.Replace(w.Location, " ", "", -1)
|
|
|
|
w.Location = strings.Replace(w.Location, "frontend", "!panel", -1)
|
|
|
|
w.Location = strings.Replace(w.Location, "!!", "", -1)
|
2017-11-29 02:34:02 +00:00
|
|
|
|
|
|
|
// Skip blank zones
|
2019-10-06 01:49:31 +00:00
|
|
|
locs := strings.Split(w.Location, "|")
|
2017-11-29 02:34:02 +00:00
|
|
|
if len(locs) > 0 {
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Location = ""
|
2017-11-29 02:34:02 +00:00
|
|
|
for _, loc := range locs {
|
|
|
|
if loc == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Location += loc + "|"
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
w.Location = w.Location[:len(w.Location)-1]
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
func GetDockList() []string {
|
|
|
|
return []string{
|
|
|
|
"leftOfNav",
|
|
|
|
"rightOfNav",
|
|
|
|
"rightSidebar",
|
|
|
|
"footer",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDock(dock string) []*Widget {
|
|
|
|
switch dock {
|
|
|
|
case "leftOfNav":
|
|
|
|
return Docks.LeftOfNav
|
|
|
|
case "rightOfNav":
|
|
|
|
return Docks.RightOfNav
|
|
|
|
case "rightSidebar":
|
|
|
|
return Docks.RightSidebar.Items
|
|
|
|
case "footer":
|
|
|
|
return Docks.Footer.Items
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func HasDock(dock string) bool {
|
|
|
|
switch dock {
|
|
|
|
case "leftOfNav", "rightOfNav", "rightSidebar", "footer":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:31:13 +00:00
|
|
|
// TODO: Find a more optimimal way of doing this...
|
2019-10-06 01:49:31 +00:00
|
|
|
func HasWidgets(dock string, h *Header) bool {
|
|
|
|
if !h.Theme.HasDock(dock) {
|
2019-02-28 09:31:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let themes forcibly override this slot
|
2019-10-06 01:49:31 +00:00
|
|
|
sbody := h.Theme.BuildDock(dock)
|
2019-02-28 09:31:13 +00:00
|
|
|
if sbody != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var widgets []*Widget
|
|
|
|
switch dock {
|
|
|
|
case "leftOfNav":
|
|
|
|
widgets = Docks.LeftOfNav
|
|
|
|
case "rightOfNav":
|
|
|
|
widgets = Docks.RightOfNav
|
|
|
|
case "rightSidebar":
|
|
|
|
widgets = Docks.RightSidebar.Items
|
|
|
|
case "footer":
|
|
|
|
widgets = Docks.Footer.Items
|
|
|
|
}
|
|
|
|
|
|
|
|
wcount := 0
|
|
|
|
for _, widget := range widgets {
|
|
|
|
if !widget.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
if widget.Allowed(h.Zone, h.ZoneID) {
|
2019-02-28 09:31:13 +00:00
|
|
|
wcount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wcount > 0
|
|
|
|
}
|
|
|
|
|
2019-10-06 01:49:31 +00:00
|
|
|
func BuildWidget(dock string, h *Header) (sbody string) {
|
|
|
|
if !h.Theme.HasDock(dock) {
|
2017-11-29 02:34:02 +00:00
|
|
|
return ""
|
|
|
|
}
|
2018-03-17 08:16:43 +00:00
|
|
|
// Let themes forcibly override this slot
|
2019-10-06 01:49:31 +00:00
|
|
|
sbody = h.Theme.BuildDock(dock)
|
2018-03-17 08:16:43 +00:00
|
|
|
if sbody != "" {
|
|
|
|
return sbody
|
|
|
|
}
|
|
|
|
|
2019-10-06 01:49:31 +00:00
|
|
|
var widgets []*Widget
|
2017-11-29 02:34:02 +00:00
|
|
|
switch dock {
|
2018-03-17 08:16:43 +00:00
|
|
|
case "leftOfNav":
|
|
|
|
widgets = Docks.LeftOfNav
|
|
|
|
case "rightOfNav":
|
|
|
|
widgets = Docks.RightOfNav
|
2018-04-22 12:33:56 +00:00
|
|
|
case "topMenu":
|
|
|
|
// 1 = id for the default menu
|
2018-05-11 05:41:51 +00:00
|
|
|
mhold, err := Menus.Get(1)
|
|
|
|
if err == nil {
|
2020-03-15 11:34:57 +00:00
|
|
|
err := mhold.Build(h.Writer, &h.CurrentUser, h.Path)
|
2018-04-22 12:33:56 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2017-11-29 02:34:02 +00:00
|
|
|
case "rightSidebar":
|
2019-01-21 12:27:59 +00:00
|
|
|
widgets = Docks.RightSidebar.Items
|
2017-11-29 02:34:02 +00:00
|
|
|
case "footer":
|
2019-01-21 12:27:59 +00:00
|
|
|
widgets = Docks.Footer.Items
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, widget := range widgets {
|
|
|
|
if !widget.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
if widget.Allowed(h.Zone, h.ZoneID) {
|
|
|
|
item, err := widget.Build(h)
|
2017-11-29 02:34:02 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
sbody += item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sbody
|
|
|
|
}
|
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
func getDockWidgets(dock string) (widgets []*Widget, err error) {
|
|
|
|
rows, err := widgetStmts.getDockList.Query(dock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
defer rows.Close()
|
2017-11-29 02:34:02 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
for rows.Next() {
|
2019-10-06 01:49:31 +00:00
|
|
|
w := &Widget{Position: 0, Side: dock}
|
|
|
|
err = rows.Scan(&w.ID, &w.Position, &w.Type, &w.Enabled, &w.Location, &w.RawBody)
|
2019-01-21 12:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-29 02:34:02 +00:00
|
|
|
|
2019-10-06 01:49:31 +00:00
|
|
|
err = preparseWidget(w, w.RawBody)
|
2019-01-21 12:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
Widgets.set(w)
|
|
|
|
widgets = append(widgets, w)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
return widgets, rows.Err()
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 09:55:34 +00:00
|
|
|
// TODO: Make a store for this?
|
2019-10-06 01:49:31 +00:00
|
|
|
func InitWidgets() (fi error) {
|
|
|
|
// TODO: Let themes set default values for widget docks, and let them lock in particular places with their stuff, e.g. leftOfNav and rightOfNav
|
|
|
|
f := func(name string) {
|
|
|
|
if fi != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dock, err := getDockWidgets(name)
|
|
|
|
if err != nil {
|
|
|
|
fi = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setDock(name, dock)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2020-03-15 11:34:57 +00:00
|
|
|
|
2019-10-06 01:49:31 +00:00
|
|
|
f("leftOfNav")
|
|
|
|
f("rightOfNav")
|
|
|
|
f("leftSidebar")
|
|
|
|
f("rightSidebar")
|
|
|
|
f("footer")
|
|
|
|
if fi != nil {
|
|
|
|
return fi
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
AddScheduledSecondTask(Docks.LeftSidebar.Scheduler.Tick)
|
|
|
|
AddScheduledSecondTask(Docks.RightSidebar.Scheduler.Tick)
|
|
|
|
AddScheduledSecondTask(Docks.Footer.Scheduler.Tick)
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 05:07:00 +00:00
|
|
|
func releaseWidgets(ws []*Widget) {
|
|
|
|
for _, w := range ws {
|
|
|
|
if w.ShutdownFunc != nil {
|
|
|
|
w.ShutdownFunc(w)
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
// TODO: Use atomics
|
|
|
|
func setDock(dock string, widgets []*Widget) {
|
2019-07-26 23:29:42 +00:00
|
|
|
dockHandle := func(dockWidgets []*Widget) {
|
2019-01-21 12:27:59 +00:00
|
|
|
DebugLog(dock, widgets)
|
|
|
|
releaseWidgets(dockWidgets)
|
|
|
|
}
|
2019-07-26 23:29:42 +00:00
|
|
|
dockHandle2 := func(dockWidgets WidgetDock) WidgetDock {
|
2019-01-21 12:27:59 +00:00
|
|
|
dockHandle(dockWidgets.Items)
|
|
|
|
if dockWidgets.Scheduler == nil {
|
|
|
|
dockWidgets.Scheduler = &WidgetScheduler{}
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
for _, widget := range widgets {
|
|
|
|
if widget.InitFunc != nil {
|
|
|
|
widget.InitFunc(widget, dockWidgets.Scheduler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dockWidgets.Scheduler.Store()
|
|
|
|
return WidgetDock{widgets, dockWidgets.Scheduler}
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
2019-10-06 01:49:31 +00:00
|
|
|
widgetUpdateMutex.Lock()
|
|
|
|
defer widgetUpdateMutex.Unlock()
|
2019-01-21 12:27:59 +00:00
|
|
|
switch dock {
|
|
|
|
case "leftOfNav":
|
|
|
|
dockHandle(Docks.LeftOfNav)
|
|
|
|
Docks.LeftOfNav = widgets
|
|
|
|
case "rightOfNav":
|
|
|
|
dockHandle(Docks.RightOfNav)
|
|
|
|
Docks.RightOfNav = widgets
|
|
|
|
case "leftSidebar":
|
|
|
|
Docks.LeftSidebar = dockHandle2(Docks.LeftSidebar)
|
|
|
|
case "rightSidebar":
|
|
|
|
Docks.RightSidebar = dockHandle2(Docks.RightSidebar)
|
|
|
|
case "footer":
|
|
|
|
Docks.Footer = dockHandle2(Docks.Footer)
|
|
|
|
default:
|
|
|
|
fmt.Printf("bad dock '%s'\n", dock)
|
|
|
|
return
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2019-01-21 12:27:59 +00:00
|
|
|
type WidgetScheduler struct {
|
|
|
|
widgets []*Widget
|
|
|
|
store atomic.Value
|
|
|
|
}
|
2018-03-17 08:16:43 +00:00
|
|
|
|
2019-10-06 01:49:31 +00:00
|
|
|
func (s *WidgetScheduler) Add(w *Widget) {
|
|
|
|
s.widgets = append(s.widgets, w)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *WidgetScheduler) Store() {
|
|
|
|
s.store.Store(s.widgets)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
2017-06-19 08:06:54 +00:00
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
func (s *WidgetScheduler) Tick() error {
|
|
|
|
widgets := s.store.Load().([]*Widget)
|
2019-01-21 12:27:59 +00:00
|
|
|
for _, widget := range widgets {
|
|
|
|
if widget.TickFunc == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
err := widget.TickFunc(widget)
|
2019-01-21 12:27:59 +00:00
|
|
|
if err != nil {
|
2019-07-26 23:29:42 +00:00
|
|
|
return errors.WithStack(err)
|
2019-01-21 12:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-19 08:06:54 +00:00
|
|
|
return nil
|
|
|
|
}
|