2013-05-28 16:36:19 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict"
|
|
|
|
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["Prompt"];
|
|
|
|
|
|
|
|
function log(msg) {
|
|
|
|
//Services.console.logStringMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Prompt(aOptions) {
|
|
|
|
this.window = "window" in aOptions ? aOptions.window : null;
|
2013-07-31 15:38:48 -07:00
|
|
|
this.msg = { async: true };
|
|
|
|
|
|
|
|
if (aOptions.priority === 1)
|
|
|
|
this.msg.type = "Prompt:ShowTop"
|
|
|
|
else
|
|
|
|
this.msg.type = "Prompt:Show"
|
2013-05-28 16:36:19 -07:00
|
|
|
|
2013-06-17 10:06:56 -07:00
|
|
|
if ("title" in aOptions && aOptions.title != null)
|
2013-05-28 16:36:19 -07:00
|
|
|
this.msg.title = aOptions.title;
|
|
|
|
|
2013-06-17 10:06:56 -07:00
|
|
|
if ("message" in aOptions && aOptions.message != null)
|
2013-05-28 16:36:19 -07:00
|
|
|
this.msg.text = aOptions.message;
|
|
|
|
|
2013-06-17 10:06:56 -07:00
|
|
|
if ("buttons" in aOptions && aOptions.buttons != null)
|
2013-05-28 16:36:19 -07:00
|
|
|
this.msg.buttons = aOptions.buttons;
|
|
|
|
|
|
|
|
let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
|
|
|
|
this.guid = idService.generateUUID().toString();
|
|
|
|
this.msg.guid = this.guid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Prompt.prototype = {
|
|
|
|
addButton: function(aOptions) {
|
|
|
|
if (!this.msg.buttons)
|
|
|
|
this.msg.buttons = [];
|
|
|
|
this.msg.buttons.push(aOptions.label);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
_addInput: function(aOptions) {
|
|
|
|
let obj = aOptions;
|
|
|
|
if (this[aOptions.type + "_count"] === undefined)
|
|
|
|
this[aOptions.type + "_count"] = 0;
|
|
|
|
|
|
|
|
obj.id = aOptions.id || (aOptions.type + this[aOptions.type + "_count"]);
|
|
|
|
this[aOptions.type + "_count"]++;
|
|
|
|
|
2013-06-03 09:20:44 -07:00
|
|
|
if (!this.msg.inputs)
|
|
|
|
this.msg.inputs = [];
|
2013-05-28 16:36:19 -07:00
|
|
|
this.msg.inputs.push(obj);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
addCheckbox: function(aOptions) {
|
|
|
|
return this._addInput({
|
|
|
|
type: "checkbox",
|
|
|
|
label: aOptions.label,
|
|
|
|
checked: aOptions.checked,
|
|
|
|
id: aOptions.id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addTextbox: function(aOptions) {
|
|
|
|
return this._addInput({
|
|
|
|
type: "textbox",
|
|
|
|
value: aOptions.value,
|
|
|
|
hint: aOptions.hint,
|
|
|
|
autofocus: aOptions.autofocus,
|
|
|
|
id: aOptions.id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addPassword: function(aOptions) {
|
|
|
|
return this._addInput({
|
|
|
|
type: "password",
|
|
|
|
value: aOptions.value,
|
|
|
|
hint: aOptions.hint,
|
|
|
|
autofocus: aOptions.autofocus,
|
|
|
|
id : aOptions.id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-06-03 09:20:44 -07:00
|
|
|
addDatePicker: function(aOptions) {
|
|
|
|
return this._addInput({
|
|
|
|
type: aOptions.type || "date",
|
|
|
|
value: aOptions.value,
|
2013-06-12 09:14:58 -07:00
|
|
|
id: aOptions.id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
addLabel: function(aOptions) {
|
|
|
|
return this._addInput({
|
|
|
|
type: "label",
|
|
|
|
label: aOptions.label,
|
|
|
|
id: aOptions.id
|
2013-06-03 09:20:44 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-28 16:36:19 -07:00
|
|
|
addMenulist: function(aOptions) {
|
|
|
|
return this._addInput({
|
|
|
|
type: "menulist",
|
|
|
|
values: aOptions.values,
|
|
|
|
id: aOptions.id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
show: function(callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
log("Sending message");
|
|
|
|
Services.obs.addObserver(this, "Prompt:Reply", false);
|
|
|
|
this._innerShow();
|
|
|
|
},
|
|
|
|
|
|
|
|
_innerShow: function() {
|
2013-08-08 08:05:48 -07:00
|
|
|
Services.androidBridge.handleGeckoMessage(JSON.stringify(this.msg));
|
2013-05-28 16:36:19 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
log("observe " + aData);
|
|
|
|
let data = JSON.parse(aData);
|
|
|
|
if (data.guid != this.guid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Services.obs.removeObserver(this, "Prompt:Reply", false);
|
|
|
|
|
|
|
|
if (this.callback)
|
|
|
|
this.callback(data);
|
|
|
|
},
|
|
|
|
|
2013-06-04 14:38:01 -07:00
|
|
|
_setListItems: function(aItems) {
|
2013-05-28 16:36:19 -07:00
|
|
|
let hasSelected = false;
|
2013-06-04 14:38:01 -07:00
|
|
|
this.msg.listitems = [];
|
2013-05-28 16:36:19 -07:00
|
|
|
|
|
|
|
aItems.forEach(function(item) {
|
|
|
|
let obj = { id: item.id };
|
|
|
|
|
|
|
|
obj.label = item.label;
|
|
|
|
|
|
|
|
if (item.disabled)
|
|
|
|
obj.disabled = true;
|
|
|
|
|
|
|
|
if (item.selected || hasSelected || this.msg.multiple) {
|
|
|
|
if (!this.msg.selected) {
|
|
|
|
this.msg.selected = new Array(this.msg.listitems.length);
|
|
|
|
hasSelected = true;
|
|
|
|
}
|
|
|
|
this.msg.selected[this.msg.listitems.length] = item.selected;
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:38:01 -07:00
|
|
|
if (item.header)
|
2013-05-28 16:36:19 -07:00
|
|
|
obj.isGroup = true;
|
2013-06-04 14:38:01 -07:00
|
|
|
|
|
|
|
if (item.menu)
|
2013-05-28 16:36:19 -07:00
|
|
|
obj.isParent = true;
|
|
|
|
|
2013-06-04 14:38:01 -07:00
|
|
|
if (item.child)
|
|
|
|
obj.inGroup = true;
|
2013-05-28 16:36:19 -07:00
|
|
|
|
2013-06-04 14:38:01 -07:00
|
|
|
this.msg.listitems.push(obj);
|
2013-06-03 12:04:51 -07:00
|
|
|
|
2013-05-28 16:36:19 -07:00
|
|
|
}, this);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setSingleChoiceItems: function(aItems) {
|
|
|
|
return this._setListItems(aItems);
|
|
|
|
},
|
|
|
|
|
|
|
|
setMultiChoiceItems: function(aItems) {
|
|
|
|
this.msg.multiple = true;
|
|
|
|
return this._setListItems(aItems);
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|