All files / lib/alert-dialog alert-dialog.component.ts

75.69% Statements 190/251
83.33% Branches 15/18
66.66% Functions 8/12
75.69% Lines 190/251

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 2521x 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 1x 1x 1x 1x 1x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 127x 1x 1x 127x 127x 127x 127x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 666x 666x 1x 1x 1x         1x 1x 1x 35x 35x   35x 1x 1x 1x 86x 86x 86x 86x 1x 1x 126x 126x 1x 1x 1x                                                       1x 1x                                               1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  Output,
  QueryList,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { LinkEvent } from '../utils/event.types';
import {
  FocusableElementOwner,
  FormattedTextContent,
  Item,
} from '../utils/util.types';
import { SoundType, itemTrack, playSound } from '../utils/utilities';
 
import { ButtonComponent } from '../button/button.component';
 
export type AlertContent =
  | FormattedTextContent
  | {
      type: 'link';
      text: string;
      url: string;
      handler?: (evt: LinkEvent) => void;
    }
  | string;
 
let UNIQUE_ID = 0;
 
const SHOW_SUCCESS_ICON_DURATION = 3000;
 
/**
 * `gc-alert-dialog` are windows containing contextual information, tasks, or workflows.
 */
@Component({
  selector: 'gc-alert-dialog',
  templateUrl: './alert-dialog.component.html',
  styleUrls: ['./alert-dialog.component.css'],
  standalone: false,
})
export class AlertDialogComponent implements FocusableElementOwner {
  /**
   * Label displayed in the header
   */
  @Input()
  public label = '';
 
  /**
   * Description displayed in the header.
   * Can contain a string or an array of strings, formatted text, and/or links
   * If an array is provided, each entry is rendered in a separate div element
   */
  @Input()
  public description: string | readonly AlertContent[] = '';
 
  /**
   * Variant of the alert dialog
   */
  @Input()
  public variant: 'info' | 'warning' = 'info';
 
  /**
   * Actions shown in the alert dialog
   */
  @Input()
  public actions: readonly Item[] = [];
 
  /**
   * Primary action shown in the alert dialog
   */
  @Input()
  public primaryAction!: Item;
 
  /**
   * Whether message is focusable in the alert dialog
   * @deprecated
   */
  @Input()
  public isMessageFocusable = false;
 
  /**
   * Control that a copy button is shown / not shown
   */
  @Input()
  public showCopyOption = false;
 
  /**
   * Event emitted when an item is selected
   */
  @Output()
  public readonly onItemAction: EventEmitter<Item> = new EventEmitter<Item>();
 
  /**
   * @ignore
   */
  @ViewChild('primaryButton', { read: ButtonComponent })
  private primaryButton?: ButtonComponent;
 
  /**
   * @ignore
   */
  @ViewChildren('button', { read: ButtonComponent })
  private buttons?: QueryList<ButtonComponent>;
 
  @ViewChild('content')
  private content?: ElementRef<HTMLElement>;
 
  /**
   * @ignore
   */
  public readonly _labelId: string;
 
  /**
   * @ignore
   */
  public readonly _descId: string;
 
  /**
   * @ignore
   */
  protected readonly _id: string;
 
  /** @ignore */
  protected copySucceeded = false;
 
  /** @ignore */
  private copySucceedResetTimeout?: ReturnType<typeof setTimeout>;
 
  constructor() {
    this._id = 'gc-alert-' + (UNIQUE_ID++).toString();
    this._labelId = this._id + '-label';
    this._descId = this._id + '-desc';
  }
 
  public focusChild(): boolean {
    if (this.buttons) {
      const firstNonDisabledButton = this.buttons.find(btn => !btn.disabled);
 
      if (this.primaryButton && !this.primaryButton.disabled) {
        return this.primaryButton.focusChild();
      } else if (firstNonDisabledButton) {
        return firstNonDisabledButton.focusChild();
      } else {
        return false;
      }
    }
 
    return false;
  }
 
  /**
   * @ignore
   */
  protected _handleHeaderAction(item: Item): void {
    this.onItemAction.emit(item);
  }
 
  /** @ignore */
  protected _isSingleValuedDescription() {
    return typeof this.description === 'string';
  }
 
  /** @ignore */
  protected _singleValueDescription(): string {
    return typeof this.description === 'string'
      ? this.description
      : 'ERROR: multi valued';
  }
 
  /** @ignore */
  protected _multiValueDescription(): readonly AlertContent[] {
    return typeof this.description !== 'string'
      ? this.description
      : ['ERROR: single valued'];
  }
 
  /** @ignore */
  protected _isFormattedType(
    content: AlertContent | string,
  ): content is Exclude<AlertContent, string> {
    return typeof content !== 'string';
  }
 
  protected _itemTrackBy(item: Item): string {
    return itemTrack(item);
  }
 
  /** @ignore */
  protected async copyToClipboard(): Promise<void> {
    const plainText = this.convertTextForCopy();

    // Firefox does not yet support that
    if (typeof ClipboardItem === 'undefined' || this.content === undefined) {
      await navigator.clipboard.writeText(plainText);
    } else {
      const htmlData = new Blob([this.content.nativeElement.innerHTML], {
        type: 'text/html',
      });
      const textData = new Blob([plainText], { type: 'text/plain' });

      const item = new ClipboardItem({
        [htmlData.type]: htmlData,
        [textData.type]: textData,
      });

      await navigator.clipboard.write([item]);
    }

    this.copySucceeded = true;
    void playSound(SoundType.SUCCESS);

    clearTimeout(this.copySucceedResetTimeout);
    this.copySucceedResetTimeout = setTimeout(() => {
      this.copySucceeded = false;
    }, SHOW_SUCCESS_ICON_DURATION);
  }
 
  private convertTextForCopy(): string {
    let stringifiedDescription = '';
    if (this._isSingleValuedDescription()) {
      stringifiedDescription = this.description.toString();
    } else {
      this._multiValueDescription().forEach(element => {
        if (this._isFormattedType(element)) {
          if (element.type === 'formattedText') {
            stringifiedDescription += element.content
              .split(/\r?\n/)
              .map(l => l.replace(/^\*/, '-'))
              .map(l => l.replace(/\*/g, ''))
              .join('\n');
          } else {
            stringifiedDescription += `Link: ${element.text} (${element.url})`;
          }
        } else {
          stringifiedDescription += element;
        }
        stringifiedDescription += '\n';
      });
    }
    return stringifiedDescription;
  }
}