2015-04-27 22:04:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const MenuView = require('./menu_view.js').MenuView;
|
|
|
|
const strUtil = require('./string_util.js');
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const util = require('util');
|
|
|
|
const assert = require('assert');
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.ToggleMenuView = ToggleMenuView;
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
function ToggleMenuView(options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.cursor = options.cursor || 'hide';
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
MenuView.call(this, options);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-24 04:23:22 +00:00
|
|
|
this.initDefaultWidth();
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
this.cachePositions = function() {
|
|
|
|
self.positionCacheExpired = false;
|
|
|
|
};
|
|
|
|
*/
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.updateSelection = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(this.focusedItemIndex >= 0 && this.focusedItemIndex <= self.items.length);
|
|
|
|
self.redraw();
|
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(ToggleMenuView, MenuView);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.redraw = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
ToggleMenuView.super_.prototype.redraw.call(this);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === this.items.length) {
|
2019-05-25 04:27:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//this.cachePositions();
|
|
|
|
|
|
|
|
this.client.term.write(this.hasFocus ? this.getFocusSGR() : this.getSGR());
|
|
|
|
|
2019-05-25 04:27:50 +00:00
|
|
|
assert(this.items.length === 2, 'ToggleMenuView must contain exactly (2) items');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var i = 0; i < 2; i++) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var item = this.items[i];
|
|
|
|
var text = strUtil.stylizeString(
|
2022-06-05 20:04:25 +00:00
|
|
|
item.text,
|
|
|
|
i === this.focusedItemIndex && this.hasFocus
|
|
|
|
? this.focusTextStyle
|
|
|
|
: this.textStyle
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (1 === i) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//console.log(this.styleColor1)
|
|
|
|
//var sepColor = this.getANSIColor(this.styleColor1 || this.getColor());
|
|
|
|
//console.log(sepColor.substr(1))
|
2018-06-23 03:26:46 +00:00
|
|
|
//var sepColor = '\u001b[0m\u001b[1;30m'; // :TODO: FIX ME!!!
|
|
|
|
// :TODO: sepChar needs to be configurable!!!
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.write(this.styleSGR1 + ' / ');
|
|
|
|
//this.client.term.write(sepColor + ' / ');
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.term.write(
|
|
|
|
i === this.focusedItemIndex ? this.getFocusSGR() : this.getSGR()
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.write(text);
|
|
|
|
}
|
2015-04-29 03:15:36 +00:00
|
|
|
};
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.setFocusItemIndex = function (index) {
|
|
|
|
ToggleMenuView.super_.prototype.setFocusItemIndex.call(this, index); // sets this.focusedItemIndex
|
2015-12-24 02:08:24 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.updateSelection();
|
2015-12-24 02:08:24 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 05:13:38 +00:00
|
|
|
ToggleMenuView.prototype.setTrue = function () {
|
2023-02-21 03:09:58 +00:00
|
|
|
this.setFocusItemIndex(1);
|
2023-01-31 05:13:38 +00:00
|
|
|
this.updateSelection();
|
|
|
|
};
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.setFalse = function () {
|
2023-02-21 03:09:58 +00:00
|
|
|
this.setFocusItemIndex(0);
|
2023-01-31 05:13:38 +00:00
|
|
|
this.updateSelection();
|
|
|
|
};
|
|
|
|
|
2023-02-12 03:27:50 +00:00
|
|
|
ToggleMenuView.prototype.isTrue = function () {
|
2023-02-21 03:09:58 +00:00
|
|
|
return this.focusedItemIndex === 1;
|
2023-02-12 03:27:50 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 05:13:38 +00:00
|
|
|
ToggleMenuView.prototype.setFromBoolean = function (bool) {
|
|
|
|
return bool ? this.setTrue() : this.setFalse();
|
|
|
|
};
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.setYes = function () {
|
|
|
|
return this.setTrue();
|
|
|
|
};
|
|
|
|
|
|
|
|
ToggleMenuView.prototype.setNo = function () {
|
|
|
|
return this.setFalse();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.setFocus = function (focused) {
|
2018-06-22 05:15:04 +00:00
|
|
|
ToggleMenuView.super_.prototype.setFocus.call(this, focused);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.redraw();
|
2015-04-27 22:04:41 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.focusNext = function () {
|
|
|
|
if (this.items.length - 1 === this.focusedItemIndex) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.focusedItemIndex = 0;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex++;
|
|
|
|
}
|
2017-01-02 04:53:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.updateSelection();
|
2017-01-02 04:53:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
ToggleMenuView.super_.prototype.focusNext.call(this);
|
2017-01-02 04:53:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.focusPrevious = function () {
|
|
|
|
if (0 === this.focusedItemIndex) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.focusedItemIndex = this.items.length - 1;
|
|
|
|
} else {
|
|
|
|
this.focusedItemIndex--;
|
|
|
|
}
|
2017-01-02 04:53:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.updateSelection();
|
2017-01-02 04:53:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
ToggleMenuView.super_.prototype.focusPrevious.call(this);
|
2017-01-02 04:53:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.onKeyPress = function (ch, key) {
|
|
|
|
if (key) {
|
|
|
|
if (this.isKeyMapped('right', key.name) || this.isKeyMapped('down', key.name)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.focusNext();
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (
|
|
|
|
this.isKeyMapped('left', key.name) ||
|
|
|
|
this.isKeyMapped('up', key.name)
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.focusPrevious();
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-22 05:15:04 +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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.getData = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.focusedItemIndex;
|
2015-04-27 22:04:41 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
ToggleMenuView.prototype.setItems = function (items) {
|
|
|
|
items = items.slice(0, 2); // switch/toggle only works with two elements
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
ToggleMenuView.super_.prototype.setItems.call(this, items);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.dimens.width = items.join(' / ').length; // :TODO: allow configurable seperator... string & color, e.g. styleColor1 (same as fillChar color)
|
2015-04-29 03:15:36 +00:00
|
|
|
};
|