-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoResize.js
49 lines (31 loc) · 1.49 KB
/
autoResize.js
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
var app = angular.module('auto-resize-textarea', []);
app.directive("autoResizeTextarea", ['$compile', function($compile){
var originalHeight = undefined;
var updateHeight = function(scope,element){
if(!originalHeight){
originalHeight = element.height();
}
var content = element.val();
var parsedContent = content.replace(/[\n\r]/g,"<br/>");
parsedContent += "<br/>";
var id = Math.random().toString(36).substr(2,9);
var div = $compile("<div id=" + id + " style='display:none;overflow-wrap:break-word;white-space:pre-wrap;min-height:" + originalHeight + "px;width:" + element.width() + "px;font-size:" + element.css("font-size")+ "'>" + parsedContent + "</div>")(scope);
var body = angular.element(document).find('body');
body.append(div);
element.height(Math.max(div.height(), originalHeight));
angular.element('#'+id).remove();
};
return {
restrict: 'A',
link: function(scope, element, attrs, controller, transcludeFn) {
element[0].style.overflow = "hidden";
element[0].style.fontSize = "13px";
element.bind('keyup', function(){
updateHeight(scope,element);
});
element.bind('focus', function(){
updateHeight(scope,element);
});
}
};
}]);