2020-01-20 20:10:57 +01:00
|
|
|
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
2020-02-21 17:54:27 +01:00
|
|
|
* sgAutogrow - A directive to conditionally grow a textarea depending on its content.
|
2020-01-20 20:10:57 +01:00
|
|
|
* This directive is an alternative to the autogrow feature of the md-input component.
|
|
|
|
* It fixes the scroll jumping issue described in #3070.
|
|
|
|
*
|
|
|
|
* - https://github.com/angular/material/issues/3070
|
|
|
|
* - https://material.angularjs.org/latest/api/directive/mdInput
|
|
|
|
*
|
2020-02-21 17:54:27 +01:00
|
|
|
* The drawback of this directive is that it requires to set md-no-autogrow.
|
2020-01-20 20:10:57 +01:00
|
|
|
* @memberof SOGo.Common
|
|
|
|
* @ngInject
|
|
|
|
* @example:
|
|
|
|
|
2020-02-21 17:54:27 +01:00
|
|
|
<textarea rows="9" md-no-autogrow sg-autogrow="!isPopup" />
|
2020-01-20 20:10:57 +01:00
|
|
|
*/
|
2020-02-12 16:57:43 +01:00
|
|
|
sgAutogrow.$inject = ['$document', '$timeout'];
|
|
|
|
function sgAutogrow($document, $timeout) {
|
2020-01-20 20:10:57 +01:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2020-02-21 17:54:27 +01:00
|
|
|
scope: {
|
|
|
|
autogrow: '=sgAutogrow'
|
|
|
|
},
|
2020-01-20 20:10:57 +01:00
|
|
|
link: function(scope, elem, attr) {
|
2020-02-21 17:54:27 +01:00
|
|
|
if (!scope.autogrow) return;
|
|
|
|
|
2020-01-20 20:10:57 +01:00
|
|
|
var textarea = elem[0];
|
2020-02-12 16:57:43 +01:00
|
|
|
var hiddenDiv = $document[0].createElement('div');
|
|
|
|
var content = null;
|
|
|
|
|
|
|
|
hiddenDiv.classList.add('md-input');
|
|
|
|
hiddenDiv.classList.add('plain-text');
|
|
|
|
hiddenDiv.style.display = 'none';
|
|
|
|
hiddenDiv.style.whiteSpace = 'pre-wrap';
|
|
|
|
hiddenDiv.style.wordWrap = 'break-word';
|
|
|
|
textarea.parentNode.appendChild(hiddenDiv);
|
|
|
|
|
|
|
|
textarea.style.resize = 'none';
|
|
|
|
textarea.style.overflow = 'hidden';
|
2020-01-20 20:10:57 +01:00
|
|
|
|
|
|
|
function AutoGrowTextArea() {
|
|
|
|
$timeout(function() {
|
2020-02-12 16:57:43 +01:00
|
|
|
content = textarea.value;
|
|
|
|
content = content.replace(/\n/g, '<br>');
|
|
|
|
hiddenDiv.innerHTML = content + '<br style="line-height: 3px;">';
|
|
|
|
hiddenDiv.style.visibility = 'hidden';
|
|
|
|
hiddenDiv.style.display = 'block';
|
|
|
|
textarea.style.height = hiddenDiv.offsetHeight + 'px';
|
|
|
|
hiddenDiv.style.visibility = 'visible';
|
|
|
|
hiddenDiv.style.display = 'none';
|
2020-01-20 20:10:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.on('keyup', AutoGrowTextArea);
|
|
|
|
elem.on('paste', AutoGrowTextArea);
|
2020-01-20 20:19:11 +01:00
|
|
|
|
2020-03-06 18:14:46 +01:00
|
|
|
var deregisterWatcher = scope.$watch(function() {
|
|
|
|
return elem[0].value;
|
|
|
|
}, function(content) {
|
|
|
|
if (content) {
|
|
|
|
AutoGrowTextArea();
|
|
|
|
deregisterWatcher(); // watch once
|
|
|
|
}
|
|
|
|
});
|
2020-01-20 20:10:57 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('SOGo.Common')
|
|
|
|
.directive('sgAutogrow', sgAutogrow);
|
|
|
|
})();
|