toolkit/components/prompts/src/SharedPromptUtils.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/prompts/src/SharedPromptUtils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,42 @@
     1.4 +this.EXPORTED_SYMBOLS = [ "PromptUtils" ];
     1.5 +
     1.6 +const Cc = Components.classes;
     1.7 +const Ci = Components.interfaces;
     1.8 +const Cr = Components.results;
     1.9 +const Cu = Components.utils;
    1.10 +
    1.11 +this.PromptUtils = {
    1.12 +    // Fire a dialog open/close event. Used by tabbrowser to focus the
    1.13 +    // tab which is triggering a prompt.
    1.14 +    // For remote dialogs, we pass in a different DOM window and a separate
    1.15 +    // target. If the caller doesn't pass in the target, then we'll simply use
    1.16 +    // the passed-in DOM window.
    1.17 +    fireDialogEvent : function (domWin, eventName, maybeTarget) {
    1.18 +        let target = maybeTarget || domWin;
    1.19 +        let event = domWin.document.createEvent("Events");
    1.20 +        event.initEvent(eventName, true, true);
    1.21 +        let winUtils = domWin.QueryInterface(Ci.nsIInterfaceRequestor)
    1.22 +                             .getInterface(Ci.nsIDOMWindowUtils);
    1.23 +        winUtils.dispatchEventToChromeOnly(target, event);
    1.24 +    },
    1.25 +
    1.26 +    objectToPropBag : function (obj) {
    1.27 +        let bag = Cc["@mozilla.org/hash-property-bag;1"].
    1.28 +                  createInstance(Ci.nsIWritablePropertyBag2);
    1.29 +        bag.QueryInterface(Ci.nsIWritablePropertyBag);
    1.30 +
    1.31 +        for (let propName in obj)
    1.32 +            bag.setProperty(propName, obj[propName]);
    1.33 +
    1.34 +        return bag;
    1.35 +    },
    1.36 +
    1.37 +    propBagToObject : function (propBag, obj) {
    1.38 +        // Here we iterate over the object's original properties, not the bag
    1.39 +        // (ie, the prompt can't return more/different properties than were
    1.40 +        // passed in). This just helps ensure that the caller provides default
    1.41 +        // values, lest the prompt forget to set them.
    1.42 +        for (let propName in obj)
    1.43 +            obj[propName] = propBag.getProperty(propName);
    1.44 +    },
    1.45 +};

mercurial