2014-10-31 04:59:21 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var View = require('./view.js').View;
|
|
|
|
var ansi = require('./ansi_term.js');
|
2014-10-31 22:25:11 +00:00
|
|
|
var miscUtil = require('./misc_util.js');
|
2014-10-31 04:59:21 +00:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
exports.MenuView = MenuView;
|
|
|
|
|
|
|
|
function MenuView(client, options) {
|
2014-10-31 22:25:11 +00:00
|
|
|
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
2014-11-01 15:50:11 +00:00
|
|
|
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
2014-10-31 22:25:11 +00:00
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
View.call(this, client, options);
|
|
|
|
|
2014-11-01 15:50:11 +00:00
|
|
|
var self = this;
|
2014-10-31 22:25:11 +00:00
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
if(this.options.items) {
|
2014-11-02 19:07:17 +00:00
|
|
|
this.setItems(this.options.items);
|
|
|
|
} else {
|
|
|
|
this.items = [];
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-01 15:50:11 +00:00
|
|
|
this.focusedItemIndex = this.options.focusedItemIndex || 0;
|
|
|
|
this.focusedItemIndex = this.items.length >= this.focusedItemIndex ? this.focusedItemIndex : 0;
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
this.itemSpacing = this.options.itemSpacing || 1;
|
2014-10-31 22:25:11 +00:00
|
|
|
this.itemSpacing = parseInt(this.itemSpacing, 10);
|
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
this.focusPrefix = this.options.focusPrefix || '';
|
|
|
|
this.focusSuffix = this.options.focusSuffix || '';
|
2014-11-02 19:07:17 +00:00
|
|
|
|
|
|
|
this.fillChar = miscUtil.valueWithDefault(this.options.fillChar, ' ').substr(0, 1);
|
|
|
|
this.justify = this.options.justify || 'none';
|
2014-11-03 23:49:15 +00:00
|
|
|
|
|
|
|
this.moveSelection = function(fromIndex, toIndex) {
|
|
|
|
assert(!self.xPositionCacheExpired);
|
|
|
|
assert(fromIndex >= 0 && fromIndex <= self.items.length);
|
|
|
|
assert(toIndex >= 0 && toIndex <= self.items.length);
|
|
|
|
|
|
|
|
self.items[fromIndex].focused = false;
|
|
|
|
self.drawItem(fromIndex);
|
|
|
|
|
|
|
|
self.items[toIndex].focused = true;
|
|
|
|
self.focusedItemIndex = toIndex;
|
|
|
|
self.drawItem(toIndex);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cachePositions = function() {
|
|
|
|
// :TODO: implement me!
|
|
|
|
};
|
|
|
|
|
|
|
|
this.drawItem = function(index) {
|
|
|
|
// :TODO: implement me!
|
|
|
|
};
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(MenuView, View);
|
|
|
|
|
2014-11-03 23:49:15 +00:00
|
|
|
MenuView.prototype.redraw = function() {
|
|
|
|
MenuView.super_.prototype.redraw.call(this);
|
|
|
|
|
|
|
|
this.cachePositions();
|
|
|
|
|
|
|
|
var count = this.items.length;
|
|
|
|
for(var i = 0; i < count; ++i) {
|
|
|
|
this.items[i].focused = this.focusedItemIndex === i;
|
|
|
|
this.drawItem(i);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-02 19:07:17 +00:00
|
|
|
MenuView.prototype.setItems = function(items) {
|
|
|
|
var self = this;
|
|
|
|
if(items) {
|
|
|
|
this.items = []; // :TODO: better way?
|
|
|
|
items.forEach(function onItem(itemText) {
|
|
|
|
self.items.push({
|
|
|
|
text : itemText,
|
|
|
|
selected : false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|