闫增涛
2024-02-23 fdfb3ca757ecd6c396632ed276ff354671d3a7e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Component({
  relations: {
    './c-sidebar-item/index': {
      type: 'descendant',
      linked(target) {
        this.children.push(target);
        this.setActive(this.properties.activeKey, true);
      },
      unlinked(target) {
        this.children = this.children.filter((item) => item !== target);
        this.setActive(this.properties.activeKey, true);
      },
    },
  },
 
  externalClasses: ['custom-class'],
 
  properties: {
    activeKey: {
      type: Number,
      value: 0,
    },
  },
  observers: {
    activeKey(newVal) {
      this.setActive(newVal);
    },
  },
 
  created() {
    this.children = [];
    this.currentActive = -1;
    this.topRightRadiusItemIndexs = [];
    this.bottomRightRadiusItemIndexs = [];
  },
 
  methods: {
    setActive(activeKey, isChildrenChange) {
      const {
        children,
        currentActive,
        topRightRadiusItemIndexs: preTopRightRadiusItemIndexs,
        bottomRightRadiusItemIndexs: preBottomRightRadiusItemIndexs,
      } = this;
 
      if (!children.length) {
        return Promise.resolve();
      }
 
      if (activeKey === currentActive && !isChildrenChange) {
        return Promise.resolve();
      }
 
      this.currentActive = activeKey;
      this.topRightRadiusItemIndexs = this.getTopRightRadiusItemIndexs(
        activeKey,
        children,
      );
      this.bottomRightRadiusItemIndexs = this.getBottomRightRadiusItemIndexs(
        activeKey,
        children,
      );
 
      const stack = []; // 任务列表,存放调用子组件的setActive后返回的一堆promise
 
      // 将旧的选中项改为false
      if (currentActive !== activeKey && children[currentActive]) {
        stack.push(children[currentActive].setActive(false));
      }
 
      // 将新的选中项改为true
      if (children[activeKey]) {
        stack.push(children[activeKey].setActive(true));
      }
 
      preTopRightRadiusItemIndexs.forEach((item) => {
        stack.push(children[item].setTopRightRadius(false));
      });
 
      preBottomRightRadiusItemIndexs.forEach((item) => {
        stack.push(children[item].setBottomRightRadius(false));
      });
 
      this.topRightRadiusItemIndexs.forEach((item) => {
        stack.push(children[item].setTopRightRadius(true));
      });
 
      this.bottomRightRadiusItemIndexs.forEach((item) => {
        stack.push(children[item].setBottomRightRadius(true));
      });
 
      return Promise.all(stack);
    },
    getTopRightRadiusItemIndexs(activeKey, children) {
      const { length } = children;
      if (activeKey !== 0 && activeKey < length - 1) return [0, activeKey + 1];
      if (activeKey !== 0) return [0];
      if (activeKey < length - 1) return [activeKey + 1];
      return [];
    },
    getBottomRightRadiusItemIndexs(activeKey) {
      if (activeKey !== 0) return [activeKey - 1];
      return [];
    },
  },
});