/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import Vue from 'vue';
import { prefix } from '../../../config.js';
import { emitEvent } from '../../../utils/event.js';
import '../../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/defineProperty';
import 'lodash/camelCase';

var rowDraggable = Vue.extend({
  name: "".concat(prefix, "-primary-table-row-draggable"),
  data: function data() {
    return {
      draggingRowCurrentIndex: -1,
      currentRowData: null
    };
  },
  computed: {
    dragging: function dragging() {
      return this.draggingRowCurrentIndex !== -1;
    }
  },
  methods: {
    onDragStart: function onDragStart(_ref) {
      var index = _ref.index,
          row = _ref.row;
      this.draggingRowCurrentIndex = index;
      this.currentRowData = row;
      this.addDragEndListener();
    },
    onDragOver: function onDragOver(_ref2) {
      var overIndex = _ref2.index,
          row = _ref2.row,
          targetElm = _ref2.targetElm;
      if (_toConsumableArray(targetElm.classList).includes("v-move")) return;
      var draggingRowCurrentIndex = this.draggingRowCurrentIndex;
      if (draggingRowCurrentIndex === -1 || draggingRowCurrentIndex === overIndex) return;
      this.emitChange(this.currentRowData, row, draggingRowCurrentIndex, overIndex);
      this.draggingRowCurrentIndex = overIndex;
    },
    addDragEndListener: function addDragEndListener() {
      var _this = this;

      var onDragEnd = function onDragEnd() {
        _this.draggingRowCurrentIndex = -1;
        document.removeEventListener("dragend", onDragEnd);
      };

      document.addEventListener("dragend", onDragEnd);
    },
    emitChange: function emitChange(current, target, currentIndex, targetIndex) {
      emitEvent(this, "drag-sort", {
        current: current,
        target: target,
        currentIndex: currentIndex,
        targetIndex: targetIndex
      });
    }
  }
});

export { rowDraggable as default };
//# sourceMappingURL=row-draggable.js.map
