2021-01-25 15:10:00 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-05-25 11:22:57 +00:00
|
|
|
import { Directive, ElementRef, Output, EventEmitter, AfterViewInit, Input, OnChanges } from '@angular/core';
|
2021-01-25 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to adapt a textarea rows depending on the input text. It's based on Moodle's data-auto-rows.
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* Usage:
|
2021-05-25 11:22:57 +00:00
|
|
|
* <textarea class="core-textarea" [(ngModel)]="message" rows="1" [core-auto-rows]="message"></textarea>
|
2021-01-25 15:10:00 +00:00
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: 'textarea[core-auto-rows], ion-textarea[core-auto-rows]',
|
|
|
|
})
|
2021-05-25 11:22:57 +00:00
|
|
|
export class CoreAutoRowsDirective implements AfterViewInit, OnChanges {
|
2021-01-25 15:10:00 +00:00
|
|
|
|
|
|
|
protected height = 0;
|
|
|
|
|
2021-05-25 11:22:57 +00:00
|
|
|
@Input('core-auto-rows') value?: string; // eslint-disable-line @angular-eslint/no-input-rename
|
2021-01-25 15:10:00 +00:00
|
|
|
@Output() onResize: EventEmitter<void>; // Emit when resizing the textarea.
|
|
|
|
|
|
|
|
constructor(protected element: ElementRef) {
|
|
|
|
this.onResize = new EventEmitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-25 11:22:57 +00:00
|
|
|
* Resize after initialized.
|
2021-01-25 15:10:00 +00:00
|
|
|
*/
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
// Wait for rendering of child views.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.resize();
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:22:57 +00:00
|
|
|
/**
|
|
|
|
* Resize when content changes.
|
|
|
|
*/
|
|
|
|
ngOnChanges(): void {
|
|
|
|
this.resize();
|
|
|
|
|
|
|
|
if (this.value === '') {
|
|
|
|
// Maybe the form was resetted. In that case it takes a bit to update the height.
|
|
|
|
setTimeout(() => this.resize(), 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* Resize the textarea.
|
|
|
|
*/
|
|
|
|
protected resize(): void {
|
2021-04-12 07:17:50 +00:00
|
|
|
let nativeElement: HTMLElement = this.element.nativeElement;
|
2021-01-25 15:10:00 +00:00
|
|
|
if (nativeElement.tagName == 'ION-TEXTAREA') {
|
2021-04-12 07:17:50 +00:00
|
|
|
// Search the actual textarea.
|
|
|
|
const textarea = nativeElement.querySelector('textarea');
|
|
|
|
if (!textarea) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nativeElement = textarea;
|
2021-01-25 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set height to 1px to force scroll height to calculate correctly.
|
|
|
|
nativeElement.style.height = '1px';
|
|
|
|
nativeElement.style.height = nativeElement.scrollHeight + 'px';
|
|
|
|
|
|
|
|
// Emit event when resizing.
|
|
|
|
if (this.height != nativeElement.scrollHeight) {
|
|
|
|
this.height = nativeElement.scrollHeight;
|
|
|
|
this.onResize.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|