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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var tdesignIconsVue = require('tdesign-icons-vue');
var config = require('../config.js');
var button_index = require('../button/index.js');
var loading_index = require('../loading/index.js');
var upload_util = require('./util.js');
var upload_props = require('./props.js');
require('../button/button.js');
require('../utils/classnames.js');
require('../button/props.js');
require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('../utils/ripple.js');
require('../utils/set-style.js');
require('../loading/loading.js');
require('../loading/icon/gradient.js');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

var FlowList = Vue__default["default"].extend({
  name: "TUploadFlowList",
  components: {
    TButton: button_index.Button,
    TLoading: loading_index.Loading,
    TIconCheckCircleFilled: tdesignIconsVue.CheckCircleFilledIcon,
    TIconTimeFilled: tdesignIconsVue.TimeFilledIcon,
    TIconErrorCircleFilled: tdesignIconsVue.ErrorCircleFilledIcon,
    TIconBrowse: tdesignIconsVue.BrowseIcon,
    TIconDelete: tdesignIconsVue.DeleteIcon
  },
  props: {
    showUploadProgress: upload_props["default"].showUploadProgress,
    files: Array,
    toUploadFiles: Array,
    placeholder: String,
    autoUpload: Boolean,
    remove: Function,
    upload: Function,
    cancel: Function,
    display: {
      type: String,
      validator: function validator(val) {
        return ["file-flow", "image-flow"].includes(val);
      }
    }
  },
  data: function data() {
    return {
      dragActive: false,
      target: null
    };
  },
  computed: {
    showInitial: function showInitial() {
      var isWatingEmpty = !this.waitingUploadFiles || !this.waitingUploadFiles.length;
      return (!this.files || !this.files.length) && isWatingEmpty;
    },
    waitingUploadFiles: function waitingUploadFiles() {
      var _this = this;

      var list = [];
      this.toUploadFiles.forEach(function (item) {
        var r = _this.files.filter(function (t) {
          return t.name === item.name;
        });

        if (!r.length) {
          list.push(item);
        }
      });
      return list;
    },
    listFiles: function listFiles() {
      if (!this.files || !this.files.length) return this.toUploadFiles;
      return this.files.concat(this.waitingUploadFiles);
    },
    failedList: function failedList() {
      return this.toUploadFiles.filter(function (file) {
        return file.status === "fail";
      });
    },
    processList: function processList() {
      return this.toUploadFiles.filter(function (file) {
        return file.status === "progress";
      });
    },
    isUploading: function isUploading() {
      return !!this.processList.length;
    },
    allowUpload: function allowUpload() {
      return Boolean(this.waitingUploadFiles && this.waitingUploadFiles.length) && !this.isUploading;
    },
    uploadText: function uploadText() {
      if (this.isUploading) return "\u4E0A\u4F20\u4E2D...";
      return this.failedList && this.failedList.length ? "\u91CD\u65B0\u4E0A\u4F20" : "\u5F00\u59CB\u4E0A\u4F20";
    }
  },
  methods: {
    renderStatus: function renderStatus(file) {
      var h = this.$createElement;
      var status = null;

      switch (file.status) {
        case "success":
          status = h("div", {
            "class": "".concat(upload_util.UPLOAD_NAME, "__flow-status")
          }, [h(tdesignIconsVue.CheckCircleFilledIcon), h("span", ["\u4E0A\u4F20\u6210\u529F"])]);
          break;

        case "fail":
          status = h("div", {
            "class": "".concat(upload_util.UPLOAD_NAME, "__flow-status")
          }, [h(tdesignIconsVue.ErrorCircleFilledIcon), h("span", ["\u4E0A\u4F20\u5931\u8D25"])]);
          break;

        case "progress":
          this.showUploadProgress && (status = h("div", {
            "class": "".concat(upload_util.UPLOAD_NAME, "__flow-status")
          }, [h(loading_index.Loading), h("span", ["\u4E0A\u4F20\u4E2D ", Math.min(file.percent, 99), "%"])]));
          break;

        case "waiting":
          status = h("div", {
            "class": "".concat(upload_util.UPLOAD_NAME, "__flow-status")
          }, [h(tdesignIconsVue.TimeFilledIcon), h("span", ["\u5F85\u4E0A\u4F20"])]);
          break;
      }

      return status;
    },
    handleDrop: function handleDrop(event) {
      event.preventDefault();
      this.$emit("change", event.dataTransfer.files);
      this.$emit("dragleave", event);
      this.dragActive = false;
    },
    handleDragenter: function handleDragenter(event) {
      this.target = event.target;
      event.preventDefault();
      this.$emit("dragenter", event);
      this.dragActive = true;
    },
    handleDragleave: function handleDragleave(event) {
      if (this.target !== event.target) return;
      event.preventDefault();
      this.$emit("dragleave", event);
      this.dragActive = false;
    },
    handleDragover: function handleDragover(event) {
      event.preventDefault();
    },
    onViewClick: function onViewClick(event, file) {
      this.$emit("imgPreview", event, file);
    },
    renderDrager: function renderDrager() {
      var h = this.$createElement;
      return h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__flow-empty"),
        "on": {
          "drop": this.handleDrop,
          "dragenter": this.handleDragenter,
          "dragover": this.handleDragover,
          "dragleave": this.handleDragleave
        }
      }, [this.dragActive ? "\u91CA\u653E\u9F20\u6807" : "\u70B9\u51FB\u4E0A\u65B9\u201C\u9009\u62E9\u6587\u4EF6\u201D\u6216\u5C06\u6587\u4EF6\u62D6\u62FD\u5230\u6B64\u533A\u57DF"]);
    },
    renderFileList: function renderFileList() {
      var _this2 = this;

      var h = this.$createElement;
      return h("table", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__flow-table")
      }, [h("tr", [h("th", ["\u6587\u4EF6\u540D"]), h("th", ["\u5927\u5C0F"]), h("th", ["\u72B6\u6001"]), h("th", ["\u64CD\u4F5C"])]), this.showInitial && h("tr", [h("td", {
        "attrs": {
          "colspan": 4
        }
      }, [this.renderDrager()])]), this.listFiles.map(function (file, index) {
        return h("tr", [h("td", [upload_util.abridgeName(file.name, 7, 10)]), h("td", [upload_util.returnFileSize(file.size)]), h("td", [_this2.renderStatus(file)]), h("td", [h("span", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__flow-button"),
          "on": {
            "click": function click(e) {
              return _this2.remove({
                e: e,
                index: index,
                file: file
              });
            }
          }
        }, ["\u5220\u9664"])])]);
      })]);
    },
    renderImgList: function renderImgList() {
      var _this3 = this;

      var h = this.$createElement;
      return h("div", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__flow-card-area")
      }, [this.showInitial && this.renderDrager(), !!this.listFiles.length && h("ul", {
        "class": "".concat(upload_util.UPLOAD_NAME, "__card clearfix")
      }, [this.listFiles.map(function (file, index) {
        return h("li", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-item")
        }, [h("div", {
          "class": ["".concat(upload_util.UPLOAD_NAME, "__card-content"), _defineProperty__default["default"]({}, "".concat(config.prefix, "-is-bordered"), file.status !== "waiting")]
        }, [file.status === "fail" && h("div", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-status-wrap")
        }, [h(tdesignIconsVue.ErrorCircleFilledIcon), h("p", ["\u4E0A\u4F20\u5931\u8D25"])]), file.status === "progress" && h("div", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-status-wrap")
        }, [h(loading_index.Loading), h("p", ["\u4E0A\u4F20\u4E2D ", Math.min(file.percent, 99)])]), (["waiting", "success"].includes(file.status) || !file.status && file.url) && h("img", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-image"),
          "attrs": {
            "src": file.url || "//tdesign.gtimg.com/tdesign-default-img.png"
          }
        }), h("div", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-mask")
        }, [file.url && h("span", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-mask-item")
        }, [h(tdesignIconsVue.BrowseIcon, {
          "nativeOn": {
            "click": function click(e) {
              return _this3.onViewClick(e, file);
            }
          }
        }), h("span", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-mask-item-divider")
        })]), h("span", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-mask-item"),
          "on": {
            "click": function click(e) {
              return _this3.remove({
                e: e,
                index: index,
                file: file
              });
            }
          }
        }, [h(tdesignIconsVue.DeleteIcon)])])]), h("p", {
          "class": "".concat(upload_util.UPLOAD_NAME, "__card-name")
        }, [upload_util.abridgeName(file.name)])]);
      })])]);
    }
  },
  render: function render() {
    var _this4 = this;

    var h = arguments[0];
    return h("div", {
      "class": ["".concat(upload_util.UPLOAD_NAME, "__flow"), "".concat(upload_util.UPLOAD_NAME, "__flow-").concat(this.display)]
    }, [h("div", {
      "class": "".concat(upload_util.UPLOAD_NAME, "__flow-op")
    }, [this.$scopedSlots["default"] && this.$scopedSlots["default"](null), h("small", {
      "class": "".concat(config.prefix, "-size-s ").concat(upload_util.UPLOAD_NAME, "__flow-placeholder")
    }, [this.placeholder])]), this.display === "file-flow" && this.renderFileList(), this.display === "image-flow" && this.renderImgList(), h("div", {
      "class": "".concat(upload_util.UPLOAD_NAME, "__flow-bottom")
    }, [h(button_index.Button, {
      "attrs": {
        "theme": "default"
      },
      "on": {
        "click": this.cancel
      }
    }, ["\u53D6\u6D88"]), h(button_index.Button, {
      "attrs": {
        "disabled": !this.allowUpload,
        "theme": "primary"
      },
      "on": {
        "click": function click(e) {
          return _this4.upload(_this4.waitingUploadFiles, e);
        }
      }
    }, [this.uploadText])])]);
  }
});

exports["default"] = FlowList;
//# sourceMappingURL=flow-list.js.map
