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