text-mode
Version:
Character based display for the DOM
1 lines • 4.46 kB
JavaScript
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){window.TextMode=a("./text-mode")},{"./text-mode":4}],2:[function(a,b,c){function d(a,b){var c=document.createElement("span");document.body.appendChild(c),c.style.cssText="font: "+a,c.appendChild(document.createTextNode(b||"╬"));var d={width:c.offsetWidth,height:c.offsetHeight};return document.body.removeChild(c),d}b.exports=d},{}],3:[function(require,module,exports){module.exports=function(template){return eval("(function(o){return '"+template.replace(/'/g,"\\'").replace(/<%=\s*/g,"'+o.").replace(/\s*%>/g,"+'")+"'})")}},{}],4:[function(a,b,c){"use strict";function d(a,b){if(!(this instanceof d))return new d(a,b);var c,h=this;"string"==typeof a?c=document.querySelector(a):a instanceof HTMLElement?c=a:(c=document.body,!b&&a&&Object.keys(a).some(function(a){return a in d.defaults})&&(b=a)),h.viewport=document.createElement("div"),c.appendChild(h.viewport),b=b||d.defaults,Object.keys(d.defaults).forEach(function(a){h[a]=a in b?b[a]:d.defaults[a]}),h.id=g(),h.viewport.id="tm-"+h.id,h.sheet=e(),h.charSize=m(h.font),h.size={width:h.columns*h.charSize.width,height:h.rows*h.charSize.height},n[h.id]={tiles:[],textNodes:[],fore:[],back:[],className:[],classMap:{},nextIndex:0};var j=o.viewport(h),k=o.tiles(h.charSize);f(h.sheet,"#"+h.viewport.id,j),f(h.sheet,"#"+h.viewport.id+" span",k),i(h)}function e(){var a=document.createElement("style");return document.head.appendChild(a),a.sheet}function f(a,b,c){var d=o.style({selector:b,rule:c});a.insertRule(d,a.cssRules.length)}function g(a){for(var b=a||"",c=0;8>c;c++)b+=Math.floor(16*Math.random()).toString(16);return b}function h(a){return n[a].nextIndex++}function i(a){a.each(function(b,c,d){var e=document.createElement("span"),f=document.createTextNode("");e.appendChild(f),a.viewport.appendChild(e),n[a.id].tiles[d]=e,n[a.id].textNodes[d]=f,n[a.id].className[d]="",j(a,b,c,d),a.setColor(b,c,a.fore,a.back)})}function j(a,b,c,d){var e=n[a.id].tiles[d],f={left:b*a.charSize.width,top:c*a.charSize.height};e.style.cssText=o.tile(f)}function k(a,b){var c=n[a.id].fore[b],d=n[a.id].back[b],e=c+","+d;return n[a.id].classMap[e]}var l=a("./string-inject"),m=a("./char-size");d.prototype.set=function(a,b,c,d,e,f){var g=this;if(Array.isArray(a)){var h=a;return void g.each(function(a,b,c){g.set(a,b,h[c].text,h[c].fore,h[c].back,h[c].className)})}var i=b*g.columns+a;n[g.id].textNodes[i].nodeValue=c,(d||e)&&g.setColor(a,b,d,e),f&&g.setClassName(a,b,f)},d.prototype.setColor=function(a,b,c,d){var e=this,f=b*e.columns+a;if(c!==n[e.id].fore[f]||d!==n[e.id].back[f]){c=c||n[e.id].fore[f]||e.fore,d=d||n[e.id].back[f]||e.back,e.fore=n[e.id].fore[f]=c,e.back=n[e.id].back[f]=d;var g=c+","+d;g in n[e.id].classMap||e.addColor(c,d);var h=n[e.id].classMap[g];n[e.id].tiles[f].className=h+" "+n[e.id].className[f]}},d.prototype.setClassName=function(a,b,c){var d=this,e=b*d.columns+a;if(n[d.id].className[e]!==c){n[d.id].className[e]=c;var f=k(d,e);n[d.id].tiles[e].className=f+" "+c}},d.prototype.get=function(a,b){var c=this;if("number"!=typeof a){var d=[];return c.each(function(a,b,e){d[e]=c.get(a,b)}),d}var e=b*c.columns+a;return{fore:n[c.id].fore[e],back:n[c.id].back[e],text:n[c.id].textNodes[e].nodeValue,className:n[c.id].className[e]}},d.prototype.addColor=function(a,b){var c=this,d=a+","+b;if(!(d in n[c.id].classMap)){var e="color-"+c.id+"-"+h(c.id);n[c.id].classMap[d]=e,f(c.sheet,"."+e,o.color({fore:a,back:b}))}},d.prototype.each=function(a){for(var b=this,c=0;c<b.columns*b.rows;c++){var d=c%b.columns,e=Math.floor(c/b.columns);a(d,e,c)}},d.defaults={columns:80,rows:25,fore:"#ddd",back:"#222",font:"16px monospace"};var n={},o={style:l("<%=selector%> { <%=rule%> }"),color:l("background: <%=back%>; color: <%=fore%>;"),viewport:l("position: relative; width: <%=size.width%>px; height: <%=size.height%>px; font: <%=font%>"),tiles:l("position: absolute; line-height: <%=height%>px; width: <%=width%>px; height: <%=height%>px;"),tile:l("top: <%=top%>px; left: <%=left%>px;")};b.exports=d},{"./char-size":2,"./string-inject":3}]},{},[1]);