Removed console.log, added possibility of choose if fit a specified container (old way) or the element parent

This commit is contained in:
Lorenzo Iovino 2017-06-19 11:24:41 +02:00
parent 97c428f0af
commit 129cb1251c
4 changed files with 67 additions and 22 deletions

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ Thumbs.db
*.js *.js
*.map *.map
*.d.ts *.d.ts
.idea

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -32,6 +32,23 @@ Import it in your Angular2 project like a module
``` ```
2) Use it in your components 2) Use it in your components
Fit with specified container (can be the parent or a deeper ancestor)
```sh
import {Component} from '@angular/core';
@Component({
selector: 'label',
template: `<div #container>
<div [fittext]="true" [activateOnResize]="true" [container]="container" [useMaxFontSize]="false">Bla bla bla...</div>
</div>`
})
export class LabelComponent {}
```
Fit with the parent element (this works if you have a variable number of element between element and parent)
```sh ```sh
import {Component} from '@angular/core'; import {Component} from '@angular/core';
@ -45,6 +62,8 @@ Import it in your Angular2 project like a module
export class LabelComponent {} export class LabelComponent {}
``` ```
**NEW! Support for autoresize input box!** **NEW! Support for autoresize input box!**
```sh ```sh
@ -52,19 +71,38 @@ Import it in your Angular2 project like a module
@Component({ @Component({
selector: 'inputbox', selector: 'inputbox',
template: `<div> template: `<div #container>
<input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true" [useMaxFontSize]="false">`, <input [fittext]="true" [activateOnResize]="true" [container]="container" [activateOnInputEvents]="true" [useMaxFontSize]="false">`,
</div>` </div>`
}) })
export class InputBoxComponent {} export class InputBoxComponent {}
``` ```
**NEW! Support for maxFontSize!**
```sh
import {Component} from '@angular/core';
@Component({
selector: 'inputbox',
template: `<div>
<input [fittext]="true" [activateOnResize]="true" [activateOnInputEvents]="true" [useMaxFontSize]="true">`,
</div>`
})
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 (if not present it fit default to parent container)| 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)
| useMaxFontSize | Use font-size from element as maximum font-size | enable/disable the usage of max font-size of the lement | useMaxFontSize | Use font-size from element as maximum font-size | enable/disable the usage of max font-size of the lement

View file

@ -3,10 +3,11 @@ import {Directive, ElementRef, Renderer, Input, AfterViewInit, HostListener, OnI
@Directive({ @Directive({
selector: '[fittext]' selector: '[fittext]'
}) })
export class FittextDirective implements AfterViewInit, OnInit { export class Ng2FittextDirective implements AfterViewInit, OnInit {
@Input('fittext') fittext: any; @Input('fittext') fittext: any;
@Input('activateOnResize') activateOnResize: boolean; @Input('activateOnResize') activateOnResize: boolean;
@Input('container') container: any;
@Input('activateOnInputEvents') activateOnInputEvents: boolean; @Input('activateOnInputEvents') activateOnInputEvents: boolean;
@Input('useMaxFontSize') useMaxFontSize: boolean; @Input('useMaxFontSize') useMaxFontSize: boolean;
private maxFontSize: number = 1000; private maxFontSize: number = 1000;
@ -17,8 +18,6 @@ export class FittextDirective implements AfterViewInit, OnInit {
} }
setFontSize(fontSize) { setFontSize(fontSize) {
console.log();
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');
} }
@ -28,8 +27,7 @@ export class FittextDirective implements AfterViewInit, OnInit {
return Math.floor(fontSize / speed); return Math.floor(fontSize / speed);
} }
checkOverflow(children: any) { checkOverflow(parent: any, 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;
@ -40,9 +38,9 @@ export class FittextDirective 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.el.nativeElement.parentElement.clientHeight); this.setFontSize(this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight);
} else { } else {
this.setFontSize(this.el.nativeElement.parentElement.clientWidth); this.setFontSize(this.container ? this.container.clientWidth : this.el.nativeElement.parentElement.clientWidth);
} }
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
@ -51,7 +49,7 @@ export class FittextDirective implements AfterViewInit, OnInit {
@HostListener('input', ['$event']) @HostListener('input', ['$event'])
onInputEvents() { onInputEvents() {
if (this.activateOnInputEvents && this.fittext) { if (this.activateOnInputEvents && this.fittext) {
this.setFontSize(this.el.nativeElement.parentElement.clientHeight); this.setFontSize(this.container ? this.container.clientHeight : this.el.nativeElement.parentElement.clientHeight);
this.ngAfterViewInit(); this.ngAfterViewInit();
} }
} }
@ -64,12 +62,14 @@ export class FittextDirective implements AfterViewInit, OnInit {
if (this.fittext) { if (this.fittext) {
this.setFontSize(this.maxFontSize); this.setFontSize(this.maxFontSize);
} }
this.el.nativeElement.style.setProperty('will-change', 'content'); this.el.nativeElement.style.setProperty('will-change', 'content');
} }
ngAfterViewInit() { ngAfterViewInit() {
if (this.fittext) { if (this.fittext) {
let overflow = this.checkOverflow(this.el.nativeElement); let overflow = this.container ? this.checkOverflow(this.container, this.el.nativeElement)
: this.checkOverflow(this.el.nativeElement.parentElement, 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();