UNPKG

neo-neo-bblessed

Version:

A fork of neo-blessed (which is a fork of blessed) with bug fixes and maintenance.

3 lines (2 loc) 2.45 kB
const C=require("./node"),g=require("./element");function n(t){if(!(this instanceof C))return new n(t);if(t=t||{},t.width==null&&t.left==null&&t.right==null||t.height==null&&t.top==null&&t.bottom==null)throw new Error("`Layout` must have a width and height!");t.layout=t.layout||"inline",g.call(this,t),t.renderer&&(this.renderer=t.renderer)}n.prototype.__proto__=g.prototype,n.prototype.type="layout",n.prototype.isRendered=function(t){return t.lpos?t.lpos.xl-t.lpos.xi>0&&t.lpos.yl-t.lpos.yi>0:!1},n.prototype.getLast=function(t){for(;this.children[--t];){const e=this.children[t];if(this.isRendered(e))return e}},n.prototype.getLastCoords=function(t){const e=this.getLast(t);if(e)return e.lpos},n.prototype._renderCoords=function(){const t=this._getCoords(!0),e=this.children;return this.children=[],this._render(),this.children=e,t},n.prototype.renderer=function(t){const e=this,o=t.xl-t.xi,l=t.yl-t.yi,h=t.xi,x=t.yi;let y=0,s=0,b=0,L=0;return this.options.layout==="grid"&&(L=this.children.reduce(function(a,i){return a=Math.max(a,i.width),a},0)),function(i,f){i.shrink=!0;const u=e.getLast(f);if(u?(i.position.left=u.lpos.xl-h,e.options.layout==="grid"&&(i.position.left+=L-(u.lpos.xl-u.lpos.xi)),i.position.left+i.width<=o||(y+=e.children.slice(s,f).reduce(function(r,d){return e.isRendered(d)&&(r=Math.max(r,d.lpos.yl-d.lpos.yi)),r},0),b=s,s=f,i.position.left=0),i.position.top=y):(i.position.left=0,i.position.top=0),e.options.layout==="inline"){let r=null,d=1/0;for(let p=b;p<s;p++){const c=e.children[p];if(!e.isRendered(c))continue;const m=Math.abs(i.position.left-(c.lpos.xi-h));m<d&&(r=c,d=m)}r&&(i.position.top=r.lpos.yl-x)}i.position.top+i.height>l}},n.prototype.render=function(){this._emit("prerender");const t=this._renderCoords();if(!t){delete this.lpos;return}if(t.xl-t.xi<=0){t.xl=Math.max(t.xl,t.xi);return}if(t.yl-t.yi<=0){t.yl=Math.max(t.yl,t.yi);return}this.lpos=t,this.border&&(t.xi++,t.xl--,t.yi++,t.yl--),this.tpadding&&(t.xi+=this.padding.left,t.xl-=this.padding.right,t.yi+=this.padding.top,t.yl-=this.padding.bottom);const e=this.renderer(t);return this.border&&(t.xi--,t.xl++,t.yi--,t.yl++),this.tpadding&&(t.xi-=this.padding.left,t.xl+=this.padding.right,t.yi-=this.padding.top,t.yl+=this.padding.bottom),this.children.forEach(function(o,l){if(o.screen._ci!==-1&&(o.index=o.screen._ci++),e(o,l)===!1){delete o.lpos;return}o.render()}),this._emit("render",[t]),t},module.exports=n; //# sourceMappingURL=layout.js.map