diff --git a/app/bitwise/expression.js b/app/bitwise/expression.js index 2339cc8..87bad8e 100644 --- a/app/bitwise/expression.js +++ b/app/bitwise/expression.js @@ -30,7 +30,7 @@ var m = new app.models.BitwiseOperation(); m.operand1 = o1; m.operand2 = o2; - m.sing = matches[2]; + m.sign = matches[2]; m.string = matches.input; m.result = eval(matches.input); diff --git a/app/services.js b/app/services.js index 1800eb0..8011681 100644 --- a/app/services.js +++ b/app/services.js @@ -1,14 +1,6 @@ (function(app, HtmlBuilder){ - app.component('html', { - builder: function () { - return new HtmlBuilder(); - }, - - element: function(template, model) { - return HtmlBuilder.createElement(template, model); - } - }); + app.component('html', HtmlBuilder); /* var template = { compile: function (template) { diff --git a/app/views.js b/app/views.js index 0e34ec3..659a73f 100644 --- a/app/views.js +++ b/app/views.js @@ -4,59 +4,50 @@ var formatter = app.component('formatter'); var calc = app.component('calc'); + app.modelView(app.models.BitwiseOperation, { $html:null, - renderView: function(model) { - return renderCalculableExpression(model, this.$html.builder()); + $calc:null, + renderView: function(expr) { + var maxLen = this.$calc.maxNumberOfBits([expr.operand1, expr.operand2, expr.result]); + var $html = app.component('html'); + + expr.operand1Binary = formatter.toBinaryString(expr.operand1, maxLen); + expr.operand2Binary = formatter.toBinaryString(expr.operand2, maxLen); + expr.resultBinary = formatter.toBinaryString(expr.result, maxLen); + + var templateId = /<<|>>/.test(expr.sign) ? 'shiftExpressionView' : 'binaryExpressionView'; + + var html = document.getElementById(templateId).innerHTML; + return $html.element(html, expr); } }); app.modelView(app.models.BitwiseNumbers, { $html:null, + $calc:null, renderView: function(model) { - return renderListOfNumbers(model.numbers, this.$html.builder()); + var maxLen = this.$calc.maxNumberOfBits(model.numbers); + var table = this.$html.element('
'); + + model.numbers.forEach(function(o){ + + var row = table.insertRow(); + var decCell = row.insertCell(); + + decCell.className = 'label'; + + var binCell = row.insertCell(); + binCell.className = 'bin'; + + decCell.innerText = o; + binCell.innerText = formatter.toBinaryString(o, maxLen); + }); + + return table; } }); - function renderCalculableExpression(expr, hb) { - var maxLen = calc.maxNumberOfBits([expr.operand1, expr.operand2, expr.result]); - - hb.element('table', { class: "expression", cellspacing: "0"}, function () { - buildRow(hb, expr.operand1, formatter.toBinaryString(expr.operand1, maxLen)); - buildRow(hb, expr.operand2, formatter.toBinaryString(expr.operand2, maxLen)); - buildRow(hb, expr.result, formatter.toBinaryString(expr.result, maxLen), { class: 'result'}); - }); - - return hb.toHtmlElement(); - } - - function renderListOfNumbers(numbers, hb) { - var maxLen = calc.maxNumberOfBits(numbers); - - hb.element('table', { class: "expression", cellspacing: "0"}, function () { - numbers.forEach(function(o){ - buildRow(hb, o, formatter.toBinaryString(o, maxLen)); - }); - }); - - return hb.toHtmlElement(); - } - - function buildRow(hb, label, binaryStr, attrs) { - hb.element('tr', attrs, function() { - hb.element('td', { class: "label"}, label); - appendBinaryColumns(hb, binaryStr); - }); - } - - function appendBinaryColumns(hb, binaryStr) { - var css; - for(var i=0;i"); - - if(typeof elementContent == 'function') { - elementContent(); - } else if (elementContent != null) { - this.sb.push(elementContent.toString()); - } - - this.sb.push(''); + HtmlBuilder.element = function(template, model) { + var el = document.createElement('div'); + el.innerHTML = HtmlBuilder.template(template, model); + return el.children[0]; }; - HtmlBuilder.prototype.toString = function () { - return this.sb.join('\r\n'); - }; - - HtmlBuilder.prototype.toHtmlElement = function (){ - return HtmlBuilder.createElement(this.toString()); - }; - - HtmlBuilder.createElement = function(template, model) { - - should.beString(template, "template") - + HtmlBuilder.template = function(template, model) { + should.beString(template, "template"); var regex = /(?:{([^}]+)})/g, html; if(model == null){ @@ -52,9 +20,7 @@ }); } - var el = document.createElement('div'); - el.innerHTML = html; - return el.children[0]; + return html; }; function getAttributesStr(attr) { @@ -72,14 +38,16 @@ return str.join(' '); } - HtmlBuilder.escapeHtml = function(html) { - if(html == null) { - return html; + HtmlBuilder.escapeHtml = function(obj) { + if(obj == null) { + return obj; } - should.beString(html); + if(typeof obj != 'string') { + obj = obj.toString(); + } - return html + return obj .replace(/&/g, "&") .replace(//g, ">") diff --git a/css/styles.css b/css/styles.css index 2a8bbdf..30b5486 100644 --- a/css/styles.css +++ b/css/styles.css @@ -6,16 +6,15 @@ code { font-size: 1.2em; font-weight: bold; } .expressionInput { width: 500px; padding: 3px; border: solid 1px lightgray; } .result { margin: 10px 10px 20px; } -.result .input { font-style: italic; margin-bottom: 10px; -} +.result .input { font-style: italic; margin-bottom: 10px; } .result .content { padding-left: 10px} .result .cur { color: lightgray; margin-right: 5px } .expression .label { font-weight: bold; padding-right: 5px } +.expression .bin { letter-spacing: 3px; } .expression .one { color: #6d9ad3 } .expression .zero { color: #70d351 } .expression .result td { border-top: dotted 1px gray; } -.expression td { padding: 3px; } .expression { font-size: 1.5em; font-family: monospace } .help { padding: 10px; background: #e0e0e0; } diff --git a/index.html b/index.html index c41f6b3..359de77 100644 --- a/index.html +++ b/index.html @@ -40,12 +40,28 @@ + + + + + +