coco-message.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. "use strict";
  2. 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); }
  3. !function (global, factory) {
  4. (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = global || self, global.cocoMessage = factory());
  5. }(void 0, function () {
  6. "use strict";
  7. var msgWrapper = c({
  8. className: "coco-msg-stage"
  9. });
  10. function c(args, children) {
  11. var el = document.createElement("div");
  12. for (var key in args) {
  13. var element = args[key];
  14. if (key == "className") {
  15. key = "class";
  16. el.setAttribute(key, element);
  17. } else if (key[0] == "_") {
  18. el.addEventListener(key.slice(1), element);
  19. }
  20. }
  21. if (typeof children == "string") {
  22. el.innerHTML = children;
  23. } else if (_typeof(children) == "object" && children.tagName) {
  24. el.appendChild(children);
  25. } else if (children) {
  26. for (var i = 0; i < children.length; i++) {
  27. var child = children[i];
  28. el.appendChild(child);
  29. }
  30. }
  31. return el;
  32. }
  33. function addAnimationEnd(el, fn) {
  34. ["a", "webkitA"].forEach(function (prefix) {
  35. var name = prefix + "nimationEnd";
  36. el.addEventListener(name, function () {
  37. fn();
  38. });
  39. });
  40. }
  41. function css(el, css) {
  42. for (var key in css) {
  43. el.style[key] = css[key];
  44. }
  45. if (el.getAttribute("style") === "") {
  46. el.removeAttribute("style");
  47. }
  48. }
  49. function addClass(el, s) {
  50. var c = el.className || "";
  51. if (!hasClass(c, s)) {
  52. var arr = c.split(/\s+/);
  53. arr.push(s);
  54. el.className = arr.join(" ");
  55. }
  56. }
  57. function hasClass(c, s) {
  58. return c.indexOf(s) > -1 ? !0 : !1;
  59. }
  60. function removeClass(el, s) {
  61. var c = el.className || "";
  62. if (hasClass(c, s)) {
  63. var arr = c.split(/\s+/);
  64. var i = arr.indexOf(s);
  65. arr.splice(i, 1);
  66. el.className = arr.join(" ");
  67. }
  68. if (el.className === "") {
  69. el.removeAttribute("class");
  70. }
  71. }
  72. var initArgs = {
  73. msg: "",
  74. duration: 2000,
  75. showClose: false
  76. };
  77. var cocoMessage = {
  78. info: function info() {
  79. initConfig(arguments, "info");
  80. },
  81. success: function success() {
  82. initConfig(arguments, "success");
  83. },
  84. warning: function warning() {
  85. initConfig(arguments, "warning");
  86. },
  87. error: function error() {
  88. initConfig(arguments, "error");
  89. },
  90. loading: function loading() {
  91. return initConfig(arguments, "loading");
  92. },
  93. destroyAll: function destroyAll() {
  94. _destroyAll();
  95. },
  96. config: function config(obj) {
  97. for (var key in obj) {
  98. if (Object.hasOwnProperty.call(obj, key)) {
  99. if (obj[key] !== undefined) {
  100. initArgs[key] = obj[key];
  101. }
  102. }
  103. }
  104. }
  105. };
  106. function initConfig(obj, type) {
  107. var args = {};
  108. for (var key in initArgs) {
  109. args[key] = initArgs[key];
  110. }
  111. for (var i = 0; i < obj.length; i++) {
  112. var it = obj[i];
  113. if (it !== undefined) {
  114. if (typeof it == "string" || _typeof(it) == "object") {
  115. args.msg = it;
  116. } else if (typeof it == "boolean") {
  117. args.showClose = it;
  118. } else if (typeof it == "function") {
  119. args.onClose = it;
  120. } else if (typeof it == "number") {
  121. args.duration = it;
  122. }
  123. }
  124. }
  125. args.type = type;
  126. return createMsgEl(args);
  127. }
  128. function createMsgEl(args) {
  129. var type = args.type,
  130. duration = args.duration,
  131. msg = args.msg,
  132. showClose = args.showClose,
  133. onClose = args.onClose;
  134. var closable = duration === 0;
  135. var iconObj = getIconObj();
  136. if (type == "loading") {
  137. msg = msg === "" ? "正在加载,请稍后" : msg;
  138. closable = showClose;
  139. duration = 0;
  140. }
  141. var el = c({
  142. className: "coco-msg-wrapper"
  143. }, [c({
  144. className: "coco-msg coco-msg-fade-in " + type
  145. }, [c({
  146. className: "coco-msg-icon"
  147. }, iconObj[type]), c({
  148. className: "coco-msg-content"
  149. }, msg), c({
  150. className: "coco-msg-wait " + (closable ? "coco-msg-pointer" : ""),
  151. _click: function _click() {
  152. if (closable) {
  153. closeMsg(el, onClose);
  154. }
  155. }
  156. }, getMsgRight(closable))])]);
  157. var anm = el.querySelector(".coco-msg__circle");
  158. if (anm) {
  159. css(anm, {
  160. animation: "coco-msg_" + type + " " + duration + "ms linear"
  161. });
  162. if ("onanimationend" in window) {
  163. addAnimationEnd(anm, function () {
  164. closeMsg(el, onClose);
  165. });
  166. } else {
  167. setTimeout(function () {
  168. closeMsg(el, onClose);
  169. }, duration);
  170. }
  171. }
  172. if (type == "loading" && duration !== 0) {
  173. setTimeout(function () {
  174. closeMsg(el, onClose);
  175. }, duration);
  176. }
  177. if (!msgWrapper.children.length) {
  178. document.body.appendChild(msgWrapper);
  179. }
  180. msgWrapper.appendChild(el);
  181. css(el, {
  182. height: el.offsetHeight + "px"
  183. });
  184. setTimeout(function () {
  185. removeClass(el.children[0], "coco-msg-fade-in");
  186. }, 300);
  187. if (type == "loading") {
  188. return function () {
  189. closeMsg(el, onClose);
  190. };
  191. }
  192. }
  193. function getMsgRight(showClose) {
  194. if (showClose) {
  195. return "\n <svg class=\"coco-msg-close\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"5514\"><path d=\"M810 274l-238 238 238 238-60 60-238-238-238 238-60-60 238-238-238-238 60-60 238 238 238-238z\" p-id=\"5515\"></path></svg>\n ";
  196. } else {
  197. return "<svg class=\"coco-msg-progress\" viewBox=\"0 0 33.83098862 33.83098862\" xmlns=\"http://www.w3.org/2000/svg\">\n <circle class=\"coco-msg__background\" cx=\"16.9\" cy=\"16.9\" r=\"15.9\"></circle>\n <circle class=\"coco-msg__circle\" stroke-dasharray=\"100,100\" cx=\"16.9\" cy=\"16.9\" r=\"15.9\"></circle>\n </svg>\n ";
  198. }
  199. }
  200. function closeMsg(el, cb) {
  201. if (!el) return;
  202. css(el, {
  203. padding: 0,
  204. height: 0
  205. });
  206. addClass(el.children[0], "coco-msg-fade-out");
  207. cb && cb();
  208. setTimeout(function () {
  209. if (!el) return;
  210. var has = false;
  211. for (var i = 0; i < msgWrapper.children.length; i++) {
  212. if (msgWrapper.children[i] === el) {
  213. has = true;
  214. }
  215. }
  216. has && removeChild(el);
  217. el = null;
  218. if (!msgWrapper.children.length) {
  219. has && removeChild(msgWrapper);
  220. }
  221. }, 300);
  222. }
  223. function getIconObj() {
  224. return {
  225. info: "\n <svg t=\"1609810636603\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"3250\"><path d=\"M469.333333 341.333333h85.333334v469.333334H469.333333z\" fill=\"#ffffff\" p-id=\"3251\"></path><path d=\"M469.333333 213.333333h85.333334v85.333334H469.333333z\" fill=\"#ffffff\" p-id=\"3252\"></path><path d=\"M384 341.333333h170.666667v85.333334H384z\" fill=\"#ffffff\" p-id=\"3253\"></path><path d=\"M384 725.333333h256v85.333334H384z\" fill=\"#ffffff\" p-id=\"3254\"></path></svg>\n ",
  226. success: "\n <svg t=\"1609781242911\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1807\"><path d=\"M455.42 731.04c-8.85 0-17.75-3.05-24.99-9.27L235.14 553.91c-16.06-13.81-17.89-38.03-4.09-54.09 13.81-16.06 38.03-17.89 54.09-4.09l195.29 167.86c16.06 13.81 17.89 38.03 4.09 54.09-7.58 8.83-18.31 13.36-29.1 13.36z\" p-id=\"1808\" fill=\"#ffffff\"></path><path d=\"M469.89 731.04c-8.51 0-17.07-2.82-24.18-8.6-16.43-13.37-18.92-37.53-5.55-53.96L734.1 307.11c13.37-16.44 37.53-18.92 53.96-5.55 16.43 13.37 18.92 37.53 5.55 53.96L499.67 716.89c-7.58 9.31-18.64 14.15-29.78 14.15z\" p-id=\"1809\" fill=\"#ffffff\"></path></svg>\n ",
  227. warning: "\n <svg t=\"1609776406944\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"18912\"><path d=\"M468.114286 621.714286c7.314286 21.942857 21.942857 36.571429 43.885714 36.571428s36.571429-14.628571 43.885714-36.571428L585.142857 219.428571c0-43.885714-36.571429-73.142857-73.142857-73.142857-43.885714 0-73.142857 36.571429-73.142857 80.457143l29.257143 394.971429zM512 731.428571c-43.885714 0-73.142857 29.257143-73.142857 73.142858s29.257143 73.142857 73.142857 73.142857 73.142857-29.257143 73.142857-73.142857-29.257143-73.142857-73.142857-73.142858z\" p-id=\"18913\" fill=\"#ffffff\"></path></svg>\n ",
  228. error: "\n <svg t=\"1609810716933\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"5514\"><path d=\"M810 274l-238 238 238 238-60 60-238-238-238 238-60-60 238-238-238-238 60-60 238 238 238-238z\" p-id=\"5515\" fill=\"#ffffff\"></path></svg>\n ",
  229. loading: "\n <div class=\"coco-msg_loading\">\n <svg class=\"coco-msg-circular\" viewBox=\"25 25 50 50\">\n <circle class=\"coco-msg-path\" cx=\"50\" cy=\"50\" r=\"20\" fill=\"none\" stroke-width=\"4\" stroke-miterlimit=\"10\"/>\n </svg>\n </div>\n "
  230. };
  231. }
  232. function removeChild(el) {
  233. el && el.parentNode.removeChild(el);
  234. }
  235. function _destroyAll() {
  236. for (var i = 0; i < msgWrapper.children.length; i++) {
  237. var element = msgWrapper.children[i];
  238. closeMsg(element);
  239. }
  240. }
  241. window.addEventListener('DOMContentLoaded', function () {
  242. insertCssInHead();
  243. });
  244. function insertCssInHead() {
  245. var doc = document;
  246. if (doc && doc.head) {
  247. var head = doc.head;
  248. var _css = doc.createElement('style');
  249. var cssStr = "\n\n[class|=coco],[class|=coco]::after,[class|=coco]::before{box-sizing:border-box;outline:0}.coco-msg-progress{width:13px;height:13px}.coco-msg__circle{stroke-width:2;stroke-linecap:square;fill:none;transform:rotate(-90deg);transform-origin:center}.coco-msg-stage:hover .coco-msg__circle{-webkit-animation-play-state:paused!important;animation-play-state:paused!important}.coco-msg__background{stroke-width:2;fill:none}.coco-msg-stage{position:fixed;top:20px;left:50%;width:auto;transform:translate(-50%,0);z-index:65535}.coco-msg-wrapper{position:relative;left:50%;transform:translate(-50%,0);transform:translate3d(-50%,0,0);transition:height .3s ease,padding .3s ease;padding:6px 0;will-change:transform,opacity}.coco-msg{padding:15px 21px;border-radius:3px;position:relative;left:50%;transform:translate(-50%,0);transform:translate3d(-50%,0,0);display:flex;align-items:center}.coco-msg-content,.coco-msg-icon,.coco-msg-wait{display:inline-block}.coco-msg-icon{position:relative;width:13px;height:13px;border-radius:100%;display:flex;justify-content:center;align-items:center}.coco-msg-icon svg{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:11px;height:11px}.coco-msg-wait{width:20px;height:20px;position:relative;fill:#4eb127}.coco-msg-wait svg{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%)}.coco-msg-close{width:14px;height:14px}.coco-msg-content{margin:0 10px;min-width:240px;text-align:left;font-size:14px;font-weight:500;font-family:-apple-system,Microsoft Yahei,sans-serif;text-shadow:0 0 1px rgba(0,0,0,.01)}.coco-msg.info{color:#0fafad;background-color:#e7fdfc;box-shadow:0 0 2px 0 rgba(0,1,1,.01),0 0 0 1px #c0faf9}.coco-msg.info .coco-msg-icon{background-color:#0fafad}.coco-msg.success{color:#4ebb23;background-color:#f3ffe8;box-shadow:0 0 2px 0 rgba(0,1,0,.01),0 0 0 1px #d9f8bb}.coco-msg.success .coco-msg-icon{background-color:#4ebb23}.coco-msg.warning{color:#f1b306;background-color:#fff9eb;box-shadow:0 0 2px 0 rgba(1,1,0,.01),0 0 0 1px #fcf2cd}.coco-msg.warning .coco-msg-icon{background-color:#f1b306}.coco-msg.error{color:#f34b51;background-color:#fff7f7;box-shadow:0 0 2px 0 rgba(1,0,0,.01),0 0 0 1px #ffe3e3}.coco-msg.error .coco-msg-icon{background-color:#f34b51}.coco-msg.loading{color:#0fafad;background-color:#e7fdfc;box-shadow:0 0 2px 0 rgba(0,1,1,.01),0 0 0 1px #c2faf9}.coco-msg_loading{flex-shrink:0;width:20px;height:20px;position:relative}.coco-msg-circular{-webkit-animation:coco-msg-rotate 2s linear infinite both;animation:coco-msg-rotate 2s linear infinite both;transform-origin:center center;height:18px!important;width:18px!important}.coco-msg-path{stroke-dasharray:1,200;stroke-dashoffset:0;stroke:#0fafad;-webkit-animation:coco-msg-dash 1.5s ease-in-out infinite;animation:coco-msg-dash 1.5s ease-in-out infinite;stroke-linecap:round}@-webkit-keyframes coco-msg-rotate{100%{transform:translate(-50%,-50%) rotate(360deg)}}@keyframes coco-msg-rotate{100%{transform:translate(-50%,-50%) rotate(360deg)}}@-webkit-keyframes coco-msg-dash{0%{stroke-dasharray:1,200;stroke-dashoffset:0}50%{stroke-dasharray:89,200;stroke-dashoffset:-35px}100%{stroke-dasharray:89,200;stroke-dashoffset:-124px}}@keyframes coco-msg-dash{0%{stroke-dasharray:1,200;stroke-dashoffset:0}50%{stroke-dasharray:89,200;stroke-dashoffset:-35px}100%{stroke-dasharray:89,200;stroke-dashoffset:-124px}}.coco-msg.info .coco-msg-wait{fill:#0fafad}.coco-msg.success .coco-msg-wait{fill:#4ebb23}.coco-msg.warning .coco-msg-wait{fill:#f1b306}.coco-msg.error .coco-msg-wait{fill:#f34b51}.coco-msg.loading .coco-msg-wait{fill:#0fafad}.coco-msg-pointer{cursor:pointer}@-webkit-keyframes coco-msg_info{0%{stroke:#0fafad}to{stroke:#0fafad;stroke-dasharray:0 100}}@keyframes coco-msg_info{0%{stroke:#0fafad}to{stroke:#0fafad;stroke-dasharray:0 100}}@-webkit-keyframes coco-msg_success{0%{stroke:#4eb127}to{stroke:#4eb127;stroke-dasharray:0 100}}@keyframes coco-msg_success{0%{stroke:#4eb127}to{stroke:#4eb127;stroke-dasharray:0 100}}@-webkit-keyframes coco-msg_warning{0%{stroke:#fcbc0b}to{stroke:#fcbc0b;stroke-dasharray:0 100}}@keyframes coco-msg_warning{0%{stroke:#fcbc0b}to{stroke:#fcbc0b;stroke-dasharray:0 100}}@-webkit-keyframes coco-msg_error{0%{stroke:#eb262d}to{stroke:#eb262d;stroke-dasharray:0 100}}@keyframes coco-msg_error{0%{stroke:#eb262d}to{stroke:#eb262d;stroke-dasharray:0 100}}.coco-msg-fade-in{-webkit-animation:coco-msg-fade .2s ease-out both;animation:coco-msg-fade .2s ease-out both}.coco-msg-fade-out{animation:coco-msg-fade .3s linear reverse both}@-webkit-keyframes coco-msg-fade{0%{opacity:0;transform:translate(-50%,0);transform:translate3d(-50%,-80%,0)}to{opacity:1;transform:translate(-50%,0);transform:translate3d(-50%,0,0)}}@keyframes coco-msg-fade{0%{opacity:0;transform:translate(-50%,0);transform:translate3d(-50%,-80%,0)}to{opacity:1;transform:translate(-50%,0);transform:translate3d(-50%,0,0)}}\n ";
  250. _css.innerHTML = cssStr;
  251. if (head.children.length) {
  252. head.insertBefore(_css, head.children[0]);
  253. } else {
  254. head.appendChild(_css);
  255. }
  256. }
  257. }
  258. return cocoMessage;
  259. });