function init(){ var h = 292; var w = 160; var wh = h+6; var ww = w+6; var ubwidget = $("#ubwidget").ubwidget({ width:w, height:h }); var historyTab = $("
") .css({ height:h, marginRight:0, width:20, float:"left", backgroundImage:"url(images/historytab.png)", backgroundRepeat:"no-repeat" }) .toggle( function(){ resizeWidget(370, 294); historyPanel.show(); $(".ubw-container").css({ backgroundImage:"url(images/back.png)", width:328 }); if(window.uniboard){ window.uniboard.setPreference('historyTab', "visible"); }; }, function(){ resizeWidget(200, 294); historyPanel.hide(); $(".ubw-container").css({ backgroundImage:"url(images/back_small.png)", width:160 }); if(window.uniboard){ window.uniboard.setPreference('historyTab', "hidden"); }; } ) .hover( function(){ $(this).css({ backgroundImage:"url(images/historytabOver.png)" }); }, function(){ $(this).css({ backgroundImage:"url(images/historytab.png)" }); } ) .appendTo($("body")); var space = $("
"); space.css({width:5, height:1, float:"left"}); var keysPanel = $("
").css({float:"left"}); var mode = "standard"; var calc = ""; var displayTrunk = ""; var historyTrunk = ""; var lastHistory = ""; var subtrunk = 0; var itrunk = 0; var trunkpos = 0; var lastchar = {type:"NaN", value:"null"}; var inparenthesis = false; var erase = false; var abtn; var readyToCompute = false; var lastResult = "0"; var gDecimalSeparator = ""; var gThousandsSeparator = ""; var gDecimalCode = 0; var gDecimalString = ""; var gError = false; var historyTxt = "0"; var display = $("
0
") .addClass("ubw-inputbox") .css({ padding:"12px", marginLeft:"2px", marginBottom:"9px", marginTop:7, backgroundImage:"url(images/display.png)", fontSize:"22px", fontFamily:"Arial, Helvetica, Sans-serif", color:"#777788", width:118, height:28, overflow:"hidden", textAlign:"right", }); var standardPanel = $("
").css({float:"left"}); var cKeySize = {w:33, h:36}; var btn7 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("7", 1)}); var btn8 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("8", 1)}); var btn9 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("9", 1)}); var btnDiv = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("/", 0)}); var btn4 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("4", 1)}); var btn5 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("5", 1)}); var btn6 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("6", 1)}); var btnMul = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("*", 0)}); var btn1 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("1", 1)}); var btn2 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("2", 1)}); var btn3 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("3", 1)}); var btnSou = $("
").ubwbutton({w:68, h:34}) .mousedown(function(){appendToDisplay("-", 0)}); var btn0 = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("0", 1)}); var btnDot = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay(".", 1)}); var btnC = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){reset()}); var btnAdd = $("
").ubwbutton({w:68, h:34}) .mousedown(function(){appendToDisplay("+", 0)}); var btnPaL = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay("(", 1)}); var btnPaR = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){appendToDisplay(")", 1)}); var btnEqu = $("
").ubwbutton({w:cKeySize.w, h:cKeySize.h}) .mousedown(function(){compute()}); var historyPanel = $("
") .css({ backgroundImage:"url(images/historyback.png)", width:"auto", height:"auto", float:"left", marginLeft:3, marginRight:2, marginTop:6 }) .hide(); var historyBox = $("") .css({ padding:10, fontSize:"20px", fontFamily:"Arial, Helvetica, Sans-serif", width:143, height:236, backgroundColor:"transparent", resize:"none", border:"none", overlay:"none", color:"#eeeeee" }) .val("0"); historyPanel.append(historyBox); var stop = $("
"); var sleft = $("
"); var sright = $("
"); stop.css({width:140}) .append(btnAdd).append(btnSou) .append(btn7).append(btn8).append(btn9).append(btnMul) .append(btn4).append(btn5).append(btn6).append(btnDiv); sleft.css({width:105, float:"left"}) .append(btn1).append(btn2).append(btn3) .append(btn0).append(btnDot).append(btnC); sright.css({width:45, float:"left"}) .append(btnEqu); standardPanel.css({width:150}) .append(stop) .append(sleft).append(sright); keysPanel .append(display) .append(standardPanel); ubwidget .append(historyPanel) .append(keysPanel); if(window.uniboard){ historyTxt = window.uniboard.preference('historyTxt', historyTxt); var ht = window.uniboard.preference('historyTab', "hidden"); $("#historyBox").val(historyTxt); if(ht === "visible"){ historyTab.trigger("click"); }; } function resizeWidget(w, h){ window.uniboard.resize(w+2, h+2); $("#indicator").remove(); var indicator = $("
") .css({ width:w, height:h, position:"absolute", left:0, top:0, border:"1px solid #ff0000" }); } function compute(){ var result = eval(calc) $("#display").text(updateDisplay(result)); lastResult = result; lastchar.type = "Nan"; lastchar.value = ""; calc = ""; $("#historyBox").val($("#historyBox").val()+historyTrunk+"\n= "+updateDisplay(result)+"\n\n"); $("#historyBox").scrollTop(99999); displayTrunk = ""; historyTrunk = ""; if(window.uniboard){ window.uniboard.setPreference('historyTxt', $("#historyBox").val()); }; lastHistory = $("#historyBox").val(); } function reset(){ $("#display").text("0"); calc=""; displayTrunk=""; historyTrunk=""; $("#historyBox").val(lastHistory); $("#historyBox").scrollTop(99999); } function formatNumberWithDelimiters(number) { var delimiter = gThousandsSeparator; var numString = number.toString(); if (!numString) return "0"; var dot = numString.indexOf(gDecimalSeparator); if (dot == -1) dot = numString.length; var stop = numString.length-dot; var characteristic = numString.substr(0, dot); if (!parseInt(characteristic)) return numString; // see if it's a negative number var numIsNegative = (parseInt(characteristic) < 0) var newNumber = ""; var delimiterCount = Math.floor((characteristic.length-1) / 3); var extras = characteristic.length % 3; if (!extras && characteristic.length > 2) extras = 3; if (extras) newNumber = newNumber + characteristic.substr(0, extras); for (var i=0;i< delimiterCount; i++) { if ( (0 == i) && numIsNegative && (extras == 1)) newNumber = newNumber + characteristic.substr(extras + (i * 3), 3); else newNumber = newNumber + delimiter + characteristic.substr(extras + (i * 3), 3); } return (dot ? (newNumber + numString.substr(dot, stop)) : newNumber); } function formatNumberWithScientificNotation(number) { if (number == 0) return number; var numString = number.toString(); if (!numString) return "0"; var num = new Number(numString); var sci = num.toExponential(5); if (sci == "NaN") sci = formatNumberWithDelimiters(numString); if (!sci) return "0"; return sci; } function updateDisplay(number){ if(String(number).length > 8){ number = formatNumberWithScientificNotation(number); } else{ number = number; } return number; } function appendToDisplay(_char, v){ var char = _char; // Display if($("#historyBox").val() === "0"){ $("#historyBox").val(""); }; if(char == "-" && lastchar.type == "NaN"){ char = "m"; }; // char is a number if(char <= 0 || char > 0 || char == "." || char == "m" || char == "(" || char == ")"){ if(char == "m"){ char = "-"; }; if(lastchar.type == "NaN"){ displayTrunk = ""; }; if(lastchar.value == "." && char == "."){ calc = calc.substr(0, calc.length-1); displayTrunk = displayTrunk.substr(0, displayTrunk.length-1); }; calc += String(char); displayTrunk += String(char); historyTrunk += String(char); lastchar.type = "Number"; } // char is an operator else { if(calc.length === 0){ var endCalc = calc; calc = lastResult + endCalc; historyTrunk = updateDisplay(lastResult); } if(lastchar.type == "NaN"){ calc = calc.substr(0, calc.length-1); calc += char; $("#historyBox").val($("#historyBox").val().substr(0, $("#historyBox").val().length-1)); } else{ try{ calc = eval(calc) + String(char); }catch(e){} } lastchar.type = "NaN"; try{ displayTrunk = eval(calc.substr(0, calc.length-1)); }catch(e){} $("#historyBox").val($("#historyBox").val()+historyTrunk+char); $("#historyBox").scrollTop(99999); historyTrunk = ""; } lastchar.value = char; $("#display").text(updateDisplay(displayTrunk)); } };