From d6e6f396887e950cfaf5373e12555568f48654ae Mon Sep 17 00:00:00 2001 From: Borys Levytskyi Date: Fri, 3 Apr 2015 23:02:10 +0300 Subject: [PATCH] Implemented ability to emphasize bytes --- app/controllers.js | 10 ++++++++++ app/views.js | 25 +++++++++++++++++++++++-- components/viewsFeature.js | 1 + css/styles.css | 3 ++- index.html | 11 ++++++++--- 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/controllers.js b/app/controllers.js index d3754c2..fd7f94f 100644 --- a/app/controllers.js +++ b/app/controllers.js @@ -74,4 +74,14 @@ app.component('resultView', resultViewController); app.controller('resultViewCtrl', resultViewController); + app.controller('configPanelCtrl', { + onViewAttached: function (){ + var chk = this.viewElement.querySelector('#displayBytes'); + chk.checked = app.emphasizeBytes; + chk.addEventListener('change', function(evt){ + app.emphasizeBytes = evt.srcElement.checked === true; + }) + } + }); + })(window.app, window.is); diff --git a/app/views.js b/app/views.js index 145020b..9363b3f 100644 --- a/app/views.js +++ b/app/views.js @@ -9,7 +9,7 @@ $html:null, $calc:null, renderView: function(expr) { - var maxLen = this.$calc.maxNumberOfBits([expr.operand1, expr.operand2, expr.result]); + var maxLen = getBinaryLength([expr.operand1, expr.operand2, expr.result]); var $html = app.component('html'); expr.operand1Binary = formatter.toBinaryString(expr.operand1, maxLen); @@ -29,9 +29,15 @@ $html:null, $calc:null, renderView: function(model) { - var maxLen = this.$calc.maxNumberOfBits(model.numbers); + var maxLen = getBinaryLength(model.numbers); var table = this.$html.element('
'); + if(app.emphasizeBytes) { + if(maxLen % 8 != 0) { + maxLen += Math.floor(maxLen / 8) + 8; + } + } + model.numbers.forEach(function(o){ var row = table.insertRow(); @@ -77,11 +83,26 @@ } }); + function getBinaryLength(arr) { + var bits = calc.maxNumberOfBits(arr); + if(app.emphasizeBytes && bits % 8 != 0) { + if(bits < 8) { + return 8; + } + + var n = bits - (bits % 8); + return n + 8; + } + return bits; + } + function colorizeBits(container) { var list = container.querySelectorAll('.bin'); Array.prototype.forEach.call(list, function(el){ var bin = el.innerText; + el.innerHTML = bin + .replace(/(\d{8})/g, '$1') .replace(/0/g, '0') .replace(/1/g, '1'); }); diff --git a/components/viewsFeature.js b/components/viewsFeature.js index 2f8603e..c086dbb 100644 --- a/components/viewsFeature.js +++ b/components/viewsFeature.js @@ -18,4 +18,5 @@ var str = func.toString(); return str.substr(8, str.indexOf('(') - 8).trim(); } + })(window.app, window.is); diff --git a/css/styles.css b/css/styles.css index 8e49eff..57b79cf 100644 --- a/css/styles.css +++ b/css/styles.css @@ -5,13 +5,14 @@ code { font-size: 1.2em; font-weight: bold; } .expressionInput { width: 500px; padding: 3px; border: solid 1px lightgray; } -.result { margin: 10px 10px 20px; } +.result { margin: 10px 10px 30px; } .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; text-align: right; } .expression .bin { letter-spacing: 3px; } +.expression .byte { margin: 0 3px; } .expression .one { color: black } .expression .zero { color: #848586 } .expression .result td { border-top: dotted 1px gray; } diff --git a/index.html b/index.html index 1eb2d77..4e01309 100644 --- a/index.html +++ b/index.html @@ -34,6 +34,12 @@ +
+ +
@@ -128,14 +134,13 @@ } }); + app.emphasizeBytes = true; // TODO: Make into model + app.bootstrap(document.getElementById('rootView')); app.debugMode = true; dispatcher.dispatch('help'); - dispatcher.dispatch('1 2 3 4 5 6 7 8'); - dispatcher.dispatch('3 << 4'); - dispatcher.dispatch('233 | 322'); })();