Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | "use strict" |
michael@0 | 6 | |
michael@0 | 7 | let Cc = Components.classes; |
michael@0 | 8 | let Ci = Components.interfaces; |
michael@0 | 9 | |
michael@0 | 10 | Components.utils.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | Components.utils.import("resource://gre/modules/Messaging.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | this.EXPORTED_SYMBOLS = ["Prompt"]; |
michael@0 | 14 | |
michael@0 | 15 | function log(msg) { |
michael@0 | 16 | Services.console.logStringMessage(msg); |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | function Prompt(aOptions) { |
michael@0 | 20 | this.window = "window" in aOptions ? aOptions.window : null; |
michael@0 | 21 | this.msg = { async: true }; |
michael@0 | 22 | |
michael@0 | 23 | if (aOptions.priority === 1) |
michael@0 | 24 | this.msg.type = "Prompt:ShowTop" |
michael@0 | 25 | else |
michael@0 | 26 | this.msg.type = "Prompt:Show" |
michael@0 | 27 | |
michael@0 | 28 | if ("title" in aOptions && aOptions.title != null) |
michael@0 | 29 | this.msg.title = aOptions.title; |
michael@0 | 30 | |
michael@0 | 31 | if ("message" in aOptions && aOptions.message != null) |
michael@0 | 32 | this.msg.text = aOptions.message; |
michael@0 | 33 | |
michael@0 | 34 | if ("buttons" in aOptions && aOptions.buttons != null) |
michael@0 | 35 | this.msg.buttons = aOptions.buttons; |
michael@0 | 36 | |
michael@0 | 37 | if ("hint" in aOptions && aOptions.hint != null) |
michael@0 | 38 | this.msg.hint = aOptions.hint; |
michael@0 | 39 | |
michael@0 | 40 | let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | Prompt.prototype = { |
michael@0 | 44 | setHint: function(aHint) { |
michael@0 | 45 | if (!aHint) |
michael@0 | 46 | delete this.msg.hint; |
michael@0 | 47 | else |
michael@0 | 48 | this.msg.hint = aHint; |
michael@0 | 49 | return this; |
michael@0 | 50 | }, |
michael@0 | 51 | |
michael@0 | 52 | addButton: function(aOptions) { |
michael@0 | 53 | if (!this.msg.buttons) |
michael@0 | 54 | this.msg.buttons = []; |
michael@0 | 55 | this.msg.buttons.push(aOptions.label); |
michael@0 | 56 | return this; |
michael@0 | 57 | }, |
michael@0 | 58 | |
michael@0 | 59 | _addInput: function(aOptions) { |
michael@0 | 60 | let obj = aOptions; |
michael@0 | 61 | if (this[aOptions.type + "_count"] === undefined) |
michael@0 | 62 | this[aOptions.type + "_count"] = 0; |
michael@0 | 63 | |
michael@0 | 64 | obj.id = aOptions.id || (aOptions.type + this[aOptions.type + "_count"]); |
michael@0 | 65 | this[aOptions.type + "_count"]++; |
michael@0 | 66 | |
michael@0 | 67 | if (!this.msg.inputs) |
michael@0 | 68 | this.msg.inputs = []; |
michael@0 | 69 | this.msg.inputs.push(obj); |
michael@0 | 70 | return this; |
michael@0 | 71 | }, |
michael@0 | 72 | |
michael@0 | 73 | addCheckbox: function(aOptions) { |
michael@0 | 74 | return this._addInput({ |
michael@0 | 75 | type: "checkbox", |
michael@0 | 76 | label: aOptions.label, |
michael@0 | 77 | checked: aOptions.checked, |
michael@0 | 78 | id: aOptions.id |
michael@0 | 79 | }); |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | addTextbox: function(aOptions) { |
michael@0 | 83 | return this._addInput({ |
michael@0 | 84 | type: "textbox", |
michael@0 | 85 | value: aOptions.value, |
michael@0 | 86 | hint: aOptions.hint, |
michael@0 | 87 | autofocus: aOptions.autofocus, |
michael@0 | 88 | id: aOptions.id |
michael@0 | 89 | }); |
michael@0 | 90 | }, |
michael@0 | 91 | |
michael@0 | 92 | addNumber: function(aOptions) { |
michael@0 | 93 | return this._addInput({ |
michael@0 | 94 | type: "number", |
michael@0 | 95 | value: aOptions.value, |
michael@0 | 96 | hint: aOptions.hint, |
michael@0 | 97 | autofocus: aOptions.autofocus, |
michael@0 | 98 | id: aOptions.id |
michael@0 | 99 | }); |
michael@0 | 100 | }, |
michael@0 | 101 | |
michael@0 | 102 | addPassword: function(aOptions) { |
michael@0 | 103 | return this._addInput({ |
michael@0 | 104 | type: "password", |
michael@0 | 105 | value: aOptions.value, |
michael@0 | 106 | hint: aOptions.hint, |
michael@0 | 107 | autofocus: aOptions.autofocus, |
michael@0 | 108 | id : aOptions.id |
michael@0 | 109 | }); |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | addDatePicker: function(aOptions) { |
michael@0 | 113 | return this._addInput({ |
michael@0 | 114 | type: aOptions.type || "date", |
michael@0 | 115 | value: aOptions.value, |
michael@0 | 116 | id: aOptions.id |
michael@0 | 117 | }); |
michael@0 | 118 | }, |
michael@0 | 119 | |
michael@0 | 120 | addColorPicker: function(aOptions) { |
michael@0 | 121 | return this._addInput({ |
michael@0 | 122 | type: "color", |
michael@0 | 123 | value: aOptions.value, |
michael@0 | 124 | id: aOptions.id |
michael@0 | 125 | }); |
michael@0 | 126 | }, |
michael@0 | 127 | |
michael@0 | 128 | addLabel: function(aOptions) { |
michael@0 | 129 | return this._addInput({ |
michael@0 | 130 | type: "label", |
michael@0 | 131 | label: aOptions.label, |
michael@0 | 132 | id: aOptions.id |
michael@0 | 133 | }); |
michael@0 | 134 | }, |
michael@0 | 135 | |
michael@0 | 136 | addMenulist: function(aOptions) { |
michael@0 | 137 | return this._addInput({ |
michael@0 | 138 | type: "menulist", |
michael@0 | 139 | values: aOptions.values, |
michael@0 | 140 | id: aOptions.id |
michael@0 | 141 | }); |
michael@0 | 142 | }, |
michael@0 | 143 | |
michael@0 | 144 | addIconGrid: function(aOptions) { |
michael@0 | 145 | return this._addInput({ |
michael@0 | 146 | type: "icongrid", |
michael@0 | 147 | items: aOptions.items, |
michael@0 | 148 | id: aOptions.id |
michael@0 | 149 | }); |
michael@0 | 150 | }, |
michael@0 | 151 | |
michael@0 | 152 | addTabs: function(aOptions) { |
michael@0 | 153 | return this._addInput({ |
michael@0 | 154 | type: "tabs", |
michael@0 | 155 | items: aOptions.items, |
michael@0 | 156 | id: aOptions.id |
michael@0 | 157 | }); |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | show: function(callback) { |
michael@0 | 161 | this.callback = callback; |
michael@0 | 162 | log("Sending message"); |
michael@0 | 163 | this._innerShow(); |
michael@0 | 164 | }, |
michael@0 | 165 | |
michael@0 | 166 | _innerShow: function() { |
michael@0 | 167 | sendMessageToJava(this.msg, (data) => { |
michael@0 | 168 | if (this.callback) |
michael@0 | 169 | this.callback(data); |
michael@0 | 170 | }); |
michael@0 | 171 | }, |
michael@0 | 172 | |
michael@0 | 173 | _setListItems: function(aItems) { |
michael@0 | 174 | let hasSelected = false; |
michael@0 | 175 | this.msg.listitems = []; |
michael@0 | 176 | |
michael@0 | 177 | aItems.forEach(function(item) { |
michael@0 | 178 | let obj = { id: item.id }; |
michael@0 | 179 | |
michael@0 | 180 | obj.label = item.label; |
michael@0 | 181 | |
michael@0 | 182 | if (item.icon) |
michael@0 | 183 | obj.icon = item.icon; |
michael@0 | 184 | |
michael@0 | 185 | if (item.disabled) |
michael@0 | 186 | obj.disabled = true; |
michael@0 | 187 | |
michael@0 | 188 | if (item.selected) { |
michael@0 | 189 | if (!this.msg.choiceMode) { |
michael@0 | 190 | this.msg.choiceMode = "single"; |
michael@0 | 191 | } |
michael@0 | 192 | obj.selected = item.selected; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if (item.header) |
michael@0 | 196 | obj.isGroup = true; |
michael@0 | 197 | |
michael@0 | 198 | if (item.menu) |
michael@0 | 199 | obj.isParent = true; |
michael@0 | 200 | |
michael@0 | 201 | if (item.child) |
michael@0 | 202 | obj.inGroup = true; |
michael@0 | 203 | |
michael@0 | 204 | if (item.showAsActions) |
michael@0 | 205 | obj.showAsActions = item.showAsActions; |
michael@0 | 206 | |
michael@0 | 207 | if (item.icon) |
michael@0 | 208 | obj.icon = item.icon; |
michael@0 | 209 | |
michael@0 | 210 | this.msg.listitems.push(obj); |
michael@0 | 211 | |
michael@0 | 212 | }, this); |
michael@0 | 213 | return this; |
michael@0 | 214 | }, |
michael@0 | 215 | |
michael@0 | 216 | setSingleChoiceItems: function(aItems) { |
michael@0 | 217 | return this._setListItems(aItems); |
michael@0 | 218 | }, |
michael@0 | 219 | |
michael@0 | 220 | setMultiChoiceItems: function(aItems) { |
michael@0 | 221 | this.msg.choiceMode = "multiple"; |
michael@0 | 222 | return this._setListItems(aItems); |
michael@0 | 223 | }, |
michael@0 | 224 | |
michael@0 | 225 | } |