| 46 | return nil |
| 47 | } |
| 48 | func NewRoot(list []*catalogue.Catalogue) *Root { |
| 49 | m := make(map[string]*Group) |
| 50 | l := make([]*Group, 0, len(list)) |
| 51 | for _, i := range list { |
| 52 | g := &Group{ |
| 53 | Uuid: i.Id, |
| 54 | Parent: i.Parent, |
| 55 | Name: i.Name, |
| 56 | Depth: 0, |
| 57 | children: nil, |
| 58 | sort: i.Sort, |
| 59 | } |
| 60 | l = append(l, g) |
| 61 | m[g.Uuid] = g |
| 62 | } |
| 63 | parentMap := make(map[string][]string) |
| 64 | root := new(Group) |
| 65 | for _, i := range l { |
| 66 | if i.Parent != "" { |
| 67 | p, has := m[i.Parent] |
| 68 | if !has { |
| 69 | i.Parent = "" |
| 70 | root.children = append(root.children, i) |
| 71 | } else { |
| 72 | p.children = append(p.children, i) |
| 73 | p.parents = getParents(i.Parent, m, parentMap) |
| 74 | } |
| 75 | |
| 76 | } else { |
| 77 | root.children = append(root.children, i) |
| 78 | } |
| 79 | } |
| 80 | root.ResetDepth(-1) |
| 81 | sort.Sort(Groups(l)) |
| 82 | return &Root{nodes: m, list: l} |
| 83 | } |
| 84 | |
| 85 | func getParents(parentID string, nodeMap map[string]*Group, parentMap map[string][]string) []string { |
| 86 | if parentID == "" { |