summaryrefslogtreecommitdiff
path: root/js/matrix.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-11-21 13:33:20 -0500
committerJules Laplace <jules@okfoc.us>2014-11-21 13:33:20 -0500
commitfea42b1513e321b6c397c914327a9a4a7d96e331 (patch)
tree0c52449d5e53fc60c30d06f75a0e7b6f9a3bd608 /js/matrix.js
parent9546cd5705e723e4c39ad4b34f0e154b8ab5e904 (diff)
split up js and style a little
Diffstat (limited to 'js/matrix.js')
-rw-r--r--js/matrix.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/js/matrix.js b/js/matrix.js
new file mode 100644
index 0000000..ff734bf
--- /dev/null
+++ b/js/matrix.js
@@ -0,0 +1,90 @@
+function Matrix (w,h,f){
+ this.w = w
+ this.h = h
+ this.f = f
+ this.initialize()
+}
+Matrix.prototype.initialize = function(){
+ var w = this.w, h = this.h, f = this.f
+ var aa = new Array (h)
+ for (var i = 0; i < h; i++) {
+ aa[i] = new Array (w)
+ for (var j = 0; j < w; j++) {
+ aa[i][j] = f(i,j)
+ }
+ }
+ this.aa = aa
+}
+Matrix.prototype.rebuild = function (){
+ this.demolish()
+ this.initialize()
+ this.append()
+ this.generate && this.generate()
+}
+Matrix.prototype.demolish = function (){
+ this.forEach(function(lex){
+ lex.demolish()
+ })
+ while (this.rapper.firstChild) {
+ this.rapper.removeChild(this.rapper.firstChild);
+ }
+ this.aa.forEach(function(row){
+ row.length = 0
+ })
+ this.aa.length = 0
+}
+Matrix.prototype.forEach = function(f){
+ this.aa.forEach(function(row, y){
+ row.forEach(function(lex, x){
+ f(lex, x, y)
+ })
+ })
+}
+Matrix.prototype.focusLex = function(y,x){
+ this.aa[mod(x,this.h)][mod(y,this.w)].focus()
+}
+Matrix.prototype.clear = function(){
+ this.forEach(function(lex,x,y){ lex.clear() })
+}
+Matrix.prototype.fill = function(fg, bg){
+ this.fg = fg
+ this.bg = bg
+ this.forEach(function(lex,x,y){
+ lex.fg = fg
+ lex.bg = bg
+ lex.build()
+ })
+}
+Matrix.prototype.build = function(){
+ this.forEach(function(lex,x,y){
+ lex.build()
+ })
+}
+Matrix.prototype.append = function(rapper){
+ rapper = this.rapper = rapper || this.rapper
+ this.aa.forEach(function(row, y){
+ var div = document.createElement("div")
+ row.forEach(function(lex, x) {
+ div.appendChild(lex.span)
+ })
+ rapper.appendChild( div )
+ })
+}
+Matrix.prototype.ascii = function () {
+ var lines = this.aa.map(function(row, y){
+ var last, line = ""
+ row.forEach(function(lex, x) {
+ if (lex.eq(last)) {
+ line += lex.char
+ }
+ else {
+ if (x > 0 && last && (last.bg != 1 || last.fg != 0)) line += "\\x03"
+ line += lex.irc()
+ last = lex
+ }
+ })
+ return line.replace(/\s+$/,"")
+ }).filter(function(line){ return line.length > 0 })
+ var txt = '/exec -out printf "' + lines.join("\\n") + '"\n'
+ return txt
+} \ No newline at end of file