Add initial versions of StackHtmlView and TapeHtmlView.
Chris Pressey
10 years ago
|
0 |
/*
|
|
1 |
* This file is part of yoob.js version 0.10-PRE
|
|
2 |
* Available from https://github.com/catseye/yoob.js/
|
|
3 |
* This file is in the public domain. See http://unlicense.org/ for details.
|
|
4 |
*/
|
|
5 |
if (window.yoob === undefined) yoob = {};
|
|
6 |
|
|
7 |
/*
|
|
8 |
* A view (in the MVC sense) for depicting a yoob.Stack (-compatible)
|
|
9 |
* object onto any DOM element that supports innerHTML.
|
|
10 |
*/
|
|
11 |
yoob.StackHTMLView = function() {
|
|
12 |
this.init = function(cfg) {
|
|
13 |
this.stack = cfg.stack;
|
|
14 |
this.element = cfg.element;
|
|
15 |
return this;
|
|
16 |
};
|
|
17 |
|
|
18 |
/*** Chainable setters ***/
|
|
19 |
|
|
20 |
this.setStack = function(stack) {
|
|
21 |
this.stack = stack;
|
|
22 |
return this;
|
|
23 |
};
|
|
24 |
|
|
25 |
/*
|
|
26 |
* For compatibility with PlayfieldCanvasView. Sets the font size.
|
|
27 |
*/
|
|
28 |
this.setCellDimensions = function(cellWidth, cellHeight) {
|
|
29 |
this.element.style.fontSize = cellHeight + "px";
|
|
30 |
return this;
|
|
31 |
};
|
|
32 |
|
|
33 |
/*
|
|
34 |
* Convert Stack values to HTML. Override to customize appearance.
|
|
35 |
*/
|
|
36 |
this.render = function(value) {
|
|
37 |
if (value === undefined) return ' ';
|
|
38 |
return value;
|
|
39 |
};
|
|
40 |
|
|
41 |
/*
|
|
42 |
* Render the stack, as HTML, on the DOM element.
|
|
43 |
*/
|
|
44 |
this.draw = function() {
|
|
45 |
|
|
46 |
var text = "";
|
|
47 |
this.stack.foreach(function(pos, value) {
|
|
48 |
text += this.render(value) + "<br/>";
|
|
49 |
}
|
|
50 |
this.element.innerHTML = text;
|
|
51 |
};
|
|
52 |
};
|
|
0 |
/*
|
|
1 |
* This file is part of yoob.js version 0.10-PRE
|
|
2 |
* Available from https://github.com/catseye/yoob.js/
|
|
3 |
* This file is in the public domain. See http://unlicense.org/ for details.
|
|
4 |
*/
|
|
5 |
if (window.yoob === undefined) yoob = {};
|
|
6 |
|
|
7 |
/*
|
|
8 |
* A view (in the MVC sense) for depicting a yoob.Tape (-compatible)
|
|
9 |
* object onto any DOM element that supports innerHTML.
|
|
10 |
*/
|
|
11 |
yoob.TapeHTMLView = function() {
|
|
12 |
this.init = function(cfg) {
|
|
13 |
this.tape = cfg.tape;
|
|
14 |
this.element = cfg.element;
|
|
15 |
return this;
|
|
16 |
};
|
|
17 |
|
|
18 |
/*** Chainable setters ***/
|
|
19 |
|
|
20 |
this.setTape = function(tape) {
|
|
21 |
this.tape = tape;
|
|
22 |
return this;
|
|
23 |
};
|
|
24 |
|
|
25 |
/*
|
|
26 |
* For compatibility with PlayfieldCanvasView. Sets the font size.
|
|
27 |
*/
|
|
28 |
this.setCellDimensions = function(cellWidth, cellHeight) {
|
|
29 |
this.element.style.fontSize = cellHeight + "px";
|
|
30 |
return this;
|
|
31 |
};
|
|
32 |
|
|
33 |
/*
|
|
34 |
* Convert Tape values to HTML. Override to customize appearance.
|
|
35 |
*/
|
|
36 |
this.render = function(value) {
|
|
37 |
if (value === undefined) return ' ';
|
|
38 |
return value;
|
|
39 |
};
|
|
40 |
|
|
41 |
/*
|
|
42 |
* Render the Tape, as HTML, on the DOM element.
|
|
43 |
* TODO: make this not awful.
|
|
44 |
*/
|
|
45 |
this.draw = function() {
|
|
46 |
var text = "";
|
|
47 |
this.tape.foreach(function(pos, value) {
|
|
48 |
text += '' + pos + ': ' + this.render(value) + "<br/>";
|
|
49 |
}
|
|
50 |
this.element.innerHTML = text;
|
|
51 |
};
|
|
52 |
};
|