Merge lp:~rpadovani/ubuntu-calculator-app/moveJavascript141209 into lp:ubuntu-calculator-app

Proposed by Riccardo Padovani
Status: Merged
Merged at revision: 28
Proposed branch: lp:~rpadovani/ubuntu-calculator-app/moveJavascript141209
Merge into: lp:ubuntu-calculator-app
Diff against target: 234 lines (+104/-49)
3 files modified
app/engine/formula.js (+90/-0)
app/ubuntu-calculator-app.qml (+12/-48)
app/ui/Screen.qml (+2/-1)
To merge this branch: bzr merge lp:~rpadovani/ubuntu-calculator-app/moveJavascript141209
Reviewer Review Type Date Requested Status
Bartosz Kosiorek Approve
Review via email: mp+244101@code.launchpad.net

Commit message

Moved some functions to a separate javascript file to have a cleaner main.

Description of the change

Moved some functions to a separate javascript file to have a cleaner main.
Delete function is broken, it has been fixed in next branch: https://code.launchpad.net/~rpadovani/ubuntu-calculator-app/improveDeleteFunction141209

To post a comment you must log in.
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

Looks ok for me

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'app/engine/formula.js'
2--- app/engine/formula.js 1970-01-01 00:00:00 +0000
3+++ app/engine/formula.js 2014-12-09 09:31:58 +0000
4@@ -0,0 +1,90 @@
5+/*
6+ * Copyright (C) 2014 Canonical Ltd
7+ *
8+ * This file is part of Ubuntu Calculator App
9+ *
10+ * Ubuntu Calculator App is free software: you can redistribute it and/or modify
11+ * it under the terms of the GNU General Public License version 3 as
12+ * published by the Free Software Foundation.
13+ *
14+ * Ubuntu Calculator App is distributed in the hope that it will be useful,
15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+ * GNU General Public License for more details.
18+ *
19+ * You should have received a copy of the GNU General Public License
20+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
21+ */
22+
23+/**
24+ * Function which will delete last element in the formula
25+ * It could be literal, operator, const (eg. "pi") or function (eg. "sin(" )
26+ *
27+ * @param bool isLastCalculate: if in the textfield there is the result of a
28+ * calc this is true and the function delete all
29+ * @param string longFormula: the formula in the textfield
30+ * @return a string with the new formula
31+ */
32+function deleteLastFormulaElement(isLastCalculate, longFormula) {
33+ if (isLastCalculate === true) {
34+ return '';
35+ }
36+
37+ if (longFormula !== '') {
38+ var removeSize = 1;
39+ longFormula = longFormula.substring(0, longFormula.length - removeSize);
40+ }
41+
42+ return longFormula;
43+}
44+
45+/**
46+ * Function to check if a char is an operator
47+ *
48+ * @param char digit: the digit to check if is a valid operator
49+ * @return bool that indicates if the digit is a valid operator
50+ */
51+function isOperator(digit) {
52+ switch(digit) {
53+ case "+":
54+ case "-":
55+ case "*":
56+ case "/":
57+ case "%":
58+ case "^":
59+ return true;
60+ default:
61+ return false;
62+ }
63+}
64+
65+/**
66+ * Function to replace some chars in the visual textfield
67+ *
68+ * @param string engineFormulaToDisplay: the string where we have to replace chars
69+ * @return a string based on param with changes in chars
70+ */
71+function returnFormulaToDisplay(engineFormulaToDisplay) {
72+ var engineToVisualMap = {
73+ '-': '−',
74+ '/': '÷',
75+ '*': '×',
76+ '.': decimalPoint,
77+ 'NaN': i18n.tr("NaN"),
78+ 'Infinity': '∞'
79+ }
80+
81+ if (engineFormulaToDisplay !== undefined) {
82+ for (var engineElement in engineToVisualMap) {
83+ // FIXME: need to add 'g' flag, but "new RegExp(engineElement, 'g');"
84+ // is not working for me
85+ engineFormulaToDisplay = engineFormulaToDisplay.replace(
86+ engineElement, engineToVisualMap[engineElement]);
87+ }
88+ }
89+ else {
90+ engineFormulaToDisplay = '';
91+ }
92+
93+ return engineFormulaToDisplay;
94+}
95
96=== modified file 'app/ubuntu-calculator-app.qml'
97--- app/ubuntu-calculator-app.qml 2014-12-09 08:59:08 +0000
98+++ app/ubuntu-calculator-app.qml 2014-12-09 09:31:58 +0000
99@@ -22,6 +22,7 @@
100 import "ui"
101 import "engine"
102 import "engine/math.js" as MathJs
103+import "engine/formula.js" as Formula
104
105 MainView {
106 id: mainView
107@@ -64,47 +65,28 @@
108
109 property var decimalPoint: Qt.locale().decimalPoint
110
111- //Function which will delete last formula element.
112- // It could be literal, operator, const (eg. "pi") or function (eg. "sin(" )
113+ /**
114+ * The function calls the Formula.deleteLastFormulaElement function and
115+ * place the result in right vars
116+ */
117 function deleteLastFormulaElement() {
118- if (isLastCalculate === true) {
119- longFormula = '';
120+ if (longFormula[longFormula.length - 1] === ".") {
121+ isAllowedToAddDot = true;
122 }
123
124- if (longFormula !== '') {
125- var removeSize = 1;
126- if (longFormula[longFormula.length - 1] === ".") {
127- isAllowedToAddDot = true;
128- }
129- longFormula = longFormula.substring(0, longFormula.length - removeSize);
130- }
131+ longFormula = Formula.deleteLastFormulaElement(isLastCalculate, longFormula);
132 shortFormula = longFormula;
133 displayedInputText = returnFormulaToDisplay(longFormula);
134 }
135
136- // Check if the given digit is one of the valid operators or not.
137- function isOperator(digit) {
138- switch(digit) {
139- case "+":
140- case "-":
141- case "*":
142- case "/":
143- case "%":
144- case "^":
145- return true;
146- default:
147- return false;
148- }
149- }
150-
151 function validateStringForAddingToFormula(stringToAddToFormula) {
152- if (isOperator(stringToAddToFormula)) {
153+ if (Formula.isOperator(stringToAddToFormula)) {
154 if ((longFormula === '') && (stringToAddToFormula !== '-')) {
155 // Do not add operator at beginning
156 return false;
157 }
158
159- if ((isOperator(previousVisual) && previousVisual !== ")")) {
160+ if ((Formula.isOperator(previousVisual) && previousVisual !== ")")) {
161 // Not two operator one after otQt.locale().decimalPointher
162 return false;
163 }
164@@ -136,23 +118,6 @@
165 return true;
166 }
167
168- function returnFormulaToDisplay(engineFormulaToDisplay) {
169- var engineToVisualMap = {
170- '-': '−',
171- '/': '÷',
172- '*': '×',
173- '.': decimalPoint,
174- 'NaN': i18n.tr("NaN"),
175- 'Infinity': '∞'
176- }
177-
178- for (var engineElement in engineToVisualMap) {
179- //FIXME need to add 'g' flag, but "new RegExp(engineElement, 'g');" is not working for me
180- engineFormulaToDisplay = engineFormulaToDisplay.replace(engineElement, engineToVisualMap[engineElement]);
181- }
182- return engineFormulaToDisplay;
183- }
184-
185 function formulaPush(visual) {
186 // If the user press a number after the press of "=" we start a new
187 // formula, otherwise we continue with the old one
188@@ -173,7 +138,6 @@
189 // We save the value until next value is pushed
190 previousVisual = visual;
191
192-
193 // If we add an operator after an operator we know has priority,
194 // we display a temporary result instead the all operation
195 if (isNaN(visual) && (visual.toString() !== ".") && isFormulaIsValidToCalculate) {
196@@ -191,7 +155,7 @@
197 shortFormula += visual.toString();
198
199 try {
200- displayedInputText = returnFormulaToDisplay(shortFormula);
201+ displayedInputText = Formula.returnFormulaToDisplay(shortFormula);
202 } catch(exception) {
203 console.log("Error: " + exception.toString());
204 return;
205@@ -224,7 +188,7 @@
206 }
207
208 try {
209- displayedInputText = returnFormulaToDisplay(result)
210+ displayedInputText = Formula.returnFormulaToDisplay(result)
211 } catch(exception) {
212 console.log("Error: " + exception.toString());
213 return;
214
215=== modified file 'app/ui/Screen.qml'
216--- app/ui/Screen.qml 2014-12-08 13:30:24 +0000
217+++ app/ui/Screen.qml 2014-12-09 09:31:58 +0000
218@@ -19,6 +19,7 @@
219 import Ubuntu.Components 1.1
220
221 import "../upstreamcomponents"
222+import "../engine/formula.js" as Formula
223
224 ListItemWithActions {
225 id: root
226@@ -60,7 +61,7 @@
227 width: parent.width - equal.width - result.width
228 anchors.bottom: parent.bottom
229
230- text: returnFormulaToDisplay(model.contents.formula)
231+ text: Formula.returnFormulaToDisplay(model.contents.formula)
232 font.pixelSize: units.gu(3)
233 lineHeight: units.gu(1) + 1
234 lineHeightMode: Text.FixedHeight

Subscribers

People subscribed via source and target branches