Files
NativeScript/ui/dialogs/dialogs.android.ts
2014-06-12 10:35:51 +03:00

195 lines
6.3 KiB
TypeScript

/**
* Android specific dialogs functions implementation.
*/
import promises = require("promises");
import dialogs = require("ui/dialogs");
import dialogs_common = require("ui/dialogs/dialogs-common");
import appmodule = require("application");
import view = require("ui/core/view");
// merge the exports of the request file with the exports of this file
declare var exports;
require("utils/module-merge").merge(dialogs_common, exports);
function createAlertDialog(message: string, options: dialogs.DialogOptions): android.app.AlertDialog.Builder {
var alert = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity);
alert.setTitle(options && options.title ? options.title : "");
alert.setMessage(message);
return alert;
}
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.DialogButtonsOptions,
callback: Function): void {
if (!options)
return;
if (options.okButtonText) {
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
callback(true);
}
}));
}
if (options.cancelButtonText) {
alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
callback(false);
}
}));
}
if (options.neutralButtonText) {
alert.setNeutralButton(options.neutralButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
callback(undefined);
}
}));
}
}
export function alert(message: string, options = { title: dialogs_common.ALERT, okButtonText: dialogs_common.OK }): promises.Promise<void> {
var d = promises.defer<void>();
try {
var alert = createAlertDialog(message, options);
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
d.resolve();
}
}));
alert.show();
} catch (ex) {
d.reject(ex);
}
return d.promise();
}
export function confirm(message: string, options = { title: dialogs_common.CONFIRM, okButtonText: dialogs_common.OK, cancelButtonText: dialogs_common.CANCEL }): promises.Promise<boolean> {
var d = promises.defer<boolean>();
try {
var alert = createAlertDialog(message, options);
addButtonsToAlertDialog(alert, options, function (result) { d.resolve(result); });
alert.show();
} catch (ex) {
d.reject(ex);
}
return d.promise();
}
export function prompt(message: string, defaultText?: string,
options = { title: dialogs_common.PROMPT, okButtonText: dialogs_common.OK, cancelButtonText: dialogs_common.CANCEL, inputType: dialogs_common.InputType.PlainText }): promises.Promise<dialogs.PromptResult> {
var d = promises.defer<dialogs.PromptResult>();
try {
var alert = createAlertDialog(message, options);
var input = new android.widget.EditText(appmodule.android.context);
if (options.inputType == dialogs_common.InputType.Password) {
input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
input.setText(defaultText ? defaultText : "");
alert.setView(input);
var getText = function () { return input.getText().toString(); };
addButtonsToAlertDialog(alert, options, function (r) { d.resolve({ result: r, text: getText() }); });
alert.show();
} catch (ex) {
d.reject(ex);
}
return d.promise();
}
export function login(message: string, userName?: string, password?: string,
options = { title: dialogs_common.LOGIN, okButtonText: dialogs_common.OK, cancelButtonText: dialogs_common.CANCEL }): promises.Promise<dialogs.LoginResult> {
var d = promises.defer<dialogs.LoginResult>();
try {
var context = appmodule.android.context;
var alert = createAlertDialog(message, options);
var userNameInput = new android.widget.EditText(context);
userNameInput.setText(userName ? userName : "");
var passwordInput = new android.widget.EditText(appmodule.android.context);
passwordInput.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordInput.setText(password ? password : "");
var layout = new android.widget.LinearLayout(context);
layout.setOrientation(1);
layout.addView(userNameInput);
layout.addView(passwordInput);
alert.setView(layout);
addButtonsToAlertDialog(alert, options, function (r) {
d.resolve({
result: r,
userName: userNameInput.getText().toString(),
password: passwordInput.getText().toString()
});
});
alert.show();
} catch (ex) {
d.reject(ex);
}
return d.promise();
}
export class Dialog {
private _dialog: android.app.AlertDialog;
private _android: android.app.AlertDialog.Builder;
//private _view: view.View;
constructor(message: string, callback?: (result: boolean) => {}, options?: dialogs.DialogButtonsOptions) {
this._android = createAlertDialog(message, options);
addButtonsToAlertDialog(this.android, options, function (r) {
if (callback) {
callback(r);
}
});
}
get android(): android.app.AlertDialog.Builder {
return this._android;
}
/*
get view(): view.View {
return this._view;
}
set view(value: view.View) {
this._view = value;
this.android.setView(this._view.android);
}*/
public show() {
this._dialog = this.android.show();
}
public hide() {
if (this._dialog) {
this._dialog.hide();
}
}
}