remove container dependency
instead of container use direct parent element that we can use this directive in variable template combination
This commit is contained in:
parent
3fbf74c803
commit
0d7652c618
2 changed files with 31 additions and 31 deletions
25
README.md
25
README.md
|
|
@ -28,20 +28,20 @@ Import it in your Angular2 project like a module
|
||||||
Ng2FittextModule
|
Ng2FittextModule
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2) Use it in your components
|
2) Use it in your components
|
||||||
```sh
|
```sh
|
||||||
import {Component} from '@angular/core';
|
import {Component} from '@angular/core';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'label',
|
selector: 'label',
|
||||||
template: `<div #container>
|
template: `<div>
|
||||||
<div [fittext]="true" [activateOnResize]="true" [container]="container">Bla bla bla...</div>
|
<div [fittext]="true" [activateOnResize]="true">Bla bla bla...</div>
|
||||||
</div>`
|
</div>`
|
||||||
})
|
})
|
||||||
|
|
||||||
export class LabelComponent {}
|
export class LabelComponent {}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -49,23 +49,22 @@ Import it in your Angular2 project like a module
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
import {Component} from '@angular/core';
|
import {Component} from '@angular/core';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'inputbox',
|
selector: 'inputbox',
|
||||||
template: `<div #container>
|
template: `<div>
|
||||||
<input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true" [container]="container">`,
|
<input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true">`,
|
||||||
</div>`
|
</div>`
|
||||||
})
|
})
|
||||||
|
|
||||||
export class InputBoxComponent {}
|
export class InputBoxComponent {}
|
||||||
```
|
```
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
|
|
||||||
| Parameter | Description | Values |
|
| Parameter | Description | Values |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| fittext | is the selector of the directive | true/false
|
| fittext | is the selector of the directive | true/false
|
||||||
| container | the container to fit | ElementRef
|
|
||||||
| activateOnResize | enable/disable the autofit in case of window resize | true or false (default false)
|
| activateOnResize | enable/disable the autofit in case of window resize | true or false (default false)
|
||||||
| activateOnInputEvents | enbale/disable the autofit in case of input box events (keydown, keyup etc..) | true or false (default false)
|
| activateOnInputEvents | enbale/disable the autofit in case of input box events (keydown, keyup etc..) | true or false (default false)
|
||||||
|
|
||||||
|
|
@ -90,4 +89,4 @@ ISC
|
||||||
|
|
||||||
|
|
||||||
**Lorenzo I.**
|
**Lorenzo I.**
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,9 @@ import {Directive, ElementRef, Renderer, Input, AfterViewInit, HostListener, OnI
|
||||||
@Directive({
|
@Directive({
|
||||||
selector: '[fittext]'
|
selector: '[fittext]'
|
||||||
})
|
})
|
||||||
export class Ng2FittextDirective implements AfterViewInit, OnInit {
|
export class FittextDirective implements AfterViewInit, OnInit {
|
||||||
|
|
||||||
@Input('fittext') fittext: any;
|
@Input('fittext') fittext: any;
|
||||||
@Input('container') container: any;
|
|
||||||
@Input('activateOnResize') activateOnResize: boolean;
|
@Input('activateOnResize') activateOnResize: boolean;
|
||||||
@Input('activateOnInputEvents') activateOnInputEvents: boolean;
|
@Input('activateOnInputEvents') activateOnInputEvents: boolean;
|
||||||
private fontSize: number = 0;
|
private fontSize: number = 0;
|
||||||
|
|
@ -17,15 +16,17 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
|
||||||
|
|
||||||
setFontSize(fontSize) {
|
setFontSize(fontSize) {
|
||||||
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
calculateFontSize(fontSize, speed){
|
calculateFontSize(fontSize, speed) {
|
||||||
// TODO Do with Gauss
|
// TODO Do with Gauss
|
||||||
return Math.floor(fontSize/speed);
|
return Math.floor(fontSize / speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkOverflow(parent:any, children:any) {
|
checkOverflow(children: any) {
|
||||||
|
const parent = children.parentElement;
|
||||||
|
|
||||||
let overflowX = children.scrollWidth - parent.clientWidth;
|
let overflowX = children.scrollWidth - parent.clientWidth;
|
||||||
let overflowY = children.clientHeight - parent.clientHeight;
|
let overflowY = children.clientHeight - parent.clientHeight;
|
||||||
return (overflowX > 1 || overflowY > 1);
|
return (overflowX > 1 || overflowY > 1);
|
||||||
|
|
@ -33,11 +34,11 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
|
||||||
|
|
||||||
@HostListener('window:resize', ['$event'])
|
@HostListener('window:resize', ['$event'])
|
||||||
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.clientHeight);
|
this.setFontSize(this.el.nativeElement.parentElement.clientHeight);
|
||||||
} else{
|
} else{
|
||||||
this.setFontSize(this.container.clientWidth);
|
this.setFontSize(this.el.nativeElement.parentElement.clientWidth);
|
||||||
}
|
}
|
||||||
this.ngAfterViewInit();
|
this.ngAfterViewInit();
|
||||||
}
|
}
|
||||||
|
|
@ -45,18 +46,18 @@ 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.clientHeight);
|
this.setFontSize(this.el.nativeElement.parentElement.clientHeight);
|
||||||
this.ngAfterViewInit();
|
this.ngAfterViewInit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
if (this.fittext) {
|
if (this.fittext) {
|
||||||
if(this.activateOnInputEvents) {
|
if (this.activateOnInputEvents) {
|
||||||
this.setFontSize(this.container.clientHeight);
|
this.setFontSize(this.el.nativeElement.parentElement.clientHeight);
|
||||||
} else {
|
} else {
|
||||||
this.setFontSize(this.container.clientWidth);
|
this.setFontSize(this.el.nativeElement.parentElement.clientWidth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.el.nativeElement.style.setProperty('will-change', 'content');
|
this.el.nativeElement.style.setProperty('will-change', 'content');
|
||||||
|
|
@ -64,11 +65,11 @@ export class Ng2FittextDirective implements AfterViewInit, OnInit {
|
||||||
|
|
||||||
ngAfterViewInit() {
|
ngAfterViewInit() {
|
||||||
if (this.fittext) {
|
if (this.fittext) {
|
||||||
let overflow = this.checkOverflow(this.container, this.el.nativeElement);
|
let overflow = this.checkOverflow(this.el.nativeElement);
|
||||||
if(overflow) {
|
if (overflow) {
|
||||||
this.setFontSize(this.calculateFontSize(this.fontSize, this.speed));
|
this.setFontSize(this.calculateFontSize(this.fontSize, this.speed));
|
||||||
this.ngAfterViewInit();
|
this.ngAfterViewInit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue