File

src/directives/read-more/read-more.ts

Implements

AfterViewInit OnChanges

Metadata

Index

Properties
Methods
Inputs
HostListeners

Constructor

constructor(el: ElementRef)
Parameters :
Name Type Optional
el ElementRef No

Inputs

length
Type : number
readMore
Type : string
showLessText
Type : string
showMoreText
Type : string

HostListeners

click
Arguments : '$event.target'

Methods

Public breakString
breakString(inputString, from, to)
Parameters :
Name Optional
inputString No
from No
to No
Returns : any
Public checkTextLength
checkTextLength()
Returns : void
Public ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnChanges
ngOnChanges()
Returns : void
onClick
onClick(e)
Decorators :
@HostListener('click', ['$event.target'])
Parameters :
Name Optional
e No
Returns : void
Public setText
setText()
Returns : void
Public splitLessAndMoreText
splitLessAndMoreText()
Returns : void

Properties

Public el
Type : ElementRef
hideToggle
Default value : true
lessText
Type : string
Default value : ''
moreText
Type : string
Default value : ''
toggleState
Default value : false
import {
  Directive,
  Input,
  ElementRef,
  AfterViewInit,
  HostListener,
  OnChanges
} from '@angular/core';

@Directive({
  selector: '[readMore]' // Attribute selector
})
export class ReadMoreDirective implements AfterViewInit, OnChanges {

  // tslint:disable-next-line:no-input-rename
  @Input('length') private maxLength: number;
  @Input('readMore') private text: string;
  @Input() private showMoreText: string;
  @Input() private showLessText: string;
  lessText = '';
  moreText = '';
  toggleState = false; // Show Less in the beginning
  hideToggle = true; // Dont show any Link for less or More before checking the length of the text.

  constructor(public el: ElementRef) { }

  public ngAfterViewInit() {
    if (this.text) {
      this.checkTextLength();
    }

    if (!this.hideToggle) {
      this.splitLessAndMoreText();
    }
  }

  ngOnChanges() {
    console.log('On Changes Triggered', this.text);
    this.ngAfterViewInit();
  }

  public checkTextLength() {
    this.hideToggle = ((this.text.length > this.maxLength) && this.maxLength > 0) ? false : true;
  }

  public splitLessAndMoreText() {
    this.lessText = this.breakString(this.text, 0, this.maxLength);
    this.moreText = this.breakString(this.text, this.maxLength, this.text.length);
    this.showLessText = this.showLessText || 'Show Less';
    this.showMoreText = this.showMoreText || 'Show More';
    this.setText();
  }

  public breakString(inputString, from, to) {
    return inputString.slice(from, to);
  }

  public setText() {
    if (this.toggleState) { // Means Show Less Button
      this.el.nativeElement.innerHTML = this.lessText + this.moreText + '<a> ' + this.showLessText + '</a>';
    } else {  // Means Show button More
      this.el.nativeElement.innerHTML = this.lessText + '...' + '<a>' + this.showMoreText + '</a>';
    }
  }

  @HostListener('click', ['$event.target']) onClick(e) {
    this.toggleState = !this.toggleState;
    this.setText();
  }
}

results matching ""

    No results matching ""