likeopera-frontend/Store.js

200 lines
6.5 KiB
JavaScript

import superagent from 'superagent';
import socket_io from 'socket.io-client';
import _ from './I18n.js';
import Util from './Util.js';
const Store = {
data: {
layout: 'message-on-right',
quickReply: true,
msg: null,
threads: false,
accounts: [],
listGroups: [],
messages: []
},
listeners: [],
on: function(cb)
{
this.listeners.push(cb);
},
un: function(cb)
{
for (var i = this.listeners.length; i >= 0; i--)
if (this.listeners[i] == cb)
this.listeners.splice(i, 1);
},
get: function(k)
{
return this.data[k];
},
set: function(k, v)
{
this.data[k] = v;
(this.listeners || []).map(i => i());
},
setAll: function(obj)
{
for (var k in obj)
this.data[k] = obj[k];
(this.listeners || []).map(i => i());
},
startIo: function()
{
var self = this;
this.io = socket_io('', { path: window.location.pathname.replace(/[^\/]+$/, 'backend/socket.io') });
this.io.on('sync', function(params)
{
if (params.state == 'start')
{
self.setAll({ progressText: 'Syncing '+params.email+' / '+params.folder, progressPercent: 0 });
}
else if (params.state == 'progress')
{
self.setAll({ progressPercent: Math.round(100*params.done/(params.total||1)) });
}
else if (params.state == 'finish-box')
{
self.setAll({ progressPercent: 100 });
}
else if (params.state == 'complete')
{
self.setAll({ progressText: '', progressPercent: 0 });
}
self.set('sync', params.progress);
});
},
loadAccounts: function()
{
superagent.get('backend/folders').end(function(err, res)
{
var ixOfAll = {
received: 1,
outbox: 3,
sent: 4,
drafts: 5,
spam: 6,
trash: 7
};
var accounts = [ {
name: _('All Messages'),
accountId: null,
unreadCount: 0,
folders: [
{ name: _('Unread'), icon: 'mail_unread', unreadCount: 0, type: 'unread' },
{ name: _('Received'), icon: 'mail_received', unreadCount: 0, type: 'inbox' },
{ name: _('Pinned'), icon: 'mail_pinned', unreadCount: 0, type: 'pinned' },
{ name: _('Outbox'), icon: 'mail_outbox', unreadCount: 0, type: 'outbox' },
{ name: _('Sent'), icon: 'mail_sent', unreadCount: 0, type: 'sent' },
{ name: _('Drafts'), icon: 'mail_drafts', unreadCount: 0, type: 'drafts' },
{ name: _('Spam'), icon: 'mail_spam', unreadCount: 0, type: 'spam' },
{ name: _('Trash'), icon: 'mail_trash', unreadCount: 0, type: 'trash' },
],
} ];
for (let a of res.body.accounts)
{
let account = {
name: a.name,
email: a.email,
accountId: a.id,
unreadCount: 0,
warning: false,
folders: [
{ name: _('Unread'), icon: 'mail_unread', unreadCount: 0, type: 'unread' },
{ name: _('Pinned'), icon: 'mail_pinned', unreadCount: a.pinned_unread_count, type: 'pinned' },
],
folderMap: a.foldermap,
folderTypes: {}
};
if (!account.folderMap.received)
{
account.folderMap.received = 'INBOX';
}
for (let f in account.folderMap)
{
account.folderTypes[account.folderMap[f]] = f;
}
for (let f of a.folders)
{
let icon = (account.folderTypes[f.name] ? 'mail_'+account.folderTypes[f.name] : 'folder');
account.folders.push({ name: f.name, icon: icon, unreadCount: f.unread_count-0, folderId: f.id });
account.folders[0].unreadCount += (f.unread_count-0);
if (account.folderTypes[f.name])
{
accounts[0].folders[ixOfAll[account.folderTypes[f.name]]].unreadCount += (f.unread_count-0);
}
account.unreadCount += (f.unread_count-0);
}
accounts.push(account);
accounts[0].unreadCount += account.unreadCount;
accounts[0].folders[0].unreadCount += account.unreadCount;
accounts[0].folders[2].unreadCount += account.folders[1].unreadCount;
}
Store.set('accounts', accounts);
});
},
loadFolder: function(folderParams)
{
superagent.get('backend/groups').query(folderParams).end(function(err, res)
{
var groups = res.body.groups.map(g => { return { name: Util.getGroupName(g.name), messageCount: g.count-0, start: 0 } });
var start = 0;
for (var i = 0; i < groups.length; i++)
{
groups[i].start = start;
start += groups[i].messageCount;
}
Store.setAll({
folderParams: folderParams,
listGroups: groups,
messages: []
});
});
},
search: function(text)
{
Store.loadFolder({ ...Store.get('folderParams'), search: text });
},
loadMessages: function(start, count)
{
var p = { ...Store.get('folderParams') };
p.offset = start;
p.limit = count;
superagent.get('backend/messages').query(p).end(function(err, res)
{
var msgs = Store.get('messages').slice(0);
var par = res.body.messages;
par.unshift(par.length);
par.unshift(start);
msgs.splice.apply(msgs, par);
Store.set('messages', msgs);
});
},
loadMessage: function(msgId, callback)
{
superagent.get('backend/message').query({ msgId: msgId }).end(function(err, res)
{
callback(res.body.msg);
});
},
startResync: function()
{
superagent.post('backend/sync').send().end(function(err, res)
{
});
}
};
Store.startIo();
export default Store;