public refreshExpandedChildrenRecursively()

in src/Sfx/App/Scripts/ViewModels/TreeNodeGroupViewModel.ts [139:203]


        public refreshExpandedChildrenRecursively(): angular.IPromise<any> {
            if (!this.childrenQuery || !this._isExpanded) {
                return this._tree.$q.when(true);
            }

            return this.childrenQuery().then((response) => {
                let children = this.children;

                // Remove nodes that no longer exist
                for (let i = 0; i < children.length; i++) {
                    let node = children[i];
                    if (!node.nodeId) {
                        continue;
                    }

                    let exists = this.exists(response, node, (a, b) => a.nodeId === b.nodeId);
                    if (!exists) {
                        // Unselect removed node
                        if (this._tree.selectedNode && (node === this._tree.selectedNode || node.isParentOf(this._tree.selectedNode))) {
                            // Select the parent node instead
                            node.parent.select();
                        }
                        children.splice(i, 1);
                        i--;
                    }
                }

                // Clone children before adding new, to refresh recursively
                let childrenToRefresh = children.slice(0);

                // Add new nodes / update existing
                for (let i = 0; i < response.length; i++) {
                    let respNode = response[i];
                    if (!respNode.nodeId) {
                        continue;
                    }

                    let existing = this.exists(children, respNode, (a, b) => a.nodeId === b.nodeId ? a : null);
                    if (existing) {
                        // Update existing
                        existing.update(respNode);
                    } else {
                        // Add new
                        let newNode = new TreeNodeViewModel(this._tree, respNode, this.owningNode);

                        // Find the correct index in the sorted array
                        let index = _.sortedIndexBy(children, newNode, (item) => item.sortBy());
                        children.splice(index, 0, newNode);
                    }
                }

                // Recursively refresh children
                let promises: angular.IPromise<void>[] = [];
                childrenToRefresh.forEach(child => {
                    promises.push(child.childGroupViewModel.refreshExpandedChildrenRecursively());
                });

                // Update paging settings
                if (this.owningNode && this.owningNode.listSettings) {
                    this.owningNode.listSettings.count = this.children.length;
                }

                return this._tree.$q.all(promises);
            });
        }