TimeLine.js 2.02 KB
/*可视化地呈现时间流信息*/
Component({
  properties: {
    // 时间轴节点数据
    nodes: {
      type: Array,
      value: [],
      observer: function(newVal, oldVal) {
        this.processNodes(newVal);
      }
    }
  },

  data: {
    timeLineNodes: []
  },

  /**
   * 组件内部方法
   */
  methods: {
    /**
     * 预处理节点数据,添加样式类
     * @param {Array} nodes - 节点数组
     */
    processNodes(nodes) {
      if (!nodes || !Array.isArray(nodes)) return;
      const timeLineNodes = nodes.map(node => {
        return {
          ...node,
          dotClass: this.getNodeDotClass(node),
          contentClass: this.getNodeContentClass(node)
        };
      });
      this.setData({
        timeLineNodes
      });
    },
    
    /**
     * 获取节点点的样式类
     * @param {Object} item - 时间轴节点项
     * @returns {String} 样式类名
     */
    getNodeDotClass(item) {
      // 直接根据节点属性返回对应的样式类
      if (item.active) {
        return 'timeline-node__dot timeline-node__dot--active';
      } else if (item.type === 'special') {
        return 'timeline-node__dot timeline-node__dot--spacial';
      } else if (item.small) {
        return 'timeline-node__dot timeline-node__dot--small';
      } else {
        return 'timeline-node__dot';
      }
    },
    
    /**
     * 获取节点内容的样式类
     * @param {Object} item - 时间轴节点项
     * @returns {String} 样式类名
     */
    getNodeContentClass(item) {
      // 非激活状态的内容使用灰色
      if (item.type === 'special') {
        return 'timeline-node__content--special';
      }else if(item.active){
        return 'active';
      }
    },
    
    /**
     * 判断是否显示节点点
     * @param {Object} item - 时间轴节点项
     * @returns {Boolean} 是否显示节点点
     */
    shouldShowNodeDot(item) {
      // 修复逻辑:所有节点默认都应该显示点
      // 只有特定情况下才不显示
      return true;
    }
  }
});