* Convert MenuModule to ES6 style class

* Convert modules that are MenuModule subclasses to ES6 style classes
* Convert mixins to ES6 style
* Various cleanup
This commit is contained in:
Bryan Ashby 2017-01-25 22:18:05 -07:00
parent 1c03c3021a
commit 99ab60bf77
26 changed files with 2214 additions and 2418 deletions

View file

@ -10,28 +10,26 @@ const async = require('async');
const _ = require('lodash');
const SSHClient = require('ssh2').Client;
exports.getModule = DoorPartyModule;
exports.moduleInfo = {
name : 'DoorParty',
desc : 'DoorParty Access Module',
author : 'NuSkooler',
};
exports.getModule = class DoorPartyModule extends MenuModule {
constructor(options) {
super(options);
function DoorPartyModule(options) {
MenuModule.call(this, options);
// establish defaults
this.config = options.menuConfig.config;
this.config.host = this.config.host || 'dp.throwbackbbs.com';
this.config.sshPort = this.config.sshPort || 2022;
this.config.rloginPort = this.config.rloginPort || 513;
}
const self = this;
// establish defaults
this.config = options.menuConfig.config;
this.config.host = this.config.host || 'dp.throwbackbbs.com';
this.config.sshPort = this.config.sshPort || 2022;
this.config.rloginPort = this.config.rloginPort || 513;
this.initSequence = function() {
initSequence() {
let clientTerminated;
const self = this;
async.series(
[
@ -125,8 +123,5 @@ function DoorPartyModule(options) {
}
}
);
};
}
require('util').inherits(DoorPartyModule, MenuModule);
}
};

View file

@ -28,4 +28,5 @@ exports.Errors = {
AccessDenied : (reason, reasonCode) => new EnigError('Access denied', -32003, reason, reasonCode),
Invalid : (reason, reasonCode) => new EnigError('Invalid', -32004, reason, reasonCode),
ExternalProcess : (reason, reasonCode) => new EnigError('External process error', -32005, reason, reasonCode),
MissingConfig : (reason, reasonCode) => new EnigError('Missing configuration', -32006, reason, reasonCode),
};

View file

@ -12,6 +12,7 @@ const getUserIdAndName = require('./user.js').getUserIdAndName;
const cleanControlCodes = require('./string_util.js').cleanControlCodes;
const StatLog = require('./stat_log.js');
const stringFormat = require('./string_format.js');
const MessageAreaConfTempSwitcher = require('./mod_mixins.js').MessageAreaConfTempSwitcher;
// deps
const async = require('async');
@ -19,12 +20,6 @@ const assert = require('assert');
const _ = require('lodash');
const moment = require('moment');
exports.FullScreenEditorModule = FullScreenEditorModule;
// :TODO: clean this up:
exports.getModule = FullScreenEditorModule;
exports.moduleInfo = {
name : 'Full Screen Editor (FSE)',
desc : 'A full screen editor/viewer',
@ -65,7 +60,7 @@ exports.moduleInfo = {
*/
var MCICodeIds = {
const MciCodeIds = {
ViewModeHeader : {
From : 1,
To : 2,
@ -97,72 +92,192 @@ var MCICodeIds = {
},
};
function FullScreenEditorModule(options) {
MenuModule.call(this, options);
// :TODO: convert code in this class to newer styles, conventions, etc. There is a lot of experimental stuff here that has better (DRY) alternatives
var self = this;
var config = this.menuConfig.config;
exports.FullScreenEditorModule = exports.getModule = class FullScreenEditorModule extends MessageAreaConfTempSwitcher(MenuModule) {
//
// menuConfig.config:
// editorType : email | area
// editorMode : view | edit | quote
//
// menuConfig.config or extraArgs
// messageAreaTag
// messageIndex / messageTotal
// toUserId
//
this.editorType = config.editorType;
this.editorMode = config.editorMode;
if(config.messageAreaTag) {
this.messageAreaTag = config.messageAreaTag;
}
this.messageIndex = config.messageIndex || 0;
this.messageTotal = config.messageTotal || 0;
this.toUserId = config.toUserId || 0;
constructor(options) {
super(options);
// extraArgs can override some config
if(_.isObject(options.extraArgs)) {
if(options.extraArgs.messageAreaTag) {
this.messageAreaTag = options.extraArgs.messageAreaTag;
const self = this;
const config = this.menuConfig.config;
//
// menuConfig.config:
// editorType : email | area
// editorMode : view | edit | quote
//
// menuConfig.config or extraArgs
// messageAreaTag
// messageIndex / messageTotal
// toUserId
//
this.editorType = config.editorType;
this.editorMode = config.editorMode;
if(config.messageAreaTag) {
this.messageAreaTag = config.messageAreaTag;
}
if(options.extraArgs.messageIndex) {
this.messageIndex = options.extraArgs.messageIndex;
this.messageIndex = config.messageIndex || 0;
this.messageTotal = config.messageTotal || 0;
this.toUserId = config.toUserId || 0;
// extraArgs can override some config
if(_.isObject(options.extraArgs)) {
if(options.extraArgs.messageAreaTag) {
this.messageAreaTag = options.extraArgs.messageAreaTag;
}
if(options.extraArgs.messageIndex) {
this.messageIndex = options.extraArgs.messageIndex;
}
if(options.extraArgs.messageTotal) {
this.messageTotal = options.extraArgs.messageTotal;
}
if(options.extraArgs.toUserId) {
this.toUserId = options.extraArgs.toUserId;
}
}
if(options.extraArgs.messageTotal) {
this.messageTotal = options.extraArgs.messageTotal;
}
if(options.extraArgs.toUserId) {
this.toUserId = options.extraArgs.toUserId;
this.isReady = false;
if(_.has(options, 'extraArgs.message')) {
this.setMessage(options.extraArgs.message);
} else if(_.has(options, 'extraArgs.replyToMessage')) {
this.replyToMessage = options.extraArgs.replyToMessage;
}
this.menuMethods = {
//
// Validation stuff
//
viewValidationListener : function(err, cb) {
var errMsgView = self.viewControllers.header.getView(MciCodeIds.ReplyEditModeHeader.ErrorMsg);
var newFocusViewId;
if(errMsgView) {
if(err) {
errMsgView.setText(err.message);
if(MciCodeIds.ViewModeHeader.Subject === err.view.getId()) {
// :TODO: for "area" mode, should probably just bail if this is emtpy (e.g. cancel)
}
} else {
errMsgView.clearText();
}
}
cb(newFocusViewId);
},
headerSubmit : function(formData, extraArgs, cb) {
self.switchToBody();
return cb(null);
},
editModeEscPressed : function(formData, extraArgs, cb) {
self.footerMode = 'editor' === self.footerMode ? 'editorMenu' : 'editor';
self.switchFooter(function next(err) {
if(err) {
// :TODO:... what now?
console.log(err)
} else {
switch(self.footerMode) {
case 'editor' :
if(!_.isUndefined(self.viewControllers.footerEditorMenu)) {
//self.viewControllers.footerEditorMenu.setFocus(false);
self.viewControllers.footerEditorMenu.detachClientEvents();
}
self.viewControllers.body.switchFocus(1);
self.observeEditorEvents();
break;
case 'editorMenu' :
self.viewControllers.body.setFocus(false);
self.viewControllers.footerEditorMenu.switchFocus(1);
break;
default : throw new Error('Unexpected mode');
}
}
return cb(null);
});
},
editModeMenuQuote : function(formData, extraArgs, cb) {
self.viewControllers.footerEditorMenu.setFocus(false);
self.displayQuoteBuilder();
return cb(null);
},
appendQuoteEntry: function(formData, extraArgs, cb) {
// :TODO: Dont' use magic # ID's here
var quoteMsgView = self.viewControllers.quoteBuilder.getView(1);
if(self.newQuoteBlock) {
self.newQuoteBlock = false;
quoteMsgView.addText(self.getQuoteByHeader());
}
var quoteText = self.viewControllers.quoteBuilder.getView(3).getItem(formData.value.quote);
quoteMsgView.addText(quoteText);
//
// If this is *not* the last item, advance. Otherwise, do nothing as we
// don't want to jump back to the top and repeat already quoted lines
//
var quoteListView = self.viewControllers.quoteBuilder.getView(3);
if(quoteListView.getData() !== quoteListView.getCount() - 1) {
quoteListView.focusNext();
} else {
self.quoteBuilderFinalize();
}
return cb(null);
},
quoteBuilderEscPressed : function(formData, extraArgs, cb) {
self.quoteBuilderFinalize();
return cb(null);
},
/*
replyDiscard : function(formData, extraArgs) {
// :TODO: need to prompt yes/no
// :TODO: @method for fallback would be better
self.prevMenu();
},
*/
editModeMenuHelp : function(formData, extraArgs, cb) {
self.viewControllers.footerEditorMenu.setFocus(false);
return self.displayHelp(cb);
},
///////////////////////////////////////////////////////////////////////
// View Mode
///////////////////////////////////////////////////////////////////////
viewModeMenuHelp : function(formData, extraArgs, cb) {
self.viewControllers.footerView.setFocus(false);
return self.displayHelp(cb);
}
};
}
this.isReady = false;
isEditMode() {
return 'edit' === this.editorMode;
}
this.isEditMode = function() {
return 'edit' === self.editorMode;
};
this.isViewMode = function() {
return 'view' === self.editorMode;
};
isViewMode() {
return 'view' === this.editorMode;
}
this.isLocalEmail = function() {
return Message.WellKnownAreaTags.Private === self.messageAreaTag;
};
isLocalEmail() {
return Message.WellKnownAreaTags.Private === this.messageAreaTag;
}
this.isReply = function() {
return !_.isUndefined(self.replyToMessage);
};
isReply() {
return !_.isUndefined(this.replyToMessage);
}
this.getFooterName = function() {
return 'footer' + _.capitalize(self.footerMode); // e.g. 'footerEditor', 'footerEditorMenu', ...
};
getFooterName() {
return 'footer' + _.capitalize(this.footerMode); // e.g. 'footerEditor', 'footerEditorMenu', ...
}
this.getFormId = function(name) {
getFormId(name) {
return {
header : 0,
body : 1,
@ -173,27 +288,13 @@ function FullScreenEditorModule(options) {
help : 50,
}[name];
};
/*ViewModeHeader : {
From : 1,
To : 2,
Subject : 3,
DateTime : 5,
MsgNum : 6,
MsgTotal : 7,
ViewCount : 8,
HashTags : 9,
MessageID : 10,
ReplyToMsgID : 11
},*/
}
// :TODO: convert to something like this for all view acces:
this.getHeaderViews = function() {
var vc = self.viewControllers.header;
getHeaderViews() {
var vc = this.viewControllers.header;
if(self.isViewMode()) {
if(this.isViewMode()) {
return {
from : vc.getView(1),
to : vc.getView(2),
@ -205,61 +306,55 @@ function FullScreenEditorModule(options) {
};
}
};
}
this.setInitialFooterMode = function() {
switch(self.editorMode) {
case 'edit' : self.footerMode = 'editor'; break;
case 'view' : self.footerMode = 'view'; break;
setInitialFooterMode() {
switch(this.editorMode) {
case 'edit' : this.footerMode = 'editor'; break;
case 'view' : this.footerMode = 'view'; break;
}
};
}
this.buildMessage = function() {
var headerValues = self.viewControllers.header.getFormData().value;
buildMessage() {
var headerValues = this.viewControllers.header.getFormData().value;
var msgOpts = {
areaTag : self.messageAreaTag,
areaTag : this.messageAreaTag,
toUserName : headerValues.to,
fromUserName : headerValues.from,
subject : headerValues.subject,
message : self.viewControllers.body.getFormData().value.message,
message : this.viewControllers.body.getFormData().value.message,
};
if(self.isReply()) {
msgOpts.replyToMsgId = self.replyToMessage.messageId;
if(this.isReply()) {
msgOpts.replyToMsgId = this.replyToMessage.messageId;
}
self.message = new Message(msgOpts);
};
this.message = new Message(msgOpts);
}
/*
this.setBodyMessageViewText = function() {
self.bodyMessageView.setText(cleanControlCodes(self.message.message));
};
*/
this.setMessage = function(message) {
self.message = message;
setMessage(message) {
this.message = message;
updateMessageAreaLastReadId(
self.client.user.userId, self.messageAreaTag, self.message.messageId, () => {
this.client.user.userId, this.messageAreaTag, this.message.messageId, () => {
if(self.isReady) {
self.initHeaderViewMode();
self.initFooterViewMode();
if(this.isReady) {
this.initHeaderViewMode();
this.initFooterViewMode();
var bodyMessageView = self.viewControllers.body.getView(1);
if(bodyMessageView && _.has(self, 'message.message')) {
//self.setBodyMessageViewText();
bodyMessageView.setText(cleanControlCodes(self.message.message));
//bodyMessageView.redraw();
var bodyMessageView = this.viewControllers.body.getView(1);
if(bodyMessageView && _.has(this, 'message.message')) {
bodyMessageView.setText(cleanControlCodes(this.message.message));
}
}
}
);
};
}
getMessage(cb) {
const self = this;
this.getMessage = function(cb) {
async.series(
[
function buildIfNecessary(callback) {
@ -295,24 +390,22 @@ function FullScreenEditorModule(options) {
cb(err, self.message);
}
);
};
}
this.updateUserStats = function(cb) {
updateUserStats(cb) {
if(Message.isPrivateAreaTag(this.message.areaTag)) {
if(cb) {
return cb(null);
cb(null);
}
return; // don't inc stats for private messages
}
StatLog.incrementUserStat(
self.client.user,
'post_count',
1,
cb
);
};
return StatLog.incrementUserStat(this.client.user, 'post_count', 1, cb);
}
redrawFooter(options, cb) {
const self = this;
this.redrawFooter = function(options, cb) {
async.waterfall(
[
function moveToFooterPosition(callback) {
@ -354,10 +447,11 @@ function FullScreenEditorModule(options) {
cb(err, artData);
}
);
};
}
this.redrawScreen = function(cb) {
redrawScreen(cb) {
var comps = [ 'header', 'body' ];
const self = this;
var art = self.menuConfig.config.art;
self.client.term.rawWrite(ansi.resetScreen());
@ -398,43 +492,44 @@ function FullScreenEditorModule(options) {
cb(err);
}
);
};
}
switchFooter(cb) {
var footerName = this.getFooterName();
this.switchFooter = function(cb) {
var footerName = self.getFooterName();
self.redrawFooter( { footerName : footerName, clear : true }, function artDisplayed(err, artData) {
this.redrawFooter( { footerName : footerName, clear : true }, (err, artData) => {
if(err) {
cb(err);
return;
}
var formId = self.getFormId(footerName);
var formId = this.getFormId(footerName);
if(_.isUndefined(self.viewControllers[footerName])) {
if(_.isUndefined(this.viewControllers[footerName])) {
var menuLoadOpts = {
callingMenu : self,
callingMenu : this,
formId : formId,
mciMap : artData.mciMap
};
self.addViewController(
this.addViewController(
footerName,
new ViewController( { client : self.client, formId : formId } )
).loadFromMenuConfig(menuLoadOpts, function footerReady(err) {
new ViewController( { client : this.client, formId : formId } )
).loadFromMenuConfig(menuLoadOpts, err => {
cb(err);
});
} else {
self.viewControllers[footerName].redrawAll();
this.viewControllers[footerName].redrawAll();
cb(null);
}
});
};
}
this.initSequence = function() {
initSequence() {
var mciData = { };
const self = this;
var art = self.menuConfig.config.art;
assert(_.isObject(art));
async.series(
@ -488,10 +583,10 @@ function FullScreenEditorModule(options) {
}
}
);
};
}
this.createInitialViews = function(mciData, cb) {
createInitialViews(mciData, cb) {
const self = this;
var menuLoadOpts = { callingMenu : self };
async.series(
@ -596,11 +691,11 @@ function FullScreenEditorModule(options) {
cb(err);
}
);
};
}
this.mciReadyHandler = function(mciData, cb) {
mciReadyHandler(mciData, cb) {
self.createInitialViews(mciData, function viewsCreated(err) {
this.createInitialViews(mciData, err => {
// :TODO: Can probably be replaced with @systemMethod:validateUserNameExists when the framework is in
// place - if this is for existing usernames else validate spec
@ -620,103 +715,94 @@ function FullScreenEditorModule(options) {
cb(err);
});
};
}
this.updateEditModePosition = function(pos) {
if(self.isEditMode()) {
var posView = self.viewControllers.footerEditor.getView(1);
updateEditModePosition(pos) {
if(this.isEditMode()) {
var posView = this.viewControllers.footerEditor.getView(1);
if(posView) {
self.client.term.rawWrite(ansi.savePos());
this.client.term.rawWrite(ansi.savePos());
// :TODO: Use new formatting techniques here, e.g. state.cursorPositionRow, cursorPositionCol and cursorPositionFormat
posView.setText(_.padLeft(String(pos.row + 1), 2, '0') + ',' + _.padLeft(String(pos.col + 1), 2, '0'));
self.client.term.rawWrite(ansi.restorePos());
this.client.term.rawWrite(ansi.restorePos());
}
}
};
}
this.updateTextEditMode = function(mode) {
if(self.isEditMode()) {
var modeView = self.viewControllers.footerEditor.getView(2);
updateTextEditMode(mode) {
if(this.isEditMode()) {
var modeView = this.viewControllers.footerEditor.getView(2);
if(modeView) {
self.client.term.rawWrite(ansi.savePos());
this.client.term.rawWrite(ansi.savePos());
modeView.setText('insert' === mode ? 'INS' : 'OVR');
self.client.term.rawWrite(ansi.restorePos());
this.client.term.rawWrite(ansi.restorePos());
}
}
};
}
this.setHeaderText = function(id, text) {
var v = self.viewControllers.header.getView(id);
if(v) {
v.setText(text);
}
};
setHeaderText(id, text) {
this.setViewText('header', id, text);
}
this.initHeaderViewMode = function() {
assert(_.isObject(self.message));
initHeaderViewMode() {
assert(_.isObject(this.message));
self.setHeaderText(MCICodeIds.ViewModeHeader.From, self.message.fromUserName);
self.setHeaderText(MCICodeIds.ViewModeHeader.To, self.message.toUserName);
self.setHeaderText(MCICodeIds.ViewModeHeader.Subject, self.message.subject);
self.setHeaderText(MCICodeIds.ViewModeHeader.DateTime, moment(self.message.modTimestamp).format(self.client.currentTheme.helpers.getDateTimeFormat()));
self.setHeaderText(MCICodeIds.ViewModeHeader.MsgNum, (self.messageIndex + 1).toString());
self.setHeaderText(MCICodeIds.ViewModeHeader.MsgTotal, self.messageTotal.toString());
self.setHeaderText(MCICodeIds.ViewModeHeader.ViewCount, self.message.viewCount);
self.setHeaderText(MCICodeIds.ViewModeHeader.HashTags, 'TODO hash tags');
self.setHeaderText(MCICodeIds.ViewModeHeader.MessageID, self.message.messageId);
self.setHeaderText(MCICodeIds.ViewModeHeader.ReplyToMsgID, self.message.replyToMessageId);
};
this.setHeaderText(MciCodeIds.ViewModeHeader.From, this.message.fromUserName);
this.setHeaderText(MciCodeIds.ViewModeHeader.To, this.message.toUserName);
this.setHeaderText(MciCodeIds.ViewModeHeader.Subject, this.message.subject);
this.setHeaderText(MciCodeIds.ViewModeHeader.DateTime, moment(this.message.modTimestamp).format(this.client.currentTheme.helpers.getDateTimeFormat()));
this.setHeaderText(MciCodeIds.ViewModeHeader.MsgNum, (this.messageIndex + 1).toString());
this.setHeaderText(MciCodeIds.ViewModeHeader.MsgTotal, this.messageTotal.toString());
this.setHeaderText(MciCodeIds.ViewModeHeader.ViewCount, this.message.viewCount);
this.setHeaderText(MciCodeIds.ViewModeHeader.HashTags, 'TODO hash tags');
this.setHeaderText(MciCodeIds.ViewModeHeader.MessageID, this.message.messageId);
this.setHeaderText(MciCodeIds.ViewModeHeader.ReplyToMsgID, this.message.replyToMessageId);
}
this.initHeaderReplyEditMode = function() {
assert(_.isObject(self.replyToMessage));
initHeaderReplyEditMode() {
assert(_.isObject(this.replyToMessage));
self.setHeaderText(MCICodeIds.ReplyEditModeHeader.To, self.replyToMessage.fromUserName);
this.setHeaderText(MciCodeIds.ReplyEditModeHeader.To, this.replyToMessage.fromUserName);
//
// We want to prefix the subject with "RE: " only if it's not already
// that way -- avoid RE: RE: RE: RE: ...
//
let newSubj = self.replyToMessage.subject;
let newSubj = this.replyToMessage.subject;
if(false === /^RE:\s+/i.test(newSubj)) {
newSubj = `RE: ${newSubj}`;
}
self.setHeaderText(MCICodeIds.ReplyEditModeHeader.Subject, newSubj);
};
this.setHeaderText(MciCodeIds.ReplyEditModeHeader.Subject, newSubj);
}
this.initFooterViewMode = function() {
function setFooterText(id, text) {
var v = self.viewControllers.footerView.getView(id);
if(v) {
v.setText(text);
}
}
initFooterViewMode() {
this.setViewText('footerView', MciCodeIds.ViewModeFooter.MsgNum, (this.messageIndex + 1).toString() );
this.setViewText('footerView', MciCodeIds.ViewModeFooter.MsgTotal, this.messageTotal.toString() );
}
setFooterText(MCICodeIds.ViewModeFooter.MsgNum, (self.messageIndex + 1).toString());
setFooterText(MCICodeIds.ViewModeFooter.MsgTotal, self.messageTotal.toString());
};
this.displayHelp = function(cb) {
self.client.term.rawWrite(ansi.resetScreen());
displayHelp(cb) {
this.client.term.rawWrite(ansi.resetScreen());
theme.displayThemeArt(
{ name : self.menuConfig.config.art.help, client : self.client },
{ name : this.menuConfig.config.art.help, client : this.client },
() => {
self.client.waitForKeyPress( () => {
self.redrawScreen( () => {
self.viewControllers[self.getFooterName()].setFocus(true);
this.client.waitForKeyPress( () => {
this.redrawScreen( () => {
this.viewControllers[this.getFooterName()].setFocus(true);
return cb(null);
});
});
}
);
};
}
this.displayQuoteBuilder = function() {
displayQuoteBuilder() {
//
// Clear body area
//
self.newQuoteBlock = true;
this.newQuoteBlock = true;
const self = this;
async.waterfall(
[
@ -772,19 +858,19 @@ function FullScreenEditorModule(options) {
}
}
);
};
}
this.observeEditorEvents = function() {
var bodyView = self.viewControllers.body.getView(1);
observeEditorEvents() {
const bodyView = this.viewControllers.body.getView(1);
bodyView.on('edit position', function cursorPosUpdate(pos) {
self.updateEditModePosition(pos);
bodyView.on('edit position', pos => {
this.updateEditModePosition(pos);
});
bodyView.on('text edit mode', function textEditMode(mode) {
self.updateTextEditMode(mode);
bodyView.on('text edit mode', mode => {
this.updateTextEditMode(mode);
});
};
}
/*
this.observeViewPosition = function() {
@ -794,43 +880,43 @@ function FullScreenEditorModule(options) {
};
*/
this.switchToHeader = function() {
self.viewControllers.body.setFocus(false);
self.viewControllers.header.switchFocus(2); // to
switchToHeader() {
this.viewControllers.body.setFocus(false);
this.viewControllers.header.switchFocus(2); // to
}
switchToBody() {
this.viewControllers.header.setFocus(false);
this.viewControllers.body.switchFocus(1);
this.observeEditorEvents();
};
this.switchToBody = function() {
self.viewControllers.header.setFocus(false);
self.viewControllers.body.switchFocus(1);
switchToFooter() {
this.viewControllers.header.setFocus(false);
this.viewControllers.body.setFocus(false);
self.observeEditorEvents();
};
this.viewControllers[this.getFooterName()].switchFocus(1); // HM1
}
this.switchToFooter = function() {
self.viewControllers.header.setFocus(false);
self.viewControllers.body.setFocus(false);
self.viewControllers[self.getFooterName()].switchFocus(1); // HM1
};
this.switchFromQuoteBuilderToBody = function() {
self.viewControllers.quoteBuilder.setFocus(false);
var body = self.viewControllers.body.getView(1);
switchFromQuoteBuilderToBody() {
this.viewControllers.quoteBuilder.setFocus(false);
var body = this.viewControllers.body.getView(1);
body.redraw();
self.viewControllers.body.switchFocus(1);
this.viewControllers.body.switchFocus(1);
// :TODO: create method (DRY)
self.updateTextEditMode(body.getTextEditMode());
self.updateEditModePosition(body.getEditPosition());
this.updateTextEditMode(body.getTextEditMode());
this.updateEditModePosition(body.getEditPosition());
self.observeEditorEvents();
};
this.observeEditorEvents();
}
this.quoteBuilderFinalize = function() {
quoteBuilderFinalize() {
// :TODO: fix magic #'s
var quoteMsgView = self.viewControllers.quoteBuilder.getView(1);
var msgView = self.viewControllers.body.getView(1);
var quoteMsgView = this.viewControllers.quoteBuilder.getView(1);
var msgView = this.viewControllers.body.getView(1);
var quoteLines = quoteMsgView.getData();
@ -841,164 +927,43 @@ function FullScreenEditorModule(options) {
quoteMsgView.setText('');
var footerName = self.getFooterName();
this.footerMode = 'editor';
self.footerMode = 'editor';
self.switchFooter(function switched(err) {
self.switchFromQuoteBuilderToBody();
this.switchFooter( () => {
this.switchFromQuoteBuilderToBody();
});
};
}
this.getQuoteByHeader = function() {
getQuoteByHeader() {
let quoteFormat = this.menuConfig.config.quoteFormats;
if(Array.isArray(quoteFormat)) {
quoteFormat = quoteFormat[ Math.floor(Math.random() * quoteFormat.length) ];
} else if(!_.isString(quoteFormat)) {
quoteFormat = 'On {dateTime} {userName} said...';
}
const dtFormat = this.menuConfig.config.quoteDateTimeFormat || self.client.currentTheme.helpers.getDateTimeFormat();
const dtFormat = this.menuConfig.config.quoteDateTimeFormat || this.client.currentTheme.helpers.getDateTimeFormat();
return stringFormat(quoteFormat, {
dateTime : moment(self.replyToMessage.modTimestamp).format(dtFormat),
userName : self.replyToMessage.fromUserName,
dateTime : moment(this.replyToMessage.modTimestamp).format(dtFormat),
userName : this.replyToMessage.fromUserName,
});
};
}
this.menuMethods = {
//
// Validation stuff
//
viewValidationListener : function(err, cb) {
var errMsgView = self.viewControllers.header.getView(MCICodeIds.ReplyEditModeHeader.ErrorMsg);
var newFocusViewId;
if(errMsgView) {
if(err) {
errMsgView.setText(err.message);
if(MCICodeIds.ViewModeHeader.Subject === err.view.getId()) {
// :TODO: for "area" mode, should probably just bail if this is emtpy (e.g. cancel)
}
} else {
errMsgView.clearText();
}
}
cb(newFocusViewId);
},
headerSubmit : function(formData, extraArgs, cb) {
self.switchToBody();
return cb(null);
},
editModeEscPressed : function(formData, extraArgs, cb) {
self.footerMode = 'editor' === self.footerMode ? 'editorMenu' : 'editor';
self.switchFooter(function next(err) {
if(err) {
// :TODO:... what now?
console.log(err)
} else {
switch(self.footerMode) {
case 'editor' :
if(!_.isUndefined(self.viewControllers.footerEditorMenu)) {
//self.viewControllers.footerEditorMenu.setFocus(false);
self.viewControllers.footerEditorMenu.detachClientEvents();
}
self.viewControllers.body.switchFocus(1);
self.observeEditorEvents();
break;
case 'editorMenu' :
self.viewControllers.body.setFocus(false);
self.viewControllers.footerEditorMenu.switchFocus(1);
break;
default : throw new Error('Unexpected mode');
}
}
return cb(null);
});
},
editModeMenuQuote : function(formData, extraArgs, cb) {
self.viewControllers.footerEditorMenu.setFocus(false);
self.displayQuoteBuilder();
return cb(null);
},
appendQuoteEntry: function(formData, extraArgs, cb) {
// :TODO: Dont' use magic # ID's here
var quoteMsgView = self.viewControllers.quoteBuilder.getView(1);
if(self.newQuoteBlock) {
self.newQuoteBlock = false;
quoteMsgView.addText(self.getQuoteByHeader());
}
var quoteText = self.viewControllers.quoteBuilder.getView(3).getItem(formData.value.quote);
quoteMsgView.addText(quoteText);
//
// If this is *not* the last item, advance. Otherwise, do nothing as we
// don't want to jump back to the top and repeat already quoted lines
//
var quoteListView = self.viewControllers.quoteBuilder.getView(3);
if(quoteListView.getData() !== quoteListView.getCount() - 1) {
quoteListView.focusNext();
} else {
self.quoteBuilderFinalize();
}
return cb(null);
},
quoteBuilderEscPressed : function(formData, extraArgs, cb) {
self.quoteBuilderFinalize();
return cb(null);
},
/*
replyDiscard : function(formData, extraArgs) {
// :TODO: need to prompt yes/no
// :TODO: @method for fallback would be better
self.prevMenu();
},
*/
editModeMenuHelp : function(formData, extraArgs, cb) {
self.viewControllers.footerEditorMenu.setFocus(false);
return self.displayHelp(cb);
},
///////////////////////////////////////////////////////////////////////
// View Mode
///////////////////////////////////////////////////////////////////////
viewModeMenuHelp : function(formData, extraArgs, cb) {
self.viewControllers.footerView.setFocus(false);
return self.displayHelp(cb);
enter() {
if(this.messageAreaTag) {
this.tempMessageConfAndAreaSwitch(this.messageAreaTag);
}
};
if(_.has(options, 'extraArgs.message')) {
this.setMessage(options.extraArgs.message);
} else if(_.has(options, 'extraArgs.replyToMessage')) {
this.replyToMessage = options.extraArgs.replyToMessage;
}
}
require('util').inherits(FullScreenEditorModule, MenuModule);
require('./mod_mixins.js').MessageAreaConfTempSwitcher.call(FullScreenEditorModule.prototype);
FullScreenEditorModule.prototype.enter = function() {
if(this.messageAreaTag) {
this.tempMessageConfAndAreaSwitch(this.messageAreaTag);
super.enter();
}
FullScreenEditorModule.super_.prototype.enter.call(this);
};
leave() {
this.tempMessageConfAndAreaRestore();
super.leave();
}
FullScreenEditorModule.prototype.leave = function() {
this.tempMessageConfAndAreaRestore();
FullScreenEditorModule.super_.prototype.leave.call(this);
};
FullScreenEditorModule.prototype.mciReady = function(mciData, cb) {
this.mciReadyHandler(mciData, cb);
mciReady(mciData, cb) {
return this.mciReadyHandler(mciData, cb);
}
};

View file

@ -9,432 +9,393 @@ const menuUtil = require('./menu_util.js');
const Config = require('./config.js').config;
const stringFormat = require('../core/string_format.js');
const MultiLineEditTextView = require('../core/multi_line_edit_text_view.js').MultiLineEditTextView;
const Errors = require('../core/enig_error.js').Errors;
// deps
const async = require('async');
const assert = require('assert');
const _ = require('lodash');
const async = require('async');
const assert = require('assert');
const _ = require('lodash');
exports.MenuModule = MenuModule;
exports.MenuModule = class MenuModule extends PluginModule {
constructor(options) {
super(options);
// :TODO: some of this is a bit off... should pause after finishedLoading()
this.menuName = options.menuName;
this.menuConfig = options.menuConfig;
this.client = options.client;
this.menuConfig.options = options.menuConfig.options || {};
this.menuMethods = {}; // methods called from @method's
this.menuConfig.config = this.menuConfig.config || {};
this.cls = _.isBoolean(this.menuConfig.options.cls) ? this.menuConfig.options.cls : Config.menus.cls;
function MenuModule(options) {
PluginModule.call(this, options);
this.viewControllers = {};
}
var self = this;
this.menuName = options.menuName;
this.menuConfig = options.menuConfig;
this.client = options.client;
// :TODO: this and the line below with .config creates empty ({}) objects in the theme --
// ...which we really should not do. If they aren't there already, don't use 'em.
this.menuConfig.options = options.menuConfig.options || {};
this.menuMethods = {}; // methods called from @method's
enter() {
this.initSequence();
}
this.cls = _.isBoolean(this.menuConfig.options.cls) ?
this.menuConfig.options.cls :
Config.menus.cls;
leave() {
this.detachViewControllers();
}
this.menuConfig.config = this.menuConfig.config || {};
initSequence() {
const self = this;
const mciData = {};
let pausePosition;
this.initViewControllers();
async.series(
[
function beforeDisplayArt(callback) {
self.beforeArt(callback);
},
function displayMenuArt(callback) {
if(!_.isString(self.menuConfig.art)) {
return callback(null);
}
this.shouldPause = function() {
return 'end' === self.menuConfig.options.pause || true === self.menuConfig.options.pause;
};
self.displayAsset(
self.menuConfig.art,
self.menuConfig.options,
(err, artData) => {
if(err) {
self.client.log.trace('Could not display art', { art : self.menuConfig.art, reason : err.message } );
} else {
mciData.menu = artData.mciMap;
}
this.hasNextTimeout = function() {
return _.isNumber(self.menuConfig.options.nextTimeout);
};
return callback(null); // any errors are non-fatal
}
);
},
function moveToPromptLocation(callback) {
if(self.menuConfig.prompt) {
// :TODO: fetch and move cursor to prompt location, if supplied. See notes/etc. on placements
}
this.autoNextMenu = function(cb) {
function goNext() {
if(_.isString(self.menuConfig.next) || _.isArray(self.menuConfig.next)) {
return callback(null);
},
function displayPromptArt(callback) {
if(!_.isString(self.menuConfig.prompt)) {
return callback(null);
}
if(!_.isObject(self.menuConfig.promptConfig)) {
return callback(Errors.MissingConfig('Prompt specified but no "promptConfig" block found'));
}
self.displayAsset(
self.menuConfig.promptConfig.art,
self.menuConfig.options,
(err, artData) => {
if(artData) {
mciData.prompt = artData.mciMap;
}
return callback(err); // pass err here; prompts *must* have art
}
);
},
function recordCursorPosition(callback) {
if(!self.shouldPause()) {
return callback(null); // cursor position not needed
}
self.client.once('cursor position report', pos => {
pausePosition = { row : pos[0], col : 1 };
self.client.log.trace('After art position recorded', { position : pausePosition } );
return callback(null);
});
self.client.term.rawWrite(ansi.queryPos());
},
function afterArtDisplayed(callback) {
return self.mciReady(mciData, callback);
},
function displayPauseIfRequested(callback) {
if(!self.shouldPause()) {
return callback(null);
}
return self.pausePrompt(pausePosition, callback);
},
function finishAndNext(callback) {
self.finishedLoading();
return self.autoNextMenu(callback);
}
],
err => {
if(err) {
self.client.log.warn('Error during init sequence', { error : err.message } );
return self.prevMenu( () => { /* dummy */ } );
}
}
);
}
beforeArt(cb) {
if(_.isNumber(this.menuConfig.options.baudRate)) {
// :TODO: some terminals not supporting cterm style emulated baud rate end up displaying a broken ESC sequence or a single "r" here
this.client.term.rawWrite(ansi.setEmulatedBaudRate(this.menuConfig.options.baudRate));
}
if(this.cls) {
this.client.term.rawWrite(ansi.resetScreen());
}
return cb(null);
}
mciReady(mciData, cb) {
// available for sub-classes
return cb(null);
}
finishedLoading() {
// nothing in base
}
getSaveState() {
// nothing in base
}
restoreSavedState(/*savedState*/) {
// nothing in base
}
getMenuResult() {
// nothing in base
}
nextMenu(cb) {
if(!this.haveNext()) {
return this.prevMenu(cb); // no next, go to prev
}
return this.client.menuStack.next(cb);
}
prevMenu(cb) {
return this.client.menuStack.prev(cb);
}
gotoMenu(name, options, cb) {
return this.client.menuStack.goto(name, options, cb);
}
addViewController(name, vc) {
assert(!this.viewControllers[name], `ViewController by the name of "${name}" already exists!`);
this.viewControllers[name] = vc;
return vc;
}
detachViewControllers() {
Object.keys(this.viewControllers).forEach( name => {
this.viewControllers[name].detachClientEvents();
});
}
shouldPause() {
return ('end' === this.menuConfig.options.pause || true === this.menuConfig.options.pause);
}
hasNextTimeout() {
return _.isNumber(this.menuConfig.options.nextTimeout);
}
haveNext() {
return (_.isString(this.menuConfig.next) || _.isArray(this.menuConfig.next));
}
autoNextMenu(cb) {
const self = this;
function gotoNextMenu() {
if(self.haveNext()) {
return menuUtil.handleNext(self.client, self.menuConfig.next, {}, cb);
} else {
return self.prevMenu(cb);
}
}
if(_.has(self.menuConfig, 'runtime.autoNext') && true === self.menuConfig.runtime.autoNext) {
/*
If 'next' is supplied, we'll use it. Otherwise, utlize fallback which
may be explicit (supplied) or non-explicit (previous menu)
'next' may be a simple asset, or a object with next.asset and
extrArgs
next: assetSpec
-or-
next: {
asset: assetSpec
extraArgs: ...
}
*/
if(self.hasNextTimeout()) {
if(_.has(this.menuConfig, 'runtime.autoNext') && true === this.menuConfig.runtime.autoNext) {
if(this.hasNextTimeout()) {
setTimeout( () => {
return goNext();
return gotoNextMenu();
}, this.menuConfig.options.nextTimeout);
} else {
goNext();
return gotoNextMenu();
}
}
};
}
this.haveNext = function() {
return (_.isString(this.menuConfig.next) || _.isArray(this.menuConfig.next));
};
}
standardMCIReadyHandler(mciData, cb) {
//
// A quick rundown:
// * We may have mciData.menu, mciData.prompt, or both.
// * Prompt form is favored over menu form if both are present.
// * Standard/prefdefined MCI entries must load both (e.g. %BN is expected to resolve)
//
const self = this;
require('util').inherits(MenuModule, PluginModule);
async.series(
[
function addViewControllers(callback) {
_.forEach(mciData, (mciMap, name) => {
assert('menu' === name || 'prompt' === name);
self.addViewController(name, new ViewController( { client : self.client } ) );
});
require('./mod_mixins.js').ViewControllerManagement.call(MenuModule.prototype);
MenuModule.prototype.enter = function() {
this.initSequence();
};
MenuModule.prototype.initSequence = function() {
var mciData = { };
const self = this;
async.series(
[
function beforeDisplayArt(callback) {
self.beforeArt(callback);
},
function displayMenuArt(callback) {
if(_.isString(self.menuConfig.art)) {
theme.displayThemedAsset(
self.menuConfig.art,
self.client,
self.menuConfig.options, // can include .font, .trailingLF, etc.
function displayed(err, artData) {
if(err) {
self.client.log.trace( { art : self.menuConfig.art, error : err.message }, 'Could not display art');
} else {
mciData.menu = artData.mciMap;
}
callback(null); // non-fatal
}
);
} else {
callback(null);
}
},
function moveToPromptLocation(callback) {
if(self.menuConfig.prompt) {
// :TODO: fetch and move cursor to prompt location, if supplied. See notes/etc. on placements
}
callback(null);
},
function displayPromptArt(callback) {
if(_.isString(self.menuConfig.prompt)) {
// If a prompt is specified, we need the configuration
if(!_.isObject(self.menuConfig.promptConfig)) {
callback(new Error('Prompt specified but configuraiton not found!'));
return;
return callback(null);
},
function createMenu(callback) {
if(!self.viewControllers.menu) {
return callback(null);
}
// Prompts *must* have art. If it's missing it's an error
// :TODO: allow inline prompts in the future, e.g. @inline:memberName -> { "memberName" : { "text" : "stuff", ... } }
var promptConfig = self.menuConfig.promptConfig;
theme.displayThemedAsset(
promptConfig.art,
self.client,
self.menuConfig.options, // can include .font, .trailingLF, etc.
function displayed(err, artData) {
if(!err) {
mciData.prompt = artData.mciMap;
}
callback(err);
});
} else {
callback(null);
}
},
function recordCursorPosition(callback) {
if(self.shouldPause()) {
self.client.once('cursor position report', function cpr(pos) {
self.afterArtPos = pos;
self.client.log.trace( { position : pos }, 'After art position recorded');
callback(null);
});
self.client.term.write(ansi.queryPos());
} else {
callback(null);
}
},
function afterArtDisplayed(callback) {
self.mciReady(mciData, callback);
},
function displayPauseIfRequested(callback) {
if(self.shouldPause()) {
self.client.term.write(ansi.goto(self.afterArtPos[0], 1));
// :TODO: really need a client.term.pause() that uses the correct art/etc.
// :TODO: Use MenuModule.pausePrompt()
theme.displayThemedPause( { client : self.client }, function keyPressed() {
callback(null);
});
} else {
callback(null);
}
},
function finishAndNext(callback) {
self.finishedLoading();
self.autoNextMenu(callback);
}
],
function complete(err) {
if(err) {
console.log(err)
// :TODO: what to do exactly?????
return self.prevMenu( () => {
// dummy
});
}
}
);
};
MenuModule.prototype.getSaveState = function() {
// nothing in base
};
MenuModule.prototype.restoreSavedState = function(/*savedState*/) {
// nothing in base
};
MenuModule.prototype.nextMenu = function(cb) {
//
// If we don't actually have |next|, we'll go previous
//
if(!this.haveNext()) {
return this.prevMenu(cb);
}
this.client.menuStack.next(cb);
};
MenuModule.prototype.prevMenu = function(cb) {
this.client.menuStack.prev(cb);
};
MenuModule.prototype.gotoMenu = function(name, options, cb) {
this.client.menuStack.goto(name, options, cb);
};
MenuModule.prototype.popAndGotoMenu = function(name, options, cb) {
this.client.menuStack.pop();
this.client.menuStack.goto(name, options, cb);
};
MenuModule.prototype.leave = function() {
this.detachViewControllers();
};
MenuModule.prototype.beforeArt = function(cb) {
//
// Set emulated baud rate - note that some terminals will display
// part of the ESC sequence here (generally a single 'r') if they
// do not support cterm style baud rates
//
if(_.isNumber(this.menuConfig.options.baudRate)) {
this.client.term.write(ansi.setEmulatedBaudRate(this.menuConfig.options.baudRate));
}
if(this.cls) {
this.client.term.write(ansi.resetScreen());
}
return cb(null);
};
MenuModule.prototype.mciReady = function(mciData, cb) {
// Reserved for sub classes
cb(null);
};
MenuModule.prototype.standardMCIReadyHandler = function(mciData, cb) {
//
// A quick rundown:
// * We may have mciData.menu, mciData.prompt, or both.
// * Prompt form is favored over menu form if both are present.
// * Standard/prefdefined MCI entries must load both (e.g. %BN is expected to resolve)
//
var self = this;
async.series(
[
function addViewControllers(callback) {
_.forEach(mciData, function entry(mciMap, name) {
assert('menu' === name || 'prompt' === name);
self.addViewController(name, new ViewController( { client : self.client } ));
});
callback(null);
},
function createMenu(callback) {
if(self.viewControllers.menu) {
var menuLoadOpts = {
const menuLoadOpts = {
mciMap : mciData.menu,
callingMenu : self,
withoutForm : _.isObject(mciData.prompt),
};
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, function menuLoaded(err) {
callback(err);
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, err => {
return callback(err);
});
} else {
callback(null);
}
},
function createPrompt(callback) {
if(self.viewControllers.prompt) {
var promptLoadOpts = {
},
function createPrompt(callback) {
if(!self.viewControllers.prompt) {
return callback(null);
}
const promptLoadOpts = {
callingMenu : self,
mciMap : mciData.prompt,
};
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, function promptLoaded(err) {
callback(err);
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, err => {
return callback(err);
});
} else {
callback(null);
}
}
],
function complete(err) {
cb(err);
}
);
};
MenuModule.prototype.finishedLoading = function() {
};
MenuModule.prototype.getMenuResult = function() {
// nothing in base
};
MenuModule.prototype.displayAsset = function(name, options, cb) {
if(_.isFunction(options)) {
cb = options;
options = {};
}
if(options.clearScreen) {
this.client.term.rawWrite(ansi.clearScreen());
}
return theme.displayThemedAsset(
name,
this.client,
Object.assign( { font : this.menuConfig.config.font }, options ),
(err, artData) => {
if(cb) {
return cb(err, artData);
}
}
);
};
MenuModule.prototype.prepViewController = function(name, formId, artData, cb) {
if(_.isUndefined(this.viewControllers[name])) {
const vcOpts = {
client : this.client,
formId : formId,
};
const vc = this.addViewController(name, new ViewController(vcOpts));
const loadOpts = {
callingMenu : this,
mciMap : artData.mciMap,
formId : formId,
};
return vc.loadFromMenuConfig(loadOpts, cb);
}
this.viewControllers[name].setFocus(true);
return cb(null);
};
MenuModule.prototype.prepViewControllerWithArt = function(name, formId, options, cb) {
this.displayAsset(
this.menuConfig.config.art[name],
options,
(err, artData) => {
if(err) {
],
err => {
return cb(err);
}
);
}
return this.prepViewController(name, formId, artData, cb);
displayAsset(name, options, cb) {
if(_.isFunction(options)) {
cb = options;
options = {};
}
);
};
MenuModule.prototype.pausePrompt = function(position, cb) {
if(!cb && _.isFunction(position)) {
cb = position;
position = null;
}
if(position) {
position.x = position.row || position.x || 1;
position.y = position.col || position.y || 1;
this.client.term.rawWrite(ansi.goto(position.x, position.y));
}
theme.displayThemedPause( { client : this.client }, cb);
};
MenuModule.prototype.setViewText = function(formName, mciId, text, appendMultiline) {
const view = this.viewControllers[formName].getView(mciId);
if(!view) {
return;
}
if(appendMultiline && (view instanceof MultiLineEditTextView)) {
view.addText(text);
} else {
view.setText(text);
}
};
MenuModule.prototype.updateCustomViewTextsWithFilter = function(formName, startId, fmtObj, options) {
options = options || {};
let textView;
let customMciId = startId;
const config = this.menuConfig.config;
while( (textView = this.viewControllers[formName].getView(customMciId)) ) {
const key = `${formName}InfoFormat${customMciId}`; // e.g. "mainInfoFormat10"
const format = config[key];
if(format && (!options.filter || options.filter.find(f => format.indexOf(f) > - 1))) {
const text = stringFormat(format, fmtObj);
if(options.appendMultiLine && (textView instanceof MultiLineEditTextView)) {
textView.addText(text);
} else {
textView.setText(text);
if(options.clearScreen) {
this.client.term.rawWrite(ansi.clearScreen());
}
return theme.displayThemedAsset(
name,
this.client,
Object.assign( { font : this.menuConfig.config.font }, options ),
(err, artData) => {
if(cb) {
return cb(err, artData);
}
}
);
}
prepViewController(name, formId, artData, cb) {
if(_.isUndefined(this.viewControllers[name])) {
const vcOpts = {
client : this.client,
formId : formId,
};
const vc = this.addViewController(name, new ViewController(vcOpts));
const loadOpts = {
callingMenu : this,
mciMap : artData.mciMap,
formId : formId,
};
return vc.loadFromMenuConfig(loadOpts, cb);
}
++customMciId;
this.viewControllers[name].setFocus(true);
return cb(null);
}
};
prepViewControllerWithArt(name, formId, options, cb) {
this.displayAsset(
this.menuConfig.config.art[name],
options,
(err, artData) => {
if(err) {
return cb(err);
}
return this.prepViewController(name, formId, artData, cb);
}
);
}
pausePrompt(position, cb) {
if(!cb && _.isFunction(position)) {
cb = position;
position = null;
}
if(position) {
position.x = position.row || position.x || 1;
position.y = position.col || position.y || 1;
this.client.term.rawWrite(ansi.goto(position.x, position.y));
}
return theme.displayThemedPause( { client : this.client }, cb);
}
setViewText(formName, mciId, text, appendMultiLine) {
const view = this.viewControllers[formName].getView(mciId);
if(!view) {
return;
}
if(appendMultiLine && (view instanceof MultiLineEditTextView)) {
view.addText(text);
} else {
view.setText(text);
}
}
updateCustomViewTextsWithFilter(formName, startId, fmtObj, options) {
options = options || {};
let textView;
let customMciId = startId;
const config = this.menuConfig.config;
while( (textView = this.viewControllers[formName].getView(customMciId)) ) {
const key = `${formName}InfoFormat${customMciId}`; // e.g. "mainInfoFormat10"
const format = config[key];
if(format && (!options.filter || options.filter.find(f => format.indexOf(f) > - 1))) {
const text = stringFormat(format, fmtObj);
if(options.appendMultiLine && (textView instanceof MultiLineEditTextView)) {
textView.addText(text);
} else {
textView.setText(text);
}
}
++customMciId;
}
}
};

View file

@ -1,56 +1,31 @@
/* jslint node: true */
'use strict';
const messageArea = require('../core/message_area.js');
const messageArea = require('../core/message_area.js');
// deps
const assert = require('assert');
//
// A simple mixin for View Controller management
//
exports.ViewControllerManagement = function() {
this.initViewControllers = function() {
this.viewControllers = {};
};
this.detachViewControllers = function() {
var self = this;
Object.keys(this.viewControllers).forEach(function vc(name) {
self.viewControllers[name].detachClientEvents();
});
};
this.addViewController = function(name, vc) {
assert(this.viewControllers, 'initViewControllers() has not been called!');
assert(!this.viewControllers[name], 'ViewController by the name of \'' + name + '\' already exists!');
this.viewControllers[name] = vc;
return vc;
};
};
exports.MessageAreaConfTempSwitcher = function() {
exports.MessageAreaConfTempSwitcher = Sup => class extends Sup {
this.tempMessageConfAndAreaSwitch = function(messageAreaTag) {
tempMessageConfAndAreaSwitch(messageAreaTag) {
messageAreaTag = messageAreaTag || this.messageAreaTag;
if(!messageAreaTag) {
return; // nothing to do!
}
this.prevMessageConfAndArea = {
confTag : this.client.user.properties.message_conf_tag,
areaTag : this.client.user.properties.message_area_tag,
};
if(!messageArea.tempChangeMessageConfAndArea(this.client, this.messageAreaTag)) {
this.client.log.warn( { messageAreaTag : messageArea }, 'Failed to perform temporary message area/conf switch');
}
};
}
this.tempMessageConfAndAreaRestore = function() {
tempMessageConfAndAreaRestore() {
if(this.prevMessageConfAndArea) {
this.client.user.properties.message_conf_tag = this.prevMessageConfAndArea.confTag;
this.client.user.properties.message_area_tag = this.prevMessageConfAndArea.areaTag;
}
};
}
};

View file

@ -17,8 +17,6 @@ exports.moduleInfo = {
author : 'NuSkooler',
};
exports.getModule = NewScanModule;
/*
* :TODO:
* * User configurable new scan: Area selection (avail from messages area) (sep module)
@ -27,48 +25,45 @@ exports.getModule = NewScanModule;
*/
var MciCodeIds = {
const MciCodeIds = {
ScanStatusLabel : 1, // TL1
ScanStatusList : 2, // VM2 (appends)
};
function NewScanModule(options) {
MenuModule.call(this, options);
exports.getModule = class NewScanModule extends MenuModule {
constructor(options) {
super(options);
var self = this;
var config = this.menuConfig.config;
this.newScanFullExit = _.has(options, 'lastMenuResult.fullExit') ? options.lastMenuResult.fullExit : false;
this.newScanFullExit = _.has(options, 'lastMenuResult.fullExit') ? options.lastMenuResult.fullExit : false;
this.currentStep = 'messageConferences';
this.currentScanAux = {};
this.currentStep = 'messageConferences';
this.currentScanAux = {};
// :TODO: Make this conf/area specific:
const config = this.menuConfig.config;
this.scanStartFmt = config.scanStartFmt || 'Scanning {confName} - {areaName}...';
this.scanFinishNoneFmt = config.scanFinishNoneFmt || 'Nothing new';
this.scanFinishNewFmt = config.scanFinishNewFmt || '{count} entries found';
this.scanCompleteMsg = config.scanCompleteMsg || 'Finished newscan';
}
// :TODO: Make this conf/area specific:
this.scanStartFmt = config.scanStartFmt || 'Scanning {confName} - {areaName}...';
this.scanFinishNoneFmt = config.scanFinishNoneFmt || 'Nothing new';
this.scanFinishNewFmt = config.scanFinishNewFmt || '{count} entries found';
this.scanCompleteMsg = config.scanCompleteMsg || 'Finished newscan';
this.updateScanStatus = function(statusText) {
var vc = self.viewControllers.allViews;
var view = vc.getView(MciCodeIds.ScanStatusLabel);
if(view) {
view.setText(statusText);
}
updateScanStatus(statusText) {
this.setViewText('allViews', MciCodeIds.ScanStatusLabel, statusText);
/*
view = vc.getView(MciCodeIds.ScanStatusList);
// :TODO: MenuView needs appendItem()
if(view) {
}
};
*/
}
this.newScanMessageConference = function(cb) {
newScanMessageConference(cb) {
// lazy init
if(!self.sortedMessageConfs) {
if(!this.sortedMessageConfs) {
const getAvailOpts = { includeSystemInternal : true }; // find new private messages, bulletins, etc.
self.sortedMessageConfs = _.map(msgArea.getAvailableMessageConferences(self.client, getAvailOpts), (v, k) => {
this.sortedMessageConfs = _.map(msgArea.getAvailableMessageConferences(this.client, getAvailOpts), (v, k) => {
return {
confTag : k,
conf : v,
@ -80,7 +75,7 @@ function NewScanModule(options) {
// always come first such that we display private mails/etc. before
// other conferences & areas
//
self.sortedMessageConfs.sort((a, b) => {
this.sortedMessageConfs.sort((a, b) => {
if('system_internal' === a.confTag) {
return -1;
} else {
@ -88,11 +83,12 @@ function NewScanModule(options) {
}
});
self.currentScanAux.conf = self.currentScanAux.conf || 0;
self.currentScanAux.area = self.currentScanAux.area || 0;
this.currentScanAux.conf = this.currentScanAux.conf || 0;
this.currentScanAux.area = this.currentScanAux.area || 0;
}
const currentConf = self.sortedMessageConfs[self.currentScanAux.conf];
const currentConf = this.sortedMessageConfs[this.currentScanAux.conf];
const self = this;
async.series(
[
@ -113,19 +109,22 @@ function NewScanModule(options) {
});
}
],
cb
err => {
return cb(err);
}
);
};
}
this.newScanMessageArea = function(conf, cb) {
newScanMessageArea(conf, cb) {
// :TODO: it would be nice to cache this - must be done by conf!
const sortedAreas = msgArea.getSortedAvailMessageAreasByConfTag(conf.confTag, { client : self.client } );
const currentArea = sortedAreas[self.currentScanAux.area];
const sortedAreas = msgArea.getSortedAvailMessageAreasByConfTag(conf.confTag, { client : this.client } );
const currentArea = sortedAreas[this.currentScanAux.area];
//
// Scan and update index until we find something. If results are found,
// we'll goto the list module & show them.
//
const self = this;
async.waterfall(
[
function checkAndUpdateIndex(callback) {
@ -165,73 +164,73 @@ function NewScanModule(options) {
}
};
return self.gotoMenu(config.newScanMessageList || 'newScanMessageList', nextModuleOpts);
return self.gotoMenu(self.menuConfig.config.newScanMessageList || 'newScanMessageList', nextModuleOpts);
}
],
cb // no more areas
err => {
return cb(err);
}
);
};
}
require('util').inherits(NewScanModule, MenuModule);
NewScanModule.prototype.getSaveState = function() {
return {
currentStep : this.currentStep,
currentScanAux : this.currentScanAux,
};
};
NewScanModule.prototype.restoreSavedState = function(savedState) {
this.currentStep = savedState.currentStep;
this.currentScanAux = savedState.currentScanAux;
};
NewScanModule.prototype.mciReady = function(mciData, cb) {
if(this.newScanFullExit) {
// user has canceled the entire scan @ message list view
return cb(null);
}
getSaveState() {
return {
currentStep : this.currentStep,
currentScanAux : this.currentScanAux,
};
}
var self = this;
var vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
restoreSavedState(savedState) {
this.currentStep = savedState.currentStep;
this.currentScanAux = savedState.currentScanAux;
}
// :TODO: display scan step/etc.
async.series(
[
function callParentMciReady(callback) {
NewScanModule.super_.prototype.mciReady.call(self, mciData, callback);
},
function loadFromConfig(callback) {
const loadOpts = {
callingMenu : self,
mciMap : mciData.menu,
noInput : true,
};
vc.loadFromMenuConfig(loadOpts, callback);
},
function performCurrentStepScan(callback) {
switch(self.currentStep) {
case 'messageConferences' :
self.newScanMessageConference( () => {
callback(null); // finished
});
break;
default : return callback(null);
}
}
],
function complete(err) {
if(err) {
self.client.log.error( { error : err.toString() }, 'Error during new scan');
}
cb(err);
mciReady(mciData, cb) {
if(this.newScanFullExit) {
// user has canceled the entire scan @ message list view
return cb(null);
}
);
super.mciReady(mciData, err => {
if(err) {
return cb(err);
}
const self = this;
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
// :TODO: display scan step/etc.
async.series(
[
function loadFromConfig(callback) {
const loadOpts = {
callingMenu : self,
mciMap : mciData.menu,
noInput : true,
};
vc.loadFromMenuConfig(loadOpts, callback);
},
function performCurrentStepScan(callback) {
switch(self.currentStep) {
case 'messageConferences' :
self.newScanMessageConference( () => {
callback(null); // finished
});
break;
default : return callback(null);
}
}
],
err => {
if(err) {
self.client.log.error( { error : err.toString() }, 'Error during new scan');
}
return cb(err);
}
);
});
}
};

View file

@ -1,9 +1,7 @@
/* jslint node: true */
'use strict';
var MenuModule = require('./menu_module.js').MenuModule;
exports.getModule = StandardMenuModule;
const MenuModule = require('./menu_module.js').MenuModule;
exports.moduleInfo = {
name : 'Standard Menu Module',
@ -11,30 +9,19 @@ exports.moduleInfo = {
author : 'NuSkooler',
};
function StandardMenuModule(menuConfig) {
MenuModule.call(this, menuConfig);
}
exports.getModule = class StandardMenuModule extends MenuModule {
constructor(options) {
super(options);
}
require('util').inherits(StandardMenuModule, MenuModule);
mciReady(mciData, cb) {
super.mciReady(mciData, err => {
if(err) {
return cb(err);
}
StandardMenuModule.prototype.enter = function() {
StandardMenuModule.super_.prototype.enter.call(this);
};
StandardMenuModule.prototype.beforeArt = function(cb) {
StandardMenuModule.super_.prototype.beforeArt.call(this, cb);
};
StandardMenuModule.prototype.mciReady = function(mciData, cb) {
var self = this;
StandardMenuModule.super_.prototype.mciReady.call(this, mciData, function mciReadyComplete(err) {
if(err) {
cb(err);
} else {
// we do this so other modules can be both customized and still perform standard tasks
StandardMenuModule.super_.prototype.standardMCIReadyHandler.call(self, mciData, cb);
}
});
return this.standardMCIReadyHandler(mciData, cb);
});
}
};

View file

@ -1,17 +1,15 @@
/* jslint node: true */
'use strict';
var MenuModule = require('./menu_module.js').MenuModule;
var ViewController = require('./view_controller.js').ViewController;
var theme = require('./theme.js');
var sysValidate = require('./system_view_validate.js');
const MenuModule = require('./menu_module.js').MenuModule;
const ViewController = require('./view_controller.js').ViewController;
const theme = require('./theme.js');
const sysValidate = require('./system_view_validate.js');
var async = require('async');
var assert = require('assert');
var _ = require('lodash');
var moment = require('moment');
exports.getModule = UserConfigModule;
const async = require('async');
const assert = require('assert');
const _ = require('lodash');
const moment = require('moment');
exports.moduleInfo = {
name : 'User Configuration',
@ -19,7 +17,7 @@ exports.moduleInfo = {
author : 'NuSkooler',
};
var MciCodeIds = {
const MciCodeIds = {
RealName : 1,
BirthDate : 2,
Sex : 3,
@ -37,192 +35,187 @@ var MciCodeIds = {
SaveCancel : 25,
};
function UserConfigModule(options) {
MenuModule.call(this, options);
exports.getModule = class UserConfigModule extends MenuModule {
constructor(options) {
super(options);
var self = this;
self.getView = function(viewId) {
return self.viewControllers.menu.getView(viewId);
};
const self = this;
self.setViewText = function(viewId, text) {
var v = self.getView(viewId);
if(v) {
v.setText(text);
}
};
this.menuMethods = {
//
// Validation support
//
validateEmailAvail : function(data, cb) {
this.menuMethods = {
//
// If nothing changed, we know it's OK
// Validation support
//
if(self.client.user.properties.email_address.toLowerCase() === data.toLowerCase()) {
return cb(null);
}
// Otherwise we can use the standard system method
return sysValidate.validateEmailAvail(data, cb);
},
validatePassword : function(data, cb) {
//
// Blank is OK - this means we won't be changing it
//
if(!data || 0 === data.length) {
return cb(null);
}
// Otherwise we can use the standard system method
return sysValidate.validatePasswordSpec(data, cb);
},
validatePassConfirmMatch : function(data, cb) {
var passwordView = self.getView(MciCodeIds.Password);
cb(passwordView.getData() === data ? null : new Error('Passwords do not match'));
},
viewValidationListener : function(err, cb) {
var errMsgView = self.getView(MciCodeIds.ErrorMsg);
var newFocusId;
if(errMsgView) {
if(err) {
errMsgView.setText(err.message);
if(err.view.getId() === MciCodeIds.PassConfirm) {
newFocusId = MciCodeIds.Password;
var passwordView = self.getView(MciCodeIds.Password);
passwordView.clearText();
err.view.clearText();
}
} else {
errMsgView.clearText();
}
}
cb(newFocusId);
},
//
// Handlers
//
saveChanges : function(formData, extraArgs, cb) {
assert(formData.value.password === formData.value.passwordConfirm);
const newProperties = {
real_name : formData.value.realName,
birthdate : new Date(Date.parse(formData.value.birthdate)).toISOString(),
sex : formData.value.sex,
location : formData.value.location,
affiliation : formData.value.affils,
email_address : formData.value.email,
web_address : formData.value.web,
term_height : formData.value.termHeight.toString(),
theme_id : self.availThemeInfo[formData.value.theme].themeId,
};
// runtime set theme
theme.setClientTheme(self.client, newProperties.theme_id);
// persist all changes
self.client.user.persistProperties(newProperties, err => {
if(err) {
self.client.log.warn( { error : err.toString() }, 'Failed persisting updated properties');
// :TODO: warn end user!
return self.prevMenu(cb);
}
validateEmailAvail : function(data, cb) {
//
// New password if it's not empty
// If nothing changed, we know it's OK
//
self.client.log.info('User updated properties');
if(formData.value.password.length > 0) {
self.client.user.setNewAuthCredentials(formData.value.password, err => {
if(err) {
self.client.log.error( { err : err }, 'Failed storing new authentication credentials');
} else {
self.client.log.info('User changed authentication credentials');
}
return self.prevMenu(cb);
});
} else {
return self.prevMenu(cb);
if(self.client.user.properties.email_address.toLowerCase() === data.toLowerCase()) {
return cb(null);
}
});
},
};
}
require('util').inherits(UserConfigModule, MenuModule);
UserConfigModule.prototype.mciReady = function(mciData, cb) {
var self = this;
var vc = self.viewControllers.menu = new ViewController( { client : self.client} );
var currentThemeIdIndex = 0;
async.series(
[
function callParentMciReady(callback) {
UserConfigModule.super_.prototype.mciReady.call(self, mciData, callback);
},
function loadFromConfig(callback) {
vc.loadFromMenuConfig( { callingMenu : self, mciMap : mciData.menu }, callback);
},
function prepareAvailableThemes(callback) {
self.availThemeInfo = _.sortBy(_.map(theme.getAvailableThemes(), function makeThemeInfo(t, themeId) {
return {
themeId : themeId,
name : t.info.name,
author : t.info.author,
desc : _.isString(t.info.desc) ? t.info.desc : '',
group : _.isString(t.info.group) ? t.info.group : '',
};
}), 'name');
currentThemeIdIndex = _.findIndex(self.availThemeInfo, function cmp(ti) {
return ti.themeId === self.client.user.properties.theme_id;
});
callback(null);
// Otherwise we can use the standard system method
return sysValidate.validateEmailAvail(data, cb);
},
function populateViews(callback) {
var user = self.client.user;
self.setViewText(MciCodeIds.RealName, user.properties.real_name);
self.setViewText(MciCodeIds.BirthDate, moment(user.properties.birthdate).format('YYYYMMDD'));
self.setViewText(MciCodeIds.Sex, user.properties.sex);
self.setViewText(MciCodeIds.Loc, user.properties.location);
self.setViewText(MciCodeIds.Affils, user.properties.affiliation);
self.setViewText(MciCodeIds.Email, user.properties.email_address);
self.setViewText(MciCodeIds.Web, user.properties.web_address);
self.setViewText(MciCodeIds.TermHeight, user.properties.term_height.toString());
validatePassword : function(data, cb) {
//
// Blank is OK - this means we won't be changing it
//
if(!data || 0 === data.length) {
return cb(null);
}
// Otherwise we can use the standard system method
return sysValidate.validatePasswordSpec(data, cb);
},
validatePassConfirmMatch : function(data, cb) {
var passwordView = self.getView(MciCodeIds.Password);
cb(passwordView.getData() === data ? null : new Error('Passwords do not match'));
},
viewValidationListener : function(err, cb) {
var errMsgView = self.getView(MciCodeIds.ErrorMsg);
var newFocusId;
if(errMsgView) {
if(err) {
errMsgView.setText(err.message);
var themeView = self.getView(MciCodeIds.Theme);
if(themeView) {
themeView.setItems(_.map(self.availThemeInfo, 'name'));
themeView.setFocusItemIndex(currentThemeIdIndex);
if(err.view.getId() === MciCodeIds.PassConfirm) {
newFocusId = MciCodeIds.Password;
var passwordView = self.getView(MciCodeIds.Password);
passwordView.clearText();
err.view.clearText();
}
} else {
errMsgView.clearText();
}
}
cb(newFocusId);
},
//
// Handlers
//
saveChanges : function(formData, extraArgs, cb) {
assert(formData.value.password === formData.value.passwordConfirm);
var realNameView = self.getView(MciCodeIds.RealName);
if(realNameView) {
realNameView.setFocus(true); // :TODO: HACK! menu.hjson sets focus, but manual population above breaks this. Needs a real fix!
}
const newProperties = {
real_name : formData.value.realName,
birthdate : new Date(Date.parse(formData.value.birthdate)).toISOString(),
sex : formData.value.sex,
location : formData.value.location,
affiliation : formData.value.affils,
email_address : formData.value.email,
web_address : formData.value.web,
term_height : formData.value.termHeight.toString(),
theme_id : self.availThemeInfo[formData.value.theme].themeId,
};
callback(null);
}
],
function complete(err) {
// runtime set theme
theme.setClientTheme(self.client, newProperties.theme_id);
// persist all changes
self.client.user.persistProperties(newProperties, err => {
if(err) {
self.client.log.warn( { error : err.toString() }, 'Failed persisting updated properties');
// :TODO: warn end user!
return self.prevMenu(cb);
}
//
// New password if it's not empty
//
self.client.log.info('User updated properties');
if(formData.value.password.length > 0) {
self.client.user.setNewAuthCredentials(formData.value.password, err => {
if(err) {
self.client.log.error( { err : err }, 'Failed storing new authentication credentials');
} else {
self.client.log.info('User changed authentication credentials');
}
return self.prevMenu(cb);
});
} else {
return self.prevMenu(cb);
}
});
},
};
}
getView(viewId) {
return this.viewControllers.menu.getView(viewId);
}
mciReady(mciData, cb) {
super.mciReady(mciData, err => {
if(err) {
self.client.log.warn( { error : err.toString() }, 'User configuration failed to init');
self.prevMenu();
} else {
cb(null);
return cb(err);
}
}
);
const self = this;
const vc = self.viewControllers.menu = new ViewController( { client : self.client} );
let currentThemeIdIndex = 0;
async.series(
[
function loadFromConfig(callback) {
vc.loadFromMenuConfig( { callingMenu : self, mciMap : mciData.menu }, callback);
},
function prepareAvailableThemes(callback) {
self.availThemeInfo = _.sortBy(_.map(theme.getAvailableThemes(), function makeThemeInfo(t, themeId) {
return {
themeId : themeId,
name : t.info.name,
author : t.info.author,
desc : _.isString(t.info.desc) ? t.info.desc : '',
group : _.isString(t.info.group) ? t.info.group : '',
};
}), 'name');
currentThemeIdIndex = _.findIndex(self.availThemeInfo, function cmp(ti) {
return ti.themeId === self.client.user.properties.theme_id;
});
callback(null);
},
function populateViews(callback) {
var user = self.client.user;
self.setViewText('menu', MciCodeIds.RealName, user.properties.real_name);
self.setViewText('menu', MciCodeIds.BirthDate, moment(user.properties.birthdate).format('YYYYMMDD'));
self.setViewText('menu', MciCodeIds.Sex, user.properties.sex);
self.setViewText('menu', MciCodeIds.Loc, user.properties.location);
self.setViewText('menu', MciCodeIds.Affils, user.properties.affiliation);
self.setViewText('menu', MciCodeIds.Email, user.properties.email_address);
self.setViewText('menu', MciCodeIds.Web, user.properties.web_address);
self.setViewText('menu', MciCodeIds.TermHeight, user.properties.term_height.toString());
var themeView = self.getView(MciCodeIds.Theme);
if(themeView) {
themeView.setItems(_.map(self.availThemeInfo, 'name'));
themeView.setFocusItemIndex(currentThemeIdIndex);
}
var realNameView = self.getView(MciCodeIds.RealName);
if(realNameView) {
realNameView.setFocus(true); // :TODO: HACK! menu.hjson sets focus, but manual population above breaks this. Needs a real fix!
}
callback(null);
}
],
function complete(err) {
if(err) {
self.client.log.warn( { error : err.toString() }, 'User configuration failed to init');
self.prevMenu();
} else {
cb(null);
}
}
);
});
}
};