2015-08-26 05:17:09 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var MenuModule = require('../core/menu_module.js').MenuModule;
|
2015-08-27 05:04:04 +00:00
|
|
|
var ViewController = require('../core/view_controller.js').ViewController;
|
2015-08-28 04:20:24 +00:00
|
|
|
var messageArea = require('../core/message_area.js');
|
2015-09-01 05:18:46 +00:00
|
|
|
var Message = require('../core/message.js');
|
2015-08-27 05:04:04 +00:00
|
|
|
|
|
|
|
//var moment = require('moment');
|
|
|
|
var async = require('async');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2015-10-11 22:05:45 +00:00
|
|
|
var moment = require('moment');
|
2015-08-26 05:17:09 +00:00
|
|
|
|
2015-10-19 03:24:13 +00:00
|
|
|
/*
|
|
|
|
Available listFormat/focusListFormat members (VM1):
|
|
|
|
|
|
|
|
msgNum : Message number
|
|
|
|
to : To username/handle
|
|
|
|
from : From username/handle
|
|
|
|
subj : Subject
|
|
|
|
ts : Message mod timestamp (format with config.dateTimeFormat)
|
|
|
|
newIndicator : New mark/indicator (config.newIndicator)
|
|
|
|
|
|
|
|
MCI codes:
|
|
|
|
|
|
|
|
VM1 : Message list
|
|
|
|
TL2 : Message area description
|
|
|
|
TL4 : Message selected #
|
|
|
|
TL5 : Total messages in area
|
|
|
|
*/
|
|
|
|
|
2015-08-26 05:17:09 +00:00
|
|
|
exports.getModule = MessageListModule;
|
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Message List',
|
|
|
|
desc : 'Module for listing/browsing available messages',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
2015-10-11 22:05:45 +00:00
|
|
|
var MciCodesIds = {
|
|
|
|
MsgList : 1,
|
|
|
|
MsgAreaDesc : 2,
|
|
|
|
|
|
|
|
MsgSelNum : 4,
|
|
|
|
MsgTotal : 5,
|
|
|
|
};
|
|
|
|
|
2015-08-26 05:17:09 +00:00
|
|
|
function MessageListModule(options) {
|
|
|
|
MenuModule.call(this, options);
|
|
|
|
|
2015-09-07 06:12:01 +00:00
|
|
|
var self = this;
|
|
|
|
var config = this.menuConfig.config;
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
this.messageAreaTag = config.messageAreaTag;
|
2016-01-04 00:59:29 +00:00
|
|
|
|
|
|
|
if(options.extraArgs) {
|
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// |extraArgs| can override |messageAreaTag| provided by config
|
2016-01-04 00:59:29 +00:00
|
|
|
// as well as supply a pre-defined message list
|
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
if(options.extraArgs.messageAreaTag) {
|
|
|
|
this.messageAreaTag = options.extraArgs.messageAreaTag;
|
2016-01-04 00:59:29 +00:00
|
|
|
}
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2016-01-04 00:59:29 +00:00
|
|
|
if(options.extraArgs.messageList) {
|
|
|
|
this.messageList = options.extraArgs.messageList;
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 03:42:24 +00:00
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
selectMessage : function(formData, extraArgs) {
|
|
|
|
if(1 === formData.submitId) {
|
2015-09-06 21:58:58 +00:00
|
|
|
var modOpts = {
|
|
|
|
extraArgs : {
|
2016-02-03 04:35:59 +00:00
|
|
|
messageAreaTag : self.messageAreaTag,
|
2015-09-06 21:58:58 +00:00
|
|
|
messageList : self.messageList,
|
2015-09-07 06:12:01 +00:00
|
|
|
messageIndex : formData.value.message,
|
2015-09-06 21:58:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-04 06:15:49 +00:00
|
|
|
self.gotoMenu(config.menuViewPost || 'messageAreaViewPost', modOpts);
|
2015-08-31 03:42:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-10-11 22:05:45 +00:00
|
|
|
|
|
|
|
this.setViewText = function(id, text) {
|
|
|
|
var v = self.viewControllers.allViews.getView(id);
|
|
|
|
if(v) {
|
|
|
|
v.setText(text);
|
|
|
|
}
|
|
|
|
};
|
2015-08-26 05:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(MessageListModule, MenuModule);
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
MessageListModule.prototype.enter = function() {
|
|
|
|
MessageListModule.super_.prototype.enter.call(this);
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2016-01-04 00:59:29 +00:00
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// Config can specify |messageAreaTag| else it comes from
|
2016-01-04 00:59:29 +00:00
|
|
|
// the user's current area
|
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
if(!this.messageAreaTag) {
|
|
|
|
this.messageAreaTag = this.client.user.properties.message_area_tag;
|
2015-09-04 06:02:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-27 05:04:04 +00:00
|
|
|
MessageListModule.prototype.mciReady = function(mciData, cb) {
|
|
|
|
var self = this;
|
2015-10-11 22:05:45 +00:00
|
|
|
var vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
2015-08-27 05:04:04 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
var firstNewEntryIndex;
|
|
|
|
|
2015-08-31 03:42:24 +00:00
|
|
|
async.series(
|
2015-08-27 05:04:04 +00:00
|
|
|
[
|
|
|
|
function callParentMciReady(callback) {
|
2015-10-12 05:26:27 +00:00
|
|
|
MessageListModule.super_.prototype.mciReady.call(self, mciData, callback);
|
2015-08-27 05:04:04 +00:00
|
|
|
},
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
var loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu
|
|
|
|
};
|
|
|
|
|
|
|
|
vc.loadFromMenuConfig(loadOpts, callback);
|
2015-08-27 05:25:49 +00:00
|
|
|
},
|
|
|
|
function fetchMessagesInArea(callback) {
|
2016-01-04 00:59:29 +00:00
|
|
|
//
|
|
|
|
// Config can supply messages else we'll need to populate the list now
|
|
|
|
//
|
|
|
|
if(_.isArray(self.messageList)) {
|
|
|
|
callback(0 === self.messageList.length ? new Error('No messages in area') : null);
|
|
|
|
} else {
|
2016-02-03 04:35:59 +00:00
|
|
|
messageArea.getMessageListForArea( { client : self.client }, self.messageAreaTag, function msgs(err, msgList) {
|
2016-06-17 04:35:27 +00:00
|
|
|
if(!msgList || 0 === msgList.length) {
|
2016-01-04 00:59:29 +00:00
|
|
|
callback(new Error('No messages in area'));
|
|
|
|
} else {
|
|
|
|
self.messageList = msgList;
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-08-28 04:20:24 +00:00
|
|
|
},
|
2015-10-22 21:44:44 +00:00
|
|
|
function getLastReadMesageId(callback) {
|
2016-02-03 04:35:59 +00:00
|
|
|
messageArea.getMessageAreaLastReadId(self.client.user.userId, self.messageAreaTag, function lastRead(err, lastReadId) {
|
2015-10-22 21:44:44 +00:00
|
|
|
self.lastReadId = lastReadId || 0;
|
|
|
|
callback(null); // ignore any errors, e.g. missing value
|
|
|
|
});
|
|
|
|
},
|
2015-08-31 03:42:24 +00:00
|
|
|
function populateList(callback) {
|
2015-10-11 22:05:45 +00:00
|
|
|
var msgListView = vc.getView(MciCodesIds.MsgList);
|
2015-08-28 04:20:24 +00:00
|
|
|
|
2015-10-12 02:52:13 +00:00
|
|
|
// :TODO: fix default format
|
2015-10-22 21:44:44 +00:00
|
|
|
var listFormat = self.menuConfig.config.listFormat || '{msgNum} - {subj} - {to}';
|
2015-10-18 17:48:08 +00:00
|
|
|
var focusListFormat = self.menuConfig.config.focusListFormat || listFormat; // :TODO: default change color here
|
2015-12-14 00:31:16 +00:00
|
|
|
var dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM Do';
|
2015-10-19 03:24:13 +00:00
|
|
|
var newIndicator = self.menuConfig.config.newIndicator || '*';
|
2015-10-10 05:35:40 +00:00
|
|
|
|
2015-08-28 04:20:24 +00:00
|
|
|
var msgNum = 1;
|
2015-10-19 03:24:13 +00:00
|
|
|
|
|
|
|
function getMsgFmtObj(mle) {
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
if(_.isUndefined(firstNewEntryIndex) &&
|
|
|
|
mle.messageId > self.lastReadId)
|
|
|
|
{
|
|
|
|
firstNewEntryIndex = msgNum - 1;
|
|
|
|
}
|
|
|
|
|
2015-10-19 03:24:13 +00:00
|
|
|
return {
|
|
|
|
msgNum : msgNum++,
|
|
|
|
subj : mle.subject,
|
|
|
|
from : mle.fromUserName,
|
|
|
|
to : mle.toUserName,
|
|
|
|
ts : moment(mle.modTimestamp).format(dateTimeFormat),
|
2015-10-22 21:44:44 +00:00
|
|
|
newIndicator : mle.messageId > self.lastReadId ? newIndicator : '',
|
2015-11-05 06:04:55 +00:00
|
|
|
};
|
2015-10-19 03:24:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 03:42:24 +00:00
|
|
|
msgListView.setItems(_.map(self.messageList, function formatMsgListEntry(mle) {
|
2015-10-19 03:24:13 +00:00
|
|
|
return listFormat.format(getMsgFmtObj(mle));
|
2015-08-28 04:20:24 +00:00
|
|
|
}));
|
|
|
|
|
2015-10-19 03:24:13 +00:00
|
|
|
msgNum = 1;
|
|
|
|
msgListView.setFocusItems(_.map(self.messageList, function formatMsgListEntry(mle) {
|
|
|
|
return focusListFormat.format(getMsgFmtObj(mle));
|
2015-10-21 04:37:39 +00:00
|
|
|
}));
|
2015-10-10 05:35:40 +00:00
|
|
|
|
2015-10-11 22:05:45 +00:00
|
|
|
msgListView.on('index update', function indexUpdated(idx) {
|
|
|
|
self.setViewText(MciCodesIds.MsgSelNum, (idx + 1).toString());
|
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2015-08-28 04:20:24 +00:00
|
|
|
msgListView.redraw();
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
if(firstNewEntryIndex > 0) {
|
|
|
|
msgListView.setFocusItemIndex(firstNewEntryIndex);
|
|
|
|
}
|
2015-09-01 05:18:46 +00:00
|
|
|
|
|
|
|
callback(null);
|
2015-10-11 22:05:45 +00:00
|
|
|
},
|
|
|
|
function populateOtherMciViews(callback) {
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
self.setViewText(MciCodesIds.MsgAreaDesc, messageArea.getMessageAreaByTag(self.messageAreaTag).name);
|
2015-10-12 02:52:13 +00:00
|
|
|
self.setViewText(MciCodesIds.MsgSelNum, (vc.getView(MciCodesIds.MsgList).getData() + 1).toString());
|
2015-10-11 22:05:45 +00:00
|
|
|
self.setViewText(MciCodesIds.MsgTotal, self.messageList.length.toString());
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
2015-08-27 05:04:04 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
2015-08-28 04:20:24 +00:00
|
|
|
if(err) {
|
2015-10-19 03:24:13 +00:00
|
|
|
self.client.log.error( { error : err.toString() }, 'Error loading message list');
|
2016-06-17 04:35:27 +00:00
|
|
|
|
2015-08-28 04:20:24 +00:00
|
|
|
}
|
2015-08-27 05:04:04 +00:00
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|