Repository navigation
Expand file tree
/
Copy pathmodel.js
More file actions
281 lines (243 loc) · 8.66 KB
/
Copy pathmodel.js
File metadata and controls
281 lines (243 loc) · 8.66 KB
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<
8000
/div>
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*global define, console */
define(function(require) {
var evt = require('evt');
function dieOnFatalError(msg) {
console.error('FATAL:', msg);
throw new Error(msg);
}
/**
* Provides a front end to the API and slice objects returned from the API.
* Since the UI right now is driven by a shared set of slices, this module
* tracks those slices and creates events when they are changed. This means
* the card modules do not need a direct reference to each other to change
* the backing data for a card, and that card modules and app logic do not
* need a hard, static dependency on the MailAPI object. This allows some
* more flexible and decoupled loading scenarios. In particular, cards can
* be created an inserted into the DOM without needing the back end to
* complete its startup and initialization.
*
* It mixes in 'evt' capabilities, so it will be common to see model
* used with 'latest' and 'latestOnce' to get the latest model data
* whenever it loads.
*
* Down the road, it may make sense to have more than one model object
* in play. At that point, it may make sense to morph this into a
* constructor function and then have the card objects receive a model
* instance property for their model reference.
*
* @type {Object}
*/
var model = {
firstRun: null,
/**
* acctsSlice event is fired when the property changes.
* event: acctsSlice
* @param {Object} the acctsSlice object.
**/
acctsSlice: null,
/**
* account event is fired when the property changes.
* event: account
* @param {Object} the account object.
**/
account: null,
/**
* foldersSlice event is fired when the property changes.
* event: foldersSlice
* @param {Object} the foldersSlice object.
**/
foldersSlice: null,
/**
* folder event is fired when the property changes.
* event: folder
* @param {Object} the folder object.
**/
folder: null,
/**
* emits an event based on a property value. Since the
* event is based on a property value that is on this
* object, *do not* use emitWhenListener, since, due to
* the possibility of queuing old values with that
* method, it could cause bad results (bug 971617), and
* it is not needed since the latest* methods will get
* the latest value on this object.
* @param {String} id event ID/property name
*/
_callEmit: function(id) {
this.emit(id, this[id]);
},
inited: false,
/**
* Returns true if there is an account. Should only be
* called after inited is true.
*/
hasAccount: function() {
return (model.getAccountCount() > 0);
},
/**
* Given an account ID, get the account object. Only works once the
* acctsSlice property is available. Use model.latestOnce to get a
* handle on an acctsSlice property, then call this method.
* @param {String} id account ID.
* @return {Object} account object.
*/
getAccount: function(id) {
if (!model.acctsSlice || !model.acctsSlice.items)
throw new Error('No acctsSlice available');
var targetAccount;
model.acctsSlice.items.some(function(account) {
if (account.id === id)
return !!(targetAccount = account);
});
return targetAccount;
},
/**
* Get the numbers of configured account.
* Should only be called after this.inited is true.
* @return {Number} numbers of account.
*/
getAccountCount: function() {
var count = 0;
if (model.acctsSlice &&
model.acctsSlice.items &&
model.acctsSlice.items.length) {
count = model.acctsSlice.items.length;
}
return count;
},
/**
* Call this to initialize the model. It can be called more than once
* per the lifetime of an app. The usual use case for multiple calls
* is when a new account has been added.
* @param {boolean} showLatest Choose the latest account in the
* acctsSlice. Otherwise it choose the account marked as the default
* account.
*/
init: function(showLatest, callback) {
// Set inited to false to indicate initialization is in progress.
this.inited = false;
require(['api'], function(MailAPI) {
if (!this.api) {
this.api = MailAPI;
this._callEmit('api', this.api);
}
// If already initialized before, clear out previous state.
this.die();
var acctsSlice = this.acctsSlice = MailAPI.viewAccounts(false);
acctsSlice. {
if (acctsSlice.items.length) {
// For now, just use the first one; we do attempt to put unified
// first so this should generally do the right thing.
// XXX: Because we don't have unified account now, we should
// switch to the latest account which user just added.
var account = showLatest ? acctsSlice.items.slice(-1)[0] :
acctsSlice.defaultAccount;
this.changeAccount(account, callback);
}
this.inited = true;
this._callEmit('acctsSlice');
}).bind(this);
}.bind(this));
},
/**
* Changes the current account tracked by the model. This results
* in changes to the 'account', 'foldersSlice' and 'folder' properties.
* @param {Object} account the account object.
* @param {Function} callback function to call once the account and
* related folder data has changed.
*/
changeAccount: function(account, callback) {
// Do not bother if account is the same.
if (this.account && this.account.id === account.id) {
if (callback)
callback();
return;
}
this._dieFolders();
this.account = account;
this._callEmit('account');
var foldersSlice = this.api.viewFolders('account', account);
foldersSlice. {
this.foldersSlice = foldersSlice;
this.selectInbox(callback);
this._callEmit('foldersSlice');
}).bind(this);
},
/**
* Given an account ID, change the current account to that account.
* @param {String} accountId
* @return {Function} callback
*/
changeAccountFromId: function(accountId, callback) {
if (!this.acctsSlice || !this.acctsSlice.items.length)
throw new Error('No accounts available');
this.acctsSlice.items.some(function(account) {
if (account.id === accountId) {
this.changeAccount(account, callback);
return true;
}
}.bind(this));
},
/**
* Just changes the folder property tracked by the model.
* Assumes the folder still belongs to the currently tracked
* account. It also does not result in any state changes or
* event emitting if the new folder is the same as the
* currently tracked folder.
* @param {Object} folder the folder object to use.
*/
changeFolder: function(folder) {
if (folder && (!this.folder || folder.id !== this.folder.id)) {
this.folder = folder;
this._callEmit('folder');
}
},
/**
* For the already loaded account and associated foldersSlice,
* set the inbox as the tracked 'folder'.
* @param {Function} callback function called once the inbox
* has been selected.
*/
selectInbox: function(callback) {
if (!this.foldersSlice)
throw new Error('No foldersSlice available');
var inboxFolder = this.foldersSlice.getFirstFolderWithType('inbox');
if (!inboxFolder)
dieOnFatalError('We have an account without an inbox!',
this.foldersSlice.items);
if (this.folder && this.folder.id === inboxFolder.id) {
if (callback)
callback();
} else {
if (callback)
this.once('folder', callback);
this.changeFolder(inboxFolder);
}
},
/**
* Called by other code when it knows the current account
* has received new inbox messages. Just triggers an
* event with the count for now.
* @param {Object} accountUpdate update object from
* sync.js accountResults object structure.
*/
notifyInboxMessages: function(accountUpdate) {
if (accountUpdate.id === this.account.id)
model.emit('newInboxMessages', accountUpdate.count);
},
_dieFolders: function() {
if (this.foldersSlice)
this.foldersSlice.die();
this.foldersSlice = null;
this.folder = null;
},
die: function() {
if (this.acctsSlice)
this.acctsSlice.die();
this.acctsSlice = null;
this.account = null;
this._dieFolders();
}
};
return evt.mix(model);
});