123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /**
- * Created by CSL on 2017-06-06.
- * public functions.
- */
- function deleteEmptyObject(arr) {
- function isEmptyObject(e) {
- var t;
- for (t in e)
- return !1;
- return !0
- };
- for (var i = 0; i < arr.length; i++) {
- if (isEmptyObject(arr[i])) {
- arr.splice(i, 1);
- i = i - 1;
- };
- };
- };
- ((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 isNumber(val) {
- return !isEmptyVal(val) && !isNaN(val);
- }
- // 是否近似相等(null = undefined = '', 1 = '1'...)
- function similarEqual(a, b) {
- // null == '' 为false,所以不能用非严等
- if (isEmptyVal(a) && isEmptyVal(b)) {
- return true;
- }
- return a == b;
- }
- // 递归获取必填项(基本信息、工程特征)
- function getRequired(rst, datas) {
- if (!datas) {
- return rst;
- }
- for (const data of datas) {
- const required = typeof data.required === 'string' ? JSON.parse(data.required) : data.required;
- const readOnly = typeof data.readOnly === 'string' ? JSON.parse(data.readOnly) : data.readOnly;
- if (required && !readOnly) {
- rst.push(data);
- }
- if (data.items && data.items.length) {
- getRequired(rst, data.items);
- }
- }
- return rst;
- }
- // 将树数据排序好
- 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;
- }
- }
- /**
- * 根据编码方式获取编码数据
- * @param {Set} source - 字符串集合数据源
- * @param {String} encoding - 编码方式
- */
- async function getEncodedData(source, encoding, toBase64 = false) {
- return await ajaxPost('/project/getEncodedData', { source, encoding, toBase64 });
- }
- return {
- isDef,
- isEmptyVal,
- isNumber,
- similarEqual,
- getRequired,
- getSortedTreeData,
- getEncodedData,
- };
- });
|