-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsrc.js
More file actions
46 lines (43 loc) · 1.54 KB
/
src.js
File metadata and controls
46 lines (43 loc) · 1.54 KB
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
/**
* @ngdoc directive
* @name rfx.directive:rAutogrow
* @element textarea
* @function
*
* @description
* Resize textarea automatically to the size of its text content.
*
* @example
<example module="rfx">
<file name="index.html">
<textarea ng-model="text" r-autogrow class="input-block-level"></textarea>
<pre>{{text}}</pre>
</file>
</example>
*/
angular.module('rfx', []).directive('rAutogrow', function() {
// add helper vor measurement to body
var testObj = angular.element('<textarea id="autogrow-helper" style="height: 0; position: absolute; top: -999px"/>');
angular.element(window.document.body).append(testObj);
return {
restrict: 'A',
link: function(scope, element, attrs) {
var adjustHeight = function() {
var height, width = element[0].clientWidth;
testObj.css('width', width + 'px').val(element.val());
height = testObj[0].scrollHeight;
element.css('height', height + 18 + 'px');
};
// adjust on model change.
scope.$watch(attrs.ngModel, adjustHeight);
// model value is trimmed so adjust on enter, space, delete too
element.bind('keyup', function(event) {
var key = event.keyCode;
if (key === 13 || key === 32 || key === 8 || key === 46) {
adjustHeight();
}
});
// insert only returns & spaces and delete per context menu is not covered;
}
};
});