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');
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2014-10-31 04:59:21 +00:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
2015-04-27 22:04:41 +00:00
|
|
|
var _ = require('lodash');
|
2014-10-31 04:59:21 +00:00
|
|
|
|
|
|
|
exports.MenuView = MenuView;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function MenuView(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
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
View.call(this, options);
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2014-11-01 15:50:11 +00:00
|
|
|
var self = this;
|
2014-10-31 22:25:11 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
if(options.items) {
|
|
|
|
this.setItems(options.items);
|
2014-11-02 19:07:17 +00:00
|
|
|
} else {
|
|
|
|
this.items = [];
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 23:27:23 +00:00
|
|
|
this.caseInsensitiveHotKeys = miscUtil.valueWithDefault(options.caseInsensitiveHotKeys, true);
|
|
|
|
|
|
|
|
this.setHotKeys(options.hotKeys);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
this.focusedItemIndex = options.focusedItemIndex || 0;
|
2014-11-01 15:50:11 +00:00
|
|
|
this.focusedItemIndex = this.items.length >= this.focusedItemIndex ? this.focusedItemIndex : 0;
|
|
|
|
|
2015-05-04 21:39:48 +00:00
|
|
|
this.itemSpacing = _.isNumber(options.itemSpacing) ? options.itemSpacing : 0;
|
2014-10-31 22:25:11 +00:00
|
|
|
|
2015-05-06 04:19:21 +00:00
|
|
|
// :TODO: probably just replace this with owner draw / pipe codes / etc. more control, less specialization
|
2015-04-09 04:54:13 +00:00
|
|
|
this.focusPrefix = options.focusPrefix || '';
|
|
|
|
this.focusSuffix = options.focusSuffix || '';
|
2014-11-02 19:07:17 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
|
|
|
|
this.justify = options.justify || 'none';
|
2015-04-27 22:04:41 +00:00
|
|
|
/*
|
2014-11-03 23:49:15 +00:00
|
|
|
this.moveSelection = function(fromIndex, toIndex) {
|
2014-11-04 07:34:54 +00:00
|
|
|
assert(!self.positionCacheExpired);
|
2014-11-03 23:49:15 +00:00
|
|
|
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);
|
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
*/
|
2014-11-03 23:49:15 +00:00
|
|
|
|
2014-11-04 07:34:54 +00:00
|
|
|
/*
|
2014-11-03 23:49:15 +00:00
|
|
|
this.cachePositions = function() {
|
|
|
|
// :TODO: implement me!
|
|
|
|
};
|
|
|
|
|
|
|
|
this.drawItem = function(index) {
|
|
|
|
// :TODO: implement me!
|
2014-11-04 07:34:54 +00:00
|
|
|
};*/
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(MenuView, View);
|
|
|
|
|
2015-04-27 03:57:23 +00:00
|
|
|
MenuView.prototype.redrawAllItems = function() {
|
2014-11-03 23:49:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2015-04-27 03:57:23 +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-03 23:49:15 +00:00
|
|
|
|
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({
|
2015-04-27 03:57:23 +00:00
|
|
|
text : itemText
|
2014-11-02 19:07:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-04 21:39:48 +00:00
|
|
|
MenuView.prototype.setItemSpacing = function(itemSpacing) {
|
|
|
|
assert(_.isNumber(itemSpacing));
|
|
|
|
|
|
|
|
this.itemSpacing = itemSpacing;
|
|
|
|
this.positionCacheExpired = true;
|
|
|
|
};
|
|
|
|
|
2015-04-27 23:27:23 +00:00
|
|
|
MenuView.prototype.setHotKeys = function(hotKeys) {
|
|
|
|
if(_.isObject(hotKeys)) {
|
|
|
|
if(this.caseInsensitiveHotKeys) {
|
|
|
|
this.hotKeys = {};
|
|
|
|
for(var key in hotKeys) {
|
|
|
|
this.hotKeys[key.toLowerCase()] = hotKeys[key];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.hotKeys = hotKeys;
|
2015-06-05 22:20:26 +00:00
|
|
|
}
|
2015-04-27 22:04:41 +00:00
|
|
|
}
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
|