function Matrix (w,h,f){ this.x = 0 this.y = 0 this.w = w this.h = h this.f = f this.initialize() } Matrix.prototype.initialize = function(f){ var w = this.w || 1, h = this.h || 1, f = f || this.f var aa = new Array (h) for (var y = 0; y < h; y++) { aa[y] = new Array (w) for (var x = 0; x < w; x++) { aa[y][x] = f(x,y) } } this.aa = aa } Matrix.prototype.rebuild = function (){ this.demolish() this.initialize() this.append() this.bind() this.generate && this.generate() check_if_lost_focus() } Matrix.prototype.clone = function () { var base = this var clone = new Matrix(this.w, this.h, function(x,y){ return base.getCell(x,y).clone() }) clone.f = this.f return clone } Matrix.prototype.assign = function (mat) { var base = this this.demolish() this.w = mat.w this.h = mat.h // this.f = function(){} this.initialize(function(x,y){ var el = mat.getCell(x,y).clone() el.build() return el }) this.append() this.bind() check_if_lost_focus() return this } Matrix.prototype.bind = function () {} 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){ if (x < 0) { y -= 1 } if (x > this.aa[0].length) { y += 1 } this.aa[mod(y,this.h)][mod(x,this.w)].focus() } Matrix.prototype.clear = function(){ this.forEach(function(lex,x,y){ lex.clear() }) } Matrix.prototype.erase = function(){ this.forEach(function(lex,x,y){ lex.erase() }) } Matrix.prototype.fill = function(fg, bg){ this.fg = fg this.bg = bg this.forEach(function(lex,x,y){ lex.fill(fg,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.region = function(w,h,x,y) { w = w || 1 h = h || 1 x = x || 0 y = y || 0 var parent = this var mat = new Matrix(w, h, function(x,y){ return parent.aa[y][x] }) mat.f = this.f return mat } Matrix.prototype.setCell = function(lex,x,y){ this.aa[y] && this.aa[y][x] && this.aa[y][x].assign(lex) } Matrix.prototype.getCell = function(x,y){ if (this.aa[y] && this.aa[y][x]) return this.aa[y][x] else return null } Matrix.prototype.resize = function(w,h){ var div, row, lex var f = this.f, old_h = this.h, old_w = this.w var rapper = this.rapper w = max(w, 1) h = max(h, 1) if (h < old_h) { for (var y = old_h; y > h; y--) { row = this.aa.pop() div = row[0].span.parentNode row.forEach(function(lex, x){ lex.demolish() }) div.parentNode.removeChild(div) } } else if (h > old_h) { for (var y = old_h; y < h; y++) { div = document.createElement("div") rapper.appendChild( div ) this.aa[y] = new Array (w) for (var x = 0; x < w; x++) { lex = this.aa[y][x] = f(x,y) div.appendChild(lex.span) } } } if (w < old_w) { this.aa.forEach(function(row, y){ while (row.length > w) { lex = row.pop() lex.demolish() } }) } else if (w > old_w) { this.aa.forEach(function(row, y){ div = row[0].span.parentNode for (var x = row.length; x < w; x++) { lex = row[x] = f(x,y) div.appendChild(lex.span) } }) } this.w = w this.h = h this.bind && this.bind() } // Matrix.prototype.ascii = function () { var lines = this.aa.map(function(row, y){ var last, line = "" row.forEach(function(lex, x) { line += lex.ascii() }) return line // .replace(/\s+$/,"") }) var txt = lines.join("\n") return txt } Matrix.prototype.mirc = function () { var lines = this.aa.map(function(row, y){ var last, line = "" row.forEach(function(lex, x) { if (lex.eqColor(last)) { line += lex.sanitize() } else { // if (x > 0 && last) line += "\x03" line += lex.mirc() last = lex } }) // if (last && ! last.isClear()) { line += "\x03" } return line.substr(0,400) }).filter(function(line){ return line.length > 0 }) return lines.join("\n") } Matrix.prototype.irssi = function(){ var txt = this.mirc() .replace(/\%/g, '%%') .replace(/\\/g, '\\x5C') .replace(/\"/g, '\\\"') .replace(/\'/g, '\\\'') .replace(/\`/g, '\\\`') .replace(/\$/g, '\\$') // .replace(/\n\s+/g, '\n') // .replace(/\s+$/g, '\n') // .replace(/^\n+/, '') .replace(/\n/g, '\\n') .replace(/\x02/g, '\\x02') .replace(/\x03/g, '\\x03') // console.log(txt.length) var escaped_txt = "", kode for (var i = 0; i < txt.length; i++) { kode = txt.charCodeAt(i) if (kode > 0x7f) { kode = kode.toString(16) switch (kode.length) { case 2: kode = "0" + kode case 3: kode = "0" + kode } escaped_txt += "\\u" + kode } else { escaped_txt += txt[i] } } return '/exec -out printf "' + escaped_txt + '"\n' }