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;
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
/*
|
2015-04-27 22:04:41 +00:00
|
|
|
this.cachePositions = function() {
|
|
|
|
self.positionCacheExpired = false;
|
|
|
|
};
|
2015-07-01 04:45:27 +00:00
|
|
|
*/
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
this.updateSelection = function() {
|
2015-07-02 02:18:34 +00:00
|
|
|
//assert(!self.positionCacheExpired);
|
2015-05-05 04:04:36 +00:00
|
|
|
assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ToggleMenuView, MenuView);
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.redraw = function() {
|
|
|
|
ToggleMenuView.super_.prototype.redraw.call(this);
|
|
|
|
|
2015-07-01 04:45:27 +00:00
|
|
|
//this.cachePositions();
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2015-04-29 21:38:20 +00:00
|
|
|
this.client.term.write(this.hasFocus ? this.getFocusSGR() : this.getSGR());
|
2015-04-27 22:04:41 +00:00
|
|
|
|
|
|
|
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) {
|
2015-04-29 04:42:22 +00:00
|
|
|
//console.log(this.styleColor1)
|
2015-04-29 21:38:20 +00:00
|
|
|
//var sepColor = this.getANSIColor(this.styleColor1 || this.getColor());
|
|
|
|
//console.log(sepColor.substr(1))
|
2015-04-30 22:41:43 +00:00
|
|
|
//var sepColor = '\u001b[0m\u001b[1;30m'; // :TODO: FIX ME!!!
|
|
|
|
// :TODO: sepChar needs to be configurable!!!
|
|
|
|
this.client.term.write(this.styleSGR1 + ' / ');
|
|
|
|
//this.client.term.write(sepColor + ' / ');
|
2015-04-27 22:04:41 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:38:20 +00:00
|
|
|
this.client.term.write(i === this.focusedItemIndex ? this.getFocusSGR() : this.getSGR());
|
2015-04-27 22:04:41 +00:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
ToggleMenuView.prototype.onKeyPress = function(ch, key) {
|
|
|
|
if(key) {
|
|
|
|
var needsUpdate;
|
2015-07-02 02:18:34 +00:00
|
|
|
if(this.isKeyMapped('right', key.name) || this.isKeyMapped('down', key.name)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
if(this.items.length - 1 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = 0;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex++;
|
|
|
|
}
|
|
|
|
needsUpdate = true;
|
2015-07-02 02:18:34 +00:00
|
|
|
} else if(this.isKeyMapped('left', key.name) || this.isKeyMapped('up', key.name)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
if(0 === this.focusedItemIndex) {
|
|
|
|
this.focusedItemIndex = this.items.length - 1;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex--;
|
|
|
|
}
|
|
|
|
needsUpdate = true;
|
|
|
|
}
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
if(needsUpdate) {
|
|
|
|
this.updateSelection();
|
|
|
|
return;
|
|
|
|
}
|
2015-04-27 22:04:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
if(ch && this.hotKeys) {
|
|
|
|
var keyIndex = this.hotKeys[this.caseInsensitiveHotKeys ? ch.toLowerCase() : ch];
|
2015-07-02 02:18:34 +00:00
|
|
|
if(_.isNumber(keyIndex)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
this.focusedItemIndex = keyIndex;
|
|
|
|
this.updateSelection();
|
2015-04-27 22:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
ToggleMenuView.super_.prototype.onKeyPress.call(this, ch, key);
|
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)
|
|
|
|
};
|