relation.js 1.5 KB

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