@bootstrap-styled/utils
Version:
UMD module for helper functions used as utilities for bootstrap-styled and other related modules
213 lines (175 loc) • 5.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTetherAttachments = getTetherAttachments;
exports.getScrollbarWidth = getScrollbarWidth;
exports.setScrollbarWidth = setScrollbarWidth;
exports.isBodyOverflowing = isBodyOverflowing;
exports.getOriginalBodyPadding = getOriginalBodyPadding;
exports.conditionallyUpdateScrollbar = conditionallyUpdateScrollbar;
exports.toHashCode = toHashCode;
exports.createChainedFunction = createChainedFunction;
exports.pick = pick;
exports.tetherAttachements = void 0;
function getTetherAttachments(placement) {
switch (placement) {
case 'top':
case 'top center':
return {
attachment: 'bottom center',
targetAttachment: 'top center'
};
case 'bottom':
case 'bottom center':
return {
attachment: 'top center',
targetAttachment: 'bottom center'
};
case 'left':
case 'left center':
return {
attachment: 'middle right',
targetAttachment: 'middle left'
};
case 'right':
case 'right center':
return {
attachment: 'middle left',
targetAttachment: 'middle right'
};
case 'top left':
return {
attachment: 'bottom left',
targetAttachment: 'top left'
};
case 'top right':
return {
attachment: 'bottom right',
targetAttachment: 'top right'
};
case 'bottom left':
return {
attachment: 'top left',
targetAttachment: 'bottom left'
};
case 'bottom right':
return {
attachment: 'top right',
targetAttachment: 'bottom right'
};
case 'right top':
return {
attachment: 'top left',
targetAttachment: 'top right'
};
case 'right bottom':
return {
attachment: 'bottom left',
targetAttachment: 'bottom right'
};
case 'left top':
return {
attachment: 'top right',
targetAttachment: 'top left'
};
case 'left bottom':
return {
attachment: 'bottom right',
targetAttachment: 'bottom left'
};
default:
return {
attachment: 'top center',
targetAttachment: 'bottom center'
};
}
}
var tetherAttachements = ['top', 'bottom', 'left', 'right', 'top left', 'top center', 'top right', 'right top', 'right middle', 'right bottom', 'bottom right', 'bottom center', 'bottom left', 'left top', 'left middle', 'left bottom']; // https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/js/src/modal.js#L436-L443
exports.tetherAttachements = tetherAttachements;
function getScrollbarWidth() {
var scrollDiv = document.createElement('div'); // .modal-scrollbar-measure styles // https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/scss/_modal.scss#L106-L113
scrollDiv.style.position = 'absolute';
scrollDiv.style.top = '-9999px';
scrollDiv.style.width = '50px';
scrollDiv.style.height = '50px';
scrollDiv.style.overflow = 'scroll';
document.body.appendChild(scrollDiv);
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
return scrollbarWidth;
}
function setScrollbarWidth(padding) {
document.body.style.paddingRight = padding > 0 ? "".concat(padding, "px") : null;
}
function isBodyOverflowing() {
return document.body.clientWidth < window.innerWidth;
}
function getOriginalBodyPadding() {
return parseInt(window.getComputedStyle(document.body, null).getPropertyValue('padding-right') || 0, 10);
}
function conditionallyUpdateScrollbar() {
var scrollbarWidth = getScrollbarWidth(); // https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/js/src/modal.js#L420
var fixedContent = document.querySelectorAll('.navbar-fixed-top, .navbar-fixed-bottom, .is-fixed')[0];
var bodyPadding = fixedContent ? parseInt(fixedContent.style.paddingRight || 0, 10) : 0;
if (isBodyOverflowing()) {
setScrollbarWidth(bodyPadding + scrollbarWidth);
}
}
/**
* http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
* generate a hash from a string
* @param str
* @returns {number}
*/
function toHashCode(str) {
var hash = 0;
if (str.length === 0) {
return hash;
}
for (var i = 0; i < str.length; i += 1) {
var char = str.charCodeAt(i);
hash = (hash << 5) - hash + char; // eslint-disable-line no-bitwise
hash = hash & hash; // eslint-disable-line no-bitwise, operator-assignment
}
return hash;
}
/**
* https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/utils/helpers.js
* Safe chained function
*
* Will only create a new function if needed,
* otherwise will pass back existing functions or null.
*
*/
function createChainedFunction() {
for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
funcs[_key] = arguments[_key];
}
return funcs.filter(function (func) {
return func != null;
}).reduce(function (acc, func) {
return function chainedFunction() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
acc.apply(this, args);
func.apply(this, args);
};
}, function () {});
}
/**
* Returns a filtered copy of an object with only the specified keys.
*/
function pick(obj, keys) {
var pickKeys = Array.isArray(keys) ? keys : [keys];
var length = pickKeys.length;
var key;
var result = {};
while (length > 0) {
length -= 1;
key = pickKeys[length];
result[key] = obj[key];
}
return result;
}
;