common_util.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Created by CSL on 2017-06-06.
  3. * public functions.
  4. */
  5. function deleteEmptyObject(arr) {
  6. function isEmptyObject(e) {
  7. var t;
  8. for (t in e)
  9. return !1;
  10. return !0
  11. };
  12. for (var i = 0; i < arr.length; i++) {
  13. if (isEmptyObject(arr[i])) {
  14. arr.splice(i, 1);
  15. i = i - 1;
  16. };
  17. };
  18. };
  19. ((factory) => {
  20. if (typeof module !== 'undefined') {
  21. module.exports = factory();
  22. } else {
  23. window.commonUtil = factory();
  24. }
  25. })(() => {
  26. // 是否定义
  27. function isDef(val) {
  28. return typeof val !== 'undefined' && val !== null;
  29. }
  30. // 是否空值
  31. function isEmptyVal(val) {
  32. return val === null || val === undefined || val === '';
  33. }
  34. // 是否数值
  35. function isNumber(val) {
  36. return !isEmptyVal(val) && !isNaN(val);
  37. }
  38. // 是否近似相等(null = undefined = '', 1 = '1'...)
  39. function similarEqual(a, b) {
  40. // null == '' 为false,所以不能用非严等
  41. if (isEmptyVal(a) && isEmptyVal(b)) {
  42. return true;
  43. }
  44. return a == b;
  45. }
  46. // 递归获取必填项(基本信息、工程特征)
  47. function getRequired(rst, datas) {
  48. if (!datas) {
  49. return rst;
  50. }
  51. for (const data of datas) {
  52. const required = typeof data.required === 'string' ? JSON.parse(data.required) : data.required;
  53. const readOnly = typeof data.readOnly === 'string' ? JSON.parse(data.readOnly) : data.readOnly;
  54. if (required && !readOnly) {
  55. rst.push(data);
  56. }
  57. if (data.items && data.items.length) {
  58. getRequired(rst, data.items);
  59. }
  60. }
  61. return rst;
  62. }
  63. // 将树数据排序好
  64. function getSortedTreeData(rootID, items) {
  65. return sortSameDedth(rootID, items).reverse();
  66. function sortSameDedth(parentID, items) {
  67. const sameDepthItems = items.filter(item => item.ParentID === parentID);
  68. if (!sameDepthItems.length) {
  69. return [];
  70. }
  71. const NextIDMapping = {};
  72. sameDepthItems.forEach(item => NextIDMapping[item.NextSiblingID] = item);
  73. let curItem = sameDepthItems.length > 1 ? sameDepthItems.find(item => item.NextSiblingID === -1) : sameDepthItems[0];
  74. const sorted = [];
  75. while (curItem) {
  76. sorted.push(...sortSameDedth(curItem.ID, items));
  77. sorted.push(curItem);
  78. curItem = NextIDMapping[curItem.ID] || null;
  79. }
  80. return sorted;
  81. }
  82. }
  83. /**
  84. * 根据编码方式获取编码数据
  85. * @param {Set} source - 字符串集合数据源
  86. * @param {String} encoding - 编码方式
  87. */
  88. async function getEncodedData(source, encoding, toBase64 = false) {
  89. return await ajaxPost('/project/getEncodedData', { source, encoding, toBase64 });
  90. }
  91. return {
  92. isDef,
  93. isEmptyVal,
  94. isNumber,
  95. similarEqual,
  96. getRequired,
  97. getSortedTreeData,
  98. getEncodedData,
  99. };
  100. });