findRelationNode.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function findRelationNode(node, p, type, isArray = false) {
  2. // parent child ancestor descendant
  3. const nodes = []
  4. const _prcess = {
  5. parent(_node) {
  6. if (!_node || !_node.$parent) { return }
  7. const _p = _node.$parent.$self.is || _node.$parent.$self.route
  8. if (_p === p) {
  9. return _node.$parent
  10. }
  11. },
  12. child(_node) {
  13. let _child = null
  14. _node.$children
  15. .forEach((child) => {
  16. const _p = child.$self.is
  17. if (_p === p) {
  18. _child = child
  19. if (!isArray) {
  20. return _child
  21. }
  22. nodes.push(_child)
  23. }
  24. })
  25. return _child
  26. },
  27. ancestor(__node) {
  28. if (!__node) { return }
  29. let _node = null
  30. _node = _prcess.parent(__node)
  31. if (!_node) {
  32. _node = _prcess.ancestor(__node.$parent)
  33. }
  34. return _node
  35. },
  36. descendant(__node) {
  37. let _node = null
  38. _node = _prcess.child(__node)
  39. if (!_node) {
  40. __node.$children
  41. .forEach((c) => {
  42. _node = _prcess.child(c)
  43. if (!_node) {
  44. _node = _prcess.descendant(c)
  45. }
  46. })
  47. }
  48. return _node
  49. },
  50. }
  51. const ret = _prcess[type](node)
  52. if (isArray) {
  53. if (type === 'parent' || type === 'ancestor') { return [ret] }
  54. return nodes
  55. }
  56. return ret
  57. }
  58. module.exports = findRelationNode