Merge pull request #10 from senty/master

Model to watch
This commit is contained in:
Lorenzo Iovino 2017-08-02 22:09:27 +02:00 committed by GitHub
commit 4b2a22f6ba

View file

@ -1,15 +1,17 @@
import {Directive, ElementRef, Renderer, Input, AfterViewInit, HostListener, OnInit} from '@angular/core'; import {Directive, ElementRef, Renderer, Input, AfterViewInit, HostListener, OnInit, OnChanges, SimpleChanges} from '@angular/core';
@Directive({ @Directive({
selector: '[fittext]' selector: '[fittext]'
}) })
export class Ng2FittextDirective implements AfterViewInit, OnInit { export class Ng2FittextDirective implements AfterViewInit, OnInit, OnChanges {
@Input('fittext') fittext: any; @Input('fittext') fittext: any;
@Input('activateOnResize') activateOnResize: boolean; @Input('activateOnResize') activateOnResize: boolean;
@Input('container') container: any; @Input('container') container: any;
@Input('activateOnInputEvents') activateOnInputEvents: boolean; @Input('activateOnInputEvents') activateOnInputEvents: boolean;
@Input('useMaxFontSize') useMaxFontSize: boolean; @Input('useMaxFontSize') useMaxFontSize: boolean;
@Input('minFontSize') minFontSize = 7;
@Input('modelToWatch') modelToWatch: any;
private maxFontSize: number = 1000; private maxFontSize: number = 1000;
private fontSize: number = 0; private fontSize: number = 0;
private speed: number = 1.05; private speed: number = 1.05;
@ -18,6 +20,11 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
} }
setFontSize(fontSize) { setFontSize(fontSize) {
if (fontSize < this.minFontSize) {
// force that font size will never be lower than minimal allowed font size
fontSize = this.minFontSize;
}
this.fontSize = fontSize; this.fontSize = fontSize;
return this.el.nativeElement.style.setProperty('font-size', (fontSize).toString() + 'px'); return this.el.nativeElement.style.setProperty('font-size', (fontSize).toString() + 'px');
} }
@ -38,11 +45,11 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
onResize() { onResize() {
if (this.activateOnResize && this.fittext) { if (this.activateOnResize && this.fittext) {
if (this.activateOnInputEvents && this.fittext) { if (this.activateOnInputEvents && this.fittext) {
this.setFontSize(this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight); this.setFontSize(this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight);
} else { } else {
this.setFontSize(this.container ? this.container.clientWidth : this.el.nativeElement.parentElement.clientWidth); this.setFontSize(this.container ? this.container.clientWidth : this.el.nativeElement.parentElement.clientWidth);
} }
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
} }
@ -50,8 +57,8 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
@HostListener('input', ['$event']) @HostListener('input', ['$event'])
onInputEvents() { onInputEvents() {
if (this.activateOnInputEvents && this.fittext) { if (this.activateOnInputEvents && this.fittext) {
this.setFontSize(this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight); this.setFontSize(this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight);
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
} }
@ -70,18 +77,28 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
ngAfterViewInit() { ngAfterViewInit() {
if (this.fittext) { if (this.fittext) {
let overflow = this.container ? this.checkOverflow(this.container, this.el.nativeElement) let overflow = this.container ? this.checkOverflow(this.container, this.el.nativeElement)
: this.checkOverflow(this.el.nativeElement.parentElement, this.el.nativeElement); : this.checkOverflow(this.el.nativeElement.parentElement, this.el.nativeElement);
if (overflow) { if (overflow) {
this.setFontSize(this.calculateFontSize(this.fontSize, this.speed)); if (this.fontSize > this.minFontSize) {
this.ngAfterViewInit(); // iterate only until font size is bigger than minimal value
this.setFontSize(this.calculateFontSize(this.fontSize, this.speed));
this.ngAfterViewInit();
}
} else { } else {
if (this.useMaxFontSize) { if (this.useMaxFontSize) {
if(this.fontSize > this.maxFontSize) { if(this.fontSize > this.maxFontSize) {
this.maxFontSize = parseInt(window.getComputedStyle(this.container ? this.container : this.el.nativeElement.parentElement).fontSize, null); this.maxFontSize = parseInt(window.getComputedStyle(this.container ? this.container : this.el.nativeElement.parentElement).fontSize, null);
this.setFontSize(this.maxFontSize); this.setFontSize(this.maxFontSize);
} }
} }
} }
} }
} }
ngOnChanges(changes: SimpleChanges): void {
if (changes.modelToWatch) {
// change of model to watch - call ngAfterViewInit where is implemented logic to change size
setTimeout(_ => this.ngAfterViewInit() );
}
}
} }