2015-06-25 04:45:21 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
var MenuModule = require('../core/menu_module.js').MenuModule;
|
|
|
|
var ViewController = require('../core/view_controller.js').ViewController;
|
|
|
|
var ansi = require('../core/ansi_term.js');
|
|
|
|
var theme = require('../core/theme.js');
|
|
|
|
var MultiLineEditTextView = require('../core/multi_line_edit_text_view.js').MultiLineEditTextView;
|
2015-07-13 04:56:33 +00:00
|
|
|
var Message = require('../core/message.js').Message;
|
2015-06-25 04:45:21 +00:00
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
var async = require('async');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2015-06-25 05:09:26 +00:00
|
|
|
|
2015-06-25 22:33:17 +00:00
|
|
|
exports.getModule = FullScreenEditorModule;
|
2015-06-25 04:45:21 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2015-06-25 22:33:17 +00:00
|
|
|
name : 'Full Screen Editor (FSE)',
|
|
|
|
desc : 'A full screen editor/viewer',
|
2015-06-25 04:45:21 +00:00
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
2015-06-25 22:33:17 +00:00
|
|
|
function FullScreenEditorModule(options) {
|
2015-06-25 05:09:26 +00:00
|
|
|
MenuModule.call(this, options);
|
|
|
|
|
2015-06-26 04:34:33 +00:00
|
|
|
var self = this;
|
|
|
|
this.menuConfig = options.menuConfig;
|
2015-07-13 20:58:45 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Editor Type ('editorType'):
|
|
|
|
// * netMail : User to user, optionally remote
|
|
|
|
// *
|
2015-06-26 04:34:33 +00:00
|
|
|
this.editorType = this.menuConfig.config.editorType;
|
2015-07-13 20:58:45 +00:00
|
|
|
|
2015-07-04 18:02:37 +00:00
|
|
|
this.artNames = [ 'header', 'body', 'footerEdit', 'footerEditMenu', 'footerView' ];
|
2015-07-09 04:07:25 +00:00
|
|
|
|
|
|
|
// :TODO: This needs to be passed in via args:
|
2015-07-13 20:58:45 +00:00
|
|
|
this.editorMode = 'edit'; // view | edit | editMenu |
|
|
|
|
this.messageAreaId = 'netMail' === this.editorType ? Message.WellKnownAreaIds.Private : options.messageAreaId;
|
2015-07-09 04:07:25 +00:00
|
|
|
|
|
|
|
this.getFooterName = function(editorMode) {
|
|
|
|
editorMode = editorMode || this.editorMode;
|
|
|
|
return 'footer' + _.capitalize(editorMode); // e.g.. 'footerEditMenu'
|
|
|
|
};
|
|
|
|
|
2015-07-10 05:23:37 +00:00
|
|
|
this.getFormId = function(name) {
|
2015-07-09 04:07:25 +00:00
|
|
|
return {
|
|
|
|
header : 0,
|
|
|
|
body : 1,
|
|
|
|
footerEdit : 2,
|
|
|
|
footerEditMenu : 3,
|
2015-07-11 22:39:42 +00:00
|
|
|
footerView : 4,
|
|
|
|
|
|
|
|
help : 50,
|
2015-07-10 05:23:37 +00:00
|
|
|
}[name];
|
2015-07-09 04:07:25 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 20:58:45 +00:00
|
|
|
this.getMessageData = function() {
|
|
|
|
var headerValues = self.viewControllers.header.getFormData().value;
|
2015-07-13 04:56:33 +00:00
|
|
|
|
|
|
|
var messageOpts = {
|
2015-07-13 20:58:45 +00:00
|
|
|
areaId : self.messageAreaId,
|
|
|
|
toUserName : headerValues.to,
|
|
|
|
fromUserName : headerValues.from,
|
|
|
|
subject : headerValues.subject,
|
|
|
|
message : self.viewControllers.body.getFormData().value.message,
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
console.log(messageOpts);
|
|
|
|
|
|
|
|
/*var msg = new Message(messageOpts);
|
|
|
|
msg.persist(function persisted(err, msgId) {
|
|
|
|
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
2015-07-09 04:07:25 +00:00
|
|
|
this.redrawFooter = function(options, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function moveToFooterPosition(callback) {
|
|
|
|
//
|
|
|
|
// Calculate footer staring position
|
|
|
|
//
|
|
|
|
// row = (header height + body height)
|
|
|
|
//
|
|
|
|
// Header: mciData.body.height
|
|
|
|
// Body : We must find this in the config / theme
|
|
|
|
//
|
2015-07-10 21:24:30 +00:00
|
|
|
// :TODO: don't hard code this -- allow footer height to be part of theme/etc.
|
2015-07-09 04:07:25 +00:00
|
|
|
self.client.term.rawWrite(ansi.goto(23, 1));
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function clearFooterArea(callback) {
|
|
|
|
if(options.clear) {
|
2015-07-10 21:24:30 +00:00
|
|
|
self.client.term.rawWrite(ansi.reset() + ansi.deleteLine(3));
|
2015-07-09 04:07:25 +00:00
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function displayFooterArt(callback) {
|
|
|
|
var footerArt = self.menuConfig.config.art[options.footerName];
|
|
|
|
|
|
|
|
self.displayArtAsset(footerArt, function artDisplayed(err, artData) {
|
|
|
|
callback(err, artData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, artData) {
|
|
|
|
cb(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
this.redrawScreen = function(options, cb) {
|
|
|
|
var comps = [ 'header', 'body' ];
|
|
|
|
var art = self.menuConfig.config.art;
|
|
|
|
|
|
|
|
self.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function displayHeaderAndBody(callback) {
|
|
|
|
async.eachSeries( comps, function dispArt(n, next) {
|
|
|
|
self.displayArtAsset(art[n], function artDisplayed(err, artData) {
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}, function complete(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function displayFooter(callback) {
|
|
|
|
// we have to treat the footer special
|
|
|
|
self.redrawFooter( { clear : false, footerName : self.getFooterName() }, function footerDisplayed(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function refreshViews(callback) {
|
|
|
|
comps.push(self.getFooterName());
|
|
|
|
|
|
|
|
comps.forEach(function artComp(n) {
|
|
|
|
self.viewControllers[n].redrawAll();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-07-09 04:07:25 +00:00
|
|
|
this.switchFooter = function(cb) {
|
|
|
|
var footerName = self.getFooterName();
|
|
|
|
|
|
|
|
self.redrawFooter( { footerName : footerName, clear : true }, function artDisplayed(err, artData) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var formId = self.getFormId(footerName);
|
|
|
|
|
|
|
|
if(_.isUndefined(self.viewControllers[footerName])) {
|
|
|
|
var menuLoadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
formId : formId,
|
|
|
|
mciMap : artData.mciMap
|
|
|
|
};
|
|
|
|
|
|
|
|
self.addViewController(
|
|
|
|
footerName,
|
|
|
|
new ViewController( { client : self.client, formId : formId } )
|
|
|
|
).loadFromMenuConfig(menuLoadOpts, function footerReady(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.viewControllers[footerName].redrawAll();
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-25 05:09:26 +00:00
|
|
|
this.initSequence = function() {
|
2015-06-26 04:34:33 +00:00
|
|
|
var mciData = { };
|
2015-07-04 18:02:37 +00:00
|
|
|
var art = self.menuConfig.config.art;
|
|
|
|
assert(_.isObject(art));
|
2015-06-26 04:34:33 +00:00
|
|
|
|
2015-07-06 01:05:55 +00:00
|
|
|
async.series(
|
2015-06-25 05:09:26 +00:00
|
|
|
[
|
|
|
|
function beforeDisplayArt(callback) {
|
|
|
|
self.beforeArt();
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-07-09 04:07:25 +00:00
|
|
|
function displayHeaderAndBodyArt(callback) {
|
2015-07-04 18:02:37 +00:00
|
|
|
assert(_.isString(art.header));
|
|
|
|
assert(_.isString(art.body));
|
|
|
|
|
|
|
|
async.eachSeries( [ 'header', 'body' ], function dispArt(n, next) {
|
|
|
|
self.displayArtAsset(art[n], function artDisplayed(err, artData) {
|
|
|
|
mciData[n] = artData;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}, function complete(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
2015-07-09 04:07:25 +00:00
|
|
|
function displayFooter(callback) {
|
|
|
|
var footerName = self.getFooterName();
|
|
|
|
self.redrawFooter( { footerName : footerName }, function artDisplayed(err, artData) {
|
2015-07-06 01:05:55 +00:00
|
|
|
mciData[footerName] = artData;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
2015-06-26 04:34:33 +00:00
|
|
|
function afterArtDisplayed(callback) {
|
|
|
|
self.mciReady(mciData);
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-07-04 18:02:37 +00:00
|
|
|
this.mciReadyHandlerNetMail = function(mciData) {
|
|
|
|
|
|
|
|
var menuLoadOpts = { callingMenu : self };
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function header(callback) {
|
2015-07-09 04:07:25 +00:00
|
|
|
menuLoadOpts.formId = self.getFormId('header');
|
2015-07-04 18:02:37 +00:00
|
|
|
menuLoadOpts.mciMap = mciData.header.mciMap;
|
|
|
|
|
|
|
|
self.addViewController(
|
|
|
|
'header',
|
2015-07-09 04:07:25 +00:00
|
|
|
new ViewController( { client : self.client, formId : menuLoadOpts.formId } )
|
2015-07-04 18:02:37 +00:00
|
|
|
).loadFromMenuConfig(menuLoadOpts, function headerReady(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function body(callback) {
|
2015-07-09 04:07:25 +00:00
|
|
|
menuLoadOpts.formId = self.getFormId('body');
|
2015-07-04 18:02:37 +00:00
|
|
|
menuLoadOpts.mciMap = mciData.body.mciMap;
|
|
|
|
|
|
|
|
self.addViewController(
|
|
|
|
'body',
|
2015-07-09 04:07:25 +00:00
|
|
|
new ViewController( { client : self.client, formId : menuLoadOpts.formId } )
|
2015-07-04 18:02:37 +00:00
|
|
|
).loadFromMenuConfig(menuLoadOpts, function bodyReady(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-07-06 01:05:55 +00:00
|
|
|
},
|
|
|
|
function footer(callback) {
|
2015-07-09 04:07:25 +00:00
|
|
|
var footerName = self.getFooterName();
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-09 04:07:25 +00:00
|
|
|
menuLoadOpts.formId = self.getFormId(footerName);
|
2015-07-06 01:05:55 +00:00
|
|
|
menuLoadOpts.mciMap = mciData[footerName].mciMap;
|
|
|
|
|
|
|
|
self.addViewController(
|
|
|
|
footerName,
|
2015-07-09 04:07:25 +00:00
|
|
|
new ViewController( { client : self.client, formId : menuLoadOpts.formId } )
|
2015-07-06 01:05:55 +00:00
|
|
|
).loadFromMenuConfig(menuLoadOpts, function footerReady(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-07-04 18:02:37 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
2015-07-12 02:12:07 +00:00
|
|
|
var bodyView = self.viewControllers.body.getView(1);
|
2015-07-06 05:16:22 +00:00
|
|
|
self.updateTextEditMode(bodyView.getTextEditMode());
|
|
|
|
self.updateEditModePosition(bodyView.getEditPosition());
|
|
|
|
|
2015-07-09 04:34:40 +00:00
|
|
|
self.viewControllers.body.setFocus(false);
|
2015-07-04 18:02:37 +00:00
|
|
|
self.viewControllers.header.switchFocus(1);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-07-06 01:05:55 +00:00
|
|
|
this.updateEditModePosition = function(pos) {
|
|
|
|
if('edit' === this.editorMode) {
|
2015-07-09 04:07:25 +00:00
|
|
|
var posView = self.viewControllers.footerEdit.getView(1);
|
2015-07-06 01:05:55 +00:00
|
|
|
if(posView) {
|
2015-07-06 05:16:22 +00:00
|
|
|
self.client.term.rawWrite(ansi.savePos());
|
|
|
|
posView.setText(_.padLeft(String(pos.row + 1), 2, '0') + ',' + _.padLeft(String(pos.col + 1), 2, '0'));
|
|
|
|
self.client.term.rawWrite(ansi.restorePos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.updateTextEditMode = function(mode) {
|
|
|
|
if('edit' === this.editorMode) {
|
2015-07-09 04:07:25 +00:00
|
|
|
var modeView = self.viewControllers.footerEdit.getView(2);
|
2015-07-06 05:16:22 +00:00
|
|
|
if(modeView) {
|
|
|
|
self.client.term.rawWrite(ansi.savePos());
|
|
|
|
modeView.setText('insert' === mode ? 'INS' : 'OVR');
|
|
|
|
self.client.term.rawWrite(ansi.restorePos());
|
2015-07-06 01:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-12 02:12:07 +00:00
|
|
|
/*
|
2015-07-11 22:39:42 +00:00
|
|
|
this.displayHelp = function() {
|
|
|
|
//
|
|
|
|
// Replace body area with a temporary read-only MultiLineEditText
|
|
|
|
// with help contents. ESC or 'Q' closes back to previous state.
|
|
|
|
//
|
|
|
|
var formId = self.getFormId('help');
|
|
|
|
|
|
|
|
if(_.isUndefined(self.viewControllers.help)) {
|
|
|
|
self.addViewController('help', new ViewController( { client : self.client, formId : formId } ));
|
|
|
|
|
|
|
|
var helpViewOpts = {
|
|
|
|
position : self.getBodyView().position,
|
|
|
|
//dimens : self.getBodyView().dimens,
|
|
|
|
acceptsFocus : true,
|
|
|
|
acceptsInput : true,
|
|
|
|
id : 1,
|
|
|
|
client : self.client,
|
|
|
|
ansiSGR : ansi.sgr( [ 'normal', 'reset' ] ), // :TODO: use a styleSGRx here; default to white on black
|
|
|
|
};
|
|
|
|
|
|
|
|
var helpView = new MultiLineEditTextView(helpViewOpts);
|
|
|
|
// :TODO: this is to work around a bug... dimens in ctor should be enough!
|
|
|
|
helpView.setWidth(self.getBodyView().dimens.width);
|
|
|
|
helpView.setHeight(self.getBodyView().dimens.height);
|
|
|
|
helpView.setText('Some help text...')
|
|
|
|
|
|
|
|
self.viewControllers.help.addView(helpView);
|
|
|
|
self.viewControllers.help.switchFocus(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.viewControllers.help.redrawAll();
|
|
|
|
};
|
2015-07-12 02:12:07 +00:00
|
|
|
*/
|
2015-07-11 22:39:42 +00:00
|
|
|
|
2015-07-12 02:12:07 +00:00
|
|
|
this.displayHelp = function() {
|
2015-07-11 22:39:42 +00:00
|
|
|
self.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
|
|
|
|
theme.displayThemeArt( { name : self.menuConfig.config.art.help, client : self.client },
|
2015-07-12 02:12:07 +00:00
|
|
|
function helpDisplayed(err, artData) {
|
2015-07-11 22:39:42 +00:00
|
|
|
self.client.waitForKeyPress(function keyPress(ch, key) {
|
|
|
|
self.redrawScreen();
|
|
|
|
self.viewControllers.footerEditMenu.setFocus(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-07-09 22:28:44 +00:00
|
|
|
this.observeEditEvents = function() {
|
2015-07-12 02:12:07 +00:00
|
|
|
var bodyView = self.viewControllers.body.getView(1);
|
2015-07-09 22:28:44 +00:00
|
|
|
|
|
|
|
bodyView.on('edit position', function cursorPosUpdate(pos) {
|
|
|
|
self.updateEditModePosition(pos);
|
|
|
|
});
|
|
|
|
|
|
|
|
bodyView.on('text edit mode', function textEditMode(mode) {
|
|
|
|
self.updateTextEditMode(mode);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-06-25 05:12:03 +00:00
|
|
|
|
2015-06-27 21:32:29 +00:00
|
|
|
this.menuMethods = {
|
2015-07-04 18:02:37 +00:00
|
|
|
headerSubmit : function(formData, extraArgs) {
|
2015-07-09 04:34:40 +00:00
|
|
|
self.viewControllers.header.setFocus(false);
|
2015-07-04 18:02:37 +00:00
|
|
|
self.viewControllers.body.switchFocus(1);
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-09 22:28:44 +00:00
|
|
|
self.observeEditEvents();
|
2015-07-04 18:02:37 +00:00
|
|
|
},
|
2015-07-09 04:49:22 +00:00
|
|
|
editModeEscPressed : function(formData, extraArgs) {
|
|
|
|
self.editorMode = 'edit' === self.editorMode ? 'editMenu' : 'edit';
|
2015-07-10 05:23:37 +00:00
|
|
|
|
2015-07-09 04:07:25 +00:00
|
|
|
self.switchFooter(function next(err) {
|
|
|
|
if(err) {
|
|
|
|
// :TODO:... what now?
|
|
|
|
console.log(err)
|
|
|
|
} else {
|
2015-07-09 04:49:22 +00:00
|
|
|
switch(self.editorMode) {
|
|
|
|
case 'edit' :
|
|
|
|
if(!_.isUndefined(self.viewControllers.footerEditMenu)) {
|
|
|
|
self.viewControllers.footerEditMenu.setFocus(false);
|
|
|
|
}
|
|
|
|
self.viewControllers.body.switchFocus(1);
|
2015-07-09 22:28:44 +00:00
|
|
|
self.observeEditEvents();
|
2015-07-09 04:49:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'editMenu' :
|
|
|
|
self.viewControllers.body.setFocus(false);
|
|
|
|
self.viewControllers.footerEditMenu.switchFocus(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default : throw new Error('Unexpected mode');
|
|
|
|
}
|
|
|
|
|
2015-07-09 04:07:25 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-09 04:49:22 +00:00
|
|
|
},
|
2015-07-12 02:12:07 +00:00
|
|
|
editModeMenuSave : function(formData, extraArgs) {
|
2015-07-13 20:58:45 +00:00
|
|
|
self.getMessageData();
|
2015-07-12 06:32:31 +00:00
|
|
|
},
|
|
|
|
editModeMenuQuote : function(formData, extraArgs) {
|
|
|
|
|
2015-07-12 02:12:07 +00:00
|
|
|
},
|
|
|
|
editModeMenuHelp : function(formData, extraArgs) {
|
|
|
|
self.viewControllers.footerEditMenu.setFocus(false);
|
|
|
|
self.displayHelp();
|
2015-06-27 21:32:29 +00:00
|
|
|
}
|
2015-06-25 05:09:26 +00:00
|
|
|
};
|
2015-06-25 04:45:21 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 22:33:17 +00:00
|
|
|
require('util').inherits(FullScreenEditorModule, MenuModule);
|
2015-06-25 05:09:26 +00:00
|
|
|
|
2015-06-25 22:33:17 +00:00
|
|
|
FullScreenEditorModule.prototype.enter = function(client) {
|
|
|
|
FullScreenEditorModule.super_.prototype.enter.call(this, client);
|
2015-06-25 05:09:26 +00:00
|
|
|
};
|
|
|
|
|
2015-06-26 04:34:33 +00:00
|
|
|
FullScreenEditorModule.prototype.mciReady = function(mciData) {
|
|
|
|
this['mciReadyHandler' + _.capitalize(this.editorType)](mciData);
|
|
|
|
};
|
|
|
|
|