новые иконки в OpenBoard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OpenBoard/resources/library/applications/Calculatrice.wgt/js/ubw-main.js

527 lines
14 KiB

14 years ago
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
14 years ago
function init(){
var h = 292;
var w = 160;
var wh = h+6;
var ww = w+6;
var clickFlag = false;
var ubwidget = $("#ubwidget").ubwidget({
width:w,
height:h
});
14 years ago
var historyTab = $("<div></div>")
.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.sankore){
window.sankore.setPreference('historyTab', "visible");
};
},
function(){
resizeWidget(200, 294);
historyPanel.hide();
$(".ubw-container").css({
backgroundImage:"url(images/back_small.png)",
width:160
});
if(window.sankore){
window.sankore.setPreference('historyTab', "hidden");
};
}
)
.hover(
function(){
$(this).css({
backgroundImage:"url(images/historytabOver.png)"
});
},
function(){
$(this).css({
backgroundImage:"url(images/historytab.png)"
});
}
)
.appendTo($("body"));
14 years ago
var space = $("<div></div>");
space.css({
width:5,
height:1,
float:"left"
});
14 years ago
var keysPanel = $("<div id='keysPanel'></div>").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;
14 years ago
var historyTxt = "0";
14 years ago
var display = $("<div id='display'>0</div>")
.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",
});
14 years ago
var standardPanel = $("<div></div>").css({
float:"left"
});
14 years ago
var cKeySize = {
w:33,
h:36
};
14 years ago
var btn7 = $("<div><img src='images/touche7.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("7", 1)
});
var btn8 = $("<div><img src='images/touche8.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("8", 1)
});
var btn9 = $("<div><img src='images/touche9.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("9", 1)
});
var btnDiv = $("<div><img src='images/touchediv.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("/", 0)
});
var btn4 = $("<div><img src='images/touche4.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("4", 1)
});
var btn5 = $("<div><img src='images/touche5.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("5", 1)
});
var btn6 = $("<div><img src='images/touche6.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("6", 1)
});
var btnMul = $("<div><img src='images/touchef.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("*", 0)
});
var btn1 = $("<div><img src='images/touche1.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("1", 1)
});
var btn2 = $("<div><img src='images/touche2.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("2", 1)
});
var btn3 = $("<div><img src='images/touche3.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("3", 1)
});
var btnSou = $("<div><img src='images/touchem.png'/></div>").ubwbutton({
w:68,
h:34
})
.mousedown(function(){
appendToDisplay("-", 0)
});
var btn0 = $("<div><img src='images/touche0.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("0", 1)
});
var btnDot = $("<div><img src='images/touchedot.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay(".", 1)
});
var btnC = $("<div><img src='images/touchec.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
reset()
});
var btnAdd = $("<div><img src='images/touchep.png'/></div>").ubwbutton({
w:68,
h:34
})
.mousedown(function(){
appendToDisplay("+", 0)
});
var btnPaL = $("<div><img src='images/touchepg.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay("(", 1)
});
var btnPaR = $("<div><img src='images/touchepd.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
appendToDisplay(")", 1)
});
var btnEqu = $("<div><img src='images/toucheeq.png'/></div>").ubwbutton({
w:cKeySize.w,
h:cKeySize.h
})
.mousedown(function(){
compute()
});
14 years ago
var historyPanel = $("<div id='historyPanel'></div>")
.css({
backgroundImage:"url(images/historyback.png)",
width:"auto",
height:"auto",
float:"left",
marginLeft:3,
marginRight:2,
marginTop:6
})
.hide();
14 years ago
var historyBox = $("<textarea id='historyBox'></textarea>")
.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");
14 years ago
historyPanel.append(historyBox);
14 years ago
var stop = $("<div></div>");
var sleft = $("<div></div>");
var sright = $("<div></div>");
14 years ago
stop.css({
width:140
})
.append(btnAdd).append(btnSou)
.append(btn7).append(btn8).append(btn9).append(btnMul)
.append(btn4).append(btn5).append(btn6).append(btnDiv);
14 years ago
sleft.css({
width:105,
float:"left"
})
.append(btn1).append(btn2).append(btn3)
.append(btn0).append(btnDot).append(btnC);
14 years ago
sright.css({
width:45,
float:"left"
})
.append(btnEqu);
14 years ago
standardPanel.css({
width:150
})
.append(stop)
.append(sleft).append(sright);
14 years ago
keysPanel
.append(display)
.append(standardPanel);
14 years ago
ubwidget
.append(historyPanel)
.append(keysPanel);
14 years ago
if(window.sankore){
historyTxt = window.sankore.preference('historyTxt', historyTxt);
var ht = window.sankore.preference('historyTab', "hidden");
$("#historyBox").val(historyTxt);
if(ht === "visible"){
historyTab.trigger("click");
};
}
14 years ago
function resizeWidget(w, h){
window.sankore.resize(w+2, h+2);
}
14 years ago
function compute(){
var result = eval(calc)
14 years ago
$("#display").text(updateDisplay(result));
lastResult = result;
lastchar.type = "Nan";
lastchar.value = "";
calc = "";
14 years ago
$("#historyBox").val($("#historyBox").val()+historyTrunk+"\n= "+updateDisplay(result)+"\n\n");
$("#historyBox").scrollTop(99999);
14 years ago
displayTrunk = "";
historyTrunk = "";
14 years ago
if(window.sankore){
window.sankore.setPreference('historyTxt', $("#historyBox").val());
};
14 years ago
lastHistory = $("#historyBox").val();
}
14 years ago
function reset(){
$("#display").text("0");
calc="";
displayTrunk="";
historyTrunk="";
$("#historyBox").val(lastHistory);
$("#historyBox").scrollTop(99999);
}
14 years ago
function formatNumberWithDelimiters(number) {
var delimiter = gThousandsSeparator;
var numString = number.toString();
14 years ago
if (!numString)
return "0";
14 years ago
var dot = numString.indexOf(gDecimalSeparator);
if (dot == -1)
dot = numString.length;
14 years ago
var stop = numString.length-dot;
var characteristic = numString.substr(0, dot);
if (!parseInt(characteristic))
return numString;
14 years ago
// see if it's a negative number
var numIsNegative = (parseInt(characteristic) < 0)
14 years ago
var newNumber = "";
var delimiterCount = Math.floor((characteristic.length-1) / 3);
var extras = characteristic.length % 3;
if (!extras && characteristic.length > 2)
extras = 3;
14 years ago
if (extras)
newNumber = newNumber + characteristic.substr(0, extras);
14 years ago
for (var i=0;i< delimiterCount; i++) {
14 years ago
if ( (0 == i) && numIsNegative && (extras == 1))
newNumber = newNumber + characteristic.substr(extras + (i * 3), 3);
else
newNumber = newNumber + delimiter + characteristic.substr(extras + (i * 3), 3);
}
14 years ago
return (dot ? (newNumber + numString.substr(dot, stop)) : newNumber);
}
14 years ago
function formatNumberWithScientificNotation(number) {
if (number == 0)
return number;
14 years ago
var numString = number.toString();
if (!numString)
return "0";
14 years ago
var num = new Number(numString);
var sci = num.toExponential(5);
if (sci == "NaN")
sci = formatNumberWithDelimiters(numString);
if (!sci)
return "0";
14 years ago
return sci;
}
14 years ago
function updateDisplay(number){
if(String(number).length > 8){
number = formatNumberWithScientificNotation(number);
}
else{
number = number;
}
14 years ago
return number;
}
14 years ago
function appendToDisplay(_char, v){
var char = _char;
14 years ago
// Display
if($("#historyBox").val() === "0"){
$("#historyBox").val("");
};
14 years ago
if(char == "-" && lastchar.type == "NaN"){
char = "m";
};
14 years ago
// char is a number
if(char <= 0 || char > 0 || char == "." || char == "m" || char == "(" || char == ")"){
if(char == "m"){
char = "-";
};
14 years ago
if(lastchar.type == "NaN"){
displayTrunk = "";
};
14 years ago
if(lastchar.value == "." && char == "."){
calc = calc.substr(0, calc.length-1);
displayTrunk = displayTrunk.substr(0, displayTrunk.length-1);
};
14 years ago
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);
}
14 years ago
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){}
}
14 years ago
lastchar.type = "NaN";
try{
displayTrunk = eval(calc.substr(0, calc.length-1));
}catch(e){}
$("#historyBox").val($("#historyBox").val()+historyTrunk+char);
$("#historyBox").scrollTop(99999);
historyTrunk = "";
}
14 years ago
lastchar.value = char;
$("#display").text(updateDisplay(displayTrunk));
}
$("#historyBox").click(function(){
if(!clickFlag){
$(this).select();
clickFlag = true;
}
else{
clickFlag = false;
$(this).blur();
}
});
$(document).disableTextSelect();
14 years ago
};