2018-02-14 16:19:09 +00:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Directive, ElementRef, HostListener, Output, EventEmitter } from '@angular/core';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to adapt a textarea rows depending on the input text. It's based on Moodle's data-auto-rows.
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* Usage:
|
|
|
|
* <textarea class="core-textarea" [(ngModel)]="message" rows="1" core-auto-rows></textarea>
|
|
|
|
*/
|
|
|
|
@Directive({
|
2018-05-25 14:22:15 +00:00
|
|
|
selector: 'textarea[core-auto-rows], ion-textarea[core-auto-rows]'
|
2018-02-14 16:19:09 +00:00
|
|
|
})
|
|
|
|
export class CoreAutoRowsDirective {
|
|
|
|
protected height = 0;
|
|
|
|
|
|
|
|
@Output() onResize: EventEmitter<void>; // Emit when resizing the textarea.
|
|
|
|
|
2018-05-25 14:22:15 +00:00
|
|
|
constructor(private element: ElementRef) {
|
2018-02-14 16:19:09 +00:00
|
|
|
this.onResize = new EventEmitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('input') onInput(): void {
|
|
|
|
this.resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
@HostListener('change') onChange(): void {
|
|
|
|
// Fired on reset. Wait to the change to be finished.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.resize();
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-25 14:22:15 +00:00
|
|
|
* Resize after content.
|
2018-02-14 16:19:09 +00:00
|
|
|
*/
|
2018-06-11 13:05:35 +00:00
|
|
|
ngAfterViewInit(): void {
|
|
|
|
// Wait for rendering of child views.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.resize();
|
|
|
|
}, 300);
|
2018-02-14 16:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize the textarea.
|
|
|
|
* @param {any} $event Event fired.
|
|
|
|
*/
|
|
|
|
protected resize($event?: any): void {
|
2018-05-25 14:22:15 +00:00
|
|
|
let nativeElement = this.element.nativeElement;
|
|
|
|
if (nativeElement.tagName == 'ION-TEXTAREA') {
|
|
|
|
// The first child of ion-textarea is the actual textarea element.
|
|
|
|
nativeElement = nativeElement.firstElementChild;
|
|
|
|
}
|
|
|
|
|
2018-02-14 16:19:09 +00:00
|
|
|
// Set height to 1px to force scroll height to calculate correctly.
|
2018-05-25 14:22:15 +00:00
|
|
|
nativeElement.style.height = '1px';
|
|
|
|
nativeElement.style.height = nativeElement.scrollHeight + 'px';
|
2018-02-14 16:19:09 +00:00
|
|
|
|
|
|
|
// Emit event when resizing.
|
2018-05-25 14:22:15 +00:00
|
|
|
if (this.height != nativeElement.scrollHeight) {
|
|
|
|
this.height = nativeElement.scrollHeight;
|
2018-02-14 16:19:09 +00:00
|
|
|
this.onResize.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|