toolkit/components/prompts/content/commonDialog.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/prompts/content/commonDialog.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cr = Components.results;
    1.10 +const Cc = Components.classes;
    1.11 +const Cu = Components.utils;
    1.12 +
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/CommonDialog.jsm");
    1.15 +
    1.16 +let propBag, args, Dialog;
    1.17 +
    1.18 +function commonDialogOnLoad() {
    1.19 +    propBag = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2)
    1.20 +                                 .QueryInterface(Ci.nsIWritablePropertyBag);
    1.21 +    // Convert to a JS object
    1.22 +    args = {};
    1.23 +    let propEnum = propBag.enumerator;
    1.24 +    while (propEnum.hasMoreElements()) {
    1.25 +        let prop = propEnum.getNext().QueryInterface(Ci.nsIProperty);
    1.26 +        args[prop.name] = prop.value;
    1.27 +    }
    1.28 +
    1.29 +    let dialog = document.documentElement;
    1.30 +
    1.31 +    let ui = {
    1.32 +        prompt             : window,
    1.33 +        loginContainer     : document.getElementById("loginContainer"),
    1.34 +        loginTextbox       : document.getElementById("loginTextbox"),
    1.35 +        loginLabel         : document.getElementById("loginLabel"),
    1.36 +        password1Container : document.getElementById("password1Container"),
    1.37 +        password1Textbox   : document.getElementById("password1Textbox"),
    1.38 +        password1Label     : document.getElementById("password1Label"),
    1.39 +        infoBody           : document.getElementById("info.body"),
    1.40 +        infoTitle          : document.getElementById("info.title"),
    1.41 +        infoIcon           : document.getElementById("info.icon"),
    1.42 +        checkbox           : document.getElementById("checkbox"),
    1.43 +        checkboxContainer  : document.getElementById("checkboxContainer"),
    1.44 +        button3            : dialog.getButton("extra2"),
    1.45 +        button2            : dialog.getButton("extra1"),
    1.46 +        button1            : dialog.getButton("cancel"),
    1.47 +        button0            : dialog.getButton("accept"),
    1.48 +        focusTarget        : window,
    1.49 +    };
    1.50 +
    1.51 +    // limit the dialog to the screen width
    1.52 +    document.getElementById("filler").maxWidth = screen.availWidth;
    1.53 +    Services.obs.addObserver(softkbObserver, "softkb-change", false);
    1.54 +
    1.55 +    Dialog = new CommonDialog(args, ui);
    1.56 +    Dialog.onLoad(dialog);
    1.57 +    window.getAttention();
    1.58 +}
    1.59 +
    1.60 +function commonDialogOnUnload() {
    1.61 +    Services.obs.removeObserver(softkbObserver, "softkb-change");
    1.62 +    // Convert args back into property bag
    1.63 +    for (let propName in args)
    1.64 +        propBag.setProperty(propName, args[propName]);
    1.65 +}
    1.66 +
    1.67 +function softkbObserver(subject, topic, data) {
    1.68 +    let rect = JSON.parse(data);
    1.69 +    if (rect) {
    1.70 +        let height = rect.bottom - rect.top;
    1.71 +        let width  = rect.right - rect.left;
    1.72 +        let top    = (rect.top + (height - window.innerHeight) / 2);
    1.73 +        let left   = (rect.left + (width - window.innerWidth) / 2);
    1.74 +        window.moveTo(left, top);
    1.75 +    }
    1.76 +}

mercurial