我正在尝试覆盖blessed-contrib 树模块中的一个函数。但是我认为我在对象继承方面遗漏了一些东西。任何帮助将不胜感激。
原始代码:
function Tree(options) {
//...
this.rows = blessed.list({
top: 1,
width: 0,
left: 1,
style: options.style,
padding: options.padding,
keys: true,
tags: options.tags,
input: options.input,
vi: options.vi,
ignoreKeys: options.ignoreKeys,
scrollable: options.scrollable,
mouse: options.mouse,
selectedBg: 'blue',
});
this.append(this.rows);
this.rows.key(options.keys, function() {
var selectedNode = self.nodeLines[this.getItemIndex(this.selected)];
if (selectedNode.children) {
selectedNode.extended = !selectedNode.extended;
self.setData(self.data);
self.screen.render();
}
self.emit('select', selectedNode, this.getItemIndex(this.selected));
});
//...
我正在尝试this.rows.key(options.keys, function() {
在我自己的代码中覆盖该函数。我正在尝试执行以下操作,但我不确定在这种情况下对象路径如何用于最终属于blessed-contrib.tree.list 类型的东西。
我的代码看起来像这样:
"use strict";
var blessed = require('blessed'),
contrib = require('blessed-contrib');
//...
//create layout and widgets
var grid = new contrib.grid({rows: 1, cols: 2, screen: screen})
var tree = grid.set(0, 0, 1, 1, contrib.tree,
{
style: {
text: "red", fg: 'blue',
selected: {
bg: 'yellow', fg: 'white'
}
},
// keys: ['+', 'space'],
vi: true,
template: { lines: true },
label: 'Filesystem Tree'
})
// monkeypatch contrib.tree.rows.key(options.keys, function() {}
// save the original
var old_tree_rows_key = tree.rows.key;
//tree.rows.key = function(options_keys) {
tree.rows.key = function(options_keys) {
var selectedNode = self.nodeLines[this.getItemIndex(this.selected)];
// handle items in options.keys array on my own for custom purposes
// ...
// Code I want commented out:
// if (selectedNode.children) {
// selectedNode.extended = !selectedNode.extended;
// self.setData(self.data);
// self.screen.render();
// }
self.emit('select', selectedNode, this.getItemIndex(this.selected));
};
//...