@cqmcui/cqmcui
Version:
轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)
226 lines (225 loc) • 6.19 kB
JavaScript
import { ref, watch, resolveComponent, openBlock, createBlock, withCtx, createElementVNode, normalizeClass, normalizeStyle, renderSlot, createElementBlock, Fragment, createTextVNode, toDisplayString, createVNode, render, onMounted, h } from "vue";
import { c as createComponent } from "./component-81a4c1d0.js";
import { P as Popup } from "./index-c55ad69e.js";
import { _ as _export_sfc } from "./_plugin-vue_export-helper-cc2b3d55.js";
import "../locale/lang";
import "./Overlay.js";
import "@cqmcui/icons-vue";
const { create } = createComponent("notify");
const _sfc_main = create({
components: {
[Popup.name]: Popup
},
props: {
id: String,
color: { type: String, default: "" },
msg: { type: String, default: "" },
duration: { type: Number, default: 3e3 },
className: {
type: String,
default: ""
},
background: { type: String, default: "" },
type: {
type: String,
default: "danger"
},
visible: {
type: Boolean,
default: false
},
position: {
type: String,
default: "top"
},
teleportDisable: {
type: Boolean,
default: true
},
onClose: Function,
onClick: Function,
unmount: Function
},
emits: ["update:visible"],
setup(props, { emit }) {
const clickCover = () => {
props.onClick && props.onClick();
};
let timer = null;
const clearTimer = () => {
timer && clearTimeout(timer);
timer = null;
};
const hide = () => {
emit("update:visible", false);
};
const isShowPopup = ref(false);
const unWatch = watch(
() => props.visible,
(newVal) => {
isShowPopup.value = props.visible;
const DURATION = props.duration;
if (newVal && DURATION) {
timer = setTimeout(() => {
hide();
}, DURATION);
}
},
{ immediate: true }
);
const onAfterLeave = () => {
clearTimer();
unWatch && unWatch();
props.unmount && props.unmount(props.id);
props.onClose && props.onClose();
};
return { onAfterLeave, clickCover, isShowPopup };
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_nut_popup = resolveComponent("cqmc-popup");
return openBlock(), createBlock(_component_nut_popup, {
visible: _ctx.isShowPopup,
"onUpdate:visible": _cache[1] || (_cache[1] = ($event) => _ctx.isShowPopup = $event),
position: _ctx.position,
overlay: false,
teleportDisable: _ctx.teleportDisable
}, {
default: withCtx(() => [
createElementVNode("div", {
class: normalizeClass(["cqmc-notify", `cqmc-notify--${_ctx.type}`, _ctx.className]),
style: normalizeStyle({ color: _ctx.color, background: _ctx.background }),
onClick: _cache[0] || (_cache[0] = (...args) => _ctx.clickCover && _ctx.clickCover(...args))
}, [
_ctx.$slots.default ? renderSlot(_ctx.$slots, "default", { key: 0 }) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
createTextVNode(toDisplayString(_ctx.msg), 1)
], 64))
], 6)
]),
_: 3
}, 8, ["visible", "position", "teleportDisable"]);
}
const Notify = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
const defaultOptions = {
type: "base",
visible: true,
msg: "",
color: void 0,
background: void 0,
duration: 3e3,
className: "",
onClose: Function,
// onClick: null,
// onOpened: null,
// textTimer: null,
teleport: "",
unmount: new Function()
};
let idsMap = [];
let optsMap = [];
const clearNotify = (id) => {
if (id) {
const container = document.getElementById(id);
optsMap = optsMap.filter((item) => item.id !== id);
idsMap = idsMap.filter((item) => item !== id);
if (container) {
document.body.removeChild(container);
}
} else {
idsMap.forEach((item) => {
const container = document.getElementById(item);
if (container) {
document.body.removeChild(container);
}
});
optsMap = [];
idsMap = [];
}
};
const updateNotify = (opts) => {
const container = document.getElementById(opts.id);
if (container) {
const currentOpt = optsMap.find((item) => item.id === opts.id);
if (currentOpt) {
opts = { ...defaultOptions, ...currentOpt, ...opts };
} else {
opts = { ...defaultOptions, ...opts };
}
const instance = createVNode(Notify, opts);
render(instance, container);
return instance.component.data;
}
};
const mountNotify = (opts) => {
opts.unmount = clearNotify;
let _id;
if (opts.id) {
_id = opts.id;
if (idsMap.find((item) => item === opts.id)) {
return updateNotify(opts);
}
} else {
_id = (/* @__PURE__ */ new Date()).getTime() + "";
}
opts = { ...defaultOptions, ...opts };
opts.id = _id;
idsMap.push(opts.id);
optsMap.push(opts);
const root = document.createElement("view");
root.id = "notify-" + opts.id;
const Wrapper = {
setup() {
opts.teleport = `#notify-${opts.id}`;
onMounted(() => {
setTimeout(() => {
opts.onClose && opts.onClose();
document.body.removeChild(root);
}, opts.duration);
});
return () => {
return h(Notify, opts);
};
}
};
const instance = createVNode(Wrapper);
document.body.appendChild(root);
render(instance, root);
};
const errorMsg = (msg) => {
if (!msg) {
console.warn("[cqmcui Notify]: msg不能为空");
return;
}
};
const showNotify = {
text(msg, obj = {}) {
errorMsg(msg);
return mountNotify({ ...obj, msg });
},
primary(msg, obj = {}) {
errorMsg(msg);
return mountNotify({ ...obj, msg, type: "primary" });
},
success(msg, obj = {}) {
errorMsg(msg);
return mountNotify({ ...obj, msg, type: "success" });
},
danger(msg, obj = {}) {
errorMsg(msg);
return mountNotify({ ...obj, msg, type: "danger" });
},
warn(msg, obj = {}) {
errorMsg(msg);
return mountNotify({ ...obj, msg, type: "warning" });
},
hide() {
clearNotify();
},
install(app) {
app.use(Notify);
}
};
export {
Notify as default,
showNotify
};