This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marquee.js
114 lines (91 loc) · 1.86 KB
/
marquee.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function marquee(_w, _h) {
this.w=_w;
this.h=_h;
this.table=document.createElement('table');
this.callback_cell_content=null;
}
marquee.prototype={
set_callback_cell_content : function(_cb) {
this.callback_cell_content=_cb;
},
create : function(_container) {
let create_row=(_t) => {
let r=document.createElement('tr');
_t.appendChild(r);
return r;
};
let create_cell=(_r) => {
let c=document.createElement('td');
if(null!==this.callback_cell_content) {
this.callback_cell_content(c);
}
else {
c.innerHTML=' ';
}
_r.appendChild(c);
}
let y=0;
while(y < this.h) {
let x=0;
let r=create_row(this.table);
while(x < this.w) {
let c=create_cell(r);
++x;
}
++y;
}
_container.appendChild(this.table);
},
get_cell : function(_x, _y) {
if(_x < 0 || _y < 0 || _x >= this.w || _y >= this.h) {
throw new Error('Invalid coordinates');
}
return this.table.rows[_y].cells[_x];
},
apply_to_all : function(_f) {
let y=0;
while(y < this.h) {
let x=0;
while(x < this.w) {
_f(this.get_cell(x, y));
x++;
}
y++;
}
// this.table.rows.forEach((_row) => {
// _row.cells.forEach((_cell) => {
// _f(_cell);
// });
// });
},
apply_to_collection : function(_cells, _f) {
_cells.forEach( (_pair) => {
_f(this.get_cell(_pair.x, _pair.y));
});
},
make_pair : function(_x, _y) {
return {x:_x, y:_y};
},
collection_from_string : function(_str) {
let pieces=_str.split('\n');
if(pieces.length!=this.h) {
throw new Error('Bad string size');
}
let y=0;
let result=[];
pieces.forEach((_item) => {
if(_item.length!=this.w) {
throw new Error('Bad string length: at "'+_item+'"');
}
let x=0;
while(x < this.w) {
if(_item.charAt(x)!=' ') {
result.push(this.make_pair(x, y));
}
++x;
}
++y;
});
return result;
}
};