mobile/android/modules/Prompt.jsm

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:5d93231debe7
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"
6
7 let Cc = Components.classes;
8 let Ci = Components.interfaces;
9
10 Components.utils.import("resource://gre/modules/Services.jsm");
11 Components.utils.import("resource://gre/modules/Messaging.jsm");
12
13 this.EXPORTED_SYMBOLS = ["Prompt"];
14
15 function log(msg) {
16 Services.console.logStringMessage(msg);
17 }
18
19 function Prompt(aOptions) {
20 this.window = "window" in aOptions ? aOptions.window : null;
21 this.msg = { async: true };
22
23 if (aOptions.priority === 1)
24 this.msg.type = "Prompt:ShowTop"
25 else
26 this.msg.type = "Prompt:Show"
27
28 if ("title" in aOptions && aOptions.title != null)
29 this.msg.title = aOptions.title;
30
31 if ("message" in aOptions && aOptions.message != null)
32 this.msg.text = aOptions.message;
33
34 if ("buttons" in aOptions && aOptions.buttons != null)
35 this.msg.buttons = aOptions.buttons;
36
37 if ("hint" in aOptions && aOptions.hint != null)
38 this.msg.hint = aOptions.hint;
39
40 let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
41 }
42
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 },
51
52 addButton: function(aOptions) {
53 if (!this.msg.buttons)
54 this.msg.buttons = [];
55 this.msg.buttons.push(aOptions.label);
56 return this;
57 },
58
59 _addInput: function(aOptions) {
60 let obj = aOptions;
61 if (this[aOptions.type + "_count"] === undefined)
62 this[aOptions.type + "_count"] = 0;
63
64 obj.id = aOptions.id || (aOptions.type + this[aOptions.type + "_count"]);
65 this[aOptions.type + "_count"]++;
66
67 if (!this.msg.inputs)
68 this.msg.inputs = [];
69 this.msg.inputs.push(obj);
70 return this;
71 },
72
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 },
81
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 },
91
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 },
101
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 },
111
112 addDatePicker: function(aOptions) {
113 return this._addInput({
114 type: aOptions.type || "date",
115 value: aOptions.value,
116 id: aOptions.id
117 });
118 },
119
120 addColorPicker: function(aOptions) {
121 return this._addInput({
122 type: "color",
123 value: aOptions.value,
124 id: aOptions.id
125 });
126 },
127
128 addLabel: function(aOptions) {
129 return this._addInput({
130 type: "label",
131 label: aOptions.label,
132 id: aOptions.id
133 });
134 },
135
136 addMenulist: function(aOptions) {
137 return this._addInput({
138 type: "menulist",
139 values: aOptions.values,
140 id: aOptions.id
141 });
142 },
143
144 addIconGrid: function(aOptions) {
145 return this._addInput({
146 type: "icongrid",
147 items: aOptions.items,
148 id: aOptions.id
149 });
150 },
151
152 addTabs: function(aOptions) {
153 return this._addInput({
154 type: "tabs",
155 items: aOptions.items,
156 id: aOptions.id
157 });
158 },
159
160 show: function(callback) {
161 this.callback = callback;
162 log("Sending message");
163 this._innerShow();
164 },
165
166 _innerShow: function() {
167 sendMessageToJava(this.msg, (data) => {
168 if (this.callback)
169 this.callback(data);
170 });
171 },
172
173 _setListItems: function(aItems) {
174 let hasSelected = false;
175 this.msg.listitems = [];
176
177 aItems.forEach(function(item) {
178 let obj = { id: item.id };
179
180 obj.label = item.label;
181
182 if (item.icon)
183 obj.icon = item.icon;
184
185 if (item.disabled)
186 obj.disabled = true;
187
188 if (item.selected) {
189 if (!this.msg.choiceMode) {
190 this.msg.choiceMode = "single";
191 }
192 obj.selected = item.selected;
193 }
194
195 if (item.header)
196 obj.isGroup = true;
197
198 if (item.menu)
199 obj.isParent = true;
200
201 if (item.child)
202 obj.inGroup = true;
203
204 if (item.showAsActions)
205 obj.showAsActions = item.showAsActions;
206
207 if (item.icon)
208 obj.icon = item.icon;
209
210 this.msg.listitems.push(obj);
211
212 }, this);
213 return this;
214 },
215
216 setSingleChoiceItems: function(aItems) {
217 return this._setListItems(aItems);
218 },
219
220 setMultiChoiceItems: function(aItems) {
221 this.msg.choiceMode = "multiple";
222 return this._setListItems(aItems);
223 },
224
225 }

mercurial