toast.js

coco-message TheWindRises-2 js toast

当前为 2021-09-18 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/426194/971659/toastjs.js

  1. // @original-script https://github.com/TheWindRises-2/coco-message
  2. "use strict";
  3.  
  4. 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); }
  5.  
  6. !function (global, factory) {
  7. (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());
  8. }(void 0, function () {
  9. "use strict";
  10.  
  11. if (typeof window === 'undefined') {
  12. return;
  13. }
  14.  
  15. var msgWrapper = c({
  16. className: "coco-msg-stage"
  17. });
  18.  
  19. function c(args, children) {
  20. var el = document.createElement("div");
  21.  
  22. for (var key in args) {
  23. var element = args[key];
  24.  
  25. if (key == "className") {
  26. key = "class";
  27. el.setAttribute(key, element);
  28. } else if (key[0] == "_") {
  29. el.addEventListener(key.slice(1), element);
  30. }
  31. }
  32.  
  33. if (typeof children == "string") {
  34. el.innerHTML = children;
  35. } else if (_typeof(children) == "object" && children.tagName) {
  36. el.appendChild(children);
  37. } else if (children) {
  38. for (var i = 0; i < children.length; i++) {
  39. var child = children[i];
  40. el.appendChild(child);
  41. }
  42. }
  43.  
  44. return el;
  45. }
  46.  
  47. function addAnimationEnd(el, fn) {
  48. ["a", "webkitA"].forEach(function (prefix) {
  49. var name = prefix + "nimationEnd";
  50. el.addEventListener(name, function () {
  51. fn();
  52. });
  53. });
  54. }
  55.  
  56. function css(el, css) {
  57. for (var key in css) {
  58. el.style[key] = css[key];
  59. }
  60.  
  61. if (el.getAttribute("style") === "") {
  62. el.removeAttribute("style");
  63. }
  64. }
  65.  
  66. function addClass(el, s) {
  67. var c = el.className || "";
  68.  
  69. if (!hasClass(c, s)) {
  70. var arr = c.split(/\s+/);
  71. arr.push(s);
  72. el.className = arr.join(" ");
  73. }
  74. }
  75.  
  76. function hasClass(c, s) {
  77. return c.indexOf(s) > -1 ? !0 : !1;
  78. }
  79.  
  80. function removeClass(el, s) {
  81. var c = el.className || "";
  82.  
  83. if (hasClass(c, s)) {
  84. var arr = c.split(/\s+/);
  85. var i = arr.indexOf(s);
  86. arr.splice(i, 1);
  87. el.className = arr.join(" ");
  88. }
  89.  
  90. if (el.className === "") {
  91. el.removeAttribute("class");
  92. }
  93. }
  94.  
  95. var initArgs = {
  96. msg: "",
  97. duration: 2000,
  98. showClose: false
  99. };
  100. var cocoMessage = {
  101. info: function info() {
  102. initConfig(arguments, "info");
  103. },
  104. success: function success() {
  105. initConfig(arguments, "success");
  106. },
  107. warning: function warning() {
  108. initConfig(arguments, "warning");
  109. },
  110. error: function error() {
  111. initConfig(arguments, "error");
  112. },
  113. loading: function loading() {
  114. return initConfig(arguments, "loading");
  115. },
  116. destroyAll: function destroyAll() {
  117. _destroyAll();
  118. },
  119. config: function config(obj) {
  120. for (var key in obj) {
  121. if (Object.hasOwnProperty.call(obj, key)) {
  122. if (obj[key] !== undefined) {
  123. initArgs[key] = obj[key];
  124. }
  125. }
  126. }
  127. }
  128. };
  129.  
  130. function initConfig(obj, type) {
  131. var args = {};
  132.  
  133. for (var key in initArgs) {
  134. args[key] = initArgs[key];
  135. }
  136.  
  137. for (var i = 0; i < obj.length; i++) {
  138. var it = obj[i];
  139.  
  140. if (it !== undefined) {
  141. if (typeof it == "string" || _typeof(it) == "object") {
  142. args.msg = it;
  143. } else if (typeof it == "boolean") {
  144. args.showClose = it;
  145. } else if (typeof it == "function") {
  146. args.onClose = it;
  147. } else if (typeof it == "number") {
  148. args.duration = it;
  149. }
  150. }
  151. }
  152.  
  153. args.type = type;
  154. return createMsgEl(args);
  155. }
  156.  
  157. function createMsgEl(args) {
  158. var type = args.type,
  159. duration = args.duration,
  160. msg = args.msg,
  161. showClose = args.showClose,
  162. onClose = args.onClose;
  163. var closable = duration === 0;
  164. var iconObj = getIconObj();
  165.  
  166. if (type == "loading") {
  167. msg = msg === "" ? "正在加载,请稍后" : msg;
  168. closable = showClose;
  169. duration = 0;
  170. }
  171.  
  172. var el = c({
  173. className: "coco-msg-wrapper"
  174. }, [c({
  175. className: "coco-msg coco-msg-fade-in ".concat(type)
  176. }, [c({
  177. className: "coco-msg-icon"
  178. }, iconObj[type]), c({
  179. className: "coco-msg-content"
  180. }, msg), c({
  181. className: "coco-msg-wait ".concat(closable ? "coco-msg-pointer" : ""),
  182. _click: function _click() {
  183. if (closable) {
  184. closeMsg(el, onClose);
  185. }
  186. }
  187. }, getMsgRight(closable))])]);
  188. var anm = el.querySelector(".coco-msg__circle");
  189.  
  190. if (anm && type !== "loading") {
  191. css(anm, {
  192. animation: "coco-msg__circle ".concat(duration, "ms linear")
  193. });
  194.  
  195. if ("onanimationend" in window) {
  196. addAnimationEnd(anm, function () {
  197. closeMsg(el, onClose);
  198. });
  199. } else {
  200. setTimeout(function () {
  201. closeMsg(el, onClose);
  202. }, duration);
  203. }
  204. }
  205.  
  206. if (type == "loading" && duration !== 0) {
  207. setTimeout(function () {
  208. closeMsg(el, onClose);
  209. }, duration);
  210. }
  211.  
  212. if (!msgWrapper.children.length) {
  213. document.body.appendChild(msgWrapper);
  214. }
  215.  
  216. msgWrapper.appendChild(el);
  217. css(el, {
  218. height: el.offsetHeight + "px"
  219. });
  220. setTimeout(function () {
  221. removeClass(el.children[0], "coco-msg-fade-in");
  222. }, 300);
  223.  
  224. if (type == "loading") {
  225. return function () {
  226. closeMsg(el, onClose);
  227. };
  228. }
  229. }
  230.  
  231. function getMsgRight(showClose) {
  232. if (showClose) {
  233. 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 ";
  234. } else {
  235. 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 ";
  236. }
  237. }
  238.  
  239. function closeMsg(el, cb) {
  240. if (!el) return;
  241. css(el, {
  242. padding: 0,
  243. height: 0
  244. });
  245. addClass(el.children[0], "coco-msg-fade-out");
  246. cb && cb();
  247. setTimeout(function () {
  248. if (!el) return;
  249. var has = false;
  250.  
  251. for (var i = 0; i < msgWrapper.children.length; i++) {
  252. if (msgWrapper.children[i] === el) {
  253. has = true;
  254. }
  255. }
  256.  
  257. has && removeChild(el);
  258. el = null;
  259.  
  260. if (!msgWrapper.children.length) {
  261. has && removeChild(msgWrapper);
  262. }
  263. }, 300);
  264. }
  265.  
  266. function getIconObj() {
  267. return {
  268. 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=\"#2980b9\" p-id=\"3251\"></path><path d=\"M469.333333 213.333333h85.333334v85.333334H469.333333z\" fill=\"#2980b9\" p-id=\"3252\"></path><path d=\"M384 341.333333h170.666667v85.333334H384z\" fill=\"#2980b9\" p-id=\"3253\"></path><path d=\"M384 725.333333h256v85.333334H384z\" fill=\"#2980b9\" p-id=\"3254\"></path></svg>\n ",
  269. 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 ",
  270. 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 ",
  271. 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 ",
  272. 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 "
  273. };
  274. }
  275.  
  276. function removeChild(el) {
  277. el && el.parentNode.removeChild(el);
  278. }
  279.  
  280. function _destroyAll() {
  281. for (var i = 0; i < msgWrapper.children.length; i++) {
  282. var element = msgWrapper.children[i];
  283. closeMsg(element);
  284. }
  285. }
  286.  
  287. window.addEventListener("DOMContentLoaded", function () {
  288. insertCssInHead();
  289. });
  290.  
  291. function insertCssInHead() {
  292. var doc = document;
  293.  
  294. if (doc && doc.head) {
  295. var head = doc.head;
  296.  
  297. var _css = doc.createElement("style");
  298.  
  299. var cssStr = "\n[class|=coco],[class|=coco]::after,[class|=coco]::before{box-sizing:border-box;outline:0}.coco-msg-progress{width:14px;height:14px}.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:3000}.coco-msg-wrapper{position:relative;left:50%;transform:translate(-50%,0);transition:height .25s ease-out,padding .25s ease-out;transition:height .35s ease-out,padding .35s ease-out;padding:8px 0;will-change:transform,opacity}.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;opacity:.8}.coco-msg-icon svg{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:11px;height:11px;box-sizing:content-box}.coco-msg-wait{width:20px;height:20px;position:relative}.coco-msg-wait svg{position:absolute;top:50%;right:-4px;transform:translate(0,-50%);fill:#b3b9b9}.coco-msg-close{width:16px;height:16px}.coco-msg-content{margin:0 10px;min-width:220px;text-align:left;font-size:14px;font-weight:400}.coco-msg.error .coco-msg-icon,.coco-msg.info .coco-msg-icon,.coco-msg.success .coco-msg-icon,.coco-msg.warning .coco-msg-icon{background-color:currentColor}.coco-msg{padding:13px 25px;border-radius:2px;position:relative;left:50%;transform:translate(-50%,0);display:flex;align-items:center}.coco-msg.info,.coco-msg.loading{color:#fff;background-color:#2980b9;box-shadow:0 0 1px 0 rgba(239,238,240,.3)}.coco-msg.success{color:#68c43b;background-color:#f0faeb;box-shadow:0 0 1px 0 rgba(145,194,126,.3)}.coco-msg.warning{color:#be820a;background-color:#faf4e1;box-shadow:0 0 1px 0 rgba(212,198,149,.3)}.coco-msg.error{color:#f74e60;background-color:#fee2e5;box-shadow:0 0 1px 0 rgba(218,163,163,.3)}.coco-msg.loading .coco-msg-icon{background-color:transparent}@keyframes coco-msg__circle{0%{stroke:#b3b9b9;stroke:currentColor}to{stroke:#b3b9b9;stroke:currentColor;stroke-dasharray:0 100}}.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:currentColor;-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-pointer{cursor:pointer}.coco-msg-fade-in{-webkit-animation:coco-msg-fade .22s ease-out both;animation:coco-msg-fade .22s ease-out both}.coco-msg-fade-out{animation:coco-msg-fade .22s linear reverse both}@-webkit-keyframes coco-msg-fade{0%{opacity:0;transform:translate(-50%,0)}to{opacity:1;transform:translate(-50%,0)}}@keyframes coco-msg-fade{0%{opacity:0;transform:translate(-50%,-80%)}to{opacity:1;transform:translate(-50%,0)}}\n ";
  300. _css.innerHTML = cssStr;
  301.  
  302. if (head.children.length) {
  303. head.insertBefore(_css, head.children[0]);
  304. } else {
  305. head.appendChild(_css);
  306. }
  307. }
  308. }
  309.  
  310. return cocoMessage;
  311. });