UNPKG

winbox-ui-next

Version:

We Design Vue 2.0: A Vue.js 3 UI Library

125 lines (124 loc) 4.14 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); import { reactive, render, ref, createVNode } from "vue"; import { MESSAGE_TYPES } from "../_utils/constant.js"; import { getOverlay } from "../_utils/dom.js"; import { isString, isUndefined, isFunction } from "../_utils/is.js"; import MessageList from "./message-list.js"; class MessageManger { constructor(config, appContext) { this.messageCount = 0; this.add = (config2) => { var _a; this.messageCount++; const id = (_a = config2.id) != null ? _a : `__w_message_${this.messageCount}`; if (this.messageIds.has(id)) { return this.update(id, config2); } const message2 = reactive(__spreadValues({ id }, config2)); this.messages.value.push(message2); this.messageIds.add(id); return { close: () => this.remove(id) }; }; this.update = (id, config2) => { for (let i = 0; i < this.messages.value.length; i++) { if (this.messages.value[i].id === id) { const resetOnUpdate = !isUndefined(config2.duration); Object.assign(this.messages.value[i], __spreadProps(__spreadValues({}, config2), { id, resetOnUpdate })); break; } } return { close: () => this.remove(id) }; }; this.remove = (id) => { for (let i = 0; i < this.messages.value.length; i++) { const item = this.messages.value[i]; if (item.id === id) { if (isFunction(item.onClose)) { item.onClose(id); } this.messages.value.splice(i, 1); this.messageIds.delete(id); break; } } }; this.clear = () => { this.messages.value.splice(0); }; this.destroy = () => { if (this.messages.value.length === 0) { render(null, this.container); document.body.removeChild(this.container); messageInstance[this.position] = void 0; } }; const { position = "top" } = config; this.container = getOverlay("message"); this.messageIds = new Set(); this.messages = ref([]); this.position = position; const vm = createVNode(MessageList, { messages: this.messages.value, position, onClose: this.remove, onAfterClose: this.destroy }); if (appContext) { vm.appContext = appContext; } render(vm, this.container); document.body.appendChild(this.container); } } const messageInstance = {}; const types = [...MESSAGE_TYPES, "loading"]; const message = types.reduce((pre, value) => { pre[value] = (config) => { if (isString(config)) { config = { content: config }; } const _config = __spreadValues({ type: value }, config); const { position = "top" } = _config; if (!messageInstance[position]) { messageInstance[position] = new MessageManger(_config); } return messageInstance[position].add(_config); }; return pre; }, {}); message.clear = (position) => { var _a; if (position) { (_a = messageInstance[position]) == null ? void 0 : _a.clear(); } else { Object.values(messageInstance).forEach((item) => item.clear()); } }; const Message = __spreadProps(__spreadValues({}, message), { install: (app) => { app.config.globalProperties.$message = message; } }); export { Message as default };