jellyfish-web/src/elements/emby-textarea/emby-textarea.js

139 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import './emby-textarea.css';
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';
/* eslint-disable indent */
2018-10-23 01:05:09 +03:00
function autoGrow(textarea, maxLines) {
2020-07-10 20:05:00 +01:00
const self = this;
if (maxLines === undefined) {
maxLines = 999;
}
/**
* Calculates the vertical padding of the element
* @param textarea
* @returns {number}
*/
self.getOffset = function (textarea) {
2020-07-10 20:05:00 +01:00
const style = window.getComputedStyle(textarea, null);
const props = ['paddingTop', 'paddingBottom'];
let offset = 0;
2020-07-10 20:05:00 +01:00
for (let i = 0; i < props.length; i++) {
offset += parseInt(style[props[i]]);
}
return offset;
};
2020-07-10 20:05:00 +01:00
let offset;
2018-10-23 01:05:09 +03:00
function reset() {
textarea.rows = 1;
offset = self.getOffset(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
}
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;
}
2020-07-10 20:05:00 +01:00
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*/;
}
2020-05-30 23:15:06 +02:00
$('.customCssContainer').css('height', newHeight + 'px');
textarea.style.height = newHeight + 'px';
2018-10-23 01:05:09 +03:00
}
// Call autogrowFn() when textarea's value is changed
textarea.addEventListener('input', autogrowFn);
textarea.addEventListener('focus', autogrowFn);
textarea.addEventListener('valueset', autogrowFn);
autogrowFn();
2018-10-23 01:05:09 +03:00
}
2020-07-10 20:05:00 +01:00
const EmbyTextAreaPrototype = Object.create(HTMLTextAreaElement.prototype);
2020-07-10 20:05:00 +01:00
let elementId = 0;
2018-10-23 01:05:09 +03:00
if (Object.getOwnPropertyDescriptor && Object.defineProperty) {
2020-07-10 20:05:00 +01:00
const descriptor = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
// descriptor returning null in webos
2018-10-23 01:05:09 +03:00
if (descriptor && descriptor.configurable) {
2020-07-10 20:05:00 +01:00
const baseSetMethod = descriptor.set;
descriptor.set = function (value) {
baseSetMethod.call(this, value);
this.dispatchEvent(new CustomEvent('valueset', {
bubbles: false,
cancelable: false
}));
};
Object.defineProperty(HTMLTextAreaElement.prototype, 'value', descriptor);
2018-10-23 01:05:09 +03:00
}
}
EmbyTextAreaPrototype.createdCallback = function () {
if (!this.id) {
this.id = 'embytextarea' + elementId;
elementId++;
}
};
EmbyTextAreaPrototype.attachedCallback = function () {
if (this.classList.contains('emby-textarea')) {
return;
2018-10-23 01:05:09 +03:00
}
this.rows = 1;
this.classList.add('emby-textarea');
2020-07-10 20:05:00 +01:00
const parentNode = this.parentNode;
const label = this.ownerDocument.createElement('label');
label.innerHTML = this.getAttribute('label') || '';
label.classList.add('textareaLabel');
label.htmlFor = this.id;
parentNode.insertBefore(label, this);
this.addEventListener('focus', function () {
label.classList.add('textareaLabelFocused');
label.classList.remove('textareaLabelUnfocused');
});
this.addEventListener('blur', function () {
label.classList.remove('textareaLabelFocused');
label.classList.add('textareaLabelUnfocused');
});
this.label = function (text) {
label.innerHTML = text;
};
new autoGrow(this);
};
document.registerElement('emby-textarea', {
2018-10-23 01:05:09 +03:00
prototype: EmbyTextAreaPrototype,
extends: 'textarea'
});
/* eslint-enable indent */