/
VirtualMailbox.service.js
220 lines (191 loc) · 6.05 KB
/
VirtualMailbox.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
(function() {
'use strict';
/**
* @name VirtualMailbox
* @constructor
* @param {object} account - the mail account associated with the virtual search
*/
function VirtualMailbox(account) {
this.$account = account;
}
/**
* @memberof VirtualMailbox
* @desc The factory we'll use to register with Angular
* @returns the VirtualMailbox constructor
*/
VirtualMailbox.$factory = ['$q', '$timeout', '$log', 'sgSettings', 'Message', 'Mailbox', 'sgMailbox_PRELOAD', function($q, $timeout, $log, Settings, Mailbox, Message, PRELOAD) {
angular.extend(VirtualMailbox, {
$q: $q,
$timeout: $timeout,
$log: $log,
$Message: Message,
selectedFolder: null,
PRELOAD: PRELOAD
});
return VirtualMailbox; // return constructor
}];
/**
* @module SOGo.MailerUI
* @desc Factory registration of VirtualMailbox in Angular module.
*/
try {
angular.module('SOGo.MailerUI');
}
catch(e) {
angular.module('SOGo.MailerUI', ['SOGo.Common']);
}
angular.module('SOGo.MailerUI')
.constant('sgMailbox_PRELOAD', {
LOOKAHEAD: 50,
SIZE: 100
})
.factory('VirtualMailbox', VirtualMailbox.$factory);
/**
* @memberof VirtualMailbox
* @desc Build the path of the virtual mailbox (or account only).
* @param {string} accountId - the account ID
* @returns a string representing the path relative to the mail module
*/
VirtualMailbox.$absolutePath = function(accountId) {
return [accountId, "virtual"].join('/');
};
/**
* @function init
* @memberof Mailbox.prototype
* @desc Extend instance with new data and compute additional attributes.
* @param {object} data - attributes of mailbox
*/
VirtualMailbox.prototype.init = function(data) {
this.$isLoading = false;
this.$mailboxes = [];
this.uidsMap = {};
angular.extend(this, data);
this.id = this.$id();
};
VirtualMailbox.prototype.setMailboxes = function(data) {
this.$mailboxes = data;
_.each(this.$mailboxes, function(mailbox) {
mailbox.$messages = [];
mailbox.uidsMap = {};
});
};
VirtualMailbox.prototype.startSearch = function(match, params) {
var _this = this,
search = VirtualMailbox.$q.when();
this.$isLoading = true;
_.each(this.$mailboxes, function(mailbox) {
search = search.then(function() {
if (_this.$isLoading) {
VirtualMailbox.$log.debug("searching mailbox " + mailbox.path);
return mailbox.$filter( {sort: "date", asc: false, match: match}, params);
}
});
});
search.finally(function() { _this.$isLoading = false; });
};
VirtualMailbox.prototype.stopSearch = function() {
VirtualMailbox.$log.debug("stopping search...");
this.$isLoading = false;
};
/**
* @function getLength
* @memberof Mailbox.prototype
* @desc Used by md-virtual-repeat / md-on-demand
* @returns the number of items in the mailbox
*/
VirtualMailbox.prototype.getLength = function() {
var len = 0;
if (!angular.isDefined(this.$mailboxes))
return len;
_.each(this.$mailboxes, function(mailbox) {
len += mailbox.$messages.length;
});
return len;
};
/**
* @function getItemAtIndex
* @memberof Mailbox.prototype
* @desc Used by md-virtual-repeat / md-on-demand
* @returns the message as the specified index
*/
VirtualMailbox.prototype.getItemAtIndex = function(index) {
var i, j, k, mailbox, message;
if (angular.isDefined(this.$mailboxes) && index >= 0) {
i = 0;
for (j = 0; j < this.$mailboxes.length; j++) {
mailbox = this.$mailboxes[j];
for (k = 0; k < mailbox.$messages.length; i++, k++) {
message = mailbox.$messages[k];
if (i == index) {
if (mailbox.$loadMessage(message.uid))
return message;
}
}
}
}
return null;
};
/**
* @function $id
* @memberof VirtualMailbox.prototype
* @desc Build the unique ID to identified the mailbox.
* @returns a string representing the path relative to the mail module
*/
VirtualMailbox.prototype.$id = function() {
return VirtualMailbox.$absolutePath(this.$account.id);
};
/**
* @function $selectedCount
* @memberof VirtualMailbox.prototype
* @desc Return the number of messages selected by the user.
* @returns the number of selected messages
*/
VirtualMailbox.prototype.$selectedCount = function() {
// TODO
return 0;
};
/**
* @function $flagMessages
* @memberof VirtualMailbox.prototype
* @desc Add or remove a flag on a message set
* @returns a promise of the HTTP operation
*/
VirtualMailbox.prototype.$flagMessages = function(uids, flags, operation) {
// TODO
// var data = {msgUIDs: uids,
// flags: flags,
// operation: operation};
// return VirtualMailbox.$$resource.post(this.id, 'addOrRemoveLabel', data);
};
/**
* @function $deleteMessages
* @memberof VirtualMailbox.prototype
* @desc Delete multiple messages from mailbox.
* @return a promise of the HTTP operation
*/
VirtualMailbox.prototype.$deleteMessages = function(uids) {
// TODO
//return VirtualMailbox.$$resource.post(this.id, 'batchDelete', {uids: uids});
};
/**
* @function $copyMessages
* @memberof VirtualMailbox.prototype
* @desc Copy multiple messages from the current mailbox to a target one
* @return a promise of the HTTP operation
*/
VirtualMailbox.prototype.$copyMessages = function(uids, folder) {
// TODO
//return VirtualMailbox.$$resource.post(this.id, 'copyMessages', {uids: uids, folder: folder});
};
/**
* @function $moveMessages
* @memberof VirtualMailbox.prototype
* @desc Move multiple messages from the current mailbox to a target one
* @return a promise of the HTTP operation
*/
VirtualMailbox.prototype.$moveMessages = function(uids, folder) {
// TODO
//return VirtualMailbox.$$resource.post(this.id, 'moveMessages', {uids: uids, folder: folder});
};
})();