t-comm
Version: 
专业、稳定、纯粹的工具库
228 lines (223 loc) • 5.48 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopNamespace(e) {
    if (e && e.__esModule) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n["default"] = e;
    return Object.freeze(n);
}
var loadingToastId = {};
var toastId = {};
var toastComp;
function getToast() {
  return new Promise(function (resolve) {
    if (toastComp) {
      resolve(toastComp);
      return;
    }
    Promise.all([Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('press-ui/press-toast/index')); })]).then(function (comp) {
      var _a;
      toastComp = (_a = comp === null || comp === void 0 ? void 0 : comp[0]) === null || _a === void 0 ? void 0 : _a["default"];
      resolve(toastComp);
    });
  });
}
/**
 * 显示普通Toast
 * @param text  文案
 * @param duration  显示时间,默认2秒
 * @example
 * ```ts
 * Toast.show('文本', 3000);
 * ```
 */
var showToast = function showToast(text, duration) {
  if (text === void 0) {
    text = '';
  }
  if (duration === void 0) {
    duration = 2000;
  }
  getToast().then(function () {
    if (loadingToastId === null || loadingToastId === void 0 ? void 0 : loadingToastId.clear) {
      setTimeout(function () {
        toastId = toastComp({
          message: text,
          zIndex: 10000,
          duration: duration
        });
      }, 100);
    } else {
      toastId = toastComp({
        message: text,
        zIndex: 10000,
        duration: duration
      });
    }
  });
};
/**
 * 显示成功样式Toast(toast带√样式)
 * @param text  文案
 * @param duration  显示时间,默认2秒
 * @example
 * ```ts
 * Toast.showSuccess('文本', 3000);
 * ```
 */
var showSuccess = function showSuccess(text, duration) {
  if (text === void 0) {
    text = '';
  }
  if (duration === void 0) {
    duration = 2000;
  }
  getToast().then(function () {
    if (loadingToastId === null || loadingToastId === void 0 ? void 0 : loadingToastId.clear) {
      setTimeout(function () {
        toastId = toastComp.success({
          message: text,
          zIndex: 10000,
          duration: duration
        });
      }, 100);
    } else {
      toastId = toastComp.success({
        message: text,
        zIndex: 10000,
        duration: duration
      });
    }
  });
};
/**
 * 显示失败样式 Toast(toast带!样式)
 * @param text  文案
 * @param duration  显示时间,默认2秒
 * @example
 * ```ts
 * Toast.showFail('文本', 3000);
 * ```
 */
var showFail = function showFail(text, duration) {
  if (text === void 0) {
    text = '';
  }
  if (duration === void 0) {
    duration = 2000;
  }
  getToast().then(function () {
    if (loadingToastId === null || loadingToastId === void 0 ? void 0 : loadingToastId.clear) {
      setTimeout(function () {
        toastId = toastComp.fail({
          message: text,
          zIndex: 10000,
          duration: duration
        });
      }, 100);
    } else {
      toastId = toastComp.fail({
        message: text,
        zIndex: 10000,
        duration: duration
      });
    }
  });
};
/**
 * 清除(隐藏)上一个toast
 * @example
 * ```ts
 * Toast.clear();
 *
 * clearToast();
 * ```
 */
var clearToast = function clearToast() {
  if (toastId.clear) toastId.clear();
};
/**
 * 显示loading Toast
 * @param {string | object} options 配置,传递字符串时候为message
 * @param {string} options.message 内容
 * @param {number} options.duration 展示时长(ms),值为 0 时,toast 不会消失
 * @param {boolean} options.forbidClick 是否禁止背景点击
 * @param {string} options.selector 自定义选择器
 * @example
 * ```ts
 * Toast.showLoading('文本');
 *
 * Toast.showLoading({
 *   message: '文本',
 *   zIndex: 1000,
 * });
 * ```
 */
var showLoading = function showLoading(options) {
  if (options === void 0) {
    options = {};
  }
  getToast().then(function () {
    if (loadingToastId === null || loadingToastId === void 0 ? void 0 : loadingToastId.clear) {
      loadingToastId.clear();
      loadingToastId = {};
    }
    if (typeof options === 'string') {
      loadingToastId = toastComp.loading({
        message: options,
        duration: 0,
        zIndex: 10000
      });
    } else {
      loadingToastId = toastComp.loading(options);
    }
  });
};
/**
 * 隐藏loading toast
 * @example
 * ```ts
 * Toast.dismissLoading();
 * ```
 */
var dismissLoading = function dismissLoading() {
  if (loadingToastId === null || loadingToastId === void 0 ? void 0 : loadingToastId.clear) {
    loadingToastId.clear();
    loadingToastId = {};
  }
};
/**
 * Toast 对象
 * @example
 * ```ts
 * Toast.show('文本')
 * ```
 */
var Toast = {
  show: showToast,
  showToast: showToast,
  showSuccess: showSuccess,
  showFail: showFail,
  clear: clearToast,
  clearToast: clearToast,
  showLoading: showLoading,
  dismissLoading: dismissLoading
};
exports.Toast = Toast;
exports.clearToast = clearToast;
exports.dismissLoading = dismissLoading;
exports.showFail = showFail;
exports.showLoading = showLoading;
exports.showSuccess = showSuccess;
exports.showToast = showToast;