2016-08-11 04:48:13 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const theme = require('./theme.js');
|
|
|
|
const resetScreen = require('./ansi_term.js').resetScreen;
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const renderStringLength = require('./string_util.js').renderStringLength;
|
2018-11-26 02:05:16 +00:00
|
|
|
const SystemLogKeys = require('./system_log.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2016-08-11 04:48:13 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Rumorz',
|
|
|
|
desc : 'Standard local rumorz',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
packageName : 'codes.l33t.enigma.rumorz',
|
2016-08-11 04:48:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
2018-06-23 03:26:46 +00:00
|
|
|
View : 0,
|
|
|
|
Add : 1,
|
2016-08-11 04:48:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciCodeIds = {
|
2018-06-23 03:26:46 +00:00
|
|
|
ViewForm : {
|
|
|
|
Entries : 1,
|
|
|
|
AddPrompt : 2,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
AddForm : {
|
2018-06-23 03:26:46 +00:00
|
|
|
NewEntry : 1,
|
|
|
|
EntryPreview : 2,
|
|
|
|
AddPrompt : 3,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-08-11 04:48:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class RumorzModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
viewAddScreen : (formData, extraArgs, cb) => {
|
|
|
|
return this.displayAddScreen(cb);
|
|
|
|
},
|
|
|
|
|
|
|
|
addEntry : (formData, extraArgs, cb) => {
|
|
|
|
if(_.isString(formData.value.rumor) && renderStringLength(formData.value.rumor) > 0) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const rumor = formData.value.rumor.trim(); // remove any trailing ws
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.appendSystemLogEntry(
|
|
|
|
SystemLogKeys.UserAddedRumorz,
|
|
|
|
rumor,
|
|
|
|
StatLog.KeepDays.Forever,
|
|
|
|
StatLog.KeepType.Forever,
|
|
|
|
() => {
|
|
|
|
this.clearAddForm();
|
|
|
|
return this.displayViewScreen(true, cb); // true=cls
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// empty message - treat as if cancel was hit
|
|
|
|
return this.displayViewScreen(true, cb); // true=cls
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelAdd : (formData, extraArgs, cb) => {
|
|
|
|
this.clearAddForm();
|
2018-06-23 03:26:46 +00:00
|
|
|
return this.displayViewScreen(true, cb); // true=cls
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get config() { return this.menuConfig.config; }
|
|
|
|
|
|
|
|
clearAddForm() {
|
2018-06-23 03:26:46 +00:00
|
|
|
const newEntryView = this.viewControllers.add.getView(MciCodeIds.AddForm.NewEntry);
|
|
|
|
const previewView = this.viewControllers.add.getView(MciCodeIds.AddForm.EntryPreview);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
newEntryView.setText('');
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// preview is optional
|
2018-06-22 05:15:04 +00:00
|
|
|
if(previewView) {
|
|
|
|
previewView.setText('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beforeDisplayArt(callback) {
|
|
|
|
self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function display(callback) {
|
|
|
|
self.displayViewScreen(false, callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Handle me -- initSequence() should really take a completion callback
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayViewScreen(clearScreen, cb) {
|
|
|
|
const self = this;
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function clearAndDisplayArt(callback) {
|
|
|
|
if(self.viewControllers.add) {
|
|
|
|
self.viewControllers.add.setFocus(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(clearScreen) {
|
|
|
|
self.client.term.rawWrite(resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
theme.displayThemedAsset(
|
|
|
|
self.config.art.entries,
|
|
|
|
self.client,
|
|
|
|
{ font : self.menuConfig.font, trailingLF : false },
|
|
|
|
(err, artData) => {
|
|
|
|
return callback(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function initOrRedrawViewController(artData, callback) {
|
|
|
|
if(_.isUndefined(self.viewControllers.add)) {
|
|
|
|
const vc = self.addViewController(
|
|
|
|
'view',
|
|
|
|
new ViewController( { client : self.client, formId : FormIds.View } )
|
|
|
|
);
|
|
|
|
|
|
|
|
const loadOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : artData.mciMap,
|
|
|
|
formId : FormIds.View,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
} else {
|
|
|
|
self.viewControllers.view.setFocus(true);
|
|
|
|
self.viewControllers.view.getView(MciCodeIds.ViewForm.AddPrompt).redraw();
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function fetchEntries(callback) {
|
|
|
|
const entriesView = self.viewControllers.view.getView(MciCodeIds.ViewForm.Entries);
|
|
|
|
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.getSystemLogEntries(SystemLogKeys.UserAddedRumorz, StatLog.Order.Timestamp, (err, entries) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err, entriesView, entries);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function populateEntries(entriesView, entries, callback) {
|
2018-11-06 04:35:12 +00:00
|
|
|
entriesView.setItems(entries.map(e => {
|
|
|
|
return {
|
|
|
|
text : e.log_value, // standard
|
|
|
|
rumor : e.log_value,
|
2018-11-26 02:05:16 +00:00
|
|
|
};
|
2018-11-06 04:35:12 +00:00
|
|
|
}));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
entriesView.redraw();
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function finalPrep(callback) {
|
|
|
|
const promptView = self.viewControllers.view.getView(MciCodeIds.ViewForm.AddPrompt);
|
2018-06-23 03:26:46 +00:00
|
|
|
promptView.setFocusItemIndex(1); // default to NO
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayAddScreen(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function clearAndDisplayArt(callback) {
|
|
|
|
self.viewControllers.view.setFocus(false);
|
|
|
|
self.client.term.rawWrite(resetScreen());
|
|
|
|
|
|
|
|
theme.displayThemedAsset(
|
|
|
|
self.config.art.add,
|
|
|
|
self.client,
|
|
|
|
{ font : self.menuConfig.font },
|
|
|
|
(err, artData) => {
|
|
|
|
return callback(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function initOrRedrawViewController(artData, callback) {
|
|
|
|
if(_.isUndefined(self.viewControllers.add)) {
|
|
|
|
const vc = self.addViewController(
|
|
|
|
'add',
|
|
|
|
new ViewController( { client : self.client, formId : FormIds.Add } )
|
|
|
|
);
|
|
|
|
|
|
|
|
const loadOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : artData.mciMap,
|
|
|
|
formId : FormIds.Add,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
} else {
|
|
|
|
self.viewControllers.add.setFocus(true);
|
|
|
|
self.viewControllers.add.redrawAll();
|
|
|
|
self.viewControllers.add.switchFocus(MciCodeIds.AddForm.NewEntry);
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function initPreviewUpdates(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const previewView = self.viewControllers.add.getView(MciCodeIds.AddForm.EntryPreview);
|
|
|
|
const entryView = self.viewControllers.add.getView(MciCodeIds.AddForm.NewEntry);
|
2018-06-22 05:15:04 +00:00
|
|
|
if(previewView) {
|
|
|
|
let timerId;
|
|
|
|
entryView.on('key press', () => {
|
|
|
|
clearTimeout(timerId);
|
|
|
|
timerId = setTimeout( () => {
|
|
|
|
const focused = self.viewControllers.add.getFocusedView();
|
|
|
|
if(focused === entryView) {
|
|
|
|
previewView.setText(entryView.getData());
|
|
|
|
focused.setFocus(true);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-08-11 04:48:13 +00:00
|
|
|
};
|