2023-01-25 01:06:36 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-04-23 12:49:32 +03:00
|
|
|
"os"
|
2023-01-25 01:06:36 +03:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// SvgImageView represents an SvgImageView view
|
2023-01-25 01:06:36 +03:00
|
|
|
type SvgImageView interface {
|
|
|
|
View
|
|
|
|
}
|
|
|
|
|
|
|
|
type svgImageViewData struct {
|
|
|
|
viewData
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSvgImageView create new SvgImageView object and return it
|
|
|
|
func NewSvgImageView(session Session, params Params) SvgImageView {
|
|
|
|
view := new(svgImageViewData)
|
|
|
|
view.init(session)
|
|
|
|
setInitParams(view, params)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSvgImageView(session Session) View {
|
2024-11-13 12:56:39 +03:00
|
|
|
return new(svgImageViewData) // NewSvgImageView(session, nil)
|
2023-01-25 01:06:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialize fields of imageView by default values
|
|
|
|
func (imageView *svgImageViewData) init(session Session) {
|
|
|
|
imageView.viewData.init(session)
|
|
|
|
imageView.tag = "SvgImageView"
|
|
|
|
imageView.systemClass = "ruiSvgImageView"
|
2024-11-13 12:56:39 +03:00
|
|
|
imageView.normalize = normalizeSvgImageViewTag
|
|
|
|
imageView.set = svgImageViewSet
|
|
|
|
imageView.changed = svgImageViewPropertyChanged
|
2023-01-25 01:06:36 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func normalizeSvgImageViewTag(tag PropertyName) PropertyName {
|
|
|
|
tag = defaultNormalize(tag)
|
2023-01-25 01:06:36 +03:00
|
|
|
switch tag {
|
|
|
|
case Source, "source":
|
|
|
|
tag = Content
|
|
|
|
|
|
|
|
case VerticalAlign:
|
|
|
|
tag = CellVerticalAlign
|
|
|
|
|
|
|
|
case HorizontalAlign:
|
|
|
|
tag = CellHorizontalAlign
|
|
|
|
}
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func svgImageViewSet(view View, tag PropertyName, value any) []PropertyName {
|
2023-01-25 01:06:36 +03:00
|
|
|
switch tag {
|
|
|
|
case Content:
|
|
|
|
if text, ok := value.(string); ok {
|
2024-11-13 12:56:39 +03:00
|
|
|
view.setRaw(Content, text)
|
|
|
|
return []PropertyName{tag}
|
2023-01-25 01:06:36 +03:00
|
|
|
}
|
|
|
|
notCompatibleType(Source, value)
|
2024-11-13 12:56:39 +03:00
|
|
|
return nil
|
2023-01-25 01:06:36 +03:00
|
|
|
|
|
|
|
default:
|
2024-11-13 12:56:39 +03:00
|
|
|
return viewSet(view, tag, value)
|
2023-01-25 01:06:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func svgImageViewPropertyChanged(view View, tag PropertyName) {
|
|
|
|
switch tag {
|
|
|
|
case Content:
|
|
|
|
updateInnerHTML(view.htmlID(), view.Session())
|
|
|
|
|
|
|
|
default:
|
|
|
|
viewPropertyChanged(view, tag)
|
|
|
|
}
|
2023-01-25 01:06:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (imageView *svgImageViewData) htmlTag() string {
|
|
|
|
return "div"
|
|
|
|
}
|
|
|
|
|
2024-10-17 18:07:17 +03:00
|
|
|
func (imageView *svgImageViewData) writeSvg(data []byte, buffer *strings.Builder) {
|
|
|
|
text := string(data)
|
|
|
|
index := strings.Index(text, "<svg")
|
|
|
|
if index > 0 {
|
|
|
|
text = text[index:]
|
|
|
|
}
|
|
|
|
|
|
|
|
index = strings.Index(text, "\n")
|
|
|
|
for index >= 0 {
|
|
|
|
if index > 0 && text[index-1] == '\r' {
|
|
|
|
buffer.WriteString(text[:index-1])
|
|
|
|
} else {
|
|
|
|
buffer.WriteString(text[:index])
|
|
|
|
}
|
|
|
|
|
|
|
|
end := len(text)
|
|
|
|
index++
|
|
|
|
for index < end && (text[index] == ' ' || text[index] == '\t' || text[index] == '\r' || text[index] == '\n') {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
|
|
|
text = text[index:]
|
|
|
|
index = strings.Index(text, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(text)
|
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:36 +03:00
|
|
|
func (imageView *svgImageViewData) htmlSubviews(self View, buffer *strings.Builder) {
|
|
|
|
if value := imageView.getRaw(Content); value != nil {
|
|
|
|
if content, ok := value.(string); ok && content != "" {
|
2023-04-23 12:49:32 +03:00
|
|
|
if strings.HasPrefix(content, "@") {
|
|
|
|
if name, ok := imageView.session.ImageConstant(content[1:]); ok {
|
|
|
|
content = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if image, ok := resources.images[content]; ok {
|
|
|
|
if image.fs != nil {
|
|
|
|
if data, err := image.fs.ReadFile(image.path); err == nil {
|
2024-10-17 18:07:17 +03:00
|
|
|
imageView.writeSvg(data, buffer)
|
2023-04-23 12:49:32 +03:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
DebugLog(err.Error())
|
|
|
|
}
|
|
|
|
} else if data, err := os.ReadFile(image.path); err == nil {
|
2024-10-17 18:07:17 +03:00
|
|
|
imageView.writeSvg(data, buffer)
|
2023-01-25 01:06:36 +03:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
DebugLog(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(content, "http://") || strings.HasPrefix(content, "https://") {
|
|
|
|
resp, err := http.Get(content)
|
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if body, err := io.ReadAll(resp.Body); err == nil {
|
2024-10-17 18:07:17 +03:00
|
|
|
imageView.writeSvg(body, buffer)
|
2023-01-25 01:06:36 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugLog(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSvgImageViewVerticalAlign return the vertical align of an SvgImageView subview: TopAlign (0), BottomAlign (1), CenterAlign (2)
|
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
|
|
|
|
func GetSvgImageViewVerticalAlign(view View, subviewID ...string) int {
|
|
|
|
return enumStyledProperty(view, subviewID, CellVerticalAlign, LeftAlign, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSvgImageViewHorizontalAlign return the vertical align of an SvgImageView subview: LeftAlign (0), RightAlign (1), CenterAlign (2)
|
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a left position of the first argument (view) is returned
|
|
|
|
func GetSvgImageViewHorizontalAlign(view View, subviewID ...string) int {
|
|
|
|
return enumStyledProperty(view, subviewID, CellHorizontalAlign, LeftAlign, false)
|
|
|
|
}
|