Ejemplo n.º 1
0
        /// <summary>
        /// 展开某个节点
        /// </summary>
        /// <param name="treeItem"></param>
        public async Task ExpandAsync(TreeItemBase treeItem, bool autoRefresh = true)
        {
            if (treeItem.Expanded)
            {
                return;
            }
            treeItem.Expanded = true;
            if (LoadFullTree != null && LoadFullTree.Value)
            {
                var children = FindChildren(treeItem);
                await ExpandAsync(children, false);

                if (autoRefresh)
                {
                    Refresh();
                }
                return;
            }
            if (!string.IsNullOrWhiteSpace(Url))
            {
                EnsureUrl();
                var responseObj = (IEnumerable)await httpClient.GetFromJsonAsync(Url + treeItem.Id, typeof(List <>).MakeGenericType(ItemType));

                var newTreeNodes = responseObj.Cast <TreeItemBase>().ToArray();
                DataSource.RemoveAll(x => newTreeNodes.Any(y => y.Id == x.Id));
                DataSource.AddRange(newTreeNodes);
                var rootNodes = FindChildren(new TreeItemModel()
                {
                    Children = items
                });
                Initilize(rootNodes, null);
                await AutoExpandAllAsync();
            }
            Refresh();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 展开某个节点,如果这个节点当前没有子节点,则动态加载子节点
        /// </summary>
        /// <param name="item"></param>
        /// <param name="forceLoad">是否强制加载新数据</param>
        public async Task ExpandAsync(TreeItemBase item, bool forceLoad = false)
        {
            if (string.IsNullOrWhiteSpace(Url))
            {
                ExceptionHelper.Throw(ExceptionHelper.ExpandOnlyHasUrl, "展开某个节点只支持异步加载");
            }
            ResetSelectAllStatus();
            item.Expanded = true;
            var finalChildren = GetAllChildren(item);

            if (forceLoad || !finalChildren.Any())
            {
                if (!forceLoad)
                {
                    item.HasChildren = true;
                }
                if (finalChildren.Count != rows.RemoveAll(finalChildren.Contains))
                {
                    ExceptionHelper.Throw(ExceptionHelper.ClearChildFailure, "清空子节点失败");
                }
                DataSource = rows;
                await LoadChildrenAsync(item, AutoExpandAll);

                Refresh();
                return;
            }

            LocalExpand(item, finalChildren);
            Refresh();
        }
Ejemplo n.º 3
0
        private async Task LoadChildrenAsync(TreeItemBase row, bool autoExpand)
        {
            row.Expanded  = true;
            row.IsLoading = true;
            Refresh();
            var newDataSource = await FetchChildrenAsync(row);

            row.IsLoading = false;
            var enumerbale = DataSource as IEnumerable;
            var startIndex = 0;

            foreach (var item in enumerbale)
            {
                if (item == row)
                {
                    break;
                }
                startIndex++;
            }
            var oldDataSource = enumerbale.Cast <object>().ToList();

            oldDataSource.InsertRange(startIndex + 1, (newDataSource as IEnumerable).Cast <object>());
            DataSource = oldDataSource;
            await InitlizeDataSourceAsync(row.Level.Value + 1, "up", autoExpand);

            if (DataSourceChanged.HasDelegate)
            {
                await DataSourceChanged.InvokeAsync(DataSource);
            }
            SyncFieldValue();
        }
Ejemplo n.º 4
0
 private List<TreeItemBase> FindChildren(TreeItemBase treeItem)
 {
     switch (DataFormat)
     {
         case DataFormat.List:
             return items.Where(x => x.ParentId == treeItem.Id).ToList();
         case DataFormat.Children:
             return treeItem.Children ?? new List<TreeItemBase>();
     }
     return new List<TreeItemBase>();
 }
Ejemplo n.º 5
0
 private async Task ToggleAsync(TreeItemBase item)
 {
     if (item.Expanded)
     {
         item.Expanded = false;
         Refresh();
     }
     else
     {
         await ExpandAsync(item);
     }
 }
Ejemplo n.º 6
0
        private List <TreeItemBase> FindChildren(TreeItemBase child)
        {
            var results   = new List <TreeItemBase>();
            var treeItems = rows.Cast <TreeItemBase>().Where(x => x.ParentId == child.Id).ToArray();

            foreach (var childTreeItem in treeItems)
            {
                results.Add(childTreeItem);
                results.AddRange(FindChildren(childTreeItem));
            }
            return(results);
        }
Ejemplo n.º 7
0
 private void Initilize(List <TreeItemBase> nodes, TreeItemBase parentNode)
 {
     foreach (var node in nodes)
     {
         node.Level     = (parentNode?.Level ?? -1) + 1;
         node.TextPaths = parentNode?.TextPaths == null ? new Dictionary <int, string>() : new Dictionary <int, string>(parentNode.TextPaths);
         if (!node.TextPaths.ContainsKey(node.Id))
         {
             node.TextPaths.Add(node.Id, node.Text);
         }
         Initilize(FindChildren(node), node);
     }
 }
Ejemplo n.º 8
0
        private List <TreeItemBase> GetAllChildren(TreeItemBase treeRow)
        {
            var treeRows      = rows.Cast <TreeItemBase>().ToArray();
            var children      = treeRows.Where(x => x.ParentId == treeRow.Id).ToList();
            var finalChildren = children.ToList();

            foreach (var item in children)
            {
                finalChildren.AddRange(FindChildren(item));
            }

            return(finalChildren);
        }
Ejemplo n.º 9
0
        private async Task ForceExpandAsync(TreeItemBase item)
        {
            if (string.IsNullOrWhiteSpace(Url))
            {
                ExceptionHelper.Throw(ExceptionHelper.ExpandOnlyHasUrl, "展开某个节点只支持异步加载");
            }
            item.Expanded = true;
            var finalChildren = GetAllChildren(item);

            item.HasChildren = true;
            rows.RemoveAll(finalChildren.Contains);
            await LoadChildrenAsync(item, AutoExpandAll);

            Refresh();
        }
Ejemplo n.º 10
0
 private async Task SelectNodeAsync(TreeItemBase treeNode)
 {
     selectedNodes.Add(treeNode);
     if (SelectedNodesChanged.HasDelegate)
     {
         _ = SelectedNodesChanged.InvokeAsync(selectedNodes);
     }
     if (Select != null)
     {
         await Select.OnInternalSelectAsync(new SelectResultModel <int?>()
         {
             Text = treeNode.TextPath,
             Key  = treeNode.Id
         });
     }
 }
Ejemplo n.º 11
0
        private async Task <IEnumerable> FetchChildrenAsync(TreeItemBase treeRow)
        {
            treeRow.Direction = "right";
            if (!Url.EndsWith("/"))
            {
                Url += "/";
            }

            var newDataSource = (IEnumerable)await httpClient.GetFromJsonAsync(Url + treeRow.Id, typeof(List <>).MakeGenericType(DataType));

            if (!newDataSource.Cast <object>().Any())
            {
                treeRow.HasChildren = false;
            }
            return(newDataSource);
        }
Ejemplo n.º 12
0
 private void CalculateLevel(List <TreeItemBase> treeItems, TreeItemBase parentNode, TreeItemBase[] allTreeItems)
 {
     foreach (var treeRow in treeItems)
     {
         if (treeRow.Level != null)
         {
             return;
         }
         treeRow.Level     = (parentNode?.Level ?? -1) + 1;
         treeRow.TextPaths = parentNode?.TextPaths == null ? new Dictionary <int, string>() : new Dictionary <int, string>(parentNode.TextPaths);
         if (!treeRow.TextPaths.ContainsKey(treeRow.Id))
         {
             treeRow.TextPaths.Add(treeRow.Id, treeRow.Text);
         }
         var children = allTreeItems.Where(x => x.ParentId == treeRow.Id).ToList();
         CalculateLevel(children, treeRow, allTreeItems);
     }
 }
Ejemplo n.º 13
0
 private void LocalExpand(TreeItemBase treeRow, List <TreeItemBase> finalChildren)
 {
     hiddenRows.RemoveAll(finalChildren.Contains);
     treeRow.Direction = "right";
     treeRow.Expanded  = true;
 }
Ejemplo n.º 14
0
 public async Task RefreshNodeAsync(TreeItemBase item)
 {
     await ExpandAsync(item, true);
 }