Skip to content

Commit

Permalink
Add automatic inclusion of last answer when an operator is entered im…
Browse files Browse the repository at this point in the history
…mediately after a calculation
  • Loading branch information
imccausl committed Jan 20, 2017
1 parent 2bab083 commit 3f888f7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 7 additions & 1 deletion js/app/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,16 @@ define( [], function (expression) {
},

pushToModel = function pushToModel(ch) {
var operators = ['+', '-', '/', '*', '%', '^', '!'];

if (_model.content.firstInput === true) {
toggleFirstInput();
_model.content.data = "";

if ( (operators.indexOf(ch) > -1) && (_model.lastAns) ) {
_model.content.data = _model.lastAns; // if one of the acceptable operators is pressed, push lastAns to model.
} else {
_model.content.data = ""; // first input after equals, clear model for a clean pushToModel slate.
}
}

_model.content.data += ch;
Expand Down
16 changes: 11 additions & 5 deletions js/app/inputFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,27 @@ define( [], function() {

_getNextFilter = function _getNextFilter(ch, lastCh) {
var inputRules = [],
defaultFilter = ['numbers', 'pi', 'log', 'root', 'decimal', 'leftParen'],
ch = ch || "";


if ( ( ch ) !== "" ) {
if (ch === "") {
// no input rules found, so initialize the input filter with the following allowed inputs:
inputRules = defaultFilter;
} else if (ch==="postCalc") {
defaultFilter.push("multiDiv", "plus", "minus", "pow", "percent");
inputRules = defaultFilter;
} else {
for( var func in _lexer ) {

inputRules = _lexer[func](ch, lastCh);
if (inputRules) {
return _makeFilter(inputRules);
break;
}
}
} else {
// no input rules found, so initialize the input filter with the following allowed inputs:
return _makeFilter(['numbers', 'pi', 'log', 'root', 'decimal', 'leftParen']);
}

return _makeFilter(inputRules);
},

_addToFilter = function _addToFilter(item) {
Expand Down
4 changes: 3 additions & 1 deletion js/app/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ define(['expression', 'MathJax'], function(expression) {
},

swapBufferForScreen = function swapBufferForScreen() {
var historyData = document.getElementById('calc--history');

mjRunning = mjPending = false;

// Only swap the buffer if it contains rendered data
Expand All @@ -107,7 +109,7 @@ define(['expression', 'MathJax'], function(expression) {
elements.history.innerHTML = elements.historyBuffer.innerHTML;
}

elements.history.scrollLeft = elements.history.scrollWidth;
historyData.scrollLeft = historyData.scrollWidth;
elements.output.scrollLeft = elements.output.scrollWidth;


Expand Down
2 changes: 1 addition & 1 deletion js/app/viewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ define(['view', 'expression', 'inputFilter', 'math'], function(view, expression,
}

finally {
inputFilter.setFilter();
inputFilter.setFilter("postCalc");
view.elements.screen.textContent = model.content.data;
view.elements.history.textContent = expr
.replace(/\*/g, " ⋅ ")
Expand Down

0 comments on commit 3f888f7

Please sign in to comment.