processRelation.js 672 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class Node {
  2. constructor(opts = {}) {
  3. this.$id = opts.id
  4. this.$opts = opts
  5. this.$children = []
  6. this.$parent = null
  7. this.$render = function() {}
  8. }
  9. appendChild(child) {
  10. this.$children.push(child)
  11. child.$parent = this
  12. }
  13. removeChild(child) {
  14. this.$children = this.$children.filter((c) => {
  15. return c.$id !== child.$id
  16. })
  17. }
  18. }
  19. module.exports = function link(opts = {}, cb) {
  20. const node = new Node({
  21. id: opts.id,
  22. })
  23. if (typeof cb === 'function') {
  24. cb(node)
  25. }
  26. if (Array.isArray(opts.children)) {
  27. opts.children.forEach((child) => {
  28. node.appendChild(link(child, cb))
  29. })
  30. }
  31. return node
  32. }