relation.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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: () => {
  26. var _a, _b;
  27. return (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.children) === null || _b === void 0 ? void 0 : _b.indexOf(this);
  28. },
  29. });
  30. },
  31. }),
  32. };
  33. }
  34. export function useChildren(name, onEffect) {
  35. const path = `../${name}/index`;
  36. return {
  37. relations: {
  38. [path]: {
  39. type: 'descendant',
  40. linked(target) {
  41. onEffect && onEffect.call(this, target);
  42. },
  43. linkChanged(target) {
  44. onEffect && onEffect.call(this, target);
  45. },
  46. unlinked(target) {
  47. onEffect && onEffect.call(this, target);
  48. },
  49. },
  50. },
  51. mixin: Behavior({
  52. created() {
  53. Object.defineProperty(this, 'children', {
  54. get: () => this.getRelationNodes(path) || [],
  55. });
  56. },
  57. }),
  58. };
  59. }