2024-05-18 21:40:33 +02:00
|
|
|
package widget
|
|
|
|
|
|
|
|
import (
|
2024-05-25 03:28:45 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-05-18 21:40:33 +02:00
|
|
|
"html/template"
|
2024-05-25 03:28:45 +02:00
|
|
|
"time"
|
2024-05-18 21:40:33 +02:00
|
|
|
|
|
|
|
"github.com/glanceapp/glance/internal/assets"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Clock struct {
|
|
|
|
widgetBase `yaml:",inline"`
|
2024-05-25 03:28:45 +02:00
|
|
|
cachedHTML template.HTML `yaml:"-"`
|
|
|
|
HourFormat string `yaml:"hour-format"`
|
|
|
|
Timezones []struct {
|
|
|
|
Timezone string `yaml:"timezone"`
|
|
|
|
Label string `yaml:"label"`
|
|
|
|
} `yaml:"timezones"`
|
2024-05-18 21:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Clock) Initialize() error {
|
|
|
|
widget.withTitle("Clock").withError(nil)
|
2024-05-25 03:28:45 +02:00
|
|
|
|
|
|
|
if widget.HourFormat == "" {
|
|
|
|
widget.HourFormat = "24h"
|
|
|
|
} else if widget.HourFormat != "12h" && widget.HourFormat != "24h" {
|
|
|
|
return errors.New("invalid hour format for clock widget, must be either 12h or 24h")
|
|
|
|
}
|
|
|
|
|
|
|
|
for t := range widget.Timezones {
|
|
|
|
if widget.Timezones[t].Timezone == "" {
|
|
|
|
return errors.New("missing timezone value for clock widget")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := time.LoadLocation(widget.Timezones[t].Timezone)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid timezone '%s' for clock widget: %v", widget.Timezones[t].Timezone, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.cachedHTML = widget.render(widget, assets.ClockTemplate)
|
|
|
|
|
2024-05-18 21:40:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Clock) Render() template.HTML {
|
2024-05-25 03:28:45 +02:00
|
|
|
return widget.cachedHTML
|
2024-05-18 21:40:33 +02:00
|
|
|
}
|