neo-neo-bblessed
Version:
A fork of neo-blessed (which is a fork of blessed) with bug fixes and maintenance.
3 lines (2 loc) • 3.86 kB
JavaScript
var s=require("../events").EventEmitter;function r(e){var t=this,n=require("./screen");if(!(this instanceof r))return new r(e);if(s.call(this),e=e||{},this.options=e,this.screen=this.screen||e.screen,!this.screen)if(this.type==="screen")this.screen=this;else if(n.total===1)this.screen=n.global;else if(e.parent)for(this.screen=e.parent;this.screen&&this.screen.type!=="screen";)this.screen=this.screen.parent;else if(n.total)this.screen=n.instances[n.instances.length-1],process.nextTick(function(){if(!t.parent)throw new Error("Element ("+t.type+") was not appended synchronously after the screen's creation. Please set a `parent` or `screen` option in the element's constructor if you are going to use multiple screens and append the element later.")});else throw new Error("No active screen.");this.parent=e.parent||null,this.children=[],this.$=this._=this.data={},this.uid=r.uid++,this.index=this.index!=null?this.index:-1,this.type!=="screen"&&(this.detached=!0),this.parent&&this.parent.append(this),(e.children||[]).forEach(this.append.bind(this))}r.uid=0,r.prototype.__proto__=s.prototype,r.prototype.type="node",r.prototype.insert=function(e,t){var n=this;if(e.screen&&e.screen!==this.screen)throw new Error("Cannot switch a node's screen.");e.detach(),e.parent=this,e.screen=this.screen,t===0?this.children.unshift(e):t===this.children.length?this.children.push(e):this.children.splice(t,0,e),e.emit("reparent",this),this.emit("adopt",e),function i(o){var a=o.detached!==n.detached;o.detached=n.detached,a&&o.emit("attach"),o.children.forEach(i)}(e),this.screen.focused||(this.screen.focused=e)},r.prototype.prepend=function(e){this.insert(e,0)},r.prototype.append=function(e){this.insert(e,this.children.length)},r.prototype.insertBefore=function(e,t){var n=this.children.indexOf(t);~n&&this.insert(e,n)},r.prototype.insertAfter=function(e,t){var n=this.children.indexOf(t);~n&&this.insert(e,n+1)},r.prototype.remove=function(e){if(e.parent===this){var t=this.children.indexOf(e);~t&&(e.clearPos(),e.parent=null,this.children.splice(t,1),t=this.screen.clickable.indexOf(e),~t&&this.screen.clickable.splice(t,1),t=this.screen.keyable.indexOf(e),~t&&this.screen.keyable.splice(t,1),e.emit("reparent",null),this.emit("remove",e),function n(i){var o=i.detached!==!0;i.detached=!0,o&&i.emit("detach"),i.children.forEach(n)}(e),this.screen.focused===e&&this.screen.rewindFocus())}},r.prototype.detach=function(){this.parent&&this.parent.remove(this)},r.prototype.free=function(){},r.prototype.destroy=function(){this.detach(),this.forDescendants(function(e){e.free(),e.destroyed=!0,e.emit("destroy")},this)},r.prototype.forDescendants=function(e,t){t&&e(this),this.children.forEach(function n(i){e(i),i.children.forEach(n)})},r.prototype.forAncestors=function(e,t){var n=this;for(t&&e(this);n=n.parent;)e(n)},r.prototype.collectDescendants=function(e){var t=[];return this.forDescendants(function(n){t.push(n)},e),t},r.prototype.collectAncestors=function(e){var t=[];return this.forAncestors(function(n){t.push(n)},e),t},r.prototype.emitDescendants=function(){var e=Array.prototype.slice(arguments),t;return typeof e[e.length-1]=="function"&&(t=e.pop()),this.forDescendants(function(n){t&&t(n),n.emit.apply(n,e)},!0)},r.prototype.emitAncestors=function(){var e=Array.prototype.slice(arguments),t;return typeof e[e.length-1]=="function"&&(t=e.pop()),this.forAncestors(function(n){t&&t(n),n.emit.apply(n,e)},!0)},r.prototype.hasDescendant=function(e){return function t(n){for(var i=0;i<n.children.length;i++)if(n.children[i]===e||t(n.children[i])===!0)return!0;return!1}(this)},r.prototype.hasAncestor=function(e){for(var t=this;t=t.parent;)if(t===e)return!0;return!1},r.prototype.get=function(e,t){return this.data.hasOwnProperty(e)?this.data[e]:t},r.prototype.set=function(e,t){return this.data[e]=t},module.exports=r;
//# sourceMappingURL=node.js.map