mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
dialogs improved with neutral button and ConfirmResult for confirm and prompt dialogs
This commit is contained in:
@@ -18,23 +18,39 @@ function createAlertDialog(options: dialogs.DialogOptions): android.app.AlertDia
|
||||
return alert;
|
||||
}
|
||||
|
||||
function addOkCancelButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
|
||||
okCallback: Function, cancelCallback?: Function): void {
|
||||
alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
okCallback();
|
||||
}
|
||||
}));
|
||||
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
|
||||
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
|
||||
|
||||
alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
if (cancelCallback) {
|
||||
cancelCallback();
|
||||
if (options.okButtonName) {
|
||||
alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
okCallback();
|
||||
}
|
||||
}
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
||||
if (options.cancelButtonName) {
|
||||
alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
if (cancelCallback) {
|
||||
cancelCallback();
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
if (options.otherButtonName) {
|
||||
alert.setNeutralButton(options.otherButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
if (neutralCallback) {
|
||||
neutralCallback();
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export function alert(arg: any): promises.Promise<void> {
|
||||
@@ -67,7 +83,7 @@ export function confirm(arg: any): promises.Promise<boolean> {
|
||||
|
||||
var alert = createAlertDialog(options);
|
||||
|
||||
addOkCancelButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); });
|
||||
addButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }, function () { d.resolve(); });
|
||||
|
||||
alert.show();
|
||||
|
||||
@@ -78,8 +94,8 @@ export function confirm(arg: any): promises.Promise<boolean> {
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
export function prompt(arg: any): promises.Promise<string> {
|
||||
var d = promises.defer<string>();
|
||||
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
|
||||
var d = promises.defer<dialogs.PromptResult>();
|
||||
try {
|
||||
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
|
||||
|
||||
@@ -90,7 +106,11 @@ export function prompt(arg: any): promises.Promise<string> {
|
||||
|
||||
alert.setView(input);
|
||||
|
||||
addOkCancelButtonsToAlertDialog(alert, options, function () { d.resolve(input.getText().toString()); });
|
||||
var getText = function () { return input.getText().toString(); };
|
||||
|
||||
addButtonsToAlertDialog(alert, options, function () { d.resolve({ result: true, text: getText() }); },
|
||||
function () { d.resolve({ result: false, text: getText() }); },
|
||||
function () { d.resolve({ result: undefined, text: getText() }); });
|
||||
|
||||
alert.show();
|
||||
|
||||
@@ -105,7 +125,7 @@ export class Dialog {
|
||||
private _dialog: android.app.AlertDialog;
|
||||
private _android: android.app.AlertDialog.Builder;
|
||||
private _title: string;
|
||||
private _view: view.View;
|
||||
//private _view: view.View;
|
||||
|
||||
constructor() {
|
||||
this._android = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity);
|
||||
@@ -122,14 +142,14 @@ export class Dialog {
|
||||
this._title = value;
|
||||
this.android.setTitle(this._title);
|
||||
}
|
||||
|
||||
/*
|
||||
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();
|
||||
|
||||
20
ui/dialogs/dialogs.d.ts
vendored
20
ui/dialogs/dialogs.d.ts
vendored
@@ -76,6 +76,11 @@
|
||||
* Gets or sets the Cancel button name.
|
||||
*/
|
||||
cancelButtonName?: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the Cancel button name.
|
||||
*/
|
||||
otherButtonName?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,6 +93,21 @@
|
||||
defaultText?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides result data from the prompt dialog.
|
||||
*/
|
||||
interface PromptResult {
|
||||
/**
|
||||
* Gets or sets the prompt dialog boolean result.
|
||||
*/
|
||||
result: boolean;
|
||||
|
||||
/**
|
||||
* Gets or sets the text entered in the prompt dialog.
|
||||
*/
|
||||
text: string;
|
||||
}
|
||||
|
||||
export class Dialog {
|
||||
/**
|
||||
* Shows the dialog.
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
import promises = require("promises");
|
||||
import dialogs = require("ui/dialogs");
|
||||
import view = require("ui/core/view");
|
||||
|
||||
var UIALERTVIEWDELEGATE = "UIAlertViewDelegate",
|
||||
STRING = "string",
|
||||
@@ -29,6 +30,20 @@ function createDelegate(callback) {
|
||||
return new delegateType;
|
||||
}
|
||||
|
||||
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void {
|
||||
if (options.okButtonName) {
|
||||
alert.addButtonWithTitle(options.okButtonName);
|
||||
}
|
||||
|
||||
if (options.cancelButtonName) {
|
||||
alert.addButtonWithTitle(options.cancelButtonName);
|
||||
}
|
||||
|
||||
if (options.otherButtonName) {
|
||||
alert.addButtonWithTitle(options.otherButtonName);
|
||||
}
|
||||
}
|
||||
|
||||
export function alert(arg: any): promises.Promise<void> {
|
||||
var d = promises.defer<void>();
|
||||
try {
|
||||
@@ -36,7 +51,9 @@ export function alert(arg: any): promises.Promise<void> {
|
||||
|
||||
var alert = createUIAlertView(options);
|
||||
|
||||
alert.addButtonWithTitle(options.buttonName);
|
||||
if (options.buttonName) {
|
||||
alert.addButtonWithTitle(options.buttonName);
|
||||
}
|
||||
|
||||
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
|
||||
var delegate = createDelegate(function (view, index) {
|
||||
@@ -62,12 +79,11 @@ export function confirm(arg: any): promises.Promise<boolean> {
|
||||
|
||||
var alert = createUIAlertView(options);
|
||||
|
||||
alert.addButtonWithTitle(options.okButtonName);
|
||||
alert.addButtonWithTitle(options.cancelButtonName);
|
||||
addButtonsToAlertDialog(alert, options);
|
||||
|
||||
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
|
||||
var delegate = createDelegate(function (view, index) {
|
||||
d.resolve(index === 0);
|
||||
d.resolve(index === 2 ? undefined : index === 0);
|
||||
// Remove the local variable for the delegate.
|
||||
delegate = undefined;
|
||||
});
|
||||
@@ -83,24 +99,22 @@ export function confirm(arg: any): promises.Promise<boolean> {
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
export function prompt(arg: any): promises.Promise<string> {
|
||||
var d = promises.defer<string>();
|
||||
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
|
||||
var d = promises.defer<dialogs.PromptResult>();
|
||||
try {
|
||||
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
|
||||
|
||||
var alert = createUIAlertView(options);
|
||||
alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput;
|
||||
alert.addButtonWithTitle(options.okButtonName);
|
||||
alert.addButtonWithTitle(options.cancelButtonName);
|
||||
|
||||
addButtonsToAlertDialog(alert, options);
|
||||
|
||||
var textField = alert.textFieldAtIndex(0);
|
||||
textField.text = options.defaultText ? options.defaultText : "";
|
||||
|
||||
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
|
||||
var delegate = createDelegate(function (view, index) {
|
||||
if (index === 0) {
|
||||
d.resolve(textField.text);
|
||||
}
|
||||
d.resolve({ result: index === 2 ? undefined : index === 0, text: textField.text });
|
||||
// Remove the local variable for the delegate.
|
||||
delegate = undefined;
|
||||
});
|
||||
@@ -118,6 +132,8 @@ export function prompt(arg: any): promises.Promise<string> {
|
||||
|
||||
export class Dialog {
|
||||
private _ios: UIKit.UIAlertView;
|
||||
//private _view: view.View;
|
||||
//private _nativeView: UIKit.UIView;
|
||||
|
||||
constructor() {
|
||||
this._ios = new UIKit.UIAlertView();
|
||||
@@ -133,6 +149,18 @@ export class Dialog {
|
||||
set title(value: string) {
|
||||
this.ios.title = value;
|
||||
}
|
||||
/*
|
||||
get view(): view.View {
|
||||
return this._view;
|
||||
}
|
||||
set view(value: view.View) {
|
||||
this._view = value;
|
||||
this._nativeView = this._view.ios;
|
||||
this._nativeView.removeFromSuperview();
|
||||
|
||||
// Not working on iOS7!
|
||||
this.ios.addSubview(this._nativeView);
|
||||
}*/
|
||||
|
||||
public show() {
|
||||
this.ios.show();
|
||||
|
||||
Reference in New Issue
Block a user