UNPKG

react-cm-ui

Version:
190 lines (168 loc) 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var domUtils = { addClassName: function addClassName(el, cls) { if (!domUtils.hasClassName(el, cls)) { el.classList.add(cls); } }, browserDetect: function browserDetect() { // https://github.com/mbasso/react-browser-detection/blob/master/src/index.js var isOpera = !!window.opr && !!window.opr.addons || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; var isFirefox = typeof InstallTrigger !== 'undefined'; var isChrome = !!window.chrome && !!window.chrome.webstore && navigator.userAgent.toLowerCase().indexOf('googlebot') === -1; var isSafari = !isChrome && navigator.userAgent.toLowerCase().indexOf('safari') !== -1; var isIE = /* @cc_on!@ */ false || !!document.documentMode; var isEdge = !isIE && !!window.StyleMedia; var isBlink = (isChrome || isOpera) && !!window.CSS; var isGoogleBot = navigator.userAgent.toLowerCase().indexOf('googlebot') !== -1; if (isOpera) { return 'opera'; } if (isFirefox) { return 'firefox'; } if (isSafari) { return 'safari'; } if (isIE) { return 'ie'; } if (isEdge) { return 'edge'; } if (isChrome) { return 'chrome'; } if (isBlink) { return 'blink'; } if (isGoogleBot) { return 'googlebot'; } return 'unknown'; }, cssAnimationType: function cssAnimationType(element) { var animations = { animation: 'animationend', OAnimation: 'oAnimationEnd', MozAnimation: 'animationend', WebkitAnimation: 'webkitAnimationEnd' }; var propValue; Object.keys(animations).forEach(function (key) { if (!propValue && element && element.style[key] !== undefined) { propValue = animations[key]; } }); return propValue; }, cssTransitionType: function cssTransitionType(element) { var transitions = { transition: 'transitionend', OTransition: 'oTransitionEnd otransitionend', MozTransition: 'transitionend', WebkitTransition: 'webkitTransitionEnd' }; var propValue; Object.keys(transitions).forEach(function (key) { if (!propValue && element && element.style[key] !== undefined) { propValue = transitions[key]; } }); return propValue; }, hasClassName: function hasClassName(el, cls) { return el.classList.contains(cls); }, isInViewport: function isInViewport(el, parentEl) { if (el && parentEl) { var elRect = el.getBoundingClientRect(); var parentElRect = parentEl.getBoundingClientRect(); var windowHeight = window.innerHeight || document.documentElement.clientHeight; var windowWidth = window.innerWidth || document.documentElement.clientWidth; var isInTop; var isInRight; var isInBottom; var isInLeft; var topBias; var bottomBias; if (!parentEl) { isInTop = elRect.top >= 0; isInRight = elRect.right <= windowWidth; isInBottom = elRect.bottom <= windowHeight; isInLeft = elRect.left >= 0; topBias = elRect.top; bottomBias = elRect.bottom; } else { isInTop = parentElRect.top >= elRect.height; isInRight = windowWidth - parentElRect.right >= elRect.width; isInBottom = windowHeight - parentElRect.bottom >= elRect.height; isInLeft = parentElRect.left >= elRect.width; topBias = parentElRect.top; bottomBias = windowHeight - parentElRect.bottom; } return { isInTop: isInTop, isInRight: isInRight, isInBottom: isInBottom, isInLeft: isInLeft, topBias: topBias, bottomBias: bottomBias }; } return false; }, removeClassName: function removeClassName(el, cls) { el.classList.remove(cls); }, scrollPos: function scrollPos(el) { if (el) { return el.scrollTop; } return window.scrollY || window.pageYOffset; }, scrollTo: function scrollTo(to, duration, parentEl) { // Defaults var o = { duration: duration || duration === 0 ? 0 : 250, parentEl: parentEl || null, to: to || 0 }; var scrollIncrementMS = 10; var currentViewPortPosistion = !o.parentEl ? document.body.scrollTop || document.documentElement.scrollTop : o.parentEl.scrollTop; // console.log('currentViewPortPosistion: ' + currentViewPortPosistion); var difference = o.to - currentViewPortPosistion; var numSteps = o.duration / scrollIncrementMS; var increment = difference / numSteps; // const startTime = Date.now(); // console.log('start: ' + startTime); var scrollInterval = setInterval(function () { currentViewPortPosistion = !o.parentEl ? document.body.scrollTop || document.documentElement.scrollTop : o.parentEl.scrollTop; difference = o.to - currentViewPortPosistion; var interval = Math.abs(difference) < Math.abs(increment) ? difference : increment; var newViewPortPosistion = currentViewPortPosistion + interval; var newParentEl = parentEl; if (!newParentEl) { document.body.scrollTop = newViewPortPosistion; document.documentElement.scrollTop = newViewPortPosistion; } else { newParentEl = _objectSpread(_objectSpread({}, newParentEl), {}, { scrollTop: newViewPortPosistion }); } if (newViewPortPosistion === o.to || !newParentEl && window.innerHeight + (window.scrollY || window.pageYOffset) >= document.body.scrollHeight) { // console.log('ellapsed: ' + parseInt(startTime - Date.now())); clearInterval(scrollInterval); } }, scrollIncrementMS); } }; var _default = domUtils; exports["default"] = _default;