File

projects/i-components/src/lib/components/validation-message-service/validation-message-service.component.ts

Description

Validation Message service holder component

Implements

AfterViewInit

Metadata

selector wm-validation-message-service
styleUrls ./validation-message-service.component.scss
templateUrl ./validation-message-service.component.html

Index

Properties
Methods
HostBindings

Constructor

constructor(elementRef: ElementRef)

Creates an instance of ValidationMessageServiceComponent.

Parameters :
Name Type Optional
elementRef ElementRef No

HostBindings

class
Type : string
Default value : 'validation-message-service'

Decorating the component with a class to allow identification in the DOM

style.left
Type : string
Default value : 'calc(50% - 11em)'

Binding to locate the message on left

style.top
Type : string
Default value : 'calc(50% - 11em)'

Binding to locate the message on top

Methods

Private calculatePosition
calculatePosition()

Calculates the position to be rendered

Returns : void
ngAfterViewInit
ngAfterViewInit()

Calculations after rendering

Returns : void

Properties

class
Type : string
Default value : 'validation-message-service'
Decorators :
@HostBinding('class')

Decorating the component with a class to allow identification in the DOM

Private horizontalPadding
Type : number
Default value : 5

Padding to add to the message on the horizontal coordinate

left
Type : string
Default value : 'calc(50% - 11em)'
Decorators :
@HostBinding('style.left')

Binding to locate the message on left

Public message
Type : string
Default value : null

Message to be shown

Public ownerTargetRect
Type : DOMRect
Default value : null

Owner element to use as anchor

top
Type : string
Default value : 'calc(50% - 11em)'
Decorators :
@HostBinding('style.top')

Binding to locate the message on top

import {
  AfterViewInit,
  Component,
  ElementRef,
  HostBinding,
} from '@angular/core';

/**
 * Validation Message service holder component
 *
 * @export
 * @class ValidationMessageServiceComponent
 */
@Component({
  selector: 'wm-validation-message-service',
  templateUrl: './validation-message-service.component.html',
  styleUrls: ['./validation-message-service.component.scss'],
})
export class ValidationMessageServiceComponent implements AfterViewInit {
  /**
   * Padding to add to the message on the horizontal coordinate
   *
   * @private
   * @type {number}
   * @memberof ValidationMessageServiceComponent
   */
  private horizontalPadding = 5;

  /**
   * Decorating the component with a class to allow identification in the DOM
   *
   * @memberof ValidationMessageServiceComponent
   */
  @HostBinding('class') class = 'validation-message-service';

  /**
   * Binding to locate the message on top
   *
   * @type {string}
   * @memberof ValidationMessageServiceComponent
   */
  @HostBinding('style.top') top = 'calc(50% - 11em)';

  /**
   * Binding to locate the message on left
   *
   * @type {string}
   * @memberof ValidationMessageServiceComponent
   */
  @HostBinding('style.left') left = 'calc(50% - 11em)';

  /**
   * Owner element to use as anchor
   *
   * @type {DOMRect}
   * @memberof ValidationMessageServiceComponent
   */
  public ownerTargetRect: DOMRect = null;

  /**
   * Message to be shown
   *
   * @type {string}
   * @memberof ValidationMessageServiceComponent
   */
  public message: string = null;

  /**
   * Creates an instance of ValidationMessageServiceComponent.
   * @param {ElementRef} elementRef
   * @memberof ValidationMessageServiceComponent
   */
  constructor(private elementRef: ElementRef) {}

  /**
   * Calculations after rendering
   *
   * @memberof ValidationMessageServiceComponent
   */
  ngAfterViewInit() {
    this.calculatePosition();
  }

  /**
   * Calculates the position to be rendered
   *
   * @private
   * @memberof ValidationMessageServiceComponent
   */
  private calculatePosition(): void {
    if (!!this.ownerTargetRect) {
      let targetLeft: number =
        this.ownerTargetRect.right + this.horizontalPadding;
      let targetTop: number = this.ownerTargetRect.top;

      const thisRect: DOMRect =
        this.elementRef?.nativeElement?.getBoundingClientRect();
      //Message will be hidden on the right and there is enought room on the left then we move it to the left
      if (
        targetLeft + thisRect.width > window.innerWidth &&
        this.ownerTargetRect.left - this.horizontalPadding - thisRect.width > 0
      ) {
        targetLeft =
          this.ownerTargetRect.left - this.horizontalPadding - thisRect.width;
      }

      //Messsage will be hidden at the bottom
      if (targetTop + thisRect.height > window.innerHeight) {
        targetTop = window.innerHeight - thisRect.height - 2;
      }

      this.top = targetTop + 'px';
      this.left = targetLeft + 'px';
    }
  }
}
{{ message }}

./validation-message-service.component.scss

@import '../../scss/variables';

:host,
.validation-message-service {
  position: fixed;
  background: $validation-color;
  color: white;
  font-size: $default-font-size;
  width: auto;
  max-width: 23em;
  padding: 0.5em;
  border-radius: 0.25em;
  box-shadow: $validation-box-shadow;
  pointer-events: none;
  z-index: 100000;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""