root/trunk/modules/prompts.jsm
| Revision 992, 4.6 kB (checked in by MaierMan, 6 months ago) |
|---|
| Line | |
|---|---|
| 1 | /* ***** BEGIN LICENSE BLOCK ***** |
| 2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 | * |
| 4 | * The contents of this file are subject to the Mozilla Public License Version |
| 5 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 6 | * the License. You may obtain a copy of the License at |
| 7 | * http://www.mozilla.org/MPL/ |
| 8 | * |
| 9 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 | * for the specific language governing rights and limitations under the |
| 12 | * License. |
| 13 | * |
| 14 | * The Original Code is DownThemAll Confirm wrappers module. |
| 15 | * |
| 16 | * The Initial Developer of the Original Code is Nils Maier |
| 17 | * Portions created by the Initial Developer are Copyright (C) 2007;2008 |
| 18 | * the Initial Developer. All Rights Reserved. |
| 19 | * |
| 20 | * Contributor(s): |
| 21 | * Nils Maier <MaierMan@web.de> |
| 22 | * |
| 23 | * Alternatively, the contents of this file may be used under the terms of |
| 24 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 25 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 26 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 27 | * of those above. If you wish to allow use of your version of this file only |
| 28 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 29 | * use your version of this file under the terms of the MPL, indicate your |
| 30 | * decision by deleting the provisions above and replace them with the notice |
| 31 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 32 | * the provisions above, a recipient may use your version of this file under |
| 33 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 34 | * |
| 35 | * ***** END LICENSE BLOCK ***** */ |
| 36 | |
| 37 | var EXPORTED_SYMBOLS = ['confirm', 'confirmOC', 'confirmYN', 'alert']; |
| 38 | |
| 39 | const Cc = Components.classes; |
| 40 | const Ci = Components.interfaces; |
| 41 | const Cr = Components.results; |
| 42 | |
| 43 | // unpack the default button types |
| 44 | for (let x in Components.interfaces.nsIPromptService) { |
| 45 | let r = new String(x).match(/BUTTON_TITLE_(\w+)$/); |
| 46 | if (r) { |
| 47 | this[r[1]] = Components.interfaces.nsIPromptService[x]; |
| 48 | EXPORTED_SYMBOLS.push(r[1]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * wrapper around confirmEx |
| 54 | * @param title. Dialog title |
| 55 | * @param text. Dialog text |
| 56 | * @param button0. Either null (omit), one of CANCEL/NO/... or a string |
| 57 | * @param button1. s.a. |
| 58 | * @param button2. s.a. |
| 59 | * @param default. Index of the Default button |
| 60 | * @param check. either null, a boolean, or string specifying the prefs id. |
| 61 | * @param checkText. The text for the checkbox |
| 62 | * @return Either the button# or {button: #, checked: bool} if check was a boolean |
| 63 | * @author Nils |
| 64 | */ |
| 65 | function confirm(aWindow, aTitle, aText, aButton0, aButton1, aButton2, aDefault, aCheck, aCheckText) { |
| 66 | let prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"] |
| 67 | .getService(Ci.nsIPromptService); |
| 68 | |
| 69 | // Set up the flags/buttons |
| 70 | let flags = 0; |
| 71 | [aButton0, aButton1, aButton2].forEach( |
| 72 | function(button, idx) { |
| 73 | if (typeof button == 'number') { |
| 74 | flags += prompts['BUTTON_POS_' + idx] * button; |
| 75 | button = null; |
| 76 | } |
| 77 | else if (typeof button == 'string' || button instanceof String) { |
| 78 | flags |= prompts['BUTTON_POS_' + idx] * prompts.BUTTON_TITLE_IS_STRING; |
| 79 | } |
| 80 | else { |
| 81 | button = 0; |
| 82 | } |
| 83 | }, |
| 84 | this |
| 85 | ); |
| 86 | if (aDefault == 1) { |
| 87 | flags += prompts.BUTTON_POS_1_DEFAULT; |
| 88 | } |
| 89 | else if (aDefault == 2) { |
| 90 | flags += prompts.BUTTON_POS_2_DEFAULT; |
| 91 | } |
| 92 | |
| 93 | // Checkmark requested? |
| 94 | let rv = null; |
| 95 | let check = {}; |
| 96 | if (aCheckText) { |
| 97 | if (typeof(aCheck) == 'boolean') { |
| 98 | rv = {}; |
| 99 | check.value = aCheck; |
| 100 | } |
| 101 | else if (typeof(aCheck) == 'string' || aCheck instanceof String) { |
| 102 | check.value = undefined; |
| 103 | try { |
| 104 | check.value = Cc['@mozilla.org/preferences-service;1'] |
| 105 | .getService(Ci.nsIPrefBranch) |
| 106 | .getBoolPref(aCheck); |
| 107 | } |
| 108 | catch (ex) { |
| 109 | // no-op |
| 110 | } |
| 111 | if (check.value == undefined) { |
| 112 | check.value = false; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | let cr = prompts.confirmEx( |
| 118 | aWindow, |
| 119 | aTitle, |
| 120 | aText, |
| 121 | flags, |
| 122 | aButton0, |
| 123 | aButton1, |
| 124 | aButton2, |
| 125 | aCheckText, |
| 126 | check |
| 127 | ); |
| 128 | |
| 129 | // We've got a checkmark request |
| 130 | if (rv) { |
| 131 | rv.checked = check.value; |
| 132 | rv.button = cr; |
| 133 | return rv; |
| 134 | } |
| 135 | |
| 136 | // Just return as usual |
| 137 | return cr; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Shortcut for OK/Cancel Confirmation dialogs |
| 142 | * @author Nils |
| 143 | */ |
| 144 | function confirmOC(aWindow, aTitle, aText) { |
| 145 | return confirm(aWindow, aTitle, aText, OK, CANCEL); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Shortcut for Yes/No Confirmation dialogs |
| 150 | * @author Nils |
| 151 | */ |
| 152 | function confirmYN(aWindow, aTitle, aText) { |
| 153 | return confirm(aWindow, aTitle, aText, YES, NO); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * wrapper around alert |
| 158 | * @author Nils |
| 159 | */ |
| 160 | function alert(aWindow, aTitle, aText) { |
| 161 | Cc["@mozilla.org/embedcomp/prompt-service;1"] |
| 162 | .getService(Ci.nsIPromptService) |
| 163 | .alert(aWindow, aTitle, aText); |
| 164 | } |
Note: See TracBrowser for help on using the browser.
