@logo-elements/component-base
Version:
A set of mixins used by Logo Elements which is extended from Vaadin components.
97 lines (89 loc) • 2.9 kB
JavaScript
/**
* @license
* Copyright LOGO YAZILIM SANAYİ VE TİCARET A.Ş.
*
* Save to the extent permitted by law, you may not use, copy, modify,
* distribute or create derivative works of this material or any part
* of it without the prior written consent of LOGO YAZILIM SANAYİ VE TİCARET A.Ş. Limited.
* Any reproduction of this material must contain this notice.
*/
/**
* Helper that provides a set of functions for RTL.
*/
class DirHelper {
/**
* Get the scroll type in the current browser view.
*
* @return {string} the scroll type. Possible values are `default|reverse|negative`
*/
static detectScrollType() {
const dummy = document.createElement('div');
dummy.textContent = 'ABCD';
dummy.dir = 'rtl';
dummy.style.fontSize = '14px';
dummy.style.width = '4px';
dummy.style.height = '1px';
dummy.style.position = 'absolute';
dummy.style.top = '-1000px';
dummy.style.overflow = 'scroll';
document.body.appendChild(dummy);
let cachedType = 'reverse';
if (dummy.scrollLeft > 0) {
cachedType = 'default';
} else {
dummy.scrollLeft = 2;
if (dummy.scrollLeft < 2) {
cachedType = 'negative';
}
}
document.body.removeChild(dummy);
return cachedType;
}
/**
* Get the scrollLeft value of the element relative to the direction
*
* @param {string} scrollType type of the scroll detected with `detectScrollType`
* @param {string} direction current direction of the element
* @param {Element} element
* @return {number} the scrollLeft value.
*/
static getNormalizedScrollLeft(scrollType, direction, element) {
const { scrollLeft } = element;
if (direction !== 'rtl' || !scrollType) {
return scrollLeft;
}
switch (scrollType) {
case 'negative':
return element.scrollWidth - element.clientWidth + scrollLeft;
case 'reverse':
return element.scrollWidth - element.clientWidth - scrollLeft;
}
return scrollLeft;
}
/**
* Set the scrollLeft value of the element relative to the direction
*
* @param {string} scrollType type of the scroll detected with `detectScrollType`
* @param {string} direction current direction of the element
* @param {Element} element
* @param {number} scrollLeft the scrollLeft value to be set
*/
static setNormalizedScrollLeft(scrollType, direction, element, scrollLeft) {
if (direction !== 'rtl' || !scrollType) {
element.scrollLeft = scrollLeft;
return;
}
switch (scrollType) {
case 'negative':
element.scrollLeft = element.clientWidth - element.scrollWidth + scrollLeft;
break;
case 'reverse':
element.scrollLeft = element.scrollWidth - element.clientWidth - scrollLeft;
break;
default:
element.scrollLeft = scrollLeft;
break;
}
}
}
export { DirHelper };