2015-04-27 22:04:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var MenuView = require('./menu_view.js').MenuView;
|
|
|
|
var ansi = require('./ansi_term.js');
|
|
|
|
var strUtil = require('./string_util.js');
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
exports.ToggleMenuView = ToggleMenuView;
|
|
|
|
|
|
|
|
function ToggleMenuView (options) {
|
|
|
|
options.cursor = options.cursor || 'hide';
|
|
|
|
|
|
|
|
MenuView.call(this, options);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.cachePositions = function() {
|
|
|
|
self.positionCacheExpired = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.updateSelection = function() {
|
|
|
|
assert(!self.positionCacheExpired);
|
|
|
|
assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length);
|
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ToggleMenuView, MenuView);
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.redraw = function() {
|
|
|
|
ToggleMenuView.super_.prototype.redraw.call(this);
|
|
|
|
|
|
|
|
this.cachePositions();
|
|
|
|
|
|
|
|
this.client.term.write(this.getANSIColor(this.hasFocus ? this.getFocusColor() : this.getColor()));
|
|
|
|
|
|
|
|
assert(this.items.length === 2);
|
|
|
|
for(var i = 0; i < 2; i++) {
|
|
|
|
var item = this.items[i];
|
2015-04-29 03:15:36 +00:00
|
|
|
var text = strUtil.stylizeString(
|
|
|
|
item.text, i === this.focusedItemIndex && this.hasFocus ? this.focusTextStyle : this.textStyle);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
if(1 === i) {
|
|
|
|
this.client.term.write(this.getANSIColor(this.getColor()) + ' / '); // :TODO: We need a color for this!!!
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client.term.write(this.getANSIColor(i === this.focusedItemIndex ? this.getFocusColor() : this.getColor()));
|
|
|
|
this.client.term.write(text);
|
|
|
|
}
|
2015-04-29 03:15:36 +00:00
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
ToggleMenuView.prototype.setFocus = function(focused) {
|
|
|
|
ToggleMenuView.super_.prototype.setFocus.call(this, focused);
|
|
|
|
|
|
|
|
this.redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.onKeyPress = function(key, isSpecial) {
|
2015-04-27 23:27:23 +00:00
|
|
|
if(isSpecial || !this.hotKeys) {
|
2015-04-27 22:04:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(1 === key.length);
|
|
|
|
|
2015-04-27 23:27:23 +00:00
|
|
|
var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? key.toLowerCase() : key];
|
2015-04-27 22:04:41 +00:00
|
|
|
if(!_.isUndefined(keyIndex)) {
|
|
|
|
this.focusedItemIndex = keyIndex;
|
|
|
|
this.updateSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
ToggleMenuView.super_.prototype.onKeyPress.call(this, key, isSpecial);
|
2015-04-29 03:15:36 +00:00
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
ToggleMenuView.prototype.onSpecialKeyPress = function(keyName) {
|
|
|
|
|
|
|
|
if(this.isSpecialKeyMapped('right', keyName) || this.isSpecialKeyMapped('down', keyName)) {
|
|
|
|
if(this.items.length - 1 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = 0;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex++;
|
|
|
|
}
|
|
|
|
} else if(this.isSpecialKeyMapped('left', keyName) || this.isSpecialKeyMapped('up', keyName)) {
|
|
|
|
if(0 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = this.items.length - 1;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateSelection();
|
|
|
|
|
|
|
|
ToggleMenuView.super_.prototype.onSpecialKeyPress.call(this, keyName);
|
2015-04-29 03:15:36 +00:00
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
ToggleMenuView.prototype.getData = function() {
|
|
|
|
return this.focusedItemIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.setItems = function(items) {
|
|
|
|
ToggleMenuView.super_.prototype.setItems.call(this, items);
|
|
|
|
|
|
|
|
this.items = this.items.splice(0, 2); // switch/toggle only works with two elements
|
|
|
|
|
2015-04-29 03:15:36 +00:00
|
|
|
this.dimens.width = this.items.join(' / ').length; // :TODO: allow configurable seperator... string & color, e.g. styleColor1 (same as fillChar color)
|
|
|
|
};
|