Removed ugly html building logic.

This commit is contained in:
Borys Levytskyi
2015-04-03 21:42:16 +03:00
parent 85f405dcf3
commit edf72d8095
6 changed files with 111 additions and 109 deletions

View File

@@ -30,7 +30,7 @@
var m = new app.models.BitwiseOperation(); var m = new app.models.BitwiseOperation();
m.operand1 = o1; m.operand1 = o1;
m.operand2 = o2; m.operand2 = o2;
m.sing = matches[2]; m.sign = matches[2];
m.string = matches.input; m.string = matches.input;
m.result = eval(matches.input); m.result = eval(matches.input);

View File

@@ -1,14 +1,6 @@
(function(app, HtmlBuilder){ (function(app, HtmlBuilder){
app.component('html', { app.component('html', HtmlBuilder);
builder: function () {
return new HtmlBuilder();
},
element: function(template, model) {
return HtmlBuilder.createElement(template, model);
}
});
/* /*
var template = { var template = {
compile: function (template) { compile: function (template) {

View File

@@ -4,59 +4,50 @@
var formatter = app.component('formatter'); var formatter = app.component('formatter');
var calc = app.component('calc'); var calc = app.component('calc');
app.modelView(app.models.BitwiseOperation, { app.modelView(app.models.BitwiseOperation, {
$html:null, $html:null,
renderView: function(model) { $calc:null,
return renderCalculableExpression(model, this.$html.builder()); 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, { app.modelView(app.models.BitwiseNumbers, {
$html:null, $html:null,
$calc:null,
renderView: function(model) { renderView: function(model) {
return renderListOfNumbers(model.numbers, this.$html.builder()); var maxLen = this.$calc.maxNumberOfBits(model.numbers);
var table = this.$html.element('<table class="expression"></table>');
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<binaryStr.length;i++) {
css = binaryStr[i] == '1' ? 'one' : 'zero';
hb.element('td', { class: css }, binaryStr[i]);
}
}
app.modelView(app.models.HelpResult, { app.modelView(app.models.HelpResult, {
$html: null, $html: null,
renderView: function(model) { renderView: function(model) {

View File

@@ -1,47 +1,15 @@
(function(){ (function(){
function HtmlBuilder() { var HtmlBuilder = {};
this.sb = [];
}
HtmlBuilder.prototype.element = function(tagName, arg1, arg2) { HtmlBuilder.element = function(template, model) {
var attrs, elementContent; var el = document.createElement('div');
el.innerHTML = HtmlBuilder.template(template, model);
if(typeof arg1 == "object") { return el.children[0];
attrs = arg1;
}
else if(typeof arg1 == "string") {
attrs = { html: arg1 };
}
else {
attrs = {};
}
elementContent = attrs.html || arg2;
this.sb.push('<' + tagName + ' ' + getAttributesStr(attrs) + ">");
if(typeof elementContent == 'function') {
elementContent();
} else if (elementContent != null) {
this.sb.push(elementContent.toString());
}
this.sb.push('</' + tagName + '>');
}; };
HtmlBuilder.prototype.toString = function () { HtmlBuilder.template = function(template, model) {
return this.sb.join('\r\n'); should.beString(template, "template");
};
HtmlBuilder.prototype.toHtmlElement = function (){
return HtmlBuilder.createElement(this.toString());
};
HtmlBuilder.createElement = function(template, model) {
should.beString(template, "template")
var regex = /(?:{([^}]+)})/g, html; var regex = /(?:{([^}]+)})/g, html;
if(model == null){ if(model == null){
@@ -52,9 +20,7 @@
}); });
} }
var el = document.createElement('div'); return html;
el.innerHTML = html;
return el.children[0];
}; };
function getAttributesStr(attr) { function getAttributesStr(attr) {
@@ -72,14 +38,16 @@
return str.join(' '); return str.join(' ');
} }
HtmlBuilder.escapeHtml = function(html) { HtmlBuilder.escapeHtml = function(obj) {
if(html == null) { if(obj == null) {
return html; return obj;
} }
should.beString(html); if(typeof obj != 'string') {
obj = obj.toString();
}
return html return obj
.replace(/&/g, "&amp;") .replace(/&/g, "&amp;")
.replace(/</g, "&lt;") .replace(/</g, "&lt;")
.replace(/>/g, "&gt;") .replace(/>/g, "&gt;")

View File

@@ -6,16 +6,15 @@ code { font-size: 1.2em; font-weight: bold; }
.expressionInput { width: 500px; padding: 3px; border: solid 1px lightgray; } .expressionInput { width: 500px; padding: 3px; border: solid 1px lightgray; }
.result { margin: 10px 10px 20px; } .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 .content { padding-left: 10px}
.result .cur { color: lightgray; margin-right: 5px } .result .cur { color: lightgray; margin-right: 5px }
.expression .label { font-weight: bold; padding-right: 5px } .expression .label { font-weight: bold; padding-right: 5px }
.expression .bin { letter-spacing: 3px; }
.expression .one { color: #6d9ad3 } .expression .one { color: #6d9ad3 }
.expression .zero { color: #70d351 } .expression .zero { color: #70d351 }
.expression .result td { border-top: dotted 1px gray; } .expression .result td { border-top: dotted 1px gray; }
.expression td { padding: 3px; }
.expression { font-size: 1.5em; font-family: monospace } .expression { font-size: 1.5em; font-family: monospace }
.help { padding: 10px; background: #e0e0e0; } .help { padding: 10px; background: #e0e0e0; }

View File

@@ -40,12 +40,28 @@
<script id="helpView" type="text/template"> <script id="helpView" type="text/template">
<div class="help"> <div class="help">
<ul> <p class="section">
<li><code>23 ^ 34</code> type bitwise expression to see result in binary (only positive integers are supported now)</li> <strong>Supported Commands</strong>
<li><code>23 34</code> type one or more numbers to see their binary representations</li> <ul>
<li><code>clear</code> clear output pane</li> <li><code>23 ^ 34</code> type bitwise expression to see result in binary (only positive integers are supported now)</li>
<li><code>help</code> display this help</li> <li><code>23 34</code> type one or more numbers to see their binary representations</li>
</ul> <li><code>clear</code> clear output pane</li>
<li><code>help</code> display this help</li>
</ul>
</p>
<p class="section">
<strong>Supportd Bitwise Operations</strong>
<ul>
<li><code>&</code> bitwise AND</li>
<li><code>|</code> bitwise inclusive OR</li>
<li><code>^</code> bitwise exclusive XOR</li>
<li><code><<</code> left shift</li>
<li><code>>></code> right shift</li>
<li><code>~</code> bitwise NOT (one's complement) (unary)</li>
</ul>
</p>
<p style="font-style: italic"> <p style="font-style: italic">
<strong>Tip:</strong> Use Up and Down keys to navigate trough executed commands. <strong>Tip:</strong> Use Up and Down keys to navigate trough executed commands.
@@ -60,6 +76,41 @@
</div> </div>
</script> </script>
<script id="shiftExpressionView" type="text/template">
<table class="expression">
<tr>
<td class="label">{operand1}</td>
<td class="bin">{operand1Binary}</td>
</tr>
<tr>
<td class="label">{operand1}{sign}{operand2}</td>
<td class="bin">{resultBinary}</td>
</tr>
</table>
</script>
<script id="binaryExpressionView" type="text/template">
<table class="expression">
<tr>
<td></td>
<td class="label">{operand1}</td>
<td class="bin">{operand1Binary}</td>
</tr>
<tr>
<td>{sign}</td>
<td class="label">{operand2}</td>
<td class="bin">{operand2Binary}</td>
</tr>
<tr>
<td>=</td>
<td class="label">{result}</td>
<td class="bin">{resultBinary}</td>
</tr>
</table>
</script>
<script type="text/javascript"> <script type="text/javascript">
(function(){ (function(){
@@ -83,7 +134,8 @@
app.debugMode = true; app.debugMode = true;
dispatcher.dispatch('help'); dispatcher.dispatch('help');
dispatcher.dispatch('3 ^ 4'); dispatcher.dispatch('3 << 4');
dispatcher.dispatch('233 | 322');
})(); })();