2 lines
4.4 KiB
JavaScript
2 lines
4.4 KiB
JavaScript
import{s as c}from"./svgLoader.qHdFluC4.js";import{_ as h}from"./index.CjrDwa0W.js";import{f as u,i as r,t as v,F as d,m as f,h as o,l as m}from"./vendor-vue.CmaRnji1.js";import"./vendor.DSdF8huI.js";import"./swiper.tg5sgs8j.js";const y={name:"CalculatorApp",components:{},data(){return{svgs:c,displayValue:"0",previousValue:null,currentOperation:null,waitingForOperand:!1,keyboard:[{svg:"ac",value:"ac",type:"action"},{svg:"plusminus",value:"+/-",type:"action"},{svg:"percent",value:"%",type:"action"},{svg:"slash",value:"/",type:"operator"},{svg:"seven",value:"7",type:"number"},{svg:"eight",value:"8",type:"number"},{svg:"nine",value:"9",type:"number"},{svg:"multiply",value:"x",type:"operator"},{svg:"four",value:"4",type:"number"},{svg:"five",value:"5",type:"number"},{svg:"six",value:"6",type:"number"},{svg:"minus",value:"-",type:"operator"},{svg:"one",value:"1",type:"number"},{svg:"two",value:"2",type:"number"},{svg:"three",value:"3",type:"number"},{svg:"plus",value:"+",type:"operator"},{svg:"zero",value:"0",type:"number"},{svg:"coma",value:",",type:"action"},{svg:"equals",value:"=",type:"action"}]}},mounted(){window.addEventListener("keydown",this.handleKeyPress)},beforeUnmount(){window.removeEventListener("keydown",this.handleKeyPress)},methods:{handleButtonClick(t){switch(t.type){case"number":this.inputNumber(t.value);break;case"operator":this.inputOperator(t.value);break;case"action":this.performAction(t.value);break}},inputNumber(t){this.waitingForOperand?(this.displayValue=String(t),this.waitingForOperand=!1):this.displayValue=this.displayValue==="0"?String(t):this.displayValue+t},inputOperator(t){const e=parseFloat(this.displayValue.replace(",","."));if(this.previousValue===null)this.previousValue=e;else if(this.currentOperation){const s=this.previousValue||0,l=this.calculate(s,e,this.currentOperation);this.displayValue=this.formatNumber(l),this.previousValue=l}this.waitingForOperand=!0,this.currentOperation=t},performAction(t){switch(t){case"ac":this.clear();break;case"+/-":this.toggleSign();break;case"%":this.percentage();break;case",":this.inputDecimal();break;case"=":this.calculate();break}},clear(){this.displayValue="0",this.previousValue=null,this.currentOperation=null,this.waitingForOperand=!1},toggleSign(){const t=parseFloat(this.displayValue.replace(",","."));this.displayValue=this.formatNumber(t*-1)},percentage(){const t=parseFloat(this.displayValue.replace(",","."));this.displayValue=this.formatNumber(t/100)},inputDecimal(){this.waitingForOperand?(this.displayValue="0,",this.waitingForOperand=!1):this.displayValue.indexOf(",")===-1&&(this.displayValue+=",")},calculate(t=null,e=null,s=null){const l=t!==null?t:this.previousValue,i=e!==null?e:parseFloat(this.displayValue.replace(",",".")),n=s||this.currentOperation;if(l===null||!n)return;let a;switch(n){case"+":a=l+i;break;case"-":a=l-i;break;case"x":a=l*i;break;case"/":a=i!==0?l/i:0;break;default:return}return s===null&&(this.displayValue=this.formatNumber(a),this.previousValue=null,this.currentOperation=null,this.waitingForOperand=!0),a},formatNumber(t){const e=Math.round(t*1e8)/1e8;let s=String(e).replace(".",",");return s.length>10&&(s=t.toExponential(5).replace(".",",")),s},handleKeyPress(t){const e=t.key;if(e>="0"&&e<="9")this.inputNumber(e);else if(e==="+"||e==="-"||e==="*"||e==="/"){const s=e==="*"?"x":e;this.inputOperator(s)}else e==="Enter"||e==="="?this.performAction("="):e==="Escape"||e==="c"||e==="C"?this.performAction("ac"):e==="."||e===","?this.performAction(","):e==="%"&&this.performAction("%")}}},g={class:"w-full rounded-[44em] overflow-hidden h-full flex items-center justify-center p-[0.5vw] bg-black"},b={class:"w-full h-full"},w={class:"w-full h-[40%]"},V={class:"w-full h-[100%] flex items-end justify-end"},k={class:"text-white font-['SFPro'] font-[400] text-[40em] pr-[0.5em]"},_={class:"w-[full] h-[312em] px-[8em] mt-[8.4%] flex flex-wrap justify-between"},O=["onClick"],x=["src"];function F(t,e,s,l,i,n){return o(),u("div",g,[r("div",b,[e[0]||(e[0]=r("div",{class:"w-full h-[2%]"},null,-1)),r("div",w,[r("div",V,[r("p",k,v(i.displayValue),1)])]),r("div",_,[(o(!0),u(d,null,f(i.keyboard,(a,p)=>(o(),u("div",{class:m([[p<=3?"":"mt-[5em]","cursor-pointer transition-opacity hover:opacity-80",a.value==="0"?"w-[50%]":""],"aspect-square h-[20%] rounded-full"]),key:a.svg,onClick:N=>n.handleButtonClick(a)},[r("img",{src:i.svgs["calculator/"+a.svg+".svg"],class:"w-full h-full",alt:""},null,8,x)],10,O))),128))])])])}const P=h(y,[["render",F],["__scopeId","data-v-d356bb77"]]);export{P as default};
|