Optimisation

This commit is contained in:
Alexei Anoshenko 2026-08-30 13:03:47 +03:00
parent 162f6dab33
commit 1a46153fa3
2 changed files with 40 additions and 7 deletions

View File

@ -219,6 +219,22 @@ function appendElement(elementId, content) {
} }
} }
function appendElementAfter(elementId, content) {
const element = document.getElementById(elementId);
if (element) {
element.insertAdjacentHTML("afterend", content);
scanElementsSize();
}
}
function insertElementAtStart(elementId, content) {
const element = document.getElementById(elementId);
if (element) {
element.insertAdjacentHTML("afterbegin", content);
scanElementsSize();
}
}
function appendToInputValue(elementId, content) { function appendToInputValue(elementId, content) {
const element = document.getElementById(elementId); const element = document.getElementById(elementId);
if (element) { if (element) {

View File

@ -116,10 +116,14 @@ func (container *viewsContainerData) Append(view View) {
} }
} }
func (container *viewsContainerData) insert(view View, index int) bool { func (container *viewsContainerData) insert(view View, index int) int {
if view != nil { if view != nil {
if container.views == nil || index < 0 || index >= len(container.views) { count := len(container.views)
return container.append(view) if count == 0 || index < 0 || index >= count {
if container.append(view) {
return len(container.views) - 1
}
return -1
} }
view.setParentID(container.htmlID()) view.setParentID(container.htmlID())
@ -128,15 +132,28 @@ func (container *viewsContainerData) insert(view View, index int) bool {
} else { } else {
container.views = append([]View{view}, container.views...) container.views = append([]View{view}, container.views...)
} }
return true return index
} }
return false return -1
} }
// Insert inserts a view to the "index" position in the list of a view children // Insert inserts a view to the "index" position in the list of a view children
func (container *viewsContainerData) Insert(view View, index int) { func (container *viewsContainerData) Insert(view View, index int) {
if container.insert(view, index) && container.created { index = container.insert(view, index)
updateInnerHTML(container.htmlID(), container.Session()) if index >= 0 && container.created {
html := allocStringBuilder()
defer freeStringBuilder(html)
viewHTML(view, html, "")
if index == 0 {
container.Session().callFunc("insertElementAtStart", container.htmlID(), html.String())
} else if index >= len(container.views)-1 {
container.Session().callFunc("appendElement", container.htmlID(), html.String())
} else {
container.Session().callFunc("appendElementAfter", container.views[index-1].htmlID(), html.String())
}
//updateInnerHTML(container.htmlID(), container.Session())
container.runChangeListener(Content) container.runChangeListener(Content)
} }
} }