Files
EdgeAdmin/web/public/js/utils.min.js
2023-12-03 11:30:02 +08:00

2 lines
14 KiB
JavaScript

window.teaweb={set:function(t,e){localStorage.setItem(t,JSON.stringify(e))},get:function(t){t=localStorage.getItem(t);return null==t||0==t.length?null:JSON.parse(t)},getString:function(t){t=this.get(t);return"string"==typeof t?t:""},getBool:function(t){return Boolean(this.get(t))},remove:function(t){localStorage.removeItem(t)},match:function(t,e){if(null==t)return!1;if(null==e)return!0;if(t=t.trim(),0==(e=e.trim()).length)return!0;if(0==t.length)return!1;for(var n=e.split(/\s+/),o=0;o<n.length;o++){var i=(i=n[o]).replace(/(\+|\*|\?|[|]|{|}|\||\\|\(|\)|\.)/g,"\\$1");if(!new RegExp(i,"i").test(t))return!1}return!0},clone:function(t){t=JSON.stringify(t);return JSON.parse(t)},loadJS:function(t,e){let n=document.createElement("script");n.setAttribute("type","text/javascript"),n.setAttribute("src",t),"function"==typeof e&&n.addEventListener("load",e),document.head.append(n)},loadCSS:function(t,e){let n=document.createElement("link");n.setAttribute("rel","stylesheet"),n.setAttribute("type","text/css"),n.setAttribute("href",t),"function"==typeof e&&n.addEventListener("load",e),document.head.append(n)},datepicker:function(e,n,o){if("undefined"==typeof Pikaday){let t=this;return this.loadJS("/js/moment.min.js",function(){t.loadJS("/js/pikaday.js",function(){t.datepicker(e,n,o)})}),this.loadCSS("/js/pikaday.css"),this.loadCSS("/js/pikaday.theme.css"),void this.loadCSS("/js/pikaday.triangle.css")}"string"==typeof e&&(e=document.getElementById(e));var t=(new Date).getFullYear();let i=new Pikaday({field:e,firstDay:1,minDate:new Date(t-1,0,1),maxDate:new Date(t+20,11,31),yearRange:[t-1,t+20],format:"YYYY-MM-DD",i18n:{previousMonth:"上月",nextMonth:"下月",months:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],weekdays:["周日","周一","周二","周三","周四","周五","周六"],weekdaysShort:["周日","周一","周二","周三","周四","周五","周六"]},theme:"triangle-theme",onSelect:function(){"function"==typeof n&&n.call(Tea.Vue,i.toString())},reposition:!o});e.picker=i},formatBytes:function(t){return(t=Math.ceil(t))<Math.pow(1024,1)?t+"B":t<Math.pow(1024,2)?Math.round(100*t/Math.pow(1024,1))/100+"KiB":t<Math.pow(1024,3)?Math.round(100*t/Math.pow(1024,2))/100+"MiB":t<Math.pow(1024,4)?Math.round(100*t/Math.pow(1024,3))/100+"GiB":t<Math.pow(1024,5)?Math.round(100*t/Math.pow(1024,4))/100+"TiB":t<Math.pow(1024,6)?Math.round(100*t/Math.pow(1024,5))/100+"PiB":Math.round(100*t/Math.pow(1024,6))/100+"EiB"},formatBits:function(t,e){t=Math.ceil(t);let n=1e4;switch(e){case 1:n=10;break;case 2:n=100;break;case 3:n=1e3;break;case 4:n=1e4}return t<Math.pow(1024,1)?t+"bps":t<Math.pow(1024,2)?Math.round(t*n/Math.pow(1024,1))/n+"Kbps":t<Math.pow(1024,3)?Math.round(t*n/Math.pow(1024,2))/n+"Mbps":t<Math.pow(1024,4)?Math.round(t*n/Math.pow(1024,3))/n+"Gbps":t<Math.pow(1024,5)?Math.round(t*n/Math.pow(1024,4))/n+"Tbps":t<Math.pow(1024,6)?Math.round(t*n/Math.pow(1024,5))/n+"Pbps":Math.round(t*n/Math.pow(1024,6))/n+"Ebps"},formatNumber:function(t){if(null==t)return"null";let e=t.toString();t=e.indexOf(".");if(0<=t)return this.formatNumber(e.substring(0,t))+"."+e.substring(t+1);if(e.length<=3)return e;let n=[];for(let t=0;t<Math.floor(e.length/3);t++){var o=e.length-3*(t+1);n.push(e.substring(o,3+o))}return e.length%3!=0&&n.push(e.substring(0,e.length%3)),n.reverse().join(", ")},formatCount:function(t){let e="",n="";return 1e9<=t?(e="B",n=1e9):1e6<=t?(e="M",n=1e6):1e3<=t&&(e="K",n=1e3),0==e.length?t.toString():Math.round(100*t/n)/100+e},bytesAxis:function(t,e){t=Math.max.apply(this,t.map(e));let n=1,o="B";return t>=Math.pow(1024,6)?(o="E",n=Math.pow(1024,6)):t>=Math.pow(1024,5)?(o="P",n=Math.pow(1024,5)):t>=Math.pow(1024,4)?(o="T",n=Math.pow(1024,4)):t>=Math.pow(1024,3)?(o="G",n=Math.pow(1024,3)):t>=Math.pow(1024,2)?(o="M",n=Math.pow(1024,2)):t>=Math.pow(1024,1)&&(o="K",n=Math.pow(1024,1)),{unit:o,divider:n}},bitsAxis:function(t,e){t=this.bytesAxis(t,e);let n=t.unit;return"B"==n?n="bps":n+="bps",{unit:n,divider:t.divider}},countAxis:function(t,e){t=Math.max.apply(this,t.map(e));let n=1,o="";return 1e9<=t?(o="B",n=1e9):1e6<=t?(o="M",n=1e6):1e3<=t&&(o="K",n=1e3),{unit:o,divider:n,max:t}},splitFormat:function(t){t=t.match(/^([0-9.]+)([a-zA-Z]+)$/);return[parseFloat(t[1]),t[2]]},popup:function(t,e){null!=t&&0<t.length&&"."==t.substring(0,1)&&(t=Tea.url(t)),null==e&&(e={});var n="40em",o="20em";window.POPUP_CALLBACK=function(){Swal.close()},null!=e.width&&(n=e.width),null!=e.height&&(o=e.height),"function"==typeof e.callback&&(window.POPUP_CALLBACK=function(){Swal.close(),e.callback.apply(Tea.Vue,arguments)}),Swal.fire({html:'<iframe src="'+t+"#popup-"+n+'" style="border:0; width: 100%; height:'+o+'"></iframe>',width:n,padding:"0.5em",showConfirmButton:!1,showCloseButton:!0,focusConfirm:!1,onClose:function(t){"function"==typeof e.onClose&&e.onClose.apply(Tea.Vue,arguments)}})},popupSuccess:function(t,e,n){let o={};null!=e&&(o.width=e),null!=n&&(o.height=n),o.callback=function(){teaweb.success("保存成功",function(){teaweb.reload()})},this.popup(t,o)},popupFinish:function(){null!=window.POPUP_CALLBACK&&window.POPUP_CALLBACK.apply(window,arguments)},popupTip:function(t){Swal.fire({html:'<div style="line-height: 1.7;text-align: left "><i class="icon question circle"></i>'+t+"</div>",width:"34em",padding:"4em",showConfirmButton:!1,showCloseButton:!0,focusConfirm:!1})},isPopup:function(){var t=window.location.hash;return null!=t&&t.startsWith("#popup")},closePopup:function(){this.isPopup()&&window.parent.Swal.close()},Swal:function(){return(this.isPopup()?window.parent:window).Swal},hasPopup:function(){return 0<document.getElementsByClassName("swal2-container").length},success:function(t,e){var n="20em";let o={confirmButtonText:"确定",buttonsStyling:!1,icon:"success",customClass:{closeButton:"ui button",cancelButton:"ui button",confirmButton:"ui button primary"},width:n=30<t.length?"30em":n,onAfterClose:function(){"function"==typeof e?setTimeout(function(){e()}):"string"==typeof e&&(window.location=e)}};t.startsWith("html:")?o.html=t.substring(5):o.text=t,Swal.fire(o)},toast:function(t,e,n){null==e&&(e=2e3);var o="20em";30<t.length&&(o="30em"),Swal.fire({text:t,icon:"info",width:o,timer:e,showConfirmButton:!1,onAfterClose:function(){"function"==typeof n&&n()}})},successToast:function(t,e,n){null==e&&(e=2e3);var o="20em";30<t.length&&(o="30em"),Swal.fire({text:t,icon:"success",width:o,timer:e,showConfirmButton:!1,onAfterClose:function(){"function"==typeof n&&n()}})},successRefresh:function(t){teaweb.success(t,function(){teaweb.reload()})},warn:function(t,e){var n="20em";30<t.length&&(n="30em"),Swal.fire({text:t,confirmButtonText:"确定",buttonsStyling:!1,customClass:{closeButton:"ui button",cancelButton:"ui button",confirmButton:"ui button primary"},icon:"warning",width:n,onAfterClose:function(){"function"==typeof e&&setTimeout(function(){e()})}})},confirm:function(t,e){let n="20em",o={confirmButtonText:"确定",cancelButtonText:"取消",showCancelButton:!0,showCloseButton:!1,buttonsStyling:!1,customClass:{closeButton:"ui button",cancelButton:"ui button",confirmButton:"ui button primary"},icon:"warning",width:n=30<t.length?"30em":n,preConfirm:function(){"function"==typeof e&&e.call(Tea.Vue)}};t.startsWith("html:")?o.html=t.substring(5):o.text=t,Swal.fire(o)},reload:function(){window.location.reload()},renderBarChart:function(e){var n=e.id;if(null==n||0==n.length)throw new Error("'options.id' should not be empty");var o=e.name;let i=e.values;if(null==i||!(i instanceof Array))throw new Error("'options.values' should be array");var a=e.x;if("function"!=typeof a)throw new Error("'options.x' should be a function");let r=e.tooltip;if("function"!=typeof r)throw new Error("'options.tooltip' should be a function");let l=e.axis;null==l&&(l={unit:"",count:1});var u=e.value;if("function"!=typeof u)throw new Error("'options.value' should be a function");let s=e.click,c=24,h=0;e=document.getElementById(n);if(null!=e){let t=this.initChart(e);n=this.xRotation(t,i.map(a)),e=(null!=n&&(c=n[0],h=n[1]),{xAxis:{data:i.map(a),axisLabel:{interval:0,rotate:h}},yAxis:{axisLabel:{formatter:function(t){return t+l.unit}}},tooltip:{show:!0,trigger:"item",formatter:function(t){return r.apply(this,[t,i])}},grid:{left:40,top:10,right:20,bottom:c},series:[{name:o,type:"bar",data:i.map(u),itemStyle:{color:this.DefaultChartColor},barWidth:"10em",areaStyle:{}}],animation:!0});t.setOption(e),null!=s&&t.on("click",function(t){s.call(this,t,i)}),t.resize()}},renderLineChart:function(e){var n=e.id,o=e.name;let i=e.values;var a=e.x,r=e.xColor;let l=e.tooltip,u=e.axis;var s=e.value,c=e.max,h=e.interval;let p=e.left,f=("number"!=typeof p&&(p=0),e.right);"number"!=typeof f&&(f=0);var m=document.getElementById(n);if(null==m)console.error("chart id '"+n+"' not found");else{let t=this.initChart(m,e.cache);n={xAxis:{data:i.map(a),axisLabel:{interval:h,textStyle:{color:r}}},yAxis:{axisLabel:{formatter:function(t){return t+u.unit}},max:c},tooltip:{show:!0,trigger:"item",formatter:function(t){return null!=l?l.apply(this,[t,i]):null}},grid:{left:40+p,top:10,right:20+f,bottom:20},series:[{name:o,type:"line",data:i.map(s),itemStyle:{color:this.DefaultChartColor},areaStyle:{},smooth:!0,markLine:e.markLine}],animation:!0,smooth:!0};t.setOption(n),t.resize()}},renderGaugeChart:function(e){var n=e.id,o=e.name,i=e.min,a=e.max,r=e.value;let l=e.unit;e.detail;var u=e.color;let s=e.startAngle,c=(null==s&&(s=225),e.endAngle);null==c&&(c=-45);u=this.chartColor(u),e=document.getElementById(n);if(null!=e){let t=this.initChart(e);n={textStyle:{fontFamily:"Lato,'Helvetica Neue',Arial,Helvetica,sans-serif"},color:u,title:null!=o&&0<o.length?{text:o,top:1,bottom:0,x:"center",textStyle:{fontSize:12,fontWeight:"bold",fontFamily:"Lato,'Helvetica Neue',Arial,Helvetica,sans-serif"}}:null,legend:{data:[""]},xAxis:{data:[]},yAxis:{},series:[{name:"",type:"gauge",min:i,max:a,startAngle:s,endAngle:c,data:[{name:"",value:Math.round(100*r)/100}],radius:"100%",center:["50%",null!=o&&0<o.length?"60%":"50%"],splitNumber:5,splitLine:{length:4},axisLine:{lineStyle:{width:4}},axisTick:{show:!0,length:2},axisLabel:{formatter:function(t){return t},textStyle:{fontSize:8}},progress:{show:!0,width:4},detail:{formatter:function(t){return l},textStyle:{fontSize:12,fontWeight:"normal",fontFamily:"Arial,Helvetica,sans-serif",color:"grey"},valueAnimation:!0},pointer:{width:2}}],grid:{left:-2,right:0,bottom:0,top:0},axisPointer:{show:!1},tooltip:{formatter:"X:{b0} Y:{c0}",show:!1},animation:!0};t.setOption(n)}},renderPercentChart:function(n){var o=n.id,i=this.chartColor(n.color),a=n.value,r=n.name;let l=n.total,u=(null==l&&(l=100),n.unit),s=(null==u&&(u=""),n.max);null!=s&&s<=a&&(s=null);var c=this.chartColor(n.maxColor),n=n.maxName,o=document.getElementById(o);if(null!=o){let t=this.initChart(o),e={tooltip:{formatter:"{a} <br/>{b} : {c}"+u},series:[{name:r,max:l,type:"gauge",radius:"100%",detail:{formatter:"{value}",show:!1,valueAnimation:!0},data:[{value:a,name:r}],pointer:{show:!1},splitLine:{show:!1},axisTick:{show:!1},axisLine:{show:!0,lineStyle:{width:4}},progress:{show:!0,width:4,itemStyle:{color:i}},splitNumber:{show:!1},title:{show:!1},startAngle:270,endAngle:-90}]};null!=s&&e.series.push({name:n,max:l,type:"gauge",radius:"100%",detail:{formatter:"{value}",show:!1,valueAnimation:!0},data:[{value:s,name:n}],pointer:{show:!1},splitLine:{show:!1},axisTick:{show:!1},axisLine:{show:!0,lineStyle:{width:4}},progress:{show:!0,width:4,itemStyle:{color:c,opacity:.3}},splitNumber:{show:!1},title:{show:!1},startAngle:270,endAngle:-90}),t.setOption(e)}},xRotation:function(t,e){t=t.getWidth();let n=0;return e.forEach(function(t){n+=10*t.length}),n<=t?null:[40,-20]},chartMap:{},initChart:function(t,e){"boolean"!=typeof e&&(e=!0);var n=t.getAttribute("id");if(null!=n&&0<n.length&&"object"==typeof this.chartMap[n])return this.chartMap[n];let o=echarts.init(t);return window.addEventListener("resize",function(){o.resize()}),e&&(this.chartMap[n]=o),o},encodeHTML:function(t){return t=(t=(t=(t=t.replace(/&/g,"&amp;")).replace(/</g,"&lt;")).replace(/>/g,"&gt;")).replace(/"/g,"&quot;")},chartColor:function(t){return t="green"==(t="blue"==(t="yellow"==(t="red"==(t=null!=t&&0!=t.length?t:"#5470c6")?"#ee6666":t)?"#fac858":t)?"#5470c6":t)?"#3ba272":t},DefaultChartColor:"#9DD3E8",validateIP:function(n){if("string"!=typeof n)return!1;if(0==n.length)return!1;if(0<=n.indexOf(":")){let t=n.split(":");if(8<t.length)return!1;let e=!0;return t.forEach(function(t){/^[\da-fA-F]{0,4}$/.test(t)||(e=!1)}),e}if(!n.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))return!1;let t=n.split("."),e=!0;return t.forEach(function(t){255<parseInt(t)&&(e=!1)}),e},playAlert:function(){let t=document.createElement("AUDIO");t.setAttribute("control",""),t.setAttribute("autoplay",""),t.innerHTML='<source src="/audios/alert.ogg" type="audio/ogg"/>',document.body.appendChild(t),t.play().then(function(){setTimeout(function(){document.body.removeChild(t)},2e3)}).catch(function(t){console.log(t.message)})},convertSizeCapacityToBytes:function(t){if(null==t)return 0;switch(t.unit){case"byte":return t.count;case"kb":return 1024*t.count;case"mb":return t.count*Math.pow(1024,2);case"gb":return t.count*Math.pow(1024,3);case"tb":return t.count*Math.pow(1024,4);case"pb":return t.count*Math.pow(1024,5);case"eb":return t.count*Math.pow(1024,6)}return 0},compareSizeCapacity:function(t,e){t=this.convertSizeCapacityToBytes(t),e=this.convertSizeCapacityToBytes(e);return t==e?0:e<t?1:-1},convertSizeCapacityToString:function(t){return null==t||null==t.count||null==t.unit?"":t.count+t.unit.toString().toUpperCase()}},String.prototype.quoteIP=function(){let t=this.toString();return 0==t.length?"":t.indexOf(":")<0||"["==t.substring(0,1)?t:"["+t+"]"};