michael@0: /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict" michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/Messaging.jsm"); michael@0: michael@0: this.EXPORTED_SYMBOLS = ["Prompt"]; michael@0: michael@0: function log(msg) { michael@0: Services.console.logStringMessage(msg); michael@0: } michael@0: michael@0: function Prompt(aOptions) { michael@0: this.window = "window" in aOptions ? aOptions.window : null; michael@0: this.msg = { async: true }; michael@0: michael@0: if (aOptions.priority === 1) michael@0: this.msg.type = "Prompt:ShowTop" michael@0: else michael@0: this.msg.type = "Prompt:Show" michael@0: michael@0: if ("title" in aOptions && aOptions.title != null) michael@0: this.msg.title = aOptions.title; michael@0: michael@0: if ("message" in aOptions && aOptions.message != null) michael@0: this.msg.text = aOptions.message; michael@0: michael@0: if ("buttons" in aOptions && aOptions.buttons != null) michael@0: this.msg.buttons = aOptions.buttons; michael@0: michael@0: if ("hint" in aOptions && aOptions.hint != null) michael@0: this.msg.hint = aOptions.hint; michael@0: michael@0: let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator); michael@0: } michael@0: michael@0: Prompt.prototype = { michael@0: setHint: function(aHint) { michael@0: if (!aHint) michael@0: delete this.msg.hint; michael@0: else michael@0: this.msg.hint = aHint; michael@0: return this; michael@0: }, michael@0: michael@0: addButton: function(aOptions) { michael@0: if (!this.msg.buttons) michael@0: this.msg.buttons = []; michael@0: this.msg.buttons.push(aOptions.label); michael@0: return this; michael@0: }, michael@0: michael@0: _addInput: function(aOptions) { michael@0: let obj = aOptions; michael@0: if (this[aOptions.type + "_count"] === undefined) michael@0: this[aOptions.type + "_count"] = 0; michael@0: michael@0: obj.id = aOptions.id || (aOptions.type + this[aOptions.type + "_count"]); michael@0: this[aOptions.type + "_count"]++; michael@0: michael@0: if (!this.msg.inputs) michael@0: this.msg.inputs = []; michael@0: this.msg.inputs.push(obj); michael@0: return this; michael@0: }, michael@0: michael@0: addCheckbox: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "checkbox", michael@0: label: aOptions.label, michael@0: checked: aOptions.checked, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addTextbox: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "textbox", michael@0: value: aOptions.value, michael@0: hint: aOptions.hint, michael@0: autofocus: aOptions.autofocus, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addNumber: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "number", michael@0: value: aOptions.value, michael@0: hint: aOptions.hint, michael@0: autofocus: aOptions.autofocus, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addPassword: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "password", michael@0: value: aOptions.value, michael@0: hint: aOptions.hint, michael@0: autofocus: aOptions.autofocus, michael@0: id : aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addDatePicker: function(aOptions) { michael@0: return this._addInput({ michael@0: type: aOptions.type || "date", michael@0: value: aOptions.value, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addColorPicker: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "color", michael@0: value: aOptions.value, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addLabel: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "label", michael@0: label: aOptions.label, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addMenulist: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "menulist", michael@0: values: aOptions.values, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addIconGrid: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "icongrid", michael@0: items: aOptions.items, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: addTabs: function(aOptions) { michael@0: return this._addInput({ michael@0: type: "tabs", michael@0: items: aOptions.items, michael@0: id: aOptions.id michael@0: }); michael@0: }, michael@0: michael@0: show: function(callback) { michael@0: this.callback = callback; michael@0: log("Sending message"); michael@0: this._innerShow(); michael@0: }, michael@0: michael@0: _innerShow: function() { michael@0: sendMessageToJava(this.msg, (data) => { michael@0: if (this.callback) michael@0: this.callback(data); michael@0: }); michael@0: }, michael@0: michael@0: _setListItems: function(aItems) { michael@0: let hasSelected = false; michael@0: this.msg.listitems = []; michael@0: michael@0: aItems.forEach(function(item) { michael@0: let obj = { id: item.id }; michael@0: michael@0: obj.label = item.label; michael@0: michael@0: if (item.icon) michael@0: obj.icon = item.icon; michael@0: michael@0: if (item.disabled) michael@0: obj.disabled = true; michael@0: michael@0: if (item.selected) { michael@0: if (!this.msg.choiceMode) { michael@0: this.msg.choiceMode = "single"; michael@0: } michael@0: obj.selected = item.selected; michael@0: } michael@0: michael@0: if (item.header) michael@0: obj.isGroup = true; michael@0: michael@0: if (item.menu) michael@0: obj.isParent = true; michael@0: michael@0: if (item.child) michael@0: obj.inGroup = true; michael@0: michael@0: if (item.showAsActions) michael@0: obj.showAsActions = item.showAsActions; michael@0: michael@0: if (item.icon) michael@0: obj.icon = item.icon; michael@0: michael@0: this.msg.listitems.push(obj); michael@0: michael@0: }, this); michael@0: return this; michael@0: }, michael@0: michael@0: setSingleChoiceItems: function(aItems) { michael@0: return this._setListItems(aItems); michael@0: }, michael@0: michael@0: setMultiChoiceItems: function(aItems) { michael@0: this.msg.choiceMode = "multiple"; michael@0: return this._setListItems(aItems); michael@0: }, michael@0: michael@0: }