element.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. /**
  2. * element.js - base element for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. /**
  7. * Modules
  8. */
  9. var assert = require('assert');
  10. var colors = require('../colors')
  11. , unicode = require('../unicode');
  12. var nextTick = global.setImmediate || process.nextTick.bind(process);
  13. var helpers = require('../helpers');
  14. var Node = require('./node');
  15. /**
  16. * Element
  17. */
  18. function Element(options) {
  19. var self = this;
  20. if (!(this instanceof Node)) {
  21. return new Element(options);
  22. }
  23. options = options || {};
  24. // Workaround to get a `scrollable` option.
  25. if (options.scrollable && !this._ignore && this.type !== 'scrollable-box') {
  26. var ScrollableBox = require('./scrollablebox');
  27. Object.getOwnPropertyNames(ScrollableBox.prototype).forEach(function(key) {
  28. if (key === 'type') return;
  29. Object.defineProperty(this, key,
  30. Object.getOwnPropertyDescriptor(ScrollableBox.prototype, key));
  31. }, this);
  32. this._ignore = true;
  33. ScrollableBox.call(this, options);
  34. delete this._ignore;
  35. return this;
  36. }
  37. Node.call(this, options);
  38. this.name = options.name;
  39. options.position = options.position || {
  40. left: options.left,
  41. right: options.right,
  42. top: options.top,
  43. bottom: options.bottom,
  44. width: options.width,
  45. height: options.height
  46. };
  47. if (options.position.width === 'shrink'
  48. || options.position.height === 'shrink') {
  49. if (options.position.width === 'shrink') {
  50. delete options.position.width;
  51. }
  52. if (options.position.height === 'shrink') {
  53. delete options.position.height;
  54. }
  55. options.shrink = true;
  56. }
  57. this.position = options.position;
  58. this.noOverflow = options.noOverflow;
  59. this.dockBorders = options.dockBorders;
  60. this.shadow = options.shadow;
  61. this.style = options.style;
  62. if (!this.style) {
  63. this.style = {};
  64. this.style.fg = options.fg;
  65. this.style.bg = options.bg;
  66. this.style.bold = options.bold;
  67. this.style.underline = options.underline;
  68. this.style.blink = options.blink;
  69. this.style.inverse = options.inverse;
  70. this.style.invisible = options.invisible;
  71. this.style.transparent = options.transparent;
  72. }
  73. this.hidden = options.hidden || false;
  74. this.fixed = options.fixed || false;
  75. this.align = options.align || 'left';
  76. this.valign = options.valign || 'top';
  77. this.wrap = options.wrap !== false;
  78. this.shrink = options.shrink;
  79. this.fixed = options.fixed;
  80. this.ch = options.ch || ' ';
  81. if (typeof options.padding === 'number' || !options.padding) {
  82. options.padding = {
  83. left: options.padding,
  84. top: options.padding,
  85. right: options.padding,
  86. bottom: options.padding
  87. };
  88. }
  89. this.padding = {
  90. left: options.padding.left || 0,
  91. top: options.padding.top || 0,
  92. right: options.padding.right || 0,
  93. bottom: options.padding.bottom || 0
  94. };
  95. this.border = options.border;
  96. if (this.border) {
  97. if (typeof this.border === 'string') {
  98. this.border = { type: this.border };
  99. }
  100. this.border.type = this.border.type || 'bg';
  101. if (this.border.type === 'ascii') this.border.type = 'line';
  102. this.border.ch = this.border.ch || ' ';
  103. this.style.border = this.style.border || this.border.style;
  104. if (!this.style.border) {
  105. this.style.border = {};
  106. this.style.border.fg = this.border.fg;
  107. this.style.border.bg = this.border.bg;
  108. }
  109. //this.border.style = this.style.border;
  110. if (this.border.left == null) this.border.left = true;
  111. if (this.border.top == null) this.border.top = true;
  112. if (this.border.right == null) this.border.right = true;
  113. if (this.border.bottom == null) this.border.bottom = true;
  114. }
  115. // if (options.mouse || options.clickable) {
  116. if (options.clickable) {
  117. this.screen._listenMouse(this);
  118. }
  119. if (options.input || options.keyable) {
  120. this.screen._listenKeys(this);
  121. }
  122. this.parseTags = options.parseTags || options.tags;
  123. this.setContent(options.content || '', true);
  124. if (options.label) {
  125. this.setLabel(options.label);
  126. }
  127. if (options.hoverText) {
  128. this.setHover(options.hoverText);
  129. }
  130. // TODO: Possibly move this to Node for onScreenEvent('mouse', ...).
  131. this.on('newListener', function fn(type) {
  132. // type = type.split(' ').slice(1).join(' ');
  133. if (type === 'mouse'
  134. || type === 'click'
  135. || type === 'mouseover'
  136. || type === 'mouseout'
  137. || type === 'mousedown'
  138. || type === 'mouseup'
  139. || type === 'mousewheel'
  140. || type === 'wheeldown'
  141. || type === 'wheelup'
  142. || type === 'mousemove') {
  143. self.screen._listenMouse(self);
  144. } else if (type === 'keypress' || type.indexOf('key ') === 0) {
  145. self.screen._listenKeys(self);
  146. }
  147. });
  148. this.on('resize', function() {
  149. self.parseContent();
  150. });
  151. this.on('attach', function() {
  152. self.parseContent();
  153. });
  154. this.on('detach', function() {
  155. delete self.lpos;
  156. });
  157. if (options.hoverBg != null) {
  158. options.hoverEffects = options.hoverEffects || {};
  159. options.hoverEffects.bg = options.hoverBg;
  160. }
  161. if (this.style.hover) {
  162. options.hoverEffects = this.style.hover;
  163. }
  164. if (this.style.focus) {
  165. options.focusEffects = this.style.focus;
  166. }
  167. if (options.effects) {
  168. if (options.effects.hover) options.hoverEffects = options.effects.hover;
  169. if (options.effects.focus) options.focusEffects = options.effects.focus;
  170. }
  171. [['hoverEffects', 'mouseover', 'mouseout', '_htemp'],
  172. ['focusEffects', 'focus', 'blur', '_ftemp']].forEach(function(props) {
  173. var pname = props[0], over = props[1], out = props[2], temp = props[3];
  174. self.screen.setEffects(self, self, over, out, self.options[pname], temp);
  175. });
  176. if (this.options.draggable) {
  177. this.draggable = true;
  178. }
  179. if (options.focused) {
  180. this.focus();
  181. }
  182. }
  183. Element.prototype.__proto__ = Node.prototype;
  184. Element.prototype.type = 'element';
  185. Element.prototype.__defineGetter__('focused', function() {
  186. return this.screen.focused === this;
  187. });
  188. Element.prototype.sattr = function(style, fg, bg) {
  189. var bold = style.bold
  190. , underline = style.underline
  191. , blink = style.blink
  192. , inverse = style.inverse
  193. , invisible = style.invisible;
  194. // if (arguments.length === 1) {
  195. if (fg == null && bg == null) {
  196. fg = style.fg;
  197. bg = style.bg;
  198. }
  199. // This used to be a loop, but I decided
  200. // to unroll it for performance's sake.
  201. if (typeof bold === 'function') bold = bold(this);
  202. if (typeof underline === 'function') underline = underline(this);
  203. if (typeof blink === 'function') blink = blink(this);
  204. if (typeof inverse === 'function') inverse = inverse(this);
  205. if (typeof invisible === 'function') invisible = invisible(this);
  206. if (typeof fg === 'function') fg = fg(this);
  207. if (typeof bg === 'function') bg = bg(this);
  208. // return (this.uid << 24)
  209. // | ((this.dockBorders ? 32 : 0) << 18)
  210. return ((invisible ? 16 : 0) << 18)
  211. | ((inverse ? 8 : 0) << 18)
  212. | ((blink ? 4 : 0) << 18)
  213. | ((underline ? 2 : 0) << 18)
  214. | ((bold ? 1 : 0) << 18)
  215. | (colors.convert(fg) << 9)
  216. | colors.convert(bg);
  217. };
  218. Element.prototype.onScreenEvent = function(type, handler) {
  219. var listeners = this._slisteners = this._slisteners || [];
  220. listeners.push({ type: type, handler: handler });
  221. this.screen.on(type, handler);
  222. };
  223. Element.prototype.onceScreenEvent = function(type, handler) {
  224. var listeners = this._slisteners = this._slisteners || [];
  225. var entry = { type: type, handler: handler };
  226. listeners.push(entry);
  227. this.screen.once(type, function() {
  228. var i = listeners.indexOf(entry);
  229. if (~i) listeners.splice(i, 1);
  230. return handler.apply(this, arguments);
  231. });
  232. };
  233. Element.prototype.removeScreenEvent = function(type, handler) {
  234. var listeners = this._slisteners = this._slisteners || [];
  235. for (var i = 0; i < listeners.length; i++) {
  236. var listener = listeners[i];
  237. if (listener.type === type && listener.handler === handler) {
  238. listeners.splice(i, 1);
  239. if (this._slisteners.length === 0) {
  240. delete this._slisteners;
  241. }
  242. break;
  243. }
  244. }
  245. this.screen.removeListener(type, handler);
  246. };
  247. Element.prototype.free = function() {
  248. var listeners = this._slisteners = this._slisteners || [];
  249. for (var i = 0; i < listeners.length; i++) {
  250. var listener = listeners[i];
  251. this.screen.removeListener(listener.type, listener.handler);
  252. }
  253. delete this._slisteners;
  254. };
  255. Element.prototype.hide = function() {
  256. if (this.hidden) return;
  257. this.clearPos();
  258. this.hidden = true;
  259. this.emit('hide');
  260. if (this.screen.focused === this) {
  261. this.screen.rewindFocus();
  262. }
  263. };
  264. Element.prototype.show = function() {
  265. if (!this.hidden) return;
  266. this.hidden = false;
  267. this.emit('show');
  268. };
  269. Element.prototype.toggle = function() {
  270. return this.hidden ? this.show() : this.hide();
  271. };
  272. Element.prototype.focus = function() {
  273. return this.screen.focused = this;
  274. };
  275. Element.prototype.setContent = function(content, noClear, noTags) {
  276. if (!noClear) this.clearPos();
  277. this.content = content || '';
  278. this.parseContent(noTags);
  279. this.emit('set content');
  280. };
  281. Element.prototype.getContent = function() {
  282. if (!this._clines) return '';
  283. return this._clines.fake.join('\n');
  284. };
  285. Element.prototype.setText = function(content, noClear) {
  286. content = content || '';
  287. content = content.replace(/\x1b\[[\d;]*m/g, '');
  288. return this.setContent(content, noClear, true);
  289. };
  290. Element.prototype.getText = function() {
  291. return this.getContent().replace(/\x1b\[[\d;]*m/g, '');
  292. };
  293. Element.prototype.parseContent = function(noTags) {
  294. if (this.detached) return false;
  295. var width = this.width - this.iwidth;
  296. if (this._clines == null
  297. || this._clines.width !== width
  298. || this._clines.content !== this.content) {
  299. var content = this.content;
  300. content = content
  301. .replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g, '')
  302. .replace(/\x1b(?!\[[\d;]*m)/g, '')
  303. .replace(/\r\n|\r/g, '\n')
  304. .replace(/\t/g, this.screen.tabc);
  305. if (this.screen.fullUnicode) {
  306. // double-width chars will eat the next char after render. create a
  307. // blank character after it so it doesn't eat the real next char.
  308. content = content.replace(unicode.chars.all, '$1\x03');
  309. // iTerm2 cannot render combining characters properly.
  310. if (this.screen.program.isiTerm2) {
  311. content = content.replace(unicode.chars.combining, '');
  312. }
  313. } else {
  314. // no double-width: replace them with question-marks.
  315. content = content.replace(unicode.chars.all, '??');
  316. // delete combining characters since they're 0-width anyway.
  317. // NOTE: We could drop this, the non-surrogates would get changed to ? by
  318. // the unicode filter, and surrogates changed to ? by the surrogate
  319. // regex. however, the user might expect them to be 0-width.
  320. // NOTE: Might be better for performance to drop!
  321. content = content.replace(unicode.chars.combining, '');
  322. // no surrogate pairs: replace them with question-marks.
  323. content = content.replace(unicode.chars.surrogate, '?');
  324. // XXX Deduplicate code here:
  325. // content = helpers.dropUnicode(content);
  326. }
  327. if (!noTags) {
  328. content = this._parseTags(content);
  329. }
  330. this._clines = this._wrapContent(content, width);
  331. this._clines.width = width;
  332. this._clines.content = this.content;
  333. this._clines.attr = this._parseAttr(this._clines);
  334. this._clines.ci = [];
  335. this._clines.reduce(function(total, line) {
  336. this._clines.ci.push(total);
  337. return total + line.length + 1;
  338. }.bind(this), 0);
  339. this._pcontent = this._clines.join('\n');
  340. this.emit('parsed content');
  341. return true;
  342. }
  343. // Need to calculate this every time because the default fg/bg may change.
  344. this._clines.attr = this._parseAttr(this._clines) || this._clines.attr;
  345. return false;
  346. };
  347. // Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`.
  348. Element.prototype._parseTags = function(text) {
  349. if (!this.parseTags) return text;
  350. if (!/{\/?[\w\-,;!#]*}/.test(text)) return text;
  351. var program = this.screen.program
  352. , out = ''
  353. , state
  354. , bg = []
  355. , fg = []
  356. , flag = []
  357. , cap
  358. , slash
  359. , param
  360. , attr
  361. , esc;
  362. for (;;) {
  363. if (!esc && (cap = /^{escape}/.exec(text))) {
  364. text = text.substring(cap[0].length);
  365. esc = true;
  366. continue;
  367. }
  368. if (esc && (cap = /^([\s\S]+?){\/escape}/.exec(text))) {
  369. text = text.substring(cap[0].length);
  370. out += cap[1];
  371. esc = false;
  372. continue;
  373. }
  374. if (esc) {
  375. // throw new Error('Unterminated escape tag.');
  376. out += text;
  377. break;
  378. }
  379. if (cap = /^{(\/?)([\w\-,;!#]*)}/.exec(text)) {
  380. text = text.substring(cap[0].length);
  381. slash = cap[1] === '/';
  382. param = cap[2].replace(/-/g, ' ');
  383. if (param === 'open') {
  384. out += '{';
  385. continue;
  386. } else if (param === 'close') {
  387. out += '}';
  388. continue;
  389. }
  390. if (param.slice(-3) === ' bg') state = bg;
  391. else if (param.slice(-3) === ' fg') state = fg;
  392. else state = flag;
  393. if (slash) {
  394. if (!param) {
  395. out += program._attr('normal');
  396. bg.length = 0;
  397. fg.length = 0;
  398. flag.length = 0;
  399. } else {
  400. attr = program._attr(param, false);
  401. if (attr == null) {
  402. out += cap[0];
  403. } else {
  404. // if (param !== state[state.length - 1]) {
  405. // throw new Error('Misnested tags.');
  406. // }
  407. state.pop();
  408. if (state.length) {
  409. out += program._attr(state[state.length - 1]);
  410. } else {
  411. out += attr;
  412. }
  413. }
  414. }
  415. } else {
  416. if (!param) {
  417. out += cap[0];
  418. } else {
  419. attr = program._attr(param);
  420. if (attr == null) {
  421. out += cap[0];
  422. } else {
  423. state.push(param);
  424. out += attr;
  425. }
  426. }
  427. }
  428. continue;
  429. }
  430. if (cap = /^[\s\S]+?(?={\/?[\w\-,;!#]*})/.exec(text)) {
  431. text = text.substring(cap[0].length);
  432. out += cap[0];
  433. continue;
  434. }
  435. out += text;
  436. break;
  437. }
  438. return out;
  439. };
  440. Element.prototype._parseAttr = function(lines) {
  441. var dattr = this.sattr(this.style)
  442. , attr = dattr
  443. , attrs = []
  444. , line
  445. , i
  446. , j
  447. , c;
  448. if (lines[0].attr === attr) {
  449. return;
  450. }
  451. for (j = 0; j < lines.length; j++) {
  452. line = lines[j];
  453. attrs[j] = attr;
  454. for (i = 0; i < line.length; i++) {
  455. if (line[i] === '\x1b') {
  456. if (c = /^\x1b\[[\d;]*m/.exec(line.substring(i))) {
  457. attr = this.screen.attrCode(c[0], attr, dattr);
  458. i += c[0].length - 1;
  459. }
  460. }
  461. }
  462. }
  463. return attrs;
  464. };
  465. Element.prototype._align = function(line, width, align) {
  466. if (!align) return line;
  467. //if (!align && !~line.indexOf('{|}')) return line;
  468. var cline = line.replace(/\x1b\[[\d;]*m/g, '')
  469. , len = cline.length
  470. , s = width - len;
  471. if (this.shrink) {
  472. s = 0;
  473. }
  474. if (len === 0) return line;
  475. if (s < 0) return line;
  476. if (align === 'center') {
  477. s = Array(((s / 2) | 0) + 1).join(' ');
  478. return s + line + s;
  479. } else if (align === 'right') {
  480. s = Array(s + 1).join(' ');
  481. return s + line;
  482. } else if (this.parseTags && ~line.indexOf('{|}')) {
  483. var parts = line.split('{|}');
  484. var cparts = cline.split('{|}');
  485. s = Math.max(width - cparts[0].length - cparts[1].length, 0);
  486. s = Array(s + 1).join(' ');
  487. return parts[0] + s + parts[1];
  488. }
  489. return line;
  490. };
  491. Element.prototype._wrapContent = function(content, width) {
  492. var tags = this.parseTags
  493. , state = this.align
  494. , wrap = this.wrap
  495. , margin = 0
  496. , rtof = []
  497. , ftor = []
  498. , out = []
  499. , no = 0
  500. , line
  501. , align
  502. , cap
  503. , total
  504. , i
  505. , part
  506. , j
  507. , lines
  508. , rest;
  509. lines = content.split('\n');
  510. if (!content) {
  511. out.push(content);
  512. out.rtof = [0];
  513. out.ftor = [[0]];
  514. out.fake = lines;
  515. out.real = out;
  516. out.mwidth = 0;
  517. return out;
  518. }
  519. if (this.scrollbar) margin++;
  520. if (this.type === 'textarea') margin++;
  521. if (width > margin) width -= margin;
  522. main:
  523. for (; no < lines.length; no++) {
  524. line = lines[no];
  525. align = state;
  526. ftor.push([]);
  527. // Handle alignment tags.
  528. if (tags) {
  529. if (cap = /^{(left|center|right)}/.exec(line)) {
  530. line = line.substring(cap[0].length);
  531. align = state = cap[1] !== 'left'
  532. ? cap[1]
  533. : null;
  534. }
  535. if (cap = /{\/(left|center|right)}$/.exec(line)) {
  536. line = line.slice(0, -cap[0].length);
  537. //state = null;
  538. state = this.align;
  539. }
  540. }
  541. // If the string is apparently too long, wrap it.
  542. while (line.length > width) {
  543. // Measure the real width of the string.
  544. for (i = 0, total = 0; i < line.length; i++) {
  545. while (line[i] === '\x1b') {
  546. while (line[i] && line[i++] !== 'm');
  547. }
  548. if (!line[i]) break;
  549. if (++total === width) {
  550. // If we're not wrapping the text, we have to finish up the rest of
  551. // the control sequences before cutting off the line.
  552. i++;
  553. if (!wrap) {
  554. rest = line.substring(i).match(/\x1b\[[^m]*m/g);
  555. rest = rest ? rest.join('') : '';
  556. out.push(this._align(line.substring(0, i) + rest, width, align));
  557. ftor[no].push(out.length - 1);
  558. rtof.push(no);
  559. continue main;
  560. }
  561. if (!this.screen.fullUnicode) {
  562. // Try to find a space to break on.
  563. if (i !== line.length) {
  564. j = i;
  565. while (j > i - 10 && j > 0 && line[--j] !== ' ');
  566. if (line[j] === ' ') i = j + 1;
  567. }
  568. } else {
  569. // Try to find a character to break on.
  570. if (i !== line.length) {
  571. // <XXX>
  572. // Compensate for surrogate length
  573. // counts on wrapping (experimental):
  574. // NOTE: Could optimize this by putting
  575. // it in the parent for loop.
  576. if (unicode.isSurrogate(line, i)) i--;
  577. for (var s = 0, n = 0; n < i; n++) {
  578. if (unicode.isSurrogate(line, n)) s++, n++;
  579. }
  580. i += s;
  581. // </XXX>
  582. j = i;
  583. // Break _past_ space.
  584. // Break _past_ double-width chars.
  585. // Break _past_ surrogate pairs.
  586. // Break _past_ combining chars.
  587. while (j > i - 10 && j > 0) {
  588. j--;
  589. if (line[j] === ' '
  590. || line[j] === '\x03'
  591. || (unicode.isSurrogate(line, j - 1) && line[j + 1] !== '\x03')
  592. || unicode.isCombining(line, j)) {
  593. break;
  594. }
  595. }
  596. if (line[j] === ' '
  597. || line[j] === '\x03'
  598. || (unicode.isSurrogate(line, j - 1) && line[j + 1] !== '\x03')
  599. || unicode.isCombining(line, j)) {
  600. i = j + 1;
  601. }
  602. }
  603. }
  604. break;
  605. }
  606. }
  607. part = line.substring(0, i);
  608. line = line.substring(i);
  609. out.push(this._align(part, width, align));
  610. ftor[no].push(out.length - 1);
  611. rtof.push(no);
  612. // Make sure we didn't wrap the line to the very end, otherwise
  613. // we get a pointless empty line after a newline.
  614. if (line === '') continue main;
  615. // If only an escape code got cut off, at it to `part`.
  616. if (/^(?:\x1b[\[\d;]*m)+$/.test(line)) {
  617. out[out.length - 1] += line;
  618. continue main;
  619. }
  620. }
  621. out.push(this._align(line, width, align));
  622. ftor[no].push(out.length - 1);
  623. rtof.push(no);
  624. }
  625. out.rtof = rtof;
  626. out.ftor = ftor;
  627. out.fake = lines;
  628. out.real = out;
  629. out.mwidth = out.reduce(function(current, line) {
  630. line = line.replace(/\x1b\[[\d;]*m/g, '');
  631. return line.length > current
  632. ? line.length
  633. : current;
  634. }, 0);
  635. return out;
  636. };
  637. Element.prototype.__defineGetter__('visible', function() {
  638. var el = this;
  639. do {
  640. if (el.detached) return false;
  641. if (el.hidden) return false;
  642. // if (!el.lpos) return false;
  643. // if (el.position.width === 0 || el.position.height === 0) return false;
  644. } while (el = el.parent);
  645. return true;
  646. });
  647. Element.prototype.__defineGetter__('_detached', function() {
  648. var el = this;
  649. do {
  650. if (el.type === 'screen') return false;
  651. if (!el.parent) return true;
  652. } while (el = el.parent);
  653. return false;
  654. });
  655. Element.prototype.enableMouse = function() {
  656. this.screen._listenMouse(this);
  657. };
  658. Element.prototype.enableKeys = function() {
  659. this.screen._listenKeys(this);
  660. };
  661. Element.prototype.enableInput = function() {
  662. this.screen._listenMouse(this);
  663. this.screen._listenKeys(this);
  664. };
  665. Element.prototype.__defineGetter__('draggable', function() {
  666. return this._draggable === true;
  667. });
  668. Element.prototype.__defineSetter__('draggable', function(draggable) {
  669. return draggable ? this.enableDrag(draggable) : this.disableDrag();
  670. });
  671. Element.prototype.enableDrag = function(verify) {
  672. var self = this;
  673. if (this._draggable) return true;
  674. if (typeof verify !== 'function') {
  675. verify = function() { return true; };
  676. }
  677. this.enableMouse();
  678. this.on('mousedown', this._dragMD = function(data) {
  679. if (self.screen._dragging) return;
  680. if (!verify(data)) return;
  681. self.screen._dragging = self;
  682. self._drag = {
  683. x: data.x - self.aleft,
  684. y: data.y - self.atop
  685. };
  686. self.setFront();
  687. });
  688. this.onScreenEvent('mouse', this._dragM = function(data) {
  689. if (self.screen._dragging !== self) return;
  690. if (data.action !== 'mousedown' && data.action !== 'mousemove') {
  691. delete self.screen._dragging;
  692. delete self._drag;
  693. return;
  694. }
  695. // This can happen in edge cases where the user is
  696. // already dragging and element when it is detached.
  697. if (!self.parent) return;
  698. var ox = self._drag.x
  699. , oy = self._drag.y
  700. , px = self.parent.aleft
  701. , py = self.parent.atop
  702. , x = data.x - px - ox
  703. , y = data.y - py - oy;
  704. if (self.position.right != null) {
  705. if (self.position.left != null) {
  706. self.width = '100%-' + (self.parent.width - self.width);
  707. }
  708. self.position.right = null;
  709. }
  710. if (self.position.bottom != null) {
  711. if (self.position.top != null) {
  712. self.height = '100%-' + (self.parent.height - self.height);
  713. }
  714. self.position.bottom = null;
  715. }
  716. self.rleft = x;
  717. self.rtop = y;
  718. self.screen.render();
  719. });
  720. return this._draggable = true;
  721. };
  722. Element.prototype.disableDrag = function() {
  723. if (!this._draggable) return false;
  724. delete this.screen._dragging;
  725. delete this._drag;
  726. this.removeListener('mousedown', this._dragMD);
  727. this.removeScreenEvent('mouse', this._dragM);
  728. return this._draggable = false;
  729. };
  730. Element.prototype.key = function() {
  731. return this.screen.program.key.apply(this, arguments);
  732. };
  733. Element.prototype.onceKey = function() {
  734. return this.screen.program.onceKey.apply(this, arguments);
  735. };
  736. Element.prototype.unkey =
  737. Element.prototype.removeKey = function() {
  738. return this.screen.program.unkey.apply(this, arguments);
  739. };
  740. Element.prototype.setIndex = function(index) {
  741. if (!this.parent) return;
  742. if (index < 0) {
  743. index = this.parent.children.length + index;
  744. }
  745. index = Math.max(index, 0);
  746. index = Math.min(index, this.parent.children.length - 1);
  747. var i = this.parent.children.indexOf(this);
  748. if (!~i) return;
  749. var item = this.parent.children.splice(i, 1)[0];
  750. this.parent.children.splice(index, 0, item);
  751. };
  752. Element.prototype.setFront = function() {
  753. return this.setIndex(-1);
  754. };
  755. Element.prototype.setBack = function() {
  756. return this.setIndex(0);
  757. };
  758. Element.prototype.clearPos = function(get, override) {
  759. if (this.detached) return;
  760. var lpos = this._getCoords(get);
  761. if (!lpos) return;
  762. this.screen.clearRegion(
  763. lpos.xi, lpos.xl,
  764. lpos.yi, lpos.yl,
  765. override);
  766. };
  767. Element.prototype.setLabel = function(options) {
  768. var self = this;
  769. var Box = require('./box');
  770. if (typeof options === 'string') {
  771. options = { text: options };
  772. }
  773. if (this._label) {
  774. this._label.setContent(options.text);
  775. if (options.side !== 'right') {
  776. this._label.rleft = 2 + (this.border ? -1 : 0);
  777. this._label.position.right = undefined;
  778. if (!this.screen.autoPadding) {
  779. this._label.rleft = 2;
  780. }
  781. } else {
  782. this._label.rright = 2 + (this.border ? -1 : 0);
  783. this._label.position.left = undefined;
  784. if (!this.screen.autoPadding) {
  785. this._label.rright = 2;
  786. }
  787. }
  788. return;
  789. }
  790. this._label = new Box({
  791. screen: this.screen,
  792. parent: this,
  793. content: options.text,
  794. top: -this.itop,
  795. tags: this.parseTags,
  796. shrink: true,
  797. style: this.style.label
  798. });
  799. if (options.side !== 'right') {
  800. this._label.rleft = 2 - this.ileft;
  801. } else {
  802. this._label.rright = 2 - this.iright;
  803. }
  804. this._label._isLabel = true;
  805. if (!this.screen.autoPadding) {
  806. if (options.side !== 'right') {
  807. this._label.rleft = 2;
  808. } else {
  809. this._label.rright = 2;
  810. }
  811. this._label.rtop = 0;
  812. }
  813. var reposition = function() {
  814. self._label.rtop = (self.childBase || 0) - self.itop;
  815. if (!self.screen.autoPadding) {
  816. self._label.rtop = (self.childBase || 0);
  817. }
  818. self.screen.render();
  819. };
  820. this.on('scroll', this._labelScroll = function() {
  821. reposition();
  822. });
  823. this.on('resize', this._labelResize = function() {
  824. nextTick(function() {
  825. reposition();
  826. });
  827. });
  828. };
  829. Element.prototype.removeLabel = function() {
  830. if (!this._label) return;
  831. this.removeListener('scroll', this._labelScroll);
  832. this.removeListener('resize', this._labelResize);
  833. this._label.detach();
  834. delete this._labelScroll;
  835. delete this._labelResize;
  836. delete this._label;
  837. };
  838. Element.prototype.setHover = function(options) {
  839. if (typeof options === 'string') {
  840. options = { text: options };
  841. }
  842. this._hoverOptions = options;
  843. this.enableMouse();
  844. this.screen._initHover();
  845. };
  846. Element.prototype.removeHover = function() {
  847. delete this._hoverOptions;
  848. if (!this.screen._hoverText || this.screen._hoverText.detached) return;
  849. this.screen._hoverText.detach();
  850. this.screen.render();
  851. };
  852. /**
  853. * Positioning
  854. */
  855. // The below methods are a bit confusing: basically
  856. // whenever Box.render is called `lpos` gets set on
  857. // the element, an object containing the rendered
  858. // coordinates. Since these don't update if the
  859. // element is moved somehow, they're unreliable in
  860. // that situation. However, if we can guarantee that
  861. // lpos is good and up to date, it can be more
  862. // accurate than the calculated positions below.
  863. // In this case, if the element is being rendered,
  864. // it's guaranteed that the parent will have been
  865. // rendered first, in which case we can use the
  866. // parant's lpos instead of recalculating it's
  867. // position (since that might be wrong because
  868. // it doesn't handle content shrinkage).
  869. Element.prototype._getPos = function() {
  870. var pos = this.lpos;
  871. assert.ok(pos);
  872. if (pos.aleft != null) return pos;
  873. pos.aleft = pos.xi;
  874. pos.atop = pos.yi;
  875. pos.aright = this.screen.cols - pos.xl;
  876. pos.abottom = this.screen.rows - pos.yl;
  877. pos.width = pos.xl - pos.xi;
  878. pos.height = pos.yl - pos.yi;
  879. return pos;
  880. };
  881. /**
  882. * Position Getters
  883. */
  884. Element.prototype._getWidth = function(get) {
  885. var parent = get ? this.parent._getPos() : this.parent
  886. , width = this.position.width
  887. , left
  888. , expr;
  889. if (typeof width === 'string') {
  890. if (width === 'half') width = '50%';
  891. expr = width.split(/(?=\+|-)/);
  892. width = expr[0];
  893. width = +width.slice(0, -1) / 100;
  894. width = parent.width * width | 0;
  895. width += +(expr[1] || 0);
  896. return width;
  897. }
  898. // This is for if the element is being streched or shrunken.
  899. // Although the width for shrunken elements is calculated
  900. // in the render function, it may be calculated based on
  901. // the content width, and the content width is initially
  902. // decided by the width the element, so it needs to be
  903. // calculated here.
  904. if (width == null) {
  905. left = this.position.left || 0;
  906. if (typeof left === 'string') {
  907. if (left === 'center') left = '50%';
  908. expr = left.split(/(?=\+|-)/);
  909. left = expr[0];
  910. left = +left.slice(0, -1) / 100;
  911. left = parent.width * left | 0;
  912. left += +(expr[1] || 0);
  913. }
  914. width = parent.width - (this.position.right || 0) - left;
  915. if (this.screen.autoPadding) {
  916. if ((this.position.left != null || this.position.right == null)
  917. && this.position.left !== 'center') {
  918. width -= this.parent.ileft;
  919. }
  920. width -= this.parent.iright;
  921. }
  922. }
  923. return width;
  924. };
  925. Element.prototype.__defineGetter__('width', function() {
  926. return this._getWidth(false);
  927. });
  928. Element.prototype._getHeight = function(get) {
  929. var parent = get ? this.parent._getPos() : this.parent
  930. , height = this.position.height
  931. , top
  932. , expr;
  933. if (typeof height === 'string') {
  934. if (height === 'half') height = '50%';
  935. expr = height.split(/(?=\+|-)/);
  936. height = expr[0];
  937. height = +height.slice(0, -1) / 100;
  938. height = parent.height * height | 0;
  939. height += +(expr[1] || 0);
  940. return height;
  941. }
  942. // This is for if the element is being streched or shrunken.
  943. // Although the width for shrunken elements is calculated
  944. // in the render function, it may be calculated based on
  945. // the content width, and the content width is initially
  946. // decided by the width the element, so it needs to be
  947. // calculated here.
  948. if (height == null) {
  949. top = this.position.top || 0;
  950. if (typeof top === 'string') {
  951. if (top === 'center') top = '50%';
  952. expr = top.split(/(?=\+|-)/);
  953. top = expr[0];
  954. top = +top.slice(0, -1) / 100;
  955. top = parent.height * top | 0;
  956. top += +(expr[1] || 0);
  957. }
  958. height = parent.height - (this.position.bottom || 0) - top;
  959. if (this.screen.autoPadding) {
  960. if ((this.position.top != null
  961. || this.position.bottom == null)
  962. && this.position.top !== 'center') {
  963. height -= this.parent.itop;
  964. }
  965. height -= this.parent.ibottom;
  966. }
  967. }
  968. return height;
  969. };
  970. Element.prototype.__defineGetter__('height', function() {
  971. return this._getHeight(false);
  972. });
  973. Element.prototype._getLeft = function(get) {
  974. var parent = get ? this.parent._getPos() : this.parent
  975. , left = this.position.left || 0
  976. , expr;
  977. if (typeof left === 'string') {
  978. if (left === 'center') left = '50%';
  979. expr = left.split(/(?=\+|-)/);
  980. left = expr[0];
  981. left = +left.slice(0, -1) / 100;
  982. left = parent.width * left | 0;
  983. left += +(expr[1] || 0);
  984. if (this.position.left === 'center') {
  985. left -= this._getWidth(get) / 2 | 0;
  986. }
  987. }
  988. if (this.position.left == null && this.position.right != null) {
  989. return this.screen.cols - this._getWidth(get) - this._getRight(get);
  990. }
  991. if (this.screen.autoPadding) {
  992. if ((this.position.left != null
  993. || this.position.right == null)
  994. && this.position.left !== 'center') {
  995. left += this.parent.ileft;
  996. }
  997. }
  998. return (parent.aleft || 0) + left;
  999. };
  1000. Element.prototype.__defineGetter__('aleft', function() {
  1001. return this._getLeft(false);
  1002. });
  1003. Element.prototype._getRight = function(get) {
  1004. var parent = get ? this.parent._getPos() : this.parent
  1005. , right;
  1006. if (this.position.right == null && this.position.left != null) {
  1007. right = this.screen.cols - (this._getLeft(get) + this._getWidth(get));
  1008. if (this.screen.autoPadding) {
  1009. right += this.parent.iright;
  1010. }
  1011. return right;
  1012. }
  1013. right = (parent.aright || 0) + (this.position.right || 0);
  1014. if (this.screen.autoPadding) {
  1015. right += this.parent.iright;
  1016. }
  1017. return right;
  1018. };
  1019. Element.prototype.__defineGetter__('aright', function() {
  1020. return this._getRight(false);
  1021. });
  1022. Element.prototype._getTop = function(get) {
  1023. var parent = get ? this.parent._getPos() : this.parent
  1024. , top = this.position.top || 0
  1025. , expr;
  1026. if (typeof top === 'string') {
  1027. if (top === 'center') top = '50%';
  1028. expr = top.split(/(?=\+|-)/);
  1029. top = expr[0];
  1030. top = +top.slice(0, -1) / 100;
  1031. top = parent.height * top | 0;
  1032. top += +(expr[1] || 0);
  1033. if (this.position.top === 'center') {
  1034. top -= this._getHeight(get) / 2 | 0;
  1035. }
  1036. }
  1037. if (this.position.top == null && this.position.bottom != null) {
  1038. return this.screen.rows - this._getHeight(get) - this._getBottom(get);
  1039. }
  1040. if (this.screen.autoPadding) {
  1041. if ((this.position.top != null
  1042. || this.position.bottom == null)
  1043. && this.position.top !== 'center') {
  1044. top += this.parent.itop;
  1045. }
  1046. }
  1047. return (parent.atop || 0) + top;
  1048. };
  1049. Element.prototype.__defineGetter__('atop', function() {
  1050. return this._getTop(false);
  1051. });
  1052. Element.prototype._getBottom = function(get) {
  1053. var parent = get ? this.parent._getPos() : this.parent
  1054. , bottom;
  1055. if (this.position.bottom == null && this.position.top != null) {
  1056. bottom = this.screen.rows - (this._getTop(get) + this._getHeight(get));
  1057. if (this.screen.autoPadding) {
  1058. bottom += this.parent.ibottom;
  1059. }
  1060. return bottom;
  1061. }
  1062. bottom = (parent.abottom || 0) + (this.position.bottom || 0);
  1063. if (this.screen.autoPadding) {
  1064. bottom += this.parent.ibottom;
  1065. }
  1066. return bottom;
  1067. };
  1068. Element.prototype.__defineGetter__('abottom', function() {
  1069. return this._getBottom(false);
  1070. });
  1071. Element.prototype.__defineGetter__('rleft', function() {
  1072. return this.aleft - this.parent.aleft;
  1073. });
  1074. Element.prototype.__defineGetter__('rright', function() {
  1075. return this.aright - this.parent.aright;
  1076. });
  1077. Element.prototype.__defineGetter__('rtop', function() {
  1078. return this.atop - this.parent.atop;
  1079. });
  1080. Element.prototype.__defineGetter__('rbottom', function() {
  1081. return this.abottom - this.parent.abottom;
  1082. });
  1083. /**
  1084. * Position Setters
  1085. */
  1086. // NOTE:
  1087. // For aright, abottom, right, and bottom:
  1088. // If position.bottom is null, we could simply set top instead.
  1089. // But it wouldn't replicate bottom behavior appropriately if
  1090. // the parent was resized, etc.
  1091. Element.prototype.__defineSetter__('width', function(val) {
  1092. if (this.position.width === val) return;
  1093. if (/^\d+$/.test(val)) val = +val;
  1094. this.emit('resize');
  1095. this.clearPos();
  1096. return this.position.width = val;
  1097. });
  1098. Element.prototype.__defineSetter__('height', function(val) {
  1099. if (this.position.height === val) return;
  1100. if (/^\d+$/.test(val)) val = +val;
  1101. this.emit('resize');
  1102. this.clearPos();
  1103. return this.position.height = val;
  1104. });
  1105. Element.prototype.__defineSetter__('aleft', function(val) {
  1106. var expr;
  1107. if (typeof val === 'string') {
  1108. if (val === 'center') {
  1109. val = this.screen.width / 2 | 0;
  1110. val -= this.width / 2 | 0;
  1111. } else {
  1112. expr = val.split(/(?=\+|-)/);
  1113. val = expr[0];
  1114. val = +val.slice(0, -1) / 100;
  1115. val = this.screen.width * val | 0;
  1116. val += +(expr[1] || 0);
  1117. }
  1118. }
  1119. val -= this.parent.aleft;
  1120. if (this.position.left === val) return;
  1121. this.emit('move');
  1122. this.clearPos();
  1123. return this.position.left = val;
  1124. });
  1125. Element.prototype.__defineSetter__('aright', function(val) {
  1126. val -= this.parent.aright;
  1127. if (this.position.right === val) return;
  1128. this.emit('move');
  1129. this.clearPos();
  1130. return this.position.right = val;
  1131. });
  1132. Element.prototype.__defineSetter__('atop', function(val) {
  1133. var expr;
  1134. if (typeof val === 'string') {
  1135. if (val === 'center') {
  1136. val = this.screen.height / 2 | 0;
  1137. val -= this.height / 2 | 0;
  1138. } else {
  1139. expr = val.split(/(?=\+|-)/);
  1140. val = expr[0];
  1141. val = +val.slice(0, -1) / 100;
  1142. val = this.screen.height * val | 0;
  1143. val += +(expr[1] || 0);
  1144. }
  1145. }
  1146. val -= this.parent.atop;
  1147. if (this.position.top === val) return;
  1148. this.emit('move');
  1149. this.clearPos();
  1150. return this.position.top = val;
  1151. });
  1152. Element.prototype.__defineSetter__('abottom', function(val) {
  1153. val -= this.parent.abottom;
  1154. if (this.position.bottom === val) return;
  1155. this.emit('move');
  1156. this.clearPos();
  1157. return this.position.bottom = val;
  1158. });
  1159. Element.prototype.__defineSetter__('rleft', function(val) {
  1160. if (this.position.left === val) return;
  1161. if (/^\d+$/.test(val)) val = +val;
  1162. this.emit('move');
  1163. this.clearPos();
  1164. return this.position.left = val;
  1165. });
  1166. Element.prototype.__defineSetter__('rright', function(val) {
  1167. if (this.position.right === val) return;
  1168. this.emit('move');
  1169. this.clearPos();
  1170. return this.position.right = val;
  1171. });
  1172. Element.prototype.__defineSetter__('rtop', function(val) {
  1173. if (this.position.top === val) return;
  1174. if (/^\d+$/.test(val)) val = +val;
  1175. this.emit('move');
  1176. this.clearPos();
  1177. return this.position.top = val;
  1178. });
  1179. Element.prototype.__defineSetter__('rbottom', function(val) {
  1180. if (this.position.bottom === val) return;
  1181. this.emit('move');
  1182. this.clearPos();
  1183. return this.position.bottom = val;
  1184. });
  1185. Element.prototype.__defineGetter__('ileft', function() {
  1186. return (this.border ? 1 : 0) + this.padding.left;
  1187. // return (this.border && this.border.left ? 1 : 0) + this.padding.left;
  1188. });
  1189. Element.prototype.__defineGetter__('itop', function() {
  1190. return (this.border ? 1 : 0) + this.padding.top;
  1191. // return (this.border && this.border.top ? 1 : 0) + this.padding.top;
  1192. });
  1193. Element.prototype.__defineGetter__('iright', function() {
  1194. return (this.border ? 1 : 0) + this.padding.right;
  1195. // return (this.border && this.border.right ? 1 : 0) + this.padding.right;
  1196. });
  1197. Element.prototype.__defineGetter__('ibottom', function() {
  1198. return (this.border ? 1 : 0) + this.padding.bottom;
  1199. // return (this.border && this.border.bottom ? 1 : 0) + this.padding.bottom;
  1200. });
  1201. Element.prototype.__defineGetter__('iwidth', function() {
  1202. // return (this.border
  1203. // ? ((this.border.left ? 1 : 0) + (this.border.right ? 1 : 0)) : 0)
  1204. // + this.padding.left + this.padding.right;
  1205. return (this.border ? 2 : 0) + this.padding.left + this.padding.right;
  1206. });
  1207. Element.prototype.__defineGetter__('iheight', function() {
  1208. // return (this.border
  1209. // ? ((this.border.top ? 1 : 0) + (this.border.bottom ? 1 : 0)) : 0)
  1210. // + this.padding.top + this.padding.bottom;
  1211. return (this.border ? 2 : 0) + this.padding.top + this.padding.bottom;
  1212. });
  1213. Element.prototype.__defineGetter__('tpadding', function() {
  1214. return this.padding.left + this.padding.top
  1215. + this.padding.right + this.padding.bottom;
  1216. });
  1217. /**
  1218. * Relative coordinates as default properties
  1219. */
  1220. Element.prototype.__defineGetter__('left', function() {
  1221. return this.rleft;
  1222. });
  1223. Element.prototype.__defineGetter__('right', function() {
  1224. return this.rright;
  1225. });
  1226. Element.prototype.__defineGetter__('top', function() {
  1227. return this.rtop;
  1228. });
  1229. Element.prototype.__defineGetter__('bottom', function() {
  1230. return this.rbottom;
  1231. });
  1232. Element.prototype.__defineSetter__('left', function(val) {
  1233. return this.rleft = val;
  1234. });
  1235. Element.prototype.__defineSetter__('right', function(val) {
  1236. return this.rright = val;
  1237. });
  1238. Element.prototype.__defineSetter__('top', function(val) {
  1239. return this.rtop = val;
  1240. });
  1241. Element.prototype.__defineSetter__('bottom', function(val) {
  1242. return this.rbottom = val;
  1243. });
  1244. /**
  1245. * Rendering - here be dragons
  1246. */
  1247. Element.prototype._getShrinkBox = function(xi, xl, yi, yl, get) {
  1248. if (!this.children.length) {
  1249. return { xi: xi, xl: xi + 1, yi: yi, yl: yi + 1 };
  1250. }
  1251. var i, el, ret, mxi = xi, mxl = xi + 1, myi = yi, myl = yi + 1;
  1252. // This is a chicken and egg problem. We need to determine how the children
  1253. // will render in order to determine how this element renders, but it in
  1254. // order to figure out how the children will render, they need to know
  1255. // exactly how their parent renders, so, we can give them what we have so
  1256. // far.
  1257. var _lpos;
  1258. if (get) {
  1259. _lpos = this.lpos;
  1260. this.lpos = { xi: xi, xl: xl, yi: yi, yl: yl };
  1261. //this.shrink = false;
  1262. }
  1263. for (i = 0; i < this.children.length; i++) {
  1264. el = this.children[i];
  1265. ret = el._getCoords(get);
  1266. // Or just (seemed to work, but probably not good):
  1267. // ret = el.lpos || this.lpos;
  1268. if (!ret) continue;
  1269. // Since the parent element is shrunk, and the child elements think it's
  1270. // going to take up as much space as possible, an element anchored to the
  1271. // right or bottom will inadvertantly make the parent's shrunken size as
  1272. // large as possible. So, we can just use the height and/or width the of
  1273. // element.
  1274. // if (get) {
  1275. if (el.position.left == null && el.position.right != null) {
  1276. ret.xl = xi + (ret.xl - ret.xi);
  1277. ret.xi = xi;
  1278. if (this.screen.autoPadding) {
  1279. // Maybe just do this no matter what.
  1280. ret.xl += this.ileft;
  1281. ret.xi += this.ileft;
  1282. }
  1283. }
  1284. if (el.position.top == null && el.position.bottom != null) {
  1285. ret.yl = yi + (ret.yl - ret.yi);
  1286. ret.yi = yi;
  1287. if (this.screen.autoPadding) {
  1288. // Maybe just do this no matter what.
  1289. ret.yl += this.itop;
  1290. ret.yi += this.itop;
  1291. }
  1292. }
  1293. if (ret.xi < mxi) mxi = ret.xi;
  1294. if (ret.xl > mxl) mxl = ret.xl;
  1295. if (ret.yi < myi) myi = ret.yi;
  1296. if (ret.yl > myl) myl = ret.yl;
  1297. }
  1298. if (get) {
  1299. this.lpos = _lpos;
  1300. //this.shrink = true;
  1301. }
  1302. if (this.position.width == null
  1303. && (this.position.left == null
  1304. || this.position.right == null)) {
  1305. if (this.position.left == null && this.position.right != null) {
  1306. xi = xl - (mxl - mxi);
  1307. if (!this.screen.autoPadding) {
  1308. xi -= this.padding.left + this.padding.right;
  1309. } else {
  1310. xi -= this.ileft;
  1311. }
  1312. } else {
  1313. xl = mxl;
  1314. if (!this.screen.autoPadding) {
  1315. xl += this.padding.left + this.padding.right;
  1316. // XXX Temporary workaround until we decide to make autoPadding default.
  1317. // See widget-listtable.js for an example of why this is necessary.
  1318. // XXX Maybe just to this for all this being that this would affect
  1319. // width shrunken normal shrunken lists as well.
  1320. // if (this._isList) {
  1321. if (this.type === 'list-table') {
  1322. xl -= this.padding.left + this.padding.right;
  1323. xl += this.iright;
  1324. }
  1325. } else {
  1326. //xl += this.padding.right;
  1327. xl += this.iright;
  1328. }
  1329. }
  1330. }
  1331. if (this.position.height == null
  1332. && (this.position.top == null
  1333. || this.position.bottom == null)
  1334. && (!this.scrollable || this._isList)) {
  1335. // NOTE: Lists get special treatment if they are shrunken - assume they
  1336. // want all list items showing. This is one case we can calculate the
  1337. // height based on items/boxes.
  1338. if (this._isList) {
  1339. myi = 0 - this.itop;
  1340. myl = this.items.length + this.ibottom;
  1341. }
  1342. if (this.position.top == null && this.position.bottom != null) {
  1343. yi = yl - (myl - myi);
  1344. if (!this.screen.autoPadding) {
  1345. yi -= this.padding.top + this.padding.bottom;
  1346. } else {
  1347. yi -= this.itop;
  1348. }
  1349. } else {
  1350. yl = myl;
  1351. if (!this.screen.autoPadding) {
  1352. yl += this.padding.top + this.padding.bottom;
  1353. } else {
  1354. yl += this.ibottom;
  1355. }
  1356. }
  1357. }
  1358. return { xi: xi, xl: xl, yi: yi, yl: yl };
  1359. };
  1360. Element.prototype._getShrinkContent = function(xi, xl, yi, yl) {
  1361. var h = this._clines.length
  1362. , w = this._clines.mwidth || 1;
  1363. if (this.position.width == null
  1364. && (this.position.left == null
  1365. || this.position.right == null)) {
  1366. if (this.position.left == null && this.position.right != null) {
  1367. xi = xl - w - this.iwidth;
  1368. } else {
  1369. xl = xi + w + this.iwidth;
  1370. }
  1371. }
  1372. if (this.position.height == null
  1373. && (this.position.top == null
  1374. || this.position.bottom == null)
  1375. && (!this.scrollable || this._isList)) {
  1376. if (this.position.top == null && this.position.bottom != null) {
  1377. yi = yl - h - this.iheight;
  1378. } else {
  1379. yl = yi + h + this.iheight;
  1380. }
  1381. }
  1382. return { xi: xi, xl: xl, yi: yi, yl: yl };
  1383. };
  1384. Element.prototype._getShrink = function(xi, xl, yi, yl, get) {
  1385. var shrinkBox = this._getShrinkBox(xi, xl, yi, yl, get)
  1386. , shrinkContent = this._getShrinkContent(xi, xl, yi, yl, get)
  1387. , xll = xl
  1388. , yll = yl;
  1389. // Figure out which one is bigger and use it.
  1390. if (shrinkBox.xl - shrinkBox.xi > shrinkContent.xl - shrinkContent.xi) {
  1391. xi = shrinkBox.xi;
  1392. xl = shrinkBox.xl;
  1393. } else {
  1394. xi = shrinkContent.xi;
  1395. xl = shrinkContent.xl;
  1396. }
  1397. if (shrinkBox.yl - shrinkBox.yi > shrinkContent.yl - shrinkContent.yi) {
  1398. yi = shrinkBox.yi;
  1399. yl = shrinkBox.yl;
  1400. } else {
  1401. yi = shrinkContent.yi;
  1402. yl = shrinkContent.yl;
  1403. }
  1404. // Recenter shrunken elements.
  1405. if (xl < xll && this.position.left === 'center') {
  1406. xll = (xll - xl) / 2 | 0;
  1407. xi += xll;
  1408. xl += xll;
  1409. }
  1410. if (yl < yll && this.position.top === 'center') {
  1411. yll = (yll - yl) / 2 | 0;
  1412. yi += yll;
  1413. yl += yll;
  1414. }
  1415. return { xi: xi, xl: xl, yi: yi, yl: yl };
  1416. };
  1417. Element.prototype._getCoords = function(get, noscroll) {
  1418. if (this.hidden) return;
  1419. // if (this.parent._rendering) {
  1420. // get = true;
  1421. // }
  1422. var xi = this._getLeft(get)
  1423. , xl = xi + this._getWidth(get)
  1424. , yi = this._getTop(get)
  1425. , yl = yi + this._getHeight(get)
  1426. , base = this.childBase || 0
  1427. , el = this
  1428. , fixed = this.fixed
  1429. , coords
  1430. , v
  1431. , noleft
  1432. , noright
  1433. , notop
  1434. , nobot
  1435. , ppos
  1436. , b;
  1437. // Attempt to shrink the element base on the
  1438. // size of the content and child elements.
  1439. if (this.shrink) {
  1440. coords = this._getShrink(xi, xl, yi, yl, get);
  1441. xi = coords.xi, xl = coords.xl;
  1442. yi = coords.yi, yl = coords.yl;
  1443. }
  1444. // Find a scrollable ancestor if we have one.
  1445. while (el = el.parent) {
  1446. if (el.scrollable) {
  1447. if (fixed) {
  1448. fixed = false;
  1449. continue;
  1450. }
  1451. break;
  1452. }
  1453. }
  1454. // Check to make sure we're visible and
  1455. // inside of the visible scroll area.
  1456. // NOTE: Lists have a property where only
  1457. // the list items are obfuscated.
  1458. // Old way of doing things, this would not render right if a shrunken element
  1459. // with lots of boxes in it was within a scrollable element.
  1460. // See: $ node test/widget-shrink-fail.js
  1461. // var thisparent = this.parent;
  1462. var thisparent = el;
  1463. if (el && !noscroll) {
  1464. ppos = thisparent.lpos;
  1465. // The shrink option can cause a stack overflow
  1466. // by calling _getCoords on the child again.
  1467. // if (!get && !thisparent.shrink) {
  1468. // ppos = thisparent._getCoords();
  1469. // }
  1470. if (!ppos) return;
  1471. // TODO: Figure out how to fix base (and cbase to only
  1472. // take into account the *parent's* padding.
  1473. yi -= ppos.base;
  1474. yl -= ppos.base;
  1475. b = thisparent.border ? 1 : 0;
  1476. // XXX
  1477. // Fixes non-`fixed` labels to work with scrolling (they're ON the border):
  1478. // if (this.position.left < 0
  1479. // || this.position.right < 0
  1480. // || this.position.top < 0
  1481. // || this.position.bottom < 0) {
  1482. if (this._isLabel) {
  1483. b = 0;
  1484. }
  1485. if (yi < ppos.yi + b) {
  1486. if (yl - 1 < ppos.yi + b) {
  1487. // Is above.
  1488. return;
  1489. } else {
  1490. // Is partially covered above.
  1491. notop = true;
  1492. v = ppos.yi - yi;
  1493. if (this.border) v--;
  1494. if (thisparent.border) v++;
  1495. base += v;
  1496. yi += v;
  1497. }
  1498. } else if (yl > ppos.yl - b) {
  1499. if (yi > ppos.yl - 1 - b) {
  1500. // Is below.
  1501. return;
  1502. } else {
  1503. // Is partially covered below.
  1504. nobot = true;
  1505. v = yl - ppos.yl;
  1506. if (this.border) v--;
  1507. if (thisparent.border) v++;
  1508. yl -= v;
  1509. }
  1510. }
  1511. // Shouldn't be necessary.
  1512. // assert.ok(yi < yl);
  1513. if (yi >= yl) return;
  1514. // Could allow overlapping stuff in scrolling elements
  1515. // if we cleared the pending buffer before every draw.
  1516. if (xi < el.lpos.xi) {
  1517. xi = el.lpos.xi;
  1518. noleft = true;
  1519. if (this.border) xi--;
  1520. if (thisparent.border) xi++;
  1521. }
  1522. if (xl > el.lpos.xl) {
  1523. xl = el.lpos.xl;
  1524. noright = true;
  1525. if (this.border) xl++;
  1526. if (thisparent.border) xl--;
  1527. }
  1528. //if (xi > xl) return;
  1529. if (xi >= xl) return;
  1530. }
  1531. if (this.noOverflow && this.parent.lpos) {
  1532. if (xi < this.parent.lpos.xi + this.parent.ileft) {
  1533. xi = this.parent.lpos.xi + this.parent.ileft;
  1534. }
  1535. if (xl > this.parent.lpos.xl - this.parent.iright) {
  1536. xl = this.parent.lpos.xl - this.parent.iright;
  1537. }
  1538. if (yi < this.parent.lpos.yi + this.parent.itop) {
  1539. yi = this.parent.lpos.yi + this.parent.itop;
  1540. }
  1541. if (yl > this.parent.lpos.yl - this.parent.ibottom) {
  1542. yl = this.parent.lpos.yl - this.parent.ibottom;
  1543. }
  1544. }
  1545. // if (this.parent.lpos) {
  1546. // this.parent.lpos._scrollBottom = Math.max(
  1547. // this.parent.lpos._scrollBottom, yl);
  1548. // }
  1549. return {
  1550. xi: xi,
  1551. xl: xl,
  1552. yi: yi,
  1553. yl: yl,
  1554. base: base,
  1555. noleft: noleft,
  1556. noright: noright,
  1557. notop: notop,
  1558. nobot: nobot,
  1559. renders: this.screen.renders
  1560. };
  1561. };
  1562. Element.prototype.render = function() {
  1563. this._emit('prerender');
  1564. this.parseContent();
  1565. var coords = this._getCoords(true);
  1566. if (!coords) {
  1567. delete this.lpos;
  1568. return;
  1569. }
  1570. if (coords.xl - coords.xi <= 0) {
  1571. coords.xl = Math.max(coords.xl, coords.xi);
  1572. return;
  1573. }
  1574. if (coords.yl - coords.yi <= 0) {
  1575. coords.yl = Math.max(coords.yl, coords.yi);
  1576. return;
  1577. }
  1578. var lines = this.screen.lines
  1579. , xi = coords.xi
  1580. , xl = coords.xl
  1581. , yi = coords.yi
  1582. , yl = coords.yl
  1583. , x
  1584. , y
  1585. , cell
  1586. , attr
  1587. , ch
  1588. , content = this._pcontent
  1589. , ci = this._clines.ci[coords.base]
  1590. , battr
  1591. , dattr
  1592. , c
  1593. , visible
  1594. , i
  1595. , bch = this.ch;
  1596. // Clip content if it's off the edge of the screen
  1597. // if (xi + this.ileft < 0 || yi + this.itop < 0) {
  1598. // var clines = this._clines.slice();
  1599. // if (xi + this.ileft < 0) {
  1600. // for (var i = 0; i < clines.length; i++) {
  1601. // var t = 0;
  1602. // var csi = '';
  1603. // var csis = '';
  1604. // for (var j = 0; j < clines[i].length; j++) {
  1605. // while (clines[i][j] === '\x1b') {
  1606. // csi = '\x1b';
  1607. // while (clines[i][j++] !== 'm') csi += clines[i][j];
  1608. // csis += csi;
  1609. // }
  1610. // if (++t === -(xi + this.ileft) + 1) break;
  1611. // }
  1612. // clines[i] = csis + clines[i].substring(j);
  1613. // }
  1614. // }
  1615. // if (yi + this.itop < 0) {
  1616. // clines = clines.slice(-(yi + this.itop));
  1617. // }
  1618. // content = clines.join('\n');
  1619. // }
  1620. if (coords.base >= this._clines.ci.length) {
  1621. ci = this._pcontent.length;
  1622. }
  1623. this.lpos = coords;
  1624. if (this.border && this.border.type === 'line') {
  1625. this.screen._borderStops[coords.yi] = true;
  1626. this.screen._borderStops[coords.yl - 1] = true;
  1627. // if (!this.screen._borderStops[coords.yi]) {
  1628. // this.screen._borderStops[coords.yi] = { xi: coords.xi, xl: coords.xl };
  1629. // } else {
  1630. // if (this.screen._borderStops[coords.yi].xi > coords.xi) {
  1631. // this.screen._borderStops[coords.yi].xi = coords.xi;
  1632. // }
  1633. // if (this.screen._borderStops[coords.yi].xl < coords.xl) {
  1634. // this.screen._borderStops[coords.yi].xl = coords.xl;
  1635. // }
  1636. // }
  1637. // this.screen._borderStops[coords.yl - 1] = this.screen._borderStops[coords.yi];
  1638. }
  1639. dattr = this.sattr(this.style);
  1640. attr = dattr;
  1641. // If we're in a scrollable text box, check to
  1642. // see which attributes this line starts with.
  1643. if (ci > 0) {
  1644. attr = this._clines.attr[Math.min(coords.base, this._clines.length - 1)];
  1645. }
  1646. if (this.border) xi++, xl--, yi++, yl--;
  1647. // If we have padding/valign, that means the
  1648. // content-drawing loop will skip a few cells/lines.
  1649. // To deal with this, we can just fill the whole thing
  1650. // ahead of time. This could be optimized.
  1651. if (this.tpadding || (this.valign && this.valign !== 'top')) {
  1652. if (this.style.transparent) {
  1653. for (y = Math.max(yi, 0); y < yl; y++) {
  1654. if (!lines[y]) break;
  1655. for (x = Math.max(xi, 0); x < xl; x++) {
  1656. if (!lines[y][x]) break;
  1657. lines[y][x][0] = colors.blend(attr, lines[y][x][0]);
  1658. // lines[y][x][1] = bch;
  1659. lines[y].dirty = true;
  1660. }
  1661. }
  1662. } else {
  1663. this.screen.fillRegion(dattr, bch, xi, xl, yi, yl);
  1664. }
  1665. }
  1666. if (this.tpadding) {
  1667. xi += this.padding.left, xl -= this.padding.right;
  1668. yi += this.padding.top, yl -= this.padding.bottom;
  1669. }
  1670. // Determine where to place the text if it's vertically aligned.
  1671. if (this.valign === 'middle' || this.valign === 'bottom') {
  1672. visible = yl - yi;
  1673. if (this._clines.length < visible) {
  1674. if (this.valign === 'middle') {
  1675. visible = visible / 2 | 0;
  1676. visible -= this._clines.length / 2 | 0;
  1677. } else if (this.valign === 'bottom') {
  1678. visible -= this._clines.length;
  1679. }
  1680. ci -= visible * (xl - xi);
  1681. }
  1682. }
  1683. // Draw the content and background.
  1684. for (y = yi; y < yl; y++) {
  1685. if (!lines[y]) {
  1686. if (y >= this.screen.height || yl < this.ibottom) {
  1687. break;
  1688. } else {
  1689. continue;
  1690. }
  1691. }
  1692. for (x = xi; x < xl; x++) {
  1693. cell = lines[y][x];
  1694. if (!cell) {
  1695. if (x >= this.screen.width || xl < this.iright) {
  1696. break;
  1697. } else {
  1698. continue;
  1699. }
  1700. }
  1701. ch = content[ci++] || bch;
  1702. // if (!content[ci] && !coords._contentEnd) {
  1703. // coords._contentEnd = { x: x - xi, y: y - yi };
  1704. // }
  1705. // Handle escape codes.
  1706. while (ch === '\x1b') {
  1707. if (c = /^\x1b\[[\d;]*m/.exec(content.substring(ci - 1))) {
  1708. ci += c[0].length - 1;
  1709. attr = this.screen.attrCode(c[0], attr, dattr);
  1710. // Ignore foreground changes for selected items.
  1711. if (this.parent._isList && this.parent.interactive
  1712. && this.parent.items[this.parent.selected] === this
  1713. && this.parent.options.invertSelected !== false) {
  1714. attr = (attr & ~(0x1ff << 9)) | (dattr & (0x1ff << 9));
  1715. }
  1716. ch = content[ci] || bch;
  1717. ci++;
  1718. } else {
  1719. break;
  1720. }
  1721. }
  1722. // Handle newlines.
  1723. if (ch === '\t') ch = bch;
  1724. if (ch === '\n') {
  1725. // If we're on the first cell and we find a newline and the last cell
  1726. // of the last line was not a newline, let's just treat this like the
  1727. // newline was already "counted".
  1728. if (x === xi && y !== yi && content[ci - 2] !== '\n') {
  1729. x--;
  1730. continue;
  1731. }
  1732. // We could use fillRegion here, name the
  1733. // outer loop, and continue to it instead.
  1734. ch = bch;
  1735. for (; x < xl; x++) {
  1736. cell = lines[y][x];
  1737. if (!cell) break;
  1738. if (this.style.transparent) {
  1739. lines[y][x][0] = colors.blend(attr, lines[y][x][0]);
  1740. if (content[ci]) lines[y][x][1] = ch;
  1741. lines[y].dirty = true;
  1742. } else {
  1743. if (attr !== cell[0] || ch !== cell[1]) {
  1744. lines[y][x][0] = attr;
  1745. lines[y][x][1] = ch;
  1746. lines[y].dirty = true;
  1747. }
  1748. }
  1749. }
  1750. continue;
  1751. }
  1752. if (this.screen.fullUnicode && content[ci - 1]) {
  1753. var point = unicode.codePointAt(content, ci - 1);
  1754. // Handle combining chars:
  1755. // Make sure they get in the same cell and are counted as 0.
  1756. if (unicode.combining[point]) {
  1757. if (point > 0x00ffff) {
  1758. ch = content[ci - 1] + content[ci];
  1759. ci++;
  1760. }
  1761. if (x - 1 >= xi) {
  1762. lines[y][x - 1][1] += ch;
  1763. } else if (y - 1 >= yi) {
  1764. lines[y - 1][xl - 1][1] += ch;
  1765. }
  1766. x--;
  1767. continue;
  1768. }
  1769. // Handle surrogate pairs:
  1770. // Make sure we put surrogate pair chars in one cell.
  1771. if (point > 0x00ffff) {
  1772. ch = content[ci - 1] + content[ci];
  1773. ci++;
  1774. }
  1775. }
  1776. if (this._noFill) continue;
  1777. if (this.style.transparent) {
  1778. lines[y][x][0] = colors.blend(attr, lines[y][x][0]);
  1779. if (content[ci]) lines[y][x][1] = ch;
  1780. lines[y].dirty = true;
  1781. } else {
  1782. if (attr !== cell[0] || ch !== cell[1]) {
  1783. lines[y][x][0] = attr;
  1784. lines[y][x][1] = ch;
  1785. lines[y].dirty = true;
  1786. }
  1787. }
  1788. }
  1789. }
  1790. // Draw the scrollbar.
  1791. // Could possibly draw this after all child elements.
  1792. if (this.scrollbar) {
  1793. // XXX
  1794. // i = this.getScrollHeight();
  1795. i = Math.max(this._clines.length, this._scrollBottom());
  1796. }
  1797. if (coords.notop || coords.nobot) i = -Infinity;
  1798. if (this.scrollbar && (yl - yi) < i) {
  1799. x = xl - 1;
  1800. if (this.scrollbar.ignoreBorder && this.border) x++;
  1801. if (this.alwaysScroll) {
  1802. y = this.childBase / (i - (yl - yi));
  1803. } else {
  1804. y = (this.childBase + this.childOffset) / (i - 1);
  1805. }
  1806. y = yi + ((yl - yi) * y | 0);
  1807. if (y >= yl) y = yl - 1;
  1808. cell = lines[y] && lines[y][x];
  1809. if (cell) {
  1810. if (this.track) {
  1811. ch = this.track.ch || ' ';
  1812. attr = this.sattr(this.style.track,
  1813. this.style.track.fg || this.style.fg,
  1814. this.style.track.bg || this.style.bg);
  1815. this.screen.fillRegion(attr, ch, x, x + 1, yi, yl);
  1816. }
  1817. ch = this.scrollbar.ch || ' ';
  1818. attr = this.sattr(this.style.scrollbar,
  1819. this.style.scrollbar.fg || this.style.fg,
  1820. this.style.scrollbar.bg || this.style.bg);
  1821. if (attr !== cell[0] || ch !== cell[1]) {
  1822. lines[y][x][0] = attr;
  1823. lines[y][x][1] = ch;
  1824. lines[y].dirty = true;
  1825. }
  1826. }
  1827. }
  1828. if (this.border) xi--, xl++, yi--, yl++;
  1829. if (this.tpadding) {
  1830. xi -= this.padding.left, xl += this.padding.right;
  1831. yi -= this.padding.top, yl += this.padding.bottom;
  1832. }
  1833. // Draw the border.
  1834. if (this.border) {
  1835. battr = this.sattr(this.style.border);
  1836. y = yi;
  1837. if (coords.notop) y = -1;
  1838. for (x = xi; x < xl; x++) {
  1839. if (!lines[y]) break;
  1840. if (coords.noleft && x === xi) continue;
  1841. if (coords.noright && x === xl - 1) continue;
  1842. cell = lines[y][x];
  1843. if (!cell) continue;
  1844. if (this.border.type === 'line') {
  1845. if (x === xi) {
  1846. ch = '\u250c'; // '┌'
  1847. if (!this.border.left) {
  1848. if (this.border.top) {
  1849. ch = '\u2500'; // '─'
  1850. } else {
  1851. continue;
  1852. }
  1853. } else {
  1854. if (!this.border.top) {
  1855. ch = '\u2502'; // '│'
  1856. }
  1857. }
  1858. } else if (x === xl - 1) {
  1859. ch = '\u2510'; // '┐'
  1860. if (!this.border.right) {
  1861. if (this.border.top) {
  1862. ch = '\u2500'; // '─'
  1863. } else {
  1864. continue;
  1865. }
  1866. } else {
  1867. if (!this.border.top) {
  1868. ch = '\u2502'; // '│'
  1869. }
  1870. }
  1871. } else {
  1872. ch = '\u2500'; // '─'
  1873. }
  1874. } else if (this.border.type === 'bg') {
  1875. ch = this.border.ch;
  1876. }
  1877. if (!this.border.top && x !== xi && x !== xl - 1) {
  1878. ch = ' ';
  1879. if (dattr !== cell[0] || ch !== cell[1]) {
  1880. lines[y][x][0] = dattr;
  1881. lines[y][x][1] = ch;
  1882. lines[y].dirty = true;
  1883. continue;
  1884. }
  1885. }
  1886. if (battr !== cell[0] || ch !== cell[1]) {
  1887. lines[y][x][0] = battr;
  1888. lines[y][x][1] = ch;
  1889. lines[y].dirty = true;
  1890. }
  1891. }
  1892. y = yi + 1;
  1893. for (; y < yl - 1; y++) {
  1894. if (!lines[y]) continue;
  1895. cell = lines[y][xi];
  1896. if (cell) {
  1897. if (this.border.left) {
  1898. if (this.border.type === 'line') {
  1899. ch = '\u2502'; // '│'
  1900. } else if (this.border.type === 'bg') {
  1901. ch = this.border.ch;
  1902. }
  1903. if (!coords.noleft)
  1904. if (battr !== cell[0] || ch !== cell[1]) {
  1905. lines[y][xi][0] = battr;
  1906. lines[y][xi][1] = ch;
  1907. lines[y].dirty = true;
  1908. }
  1909. } else {
  1910. ch = ' ';
  1911. if (dattr !== cell[0] || ch !== cell[1]) {
  1912. lines[y][xi][0] = dattr;
  1913. lines[y][xi][1] = ch;
  1914. lines[y].dirty = true;
  1915. }
  1916. }
  1917. }
  1918. cell = lines[y][xl - 1];
  1919. if (cell) {
  1920. if (this.border.right) {
  1921. if (this.border.type === 'line') {
  1922. ch = '\u2502'; // '│'
  1923. } else if (this.border.type === 'bg') {
  1924. ch = this.border.ch;
  1925. }
  1926. if (!coords.noright)
  1927. if (battr !== cell[0] || ch !== cell[1]) {
  1928. lines[y][xl - 1][0] = battr;
  1929. lines[y][xl - 1][1] = ch;
  1930. lines[y].dirty = true;
  1931. }
  1932. } else {
  1933. ch = ' ';
  1934. if (dattr !== cell[0] || ch !== cell[1]) {
  1935. lines[y][xl - 1][0] = dattr;
  1936. lines[y][xl - 1][1] = ch;
  1937. lines[y].dirty = true;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. y = yl - 1;
  1943. if (coords.nobot) y = -1;
  1944. for (x = xi; x < xl; x++) {
  1945. if (!lines[y]) break;
  1946. if (coords.noleft && x === xi) continue;
  1947. if (coords.noright && x === xl - 1) continue;
  1948. cell = lines[y][x];
  1949. if (!cell) continue;
  1950. if (this.border.type === 'line') {
  1951. if (x === xi) {
  1952. ch = '\u2514'; // '└'
  1953. if (!this.border.left) {
  1954. if (this.border.bottom) {
  1955. ch = '\u2500'; // '─'
  1956. } else {
  1957. continue;
  1958. }
  1959. } else {
  1960. if (!this.border.bottom) {
  1961. ch = '\u2502'; // '│'
  1962. }
  1963. }
  1964. } else if (x === xl - 1) {
  1965. ch = '\u2518'; // '┘'
  1966. if (!this.border.right) {
  1967. if (this.border.bottom) {
  1968. ch = '\u2500'; // '─'
  1969. } else {
  1970. continue;
  1971. }
  1972. } else {
  1973. if (!this.border.bottom) {
  1974. ch = '\u2502'; // '│'
  1975. }
  1976. }
  1977. } else {
  1978. ch = '\u2500'; // '─'
  1979. }
  1980. } else if (this.border.type === 'bg') {
  1981. ch = this.border.ch;
  1982. }
  1983. if (!this.border.bottom && x !== xi && x !== xl - 1) {
  1984. ch = ' ';
  1985. if (dattr !== cell[0] || ch !== cell[1]) {
  1986. lines[y][x][0] = dattr;
  1987. lines[y][x][1] = ch;
  1988. lines[y].dirty = true;
  1989. }
  1990. continue;
  1991. }
  1992. if (battr !== cell[0] || ch !== cell[1]) {
  1993. lines[y][x][0] = battr;
  1994. lines[y][x][1] = ch;
  1995. lines[y].dirty = true;
  1996. }
  1997. }
  1998. }
  1999. if (this.shadow) {
  2000. // right
  2001. y = Math.max(yi + 1, 0);
  2002. for (; y < yl + 1; y++) {
  2003. if (!lines[y]) break;
  2004. x = xl;
  2005. for (; x < xl + 2; x++) {
  2006. if (!lines[y][x]) break;
  2007. // lines[y][x][0] = colors.blend(this.dattr, lines[y][x][0]);
  2008. lines[y][x][0] = colors.blend(lines[y][x][0]);
  2009. lines[y].dirty = true;
  2010. }
  2011. }
  2012. // bottom
  2013. y = yl;
  2014. for (; y < yl + 1; y++) {
  2015. if (!lines[y]) break;
  2016. for (x = Math.max(xi + 1, 0); x < xl; x++) {
  2017. if (!lines[y][x]) break;
  2018. // lines[y][x][0] = colors.blend(this.dattr, lines[y][x][0]);
  2019. lines[y][x][0] = colors.blend(lines[y][x][0]);
  2020. lines[y].dirty = true;
  2021. }
  2022. }
  2023. }
  2024. this.children.forEach(function(el) {
  2025. if (el.screen._ci !== -1) {
  2026. el.index = el.screen._ci++;
  2027. }
  2028. // if (el.screen._rendering) {
  2029. // el._rendering = true;
  2030. // }
  2031. el.render();
  2032. // if (el.screen._rendering) {
  2033. // el._rendering = false;
  2034. // }
  2035. });
  2036. this._emit('render', [coords]);
  2037. return coords;
  2038. };
  2039. Element.prototype._render = Element.prototype.render;
  2040. /**
  2041. * Content Methods
  2042. */
  2043. Element.prototype.insertLine = function(i, line) {
  2044. if (typeof line === 'string') line = line.split('\n');
  2045. if (i !== i || i == null) {
  2046. i = this._clines.ftor.length;
  2047. }
  2048. i = Math.max(i, 0);
  2049. while (this._clines.fake.length < i) {
  2050. this._clines.fake.push('');
  2051. this._clines.ftor.push([this._clines.push('') - 1]);
  2052. this._clines.rtof(this._clines.fake.length - 1);
  2053. }
  2054. // NOTE: Could possibly compare the first and last ftor line numbers to see
  2055. // if they're the same, or if they fit in the visible region entirely.
  2056. var start = this._clines.length
  2057. , diff
  2058. , real;
  2059. if (i >= this._clines.ftor.length) {
  2060. real = this._clines.ftor[this._clines.ftor.length - 1];
  2061. real = real[real.length - 1] + 1;
  2062. } else {
  2063. real = this._clines.ftor[i][0];
  2064. }
  2065. for (var j = 0; j < line.length; j++) {
  2066. this._clines.fake.splice(i + j, 0, line[j]);
  2067. }
  2068. this.setContent(this._clines.fake.join('\n'), true);
  2069. diff = this._clines.length - start;
  2070. if (diff > 0) {
  2071. var pos = this._getCoords();
  2072. if (!pos) return;
  2073. var height = pos.yl - pos.yi - this.iheight
  2074. , base = this.childBase || 0
  2075. , visible = real >= base && real - base < height;
  2076. if (pos && visible && this.screen.cleanSides(this)) {
  2077. this.screen.insertLine(diff,
  2078. pos.yi + this.itop + real - base,
  2079. pos.yi,
  2080. pos.yl - this.ibottom - 1);
  2081. }
  2082. }
  2083. };
  2084. Element.prototype.deleteLine = function(i, n) {
  2085. n = n || 1;
  2086. if (i !== i || i == null) {
  2087. i = this._clines.ftor.length - 1;
  2088. }
  2089. i = Math.max(i, 0);
  2090. i = Math.min(i, this._clines.ftor.length - 1);
  2091. // NOTE: Could possibly compare the first and last ftor line numbers to see
  2092. // if they're the same, or if they fit in the visible region entirely.
  2093. var start = this._clines.length
  2094. , diff
  2095. , real = this._clines.ftor[i][0];
  2096. while (n--) {
  2097. this._clines.fake.splice(i, 1);
  2098. }
  2099. this.setContent(this._clines.fake.join('\n'), true);
  2100. diff = start - this._clines.length;
  2101. // XXX clearPos() without diff statement?
  2102. var height = 0;
  2103. if (diff > 0) {
  2104. var pos = this._getCoords();
  2105. if (!pos) return;
  2106. height = pos.yl - pos.yi - this.iheight;
  2107. var base = this.childBase || 0
  2108. , visible = real >= base && real - base < height;
  2109. if (pos && visible && this.screen.cleanSides(this)) {
  2110. this.screen.deleteLine(diff,
  2111. pos.yi + this.itop + real - base,
  2112. pos.yi,
  2113. pos.yl - this.ibottom - 1);
  2114. }
  2115. }
  2116. if (this._clines.length < height) {
  2117. this.clearPos();
  2118. }
  2119. };
  2120. Element.prototype.insertTop = function(line) {
  2121. var fake = this._clines.rtof[this.childBase || 0];
  2122. return this.insertLine(fake, line);
  2123. };
  2124. Element.prototype.insertBottom = function(line) {
  2125. var h = (this.childBase || 0) + this.height - this.iheight
  2126. , i = Math.min(h, this._clines.length)
  2127. , fake = this._clines.rtof[i - 1] + 1;
  2128. return this.insertLine(fake, line);
  2129. };
  2130. Element.prototype.deleteTop = function(n) {
  2131. var fake = this._clines.rtof[this.childBase || 0];
  2132. return this.deleteLine(fake, n);
  2133. };
  2134. Element.prototype.deleteBottom = function(n) {
  2135. var h = (this.childBase || 0) + this.height - 1 - this.iheight
  2136. , i = Math.min(h, this._clines.length - 1)
  2137. , fake = this._clines.rtof[i];
  2138. n = n || 1;
  2139. return this.deleteLine(fake - (n - 1), n);
  2140. };
  2141. Element.prototype.setLine = function(i, line) {
  2142. i = Math.max(i, 0);
  2143. while (this._clines.fake.length < i) {
  2144. this._clines.fake.push('');
  2145. }
  2146. this._clines.fake[i] = line;
  2147. return this.setContent(this._clines.fake.join('\n'), true);
  2148. };
  2149. Element.prototype.setBaseLine = function(i, line) {
  2150. var fake = this._clines.rtof[this.childBase || 0];
  2151. return this.setLine(fake + i, line);
  2152. };
  2153. Element.prototype.getLine = function(i) {
  2154. i = Math.max(i, 0);
  2155. i = Math.min(i, this._clines.fake.length - 1);
  2156. return this._clines.fake[i];
  2157. };
  2158. Element.prototype.getBaseLine = function(i) {
  2159. var fake = this._clines.rtof[this.childBase || 0];
  2160. return this.getLine(fake + i);
  2161. };
  2162. Element.prototype.clearLine = function(i) {
  2163. i = Math.min(i, this._clines.fake.length - 1);
  2164. return this.setLine(i, '');
  2165. };
  2166. Element.prototype.clearBaseLine = function(i) {
  2167. var fake = this._clines.rtof[this.childBase || 0];
  2168. return this.clearLine(fake + i);
  2169. };
  2170. Element.prototype.unshiftLine = function(line) {
  2171. return this.insertLine(0, line);
  2172. };
  2173. Element.prototype.shiftLine = function(n) {
  2174. return this.deleteLine(0, n);
  2175. };
  2176. Element.prototype.pushLine = function(line) {
  2177. if (!this.content) return this.setLine(0, line);
  2178. return this.insertLine(this._clines.fake.length, line);
  2179. };
  2180. Element.prototype.popLine = function(n) {
  2181. return this.deleteLine(this._clines.fake.length - 1, n);
  2182. };
  2183. Element.prototype.getLines = function() {
  2184. return this._clines.fake.slice();
  2185. };
  2186. Element.prototype.getScreenLines = function() {
  2187. return this._clines.slice();
  2188. };
  2189. Element.prototype.strWidth = function(text) {
  2190. text = this.parseTags
  2191. ? helpers.stripTags(text)
  2192. : text;
  2193. return this.screen.fullUnicode
  2194. ? unicode.strWidth(text)
  2195. : helpers.dropUnicode(text).length;
  2196. };
  2197. Element.prototype.screenshot = function(xi, xl, yi, yl) {
  2198. xi = this.lpos.xi + this.ileft + (xi || 0);
  2199. if (xl != null) {
  2200. xl = this.lpos.xi + this.ileft + (xl || 0);
  2201. } else {
  2202. xl = this.lpos.xl - this.iright;
  2203. }
  2204. yi = this.lpos.yi + this.itop + (yi || 0);
  2205. if (yl != null) {
  2206. yl = this.lpos.yi + this.itop + (yl || 0);
  2207. } else {
  2208. yl = this.lpos.yl - this.ibottom;
  2209. }
  2210. return this.screen.screenshot(xi, xl, yi, yl);
  2211. };
  2212. /**
  2213. * Expose
  2214. */
  2215. module.exports = Element;