Implemented support for hex and dec commands in cmd

This commit is contained in:
Borys Levytskyi
2015-04-11 19:49:53 +03:00
parent dbca2e03b4
commit b1836272cc
4 changed files with 21 additions and 12 deletions

View File

@@ -132,17 +132,17 @@
<table class="expression"> <table class="expression">
<tr> <tr>
<td></td> <td></td>
<td class="label">{operand1}</td> <td class="label">{operand1Str}</td>
<td class="bin">{operand1Binary}</td> <td class="bin">{operand1Binary}</td>
</tr> </tr>
<tr> <tr>
<td>{sign}</td> <td>{sign}</td>
<td class="label">{operand2}</td> <td class="label">{operand2Str}</td>
<td class="bin">{operand2Binary}</td> <td class="bin">{operand2Binary}</td>
</tr> </tr>
<tr class="result"> <tr class="result">
<td>=</td> <td>=</td>
<td class="label">{result}</td> <td class="label">{resultStr}</td>
<td class="bin">{resultBinary}</td> <td class="bin">{resultBinary}</td>
</tr> </tr>
</table> </table>

View File

@@ -27,8 +27,6 @@ app.set("formatter", function() {
} }
}; };
function getBase(mode) { function getBase(mode) {
switch (mode){ switch (mode){
case 'bin': return 2; case 'bin': return 2;

View File

@@ -26,6 +26,12 @@ app.run(function() {
light: function () { light: function () {
cmdConfig.theme = 'light'; cmdConfig.theme = 'light';
}, },
dec: function () {
cmdConfig.mode = 'dec';
},
hex: function() {
cmdConfig.mode = 'hex';
},
about: function() { about: function() {
var aboutResult = document.querySelector('.result .aboutTpl'); var aboutResult = document.querySelector('.result .aboutTpl');
if(aboutResult != null) { if(aboutResult != null) {
@@ -38,9 +44,9 @@ app.run(function() {
// TODO: Make as function // TODO: Make as function
cmd.command({ cmd.command({
canHandle: function(input) { return app.get('expression').canParse(input); }, canHandle: function(input) { return app.get('expression').canParse(input, cmdConfig.mode); },
handle: function(input) { handle: function(input) {
return app.get('expression').parse(input); return app.get('expression').parse(input, cmdConfig.mode);
} }
}); });

View File

@@ -13,10 +13,15 @@ app.compose(function () {
var maxLen = getBinaryLength([expr.operand1, expr.operand2, result]); var maxLen = getBinaryLength([expr.operand1, expr.operand2, result]);
var model = Object.create(expr); var model = Object.create(expr);
model.result = result;
model.resultStr = formatter.formatString(result, cmdConfig.mode);
model.operand1Str = formatter.formatString(expr.operand1, cmdConfig.mode);
model.operand2Str = formatter.formatString(expr.operand2, cmdConfig.mode);
model.operand1Binary = formatter.padLeft(formatter.formatString(expr.operand1), maxLen); model.operand1Binary = formatter.padLeft(formatter.formatString(expr.operand1), maxLen);
model.operand2Binary = formatter.padLeft(formatter.formatString(expr.operand2), maxLen); model.operand2Binary = formatter.padLeft(formatter.formatString(expr.operand2), maxLen);
model.resultBinary = formatter.padLeft(formatter.formatString(result), maxLen); model.resultBinary = formatter.padLeft(formatter.formatString(result, cmdConfig.mode), maxLen);
console.log(model);
var templateId = /<<|>>/.test(model.sign) ? 'shiftExpressionView' : 'binaryExpressionView'; var templateId = /<<|>>/.test(model.sign) ? 'shiftExpressionView' : 'binaryExpressionView';
var template = app.template(templateId) var template = app.template(templateId)
@@ -32,7 +37,7 @@ app.compose(function () {
var maxLen = getBinaryLength(model.numbers); var maxLen = getBinaryLength(model.numbers);
var table = html.element('<table class="expression"></table>'); var table = html.element('<table class="expression"></table>');
model.numbers.forEach(function(o){ model.numbers.forEach(function(n){
var row = table.insertRow(); var row = table.insertRow();
var decCell = row.insertCell(); var decCell = row.insertCell();
@@ -42,8 +47,8 @@ app.compose(function () {
var binCell = row.insertCell(); var binCell = row.insertCell();
binCell.className = 'bin'; binCell.className = 'bin';
decCell.textContent = o; decCell.textContent = formatter.formatString(n, cmdConfig.mode);
binCell.textContent = formatter.formatString(o, maxLen); binCell.textContent = formatter.padLeft(formatter.formatString(n), maxLen);
}); });
colorizeBits(table); colorizeBits(table);