MCPcopy Create free account
hub / github.com/angular/angular / updateSegmentGroupChildren

Function updateSegmentGroupChildren

packages/router/src/create_url_tree.ts:417–479  ·  view source on GitHub ↗
(
  segmentGroup: UrlSegmentGroup,
  startIndex: number,
  commands: readonly any[],
)

Source from the content-addressed store, hash-verified

415}
416
417function updateSegmentGroupChildren(
418 segmentGroup: UrlSegmentGroup,
419 startIndex: number,
420 commands: readonly any[],
421): UrlSegmentGroup {
422 if (commands.length === 0) {
423 return new UrlSegmentGroup(segmentGroup.segments, {});
424 } else {
425 const outlets = getOutlets(commands);
426 // Keyed by outlet name, which can be `__proto__`, so use a null-prototype map.
427 const children: {[key: string]: UrlSegmentGroup} = Object.create(null);
428 // If the set of commands applies to anything other than the primary outlet and the child
429 // segment is an empty path primary segment on its own, we want to apply the commands to the
430 // empty child path rather than here. The outcome is that the empty primary child is effectively
431 // removed from the final output UrlTree. Imagine the following config:
432 //
433 // {path: '', children: [{path: '**', outlet: 'popup'}]}.
434 //
435 // Navigation to /(popup:a) will activate the child outlet correctly Given a follow-up
436 // navigation with commands
437 // ['/', {outlets: {'popup': 'b'}}], we _would not_ want to apply the outlet commands to the
438 // root segment because that would result in
439 // //(popup:a)(popup:b) since the outlet command got applied one level above where it appears in
440 // the `ActivatedRoute` rather than updating the existing one.
441 //
442 // Because empty paths do not appear in the URL segments and the fact that the segments used in
443 // the output `UrlTree` are squashed to eliminate these empty paths where possible
444 // https://github.com/angular/angular/blob/13f10de40e25c6900ca55bd83b36bd533dacfa9e/packages/router/src/url_tree.ts#L755
445 // it can be hard to determine what is the right thing to do when applying commands to a
446 // `UrlSegmentGroup` that is created from an "unsquashed"/expanded `ActivatedRoute` tree.
447 // This code effectively "squashes" empty path primary routes when they have no siblings on
448 // the same level of the tree.
449 if (
450 Object.keys(outlets).some((o) => o !== PRIMARY_OUTLET) &&
451 segmentGroup.children[PRIMARY_OUTLET] &&
452 segmentGroup.numberOfChildren === 1 &&
453 segmentGroup.children[PRIMARY_OUTLET].segments.length === 0
454 ) {
455 const childrenOfEmptyChild = updateSegmentGroupChildren(
456 segmentGroup.children[PRIMARY_OUTLET],
457 startIndex,
458 commands,
459 );
460 return new UrlSegmentGroup(segmentGroup.segments, childrenOfEmptyChild.children);
461 }
462
463 Object.entries(outlets).forEach(([outlet, commands]) => {
464 if (typeof commands === 'string') {
465 commands = [commands];
466 }
467 if (commands !== null) {
468 children[outlet] = updateSegmentGroup(segmentGroup.children[outlet], startIndex, commands);
469 }
470 });
471
472 Object.entries(segmentGroup.children).forEach(([childOutlet, child]) => {
473 if (outlets[childOutlet] === undefined) {
474 children[childOutlet] = child;

Callers 2

updateSegmentGroupFunction · 0.85

Calls 7

getOutletsFunction · 0.85
updateSegmentGroupFunction · 0.85
someMethod · 0.80
createMethod · 0.65
keysMethod · 0.65
forEachMethod · 0.45
entriesMethod · 0.45

Tested by

no test coverage detected