From 06f4854c6b527da1f5d07b49e3eb5b1d2d4f2f49 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sat, 18 May 2013 23:15:00 +0200 Subject: Making it work. * it is mozContact(), not Contact() * added * fix manifest.webapp * completely misunderstood API ... subobjects are not created directly, but in a plain Object, which is then pushed to the mozContact via .init() method. --- importAddrBook.js | 96 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 41 deletions(-) (limited to 'importAddrBook.js') diff --git a/importAddrBook.js b/importAddrBook.js index 395e900..9a0e6f4 100644 --- a/importAddrBook.js +++ b/importAddrBook.js @@ -2,7 +2,7 @@ bitwise:true, strict:true, undef:true, curly:true, browser:true, devel:true, indent:2, maxerr:50, moz:true, newcap:false, moz:true */ -/* global Contact: false, ContactAddress: false, ContactField: false, +/* global mozContact: false, ContactTelField: false, parseLDIF: false */ (function () { @@ -47,7 +47,8 @@ * @see https://tools.ietf.org/html/rfc4519 */ function translateObjectToContact(inRec) { - var contact = new Contact(), pomStr = "", curRec = null; + var contact = {}, + pomStr = "", curRec = null; /** * Find the proper record (or create new one) in the multi-value @@ -61,21 +62,24 @@ * * Uses global variable contact. */ - function findSubElement(idx, subClass, subType) { + function findSubElement(idx, subType) { var cAddr = null; + console.log("idx = " + idx + ", subType = " + subType); // No contact.adr at all if (!contact.hasOwnProperty(idx)) { - cAddr = new subClass(); - cAddr.type = [subType]; + cAddr = { + 'type': [subType] + }; contact[idx] = cAddr; } // Single-element property else if (!Array.isArray(contact[idx])) { if (contact[idx].type.indexOf(subType) === -1) { contact[idx] = [contact[idx]]; - cAddr = new subClass(); - cAddr.type = [subType]; + cAddr = { + 'type': [subType] + }; contact[idx].push(cAddr); } else { @@ -88,8 +92,9 @@ return addr.type.indexOf(subType) !== -1; }); if (cAddr.length === 0) { - cAddr = new subClass(); - cAddr.type = [subType]; + cAddr = { + 'type': [subType] + }; contact[idx].push(cAddr); } else { @@ -97,6 +102,8 @@ } } + console.log("contact:\n" + contact.toSource()); + console.log("cAddr:\n" + cAddr.toSource()); return cAddr; } @@ -107,8 +114,8 @@ pomStr.slice(6,8)); } else if (key === "c") { - curRec = findSubElement("adr", ContactAddress, "work"); - curRec.adr.countryName = inRec[key]; + curRec = findSubElement("adr", "work"); + curRec.countryName = inRec[key]; } else if (key === "cn") { contact.name = [inRec[key]]; @@ -117,51 +124,51 @@ contact.note = [inRec[key]]; } else if (key === "facsimiletelephonenumber") { - curRec = findSubElement("tel", ContactTelField, "fax"); - curRec.tel.value = inRec[key]; + curRec = findSubElement("tel", "fax"); + curRec.value = inRec[key]; } else if (key === "givenName") { contact.givenName = [inRec[key]]; } else if (key === "homePhone") { - curRec = findSubElement("tel", ContactTelField, "home"); - curRec.tel.value = inRec[key]; + curRec = findSubElement("tel", "home"); + curRec.value = inRec[key]; } else if (key === "l") { - curRec = findSubElement("adr", ContactAddress, "work"); - curRec.adr.locality = inRec[key]; + curRec = findSubElement("adr", "work"); + curRec.locality = inRec[key]; } else if (key === "mail") { - curRec = findSubElement("email", ContactField, "work"); - curRec.email.value = inRec[key]; + curRec = findSubElement("email", "work"); + curRec.value = inRec[key]; } else if (key === "mobile") { - curRec = findSubElement("tel", ContactTelField, "mobile"); - curRec.tel.value = inRec[key]; + curRec = findSubElement("tel", "mobile"); + curRec.value = inRec[key]; } else if (key === "mozillaHomeCountryName") { - curRec = findSubElement("adr", ContactAddress, "home"); - curRec.adr.countryName = inRec[key]; + curRec = findSubElement("adr", "home"); + curRec.countryName = inRec[key]; } else if (key === "mozillaHomeLocalityName") { - curRec = findSubElement("adr", ContactAddress, "home"); - curRec.adr.locality = inRec[key]; + curRec = findSubElement("adr", "home"); + curRec.locality = inRec[key]; } else if (key === "mozillaHomePostalCode") { - curRec = findSubElement("adr", ContactAddress, "home"); - curRec.adr.postalCode = inRec[key]; + curRec = findSubElement("adr", "home"); + curRec.postalCode = inRec[key]; } else if (key === "mozillaHomeState") { - curRec = findSubElement("adr", ContactAddress, "home"); - curRec.adr.region = inRec[key]; + curRec = findSubElement("adr", "home"); + curRec.region = inRec[key]; } else if (key === "mozillaHomeStreet") { - curRec = findSubElement("adr", ContactAddress, "home"); - curRec.adr.streetAddress = inRec[key]; + curRec = findSubElement("adr", "home"); + curRec.streetAddress = inRec[key]; } else if (key === "mozillaHomeUrl") { - curRec = findSubElement("url", ContactField, "home"); - curRec.email.value = inRec[key]; + curRec = findSubElement("url", "home"); + curRec.value = inRec[key]; } else if (key === "mozillaNickname") { contact.nickname = [inRec[key]]; @@ -173,16 +180,16 @@ contact.familyName = [inRec[key]]; } else if (key === "st") { - curRec = findSubElement("adr", ContactAddress, "work"); - curRec.adr.region = inRec[key]; + curRec = findSubElement("adr", "work"); + curRec.region = inRec[key]; } else if (key === "street") { - curRec = findSubElement("adr", ContactAddress, "work"); - curRec.adr.streetAddress = inRec[key]; + curRec = findSubElement("adr", "work"); + curRec.streetAddress = inRec[key]; } else if (key === "telephoneNumber") { - curRec = findSubElement("tel", ContactTelField, "work"); - curRec.tel.value = inRec[key]; + curRec = findSubElement("tel", "work"); + curRec.value = inRec[key]; } else if (key === "title") { contact.jobTitle = [inRec[key]]; @@ -194,7 +201,13 @@ } } - return contact; + console.log("contact:\n" + contact.toSource()); + if (Object.keys(contact).length > 0) { + var outObj = new mozContact(); + outObj.init(contact); + return contact; + } + return null; } /** @@ -244,6 +257,7 @@ var records = parseLDIF(ldifText.split("\n")); + console.log("records.length = " + records.length); if (records.length > 0) { if (!window.confirm("THIS WILL ERASE ALL CONTACTS ON YOUR PHONE!\n" + "Are you cool with that?")) { @@ -262,6 +276,7 @@ progressEl.value = 0; records.forEach(function (rec) { + console.log("processed contact:\n" + rec.toSource()); var add_contact = translateObjectToContact(rec); var sav_req = navigator.mozContacts.save(add_contact); @@ -286,7 +301,6 @@ var URL = document.getElementsByName("URL")[0].value; var progressForm = document.getElementById("progress-div"); - console.log("URL = " + URL); document.getElementById("URL-form").style.display = "none"; progressForm.style.display = "block"; -- cgit