File: /home/barbeatleanalyti/www/public_html/webmail/modules/MailWebclient/js/models/CUidListModel.js
'use strict';
var
_ = require('underscore'),
ko = require('knockout'),
Settings = require('modules/%ModuleName%/js/Settings.js')
;
/**
* @constructor
*
* !!!Attention!!!
* It is not used underscore, because the collection may contain undefined-elements.
* They have their own importance. But all underscore-functions removes them automatically.
*/
function CUidListModel()
{
this.resultCount = ko.observable(-1);
this.search = ko.observable('');
this.filters = ko.observable('');
this.collection = ko.observableArray([]);
this.threadUids = {};
}
/**
* @param {string} sUid
* @param {Array} aThreadUids
*/
CUidListModel.prototype.addThreadUids = function (sUid, aThreadUids)
{
if (-1 !== _.indexOf(this.collection(), sUid))
{
this.threadUids[sUid] = aThreadUids;
}
};
/**
* @param {Object} oResult
*/
CUidListModel.prototype.setUidsAndCount = function (oResult)
{
if (oResult['@Object'] === 'Collection/MessageCollection')
{
_.each(oResult.Uids, function (sUid, iIndex) {
this.collection()[iIndex + oResult.Offset] = sUid.toString();
}, this);
this.resultCount(oResult.MessageResultCount);
}
};
/**
* @param {number} iOffset
* @param {Object} oMessages
*/
CUidListModel.prototype.getUidsForOffset = function (iOffset, oMessages)
{
var
iIndex = 0,
iLen = this.collection().length,
sUid = '',
iExistsCount = 0,
aUids = [],
oMsg = null
;
for(; iIndex < iLen; iIndex++)
{
if (iIndex >= iOffset && iExistsCount < Settings.MailsPerPage) {
sUid = this.collection()[iIndex];
oMsg = oMessages[sUid];
if (oMsg && !oMsg.deleted() || sUid === undefined)
{
iExistsCount++;
if (sUid !== undefined)
{
aUids.push(sUid);
}
}
}
}
return aUids;
};
/**
* @param {Array} aUids
*/
CUidListModel.prototype.deleteUids = function (aUids)
{
var
iIndex = 0,
iLen = this.collection().length,
sUid = '',
aNewCollection = [],
iDiff = 0
;
for (; iIndex < iLen; iIndex++)
{
sUid = this.collection()[iIndex];
if (_.indexOf(aUids, sUid) === -1)
{
aNewCollection.push(sUid);
}
else
{
iDiff++;
}
}
this.collection(aNewCollection);
this.resultCount(this.resultCount() - iDiff);
};
module.exports = CUidListModel;