This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
contacts_details.js
497 lines (431 loc) · 15.2 KB
/
contacts_details.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
'use strict';
var contacts = window.contacts || {};
contacts.Details = (function() {
var photoPos = 7;
var initMargin = 8;
var contactData,
contactDetails,
listContainer,
star,
detailsName,
orgTitle,
birthdayTemplate,
phonesTemplate,
emailsTemplate,
addressesTemplate,
socialTemplate,
notesTemplate,
isFbContact,
isFbLinked,
editContactButton,
cover,
favoriteMessage,
detailsInner,
TAG_OPTIONS,
dom,
currentSocial,
_;
var socialButtonIds = [
'#profile_button',
'#wall_button',
'#msg_button'
];
var init = function cd_init(currentDom) {
_ = navigator.mozL10n.get;
dom = currentDom || document;
contactDetails = dom.querySelector('#contact-detail');
listContainer = dom.querySelector('#details-list');
star = dom.querySelector('#favorite-star');
detailsName = dom.querySelector('#contact-name-title');
orgTitle = dom.querySelector('#org-title');
birthdayTemplate = dom.querySelector('#birthday-template-\\#i\\#');
phonesTemplate = dom.querySelector('#phone-details-template-\\#i\\#');
emailsTemplate = dom.querySelector('#email-details-template-\\#i\\#');
addressesTemplate = dom.querySelector('#address-details-template-\\#i\\#');
socialTemplate = dom.querySelector('#social-template-\\#i\\#');
editContactButton = dom.querySelector('#edit-contact-button');
cover = dom.querySelector('#cover-img');
detailsInner = dom.querySelector('#contact-detail-inner');
favoriteMessage = dom.querySelector('#toggle-favorite');
notesTemplate = dom.querySelector('#note-details-template-\\#i\\#');
initPullEffect(cover);
};
var setContact = function cd_setContact(currentContact) {
contactData = currentContact;
};
var setContainer = function cd_setContainer(container) {
listContainer = container;
};
var initPullEffect = function cd_initPullEffect(cover) {
cover.addEventListener('mousedown', function(event) {
if (event.target != cover || contactDetails.classList.contains('no-photo'))
return;
var startPosition = event.clientY;
contactDetails.classList.add('up');
cover.classList.add('up');
var onMouseMove = function onMouseMove(event) {
var newMargin = event.clientY - startPosition;
if (newMargin > 0 && newMargin < 150) {
contactDetails.classList.remove('up');
cover.classList.remove('up');
var calc = 'calc(' + initMargin + 'rem + ' + newMargin + 'px)';
// Divide by 40 (4 times slower and in rems)
contactDetails.style.transform = 'translateY(' + calc + ')';
var newPos = (-photoPos + (newMargin / 40)) + 'rem';
cover.style.transform = 'translateY(' + newPos + ')';
}
};
var onMouseUp = function onMouseUp(event) {
contactDetails.classList.add('up');
cover.classList.add('up');
contactDetails.style.transform = null;
cover.style.transform = null;
removeEventListener('mousemove', onMouseMove);
removeEventListener('mouseup', onMouseUp);
};
addEventListener('mousemove', onMouseMove);
addEventListener('mouseup', onMouseUp);
});
};
var render = function cd_render(currentContact, tags) {
contactData = currentContact || contactData;
TAG_OPTIONS = tags || TAG_OPTIONS;
isFbContact = fb.isFbContact(contactData);
isFbLinked = fb.isFbLinked(contactData);
// Initially enabled and only disabled if necessary
editContactButton.removeAttribute('disabled');
if (isFbContact) {
var fbContact = new fb.Contact(contactData);
var req = fbContact.getData();
req.onsuccess = function do_reload() {
doReloadContactDetails(req.result);
}
req.onerror = function() {
window.console.error('FB: Error while loading FB contact data');
doReloadContactDetails(contactData);
}
} else {
doReloadContactDetails(contactData);
}
};
//
// Method that generates HTML markup for the contact
//
var doReloadContactDetails = function doReloadContactDetails(contact) {
detailsName.textContent = contact.name;
contactDetails.classList.remove('no-photo');
contactDetails.classList.remove('up');
listContainer.innerHTML = '';
renderFavorite(contact);
renderOrg(contact);
renderBday(contact);
renderPhones(contact);
renderEmails(contact);
renderAddresses(contact);
renderNotes(contact);
renderPhoto(contact);
if (fb.isEnabled) {
renderSocial(contact);
}
};
var renderFavorite = function cd_renderFavorite(contact) {
var favorite = isFavorite(contact);
toggleFavoriteMessage(favorite);
if (contact.category && contact.category.indexOf('favorite') != -1) {
star.classList.remove('hide');
} else {
star.classList.add('hide');
}
};
var isFavorite = function isFavorite(contact) {
return contact != null && contact.category != null &&
contact.category.indexOf('favorite') != -1;
};
var toggleFavorite = function toggleFavorite() {
var contact = contactData;
var favorite = !isFavorite(contact);
toggleFavoriteMessage(favorite);
if (favorite) {
contact.category = contact.category || [];
contact.category.push('favorite');
} else {
if (!contact.category) {
return;
}
var pos = contact.category.indexOf('favorite');
if (pos > -1) {
delete contact.category[pos];
}
}
// Disabling button while saving the contact
favoriteMessage.style.pointerEvents = 'none';
var request = navigator.mozContacts.save(contact);
request.onsuccess = function onsuccess() {
var cList = contacts.List;
/*
Two contacts are returned because the enrichedContact is readonly
and if the Contact is edited we need to prevent saving
FB data on the mozContacts DB.
*/
cList.getContactById(contact.id,
function onSuccess(savedContact, enrichedContact) {
contactData = savedContact;
Contacts.setCurrent(contactData);
if (enrichedContact) {
cList.refresh(enrichedContact);
} else {
cList.refresh(contact);
}
renderFavorite(contactData);
favoriteMessage.style.pointerEvents = 'auto';
}, function onError() {
console.error('Error reloading contact');
favoriteMessage.style.pointerEvents = 'auto';
});
};
request.onerror = function onerror() {
favoriteMessage.style.pointerEvents = 'auto';
console.error('Error saving favorite');
};
};
var toggleFavoriteMessage = function toggleFavMessage(isFav) {
var cList = favoriteMessage.classList;
var text = isFav ? _('removeFavorite') : _('addFavorite');
favoriteMessage.textContent = text;
isFav ? cList.add('on') : cList.remove('on');
};
var renderOrg = function cd_renderOrg(contact) {
if (contact.org && contact.org.length > 0 && contact.org[0] != '') {
orgTitle.textContent = contact.org[0];
orgTitle.className = '';
} else {
orgTitle.className = 'hide';
orgTitle.textContent = '';
}
};
var renderBday = function cd_renderBday(contact) {
if (!contact.bday) {
return;
}
var f = new navigator.mozL10n.DateTimeFormat();
var birthdayFormat = _('birthdayDateFormat') || '%e %B';
var birthdayString = '';
try {
birthdayString = f.localeFormat(contact.bday, birthdayFormat);
} catch (err) {
console.error('Error parsing birthday');
return;
}
var element = utils.templates.render(birthdayTemplate, {
i: contact.id,
bday: birthdayString
});
listContainer.appendChild(element);
};
var renderSocial = function cd_renderSocial(contact) {
var linked = isFbLinked;
var action = linked ? _('social-unlink') : _('social-link');
var slinked = linked ? 'false' : 'true';
var social = utils.templates.render(socialTemplate, {
i: contact.id,
action: action,
linked: slinked
});
currentSocial = social;
var linkButton = social.querySelector('#link_button');
if (!isFbContact) {
socialButtonIds.forEach(function check(id) {
var button = social.querySelector(id);
if (button) {
button.classList.add('hide');
}
});
// Checking whether link should be enabled or not
doDisableButton(linkButton);
} else {
var socialLabel = social.querySelector('#social-label');
if (socialLabel)
socialLabel.textContent = _('facebook');
// Check whether the social buttons that require to be online
// should be there
disableButtons(social, socialButtonIds);
}
// If it is a FB Contact but not linked unlink must be hidden
if (isFbContact && !linked) {
linkButton.classList.add('hide');
}
Contacts.extFb.initEventHandlers(social, contact, linked);
listContainer.appendChild(social);
}
var checkOnline = function(social) {
var socialTemplate = social || currentSocial;
if (socialTemplate) {
if (isFbContact) {
disableButtons(socialTemplate, socialButtonIds);
}
else {
disableButtons(socialTemplate, ['#link_button']);
}
}
}
function disableButtons(tree, buttonIds) {
buttonIds.forEach(function enable(id) {
var button = tree.querySelector(id);
if (button) {
doDisableButton(button);
}
});
}
function doDisableButton(buttonElement) {
if (navigator.onLine === true) {
buttonElement.removeAttribute('disabled');
}
else {
buttonElement.setAttribute('disabled', 'disabled');
}
}
var renderPhones = function cd_renderPhones(contact) {
if (!contact.tel) {
return;
}
var telLength = Contacts.getLength(contact.tel);
for (var tel = 0; tel < telLength; tel++) {
var currentTel = contact.tel[tel];
var escapedType = utils.text.escapeHTML(currentTel.type, true);
var telField = {
value: utils.text.escapeHTML(currentTel.value, true) || '',
type: escapedType || TAG_OPTIONS['phone-type'][0].value,
carrier: utils.text.escapeHTML(currentTel.carrier || '', true) || '',
i: tel
};
var template = utils.templates.render(phonesTemplate, telField);
// Add event listeners to the phone template components
var sendSmsButton = template.querySelector('#send-sms-button-' + tel);
sendSmsButton.dataset['tel'] = telField.value;
sendSmsButton.addEventListener('click', onSendSmsClicked);
var callOrPickButton = template.querySelector('#call-or-pick-' + tel);
callOrPickButton.dataset['tel'] = telField.value;
callOrPickButton.addEventListener('click', onCallOrPickClicked);
listContainer.appendChild(template);
}
};
var onSendSmsClicked = function onSendSmsClicked(evt) {
var tel = evt.target.dataset['tel'];
Contacts.sendSms(tel);
};
var onCallOrPickClicked = function onCallOrPickClicked(evt) {
var tel = evt.target.dataset['tel'];
Contacts.callOrPick(tel);
}
var renderEmails = function cd_renderEmails(contact) {
if (!contact.email) {
return;
}
var emailLength = Contacts.getLength(contact.email);
for (var email = 0; email < emailLength; email++) {
var currentEmail = contact.email[email];
var escapedType = utils.text.escapeHTML(currentEmail['type'], true);
var emailField = {
value: utils.text.escapeHTML(currentEmail['value'], true) || '',
type: escapedType || TAG_OPTIONS['email-type'][0].value,
i: email
};
var template = utils.templates.render(emailsTemplate, emailField);
// Add event listeners to the phone template components
var emailButton = template.querySelector('#email-or-pick-' + email);
emailButton.dataset['email'] = emailField.value;
emailButton.addEventListener('click', onEmailOrPickClick);
listContainer.appendChild(template);
}
};
var onEmailOrPickClick = function onEmailOrPickClick(evt) {
evt.preventDefault();
var email = evt.target.dataset['email'];
Contacts.sendEmailOrPick(email);
return false;
}
var renderAddresses = function cd_renderAddresses(contact) {
if (!contact.adr) {
return;
}
for (var i = 0; i < contact.adr.length; i++) {
var currentAddress = contact.adr[i];
// Sanity check
if (Contacts.isEmpty(currentAddress, ['streetAddress', 'postalCode',
'locality', 'countryName'])) {
continue;
}
var address = currentAddress['streetAddress'] || '';
var escapedStreet = utils.text.escapeHTML(address, true);
var locality = currentAddress['locality'];
var escapedLocality = utils.text.escapeHTML(locality, true);
var escapedType = utils.text.escapeHTML(currentAddress['type'], true);
var country = currentAddress['countryName'] || '';
var escapedCountry = utils.text.escapeHTML(country, true);
var postalCode = currentAddress['postalCode'] || '';
var escapedPostalCode = utils.text.escapeHTML(postalCode, true);
var addressField = {
streetAddress: escapedStreet,
postalCode: escapedPostalCode,
locality: escapedLocality || '',
countryName: escapedCountry,
type: escapedType || TAG_OPTIONS['address-type'][0].value,
i: i
};
var template = utils.templates.render(addressesTemplate, addressField);
listContainer.appendChild(template);
}
};
var renderNotes = function cd_rederNotes(contact) {
if (!contact.note || contact.note.length === 0) {
return;
}
var container = document.createElement('li');
var title = document.createElement('h2');
title.textContent = _('comments');
container.appendChild(title);
for (var i = 0; i < contact.note.length; i++) {
var currentNote = contact.note[i];
var noteField = {
note: utils.text.escapeHTML(currentNote, true) || '',
i: i
};
var template = utils.templates.render(notesTemplate, noteField);
container.appendChild(template);
listContainer.appendChild(container);
}
};
var renderPhoto = function cd_renderPhoto(contact) {
contactDetails.classList.remove('up');
if (contact.photo && contact.photo.length > 0) {
contactDetails.classList.add('up');
var clientHeight = contactDetails.clientHeight - (initMargin * 10);
if (detailsInner.offsetHeight < clientHeight) {
cover.style.overflow = 'hidden';
} else {
cover.style.overflow = 'auto';
}
Contacts.updatePhoto(contact.photo[0], cover);
} else {
cover.style.backgroundImage = '';
cover.style.overflow = 'auto';
contactDetails.style.transform = '';
contactDetails.classList.add('no-photo');
}
};
var reMark = function(field, value) {
var selector = '[data-' + field + '="' + value + '"]';
var elements = listContainer.querySelectorAll(selector);
for (var i = 0; i < elements.length; i++) {
elements[i].classList.add('remark');
}
}
return {
'init': init,
'setContact': setContact,
'toggleFavorite': toggleFavorite,
'render': render,
'onLineChanged': checkOnline,
'reMark': reMark
};
})();