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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var Vue = require('vue');
var config = require('../../../config.js');
var utils_event = require('../../../utils/event.js');
require('../../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/defineProperty');
require('lodash/camelCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

var rowDraggable = Vue__default["default"].extend({
  name: "".concat(config.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__default["default"](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) {
      utils_event.emitEvent(this, "drag-sort", {
        current: current,
        target: target,
        currentIndex: currentIndex,
        targetIndex: targetIndex
      });
    }
  }
});

exports["default"] = rowDraggable;
//# sourceMappingURL=row-draggable.js.map
