relation.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. export function useParent(name, onEffect) {
  2. const path = `../${name}/index`;
  3. return {
  4. relations: {
  5. [path]: {
  6. type: 'ancestor',
  7. linked() {
  8. onEffect && onEffect.call(this);
  9. },
  10. linkChanged() {
  11. onEffect && onEffect.call(this);
  12. },
  13. unlinked() {
  14. onEffect && onEffect.call(this);
  15. },
  16. },
  17. },
  18. mixin: Behavior({
  19. created() {
  20. Object.defineProperty(this, 'parent', {
  21. get: () => this.getRelationNodes(path)[0],
  22. });
  23. Object.defineProperty(this, 'index', {
  24. // @ts-ignore
  25. get: () => { var _a, _b; return (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.children) === null || _b === void 0 ? void 0 : _b.indexOf(this); },
  26. });
  27. },
  28. }),
  29. };
  30. }
  31. export function useChildren(name, onEffect) {
  32. const path = `../${name}/index`;
  33. return {
  34. relations: {
  35. [path]: {
  36. type: 'descendant',
  37. linked(target) {
  38. onEffect && onEffect.call(this, target);
  39. },
  40. linkChanged(target) {
  41. onEffect && onEffect.call(this, target);
  42. },
  43. unlinked(target) {
  44. onEffect && onEffect.call(this, target);
  45. },
  46. },
  47. },
  48. mixin: Behavior({
  49. created() {
  50. Object.defineProperty(this, 'children', {
  51. get: () => this.getRelationNodes(path) || [],
  52. });
  53. },
  54. }),
  55. };
  56. }