all files / common/core/grouping/ grouping.service.ts

93.85% Statements 61/65
87.5% Branches 35/40
90.48% Functions 19/21
94.64% Lines 53/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                      72× 54×     53×   21× 26× 21×     32× 32×   26×       63× 63×     63× 63× 63×       63×                         89× 89× 89×   24189×   24189×     24189×     24189×   24189× 645× 645×   837×     645×     24189× 24189×   645× 645×   89×   88×                      
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { from as observableFrom ,  Observable ,  of as observableOf } from 'rxjs';
import { map ,  reduce ,  switchMap ,  tap } from 'rxjs/operators';
import { IItemTree } from '../item-list/item-tree';
import { SortingService } from '../sorting/sorting.service';
import { IGroupInfo } from './group-infos';
 
/** Service de regroupement d'un tableau de modèles */
export class GroupingService {
    /** Groupe les éléments de la liste hierarchique spécifiée à partir du niveau spécifié, et en fonction du modèle de groupe spécifié
     * @param tree Liste à trier.
     * @param groupInfos Modèle de groupe à appliquer.
     * @param childrenField Champs à utiliser comme collection des enfants d'un parent.
     * @param depth Niveau à partir duquel le modèle de regroupement doit être appliqué.
     * @return Observable résolu par la fonction.
     */
    public group$(tree: any[], groupInfos: IGroupInfo[] | IGroupInfo, childrenField = 'items'): Observable<any[]> {
        if (!tree || tree.length === 0 || !groupInfos) {
            return observableOf(tree);
        }
 
        if (groupInfos instanceof Array) {
            // Create a observable stream with a sequence for each groupinfos.
            let result$ = observableOf(tree);
            groupInfos.forEach((groupInfo) => result$ = result$.pipe(switchMap((t) => this.group$(t, groupInfo, childrenField))));
            return result$;
        } else {
            // Group the tree with the current groupInfo
            const groupInfo = groupInfos as IGroupInfo;
            if (!tree[0][childrenField]) {
                // No children, group the tree
                return this.groupChildren$(tree, groupInfo, 0, childrenField);
            }
 
            const groupTree$: any = (t: any[], curDepth: number) => {
                return observableFrom(t).pipe(
                    switchMap((treeItem) => {
                        const children = treeItem[childrenField];
                        Iif (children[0] && children[0][childrenField]) {
                            return groupTree$(children, curDepth + 1).map(() => treeItem);
                        } else {
                            return this.groupChildren$(children, groupInfo, curDepth, childrenField).pipe(map((groupedChildren) => {
                                treeItem[childrenField] = groupedChildren;
                                return treeItem;
                            }));
                        }
                    }),
                    reduce((acc: any[], cur: any) => [...acc, cur], []));
            };
 
            // If the tree has chidren, group only the last level items
            return groupTree$(tree, 1);
        }
    }
 
    /**
     * @deprecated > 06.11.2017
     */
    public group(tree: any[], groupInfos: IGroupInfo[] | IGroupInfo, childrenField?: string) {
        return this.group$(tree, groupInfos, childrenField).toPromise();
    }
 
    protected groupChildren$(list: any[], groupInfo: IGroupInfo, _depth: number, childrenField: string): Observable<any[]> {
        return observableOf(list).pipe(
            switchMap((l) => l),
            reduce((groups: { [groupby: string]: IItemTree }, item) => {
                let groupedBy = typeof groupInfo.groupByField === 'function' ? groupInfo.groupByField(item) : item[groupInfo.groupByField];
 
                if (typeof item[groupedBy] === 'function') {
                    groupedBy = item[groupedBy]();
                }
 
                if (!groupedBy) {
                    groupedBy = this.getTextValue(item);
                }
 
                let parent = groups[groupedBy];
 
                if (!parent) {
                    const groupLabel = groupInfo.groupTextField ? (typeof groupInfo.groupTextField === 'function' ? groupInfo.groupTextField(item) : item[groupInfo.groupTextField]) : groupedBy;
                    parent = groups[groupedBy] = {
                        depth: _depth,
                        toString: () => groupLabel,
                        $text: groupLabel,
                    } as IItemTree;
                    (<any>parent)[childrenField] = [];
                }
 
                (<any>parent)[childrenField].push(item);
                return groups;
            }, {}),
            map((grps: { [groupby: string]: any }) => Object.keys(grps).map((key) => grps[key])),
            tap((groupedChildren) => groupedChildren.forEach((parent) => parent.sortField = (groupInfo.sortInfos && groupInfo.sortInfos.name) || 'toString')),
            switchMap((groupedChildren) => {
                if (groupInfo.sortInfos) {
                    const sortingService = new SortingService();
                    return sortingService.sort$(groupedChildren, groupInfo.sortInfos);
                } else {
                    return observableOf(groupedChildren);
                }
            }));
    }
 
    private getTextValue(value: any) {
        Iif (!value) {
            return '';
        } else {
            if (value.displayName) {
                return typeof value.displayName === 'string' ? value.displayName : value.displayName();
            } else Eif (typeof value.toString === 'function') {
                return value.toString();
            }
        }
    }
}