1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/elements/emby-textarea/emby-textarea.js

136 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-01-26 15:42:02 -05:00
import './emby-textarea.scss';
2020-09-08 02:43:56 -04:00
import 'webcomponents.js/webcomponents-lite';
2020-08-14 08:46:34 +02:00
import '../emby-input/emby-input';
2022-10-22 17:32:01 +03:00
/**
* Calculates the vertical padding of the element
* @param textarea
* @returns {number}
*/
function calculateOffset(textarea) {
const style = window.getComputedStyle(textarea, null);
const props = ['paddingTop', 'paddingBottom'];
let offset = 0;
2022-10-16 16:04:37 +02:00
2023-05-04 11:27:15 -04:00
for (const prop of props) {
offset += parseInt(style[prop], 10);
2022-10-16 16:04:37 +02:00
}
return offset;
}
2022-10-16 16:04:37 +02:00
2023-07-06 11:49:55 -04:00
function AutoGrow(textarea, maxLines) {
2023-04-19 01:56:05 -04:00
const self = this;
2023-04-19 01:56:05 -04:00
if (maxLines === undefined) {
maxLines = 999;
}
2023-04-19 01:56:05 -04:00
let offset;
function reset() {
textarea.rows = 1;
offset = calculateOffset(textarea);
self.rows = textarea.rows || 1;
self.lineHeight = (textarea.scrollHeight / self.rows) - (offset / self.rows);
self.maxAllowedHeight = (self.lineHeight * maxLines) - offset;
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function autogrowFn() {
if (!self.lineHeight || self.lineHeight <= 0) {
reset();
}
if (self.lineHeight <= 0) {
textarea.style.overflowY = 'scroll';
textarea.style.height = 'auto';
textarea.rows = 3;
return;
}
let newHeight = 0;
if ((textarea.scrollHeight - offset) > self.maxAllowedHeight) {
textarea.style.overflowY = 'scroll';
newHeight = self.maxAllowedHeight;
} else {
textarea.style.overflowY = 'hidden';
textarea.style.height = 'auto';
newHeight = textarea.scrollHeight/* - offset*/;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
textarea.style.height = newHeight + 'px';
}
2023-04-19 01:56:05 -04:00
// Call autogrowFn() when textarea's value is changed
textarea.addEventListener('input', autogrowFn);
textarea.addEventListener('focus', autogrowFn);
textarea.addEventListener('valueset', autogrowFn);
2023-04-19 01:56:05 -04:00
autogrowFn();
}
2023-04-19 01:56:05 -04:00
const EmbyTextAreaPrototype = Object.create(HTMLTextAreaElement.prototype);
2023-04-19 01:56:05 -04:00
let elementId = 0;
2023-04-19 01:56:05 -04:00
if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
const descriptor = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
2023-04-19 01:56:05 -04:00
// descriptor returning null in webos
2023-07-06 13:39:48 -04:00
if (descriptor?.configurable) {
2023-04-19 01:56:05 -04:00
const baseSetMethod = descriptor.set;
descriptor.set = function (value) {
baseSetMethod.call(this, value);
2023-04-19 01:56:05 -04:00
this.dispatchEvent(new CustomEvent('valueset', {
bubbles: false,
cancelable: false
}));
};
2023-04-19 01:56:05 -04:00
Object.defineProperty(HTMLTextAreaElement.prototype, 'value', descriptor);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2023-04-19 01:56:05 -04:00
EmbyTextAreaPrototype.createdCallback = function () {
if (!this.id) {
this.id = 'embytextarea' + elementId;
elementId++;
}
};
2023-04-19 01:56:05 -04:00
EmbyTextAreaPrototype.attachedCallback = function () {
if (this.classList.contains('emby-textarea')) {
return;
}
2023-04-19 01:56:05 -04:00
this.rows = 1;
this.classList.add('emby-textarea');
2023-04-19 01:56:05 -04:00
const parentNode = this.parentNode;
const label = this.ownerDocument.createElement('label');
label.innerText = this.getAttribute('label') || '';
label.classList.add('textareaLabel');
2023-04-19 01:56:05 -04:00
label.htmlFor = this.id;
parentNode.insertBefore(label, this);
2023-04-19 01:56:05 -04:00
this.addEventListener('focus', function () {
label.classList.add('textareaLabelFocused');
label.classList.remove('textareaLabelUnfocused');
});
this.addEventListener('blur', function () {
label.classList.remove('textareaLabelFocused');
label.classList.add('textareaLabelUnfocused');
});
2023-04-19 01:56:05 -04:00
this.label = function (text) {
label.innerText = text;
};
2023-07-06 11:49:55 -04:00
new AutoGrow(this);
2023-04-19 01:56:05 -04:00
};
document.registerElement('emby-textarea', {
prototype: EmbyTextAreaPrototype,
extends: 'textarea'
});