mobile/android/modules/Prompt.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial