UNPKG

@zohodesk/components

Version:

In this Package, we Provide Some Basic Components to Build Web App

519 lines (417 loc) • 14.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DUMMY_OBJECT = void 0; exports.bind = bind; exports.bytesToSize = bytesToSize; exports.cancelBubblingEffect = cancelBubblingEffect; exports.capitalize = capitalize; exports.checkIsImageFile = checkIsImageFile; exports.debounce = exports.cs = void 0; exports.doAfterScrollEnd = doAfterScrollEnd; exports.encodeForHtml = encodeForHtml; exports.filterValue = filterValue; exports.findScrollEnd = findScrollEnd; exports.format12Hour = format12Hour; exports.formatValue = formatValue; exports.getDateOnly = getDateOnly; exports.getElementSpace = getElementSpace; exports.getFullDateString = getFullDateString; exports.getFullName = getFullName; exports.getIsEmptyValue = void 0; exports.getKeyValue = getKeyValue; exports.getListOfDetails = getListOfDetails; exports.getMemSize = getMemSize; exports.getPortalName = getPortalName; exports.getSearchString = getSearchString; exports.getStringifiedOutputForJson = getStringifiedOutputForJson; exports.getTotalDimension = void 0; exports.isDescendant = isDescendant; exports.isTextSelected = isTextSelected; exports.mapValues = mapValues; exports.merge = merge; exports.parseDecimal = void 0; exports.remConvert = remConvert; exports.stopAllEventPropagation = exports.scrollTo = void 0; exports.stopPropagation = stopPropagation; exports.throttle = void 0; var _viewPort = _interopRequireDefault(require("../Popup/viewPort")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } //$Id$// var frameRelativeRects = _viewPort["default"].frameRelativeRects; var cs = function cs(classArr) { return classArr.filter(function (name) { return name; }).join(' '); }; exports.cs = cs; var parseDecimal = function parseDecimal(val) { return val ? parseFloat(val) : 0; }; exports.parseDecimal = parseDecimal; var getTotalDimension = function getTotalDimension(elem, align) { var style = window.getComputedStyle(elem); return align === 'vertical' ? elem.offsetWidth + parseDecimal(style.marginLeft) + parseDecimal(style.marginRight) : elem.offsetHeight + parseDecimal(style.marginTop) + parseDecimal(style.marginBottom); }; exports.getTotalDimension = getTotalDimension; function stopPropagation(e) { e && e.stopPropagation && e.stopPropagation(); } ; var stopAllEventPropagation = function stopAllEventPropagation(e) { e.stopPropagation && e.stopPropagation(); e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation(); }; exports.stopAllEventPropagation = stopAllEventPropagation; function cancelBubblingEffect(e) { e && e.stopPropagation && e.stopPropagation(); e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation(); } function getKeyValue(event) { return event && event.key && event.key; } // export function getCommentsDetails( // commentIds = [], // comments = {}, // agents = {} // ) { // return ( // commentIds.map(commentId => { // const comment = comments[commentId] || {}; // const commentedBy = comment.commenterId || null; // return Object.assign({}, comment, { commentedBy: agents[commentedBy] }); // }) || [] // ); // } function encodeForHtml(str) { if (/<|>|&|"|'|\\/g.test(str) == true) { str = str.replace(/&/g, '&amp;'); str = str.replace(/</g, '&lt;'); str = str.replace(/>/g, '&gt;'); str = str.replace(/"/g, '&quot;'); str = str.replace(/\\/g, '&#x5c;'); str = str.replace(/'/g, '&#39;'); } return str; } function getListOfDetails() { var Obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var fn = arguments.length > 1 ? arguments[1] : undefined; var ids = Object.keys(Obj) || []; return ids.map(function (id) { return fn(Obj[id]); }); } function capitalize() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var newValue = ''; for (var i = 0; i < value.length; i++) { if (i == 0) { newValue += value[i].toUpperCase(); } else { newValue += value[i].toLowerCase(); } } return newValue; } function bind() { var _this = this; for (var _len = arguments.length, handlers = new Array(_len), _key = 0; _key < _len; _key++) { handlers[_key] = arguments[_key]; } handlers.forEach(function (handler) { _this[handler] = _this[handler].bind(_this); }); } function getFullName() { var firstName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var lastName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var fullName = ''; if (firstName !== null && firstName !== '') { fullName = firstName; } if (lastName !== null && lastName !== '') { fullName = "".concat(fullName).concat(lastName); } return fullName; } function getStringifiedOutputForJson() { var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var keys = Object.keys(input); var modifiedInput = keys.reduce(function (curr, next) { var values = input[next] || []; if (values.length) { var valueList = values.reduce(function (curr, next) { curr.push(next.id); return curr; }, []); curr[next] = valueList; return curr; } return curr; }, {}); return JSON.stringify(modifiedInput); } function mapValues(obj, fn) { return obj && Object.keys(obj).reduce(function (res, key) { res[key] = fn(key, obj[key]); return res; }, {}) || obj; } function filterValue(obj, val) { var result; var ids = obj ? Object.keys(obj) : []; ids.forEach(function (key) { var value = obj[key] || {}; if (value.name === val && !value.isCustomField) { result = value.id; } }); return result; } function merge() { var oldObj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var newObj = arguments.length > 1 ? arguments[1] : undefined; var copyOfOld = Object.assign({}, oldObj); return Object.keys(newObj).reduce(function (res, key) { res[key] = Object.assign({}, res[key] || {}, newObj[key]); return res; }, copyOfOld); } function formatValue() { var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var valueField = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id'; var textField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'name'; return values && values.map(function (value) { var formattedValue = value; if (typeof value === 'string' || typeof value === 'number') { formattedValue = {}; formattedValue[valueField] = value; formattedValue[textField] = value; } return formattedValue; }) || []; } function bytesToSize(bytes) { bytes = parseInt(bytes, 10); var sizes = ['B', 'KB', 'MB', 'GB', 'TB']; if (bytes === 0) { return '0 Byte'; } var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10); return "".concat(Math.round(bytes / Math.pow(1024, i), 2), " ").concat(sizes[i]); } function getMemSize(bytes) { var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : !0; var thresh = size ? 1000 : 1024; if (Math.abs(bytes) < thresh) { return "".concat(bytes, " B"); } var units = size ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'], i = -1; do { bytes /= thresh, ++i; } while (Math.abs(bytes) >= thresh && i < units.length - 1); { return "".concat(bytes.toFixed(1), " ").concat(units[i]); } } function checkIsImageFile(fileName) { fileName = fileName || ''; return /jpeg|jpg|png|gif|bmp|tiff|tif|ico/gi.test(fileName.substr(fileName.lastIndexOf('.') + 1)); } function getI18NValue(key, values) { if (typeof i18n === 'undefined' || key === '' || typeof key === 'undefined') { return key; } var i18nStr = i18n[key]; if (i18nStr === undefined) { return key; } i18nStr = replaceI18NValuesWithRegex(i18nStr, values); return unescapeUnicode(i18nStr); } global.getI18NValue = getI18NValue; function replaceI18NValuesWithRegex(i18nStr, values) { if (typeof values !== 'undefined') { if (Array.isArray(values)) { for (var i = 0; i < values.length; i++) { i18nStr = i18nStr.replace(new RegExp("\\{".concat(i, "\\}"), 'g'), values[i]); } } else { i18nStr = i18nStr.replace(new RegExp('\\{0\\}', 'g'), values); } } return i18nStr; } function unescapeUnicode(str) { return str.replace(/\\u([a-fA-F0-9]{4})/g, function (g, m1) { return String.fromCharCode(parseInt(m1, 16)); }); } function getPortalName(portalList, portalId) { return portalList && portalList.reduce(function (res, portal) { if (portal.orgId === portalId) { return portal.orgName; } return res; }, null); } function getFullDateString(fullDateObj) { var dateStr = ''; var monthArr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; if (fullDateObj.toDateString() == new Date().toDateString()) { dateStr = format12Hour(fullDateObj); } else { var year = fullDateObj.getFullYear(); var month = monthArr[fullDateObj.getMonth()]; var date = fullDateObj.getDate(); var time = format12Hour(fullDateObj); dateStr = "".concat(month, " ").concat(date, " ").concat(year, " ").concat(time); } return dateStr; } function format12Hour(date) { var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'pm' : 'am'; hours %= 12; hours = hours ? hours : 12; // the hour '0' should be '12' minutes = minutes < 10 ? "0".concat(minutes) : minutes; var strTime = "".concat(hours, ":").concat(minutes, " ").concat(ampm); return strTime; } function getDateOnly(fullDateObj) { var dateStr = ''; var monthArr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; if (fullDateObj.toDateString() == new Date().toDateString()) { dateStr = 'Today'; } else { var year = fullDateObj.getFullYear(); var month = monthArr[fullDateObj.getMonth()]; var date = fullDateObj.getDate(); dateStr = "".concat(month, " ").concat(date, " ").concat(year); } return dateStr; } function doAfterScrollEnd(element, callback) { var scrollTop = element.scrollTop; var clientHeight = element.clientHeight; var scrollHeight = element.scrollHeight; var scrollPercent = scrollTop / (scrollHeight - clientHeight) * 100; if (scrollPercent > 80) { callback && callback(); } } var debounce = function debounce(func, wait) { var interval; return function (e) { clearTimeout(interval); interval = setTimeout(func.bind(undefined, e), wait); }; }; exports.debounce = debounce; var timerId; var throttle = function throttle(func, wait) { for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { args[_key2 - 2] = arguments[_key2]; } if (timerId) { return; } timerId = setTimeout(function () { try { func.apply(void 0, args); } finally { timerId = undefined; } }, wait); }; exports.throttle = throttle; var scrollTo = function scrollTo(suggestionContainer, selSuggestion) { if (!suggestionContainer || !selSuggestion) { return; } var _ref = frameRelativeRects(selSuggestion, suggestionContainer) || {}, rectGap = _ref.rectGap; var _ref2 = rectGap || {}, _ref2$top = _ref2.top, top = _ref2$top === void 0 ? 0 : _ref2$top, _ref2$bottom = _ref2.bottom, bottom = _ref2$bottom === void 0 ? 0 : _ref2$bottom; if (top < 0 && bottom < 0) { return; } var isElementInViewPort = top < 0 || bottom < 0 ? false : true; var suggestionContainerScrollTop = suggestionContainer.scrollTop; suggestionContainer.scrollTop = !isElementInViewPort ? bottom < 0 ? suggestionContainer.scrollTop + Math.abs(bottom) : top < 0 ? suggestionContainer.scrollTop - Math.abs(top) : suggestionContainerScrollTop : suggestionContainerScrollTop; }; exports.scrollTo = scrollTo; var getIsEmptyValue = function getIsEmptyValue(value) { return typeof value === 'string' ? value.trim() ? false : true : value === 0 || value ? false : true; }; exports.getIsEmptyValue = getIsEmptyValue; function remConvert(val) { var baseValue = parseFloat(window.getComputedStyle(document.body).getPropertyValue('font-size')).toFixed(4); return (val / baseValue).toPrecision(4); } function isDescendant(parent, child) { var node = child.parentNode; while (node !== null) { if (node === parent) { return true; } node = node.parentNode; } return false; } function getElementSpace(elementRef) { var neededSpace = 0, availableInsideSpace = 0; if (elementRef) { var getStyle = window.getComputedStyle(elementRef); var margin = parseFloat(getStyle.marginLeft) + parseFloat(getStyle.marginRight); var padding = parseFloat(getStyle.paddingLeft) + parseFloat(getStyle.paddingRight); var width = parseFloat(elementRef.offsetWidth); neededSpace = width + margin; availableInsideSpace = width - padding; } return { neededSpace: neededSpace, availableInsideSpace: availableInsideSpace }; } function getSearchString() { var charachers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return (charachers || '').toString().toLowerCase(); //.replace(/\s+/g, ''); } function findScrollEnd(element) { var _ref3 = element || {}, scrollHeight = _ref3.scrollHeight, scrollTop = _ref3.scrollTop, offsetHeight = _ref3.offsetHeight, clientHeight = _ref3.clientHeight; var isElementScrollable = scrollHeight > clientHeight; if (isElementScrollable) { if (scrollHeight <= Math.ceil(scrollTop + offsetHeight)) { return true; } } // else { // throw 'Is scroll not present in this element'; // } return false; } function isTextSelected() { var selectedText = ''; if (window.getSelection) { selectedText = window.getSelection().toString(); } else if (document.getSelection) { selectedText = document.getSelection().toString(); } if (selectedText !== '') { return true; } return false; } var DUMMY_OBJECT = {}; exports.DUMMY_OBJECT = DUMMY_OBJECT;