| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class Tape { | 5 class Tape { |
| 6 static const int OP_NOOP = 0; | 6 static const int OP_NOOP = 0; |
| 7 static const int OP_PLUS = 1; | 7 static const int OP_PLUS = 1; |
| 8 static const int OP_MINUS = 2; | 8 static const int OP_MINUS = 2; |
| 9 static const int OP_MULTI = 3; | 9 static const int OP_MULTI = 3; |
| 10 static const int OP_DIV = 4; | 10 static const int OP_DIV = 4; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 111 } |
| 112 if (op != OP_NOOP) { | 112 if (op != OP_NOOP) { |
| 113 currentOperator = op; | 113 currentOperator = op; |
| 114 } | 114 } |
| 115 | 115 |
| 116 final element = new Element.tag('div'); | 116 final element = new Element.tag('div'); |
| 117 | 117 |
| 118 DivElement active = activeInput; | 118 DivElement active = activeInput; |
| 119 if (op == OP_NOOP) { | 119 if (op == OP_NOOP) { |
| 120 if (active != null) { | 120 if (active != null) { |
| 121 String displayedOp = active.elements[0].text; | 121 String displayedOp = active.children[0].text; |
| 122 active.innerHTML = tapeUI.firstOp(displayedOp, number); | 122 active.innerHTML = tapeUI.firstOp(displayedOp, number); |
| 123 } else { | 123 } else { |
| 124 element.innerHTML = tapeUI.displayOpAndNumber(opAsStr, number); | 124 element.innerHTML = tapeUI.displayOpAndNumber(opAsStr, number); |
| 125 tapeUI.tape.elements.add(element.elements[0]); | 125 tapeUI.tape.children.add(element.children[0]); |
| 126 element.innerHTML = tapeUI.alignSubTotal();; | 126 element.innerHTML = tapeUI.alignSubTotal();; |
| 127 tapeUI.tape.elements.add(element.elements[0]); | 127 tapeUI.tape.children.add(element.children[0]); |
| 128 element.innerHTML = tapeUI.lineBreak(); | 128 element.innerHTML = tapeUI.lineBreak(); |
| 129 tapeUI.tape.elements.add(element.elements[0]); | 129 tapeUI.tape.children.add(element.children[0]); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Update running total if it exist. | 132 // Update running total if it exist. |
| 133 active = activeTotal; | 133 active = activeTotal; |
| 134 if (active != null) { | 134 if (active != null) { |
| 135 active.text = "= ${formatOutput(displayTotal)}"; | 135 active.text = "= ${formatOutput(displayTotal)}"; |
| 136 } | 136 } |
| 137 } else { | 137 } else { |
| 138 removeActiveElements(); | 138 removeActiveElements(); |
| 139 | 139 |
| 140 String leftSide; | 140 String leftSide; |
| 141 if (op == OP_EQUAL) { | 141 if (op == OP_EQUAL) { |
| 142 leftSide = tapeUI.displayEqual(); | 142 leftSide = tapeUI.displayEqual(); |
| 143 } else { | 143 } else { |
| 144 leftSide = tapeUI.displayOp(opAsStr); | 144 leftSide = tapeUI.displayOp(opAsStr); |
| 145 } | 145 } |
| 146 | 146 |
| 147 element.innerHTML = leftSide; | 147 element.innerHTML = leftSide; |
| 148 tapeUI.tape.elements.add(element.elements[0]); | 148 tapeUI.tape.children.add(element.children[0]); |
| 149 | 149 |
| 150 if (op == OP_EQUAL) { | 150 if (op == OP_EQUAL) { |
| 151 element.innerHTML = tapeUI.displayTotal(formatOutput(displayTotal)); | 151 element.innerHTML = tapeUI.displayTotal(formatOutput(displayTotal)); |
| 152 } else { | 152 } else { |
| 153 element.innerHTML = tapeUI.displayActiveTotal(); | 153 element.innerHTML = tapeUI.displayActiveTotal(); |
| 154 } | 154 } |
| 155 tapeUI.tape.elements.add(element.elements[0]); | 155 tapeUI.tape.children.add(element.children[0]); |
| 156 | 156 |
| 157 element.innerHTML = tapeUI.lineBreak(); | 157 element.innerHTML = tapeUI.lineBreak(); |
| 158 tapeUI.tape.elements.add(element.elements[0]); | 158 tapeUI.tape.children.add(element.children[0]); |
| 159 } | 159 } |
| 160 | 160 |
| 161 scrollToTapeBottom(); | 161 scrollToTapeBottom(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 String formatOutput(double displayTotal) { | 164 String formatOutput(double displayTotal) { |
| 165 String formattedNum = "${displayTotal.toStringAsFixed(5)}"; | 165 String formattedNum = "${displayTotal.toStringAsFixed(5)}"; |
| 166 int dotIdx = formattedNum.indexOf("."); | 166 int dotIdx = formattedNum.indexOf("."); |
| 167 if (dotIdx >= 0) { | 167 if (dotIdx >= 0) { |
| 168 if (formattedNum.substring(dotIdx) == ".00000") { | 168 if (formattedNum.substring(dotIdx) == ".00000") { |
| 169 formattedNum = formattedNum.substring(0, dotIdx); | 169 formattedNum = formattedNum.substring(0, dotIdx); |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 return formattedNum; | 173 return formattedNum; |
| 174 } | 174 } |
| 175 | 175 |
| 176 void displayError(String err) { | 176 void displayError(String err) { |
| 177 removeActiveElements(); | 177 removeActiveElements(); |
| 178 DivElement element = new Element.tag("div"); | 178 DivElement element = new Element.tag("div"); |
| 179 element.innerHTML = tapeUI.displayError(err); | 179 element.innerHTML = tapeUI.displayError(err); |
| 180 tapeUI.tape.elements.add(element.elements[0]); | 180 tapeUI.tape.children.add(element.children[0]); |
| 181 | 181 |
| 182 scrollToTapeBottom(); | 182 scrollToTapeBottom(); |
| 183 | 183 |
| 184 currentOperator = null; | 184 currentOperator = null; |
| 185 currentRegister = ""; | 185 currentRegister = ""; |
| 186 total = 0.0; | 186 total = 0.0; |
| 187 } | 187 } |
| 188 | 188 |
| 189 void scrollToTapeBottom() { | 189 void scrollToTapeBottom() { |
| 190 window.requestLayoutFrame(() { | 190 window.requestLayoutFrame(() { |
| 191 // theTape.rect.scroll.top = rect.scroll.height; | 191 // theTape.rect.scroll.top = rect.scroll.height; |
| 192 // TODO(terry): Would like to set scrollTop of tape doesn't seem to work | 192 // TODO(terry): Would like to set scrollTop of tape doesn't seem to work |
| 193 // so I scroll by max lines. | 193 // so I scroll by max lines. |
| 194 tapeUI.tape.scrollByLines(100000); | 194 tapeUI.tape.scrollByLines(100000); |
| 195 }); | 195 }); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void removeActiveElements() { | 198 void removeActiveElements() { |
| 199 final element = new Element.tag('div'); | 199 final element = new Element.tag('div'); |
| 200 | 200 |
| 201 DivElement active = activeInput; | 201 DivElement active = activeInput; |
| 202 if (active != null) { | 202 if (active != null) { |
| 203 element.innerHTML = tapeUI.replaceActiveOp(active.innerHTML); | 203 element.innerHTML = tapeUI.replaceActiveOp(active.innerHTML); |
| 204 active.replaceWith(element.elements[0]); | 204 active.replaceWith(element.children[0]); |
| 205 | 205 |
| 206 active = activeTotal; | 206 active = activeTotal; |
| 207 if (active != null) { | 207 if (active != null) { |
| 208 element.innerHTML = tapeUI.replaceActiveTotal(active.text); | 208 element.innerHTML = tapeUI.replaceActiveTotal(active.text); |
| 209 active.replaceWith(element.elements[0]); | 209 active.replaceWith(element.children[0]); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 void clearTotal() { | 214 void clearTotal() { |
| 215 final element = new Element.tag('div'); | 215 final element = new Element.tag('div'); |
| 216 | 216 |
| 217 element.innerHTML = tapeUI.clearCalculation(); | 217 element.innerHTML = tapeUI.clearCalculation(); |
| 218 tapeUI.tape.elements.add(element.elements[0]); | 218 tapeUI.tape.children.add(element.children[0]); |
| 219 | 219 |
| 220 scrollToTapeBottom(); | 220 scrollToTapeBottom(); |
| 221 } | 221 } |
| 222 | 222 |
| 223 bool get isClear { | 223 bool get isClear { |
| 224 return tapeUI.tape.elements.last.classes.contains(TapeUI.clearCalc); | 224 return tapeUI.tape.children.last.classes.contains(TapeUI.clearCalc); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void clearTape() { | 227 void clearTape() { |
| 228 tapeUI.tape.elements.clear(); | 228 tapeUI.tape.children.clear(); |
| 229 | 229 |
| 230 SettingsDialog settingsUI = new SettingsDialog(); | 230 SettingsDialog settingsUI = new SettingsDialog(); |
| 231 tapeUI.tape.elements.add(settingsUI.root); | 231 tapeUI.tape.children.add(settingsUI.root); |
| 232 mySettings = new Settings(settingsUI, Settings.THEME_BUTTON); | 232 mySettings = new Settings(settingsUI, Settings.THEME_BUTTON); |
| 233 } | 233 } |
| 234 | 234 |
| 235 DivElement get activeInput => window.document.query("#activeInput"); | 235 DivElement get activeInput => window.document.query("#activeInput"); |
| 236 DivElement get activeTotal => window.document.query("#activeTotal"); | 236 DivElement get activeTotal => window.document.query("#activeTotal"); |
| 237 | 237 |
| 238 void clear() { | 238 void clear() { |
| 239 final element = new Element.tag('div'); | 239 final element = new Element.tag('div'); |
| 240 element.innerHTML = tapeUI.clearCalculation(); | 240 element.innerHTML = tapeUI.clearCalculation(); |
| 241 tapeUI.tape.elements.add(element.elements[0]); | 241 tapeUI.tape.children.add(element.children[0]); |
| 242 } | 242 } |
| 243 } | 243 } |
| OLD | NEW |