/** *auth: liuyu 4654081@qq.com *Date: 2021/7/31 *Desc: layui消息提示插件 * */ layui.define(function (exports) { "use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } !function (global, factory) { (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.notify = factory()); }(void 0, function () { "use strict"; function c(args, children) { var el = document.createElement("div"); for (var key in args) { var element = args[key]; if (key == "className") { key = "class"; el.setAttribute(key, element); } else if (key[0] == "_") { el.addEventListener(key.slice(1), element); } } if (typeof children == "string") { el.innerHTML = children; } else if (_typeof(children) == "object" && children.tagName) { el.appendChild(children); } else if (children) { for (var i = 0; i < children.length; i++) { var child = children[i]; el.appendChild(child); } } return el; } function addAnimationEnd(el, fn) { ["a", "webkitA"].forEach(function (prefix) { var name = prefix + "nimationEnd"; el.addEventListener(name, function () { fn(); }); }); } function css(el, css) { for (var key in css) { el.style[key] = css[key]; } if (el.getAttribute("style") === "") { el.removeAttribute("style"); } } function addClass(el, s) { var c = el.className || ""; if (!hasClass(c, s)) { var arr = c.split(/\s+/); arr.push(s); el.className = arr.join(" "); } } function hasClass(c, s) { return c.indexOf(s) > -1 ? !0 : !1; } function removeClass(el, s) { var c = el.className || ""; if (hasClass(c, s)) { var arr = c.split(/\s+/); var i = arr.indexOf(s); arr.splice(i, 1); el.className = arr.join(" "); } if (el.className === "") { el.removeAttribute("class"); } } var initArgs = { msg: "", //文字内容 position: 'topCenter', // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter, center duration: 2000, //默认2秒关闭 showClose: true //显示关闭按钮 }; var notify = { info: function info() { initConfig(arguments, "info"); }, success: function success() { initConfig(arguments, "success"); }, warning: function warning() { initConfig(arguments, "warning"); }, error: function error() { initConfig(arguments, "error"); }, loading: function loading() { return initConfig(arguments, "loading"); }, destroyAll: function destroyAll() { _destroyAll(); }, config: function config(obj) { for (var key in obj) { if (Object.hasOwnProperty.call(obj, key)) { if (obj[key] !== undefined) { initArgs[key] = obj[key]; } } } } }; function initConfig(obj, type) { var args = {}; for (var key in initArgs) { args[key] = initArgs[key]; } var posArr = ["bottomRight", "bottomLeft", "topRight", "topLeft", "topCenter", "bottomCenter", "center"]; for (var i = 0; i < obj.length; i++) { var it = obj[i]; if (it !== undefined) { if (typeof it == "string" || _typeof(it) === "object") { if (posArr.indexOf(it) > -1) { args.position = it; } else { args.msg = it; } } else if (typeof it == "boolean") { args.showClose = it; } else if (typeof it == "function") { args.onClose = it; } else if (typeof it == "number") { args.duration = it; } } } args.type = type; return createMsgEl(args); } var msgWrappers = new Array(); function createMsgEl(args) { var _msgWrapper; var type = args.type, duration = args.duration, msg = args.msg, position = args.position, closable = args.showClose, onClose = args.onClose; var iconObj = getIconObj(); if (document.getElementsByClassName(position)[0]) { _msgWrapper = document.getElementsByClassName(position)[0]; } else { _msgWrapper = c({ className: "notify-msg-stage " + position }); msgWrappers.push(_msgWrapper); } if (type === "loading") { msg = msg === "" ? "正在加载,请稍后" : msg; closable = false; //loading不显示关闭按钮 } var el = c({ className: "notify-msg-wrapper" }, [c({ className: "notify-msg notify-msg-fade-in " + type }, [c({ className: "notify-msg-icon" }, iconObj[type]), c({ className: "notify-msg-content" }, msg), c({ className: "notify-msg-wait " + (closable ? "notify-msg-pointer" : ""), _click: function _click() { if (closable) { closeFlag = true; //点击关闭按钮标志 flag = false; //正常关闭标志 closeMsg(el, onClose, _msgWrapper); } } }, getMsgRight(closable))])]); var anm = el.querySelector(".notify-msg__circle"); if (anm) { css(anm, { animation: "notify-msg_" + type + " " + duration + "ms linear" }); if ("onanimationend" in window) { addAnimationEnd(anm, function () { closeMsg(el, onClose, _msgWrapper); }); } else { setTimeout(function () { closeMsg(el, onClose, _msgWrapper); }, duration); } } if (type != "loading") { setTimeout(function () { closeMsg(el, onClose, _msgWrapper); }, duration); } if (!_msgWrapper.children.length) { document.body.appendChild(_msgWrapper); } _msgWrapper.appendChild(el); css(el, { height: el.offsetHeight + "px" }); setTimeout(function () { removeClass(el.children[0], "notify-msg-fade-in"); }, 300); if (type == "loading") { return function () { closeMsg(el, onClose, _msgWrapper); }; } } function getMsgRight(showClose) { if (showClose) { //if (1 == 1) {//永远显示关闭按钮 return "\n \n "; } else { return "\n "; } } var flag = true; //正常关闭标志 var closeFlag = false;//点击关闭按钮标志 function closeMsg(el, cb, _msgWrapper) { if (!el) return; css(el, { padding: 0, height: 0 }); addClass(el.children[0], "notify-msg-fade-out"); if (closeFlag) { //点击关闭按钮 closeFlag = false; cb && cb(); //回调方法 } else { if (flag) {//正常关闭,全局变量 cb && cb(); } else { flag = true // return; } } setTimeout(function () { if (!el) return; var has = false; if (_msgWrapper) { for (var i = 0; i < _msgWrapper.children.length; i++) { if (_msgWrapper.children[i] && _msgWrapper.children[i] === el) { has = true; } } has && removeChild(el); el = null; if (!_msgWrapper.children.length) { has && removeChild(_msgWrapper); } } }, 300); } function getIconObj() { return { info: "\n \n ", success: "\n \n ", warning: "\n \n ", error: "\n \n ", loading: "\n