diff --git a/app_scripts.js b/app_scripts.js index 8c40e74..e187c4a 100644 --- a/app_scripts.js +++ b/app_scripts.js @@ -223,6 +223,13 @@ function appendToInputValue(elementId, content) { } } +function removeView(elementId) { + const element = document.getElementById(elementId); + if (element) { + element.remove() + } +} + function setDisabled(elementId, disabled) { const element = document.getElementById(elementId); if (element) { diff --git a/stackLayout.go b/stackLayout.go index a2cc899..05c7f49 100644 --- a/stackLayout.go +++ b/stackLayout.go @@ -176,7 +176,7 @@ func (layout *stackLayoutData) transitionFinished(view View, tag PropertyName) { case "pop": if finished, ok := layout.onPopFinished[viewID]; ok { - session.updateCSSProperty(viewID+"page", "display", "none") + session.callFunc("removeView", viewID+"page") if finished.listener != nil { finished.listener(finished.view) } @@ -405,6 +405,115 @@ func transformMirror(transform TransformProperty, session Session) TransformProp return result } +// Append appends a view to the end of the list of a view children +func (layout *stackLayoutData) Append(view View) { + if view == nil { + ErrorLog("StackLayout.Append(nil) is forbidden") + return + } + + stackID := layout.htmlID() + view.setParentID(stackID) + + count := len(layout.views) + if count == 0 { + layout.views = []View{view} + } else { + layout.views = append(layout.views, view) + } + + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + + buffer.WriteString(`
`) + viewHTML(view, buffer, "") + buffer.WriteString(`
`) + + session := layout.Session() + if count > 0 { + session.updateCSSProperty(layout.views[count-1].htmlID()+"page", "visibility", "hidden") + } + session.appendToInnerHTML(stackID, buffer.String()) + + if listener, ok := layout.changeListener[Content]; ok { + listener(layout, Content) + } +} + +// Insert inserts a view to the "index" position in the list of a view children +func (layout *stackLayoutData) Insert(view View, index int) { + if view == nil { + ErrorLog("StackLayout.Insert(nil, ...) is forbidden") + return + } + + if layout.views == nil || index < 0 || index >= len(layout.views) { + layout.Append(view) + return + } + + stackID := layout.htmlID() + view.setParentID(stackID) + if index > 0 { + layout.views = append(layout.views[:index], append([]View{view}, layout.views[index:]...)...) + } else { + layout.views = append([]View{view}, layout.views...) + } + + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + + buffer.WriteString(``) + + session := layout.Session() + session.appendToInnerHTML(stackID, buffer.String()) + + if listener, ok := layout.changeListener[Content]; ok { + listener(layout, Content) + } +} + +// Remove removes view from list and return it +func (layout *stackLayoutData) RemoveView(index int) View { + if layout.views == nil { + layout.views = []View{} + return nil + } + + count := len(layout.views) + if index < 0 || index >= count { + return nil + } + + session := layout.Session() + view := layout.views[index] + view.setParentID("") + + if count == 1 { + layout.views = []View{} + } else if index == 0 { + layout.views = layout.views[1:] + } else if index == count-1 { + layout.views = layout.views[:index] + session.updateCSSProperty(layout.views[count-2].htmlID()+"page", "visibility", "visible") + } else { + layout.views = append(layout.views[:index], layout.views[index+1:]...) + } + + layout.Session().callFunc("removeView", view.htmlID()+"page") + + if listener, ok := layout.changeListener[Content]; ok { + listener(layout, Content) + } + return view +} + func (layout *stackLayoutData) Push(view View, onPushFinished func()) { if view == nil { ErrorLog("StackLayout.Push(nil, ....) is forbidden") diff --git a/viewsContainer.go b/viewsContainer.go index 7a9bca1..678719c 100644 --- a/viewsContainer.go +++ b/viewsContainer.go @@ -64,7 +64,7 @@ func (container *viewsContainerData) Views() []View { return []View{} } -func viewsContainerContentChanged(container *viewsContainerData) { +func viewsContainerContentChanged1(container *viewsContainerData) { updateInnerHTML(container.htmlID(), container.Session()) if listener, ok := container.changeListener[Content]; ok { listener(container, Content) @@ -81,24 +81,37 @@ func (container *viewsContainerData) Append(view View) { } else { container.views = append(container.views, view) } - viewsContainerContentChanged(container) + + buffer := allocStringBuilder() + defer freeStringBuilder(buffer) + + viewHTML(view, buffer, "") + container.Session().appendToInnerHTML(htmlID, buffer.String()) + + if listener, ok := container.changeListener[Content]; ok { + listener(container, Content) + } } } // Insert inserts a view to the "index" position in the list of a view children func (container *viewsContainerData) Insert(view View, index int) { if view != nil { - htmlID := container.htmlID() if container.views == nil || index < 0 || index >= len(container.views) { container.Append(view) - } else if index > 0 { - view.setParentID(htmlID) + return + } + + htmlID := container.htmlID() + view.setParentID(htmlID) + if index > 0 { container.views = append(container.views[:index], append([]View{view}, container.views[index:]...)...) - viewsContainerContentChanged(container) } else { - view.setParentID(htmlID) container.views = append([]View{view}, container.views...) - viewsContainerContentChanged(container) + } + updateInnerHTML(htmlID, container.Session()) + if listener, ok := container.changeListener[Content]; ok { + listener(container, Content) } } } @@ -125,7 +138,11 @@ func (container *viewsContainerData) RemoveView(index int) View { } view.setParentID("") - viewsContainerContentChanged(container) + + container.Session().callFunc("removeView", view.htmlID()) + if listener, ok := container.changeListener[Content]; ok { + listener(container, Content) + } return view }