UNPKG

ten-design-vue

Version:

ten-vue

123 lines (99 loc) 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _vue = _interopRequireDefault(require("vue")); var _message = _interopRequireDefault(require("./message")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var instances = []; var seed = 0; var container; var config = {}; var message = function message(options) { if (!container) { container = document.createElement('div'); container.style.position = 'fixed'; container.style.top = "".concat(options.offset || 14, "px"); container.style.height = 0; container.style.zIndex = 99999; document.body.appendChild(container); } seed += 1; var id = "message__".concat(seed); var instance = { id: id, node: {}, wrapper: {} }; var MComponent = _vue.default.extend({ render: function render(h) { return h(_message.default, { props: _objectSpread(_objectSpread(_objectSpread({}, config), options), {}, { onClose: function onClose() { message.onClose(id, options.onClose); }, onAfterClose: function onAfterClose() { instance.wrapper.$destroy(); if (!instances.length && container) { document.body.removeChild(container); container = null; } } }) }); } }); instance.wrapper = new MComponent().$mount(); instance.node = instance.wrapper.$children[0]; // eslint-disable-line instances.push(instance); container.appendChild(instance.node.$el); return instance.node; }; var methods = ['success', 'warning', 'info', 'error']; methods.forEach(function (type) { message[type] = function (options) { var opts = options; if (typeof options === 'string') { opts = { content: options }; } return message(_objectSpread(_objectSpread({}, opts), {}, { type: type })); }; }); message.close = function (id) { var instance = instances.find(function (item) { return item.id === id; }); if (instance) { instance.node.close(); container.removeChild(instance.node.$el); } }; message.onClose = function (id, userOnClose) { for (var i = 0, len = instances.length; i < len; i++) { if (id === instances[i].id) { if (typeof userOnClose === 'function') { userOnClose(instances[i]); } instances.splice(i, 1); break; } } }; message.closeAll = function () { for (var i = instances.length - 1; i >= 0; i--) { message.close(instances[i].id); } }; message.config = function (options) { config = options; }; var _default = message; exports.default = _default;