mobile/android/modules/Prompt.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/modules/Prompt.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,225 @@
     1.4 +/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +"use strict"
     1.9 +
    1.10 +let Cc = Components.classes;
    1.11 +let Ci = Components.interfaces;
    1.12 +
    1.13 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.14 +Components.utils.import("resource://gre/modules/Messaging.jsm");
    1.15 +
    1.16 +this.EXPORTED_SYMBOLS = ["Prompt"];
    1.17 +
    1.18 +function log(msg) {
    1.19 +  Services.console.logStringMessage(msg);
    1.20 +}
    1.21 +
    1.22 +function Prompt(aOptions) {
    1.23 +  this.window = "window" in aOptions ? aOptions.window : null;
    1.24 +  this.msg = { async: true };
    1.25 +
    1.26 +  if (aOptions.priority === 1)
    1.27 +    this.msg.type = "Prompt:ShowTop"
    1.28 +  else
    1.29 +    this.msg.type = "Prompt:Show"
    1.30 +
    1.31 +  if ("title" in aOptions && aOptions.title != null)
    1.32 +    this.msg.title = aOptions.title;
    1.33 +
    1.34 +  if ("message" in aOptions && aOptions.message != null)
    1.35 +    this.msg.text = aOptions.message;
    1.36 +
    1.37 +  if ("buttons" in aOptions && aOptions.buttons != null)
    1.38 +    this.msg.buttons = aOptions.buttons;
    1.39 +
    1.40 +  if ("hint" in aOptions && aOptions.hint != null)
    1.41 +    this.msg.hint = aOptions.hint;
    1.42 +
    1.43 +  let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
    1.44 +}
    1.45 +
    1.46 +Prompt.prototype = {
    1.47 +  setHint: function(aHint) {
    1.48 +    if (!aHint)
    1.49 +      delete this.msg.hint;
    1.50 +    else
    1.51 +      this.msg.hint = aHint;
    1.52 +    return this;
    1.53 +  },
    1.54 +
    1.55 +  addButton: function(aOptions) {
    1.56 +    if (!this.msg.buttons)
    1.57 +      this.msg.buttons = [];
    1.58 +    this.msg.buttons.push(aOptions.label);
    1.59 +    return this;
    1.60 +  },
    1.61 +
    1.62 +  _addInput: function(aOptions) {
    1.63 +    let obj = aOptions;
    1.64 +    if (this[aOptions.type + "_count"] === undefined)
    1.65 +      this[aOptions.type + "_count"] = 0;
    1.66 +
    1.67 +    obj.id = aOptions.id || (aOptions.type + this[aOptions.type + "_count"]);
    1.68 +    this[aOptions.type + "_count"]++;
    1.69 +
    1.70 +    if (!this.msg.inputs)
    1.71 +      this.msg.inputs = [];
    1.72 +    this.msg.inputs.push(obj);
    1.73 +    return this;
    1.74 +  },
    1.75 +
    1.76 +  addCheckbox: function(aOptions) {
    1.77 +    return this._addInput({
    1.78 +      type: "checkbox",
    1.79 +      label: aOptions.label,
    1.80 +      checked: aOptions.checked,
    1.81 +      id: aOptions.id
    1.82 +    });
    1.83 +  },
    1.84 +
    1.85 +  addTextbox: function(aOptions) {
    1.86 +    return this._addInput({
    1.87 +      type: "textbox",
    1.88 +      value: aOptions.value,
    1.89 +      hint: aOptions.hint,
    1.90 +      autofocus: aOptions.autofocus,
    1.91 +      id: aOptions.id
    1.92 +    });
    1.93 +  },
    1.94 +
    1.95 +  addNumber: function(aOptions) {
    1.96 +    return this._addInput({
    1.97 +      type: "number",
    1.98 +      value: aOptions.value,
    1.99 +      hint: aOptions.hint,
   1.100 +      autofocus: aOptions.autofocus,
   1.101 +      id: aOptions.id
   1.102 +    });
   1.103 +  },
   1.104 +
   1.105 +  addPassword: function(aOptions) {
   1.106 +    return this._addInput({
   1.107 +      type: "password",
   1.108 +      value: aOptions.value,
   1.109 +      hint: aOptions.hint,
   1.110 +      autofocus: aOptions.autofocus,
   1.111 +      id : aOptions.id
   1.112 +    });
   1.113 +  },
   1.114 +
   1.115 +  addDatePicker: function(aOptions) {
   1.116 +    return this._addInput({
   1.117 +      type: aOptions.type || "date",
   1.118 +      value: aOptions.value,
   1.119 +      id: aOptions.id
   1.120 +    });
   1.121 +  },
   1.122 +
   1.123 +  addColorPicker: function(aOptions) {
   1.124 +    return this._addInput({
   1.125 +      type: "color",
   1.126 +      value: aOptions.value,
   1.127 +      id: aOptions.id
   1.128 +    });
   1.129 +  },
   1.130 +
   1.131 +  addLabel: function(aOptions) {
   1.132 +    return this._addInput({
   1.133 +      type: "label",
   1.134 +      label: aOptions.label,
   1.135 +      id: aOptions.id
   1.136 +    });
   1.137 +  },
   1.138 +
   1.139 +  addMenulist: function(aOptions) {
   1.140 +    return this._addInput({
   1.141 +      type: "menulist",
   1.142 +      values: aOptions.values,
   1.143 +      id: aOptions.id
   1.144 +    });
   1.145 +  },
   1.146 +
   1.147 +  addIconGrid: function(aOptions) {
   1.148 +    return this._addInput({
   1.149 +      type: "icongrid",
   1.150 +      items: aOptions.items,
   1.151 +      id: aOptions.id
   1.152 +    });
   1.153 +  },
   1.154 +
   1.155 +  addTabs: function(aOptions) {
   1.156 +    return this._addInput({
   1.157 +      type: "tabs",
   1.158 +      items: aOptions.items,
   1.159 +      id: aOptions.id
   1.160 +    });
   1.161 +  },
   1.162 +
   1.163 +  show: function(callback) {
   1.164 +    this.callback = callback;
   1.165 +    log("Sending message");
   1.166 +    this._innerShow();
   1.167 +  },
   1.168 +
   1.169 +  _innerShow: function() {
   1.170 +    sendMessageToJava(this.msg, (data) => {
   1.171 +      if (this.callback)
   1.172 +        this.callback(data);
   1.173 +    });
   1.174 +  },
   1.175 +
   1.176 +  _setListItems: function(aItems) {
   1.177 +    let hasSelected = false;
   1.178 +    this.msg.listitems = [];
   1.179 +
   1.180 +    aItems.forEach(function(item) {
   1.181 +      let obj = { id: item.id };
   1.182 +
   1.183 +      obj.label = item.label;
   1.184 +
   1.185 +      if (item.icon)
   1.186 +        obj.icon = item.icon;
   1.187 +
   1.188 +      if (item.disabled)
   1.189 +        obj.disabled = true;
   1.190 +
   1.191 +      if (item.selected) {
   1.192 +        if (!this.msg.choiceMode) {
   1.193 +          this.msg.choiceMode = "single";
   1.194 +        }
   1.195 +        obj.selected = item.selected;
   1.196 +      }
   1.197 +
   1.198 +      if (item.header)
   1.199 +        obj.isGroup = true;
   1.200 +
   1.201 +      if (item.menu)
   1.202 +        obj.isParent = true;
   1.203 +
   1.204 +      if (item.child)
   1.205 +        obj.inGroup = true;
   1.206 +
   1.207 +      if (item.showAsActions)
   1.208 +        obj.showAsActions = item.showAsActions;
   1.209 +
   1.210 +      if (item.icon)
   1.211 +        obj.icon = item.icon;
   1.212 +
   1.213 +      this.msg.listitems.push(obj);
   1.214 +
   1.215 +    }, this);
   1.216 +    return this;
   1.217 +  },
   1.218 +
   1.219 +  setSingleChoiceItems: function(aItems) {
   1.220 +    return this._setListItems(aItems);
   1.221 +  },
   1.222 +
   1.223 +  setMultiChoiceItems: function(aItems) {
   1.224 +    this.msg.choiceMode = "multiple";
   1.225 +    return this._setListItems(aItems);
   1.226 +  },
   1.227 +
   1.228 +}

mercurial