forked from mbk-lab/rui_orig
2
0
Fork 0
rui/sessionUtils.go

153 lines
4.1 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"fmt"
2022-07-22 13:10:55 +03:00
"strings"
2021-09-07 17:36:50 +03:00
)
2022-07-22 13:10:55 +03:00
func (session *sessionData) startUpdateScript(htmlID string) {
buffer := allocStringBuilder()
session.updateScripts[htmlID] = buffer
buffer.WriteString("var element = document.getElementById('")
buffer.WriteString(htmlID)
buffer.WriteString("');\nif (element) {\n")
}
func (session *sessionData) updateScript(htmlID string) *strings.Builder {
if buffer, ok := session.updateScripts[htmlID]; ok {
return buffer
}
return nil
}
func (session *sessionData) finishUpdateScript(htmlID string) {
if buffer, ok := session.updateScripts[htmlID]; ok {
buffer.WriteString("scanElementsSize();\n}\n")
session.runScript(buffer.String())
freeStringBuilder(buffer)
delete(session.updateScripts, htmlID)
}
}
2021-09-07 17:36:50 +03:00
func sizeConstant(session Session, tag string) (SizeUnit, bool) {
if text, ok := session.Constant(tag); ok {
return StringToSizeUnit(text)
}
return AutoSize(), false
}
func updateCSSStyle(htmlID string, session Session) {
if !session.ignoreViewUpdates() {
if view := session.viewByHTMLID(htmlID); view != nil {
2022-10-29 20:16:40 +03:00
builder := viewCSSBuilder{buffer: allocStringBuilder()}
2021-09-07 17:36:50 +03:00
view.cssStyle(view, &builder)
2022-10-29 20:16:40 +03:00
session.runFunc("updateCSSStyle", view.htmlID(), builder.finish())
2021-09-07 17:36:50 +03:00
}
}
}
func updateInnerHTML(htmlID string, session Session) {
if !session.ignoreViewUpdates() {
2022-05-17 10:46:00 +03:00
var view View
if htmlID == "ruiRootView" {
view = session.RootView()
} else {
view = session.viewByHTMLID(htmlID)
}
if view != nil {
2021-09-07 17:36:50 +03:00
script := allocStringBuilder()
defer freeStringBuilder(script)
script.Grow(32 * 1024)
view.htmlSubviews(view, script)
2022-10-29 20:16:40 +03:00
session.runFunc("updateInnerHTML", view.htmlID(), script.String())
2021-09-07 17:36:50 +03:00
}
}
}
func appendToInnerHTML(htmlID, content string, session Session) {
if !session.ignoreViewUpdates() {
if view := session.viewByHTMLID(htmlID); view != nil {
2022-10-29 20:16:40 +03:00
session.runFunc("appendToInnerHTML", view.htmlID(), content)
2021-09-07 17:36:50 +03:00
}
}
}
func updateProperty(htmlID, property, value string, session Session) {
if !session.ignoreViewUpdates() {
2022-07-22 13:10:55 +03:00
if buffer := session.updateScript(htmlID); buffer != nil {
buffer.WriteString(fmt.Sprintf(`element.setAttribute('%v', '%v');`, property, value))
buffer.WriteRune('\n')
} else {
2022-10-29 20:16:40 +03:00
session.runFunc("updateProperty", htmlID, property, value)
2022-07-22 13:10:55 +03:00
}
2021-09-07 17:36:50 +03:00
}
}
func updateCSSProperty(htmlID, property, value string, session Session) {
if !session.ignoreViewUpdates() {
2022-07-22 13:10:55 +03:00
if buffer := session.updateScript(htmlID); buffer != nil {
buffer.WriteString(fmt.Sprintf(`element.style['%v'] = '%v';`, property, value))
buffer.WriteRune('\n')
} else {
2022-10-29 20:16:40 +03:00
session.runFunc("updateCSSProperty", htmlID, property, value)
2022-07-22 13:10:55 +03:00
}
2021-09-07 17:36:50 +03:00
}
}
func updateBoolProperty(htmlID, property string, value bool, session Session) {
if !session.ignoreViewUpdates() {
2022-07-22 13:10:55 +03:00
if buffer := session.updateScript(htmlID); buffer != nil {
if value {
buffer.WriteString(fmt.Sprintf(`element.setAttribute('%v', true);`, property))
} else {
buffer.WriteString(fmt.Sprintf(`element.setAttribute('%v', false);`, property))
}
buffer.WriteRune('\n')
2021-09-07 17:36:50 +03:00
} else {
2022-10-29 20:16:40 +03:00
session.runFunc("updateProperty", htmlID, property, value)
2021-09-07 17:36:50 +03:00
}
}
}
func removeProperty(htmlID, property string, session Session) {
if !session.ignoreViewUpdates() {
2022-07-22 13:10:55 +03:00
if buffer := session.updateScript(htmlID); buffer != nil {
buffer.WriteString(fmt.Sprintf(`if (element.hasAttribute('%v')) { element.removeAttribute('%v');}`, property, property))
buffer.WriteRune('\n')
} else {
2022-10-29 20:16:40 +03:00
session.runFunc("removeProperty", htmlID, property)
2022-07-22 13:10:55 +03:00
}
2021-09-07 17:36:50 +03:00
}
}
/*
func setDisabled(htmlID string, disabled bool, session Session) {
if !session.ignoreViewUpdates() {
if disabled {
session.runScript(fmt.Sprintf(`setDisabled('%v', true);`, htmlID))
} else {
session.runScript(fmt.Sprintf(`setDisabled('%v', false);`, htmlID))
}
}
}
*/
func viewByHTMLID(id string, startView View) View {
if startView != nil {
if startView.htmlID() == id {
return startView
}
if container, ok := startView.(ParanetView); ok {
for _, view := range container.Views() {
if view != nil {
if v := viewByHTMLID(id, view); v != nil {
return v
}
}
}
}
}
return nil
}