Code coverage report for app/components/breadcrumb/breadcrumb.controller.js

Statements: 100% (53 / 53)      Branches: 100% (18 / 18)      Functions: 100% (10 / 10)      Lines: 100% (27 / 27)      Ignored: 2 statements, 4 branches     

All files » app/components/breadcrumb/ » breadcrumb.controller.js
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                                10× 10× 10× 12× 12×     10×              
class BreadcrumbController {
    constructor ($state, $rootScope) {
        Object.assign(this, {$state, $rootScope});
 
        this._applyNewBreadcrumb(this.$state.current, this.$state.params);
        this.$rootScope.$on('$stateChangeSuccess',
            (event, toState, toParams) => {
                this._applyNewBreadcrumb(toState, toParams);
            });
    }
 
    _applyNewBreadcrumb (state, params) {
        this.breadcrumbs = [];
        const curName = state.name;
        const parentStateNames = this._getAncestorStates(curName);
        parentStateNames.forEach((name) => {
            const stateConfig = this.$state.get(name);
            if (stateConfig.abstract) {
                return;
            }
            const breadcrumb = {
                link: name,
                text: stateConfig.breadcrumb
            };
            this.breadcrumbs.push(breadcrumb);
        });
        const length = this.breadcrumbs.length;
        if (params && length > 0) {
            const lastBreadcrumb = this.breadcrumbs[length - 1];
            lastBreadcrumb.link = `${lastBreadcrumb.link}(${JSON.stringify(params)})`;
        }
    }
 
    _getAncestorStates (stateName) {
        const ancestors = [];
        const pieces = stateName.split('.');
        if (pieces.length > 1) {
            for (let i = 1; i < pieces.length; i++) {
                const name = pieces.slice(0, i + 1);
                ancestors.push(name.join('.'));
            }
        }
        return ancestors;
    }
}
 
BreadcrumbController.$inject = ['$state', '$rootScope'];
 
export default BreadcrumbController;