"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _vueTypes = _interopRequireDefault(require("../../_util/vue-types"));

var _classNames = _interopRequireDefault(require("../../_util/classNames"));

var _util = require("./util");

var _propsUtil = require("../../_util/props-util");

var _BaseMixin = _interopRequireDefault(require("../../_util/BaseMixin"));

var _getTransitionProps = _interopRequireDefault(require("../../_util/getTransitionProps"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function noop() {}

var ICON_OPEN = 'open';
var ICON_CLOSE = 'close';
var defaultTitle = '---';
var TreeNode = {
  name: 'TreeNode',
  inheritAttrs: false,
  mixins: [_BaseMixin.default],
  __ANT_TREE_NODE: true,
  props: (0, _propsUtil.initDefaultProps)({
    eventKey: _vueTypes.default.oneOfType([_vueTypes.default.string, _vueTypes.default.number]),
    // Pass by parent `cloneElement`
    prefixCls: _vueTypes.default.string,
    // className: PropTypes.string,
    root: _vueTypes.default.object,
    // onSelect: PropTypes.func,
    // By parent
    expanded: _vueTypes.default.bool,
    selected: _vueTypes.default.bool,
    checked: _vueTypes.default.bool,
    loaded: _vueTypes.default.bool,
    loading: _vueTypes.default.bool,
    halfChecked: _vueTypes.default.bool,
    title: _vueTypes.default.any,
    pos: _vueTypes.default.string,
    dragOver: _vueTypes.default.bool,
    dragOverGapTop: _vueTypes.default.bool,
    dragOverGapBottom: _vueTypes.default.bool,
    // By user
    isLeaf: _vueTypes.default.bool,
    checkable: _vueTypes.default.bool,
    selectable: _vueTypes.default.bool,
    disabled: _vueTypes.default.bool,
    disableCheckbox: _vueTypes.default.bool,
    icon: _vueTypes.default.any,
    dataRef: _vueTypes.default.object,
    switcherIcon: _vueTypes.default.any,
    label: _vueTypes.default.any,
    value: _vueTypes.default.any
  }, {}),
  data: function data() {
    this.children = null;
    return {
      dragNodeHighlight: false
    };
  },
  setup: function setup() {
    return {
      vcTree: (0, _vue.inject)('vcTree', {}),
      vcTreeNode: (0, _vue.inject)('vcTreeNode', {})
    };
  },
  created: function created() {
    (0, _vue.provide)('vcTreeNode', this);
  },
  // Isomorphic needn't load data in server side
  mounted: function mounted() {
    var eventKey = this.eventKey,
        registerTreeNode = this.vcTree.registerTreeNode;
    this.syncLoadData(this.$props);
    registerTreeNode && registerTreeNode(eventKey, this);
  },
  updated: function updated() {
    this.syncLoadData(this.$props);
  },
  beforeUnmount: function beforeUnmount() {
    var eventKey = this.eventKey,
        registerTreeNode = this.vcTree.registerTreeNode;
    registerTreeNode && registerTreeNode(eventKey, null);
  },
  methods: {
    onSelectorClick: function onSelectorClick(e) {
      // Click trigger before select/check operation
      var onNodeClick = this.vcTree.onNodeClick;
      onNodeClick(e, this);

      if (this.isSelectable()) {
        this.onSelect(e);
      } else {
        this.onCheck(e);
      }
    },
    onSelectorDoubleClick: function onSelectorDoubleClick(e) {
      var onNodeDoubleClick = this.vcTree.onNodeDoubleClick;
      onNodeDoubleClick(e, this);
    },
    onSelect: function onSelect(e) {
      if (this.isDisabled()) return;
      var onNodeSelect = this.vcTree.onNodeSelect;
      e.preventDefault();
      onNodeSelect(e, this);
    },
    onCheck: function onCheck(e) {
      if (this.isDisabled()) return;
      var disableCheckbox = this.disableCheckbox,
          checked = this.checked;
      var onNodeCheck = this.vcTree.onNodeCheck;
      if (!this.isCheckable() || disableCheckbox) return;
      e.preventDefault();
      var targetChecked = !checked;
      onNodeCheck(e, this, targetChecked);
    },
    onMouseEnter: function onMouseEnter(e) {
      var onNodeMouseEnter = this.vcTree.onNodeMouseEnter;
      onNodeMouseEnter(e, this);
    },
    onMouseLeave: function onMouseLeave(e) {
      var onNodeMouseLeave = this.vcTree.onNodeMouseLeave;
      onNodeMouseLeave(e, this);
    },
    onContextMenu: function onContextMenu(e) {
      var onNodeContextMenu = this.vcTree.onNodeContextMenu;
      onNodeContextMenu(e, this);
    },
    onDragStart: function onDragStart(e) {
      var onNodeDragStart = this.vcTree.onNodeDragStart;
      e.stopPropagation();
      this.setState({
        dragNodeHighlight: true
      });
      onNodeDragStart(e, this);

      try {
        // ie throw error
        // firefox-need-it
        e.dataTransfer.setData('text/plain', '');
      } catch (error) {// empty
      }
    },
    onDragEnter: function onDragEnter(e) {
      var onNodeDragEnter = this.vcTree.onNodeDragEnter;
      e.preventDefault();
      e.stopPropagation();
      onNodeDragEnter(e, this);
    },
    onDragOver: function onDragOver(e) {
      var onNodeDragOver = this.vcTree.onNodeDragOver;
      e.preventDefault();
      e.stopPropagation();
      onNodeDragOver(e, this);
    },
    onDragLeave: function onDragLeave(e) {
      var onNodeDragLeave = this.vcTree.onNodeDragLeave;
      e.stopPropagation();
      onNodeDragLeave(e, this);
    },
    onDragEnd: function onDragEnd(e) {
      var onNodeDragEnd = this.vcTree.onNodeDragEnd;
      e.stopPropagation();
      this.setState({
        dragNodeHighlight: false
      });
      onNodeDragEnd(e, this);
    },
    onDrop: function onDrop(e) {
      var onNodeDrop = this.vcTree.onNodeDrop;
      e.preventDefault();
      e.stopPropagation();
      this.setState({
        dragNodeHighlight: false
      });
      onNodeDrop(e, this);
    },
    // Disabled item still can be switch
    onExpand: function onExpand(e) {
      var onNodeExpand = this.vcTree.onNodeExpand;
      onNodeExpand(e, this);
    },
    // Drag usage
    setSelectHandle: function setSelectHandle(node) {
      this.selectHandle = node;
    },
    getNodeChildren: function getNodeChildren() {
      var originList = this.children;
      var targetList = (0, _util.getNodeChildren)(originList);

      if (originList.length !== targetList.length) {
        (0, _util.warnOnlyTreeNode)();
      }

      return targetList;
    },
    getNodeState: function getNodeState() {
      var expanded = this.expanded;

      if (this.isLeaf2()) {
        return null;
      }

      return expanded ? ICON_OPEN : ICON_CLOSE;
    },
    isLeaf2: function isLeaf2() {
      var isLeaf = this.isLeaf,
          loaded = this.loaded;
      var loadData = this.vcTree.loadData;
      var hasChildren = this.getNodeChildren().length !== 0;

      if (isLeaf === false) {
        return false;
      }

      return isLeaf || !loadData && !hasChildren || loadData && loaded && !hasChildren;
    },
    isDisabled: function isDisabled() {
      var disabled = this.disabled;
      var treeDisabled = this.vcTree.disabled; // Follow the logic of Selectable

      if (disabled === false) {
        return false;
      }

      return !!(treeDisabled || disabled);
    },
    isCheckable: function isCheckable() {
      var checkable = this.$props.checkable;
      var treeCheckable = this.vcTree.checkable; // Return false if tree or treeNode is not checkable

      if (!treeCheckable || checkable === false) return false;
      return treeCheckable;
    },
    // Load data to avoid default expanded tree without data
    syncLoadData: function syncLoadData(props) {
      var expanded = props.expanded,
          loading = props.loading,
          loaded = props.loaded;
      var _this$vcTree = this.vcTree,
          loadData = _this$vcTree.loadData,
          onNodeLoad = _this$vcTree.onNodeLoad;
      if (loading) return; // read from state to avoid loadData at same time

      if (loadData && expanded && !this.isLeaf2()) {
        // We needn't reload data when has children in sync logic
        // It's only needed in node expanded
        var hasChildren = this.getNodeChildren().length !== 0;

        if (!hasChildren && !loaded) {
          onNodeLoad(this);
        }
      }
    },
    isSelectable: function isSelectable() {
      var selectable = this.selectable;
      var treeSelectable = this.vcTree.selectable; // Ignore when selectable is undefined or null

      if (typeof selectable === 'boolean') {
        return selectable;
      }

      return treeSelectable;
    },
    // Switcher
    renderSwitcher: function renderSwitcher() {
      var expanded = this.expanded;
      var prefixCls = this.vcTree.prefixCls;
      var switcherIcon = (0, _propsUtil.getComponent)(this, 'switcherIcon', {}, false) || (0, _propsUtil.getComponent)(this.vcTree, 'switcherIcon', {}, false);

      if (this.isLeaf2()) {
        return _vue.createVNode("span", {
          "key": "switcher",
          "class": (0, _classNames.default)("".concat(prefixCls, "-switcher"), "".concat(prefixCls, "-switcher-noop"))
        }, [typeof switcherIcon === 'function' ? switcherIcon(_objectSpread(_objectSpread(_objectSpread({}, this.$props), this.$props.dataRef), {}, {
          isLeaf: true
        })) : switcherIcon]);
      }

      var switcherCls = (0, _classNames.default)("".concat(prefixCls, "-switcher"), "".concat(prefixCls, "-switcher_").concat(expanded ? ICON_OPEN : ICON_CLOSE));
      return _vue.createVNode("span", {
        "key": "switcher",
        "onClick": this.onExpand,
        "class": switcherCls
      }, [typeof switcherIcon === 'function' ? switcherIcon(_objectSpread(_objectSpread(_objectSpread({}, this.$props), this.$props.dataRef), {}, {
        isLeaf: false
      })) : switcherIcon]);
    },
    // Checkbox
    renderCheckbox: function renderCheckbox() {
      var checked = this.checked,
          halfChecked = this.halfChecked,
          disableCheckbox = this.disableCheckbox;
      var prefixCls = this.vcTree.prefixCls;
      var disabled = this.isDisabled();
      var checkable = this.isCheckable();
      if (!checkable) return null; // [Legacy] Custom element should be separate with `checkable` in future

      var $custom = typeof checkable !== 'boolean' ? checkable : null;
      return _vue.createVNode("span", {
        "key": "checkbox",
        "class": (0, _classNames.default)("".concat(prefixCls, "-checkbox"), checked && "".concat(prefixCls, "-checkbox-checked"), !checked && halfChecked && "".concat(prefixCls, "-checkbox-indeterminate"), (disabled || disableCheckbox) && "".concat(prefixCls, "-checkbox-disabled")),
        "onClick": this.onCheck
      }, [$custom]);
    },
    renderIcon: function renderIcon() {
      var loading = this.loading;
      var prefixCls = this.vcTree.prefixCls;
      return _vue.createVNode("span", {
        "key": "icon",
        "class": (0, _classNames.default)("".concat(prefixCls, "-iconEle"), "".concat(prefixCls, "-icon__").concat(this.getNodeState() || 'docu'), loading && "".concat(prefixCls, "-icon_loading"))
      }, null);
    },
    // Icon + Title
    renderSelector: function renderSelector() {
      var selected = this.selected,
          loading = this.loading,
          dragNodeHighlight = this.dragNodeHighlight;
      var icon = (0, _propsUtil.getComponent)(this, 'icon', {}, false);
      var _this$vcTree2 = this.vcTree,
          prefixCls = _this$vcTree2.prefixCls,
          showIcon = _this$vcTree2.showIcon,
          treeIcon = _this$vcTree2.icon,
          draggable = _this$vcTree2.draggable,
          loadData = _this$vcTree2.loadData;
      var disabled = this.isDisabled();
      var title = (0, _propsUtil.getComponent)(this, 'title', {}, false);
      var wrapClass = "".concat(prefixCls, "-node-content-wrapper"); // Icon - Still show loading icon when loading without showIcon

      var $icon;

      if (showIcon) {
        var currentIcon = icon || treeIcon;
        $icon = currentIcon ? _vue.createVNode("span", {
          "class": (0, _classNames.default)("".concat(prefixCls, "-iconEle"), "".concat(prefixCls, "-icon__customize"))
        }, [typeof currentIcon === 'function' ? currentIcon(_objectSpread(_objectSpread({}, this.$props), this.$props.dataRef)) : currentIcon]) : this.renderIcon();
      } else if (loadData && loading) {
        $icon = this.renderIcon();
      }

      var currentTitle = title;
      var $title = currentTitle ? _vue.createVNode("span", {
        "class": "".concat(prefixCls, "-title")
      }, [typeof currentTitle === 'function' ? currentTitle(_objectSpread(_objectSpread({}, this.$props), this.$props.dataRef)) : currentTitle]) : _vue.createVNode("span", {
        "class": "".concat(prefixCls, "-title")
      }, [defaultTitle]);
      return _vue.createVNode("span", {
        "key": "selector",
        "ref": this.setSelectHandle,
        "title": typeof title === 'string' ? title : '',
        "class": (0, _classNames.default)("".concat(wrapClass), "".concat(wrapClass, "-").concat(this.getNodeState() || 'normal'), !disabled && (selected || dragNodeHighlight) && "".concat(prefixCls, "-node-selected"), !disabled && draggable && 'draggable'),
        "draggable": !disabled && draggable || undefined,
        "aria-grabbed": !disabled && draggable || undefined,
        "onMouseenter": this.onMouseEnter,
        "onMouseleave": this.onMouseLeave,
        "onContextmenu": this.onContextMenu,
        "onClick": this.onSelectorClick,
        "onDblclick": this.onSelectorDoubleClick,
        "onDragstart": draggable ? this.onDragStart : noop
      }, [$icon, $title]);
    },
    // Children list wrapped with `Animation`
    renderChildren: function renderChildren() {
      var expanded = this.expanded,
          pos = this.pos;
      var _this$vcTree3 = this.vcTree,
          prefixCls = _this$vcTree3.prefixCls,
          openTransitionName = _this$vcTree3.openTransitionName,
          openAnimation = _this$vcTree3.openAnimation,
          renderTreeNode = _this$vcTree3.renderTreeNode;
      var animProps = {};

      if (openTransitionName) {
        animProps = (0, _getTransitionProps.default)(openTransitionName);
      } else if (_typeof(openAnimation) === 'object') {
        animProps = _objectSpread(_objectSpread({}, openAnimation), {}, {
          css: false
        }, animProps);
      } // Children TreeNode


      var nodeList = this.getNodeChildren();

      if (nodeList.length === 0) {
        return null;
      }

      var $children;

      if (expanded) {
        $children = _vue.createVNode("ul", {
          "class": (0, _classNames.default)("".concat(prefixCls, "-child-tree"), expanded && "".concat(prefixCls, "-child-tree-open")),
          "data-expanded": expanded,
          "role": "group"
        }, [(0, _util.mapChildren)(nodeList, function (node, index) {
          return renderTreeNode(node, index, pos);
        })]);
      }

      return _vue.createVNode(_vue.Transition, animProps, {
        default: function _default() {
          return [$children];
        }
      });
    }
  },
  render: function render() {
    var _ref;

    this.children = (0, _propsUtil.getSlot)(this);
    var _this$$props = this.$props,
        dragOver = _this$$props.dragOver,
        dragOverGapTop = _this$$props.dragOverGapTop,
        dragOverGapBottom = _this$$props.dragOverGapBottom,
        isLeaf = _this$$props.isLeaf,
        expanded = _this$$props.expanded,
        selected = _this$$props.selected,
        checked = _this$$props.checked,
        halfChecked = _this$$props.halfChecked,
        loading = _this$$props.loading;
    var _this$vcTree4 = this.vcTree,
        prefixCls = _this$vcTree4.prefixCls,
        filterTreeNode = _this$vcTree4.filterTreeNode,
        draggable = _this$vcTree4.draggable;
    var disabled = this.isDisabled();
    var dataOrAriaAttributeProps = (0, _util.getDataAndAria)(_objectSpread(_objectSpread({}, this.$props), this.$attrs));
    var _this$$attrs = this.$attrs,
        className = _this$$attrs.class,
        style = _this$$attrs.style;
    return _vue.createVNode("li", _vue.mergeProps(dataOrAriaAttributeProps, {
      "class": (_ref = {
        className: className
      }, _defineProperty(_ref, "".concat(prefixCls, "-treenode-disabled"), disabled), _defineProperty(_ref, "".concat(prefixCls, "-treenode-switcher-").concat(expanded ? 'open' : 'close'), !isLeaf), _defineProperty(_ref, "".concat(prefixCls, "-treenode-checkbox-checked"), checked), _defineProperty(_ref, "".concat(prefixCls, "-treenode-checkbox-indeterminate"), halfChecked), _defineProperty(_ref, "".concat(prefixCls, "-treenode-selected"), selected), _defineProperty(_ref, "".concat(prefixCls, "-treenode-loading"), loading), _defineProperty(_ref, 'drag-over', !disabled && dragOver), _defineProperty(_ref, 'drag-over-gap-top', !disabled && dragOverGapTop), _defineProperty(_ref, 'drag-over-gap-bottom', !disabled && dragOverGapBottom), _defineProperty(_ref, 'filter-node', filterTreeNode && filterTreeNode(this)), _ref),
      "style": style,
      "role": "treeitem",
      "onDragenter": draggable ? this.onDragEnter : noop,
      "onDragover": draggable ? this.onDragOver : noop,
      "onDragleave": draggable ? this.onDragLeave : noop,
      "onDrop": draggable ? this.onDrop : noop,
      "onDragend": draggable ? this.onDragEnd : noop
    }), [this.renderSwitcher(), this.renderCheckbox(), this.renderSelector(), this.renderChildren()]);
  }
};
TreeNode.isTreeNode = 1;
var _default2 = TreeNode;
exports.default = _default2;