<template>
  <div
    v-show="node.visible"
    ref="node"
    class="el-tree-node"
    :class="{
      'is-expanded': expanded,
      'is-current': node.isCurrent,
      'is-hidden': !node.visible,
      'is-focusable': !node.disabled,
      'is-checked': !node.disabled && node.checked
    }"
    role="treeitem"
    tabindex="-1"
    :aria-expanded="expanded"
    :aria-disabled="node.disabled"
    :aria-checked="node.checked"
    :draggable="tree.draggable"
    @click.stop="handleClick"
    @contextmenu="($event) => this.handleContextMenu($event)"
    @dragstart.stop="handleDragStart"
    @dragover.stop="handleDragOver"
    @dragend.stop="handleDragEnd"
    @drop.stop="handleDrop"
  >
    <div
      class="el-tree-node__content"
      :style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }"
    >
      <component :is="iconComponent" :node="node" :expanded="expanded" />

      <!--<span @click.stop="handleExpandIconClick"-->
      <!--        :class=>-->
      <!--</span>-->
      <ta-checkbox
        v-if="showCheckbox"
        :checked="node.checked"
        :indeterminate="node.indeterminate"
        :disabled="!!node.disabled"
        @click.native.stop
        @change="handleCheckChange"
      />
      <ta-icon
        v-if="node.loading"
        type="loading"
        class="el-tree-node__loading-icon "
      />
      <ta-tooltip v-if="allowRefreshNode" title="点击刷新数据">
        <ta-icon type="sync" class="el-tree-node__loading-icon" @click.stop="syncNodeChildren" />
      </ta-tooltip>
      <node-content :node="node" />
    </div>
    <el-collapse-transition>
      <div
        v-if="!renderAfterExpand || childNodeRendered"
        v-show="expanded"
        class="el-tree-node__children"
        role="group"
        :aria-expanded="expanded"
      >
        <el-tree-node
          v-for="child in node.childNodes"
          :key="getNodeKey(child)"
          :render-content="renderContent"
          :render-after-expand="renderAfterExpand"
          :show-checkbox="showCheckbox"
          :node="child"
          :allow-refresh-node="allowRefreshNode"
          @node-expand="handleChildNodeExpand"
        />
      </div>
    </el-collapse-transition>
  </div>
</template>

<script type="text/jsx">
import ElCollapseTransition from 'element-ui/src/transitions/collapse-transition'
import ElCheckbox from 'element-ui/packages/checkbox'
import emitter from 'element-ui/src/mixins/emitter'
import { getNodeKey } from './model/util'
import ToolTip from '../../tooltip'

export default {
  name: 'ElTreeNode',

  componentName: 'ElTreeNode',

  components: {
    ElCollapseTransition,
    ElCheckbox,
    NodeContent: {
      props: {
        node: {
          required: true,
        },
      },
      render (h) {
        const parent = this.$parent
        const tree = parent.tree
        const node = this.node
        const { data, store } = node
        return (
          parent.renderContent
            ? parent.renderContent.call(parent._renderProxy, h, { _self: tree.$vnode.context, node, data, store })
            : tree.$scopedSlots.default
              ? tree.$scopedSlots.default({ node, data })
              : (<ToolTip title={node.label}><span class="el-tree-node__label">{node.label}</span></ToolTip>)
        )
      },
    },
  },

  mixins: [emitter],

  props: {
    node: {
      default () {
        return {}
      },
    },
    props: {},
    renderContent: Function,
    renderAfterExpand: {
      type: Boolean,
      default: true,
    },
    showCheckbox: {
      type: Boolean,
      default: false,
    },
    allowRefreshNode: {
      type: Boolean,
      default: false,
    },
  },

  data () {
    return {
      tree: null,
      expanded: false,
      childNodeRendered: false,
      oldChecked: null,
      oldIndeterminate: null,
    }
  },

  computed: {
    iconComponent () {
      const that = this
      let template
      if (this.tree.iconClass) {
        template = `<ta-icon type="${this.tree.iconClass}" :class="[{ 'is-leaf': this.node.isLeaf, expanded: !this.node.isLeaf && this.expanded },this.handleIconClass() ]" @click.stop="handleExpandIconClick"/>`
      } else if (this.tree.iconOpen && this.tree.iconClose) {
        template = `
        <ta-icon type="${(this.tree.iconOpen || this.tree.iconClose)}" v-if="this.expanded" :class="[{ 'is-leaf': this.node.isLeaf, expanded: !this.node.isLeaf && this.expanded },this.handleIconClass() ]" @click.stop="handleExpandIconClick"/>
        <ta-icon type="${this.tree.iconClose}" v-else="!this.expanded" :class="[{ 'is-leaf': this.node.isLeaf, expanded: !this.node.isLeaf && this.expanded },this.handleIconClass() ]" @click.stop="handleExpandIconClick"/>
        `
      } else {
        template = `<ta-icon type="caret-down" :class="[{ 'is-leaf': this.node.isLeaf, expanded: !this.node.isLeaf && this.expanded },this.handleIconClass() ]" @click.stop="handleExpandIconClick"/>`
      }

      return {
        props: ['node', 'expanded', 'type'],
        methods: {
          handleIconClass () {
            return that.handleIconClass()
          },
          handleExpandIconClick () {
            return that.handleExpandIconClick()
          },
        },
        template,
      }
    },
  },

  watch: {
    'node.indeterminate' (val) {
      this.handleSelectChange(this.node.checked, val)
    },

    'node.checked' (val) {
      this.handleSelectChange(val, this.node.indeterminate)
    },

    'node.expanded' (val) {
      this.$nextTick(() => this.expanded = val)
      if (val) {
        this.childNodeRendered = true
      }
    },
  },

  created () {
    const parent = this.$parent

    if (parent.isTree) {
      this.tree = parent
    } else {
      this.tree = parent.tree
    }

    const tree = this.tree
    if (!tree) {
      console.warn('Can not find node\'s tree.')
    }

    const props = tree.props || {}
    const childrenKey = props['children'] || 'children'

    this.$watch(`node.data.${childrenKey}`, () => {
      this.node.updateChildren()
    })

    if (this.node.expanded) {
      this.expanded = true
      this.childNodeRendered = true
    }

    if (this.tree.accordion) {
      this.$on('tree-node-expand', node => {
        if (this.node !== node) {
          this.node.collapse()
        }
      })
    }
  },

  methods: {
    getNodeKey (node) {
      return getNodeKey(this.tree.nodeKey, node.data)
    },

    emitCheckEvent: function (isFromCheckbox) {
      const store = this.tree.store
      this.tree.$emit('check', this.node.data, {
        checkedNodes: store.getCheckedNodes(),
        checkedKeys: store.getCheckedKeys(),
        halfCheckedNodes: store.getHalfCheckedNodes(),
        halfCheckedKeys: store.getHalfCheckedKeys(),
      }, isFromCheckbox)
    },

    handleSelectChange (checked, indeterminate) {
      if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
        this.tree.$emit('check-change', this.node.data, checked, indeterminate)
      }
      this.oldChecked = checked
      this.indeterminate = indeterminate
    },

    handleClick () {
      const store = this.tree.store
      store.setCurrentNode(this.node)
      this.tree.$emit('current-change', store.currentNode ? store.currentNode.data : null, store.currentNode)
      this.tree.currentNode = this
      if (this.tree.expandOnClickNode) {
        this.handleExpandIconClick()
      }
      if (this.tree.checkOnClickNode && !this.node.disabled) {
        this.handleCheckChange(null, {
          target: { checked: !this.node.checked },
        })
      }
      this.tree.$emit('node-click', this.node.data, this.node, this)
    },

    handleContextMenu (event) {
      if (this.tree._events['node-contextmenu'] && this.tree._events['node-contextmenu'].length > 0) {
        event.stopPropagation()
        event.preventDefault()
      }
      this.tree.$emit('node-contextmenu', event, this.node.data, this.node, this)
    },

    handleExpandIconClick () {
      if (this.node.isLeaf) return
      if (this.expanded) {
        this.tree.$emit('node-collapse', this.node.data, this.node, this)
        this.node.collapse()
      } else {
        this.node.expand()
        this.$emit('node-expand', this.node.data, this.node, this)
      }
    },

    handleCheckChange (e) {
      this.node.setChecked(e.target.checked, !this.tree.checkStrictly)
      this.$nextTick(() => {
        this.emitCheckEvent(true)
      })
    },

    handleChildNodeExpand (nodeData, node, instance) {
      this.broadcast('ElTreeNode', 'tree-node-expand', node)
      this.tree.$emit('node-expand', nodeData, node, instance)
    },

    handleDragStart (event) {
      if (!this.tree.draggable) return
      this.tree.$emit('tree-node-drag-start', event, this)
    },

    handleDragOver (event) {
      if (!this.tree.draggable) return
      this.tree.$emit('tree-node-drag-over', event, this)
      event.preventDefault()
    },

    handleDrop (event) {
      event.preventDefault()
    },

    handleDragEnd (event) {
      if (!this.tree.draggable) return
      this.tree.$emit('tree-node-drag-end', event, this)
    },

    handleIconClass () {
      if (this.tree.iconClass) {
        return `el-tree-node__expand-icon el-tree-node__expand-custom-icon`
      }
      if (this.tree.iconOpen && this.tree.iconClose) {
        return `el-tree-node__expand-icon1 el-tree-node__expand-custom-icon1`
      }
      return 'el-tree-node__expand-icon el-icon-caret-right'
    },
    syncNodeChildren () {
      this.node.removeChilds()
      this.node.expand()
    },
  },
}
</script>

<style>
  .el-tree-node__label{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space:nowrap;
    height:24px;
    /*display:block;*/
  }
</style>
