project_screen.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. module.exports = {
  3. getProgressPaths(rows) {
  4. const nodes = new Map(rows.map(row => [String(row.tree_id), row]));
  5. const paths = new Map();
  6. for (const row of rows) {
  7. const path = [];
  8. const visited = new Set();
  9. let node = row;
  10. while (node && !visited.has(String(node.tree_id))) {
  11. path.unshift(node);
  12. visited.add(String(node.tree_id));
  13. node = nodes.get(String(node.tree_pid));
  14. }
  15. paths.set(row.id, path);
  16. }
  17. return paths;
  18. },
  19. sortProgressRows(rows, paths) {
  20. return rows.slice().sort((a, b) => {
  21. const pathA = paths.get(a.id);
  22. const pathB = paths.get(b.id);
  23. for (let i = 0; i < Math.min(pathA.length, pathB.length); i++) {
  24. if (pathA[i].id === pathB[i].id) continue;
  25. return pathA[i].tree_order - pathB[i].tree_order || String(pathA[i].id).localeCompare(String(pathB[i].id));
  26. }
  27. return pathA.length - pathB.length;
  28. });
  29. },
  30. formatProgressPath(path, abbreviated = false) {
  31. const names = path.map(node => node.name);
  32. if (abbreviated && names.length > 2) return [names[0], '...', names[names.length - 1]].join(' / ');
  33. return names.join(' / ');
  34. },
  35. };