From d53b9140d0768522eeb6a024a17fc41dc73b4966 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 16 Jul 2010 17:45:14 +0200 Subject: Create a special library prompts, add prompts.prompt, and switch bzpage and logger to use it. --- lib/prompts.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 lib/prompts.js (limited to 'lib/prompts.js') diff --git a/lib/prompts.js b/lib/prompts.js new file mode 100644 index 0000000..ac21c49 --- /dev/null +++ b/lib/prompts.js @@ -0,0 +1,82 @@ +/*global exports: false, require: false, console: false */ +/*jslint onevar: false */ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php +"use strict"; +// ============================================================== +var {Cc,Ci} = require("chrome"); +// just for JSLINT var Cc, Ci = {}; +var promptTitle = "Bugzilla Triage Script"; + +/** + * general prompts for a string method + * + * @return String with the password + */ +exports.prompt = function prompt(prompt, defaultValue) { + var stringValue = { + value: defaultValue ? defaultValue : "" + }; + + var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"] + .getService(Ci.nsIPromptService); + var result = prompts.prompt(null, promptTitle, prompt, + stringValue, null, {}); + if (result) { + return stringValue.value; + } else { + return undefined; + } +}; + +/** + * returns password with a special password + * + * @return String with the password + */ +exports.promptPassword = function promptPassword(prompt) { + if (!prompt) { // either undefined or null + prompt = "Enter password:"; + } + var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"] + .getService(Ci.nsIPromptService); + var password = { + value : "" + }; // default the password to pass + var check = { + value : true + }; // default the checkbox to true + var result = prompts.promptPassword(null, "Bugzilla Triage Script", prompt, + password, null, check); + // result is true if OK was pressed, false if cancel was pressed. + // password.value is set if OK was pressed. + // The checkbox is not displayed. + if (result) { + return password.value ? password.value : null; + } else { + return undefined; + } +}; + +/** + * YES/NO prompt; returns boolean or null (for Cancel) + * https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIPromptService + */ +exports.promptYesNoCancel = function promptOKNoCancel(prompt) { + if (!prompt) { // either undefined or null + console.error("Prompt is required!"); + return undefined; + } + var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"] + .getService(Ci.nsIPromptService); + + var result = prompts.confirmEx(null, "Bugzilla Triage Script", prompt, + prompts.STD_YES_NO_BUTTONS, null, null, null, null, {}); + if (result === 0) { + return true; + } else if (result === 1) { + return false; + } else { + return null; + } +}; -- cgit