common_util.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. return {
  43. isDef,
  44. isEmptyVal,
  45. getSortedTreeData
  46. };
  47. });