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

163 lines
4.6 KiB
Go

package rui
import (
"fmt"
"strings"
)
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)
}
}
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 {
var builder viewCSSBuilder
builder.buffer = allocStringBuilder()
builder.buffer.WriteString(`updateCSSStyle('`)
builder.buffer.WriteString(view.htmlID())
builder.buffer.WriteString(`', '`)
view.cssStyle(view, &builder)
builder.buffer.WriteString(`');`)
view.Session().runScript(builder.finish())
}
}
}
func updateInnerHTML(htmlID string, session Session) {
if !session.ignoreViewUpdates() {
var view View
if htmlID == "ruiRootView" {
view = session.RootView()
} else {
view = session.viewByHTMLID(htmlID)
}
if view != nil {
script := allocStringBuilder()
defer freeStringBuilder(script)
script.Grow(32 * 1024)
view.htmlSubviews(view, script)
view.Session().runScript(fmt.Sprintf(`updateInnerHTML('%v', '%v');`, view.htmlID(), script.String()))
//view.updateEventHandlers()
}
}
}
func appendToInnerHTML(htmlID, content string, session Session) {
if !session.ignoreViewUpdates() {
if view := session.viewByHTMLID(htmlID); view != nil {
view.Session().runScript(fmt.Sprintf(`appendToInnerHTML('%v', '%v');`, view.htmlID(), content))
//view.updateEventHandlers()
}
}
}
func updateProperty(htmlID, property, value string, session Session) {
if !session.ignoreViewUpdates() {
if buffer := session.updateScript(htmlID); buffer != nil {
buffer.WriteString(fmt.Sprintf(`element.setAttribute('%v', '%v');`, property, value))
buffer.WriteRune('\n')
} else {
session.runScript(fmt.Sprintf(`updateProperty('%v', '%v', '%v');`, htmlID, property, value))
}
}
}
func updateCSSProperty(htmlID, property, value string, session Session) {
if !session.ignoreViewUpdates() {
if buffer := session.updateScript(htmlID); buffer != nil {
buffer.WriteString(fmt.Sprintf(`element.style['%v'] = '%v';`, property, value))
buffer.WriteRune('\n')
} else {
session.runScript(fmt.Sprintf(`updateCSSProperty('%v', '%v', '%v');`, htmlID, property, value))
}
}
}
func updateBoolProperty(htmlID, property string, value bool, session Session) {
if !session.ignoreViewUpdates() {
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')
} else if value {
session.runScript(fmt.Sprintf(`updateProperty('%v', '%v', true);`, htmlID, property))
} else {
session.runScript(fmt.Sprintf(`updateProperty('%v', '%v', false);`, htmlID, property))
}
}
}
func removeProperty(htmlID, property string, session Session) {
if !session.ignoreViewUpdates() {
if buffer := session.updateScript(htmlID); buffer != nil {
buffer.WriteString(fmt.Sprintf(`if (element.hasAttribute('%v')) { element.removeAttribute('%v');}`, property, property))
buffer.WriteRune('\n')
} else {
session.runScript(fmt.Sprintf(`removeProperty('%v', '%v');`, htmlID, property))
}
}
}
/*
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
}