Last active
December 17, 2015 09:39
-
-
Save reuben/5589312 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dictionary ContactFieldInitializer { | |
| sequence<DOMString>? type; | |
| DOMString? value; | |
| }; | |
| [Constructor(sequence<DOMString> type, DOMString value, boolean pref), | |
| JSImplementation="@mozilla.org/contactField;1"] | |
| interface ContactField { | |
| attribute any type; | |
| attribute DOMString? value; | |
| object toJSON(); | |
| }; | |
| dictionary ContactProperties { | |
| sequence<ContactFieldInitializer>? email; | |
| }; | |
| [Constructor(optional ContactProperties properties), | |
| JSImplementation="@mozilla.org/contact;1"] | |
| interface mozContact { | |
| attribute any email; | |
| object toJSON(); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Contact() { } | |
| Contact.prototype = { | |
| init: function(aWindow) { | |
| this._window = aWindow; | |
| }, | |
| __init: function(aProp) { | |
| if (aProp.email) { | |
| this.email = []; | |
| for (let email of aProp.email) { | |
| let obj = new this._window.ContactField(email.type, email.value, email.pref); | |
| this.email.push(obj); | |
| } | |
| } else { | |
| this.email = null; | |
| } | |
| }, | |
| toJSON: function() { | |
| return { | |
| email: this.email | |
| }; | |
| }, | |
| classID: Components.ID("{72a5ee28-81d8-4af8-90b3-ae935396cc66}"), | |
| contractID: "@mozilla.org/contact;1", | |
| classDescription: "Contact", | |
| QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, Ci.nsISupports]) | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function ContactManager() { } | |
| ContactManager.prototype = { | |
| _convertContact: function(aContact) { | |
| return new this._window.mozContact(aContact, {id: aContact.id, | |
| published: aContact.published, | |
| updated: aContact.updated}); | |
| }, | |
| _convertContacts: function(aContacts) { | |
| let contacts = []; | |
| for (let i in aContacts) { | |
| contacts.push(this._convertContact(aContacts[i])); | |
| } | |
| return contacts; | |
| }, | |
| receiveMessage: function(aMessage) { | |
| let msg = aMessage.json; | |
| switch (aMessage.name) { | |
| case "Contacts:Find:Return:OK": | |
| let req = this.getRequest(msg.requestID); | |
| if (req) { | |
| let result = this._convertContacts(msg.contacts); | |
| Services.DOMRequest.fireSuccess(req.request, result); | |
| } | |
| break; | |
| } | |
| this.removeRequest(msg.requestID); | |
| }, | |
| save: function(aContact) { | |
| let reason; | |
| if (aContact.id === "undefined") { | |
| aContact.setMetadata(this._getRandomId().replace(/[{}-]/g, "")); | |
| reason = "create"; | |
| } else { | |
| reason = "update"; | |
| } | |
| let request = this.createRequest(); | |
| let options = { contact: aContact, reason: reason }; | |
| let allowCallback = function() { | |
| cpmm.sendAsyncMessage("Contact:Save", {requestID: this.getRequestId({request: request, reason: reason}), options: options}); | |
| }.bind(this); | |
| this.askPermission(reason, request, allowCallback); | |
| return request; | |
| }, | |
| classID: Components.ID("{1d70322b-f11b-4f19-9586-7bf291f212aa}"), | |
| contractID: "@mozilla.org/contactManager;1", | |
| classDescription: "ContactManager", | |
| QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, Ci.nsISupports]) | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var c = new mozContact({email: {type: ["work"], value: "foo@bar"}}); | |
| navigator.mozContacts.save(c).onsuccess = function() { | |
| c.email = [{value: "foo"}]; | |
| navigator.mozContacts.save(c); // boom! email is not a ContactField | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment