mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
dialogs refactored
This commit is contained in:
@ -11,18 +11,18 @@ var STRING = "string",
|
||||
OK = "OK",
|
||||
CANCEL = "Cancel";
|
||||
|
||||
function createAlertDialog(options: dialogs.DialogOptions): android.app.AlertDialog.Builder {
|
||||
function createAlertDialog(message: string, options: dialogs.DialogOptions): android.app.AlertDialog.Builder {
|
||||
var alert = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity);
|
||||
alert.setTitle(options.title);
|
||||
alert.setMessage(options.message);
|
||||
alert.setTitle(options && options.title ? options.title : "");
|
||||
alert.setMessage(message);
|
||||
return alert;
|
||||
}
|
||||
|
||||
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
|
||||
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
|
||||
|
||||
if (options.okButtonName) {
|
||||
alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
if (options.okButtonText) {
|
||||
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
okCallback();
|
||||
@ -30,8 +30,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
|
||||
}));
|
||||
}
|
||||
|
||||
if (options.cancelButtonName) {
|
||||
alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
if (options.cancelButtonText) {
|
||||
alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
if (cancelCallback) {
|
||||
@ -41,8 +41,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
|
||||
}));
|
||||
}
|
||||
|
||||
if (options.otherButtonName) {
|
||||
alert.setNeutralButton(options.otherButtonName, new android.content.DialogInterface.OnClickListener({
|
||||
if (options.otherButtonText) {
|
||||
alert.setNeutralButton(options.otherButtonText, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
if (neutralCallback) {
|
||||
@ -53,14 +53,12 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
|
||||
}
|
||||
}
|
||||
|
||||
export function alert(arg: any): promises.Promise<void> {
|
||||
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
|
||||
var d = promises.defer<void>();
|
||||
try {
|
||||
var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg
|
||||
var alert = createAlertDialog(message, options);
|
||||
|
||||
var alert = createAlertDialog(options);
|
||||
|
||||
alert.setPositiveButton(options.buttonName, new android.content.DialogInterface.OnClickListener({
|
||||
alert.setPositiveButton(options.buttonText, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
dialog.cancel();
|
||||
d.resolve();
|
||||
@ -76,12 +74,10 @@ export function alert(arg: any): promises.Promise<void> {
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
export function confirm(arg: any): promises.Promise<boolean> {
|
||||
export function confirm(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<boolean> {
|
||||
var d = promises.defer<boolean>();
|
||||
try {
|
||||
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
|
||||
|
||||
var alert = createAlertDialog(options);
|
||||
var alert = createAlertDialog(message, options);
|
||||
|
||||
addButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }, function () { d.resolve(); });
|
||||
|
||||
@ -94,12 +90,10 @@ export function confirm(arg: any): promises.Promise<boolean> {
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
|
||||
export function prompt(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): 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 = createAlertDialog(options);
|
||||
var alert = createAlertDialog(message, options);
|
||||
|
||||
var input = new android.widget.EditText(appmodule.android.context);
|
||||
input.setText(options.defaultText ? options.defaultText : "");
|
||||
|
40
ui/dialogs/dialogs.d.ts
vendored
40
ui/dialogs/dialogs.d.ts
vendored
@ -5,48 +5,28 @@
|
||||
/**
|
||||
* The alert() method displays an alert box with a specified message.
|
||||
* @param message Specifies the text to display in the alert box.
|
||||
* @param options Specifies the options for the alert box. Optional.
|
||||
*/
|
||||
function alert(message: string): promises.Promise<void>;
|
||||
|
||||
/**
|
||||
* The alert() method displays an alert box with a specified options.
|
||||
* @param options Specifies the options for the alert box.
|
||||
*/
|
||||
function alert(options: AlertOptions): promises.Promise<void>;
|
||||
function alert(message: string, options?: AlertOptions): promises.Promise<void>;
|
||||
|
||||
/**
|
||||
* The confirm() method displays a dialog box with a specified message.
|
||||
* @param message Specifies the text to display in the confirm box.
|
||||
* @param options Specifies the options for the confirm box. Optional.
|
||||
*/
|
||||
function confirm(message: string): promises.Promise<boolean>;
|
||||
|
||||
/**
|
||||
* The confirm() method displays a dialog box with a specified message.
|
||||
* @param options Specifies the options for the confirm box.
|
||||
*/
|
||||
function confirm(options: ConfirmOptions): promises.Promise<boolean>;
|
||||
function confirm(message: string, options?: ConfirmOptions): promises.Promise<boolean>;
|
||||
|
||||
/**
|
||||
* The prompt() method displays a dialog box that prompts the visitor for input.
|
||||
* @param message The text to display in the dialog box.
|
||||
* @param options The options for the dialog box. Optional.
|
||||
*/
|
||||
function prompt(message: string): promises.Promise<string>;
|
||||
|
||||
/**
|
||||
* The prompt() method displays a dialog box that prompts the visitor for input.
|
||||
* @param options The options for the dialog box.
|
||||
*/
|
||||
function prompt(options: PromptOptions): promises.Promise<string>;
|
||||
function prompt(message: string, options?: PromptOptions): promises.Promise<string>;
|
||||
|
||||
/**
|
||||
* Provides options for the dialog.
|
||||
*/
|
||||
interface DialogOptions {
|
||||
/**
|
||||
* Gets or sets the alert message.
|
||||
*/
|
||||
message: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the alert title.
|
||||
*/
|
||||
@ -60,7 +40,7 @@
|
||||
/**
|
||||
* Gets or sets the button name.
|
||||
*/
|
||||
buttonName?: string;
|
||||
buttonText?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,17 +50,17 @@
|
||||
/**
|
||||
* Gets or sets the OK button name.
|
||||
*/
|
||||
okButtonName?: string;
|
||||
okButtonText?: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the Cancel button name.
|
||||
*/
|
||||
cancelButtonName?: string;
|
||||
cancelButtonText?: string;
|
||||
|
||||
/**
|
||||
* Gets or sets the Cancel button name.
|
||||
*/
|
||||
otherButtonName?: string;
|
||||
otherButtonText?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,20 +7,22 @@ import view = require("ui/core/view");
|
||||
|
||||
var UIALERTVIEWDELEGATE = "UIAlertViewDelegate",
|
||||
STRING = "string",
|
||||
PROMPT = "Prompt",
|
||||
CONFIRM = "Confirm",
|
||||
ALERT = "Alert",
|
||||
OK = "OK",
|
||||
CANCEL = "Cancel";
|
||||
|
||||
function createUIAlertView(options: dialogs.DialogOptions): UIKit.UIAlertView {
|
||||
function createUIAlertView(message: string, options: dialogs.DialogOptions): UIKit.UIAlertView {
|
||||
var alert = new UIKit.UIAlertView();
|
||||
alert.title = options.title;
|
||||
alert.message = options.message;
|
||||
alert.title = options && options.title ? options.title : "";
|
||||
alert.message = message;
|
||||
return alert;
|
||||
}
|
||||
|
||||
function createDelegate(callback) {
|
||||
var delegateType = Foundation.NSObject.extends({}, {}).implements({
|
||||
protocol: UIALERTVIEWDELEGATE,
|
||||
protocol: "UIAlertViewDelegate",
|
||||
implementation: {
|
||||
alertViewClickedButtonAtIndex: function (view, index) {
|
||||
callback(view, index);
|
||||
@ -31,28 +33,29 @@ function createDelegate(callback) {
|
||||
}
|
||||
|
||||
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void {
|
||||
if (options.okButtonName) {
|
||||
alert.addButtonWithTitle(options.okButtonName);
|
||||
if (!options)
|
||||
return;
|
||||
|
||||
if (options.okButtonText) {
|
||||
alert.addButtonWithTitle(options.okButtonText);
|
||||
}
|
||||
|
||||
if (options.cancelButtonName) {
|
||||
alert.addButtonWithTitle(options.cancelButtonName);
|
||||
if (options.cancelButtonText) {
|
||||
alert.addButtonWithTitle(options.cancelButtonText);
|
||||
}
|
||||
|
||||
if (options.otherButtonName) {
|
||||
alert.addButtonWithTitle(options.otherButtonName);
|
||||
if (options.otherButtonText) {
|
||||
alert.addButtonWithTitle(options.otherButtonText);
|
||||
}
|
||||
}
|
||||
|
||||
export function alert(arg: any): promises.Promise<void> {
|
||||
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
|
||||
var d = promises.defer<void>();
|
||||
try {
|
||||
var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg
|
||||
var alert = createUIAlertView(message, options);
|
||||
|
||||
var alert = createUIAlertView(options);
|
||||
|
||||
if (options.buttonName) {
|
||||
alert.addButtonWithTitle(options.buttonName);
|
||||
if (options.buttonText) {
|
||||
alert.addButtonWithTitle(options.buttonText);
|
||||
}
|
||||
|
||||
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
|
||||
@ -72,12 +75,10 @@ export function alert(arg: any): promises.Promise<void> {
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
export function confirm(arg: any): promises.Promise<boolean> {
|
||||
export function confirm(message: string, options = { title: CONFIRM, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<boolean> {
|
||||
var d = promises.defer<boolean>();
|
||||
try {
|
||||
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
|
||||
|
||||
var alert = createUIAlertView(options);
|
||||
var alert = createUIAlertView(message, options);
|
||||
|
||||
addButtonsToAlertDialog(alert, options);
|
||||
|
||||
@ -99,12 +100,10 @@ export function confirm(arg: any): promises.Promise<boolean> {
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
|
||||
export function prompt(message: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): 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);
|
||||
var alert = createUIAlertView(message, options);
|
||||
alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput;
|
||||
|
||||
addButtonsToAlertDialog(alert, options);
|
||||
|
Reference in New Issue
Block a user