-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidingTextarea.ts
60 lines (50 loc) · 1.81 KB
/
slidingTextarea.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// <reference path="arkivbas.ts" />
module MOA {
export class SlidingTextarea {
constructor(selector: any, lineHeight: number = 10, maxRows: number = 15) {
this.input = $(selector);
this.lineHeight = lineHeight;
this.maxRows = maxRows;
this.calculateHeight();
this.registerEvents();
}
private input: JQuery;
private lineHeight: number;
private maxRows: number;
private registerEvents() {
this.input.on("keyup", () => {
this.calculateHeight();
});
}
private oldRows = 1;
private calculateHeight() {
var oldHeight = this.input.height();
this.input.height(this.lineHeight-1);
var height = this.input[0].scrollHeight;
this.input.height(oldHeight);
var rows = Math.floor(height / this.lineHeight);
if (rows == 0)
rows = 1;
this.setScrollbar(rows);
this.setTextareaHeight(rows);
}
private setTextareaHeight(rows) {
if (this.oldRows != rows) {
if (this.maxRows < rows)
rows = this.maxRows;
var newHeight;
if (rows > 2) {
newHeight = this.lineHeight * rows + 2;
}
else {
newHeight = this.lineHeight * rows - 1;
}
this.input.animate({ height: newHeight}, 'fast');
this.oldRows = rows;
}
}
private setScrollbar(rows) {
this.input[0].style.overflow = (rows > this.maxRows ? 'visible' : 'hidden');
}
}
}