listtable.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * listtable.js - list table 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 Node = require('./node');
  10. var Box = require('./box');
  11. var List = require('./list');
  12. var Table = require('./table');
  13. /**
  14. * ListTable
  15. */
  16. function ListTable(options) {
  17. var self = this;
  18. if (!(this instanceof Node)) {
  19. return new ListTable(options);
  20. }
  21. options = options || {};
  22. options.shrink = true;
  23. options.normalShrink = true;
  24. options.style = options.style || {};
  25. options.style.border = options.style.border || {};
  26. options.style.header = options.style.header || {};
  27. options.style.cell = options.style.cell || {};
  28. this.__align = options.align || 'center';
  29. delete options.align;
  30. options.style.selected = options.style.cell.selected;
  31. options.style.item = options.style.cell;
  32. List.call(this, options);
  33. this._header = new Box({
  34. parent: this,
  35. left: this.screen.autoPadding ? 0 : this.ileft,
  36. top: 0,
  37. width: 'shrink',
  38. height: 1,
  39. style: options.style.header,
  40. tags: options.parseTags || options.tags
  41. });
  42. this.on('scroll', function() {
  43. self._header.setFront();
  44. self._header.rtop = self.childBase;
  45. if (!self.screen.autoPadding) {
  46. self._header.rtop = self.childBase + (self.border ? 1 : 0);
  47. }
  48. });
  49. this.pad = options.pad != null
  50. ? options.pad
  51. : 2;
  52. this.setData(options.rows || options.data);
  53. this.on('attach', function() {
  54. self.setData(self.rows);
  55. });
  56. this.on('resize', function() {
  57. var selected = self.selected;
  58. self.setData(self.rows);
  59. self.select(selected);
  60. self.screen.render();
  61. });
  62. }
  63. ListTable.prototype.__proto__ = List.prototype;
  64. ListTable.prototype.type = 'list-table';
  65. ListTable.prototype._calculateMaxes = Table.prototype._calculateMaxes;
  66. ListTable.prototype.setRows =
  67. ListTable.prototype.setData = function(rows) {
  68. var self = this
  69. , align = this.__align;
  70. if (this.visible && this.lpos) {
  71. this.clearPos();
  72. }
  73. this.clearItems();
  74. this.rows = rows || [];
  75. this._calculateMaxes();
  76. if (!this._maxes) return;
  77. this.addItem('');
  78. this.rows.forEach(function(row, i) {
  79. var isHeader = i === 0;
  80. var text = '';
  81. row.forEach(function(cell, i) {
  82. var width = self._maxes[i];
  83. var clen = self.strWidth(cell);
  84. if (i !== 0) {
  85. text += ' ';
  86. }
  87. while (clen < width) {
  88. if (align === 'center') {
  89. cell = ' ' + cell + ' ';
  90. clen += 2;
  91. } else if (align === 'left') {
  92. cell = cell + ' ';
  93. clen += 1;
  94. } else if (align === 'right') {
  95. cell = ' ' + cell;
  96. clen += 1;
  97. }
  98. }
  99. if (clen > width) {
  100. if (align === 'center') {
  101. cell = cell.substring(1);
  102. clen--;
  103. } else if (align === 'left') {
  104. cell = cell.slice(0, -1);
  105. clen--;
  106. } else if (align === 'right') {
  107. cell = cell.substring(1);
  108. clen--;
  109. }
  110. }
  111. text += cell;
  112. });
  113. if (isHeader) {
  114. self._header.setContent(text);
  115. } else {
  116. self.addItem(text);
  117. }
  118. });
  119. this._header.setFront();
  120. this.select(0);
  121. };
  122. ListTable.prototype._select = ListTable.prototype.select;
  123. ListTable.prototype.select = function(i) {
  124. if (i === 0) {
  125. i = 1;
  126. }
  127. if (i <= this.childBase) {
  128. this.setScroll(this.childBase - 1);
  129. }
  130. return this._select(i);
  131. };
  132. ListTable.prototype.render = function() {
  133. var self = this;
  134. var coords = this._render();
  135. if (!coords) return;
  136. this._calculateMaxes();
  137. if (!this._maxes) return coords;
  138. var lines = this.screen.lines
  139. , xi = coords.xi
  140. , yi = coords.yi
  141. , rx
  142. , ry
  143. , i;
  144. var battr = this.sattr(this.style.border);
  145. var height = coords.yl - coords.yi - this.ibottom;
  146. if (!this.border || this.options.noCellBorders) return coords;
  147. // Draw border with correct angles.
  148. ry = 0;
  149. for (i = 0; i < height + 1; i++) {
  150. if (!lines[yi + ry]) break;
  151. rx = 0;
  152. self._maxes.slice(0, -1).forEach(function(max) {
  153. rx += max;
  154. if (!lines[yi + ry][xi + rx + 1]) return;
  155. // center
  156. if (ry === 0) {
  157. // top
  158. rx++;
  159. lines[yi + ry][xi + rx][0] = battr;
  160. lines[yi + ry][xi + rx][1] = '\u252c'; // '┬'
  161. // XXX If we alter iheight and itop for no borders - nothing should be written here
  162. if (!self.border.top) {
  163. lines[yi + ry][xi + rx][1] = '\u2502'; // '│'
  164. }
  165. lines[yi + ry].dirty = true;
  166. } else if (ry === height) {
  167. // bottom
  168. rx++;
  169. lines[yi + ry][xi + rx][0] = battr;
  170. lines[yi + ry][xi + rx][1] = '\u2534'; // '┴'
  171. // XXX If we alter iheight and ibottom for no borders - nothing should be written here
  172. if (!self.border.bottom) {
  173. lines[yi + ry][xi + rx][1] = '\u2502'; // '│'
  174. }
  175. lines[yi + ry].dirty = true;
  176. } else {
  177. // middle
  178. rx++;
  179. }
  180. });
  181. ry += 1;
  182. }
  183. // Draw internal borders.
  184. for (ry = 1; ry < height; ry++) {
  185. if (!lines[yi + ry]) break;
  186. rx = 0;
  187. self._maxes.slice(0, -1).forEach(function(max) {
  188. rx += max;
  189. if (!lines[yi + ry][xi + rx + 1]) return;
  190. if (self.options.fillCellBorders !== false) {
  191. var lbg = lines[yi + ry][xi + rx][0] & 0x1ff;
  192. rx++;
  193. lines[yi + ry][xi + rx][0] = (battr & ~0x1ff) | lbg;
  194. } else {
  195. rx++;
  196. lines[yi + ry][xi + rx][0] = battr;
  197. }
  198. lines[yi + ry][xi + rx][1] = '\u2502'; // '│'
  199. lines[yi + ry].dirty = true;
  200. });
  201. }
  202. return coords;
  203. };
  204. /**
  205. * Expose
  206. */
  207. module.exports = ListTable;