All files / lib/internal/sidebar sidebar.component.ts

98.73% Statements 311/315
94.44% Branches 34/36
100% Functions 17/17
98.73% Lines 311/315

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 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 3161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 1x 1x 14x 12x 14x 14x 14x 14x 14x 5x 5x 5x 5x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x       14x 14x 14x 14x 9x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 6x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 6x 6x 5x 6x 6x 1x 1x 1x 12x 5x 5x 12x 12x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 4x 1x 1x 4x 4x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 1x 9x 9x 9x 9x 9x 9x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnDestroy,
  Output,
  Type,
  ViewChild,
  ViewContainerRef,
} from '@angular/core';
 
import { observeSize, subscribe } from '../../utils/internal-utils';
import { Callback } from '../../utils/util.types';
 
import {
  DynamicHeadingLevelPassthroughService,
  DynamicHeadingLevelService,
} from '../../dynamic-heading/dynamic-heading-level.service';
import { AutoFocusAreaDirective } from '../auto-focus-area.directive';
 
/**
 * The sidebar component is designed to add side content to a fullscreen app.
 */
@Component({
  selector: 'gc-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css'],
  providers: [DynamicHeadingLevelService],
  viewProviders: [
    {
      provide: DynamicHeadingLevelService,
      useClass: DynamicHeadingLevelPassthroughService,
    },
  ],
})
export class SidebarComponent implements AfterViewInit, OnDestroy {
  /**
   * Label that shows on the sidebar.
   */
  @Input()
  public label = '';
 
  /**
   * Decides if the sidebar will be placed on the left or right side of the parent element.
   */
  @Input()
  public position: 'left' | 'right' = 'right';
 
  /**
   * Width of the sidebar in pixels.
   */
  @Input()
  public width?: number;
 
  /**
   * Top offset of the sidebar in pixels.
   */
  @Input()
  @HostBinding('style.--gc-sidebar-top-offset.px')
  public topOffset?: number;
 
  /**
   * Component that the sidebar displays as it's content.
   */
  @Input()
  public content?: Type<unknown>;
 
  /**
   * Decides if the sidebar is animated when opening or not.
   */
  @Input()
  public animateOnOpen = true;
 
  /**
   * Decides if the sidebar is animated when closing or not.
   */
  @Input()
  public animateOnClose = true;
 
  /**
   * Informs about status of the sidebar state.
   */
  @Output()
  public readonly onClosed: EventEmitter<this> = new EventEmitter<this>();
 
  /**
   * @ignore
   */
  @ViewChild('sidebar')
  private sidebar!: ElementRef<HTMLDivElement>;
 
  /**
   * @ignore
   */
  @ViewChild('content', { read: ViewContainerRef, static: true })
  private contentPlaceholder!: ViewContainerRef;
 
  /** @ignore */
  @ViewChild('contentContainer')
  private contentContainer!: ElementRef<HTMLElement>;
 
  /**
   * Decides if the status of the sidebar is open or not open.
   */
  @Input()
  get isOpen(): boolean {
    return this._isOpen;
  }
 
  set isOpen(isOpen: boolean) {
    if ((isOpen && this.animateOnOpen) || (!isOpen && this.animateOnClose)) {
      this._animationEnabled = true;
    }
 
    this._isOpen = isOpen;
 
    if (!isOpen) {
      if (!this.animateOnClose) {
        // fire the event when closing without an animation,
        // when it's animated this is done via the transition event
        this.onClosed.emit(this);
      } else {
        // when the sidebar is closed immediately after opening it (with an animation enabled),
        // transition events may not fire:
        // * there may be no new transition for the closure started in case the open transition didn't actually
        //   move the element yet
        // * there may be no transition at all when the closing happens before the open transition starts (may need a
        //   few ticks before the start event would be triggered)
        // => async closure whith minimal delay which is canceled if a new transition starts, as fallback
        // for closure if no new transition follows
        this.closeEventTimeout = setTimeout(() => {
          this._animationEnabled = false;
          this.onClosed.emit(this);
          this.closeEventTimeout = undefined;
        }, 75);
      }
    }
    if (isOpen) {
      setTimeout(() => this.moveFocusIntoContent());
    }
  }
 
  /**
   * @ignore
   */
  public _animationEnabled = false;
 
  /**
   * @ignore
   */
  public _fullWidth = false;
 
  /**
   * @ignore
   */
  public _isOpen = false;
 
  /**
   * @ignore
   */
  private resizeObserverCleanup?: Callback;
 
  /** @ignore */
  private clickHandlerCleanup?: Callback;
 
  /** @ignore */
  private closeEventTimeout?: ReturnType<typeof setTimeout>;
 
  constructor(
    private zone: NgZone,
    private el: ElementRef<HTMLElement>,
  ) {}
 
  /**
   * @ignore
   */
  ngAfterViewInit(): void {
    this.checkWidth();
    this.setWidth();
    this.listenForModalOverlayClicks();
 
    if (this.content) {
      this.contentPlaceholder.clear();
      this.contentPlaceholder.createComponent(this.content);
    }
 
    this.resizeObserverCleanup = observeSize(
      new ElementRef(document.documentElement),
      this.zone,
      this.resizeObserverCallback.bind(this),
    );
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    if (this.resizeObserverCleanup) {
      this.resizeObserverCleanup();
    }
 
    if (this.clickHandlerCleanup) {
      this.clickHandlerCleanup();
    }
 
    if (this.closeEventTimeout) {
      clearTimeout(this.closeEventTimeout);
    }
  }
 
  /**
   * @ignore
   */
  public _closeSidebar() {
    this.isOpen = false;
  }
 
  /**
   * @ignore
   */
  public _onKeyDown(event: KeyboardEvent): void {
    const key = event.key;
 
    if (key === 'Escape' && this.isOpen) {
      this._closeSidebar();
    }
  }
 
  /**
   * @ignore
   */
  public _onTransitionEnd() {
    this._animationEnabled = false;
    if (!this.isOpen) {
      this.onClosed.emit(this);
    }
  }
 
  /** @ignore */
  public _onTransitionStart() {
    if (this.closeEventTimeout) {
      clearTimeout(this.closeEventTimeout);
      this.closeEventTimeout = undefined;
    }
  }
 
  /**
   * @ignore
   */
  private resizeObserverCallback() {
    this.checkWidth();
  }
 
  /**
   * @ignore
   */
  private listenForModalOverlayClicks(): void {
    if (this.el.nativeElement.parentElement != null) {
      this.clickHandlerCleanup = subscribe(
        this.el.nativeElement.parentElement,
        'click',
        this.closeSidebarOnModalClick.bind(this),
      );
    }
  }
 
  /**
   * @ignore
   */
  private closeSidebarOnModalClick(event: MouseEvent): void {
    const modalElement = this.el.nativeElement.parentElement;
    if (event.target === modalElement) {
      this._closeSidebar();
    }
  }
 
  /**
   * @ignore
   */
  private checkWidth(): void {
    const clientWidth = window.document.documentElement.clientWidth;
    if (clientWidth < 480 || (this.width ?? 0) > clientWidth) {
      this._fullWidth = true;
    } else {
      this._fullWidth = false;
    }
  }
 
  /**
   * @ignore
   */
  private setWidth(): void {
    if (this.width) {
      this.sidebar.nativeElement.style.width = this.width.toString() + 'px';
    }
  }
 
  /** @ignore */
  private moveFocusIntoContent(): void {
    // cannot apply the directive directly as the element is off-screen/hidden initially
    // due to the animation -> need to wait for it to be visible before moving focus works
    AutoFocusAreaDirective.focusInitialElement(
      this.contentContainer.nativeElement,
    );
  }
}