All files / src/app/core/routing/content-page content-page.route.ts

87.5% Statements 28/32
75% Branches 6/8
62.5% Functions 5/8
92.85% Lines 26/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 8027x 27x         27x 27x                 77x       27x               27x   4x           5x 4x 3x 3x 3x 3x   3x         1x                 27x 41x 2x     39x     77x     39x 37x   39x   39x     27x      
import { UrlMatchResult, UrlSegment } from '@angular/router';
import { MonoTypeOperatorFunction, filter } from 'rxjs';
 
import { ContentPageTreeView } from 'ish-core/models/content-page-tree-view/content-page-tree-view.model';
import { ContentPageTreeElement } from 'ish-core/models/content-page-tree/content-page-tree.model';
import { CoreState } from 'ish-core/store/core/core-store';
import { selectRouteParam } from 'ish-core/store/core/router';
import { sanitizeSlugData } from 'ish-core/utils/routing';
 
/**
 * generate a localized content page slug
 *
 * @param page content page element for slug
 * @returns localized, formatted content page slug
 */
function generateLocalizedContentPageSlug(page: ContentPageTreeElement) {
  return sanitizeSlugData(page?.name);
}
 
// matcher to check if a given url is a content page route
const contentRouteFormat = /^\/(?!page\/.*$)(.*-)?pg(.*)$/;
 
/**
 * check if given url is a content page route
 *
 * @param segments current url segments
 * @returns match result if given url is a content page route or not
 */
export function matchContentRoute(segments: UrlSegment[]): UrlMatchResult {
  // compatibility to old routes
  Iif (segments && segments.length === 2 && segments[0].path === 'page') {
    return {
      consumed: [],
    };
  }
 
  const url = `/${segments.map(s => s.path).join('/')}`;
  if (contentRouteFormat.test(url)) {
    const match = contentRouteFormat.exec(url);
    const posParams: { [id: string]: UrlSegment } = {};
    if (match[2]) {
      posParams.contentPageId = new UrlSegment(match[2], {});
    }
    return {
      consumed: [],
      posParams,
    };
  }
  return;
}
 
/**
 * generate a localized content page url from a content page
 *
 * @param page content page
 * @returns localized content page url
 */
export function generateContentPageUrl(page: ContentPageTreeView): string {
  if (!page) {
    return '/';
  }
 
  let route = '/';
 
  // generate for each path element from the given content page hierarchy a content page slug and join them together to a complete route
  route += page.pathElements?.map(p => generateLocalizedContentPageSlug(p)).join('/');
 
  // add to content page route the content page identifier
  if (route !== '/') {
    route += '-';
  }
  route += `pg${page.contentPageId}`;
 
  return route;
}
 
export function ofContentPageUrl(): MonoTypeOperatorFunction<{}> {
  return source$ => source$.pipe(filter((state: CoreState) => !!selectRouteParam('contentPageId')(state)));
}