2014-10-31 04:59:21 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const View = require('./view.js').View;
|
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const pipeToAnsi = require('./color_codes.js').pipeToAnsi;
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const util = require('util');
|
|
|
|
const assert = require('assert');
|
|
|
|
const _ = require('lodash');
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.MenuView = MenuView;
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function MenuView(options) {
|
2022-01-23 16:46:09 +00:00
|
|
|
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
|
|
|
|
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
View.call(this, options);
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.disablePipe = options.disablePipe || false;
|
2016-08-11 04:48:13 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
const self = this;
|
2014-10-31 22:25:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.items) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.setItems(options.items);
|
|
|
|
} else {
|
|
|
|
this.items = [];
|
|
|
|
}
|
2014-10-31 04:59:21 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.renderCache = {};
|
2018-02-02 03:29:26 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.caseInsensitiveHotKeys = miscUtil.valueWithDefault(
|
|
|
|
options.caseInsensitiveHotKeys,
|
|
|
|
true
|
|
|
|
);
|
2015-04-27 23:27:23 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.setHotKeys(options.hotKeys);
|
2015-04-27 22:04:41 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.focusedItemIndex = options.focusedItemIndex || 0;
|
2022-06-05 20:04:25 +00:00
|
|
|
this.focusedItemIndex =
|
|
|
|
this.items.length >= this.focusedItemIndex ? this.focusedItemIndex : 0;
|
2014-11-01 15:50:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.itemSpacing = _.isNumber(options.itemSpacing) ? options.itemSpacing : 0;
|
|
|
|
this.itemHorizSpacing = _.isNumber(options.itemHorizSpacing)
|
|
|
|
? options.itemHorizSpacing
|
|
|
|
: 0;
|
2014-10-31 22:25:11 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
// :TODO: probably just replace this with owner draw / pipe codes / etc. more control, less specialization
|
2022-06-05 20:04:25 +00:00
|
|
|
this.focusPrefix = options.focusPrefix || '';
|
|
|
|
this.focusSuffix = options.focusSuffix || '';
|
2014-11-02 19:07:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
|
2015-08-26 05:17:09 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.hasFocusItems = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
return !_.isUndefined(self.focusItems);
|
|
|
|
};
|
2015-12-24 02:08:24 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getHotKeyItemIndex = function (ch) {
|
|
|
|
if (ch && self.hotKeys) {
|
|
|
|
const keyIndex =
|
|
|
|
self.hotKeys[self.caseInsensitiveHotKeys ? ch.toLowerCase() : ch];
|
|
|
|
if (_.isNumber(keyIndex)) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return keyIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
2018-06-10 04:45:01 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.emitIndexUpdate = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
self.emit('index update', self.focusedItemIndex);
|
|
|
|
};
|
2014-10-31 04:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(MenuView, View);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setTextOverflow = function (overflow) {
|
|
|
|
this.textOverflow = overflow;
|
|
|
|
this.invalidateRenderCache();
|
|
|
|
};
|
2022-01-19 19:07:02 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.hasTextOverflow = function () {
|
|
|
|
return this.textOverflow != undefined;
|
|
|
|
};
|
2022-01-19 19:07:02 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setItems = function (items) {
|
|
|
|
if (Array.isArray(items)) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.sorted = false;
|
|
|
|
this.renderCache = {};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Items can be an array of strings or an array of objects.
|
|
|
|
//
|
|
|
|
// In the case of objects, items are considered complex and
|
|
|
|
// may have one or more members that can later be formatted
|
|
|
|
// against. The default member is 'text'. The member 'data'
|
|
|
|
// may be overridden to provide a form value other than the
|
|
|
|
// item's index.
|
|
|
|
//
|
|
|
|
// Items can be formatted with 'itemFormat' and 'focusItemFormat'
|
|
|
|
//
|
|
|
|
let text;
|
|
|
|
let stringItem;
|
|
|
|
this.items = items.map(item => {
|
|
|
|
stringItem = _.isString(item);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (stringItem) {
|
2022-01-23 16:46:09 +00:00
|
|
|
text = item;
|
|
|
|
} else {
|
|
|
|
text = item.text || '';
|
|
|
|
this.complexItems = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
text = this.disablePipe ? text : pipeToAnsi(text, this.client);
|
2022-06-05 20:04:25 +00:00
|
|
|
return Object.assign({}, { text }, stringItem ? {} : item); // ensure we have a text member, plus any others
|
2022-01-23 16:46:09 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.complexItems) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.itemFormat = this.itemFormat || '{text}';
|
|
|
|
}
|
2022-01-16 18:12:41 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.invalidateRenderCache();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2014-11-02 19:07:17 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.getRenderCacheItem = function (index, focusItem = false) {
|
2022-01-23 16:46:09 +00:00
|
|
|
const item = this.renderCache[index];
|
|
|
|
return item && item[focusItem ? 'focus' : 'standard'];
|
2018-02-02 03:29:26 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.removeRenderCacheItem = function (index) {
|
2022-01-23 16:46:09 +00:00
|
|
|
delete this.renderCache[index];
|
2018-07-05 00:48:35 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setRenderCacheItem = function (index, rendered, focusItem = false) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.renderCache[index] = this.renderCache[index] || {};
|
|
|
|
this.renderCache[index][focusItem ? 'focus' : 'standard'] = rendered;
|
2018-02-02 03:29:26 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.invalidateRenderCache = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.renderCache = {};
|
2018-07-05 00:48:35 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setSort = function (sort) {
|
|
|
|
if (this.sorted || !Array.isArray(this.items) || 0 === this.items.length) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-01-23 16:46:09 +00:00
|
|
|
|
|
|
|
const key = true === sort ? 'text' : sort;
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('text' !== sort && !this.complexItems) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return; // need a valid sort key
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.items.sort((a, b) => {
|
2022-01-23 16:46:09 +00:00
|
|
|
const a1 = a[key];
|
|
|
|
const b1 = b[key];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!a1) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!b1) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return a1.localeCompare(b1, { sensitivity: false, numeric: true });
|
2022-01-23 16:46:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.sorted = true;
|
2018-01-28 20:03:11 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.removeItem = function (index) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.sorted = false;
|
|
|
|
this.items.splice(index, 1);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.focusItems) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.focusItems.splice(index, 1);
|
|
|
|
}
|
2017-02-02 02:42:27 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.focusedItemIndex >= index) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.focusedItemIndex = Math.max(this.focusedItemIndex - 1, 0);
|
|
|
|
}
|
2017-02-02 02:42:27 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.removeRenderCacheItem(index);
|
2018-07-05 00:48:35 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
this.positionCacheExpired = true;
|
2017-02-02 02:42:27 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.getCount = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
return this.items.length;
|
2016-01-05 06:32:43 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.getItems = function () {
|
|
|
|
if (this.complexItems) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return this.items;
|
|
|
|
}
|
2018-01-28 05:21:48 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.items.map(item => {
|
2022-01-23 16:46:09 +00:00
|
|
|
return item.text;
|
|
|
|
});
|
2016-01-05 06:32:43 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.getItem = function (index) {
|
|
|
|
if (this.complexItems) {
|
2022-01-23 16:46:09 +00:00
|
|
|
return this.items[index];
|
|
|
|
}
|
2018-01-28 05:21:48 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
return this.items[index].text;
|
2015-09-16 04:44:31 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.focusNext = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emitIndexUpdate();
|
2015-09-18 04:53:19 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.focusPrevious = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emitIndexUpdate();
|
2015-09-18 04:53:19 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.focusNextPageItem = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emitIndexUpdate();
|
2017-09-01 23:41:08 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.focusPreviousPageItem = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emitIndexUpdate();
|
2018-06-10 04:45:01 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.focusFirst = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emitIndexUpdate();
|
2018-06-10 04:45:01 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.focusLast = function () {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emitIndexUpdate();
|
2017-09-01 23:41:08 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setFocusItemIndex = function (index) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.focusedItemIndex = index;
|
2015-12-24 02:08:24 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.onKeyPress = function (ch, key) {
|
2022-01-23 16:46:09 +00:00
|
|
|
const itemIndex = this.getHotKeyItemIndex(ch);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (itemIndex >= 0) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.setFocusItemIndex(itemIndex);
|
2015-12-24 02:08:24 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === this.hotKeySubmit) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.emit('action', 'accept');
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-12-24 02:08:24 +00:00
|
|
|
|
2022-01-23 16:46:09 +00:00
|
|
|
MenuView.super_.prototype.onKeyPress.call(this, ch, key);
|
2015-12-24 02:08:24 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setFocusItems = function (items) {
|
2022-01-23 16:46:09 +00:00
|
|
|
const self = this;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (items) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.focusItems = [];
|
2022-06-05 20:04:25 +00:00
|
|
|
items.forEach(itemText => {
|
|
|
|
this.focusItems.push({
|
|
|
|
text: self.disablePipe ? itemText : pipeToAnsi(itemText, self.client),
|
|
|
|
});
|
2022-01-23 16:46:09 +00:00
|
|
|
});
|
|
|
|
}
|
2015-10-11 22:05:45 +00:00
|
|
|
};
|
2015-08-26 05:17:09 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setItemSpacing = function (itemSpacing) {
|
2022-01-23 16:46:09 +00:00
|
|
|
itemSpacing = parseInt(itemSpacing);
|
|
|
|
assert(_.isNumber(itemSpacing));
|
2015-05-04 21:39:48 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.itemSpacing = itemSpacing;
|
|
|
|
this.positionCacheExpired = true;
|
2015-05-04 21:39:48 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setItemHorizSpacing = function (itemHorizSpacing) {
|
2022-01-23 16:46:09 +00:00
|
|
|
itemHorizSpacing = parseInt(itemHorizSpacing);
|
|
|
|
assert(_.isNumber(itemHorizSpacing));
|
2022-01-12 16:38:47 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.itemHorizSpacing = itemHorizSpacing;
|
|
|
|
this.positionCacheExpired = true;
|
2022-01-12 16:38:47 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setPropertyValue = function (propName, value) {
|
|
|
|
switch (propName) {
|
|
|
|
case 'itemSpacing':
|
|
|
|
this.setItemSpacing(value);
|
|
|
|
break;
|
|
|
|
case 'itemHorizSpacing':
|
|
|
|
this.setItemHorizSpacing(value);
|
|
|
|
break;
|
|
|
|
case 'items':
|
|
|
|
this.setItems(value);
|
|
|
|
break;
|
|
|
|
case 'focusItems':
|
|
|
|
this.setFocusItems(value);
|
|
|
|
break;
|
|
|
|
case 'hotKeys':
|
|
|
|
this.setHotKeys(value);
|
|
|
|
break;
|
|
|
|
case 'textOverflow':
|
|
|
|
this.setTextOverflow(value);
|
|
|
|
break;
|
|
|
|
case 'hotKeySubmit':
|
|
|
|
this.hotKeySubmit = value;
|
|
|
|
break;
|
|
|
|
case 'justify':
|
|
|
|
this.setJustify(value);
|
|
|
|
break;
|
|
|
|
case 'fillChar':
|
|
|
|
this.setFillChar(value);
|
|
|
|
break;
|
|
|
|
case 'focusItemIndex':
|
|
|
|
this.focusedItemIndex = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'itemFormat':
|
|
|
|
case 'focusItemFormat':
|
2022-01-23 16:46:09 +00:00
|
|
|
this[propName] = value;
|
|
|
|
// if there is a cache currently, invalidate it
|
|
|
|
this.invalidateRenderCache();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'sort':
|
|
|
|
this.setSort(value);
|
|
|
|
break;
|
2022-01-23 16:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MenuView.super_.prototype.setPropertyValue.call(this, propName, value);
|
2015-06-30 05:14:17 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setFillChar = function (fillChar) {
|
|
|
|
this.fillChar = miscUtil.valueWithDefault(fillChar, ' ').substr(0, 1);
|
|
|
|
this.invalidateRenderCache();
|
|
|
|
};
|
2022-01-19 21:11:33 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setJustify = function (justify) {
|
|
|
|
this.justify = justify;
|
|
|
|
this.invalidateRenderCache();
|
|
|
|
this.positionCacheExpired = true;
|
|
|
|
};
|
2022-01-19 20:36:43 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MenuView.prototype.setHotKeys = function (hotKeys) {
|
|
|
|
if (_.isObject(hotKeys)) {
|
|
|
|
if (this.caseInsensitiveHotKeys) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.hotKeys = {};
|
2022-06-05 20:04:25 +00:00
|
|
|
for (var key in hotKeys) {
|
2022-01-23 16:46:09 +00:00
|
|
|
this.hotKeys[key.toLowerCase()] = hotKeys[key];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.hotKeys = hotKeys;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|