examples/script/calculator/calculator.js
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 Function.prototype.bind = function() {
       
     2     var func = this;
       
     3     var thisObject = arguments[0];
       
     4     var args = Array.prototype.slice.call(arguments, 1);
       
     5     return function() {
       
     6         return func.apply(thisObject, args);
       
     7     }
       
     8 }
       
     9 
       
    10 //! [0]
       
    11 function Calculator(ui)
       
    12 {
       
    13     this.ui = ui;
       
    14 
       
    15     this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
       
    16     this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
       
    17     this.sumInMemory = 0;
       
    18     this.sumSoFar = 0;
       
    19     this.factorSoFar = 0;
       
    20     this.waitingForOperand = true;
       
    21 
       
    22     with (ui) {
       
    23         display.text = "0";
       
    24 
       
    25         zeroButton.clicked.connect(this.digitClicked.bind(this, 0));
       
    26         oneButton.clicked.connect(this.digitClicked.bind(this, 1));
       
    27         twoButton.clicked.connect(this.digitClicked.bind(this, 2));
       
    28         threeButton.clicked.connect(this.digitClicked.bind(this, 3));
       
    29         fourButton.clicked.connect(this.digitClicked.bind(this, 4));
       
    30         fiveButton.clicked.connect(this.digitClicked.bind(this, 5));
       
    31         sixButton.clicked.connect(this.digitClicked.bind(this, 6));
       
    32         sevenButton.clicked.connect(this.digitClicked.bind(this, 7));
       
    33         eightButton.clicked.connect(this.digitClicked.bind(this, 8));
       
    34         nineButton.clicked.connect(this.digitClicked.bind(this, 9));
       
    35 
       
    36         pointButton.clicked.connect(this, "pointClicked");
       
    37         changeSignButton.clicked.connect(this, "changeSignClicked");
       
    38 
       
    39         backspaceButton.clicked.connect(this, "backspaceClicked");
       
    40         clearButton.clicked.connect(this, "clear");
       
    41         clearAllButton.clicked.connect(this, "clearAll");
       
    42 
       
    43         clearMemoryButton.clicked.connect(this, "clearMemory");
       
    44         readMemoryButton.clicked.connect(this, "readMemory");
       
    45         setMemoryButton.clicked.connect(this, "setMemory");
       
    46         addToMemoryButton.clicked.connect(this, "addToMemory");
       
    47   
       
    48         divisionButton.clicked.connect(this.multiplicativeOperatorClicked.bind(this, Calculator.DIVISION_OPERATOR));
       
    49         timesButton.clicked.connect(this.multiplicativeOperatorClicked.bind(this, Calculator.TIMES_OPERATOR));
       
    50         minusButton.clicked.connect(this.additiveOperatorClicked.bind(this, Calculator.MINUS_OPERATOR));
       
    51         plusButton.clicked.connect(this.additiveOperatorClicked.bind(this, Calculator.PLUS_OPERATOR));
       
    52 
       
    53         squareRootButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.SQUARE_OPERATOR));
       
    54         powerButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.POWER_OPERATOR));
       
    55         reciprocalButton.clicked.connect(this.unaryOperatorClicked.bind(this, Calculator.RECIPROCAL_OPERATOR));
       
    56         equalButton.clicked.connect(this, "equalClicked");
       
    57     }
       
    58 }
       
    59 //! [0]
       
    60 
       
    61 Calculator.NO_OPERATOR = 0;
       
    62 Calculator.SQUARE_OPERATOR = 1;
       
    63 Calculator.POWER_OPERATOR = 2;
       
    64 Calculator.RECIPROCAL_OPERATOR = 3;
       
    65 Calculator.DIVISION_OPERATOR = 4;
       
    66 Calculator.TIMES_OPERATOR = 5;
       
    67 Calculator.MINUS_OPERATOR = 6;
       
    68 Calculator.PLUS_OPERATOR = 7;
       
    69 
       
    70 Calculator.prototype.abortOperation = function()
       
    71 {
       
    72     this.clearAll();
       
    73     this.ui.display.text = "####";
       
    74 }
       
    75 
       
    76 Calculator.prototype.calculate = function(rightOperand, pendingOperator)
       
    77 {
       
    78     if (pendingOperator == Calculator.PLUS_OPERATOR) {
       
    79         this.sumSoFar += rightOperand;
       
    80     } else if (pendingOperator == Calculator.MINUS_OPERATOR) {
       
    81         this.sumSoFar -= rightOperand;
       
    82     } else if (pendingOperator == Calculator.TIMES_OPERATOR) {
       
    83         this.factorSoFar *= rightOperand;
       
    84     } else if (pendingOperator == Calculator.DIVISION_OPERATOR) {
       
    85         if (rightOperand == 0)
       
    86             return false;
       
    87         this.factorSoFar /= rightOperand;
       
    88     }
       
    89     return true;
       
    90 }
       
    91 
       
    92 //! [1]
       
    93 Calculator.prototype.digitClicked = function(digitValue)
       
    94 {
       
    95     if ((digitValue == 0) && (this.ui.display.text == "0"))
       
    96         return;
       
    97     if (this.waitingForOperand) {
       
    98         this.ui.display.clear();
       
    99         this.waitingForOperand = false;
       
   100     }
       
   101     this.ui.display.text += digitValue;
       
   102 }
       
   103 //! [1]
       
   104 
       
   105 Calculator.prototype.unaryOperatorClicked = function(op)
       
   106 {
       
   107     var operand = this.ui.display.text - 0;
       
   108     var result = 0;
       
   109     if (op == Calculator.SQUARE_OPERATOR) {
       
   110         if (operand < 0) {
       
   111             this.abortOperation();
       
   112             return;
       
   113         }
       
   114         result = Math.sqrt(operand);
       
   115     } else if (op == Calculator.POWER_OPERATOR) {
       
   116         result = Math.pow(operand, 2);
       
   117     } else if (op == Calculator.RECIPROCAL_OPERATOR) {
       
   118         if (operand == 0.0) {
       
   119             this.abortOperation();
       
   120             return;
       
   121         }
       
   122         result = 1 / operand;
       
   123     }
       
   124     this.ui.display.text = result + "";
       
   125     this.waitingForOperand = true;
       
   126 }
       
   127 
       
   128 Calculator.prototype.additiveOperatorClicked = function(op)
       
   129 {
       
   130     var operand = this.ui.display.text - 0;
       
   131 
       
   132     if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
       
   133         if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
       
   134             this.abortOperation();
       
   135             return;
       
   136         }
       
   137         this.ui.display.text = this.factorSoFar + "";
       
   138         operand = this.factorSoFar;
       
   139         this.factorSoFar = 0;
       
   140         this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
       
   141     }
       
   142 
       
   143     if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) {
       
   144         if (!this.calculate(operand, this.pendingAdditiveOperator)) {
       
   145             this.abortOperation();
       
   146             return;
       
   147         }
       
   148         this.ui.display.text = this.sumSoFar + "";
       
   149     } else {
       
   150         this.sumSoFar = operand;
       
   151     }
       
   152 
       
   153     this.pendingAdditiveOperator = op;
       
   154     this.waitingForOperand = true;
       
   155 }
       
   156 
       
   157 Calculator.prototype.multiplicativeOperatorClicked = function(op)
       
   158 {
       
   159     var operand = this.ui.display.text - 0;
       
   160 
       
   161     if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
       
   162         if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
       
   163             this.abortOperation();
       
   164             return;
       
   165         }
       
   166         this.ui.display.text = this.factorSoFar + "";
       
   167     } else {
       
   168         this.factorSoFar = operand;
       
   169     }
       
   170 
       
   171     this.pendingMultiplicativeOperator = op;
       
   172     this.waitingForOperand = true;
       
   173 }
       
   174 
       
   175 Calculator.prototype.equalClicked = function()
       
   176 {
       
   177     var operand = this.ui.display.text - 0;
       
   178 
       
   179     if (this.pendingMultiplicativeOperator != Calculator.NO_OPERATOR) {
       
   180         if (!this.calculate(operand, this.pendingMultiplicativeOperator)) {
       
   181             this.abortOperation();
       
   182             return;
       
   183         }
       
   184         operand = this.factorSoFar;
       
   185         this.factorSoFar = 0.0;
       
   186         this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
       
   187     }
       
   188     if (this.pendingAdditiveOperator != Calculator.NO_OPERATOR) {
       
   189         if (!this.calculate(operand, this.pendingAdditiveOperator)) {
       
   190             this.abortOperation();
       
   191             return;
       
   192         }
       
   193         this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
       
   194     } else {
       
   195         this.sumSoFar = operand;
       
   196     }
       
   197 
       
   198     this.ui.display.text = this.sumSoFar + "";
       
   199     this.sumSoFar = 0.0;
       
   200     this.waitingForOperand = true;
       
   201 }
       
   202 
       
   203 Calculator.prototype.pointClicked = function()
       
   204 {
       
   205     if (this.waitingForOperand)
       
   206         this.ui.display.text = "0";
       
   207     if (this.ui.display.text.indexOf(".") == -1)
       
   208         this.ui.display.text += ".";
       
   209     this.waitingForOperand = false;
       
   210 }
       
   211 
       
   212 //! [2]
       
   213 Calculator.prototype.changeSignClicked = function()
       
   214 {
       
   215     var text = this.ui.display.text;
       
   216     var value = text - 0;
       
   217 
       
   218     if (value > 0) {
       
   219         text = "-" + text;
       
   220     } else if (value < 0) {
       
   221         text = text.slice(1);
       
   222     }
       
   223     this.ui.display.text = text;
       
   224 }
       
   225 //! [2]
       
   226 
       
   227 Calculator.prototype.backspaceClicked = function()
       
   228 {
       
   229     if (this.waitingForOperand)
       
   230         return;
       
   231 
       
   232     var text = this.ui.display.text;
       
   233     text = text.slice(0, -1);
       
   234     if (text.length == 0) {
       
   235         text = "0";
       
   236         this.waitingForOperand = true;
       
   237     }
       
   238     this.ui.display.text = text;
       
   239 }
       
   240 
       
   241 Calculator.prototype.clear = function()
       
   242 {
       
   243     if (this.waitingForOperand)
       
   244         return;
       
   245 
       
   246     this.ui.display.text = "0";
       
   247     this.waitingForOperand = true;
       
   248 }
       
   249 
       
   250 Calculator.prototype.clearAll = function()
       
   251 {
       
   252     this.sumSoFar = 0.0;
       
   253     this.factorSoFar = 0.0;
       
   254     this.pendingAdditiveOperator = Calculator.NO_OPERATOR;
       
   255     this.pendingMultiplicativeOperator = Calculator.NO_OPERATOR;
       
   256     this.ui.display.text = "0";
       
   257     this.waitingForOperand = true;
       
   258 }
       
   259 
       
   260 Calculator.prototype.clearMemory = function()
       
   261 {
       
   262     this.sumInMemory = 0.0;
       
   263 }
       
   264 
       
   265 Calculator.prototype.readMemory = function()
       
   266 {
       
   267     this.ui.display.text = this.sumInMemory + "";
       
   268     this.waitingForOperand = true;
       
   269 }
       
   270 
       
   271 Calculator.prototype.setMemory = function()
       
   272 {
       
   273     this.equalClicked();
       
   274     this.sumInMemory = this.ui.display.text - 0;
       
   275 }
       
   276 
       
   277 Calculator.prototype.addToMemory = function()
       
   278 {
       
   279     this.equalClicked();
       
   280     this.sumInMemory += this.ui.display.text - 0;
       
   281 }