<template>
  <div class="toc"
  >
    <div
        :class="{
            'toc-view':expand||tempExpand,
            'toc-small-view':!(expand||tempExpand),
            'toc-temp-view':!expand&&tempExpand
         }"
        @mouseleave="tempExpand=false"
        @mouseenter="tempExpand=true"
        v-affix="{fix:`top`,offset:30}">
      <div class="ne-toc-pin">
        <span class="ne-toc-pin-text">大纲</span>
        <div class="ne-toc-pin-wrap" @click="toogle()">
          <div class="ne-icon ne-icon-toc-hide" data-name="toc-hide" style="font-size: 16px;">
            <i class="el-icon-circle-close" title="隐藏大纲" v-if="expand"></i>
            <i class="el-icon-view" title="显示大纲" v-else></i>
          </div>
        </div>
        <div class="ne-toc-fold-wrap">
          <div class="ne-icon ne-icon-toc-unfold" data-name="toc-unfold" style="font-size: 16px;"
               @click="expandTree">
            <i class="el-icon-s-fold" title="全部折叠" v-if="treeExpand"></i>
            <i class="el-icon-s-unfold" title="全部展开" v-else></i>
          </div>
        </div>
      </div>
      <el-tree
          :data="data"
          v-if="treeRender"
          :props="defaultProps"
          :indent="14"
          :default-expand-all="treeExpand"
          :expand-on-click-node="false"
          :highlight-current="true"
          @node-click="handleNodeClick"
      ></el-tree>
    </div>
  </div>
</template>

<script>
import getTreeData from "./getTreeData";

export default {
  name: "outline",
  data() {
    return {
      expand: true,
      tempExpand: false,
      treeExpand: true,
      treeRender: true,
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    };
  },
  watch: {
    expand: {
      immediate: true,
      handler() {
        this.widthChange();
      }
    }
  },
  mounted() {
    let vm = this;
    setTimeout(function () {
      vm.data = getTreeData(vm.$parent.$el);
    }, 500)
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
      document.querySelector(`#${data.id}`).scrollIntoView({
        behavior: "smooth", // 定义动画过渡效果， "auto"或 "smooth" 之一。默认为 "auto"
        block: "start", // 定义垂直方向的对齐， "start", "center", "end", 或 "nearest"之一。默认为 "start"
        inline: "nearest" // 定义水平方向的对齐， "start", "center", "end", 或 "nearest"之一。默认为 "nearest"
      })
    },
    expandTree() {
      this.treeExpand = !this.treeExpand;
      this.treeRender = false;
      this.$nextTick(() => {
        this.treeRender = true;
      })
    },
    toogle() {
      this.expand = !this.expand;
    },
    widthChange() {
      this.$emit("widthChange", this.expand ? 250 : 30)
    }
  }
}
</script>

<style scoped lang="less">
.toc {
  position: absolute;
  top: 0;
  right: 0;
}

.toc-temp-view {
  box-shadow: 0 1px 3px 3px rgb(0 0 0 / 10%);
}

.toc-view {
  width: 250px;
  border-left: 1px solid #d8dad9;
  background-color: white;

  .ne-toc-pin {
    margin-bottom: 12px;
    padding-top: 8px;
    padding-left: 10px;
    display: flex;
    align-items: center;
    height: 30px;
    flex-shrink: 0;

    .ne-toc-pin-text {
      color: #262626;
      margin-right: 8px;
      font-weight: bold;
    }

    .ne-icon {
      cursor: pointer;
    }

    .ne-toc-pin-wrap, .ne-toc-fold-wrap {
      position: relative;
      font-size: 0;
      height: 24px;
      width: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }

  /deep/ .el-tree {
    min-height: 500px;
  }
}

.toc-small-view {
  width: 30px;

  .ne-toc-pin {
    display: none;
  }

  /deep/ .el-tree-node__content {
    border-bottom: 1px solid #d8dad9;
    //height: 2px;
    span {
      display: none;
    }
  }

  /deep/ .is-current {
    border-bottom: 1px solid green;
  }
}

/deep/ .el-tree-node__label::before {
  //content: counter(chapter) '. ';
}

/deep/ .el-tree-node__content {
  //counter-increment: chapter;

  .el-tree-node__label {
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

@keyframes rtl-drawer-in {
  0% {
    transform: translate(100%);
  }

  to {
    transform: translate(50)
  }
}
</style>
