'use strict'; /** * * * @author Zhong * @date 2019/11/12 * @version */ ((factory) => { if (typeof module !== 'undefined') { module.exports = factory(); } else { window.commonUtil = factory(); } })(() => { function isDef(val) { return typeof val !== 'undefined' && val !== null; } function isEmptyVal(val) { return val === null || val === undefined || val === ''; } // 将树数据排序好 function getSortedTreeData(rootID, items) { return sortSameDedth(rootID, items).reverse(); function sortSameDedth(parentID, items) { const sameDepthItems = items.filter(item => item.ParentID === parentID); if (!sameDepthItems.length) { return []; } const NextIDMapping = {}; sameDepthItems.forEach(item => NextIDMapping[item.NextSiblingID] = item); let curItem = sameDepthItems.length > 1 ? sameDepthItems.find(item => item.NextSiblingID === -1) : sameDepthItems[0]; const sorted = []; while (curItem) { sorted.push(...sortSameDedth(curItem.ID, items)); sorted.push(curItem); curItem = NextIDMapping[curItem.ID] || null; } return sorted; } } // 控制全屏(浏览器有限制) // Element.requestFullscreen的全屏和“F11”的全屏是不一样的。前者是将相关Element变成全屏显示。后者是将浏览器导航、标签等隐藏。 // Fullscreen API对于全屏的判断和监听都是基于Element.requestFullscreen的,比如Document.fullscreenElement。通过F11触发的全屏Document.fullscreenElement返回null,无法正确返回全屏状态。 // F11全屏后,无法通过Fullscreen API对全屏状态判断,会导致F11全屏后点击按钮变成了再次调用api全屏。因此,使用window.innerHeight和window.screen.height作为判断。(打开了控制台后,此方法可能会失效:无法正确或缺innerHeight) // 通过F11打开全屏后,没有办法通过代码退出全屏,只能通过F11退出: // https://stackoverflow.com/questions/51114885/combining-requestfullscreen-and-f11; https://stackoverflow.com/questions/43392583/fullscreen-api-not-working-if-triggered-with-f11/44368592#44368592; function handleFullscreen() { const isFullscreen = window.innerHeight === window.screen.height; if (isFullscreen) { const p = document.exitFullscreen(); p.catch(() => alert('按F11即可退出全屏模式')); } else { document.documentElement.requestFullscreen(); } } // 给数值加上分割 // eg: 1234567.00 => 1,234,567.00 function standardNumber(str) { if (typeof str === 'number') { str = String(str); } if (typeof str !== 'string') { return ''; } const [intPart, decimalPart] = str.split('.'); // 给整数部分加上“,” const temp = []; for (let i = intPart.length - 1, j = 1; i >= 0; i--, j++) { temp.push(intPart[i]); if (j !==0 && j % 3 === 0 && i - 1 >= 0) { temp.push(','); } } const standardIntPart = temp.reverse().join(''); return `${standardIntPart}${decimalPart ? '.' + decimalPart : ''}`; } return { isDef, isEmptyVal, getSortedTreeData, handleFullscreen, standardNumber, }; });