Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Insert at on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Nov 28, 2020
1 parent 742e004 commit 189460b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions internal/page/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func (ctl *Control) ID() string {
return (*ctl)["i"].(string)
}

func (ctl *Control) At() int {
at, ok := (*ctl)["at"].(int)
if ok {
return at
}
return -1
}

// ParentID returns the ID of parent control.
func (ctl *Control) ParentID() string {
return (*ctl)["p"].(string)
Expand All @@ -71,6 +79,11 @@ func (ctl *Control) AddChildID(childID string) {
(*ctl)["c"] = append(childIds, childID)
}

func (ctl *Control) InsertChildID(childID string, at int) {
childIds, _ := (*ctl)["c"].([]string)
(*ctl)["c"] = utils.InsertString(childIds, childID, at)
}

func (ctl *Control) RemoveChild(childID string) {
childIds, _ := (*ctl)["c"].([]string)
(*ctl)["c"] = utils.RemoveString(childIds, childID)
Expand Down
9 changes: 8 additions & 1 deletion internal/page/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func add(session *Session, cmd command.Command) (result string, err error) {

if parentAt != -1 {
batchItem.Control.SetAttr("at", parentAt)
topParentAt++
}

for k, v := range batchItem.Command.Attrs {
Expand All @@ -181,6 +182,8 @@ func add(session *Session, cmd command.Command) (result string, err error) {
payload.Controls = append(payload.Controls, batchItem.Control)
}

//log.Println("CONTROLS:", utils.ToJSON(session.Controls))

// broadcast new controls to all connected web clients
session.broadcastCommandToWebClients(NewMessage(AddPageControlsAction, payload))
return strings.Join(ids, " "), nil
Expand Down Expand Up @@ -352,7 +355,11 @@ func (session *Session) AddControl(ctrl *Control) error {
}

// update parent's childIds
parentctrl.AddChildID(ctrl.ID())
if at := ctrl.At(); at != -1 {
parentctrl.InsertChildID(ctrl.ID(), at)
} else {
parentctrl.AddChildID(ctrl.ID())
}
}

return nil
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func ContainsString(arr []string, str string) bool {
return false
}

// https://stackoverflow.com/questions/46128016/insert-a-value-in-a-slice-at-a-given-index
func InsertString(arr []string, value string, index int) []string {
if index >= len(arr) { // nil or empty slice or after last element
return append(arr, value)
}
arr = append(arr[:index+1], arr[index:]...) // index < len(a)
arr[index] = value
return arr
}

func RemoveString(arr []string, str string) []string {
for i, v := range arr {
if v == str {
Expand Down

0 comments on commit 189460b

Please sign in to comment.