forked from ebidel/polymer-gmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
453 lines (348 loc) · 12.2 KB
/
app.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
(function() {
var DEBUG = location.search.indexOf('debug') != -1;
var FROM_HEADER_REGEX = new RegExp(/"?(.*?)"?\s?<(.*)>/);
var Labels = {
UNREAD: 'UNREAD',
STARRED: 'STARRED'
};
var previouslySelected = [];
var GMail = window.GMail || {};
GMail.fetchMail = function(q, opt_callback) {
var gmail = gapi.client.gmail.users;
// Fetch only the emails in the user's inbox.
gmail.threads.list({userId: 'me', q: q}).then(function(resp) {
var threads = resp.result.threads;
var batch = gapi.client.newBatch();
threads.forEach(function(thread, i) {
var req = gmail.threads.get({userId: 'me', 'id': thread.id});
batch.add(req);
req.then(function(resp) {
thread.messages = fixUpMessages(resp).reverse();
//thread.archived = false;
// Set entire thread data at once, when it's all been processed.
template.job('addthreads', function() {
this.threads = threads;
opt_callback && opt_callback(threads);
}, 100);
});
});
batch.then();
});
};
function getValueForHeaderField(headers, field) {
for (var i = 0, header; header = headers[i]; ++i) {
if (header.name == field || header.name == field.toLowerCase()) {
return header.value;
}
}
return null;
}
function getAllUserProfileImages(users, nextPageToken, callback) {
gapi.client.plus.people.list({
userId: 'me', collection: 'visible', pageToken: nextPageToken
}).then(function(resp) {
users = resp.result.items.reduce(function(o, v, i) {
o[v.displayName] = v.image.url;
return o;
}, users);
if (resp.result.nextPageToken) {
getAllUserProfileImages(users, resp.result.nextPageToken, callback);
} else {
callback(users);
}
});
}
function fixUpMessages(resp) {
var messages = resp.result.messages;
for (var j = 0, m; m = messages[j]; ++j) {
var headers = m.payload.headers;
// Example: Thu Sep 25 2014 14:43:18 GMT-0700 (PDT) -> Sept 25.
var date = new Date(getValueForHeaderField(headers, 'Date'));
m.date = date.toDateString().split(' ').slice(1, 3).join(' ');
m.to = getValueForHeaderField(headers, 'To');
m.subject = getValueForHeaderField(headers, 'Subject');
var fromHeaders = getValueForHeaderField(headers, 'From');
var fromHeaderMatches = fromHeaders.match(FROM_HEADER_REGEX);
m.from = {};
// Use name if one was found. Otherwise, use email address.
if (fromHeaderMatches) {
// If no a name, use email address for displayName.
m.from.name = fromHeaderMatches[1].length ? fromHeaderMatches[1] :
fromHeaderMatches[2];
m.from.email = fromHeaderMatches[2];
} else {
m.from.name = fromHeaders.split('@')[0];
m.from.email = fromHeaders;
}
m.from.name = m.from.name.split('@')[0]; // Ensure email is split.
m.unread = m.labelIds.indexOf(Labels.UNREAD) != -1;
m.starred = m.labelIds.indexOf(Labels.STARRED) != -1;
}
return messages;
}
var template = document.querySelector('#t');
template.toggleSearch = function() {
this.$.search.toggle();
};
template.undoAll = function(e, detail, sender) {
e.stopPropagation();
for (var i = 0, threadEl; threadEl = previouslySelected[i]; ++i) {
threadEl.archived = false;
}
previouslySelected = [];
};
template.refreshInbox = function(opt_callback) {
var q = 'in:inbox';
if (opt_callback) {
GMail.fetchMail(q, opt_callback.bind(this));
} else {
GMail.fetchMail(q);
}
};
template.onToastOpenClose = function(e, opened, sender) {
if (opened) {
this.$.fab.classList.add('moveup');
// for (var i = 0, threadEl; threadEl = previouslySelected[i]; ++i) {
// threadEl.undo = false; // hide in-place UNDO UI.
// }
} else {
previouslySelected = [];
this.$.fab.classList.remove('moveup');
}
};
template.onRefreshStart = function(e, detail, sender) {
if (this.syncing) {
return;
}
var atTop = this.$.scrollheader.scroller.scrollTop == 0;
if (atTop && e.yDirection > 0) {
this.refreshStarted = true;
this.$.refreshspinner.active = true;
} else if (!this.refreshStarted) {
template.touchAction = 'pan-y';
}
};
template.onMainAreaTrack = function(e, detail, sender) {
if (this.syncing) {
return;
}
var y = Math.min(e.dy, this.MAX_REFRESH_Y);
this.$.refreshspinner.style.opacity =
Math.min(1, 1 - ((this.MAX_REFRESH_Y - e.dy) / this.MAX_REFRESH_Y));
var style = this.$.refresh.style;
style.transform = style.webkitTransform = 'translate3d(0, ' + y + 'px, 0)';
if (!this.refreshStarted) {
// TODO(ericbidelman): fake scrolling. We're already in a touch event, and
// scrolling won't kick in until after the user releaes. Ask Dan about this.
this.$.scrollheader.scroller.scrollTop = Math.abs(e.dy);
}
};
template.onRefreshUp = function(e, detail, sender) {
if (!this.refreshStarted || this.syncing) {
return;
}
var style = this.$.refresh.style;
style.transform = style.webkitTransform = '';
var threshhold = this.MAX_REFRESH_Y / 2;
if (e.dy >= threshhold) {
this.syncing = true;
this.$.refreshspinner.style.opacity = 1;
this.$.refresh.classList.add('snapback');
} else {
this.$.refresh.classList.add('snapback', 'tostart');
}
};
template.onRefreshTransitionEnd = function(e, detail, sender) {
if (!this.refreshStarted) {
return;
}
this.refreshStarted = false;
if (this.$.refresh.classList.contains('tostart')) {
this.$.refresh.classList.remove('snapback', 'shrink', 'tostart');
return;
}
this.refreshInbox(function(threads) {
this.$.refreshspinner.active = false;
this.$.refresh.classList.add('shrink');
this.async(function() {
this.$.refresh.classList.remove('snapback', 'shrink', 'tostart');
this.$.refreshspinner.style.opacity = 0;
this.syncing = false;
}, null, 150);
});
};
template.newMail = function(e, detail, sender) {
console.warn('Not implemented: Create new mail');
};
template.menuSelect = function(e, detail, sender) {
if (detail.isSelected) {
this.$ && this.$.drawerPanel.togglePanel();
}
};
template.deselectAll = function(e, detail, sender) {
this.selectedThreads = [];
};
// Archives currently selected messages.
template.archiveAll = function(e, detail, sender) {
e.stopPropagation();
for (var i = 0, threadEl; threadEl = this.$.threadlist.selectedItem[i]; ++i) {
threadEl.archived = true;
previouslySelected.push(threadEl);
}
this.toastMessage = this.selectedThreads.length + ' archived';
this.async(function() {
this.$.toast.show();
}, null, 1000); // delay showing the toast.
};
// TODO(ericbidelman): listenOnce is defined in core-transition
/**
* Utility function to listen to an event on a node once.
*
* @method listenOnce
* @param {Node} node The animated node
* @param {string} event Name of an event
* @param {Function} fn Event handler
* @param {Array} args Additional arguments to pass to `fn`
*/
template.listenOnce = function(node, event, fn, args) {
var self = this;
var listener = function() {
fn.apply(self, args);
node.removeEventListener(event, listener, false);
};
node.addEventListener(event, listener, false);
};
template.onThreadArchive = function(e, detail, sender) {
// When user interacts with app, remove any visibly archived threads,
// then remove touch listener.
if (!detail.showUndo) {
return;
}
// TODO: if user archive/undos several times, this adds a listener each time.
this.listenOnce(this.$.scrollheader, 'scroll', function() {
for (var i = 0, threadEl; threadEl = this.$.threadlist.items[i]; ++i) {
if (threadEl.archived) {
threadEl.classList.add('shrink');
threadEl.undo = false; // hide in-place UNDO UI.
}
}
});
};
template.onSigninFailure = function(e, detail, sender) {
if (DEBUG) {
return;
}
this.isAuthenticated = false;
};
template.onSigninSuccess = function(e, detail, sender) {
this.isAuthenticated = true;
// Cached data? We're already using it. Bomb out before making unnecessary requests.
if ((template.threads && template.users) || DEBUG) {
return;
}
this.gapi = e.detail.gapi;
gapi.client.load('gmail', 'v1').then(function() {
var gmail = gapi.client.gmail.users;
template.refreshInbox();
gmail.labels.list({userId: 'me'}).then(function(resp) {
// Don't include system labels.
var labels = resp.result.labels.filter(function(label, i) {
label.color = template.LABEL_COLORS[
Math.round(Math.random() * template.LABEL_COLORS.length)];
return label.type != 'system';
});
template.labels = labels;
template.labelMap = labels.reduce(function(o, v, i) {
o[v.id] = v;
return o;
}, {});
});
});
gapi.client.load('plus', 'v1').then(function() {
// Get user's profile pic, cover image, email, and name.
gapi.client.plus.people.get({userId: 'me'}).then(function(resp) {
var PROFILE_IMAGE_SIZE = 75;
var COVER_IMAGE_SIZE = 315;
var img = resp.result.image && resp.result.image.url.replace(/(.+)\?sz=\d\d/, "$1?sz=" + PROFILE_IMAGE_SIZE);
var coverImg = resp.result.cover && resp.result.cover.coverPhoto.url.replace(/\/s\d{3}-/, "/s" + COVER_IMAGE_SIZE + "-");
template.user = {
name: resp.result.displayName,
email: resp.result.emails[0].value,
profile: img || null,
cover: coverImg || null
};
template.$['navheaderstyle'].coverImg = coverImg;
template.$.navheader.classList.add('coverimg');
var users = {};
getAllUserProfileImages(users, null, function(users) {
template.users = users;
template.users[template.user.name] = template.user.profile; // signed in user.
});
});
});
};
template.LABEL_COLORS = ['pink', 'orange', 'green', 'yellow', 'teal', 'purple'];
// Better UX: presume user is logged in when app loads.
template.isAuthenticated = true;
template.threads = [];
template.selectedThreads = [];
template.touchAction = 'none'; // Allow track events from x/y directions.
template.MAX_REFRESH_Y = 150;
template.syncing = false; // True, if the mail is syncing.
template.refreshStarted = false; // True if the pull to refresh has been enabled.
// TODO: save this from users past searches using core-localstorage.
template.previousSearches = [
"something fun",
"tax forms",
'to: me',
'airline tickets',
'party on saturday'
];
template.addEventListener('template-bound', function(e) {
var headerEl = document.querySelector('#mainheader');
var titleStyle = document.querySelector('.title').style;
this.$.drawerPanel.addEventListener('core-header-transform', function(e) {
if (!headerEl.classList.contains('tall')) {
return;
}
var d = e.detail;
// If at the top, allow swiping and pull down refresh. When scrolled, set
// pan-y so track events don't fire in the y direction.
template.touchAction = d.y == 0 ? 'none' : 'pan-y';
// d.y: the amount that the header moves up
// d.height: the height of the header when it is at its full size
// d.condensedHeight: the height of the header when it is condensed
//scale header's title
var m = d.height - d.condensedHeight;
var scale = Math.max(0.5, (m - d.y) / (m / 0.25) + 0.5);
// var scale = Math.max(0.5, (m - d.y) / (m / 0.4) + 0.5);
titleStyle.transform = titleStyle.transform = 'scale(' + scale + ') translateZ(0)';
// Adjust header's color
//document.querySelector('#mainheader').style.color = (d.y >= d.height - d.condensedHeight) ? '#fff' : '';
});
});
// // Prevent context menu.
// window.oncontextmenu = function() {
// return false;
// };
if (!navigator.onLine || DEBUG) {
document.addEventListener('polymer-ready', function(e) {
var ajax = document.createElement('core-ajax');
ajax.auto = true;
ajax.url = '/data/users.json';
ajax.addEventListener('core-response', function(e) {
template.users = e.detail.response;
});
var ajax2 = document.createElement('core-ajax');
ajax2.auto = true;
ajax2.url = '/data/threads.json';
ajax2.addEventListener('core-response', function(e) {
var threads = e.detail.response;
// for (var i = 0, thread; thread = threads[i]; ++i) {
// thread.archived = false;
// }
template.threads = threads;
});
});
}
})();