common_util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // v是否有值,不为undefined、null、''
  23. function hasValue(v) {
  24. return typeof v !== 'undefined' && v !== null && v !== '';
  25. }
  26. // 是否近似相等(null = undefined = '', 1 = '1'...)
  27. function similarEqual(a, b) {
  28. // null == '' 为false,所以不能用非严等
  29. if (isEmptyVal(a) && isEmptyVal(b)) {
  30. return true;
  31. }
  32. return a == b;
  33. }
  34. // 是否是汉字(基本汉字)
  35. function isHan(str) {
  36. const reg = /[\u4e00-\u9fa5]/;
  37. return reg.test(str);
  38. }
  39. // 将树数据排序好
  40. function getSortedTreeData(rootID, items) {
  41. return sortSameDedth(rootID, items).reverse();
  42. function sortSameDedth(parentID, items) {
  43. const sameDepthItems = items.filter(item => item.ParentID === parentID);
  44. if (!sameDepthItems.length) {
  45. return [];
  46. }
  47. const NextIDMapping = {};
  48. sameDepthItems.forEach(item => NextIDMapping[item.NextSiblingID] = item);
  49. let curItem = sameDepthItems.length > 1 ? sameDepthItems.find(item => item.NextSiblingID === -1) : sameDepthItems[0];
  50. const sorted = [];
  51. while (curItem) {
  52. sorted.push(...sortSameDedth(curItem.ID, items));
  53. sorted.push(curItem);
  54. curItem = NextIDMapping[curItem.ID] || null;
  55. }
  56. return sorted;
  57. }
  58. }
  59. // 控制全屏(浏览器有限制)
  60. // Element.requestFullscreen的全屏和“F11”的全屏是不一样的。前者是将相关Element变成全屏显示。后者是将浏览器导航、标签等隐藏。
  61. // Fullscreen API对于全屏的判断和监听都是基于Element.requestFullscreen的,比如Document.fullscreenElement。通过F11触发的全屏Document.fullscreenElement返回null,无法正确返回全屏状态。
  62. // F11全屏后,无法通过Fullscreen API对全屏状态判断,会导致F11全屏后点击按钮变成了再次调用api全屏。因此,使用window.innerHeight和window.screen.height作为判断。(打开了控制台后,此方法可能会失效:无法正确或缺innerHeight)
  63. // 通过F11打开全屏后,没有办法通过代码退出全屏,只能通过F11退出:
  64. // https://stackoverflow.com/questions/51114885/combining-requestfullscreen-and-f11; https://stackoverflow.com/questions/43392583/fullscreen-api-not-working-if-triggered-with-f11/44368592#44368592;
  65. function handleFullscreen() {
  66. if (isFullscreen()) {
  67. const p = exitFullscreen();
  68. if (Object.prototype.toString.call(p) === '[object Promise]') {
  69. p.catch(() => alert('按F11即可退出全屏模式'));
  70. }
  71. } else {
  72. fullscreen(document.documentElement);
  73. }
  74. }
  75. function isFullscreen() {
  76. return window.innerHeight === window.screen.height;
  77. }
  78. function fullscreen(ele) {
  79. if (ele.requestFullscreen) {
  80. ele.requestFullscreen();
  81. } else if (ele.mozRequestFullscreen) {
  82. ele.mozRequestFullScreen();
  83. } else if (ele.webkitRequestFullscreen) {
  84. ele.webkitRequestFullscreen();
  85. } else if (ele.msRequestFullscreen) {
  86. ele.msRequestFullscreen();
  87. }
  88. }
  89. function exitFullscreen() {
  90. if(document.exitFullscreen) {
  91. return document.exitFullscreen();
  92. } else if(document.mozCancelFullscreen) {
  93. return document.mozCancelFullscreen();
  94. } else if(document.webkitExitFullscreen) {
  95. return document.webkitExitFullscreen();
  96. } else if(document.msExitFullscreen) {
  97. return document.msExitFullscreen();
  98. }
  99. }
  100. // 给数值加上分割
  101. // eg: 1234567.00 => 1,234,567.00
  102. function standardNumber(str) {
  103. if (typeof str === 'number') {
  104. str = String(str);
  105. }
  106. if (typeof str !== 'string') {
  107. return '';
  108. }
  109. const [intPart, decimalPart] = str.split('.');
  110. // 给整数部分加上“,”
  111. const temp = [];
  112. for (let i = intPart.length - 1, j = 1; i >= 0; i--, j++) {
  113. temp.push(intPart[i]);
  114. if (j !==0 && j % 3 === 0 && i - 1 >= 0) {
  115. temp.push(',');
  116. }
  117. }
  118. const standardIntPart = temp.reverse().join('');
  119. return `${standardIntPart}${decimalPart ? '.' + decimalPart : ''}`;
  120. }
  121. return {
  122. isDef,
  123. isEmptyVal,
  124. hasValue,
  125. similarEqual,
  126. isHan,
  127. getSortedTreeData,
  128. handleFullscreen,
  129. standardNumber,
  130. };
  131. });