common_util.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2019/11/12
  7. * @version
  8. */
  9. ((factory) => {
  10. if (typeof module !== 'undefined') {
  11. module.exports = factory();
  12. } else {
  13. window.commonUtil = factory();
  14. }
  15. })(() => {
  16. function isDef(val) {
  17. return typeof val !== 'undefined' && val !== null;
  18. }
  19. function isEmptyVal(val) {
  20. return val === null || val === undefined || val === '';
  21. }
  22. // 将树数据排序好
  23. function getSortedTreeData(rootID, items) {
  24. return sortSameDedth(rootID, items).reverse();
  25. function sortSameDedth(parentID, items) {
  26. const sameDepthItems = items.filter(item => item.ParentID === parentID);
  27. if (!sameDepthItems.length) {
  28. return [];
  29. }
  30. const NextIDMapping = {};
  31. sameDepthItems.forEach(item => NextIDMapping[item.NextSiblingID] = item);
  32. let curItem = sameDepthItems.length > 1 ? sameDepthItems.find(item => item.NextSiblingID === -1) : sameDepthItems[0];
  33. const sorted = [];
  34. while (curItem) {
  35. sorted.push(...sortSameDedth(curItem.ID, items));
  36. sorted.push(curItem);
  37. curItem = NextIDMapping[curItem.ID] || null;
  38. }
  39. return sorted;
  40. }
  41. }
  42. // 控制全屏(浏览器有限制)
  43. // Element.requestFullscreen的全屏和“F11”的全屏是不一样的。前者是将相关Element变成全屏显示。后者是将浏览器导航、标签等隐藏。
  44. // Fullscreen API对于全屏的判断和监听都是基于Element.requestFullscreen的,比如Document.fullscreenElement。通过F11触发的全屏Document.fullscreenElement返回null,无法正确返回全屏状态。
  45. // F11全屏后,无法通过Fullscreen API对全屏状态判断,会导致F11全屏后点击按钮变成了再次调用api全屏。因此,使用window.innerHeight和window.screen.height作为判断。(打开了控制台后,此方法可能会失效:无法正确或缺innerHeight)
  46. // 通过F11打开全屏后,没有办法通过代码退出全屏,只能通过F11退出:
  47. // https://stackoverflow.com/questions/51114885/combining-requestfullscreen-and-f11; https://stackoverflow.com/questions/43392583/fullscreen-api-not-working-if-triggered-with-f11/44368592#44368592;
  48. function handleFullscreen() {
  49. const isFullscreen = window.innerHeight === window.screen.height;
  50. if (isFullscreen) {
  51. const p = document.exitFullscreen();
  52. p.catch(() => alert('按F11即可退出全屏模式'));
  53. } else {
  54. document.documentElement.requestFullscreen();
  55. }
  56. }
  57. // 给数值加上分割
  58. // eg: 1234567.00 => 1,234,567.00
  59. function standardNumber(str) {
  60. if (typeof str === 'number') {
  61. str = String(str);
  62. }
  63. if (typeof str !== 'string') {
  64. return '';
  65. }
  66. const [intPart, decimalPart] = str.split('.');
  67. // 给整数部分加上“,”
  68. const temp = [];
  69. for (let i = intPart.length - 1, j = 1; i >= 0; i--, j++) {
  70. temp.push(intPart[i]);
  71. if (j !==0 && j % 3 === 0 && i - 1 >= 0) {
  72. temp.push(',');
  73. }
  74. }
  75. const standardIntPart = temp.reverse().join('');
  76. return `${standardIntPart}${decimalPart ? '.' + decimalPart : ''}`;
  77. }
  78. return {
  79. isDef,
  80. isEmptyVal,
  81. getSortedTreeData,
  82. handleFullscreen,
  83. standardNumber,
  84. };
  85. });