/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * Portions created by the Initial Developer are Copyright (C) 2009 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Fabrice Desré , Original author * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ const Ci = Components.interfaces; const Cc = Components.classes; const Cr = Components.results; const Cu = Components.utils; Cu.import("resource://gre/modules/XPCOMUtils.jsm"); function promptService() { let bundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService); this._bundle = bundleService.createBundle("chrome://global/locale/commonDialogs.properties"); } promptService.prototype = { classDescription: "Mobile Prompt Service", contractID: "@mozilla.org/embedcomp/prompt-service;1", classID: Components.ID("{9a61149b-2276-4a0a-b79c-be994ad106cf}"), QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptService, Ci.nsIPromptService2]), // helper function do get the current document getDocument: function() { let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); return wm.getMostRecentWindow("navigator:browser").document; }, // add a width style to prevent a element to grow larger // than the screen width sizeElement: function(id, percent) { let elem = this.getDocument().getElementById(id); let screenW = this.getDocument().getElementById("main-window").getBoundingClientRect().width; elem.style.width = screenW * percent / 100 + "px" }, // size the height of the scrollable message. this assumes the given element // is a child of a scrollbox sizeScrollableMsg: function(id, percent) { let screenH = this.getDocument().getElementById("main-window").getBoundingClientRect().height; let maxHeight = screenH * percent / 100; let elem = this.getDocument().getElementById(id); let height = elem.getBoundingClientRect().height; if (height > maxHeight) height = maxHeight; elem.parentNode.style.height = height + "px"; }, openDialog: function(src, params) { let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); let browser = wm.getMostRecentWindow("navigator:browser"); return browser.importDialog(src, params); }, alert: function(aParent, aTitle, aText) { let dialog = this.openDialog("chrome://browser/content/prompt/alert.xul", null); let doc = this.getDocument(); doc.getElementById("prompt-alert-title").value = aTitle; doc.getElementById("prompt-alert-message").appendChild(doc.createTextNode(aText)); this.sizeElement("prompt-alert-message", 80); this.sizeScrollableMsg("prompt-alert-message", 25); dialog.waitForClose(); }, alertCheck: function(aParent, aTitle, aText, aCheckMsg, aCheckState) { let dialog = this.openDialog("chrome://browser/content/prompt/alert.xul", aCheckState); let doc = this.getDocument(); doc.getElementById("prompt-alert-title").value = aTitle; doc.getElementById("prompt-alert-message").appendChild(doc.createTextNode(aText)); this.sizeElement("prompt-alert-message", 80); this.sizeScrollableMsg("prompt-alert-message", 25); doc.getElementById("prompt-alert-checkbox").checked = aCheckState.value; doc.getElementById("prompt-alert-checkbox-msg").value = aCheckMsg; this.sizeElement("prompt-alert-checkbox-msg", 50); doc.getElementById("prompt-alert-checkbox-box").removeAttribute("collapsed"); dialog.waitForClose(); }, confirm: function(aParent, aTitle, aText) { var params = new Object(); params.result = false; let doc = this.getDocument(); let dialog = this.openDialog("chrome://browser/content/prompt/confirm.xul", params); doc.getElementById("prompt-confirm-title").value = aTitle; doc.getElementById("prompt-confirm-message").appendChild(doc.createTextNode(aText)); this.sizeElement("prompt-confirm-message", 80); this.sizeScrollableMsg("prompt-confirm-message", 25); dialog.waitForClose(); return params.result; }, confirmCheck: function(aParent, aTitle, aText, aCheckMsg, aCheckState) { var params = new Object(); params.result = false; params.checkbox = aCheckState; let doc = this.getDocument(); let dialog = this.openDialog("chrome://browser/content/prompt/confirm.xul", params); doc.getElementById("prompt-confirm-title").value = aTitle; doc.getElementById("prompt-confirm-message").appendChild(doc.createTextNode(aText)); this.sizeElement("prompt-confirm-message", 80); this.sizeScrollableMsg("prompt-confirm-message", 25); doc.getElementById("prompt-confirm-checkbox").checked = aCheckState.value; doc.getElementById("prompt-confirm-checkbox-msg").value = aCheckMsg; this.sizeElement("prompt-confirm-checkbox-msg", 50); doc.getElementById("prompt-confirm-checkbox-box").removeAttribute("collapsed"); dialog.waitForClose(); return params.result; }, getLocaleString: function(key) { return this._bundle.GetStringFromName(key); }, // // Copied from chrome://global/content/commonDialog.js // setLabelForNode: function(aNode, aLabel, aIsLabelFlag) { // This is for labels which may contain embedded access keys. // If we end in (&X) where X represents the access key, optionally preceded // by spaces and/or followed by the ':' character, store the access key and // remove the access key placeholder + leading spaces from the label. // Otherwise a character preceded by one but not two &s is the access key. // Store it and remove the &. // Note that if you change the following code, see the comment of // nsTextBoxFrame::UpdateAccessTitle. var accessKey = null; if (/ *\(\&([^&])\)(:)?$/.test(aLabel)) { aLabel = RegExp.leftContext + RegExp.$2; accessKey = RegExp.$1; } else if (/^(.*[^&])?\&(([^&]).*$)/.test(aLabel)) { aLabel = RegExp.$1 + RegExp.$2; accessKey = RegExp.$3; } // && is the magic sequence to embed an & in your label. aLabel = aLabel.replace(/\&\&/g, "&"); if (aIsLabelFlag) { // Set text for