Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class Tape { | |
| 6 static final int OP_NOOP = 0; | |
|
Jacob
2012/04/05 18:06:01
looks like these could be const not final. same fo
| |
| 7 static final int OP_PLUS = 1; | |
| 8 static final int OP_MINUS = 2; | |
| 9 static final int OP_MULTI = 3; | |
| 10 static final int OP_DIV = 4; | |
| 11 static final int OP_EQUAL = 5; | |
| 12 static final int OP_CLEAR = 6; | |
| 13 | |
| 14 Tape() { | |
| 15 clearTape(); | |
| 16 } | |
| 17 | |
| 18 void addToTape(int op, String number) { | |
| 19 double displayTotal; | |
| 20 double numberAsValue; | |
| 21 | |
| 22 if (number != "." && number != "-" && number != "-.") { | |
| 23 try { | |
| 24 numberAsValue = Math.parseDouble(number.length == 0 ? "0" : number); | |
| 25 } catch (final BadNumberFormatException e) { | |
| 26 displayError(e.toString()); | |
| 27 return; | |
| 28 } | |
| 29 } else { | |
| 30 // Display just the . | |
| 31 displayTotal = total; | |
| 32 numberAsValue = 0.0; | |
| 33 } | |
| 34 | |
| 35 String opAsStr; | |
| 36 | |
| 37 if (op != OP_NOOP || currentOperator == null) { | |
| 38 // Compute the real running total user pressed a mathematical operator. | |
| 39 switch (currentOperator) { | |
| 40 case OP_NOOP: | |
| 41 total = numberAsValue; | |
| 42 break; | |
| 43 case OP_PLUS: | |
| 44 total += numberAsValue; | |
| 45 break; | |
| 46 case OP_MINUS: | |
| 47 total -= numberAsValue; | |
| 48 break; | |
| 49 case OP_MULTI: | |
| 50 total *= numberAsValue; | |
| 51 break; | |
| 52 case OP_DIV: | |
| 53 total /= numberAsValue; | |
|
Jacob
2012/04/05 18:06:01
do you want to do anything to avoid div by 0.?
| |
| 54 break; | |
| 55 case OP_EQUAL: | |
| 56 // Do nothing total is final total. | |
| 57 if (number.length == 0) return; | |
| 58 break; | |
| 59 default: | |
| 60 total = numberAsValue; | |
| 61 } | |
| 62 } else if (op == OP_NOOP && currentOperator == OP_EQUAL) { | |
| 63 // If no operator after = pressed we're starting from scratch. | |
| 64 total = numberAsValue; | |
| 65 } | |
| 66 | |
| 67 // Compute if the op (key pressed) it's not a mathematical operator but we | |
| 68 // want to display the current subtotal if real math operator was pressed. | |
| 69 if (op == OP_NOOP) { | |
| 70 switch (currentOperator) { | |
| 71 case OP_PLUS: | |
| 72 displayTotal = total + numberAsValue; | |
| 73 break; | |
| 74 case OP_MINUS: | |
| 75 displayTotal = total - numberAsValue; | |
| 76 break; | |
| 77 case OP_MULTI: | |
| 78 displayTotal = total * numberAsValue; | |
| 79 break; | |
| 80 case OP_DIV: | |
| 81 displayTotal = total / numberAsValue; | |
| 82 break; | |
| 83 case OP_EQUAL: | |
| 84 displayTotal = total; | |
| 85 break; | |
| 86 default: | |
| 87 // nothing to do. | |
| 88 } | |
| 89 } else { | |
| 90 displayTotal = total; | |
| 91 } | |
| 92 | |
| 93 // Current operator to display. | |
| 94 switch (op) { | |
| 95 case OP_NOOP: | |
| 96 case OP_EQUAL: | |
| 97 opAsStr = " "; | |
| 98 break; | |
| 99 case OP_PLUS: | |
| 100 opAsStr = "+ "; | |
|
Jacob
2012/04/05 18:06:01
perhaps use a the concise Dart syntax for a map li
| |
| 101 break; | |
| 102 case OP_MINUS: | |
| 103 opAsStr = "- "; | |
| 104 break; | |
| 105 case OP_MULTI: | |
| 106 opAsStr = "* "; | |
| 107 break; | |
| 108 case OP_DIV: | |
| 109 opAsStr = "/ "; | |
| 110 break; | |
| 111 } | |
| 112 if (op != OP_NOOP) { | |
| 113 currentOperator = op; | |
| 114 } | |
| 115 | |
| 116 final element = new Element.tag('div'); | |
| 117 | |
| 118 DivElement active = activeInput; | |
|
Jacob
2012/04/05 18:06:01
DivElement --> Element here and elsewhere
Casting
| |
| 119 if (op == OP_NOOP) { | |
| 120 if (active != null) { | |
| 121 String displayedOp = active.elements[0].text; | |
| 122 active.innerHTML = tapeUI.firstOp(displayedOp, number); | |
| 123 } else { | |
| 124 element.innerHTML = tapeUI.displayOpAndNumber(opAsStr, number); | |
| 125 tapeUI.tape.elements.add(element.elements[0]); | |
| 126 element.innerHTML = tapeUI.alignSubTotal();; | |
| 127 tapeUI.tape.elements.add(element.elements[0]); | |
| 128 element.innerHTML = tapeUI.lineBreak(); | |
| 129 tapeUI.tape.elements.add(element.elements[0]); | |
| 130 } | |
| 131 | |
| 132 // Update running total if it exist. | |
| 133 active = activeTotal; | |
| 134 if (active != null) { | |
| 135 active.text = "= ${formatOutput(displayTotal)}"; | |
| 136 } | |
| 137 } else { | |
| 138 removeActiveElements(); | |
| 139 | |
| 140 String leftSide; | |
| 141 if (op == OP_EQUAL) { | |
| 142 leftSide = tapeUI.displayEqual(); | |
| 143 } else { | |
| 144 leftSide = tapeUI.displayOp(opAsStr); | |
| 145 } | |
| 146 | |
| 147 element.innerHTML = leftSide; | |
| 148 tapeUI.tape.elements.add(element.elements[0]); | |
| 149 | |
| 150 if (op == OP_EQUAL) { | |
| 151 element.innerHTML = tapeUI.displayTotal(formatOutput(displayTotal)); | |
| 152 } else { | |
| 153 element.innerHTML = tapeUI.displayActiveTotal(); | |
| 154 } | |
| 155 tapeUI.tape.elements.add(element.elements[0]); | |
| 156 | |
| 157 element.innerHTML = tapeUI.lineBreak(); | |
| 158 tapeUI.tape.elements.add(element.elements[0]); | |
| 159 } | |
| 160 | |
| 161 scrollToTapeBottom(); | |
| 162 } | |
| 163 | |
| 164 String formatOutput(double displayTotal) { | |
| 165 String formattedNum = "${displayTotal.toStringAsFixed(5)}"; | |
| 166 int dotIdx = formattedNum.indexOf("."); | |
| 167 if (dotIdx >= 0) { | |
| 168 if (formattedNum.substring(dotIdx) == ".00000") { | |
| 169 formattedNum = formattedNum.substring(0, dotIdx); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 return formattedNum; | |
| 174 } | |
| 175 | |
| 176 void displayError(String err) { | |
| 177 removeActiveElements(); | |
| 178 DivElement element = new Element.tag("div"); | |
| 179 element.innerHTML = tapeUI.displayError(err); | |
| 180 tapeUI.tape.elements.add(element.elements[0]); | |
| 181 | |
| 182 scrollToTapeBottom(); | |
| 183 | |
| 184 currentOperator = null; | |
| 185 currentRegister = ""; | |
| 186 total = 0.0; | |
| 187 } | |
| 188 | |
| 189 void scrollToTapeBottom() { | |
| 190 tapeUI.tape.rect.then((ElementRect rect) { | |
| 191 // theTape.rect.scroll.top = rect.scroll.height; | |
| 192 // TODO(terry): Would like to set scrollTop of tape doesn't seem to work | |
| 193 // so I scroll by max lines. | |
| 194 tapeUI.tape.scrollByLines(100000); | |
| 195 }); | |
| 196 } | |
| 197 | |
| 198 void removeActiveElements() { | |
| 199 final element = new Element.tag('div'); | |
| 200 | |
| 201 DivElement active = activeInput; | |
| 202 if (active != null) { | |
| 203 element.innerHTML = tapeUI.replaceActiveOp(active.innerHTML); | |
| 204 active.replaceWith(element.elements[0]); | |
| 205 | |
| 206 active = activeTotal; | |
| 207 if (active != null) { | |
| 208 element.innerHTML = tapeUI.replaceActiveTotal(active.text); | |
| 209 active.replaceWith(element.elements[0]); | |
| 210 } | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 void clearTotal() { | |
| 215 final element = new Element.tag('div'); | |
| 216 | |
| 217 element.innerHTML = tapeUI.clearCalculation(); | |
| 218 tapeUI.tape.elements.add(element.elements[0]); | |
| 219 | |
| 220 scrollToTapeBottom(); | |
| 221 } | |
| 222 | |
| 223 bool get isClear() { | |
| 224 return tapeUI.tape.elements.last().classes.contains(TapeUI.clearCalc); | |
| 225 } | |
| 226 | |
| 227 void clearTape() { | |
| 228 tapeUI.tape.elements.clear(); | |
| 229 | |
| 230 SettingsDialog settingsUI = new SettingsDialog(); | |
| 231 tapeUI.tape.elements.add(settingsUI.root); | |
| 232 mySettings = new Settings(settingsUI, Settings.THEME_BUTTON); | |
| 233 } | |
| 234 | |
| 235 DivElement get activeInput() => window.document.query("#activeInput"); | |
|
Jacob
2012/04/05 18:06:01
window.document.query --> document.query
or bette
| |
| 236 DivElement get activeTotal() => window.document.query("#activeTotal"); | |
| 237 | |
| 238 void clear() { | |
| 239 final element = new Element.tag('div'); | |
| 240 element.innerHTML = tapeUI.clearCalculation(); | |
| 241 tapeUI.tape.elements.add(element.elements[0]); | |
|
Jacob
2012/04/05 18:06:01
i see the pattern of creating a div, setting some
| |
| 242 } | |
| 243 } | |
| OLD | NEW |