|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var blessed = require("blessed"); |
| 4 | +var _ = require("lodash"); |
| 5 | +var BaseView = require("./base-view"); |
| 6 | + |
| 7 | +var BaseDetailsView = function BaseDetailsView(options) { |
| 8 | + BaseView.call(this, options); |
| 9 | + |
| 10 | + this.screen = options.parent.screen; |
| 11 | + this.node = blessed.box(this.layoutConfig); |
| 12 | + this.parent.append(this.node); |
| 13 | + |
| 14 | + this.refreshContent(); |
| 15 | + this.recalculatePosition(); |
| 16 | +}; |
| 17 | + |
| 18 | +BaseDetailsView.prototype = Object.create(BaseView.prototype); |
| 19 | + |
| 20 | +BaseDetailsView.prototype.refreshContent = function () { |
| 21 | + this.node.setContent(this._getBoxContent(this.getDetails())); |
| 22 | + this.screen.render(); |
| 23 | +}; |
| 24 | + |
| 25 | +BaseDetailsView.prototype.getDetails = function () { |
| 26 | + return []; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Given data and optional filters, return the content for a box. |
| 31 | + * |
| 32 | + * @param {Object[]} data |
| 33 | + * This is the array of label/data objects that define each data |
| 34 | + * point for the box. |
| 35 | + * |
| 36 | + * @returns {String} |
| 37 | + * The content string for the box is returned. |
| 38 | + */ |
| 39 | +BaseDetailsView.prototype._getBoxContent = function (data) { |
| 40 | + var longestLabel = _.reduce(data, function (prev, detail) { |
| 41 | + return Math.max(prev, detail.label.length); |
| 42 | + }, 0); |
| 43 | + |
| 44 | + var getFormattedContent = function (prev, details) { |
| 45 | + prev += "{cyan-fg}{bold}" + details.label + "{/}" |
| 46 | + + _.repeat(" ", longestLabel - details.label.length + 1) |
| 47 | + + "{green-fg}" + details.data + "{/}\n"; |
| 48 | + return prev; |
| 49 | + }; |
| 50 | + |
| 51 | + return _.trimEnd(_.reduce(data, getFormattedContent, ""), "\n"); |
| 52 | +}; |
| 53 | + |
| 54 | +module.exports = BaseDetailsView; |
0 commit comments