mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
alert. confirm and prompt options combined. defaultText now part of the confirm method signature
This commit is contained in:
@ -7,6 +7,8 @@ import appmodule = require("application");
|
|||||||
import view = require("ui/core/view");
|
import view = require("ui/core/view");
|
||||||
|
|
||||||
var STRING = "string",
|
var STRING = "string",
|
||||||
|
PROMPT = "Prompt",
|
||||||
|
CONFIRM = "Confirm",
|
||||||
ALERT = "Alert",
|
ALERT = "Alert",
|
||||||
OK = "OK",
|
OK = "OK",
|
||||||
CANCEL = "Cancel";
|
CANCEL = "Cancel";
|
||||||
@ -18,7 +20,7 @@ function createAlertDialog(message: string, options: dialogs.DialogOptions): and
|
|||||||
return alert;
|
return alert;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
|
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.DialogButtonOptions,
|
||||||
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
|
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
|
||||||
|
|
||||||
if (options.okButtonText) {
|
if (options.okButtonText) {
|
||||||
@ -41,8 +43,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.otherButtonText) {
|
if (options.neutralButtonText) {
|
||||||
alert.setNeutralButton(options.otherButtonText, new android.content.DialogInterface.OnClickListener({
|
alert.setNeutralButton(options.neutralButtonText, 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,12 +55,12 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
|
export function alert(message: string, options = { title: ALERT, okButtonText: OK }): promises.Promise<void> {
|
||||||
var d = promises.defer<void>();
|
var d = promises.defer<void>();
|
||||||
try {
|
try {
|
||||||
var alert = createAlertDialog(message, options);
|
var alert = createAlertDialog(message, options);
|
||||||
|
|
||||||
alert.setPositiveButton(options.buttonText, 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();
|
||||||
d.resolve();
|
d.resolve();
|
||||||
@ -74,7 +76,7 @@ export function alert(message: string, options = { title: ALERT, buttonText: OK
|
|||||||
return d.promise();
|
return d.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function confirm(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL }): 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 alert = createAlertDialog(message, options);
|
var alert = createAlertDialog(message, options);
|
||||||
@ -90,13 +92,13 @@ export function confirm(message: string, options = { title: ALERT, okButtonText:
|
|||||||
return d.promise();
|
return d.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prompt(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
|
export function prompt(message: string, defaultText?: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<dialogs.PromptResult> {
|
||||||
var d = promises.defer<dialogs.PromptResult>();
|
var d = promises.defer<dialogs.PromptResult>();
|
||||||
try {
|
try {
|
||||||
var alert = createAlertDialog(message, options);
|
var alert = createAlertDialog(message, 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(defaultText ? defaultText : "");
|
||||||
|
|
||||||
alert.setView(input);
|
alert.setView(input);
|
||||||
|
|
||||||
|
31
ui/dialogs/dialogs.d.ts
vendored
31
ui/dialogs/dialogs.d.ts
vendored
@ -14,14 +14,14 @@
|
|||||||
* @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.
|
* @param options Specifies the options for the confirm box. Optional.
|
||||||
*/
|
*/
|
||||||
function confirm(message: string, options?: ConfirmOptions): promises.Promise<boolean>;
|
function confirm(message: string, options?: DialogButtonOptions): 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.
|
* @param options The options for the dialog box. Optional.
|
||||||
*/
|
*/
|
||||||
function prompt(message: string, options?: PromptOptions): promises.Promise<string>;
|
function prompt(message: string, defaultText?: string, options?: DialogButtonOptions): promises.Promise<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides options for the dialog.
|
* Provides options for the dialog.
|
||||||
@ -38,39 +38,24 @@
|
|||||||
*/
|
*/
|
||||||
interface AlertOptions extends DialogOptions {
|
interface AlertOptions extends DialogOptions {
|
||||||
/**
|
/**
|
||||||
* Gets or sets the button name.
|
* Gets or sets the OK button text.
|
||||||
*/
|
*/
|
||||||
buttonText?: string;
|
okButtonText?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides options for the confirm.
|
* Provides options for the confirm.
|
||||||
*/
|
*/
|
||||||
interface ConfirmOptions extends DialogOptions {
|
interface DialogButtonOptions extends AlertOptions {
|
||||||
/**
|
/**
|
||||||
* Gets or sets the OK button name.
|
* Gets or sets the Cancel button text.
|
||||||
*/
|
|
||||||
okButtonText?: string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets or sets the Cancel button name.
|
|
||||||
*/
|
*/
|
||||||
cancelButtonText?: string;
|
cancelButtonText?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or sets the Cancel button name.
|
* Gets or sets the neutral button text.
|
||||||
*/
|
*/
|
||||||
otherButtonText?: string;
|
neutralButtonText?: string;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides options for the prompt.
|
|
||||||
*/
|
|
||||||
interface PromptOptions extends ConfirmOptions {
|
|
||||||
/**
|
|
||||||
* Gets or sets the default text.
|
|
||||||
*/
|
|
||||||
defaultText?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,7 +32,7 @@ function createDelegate(callback) {
|
|||||||
return new delegateType;
|
return new delegateType;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void {
|
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.DialogButtonOptions): void {
|
||||||
if (!options)
|
if (!options)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -44,18 +44,18 @@ function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.Conf
|
|||||||
alert.addButtonWithTitle(options.cancelButtonText);
|
alert.addButtonWithTitle(options.cancelButtonText);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.otherButtonText) {
|
if (options.neutralButtonText) {
|
||||||
alert.addButtonWithTitle(options.otherButtonText);
|
alert.addButtonWithTitle(options.neutralButtonText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
|
export function alert(message: string, options = { title: ALERT, okButtonText: OK }): promises.Promise<void> {
|
||||||
var d = promises.defer<void>();
|
var d = promises.defer<void>();
|
||||||
try {
|
try {
|
||||||
var alert = createUIAlertView(message, options);
|
var alert = createUIAlertView(message, options);
|
||||||
|
|
||||||
if (options.buttonText) {
|
if (options.okButtonText) {
|
||||||
alert.addButtonWithTitle(options.buttonText);
|
alert.addButtonWithTitle(options.okButtonText);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@ -100,7 +100,7 @@ export function confirm(message: string, options = { title: CONFIRM, okButtonTe
|
|||||||
return d.promise();
|
return d.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prompt(message: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
|
export function prompt(message: string, defaultText?: 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 alert = createUIAlertView(message, options);
|
var alert = createUIAlertView(message, options);
|
||||||
@ -109,7 +109,7 @@ export function prompt(message: string, options = { title: PROMPT, okButtonText:
|
|||||||
addButtonsToAlertDialog(alert, options);
|
addButtonsToAlertDialog(alert, options);
|
||||||
|
|
||||||
var textField = alert.textFieldAtIndex(0);
|
var textField = alert.textFieldAtIndex(0);
|
||||||
textField.text = options.defaultText ? options.defaultText : "";
|
textField.text = defaultText ? defaultText : "";
|
||||||
|
|
||||||
// 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.
|
||||||
var delegate = createDelegate(function (view, index) {
|
var delegate = createDelegate(function (view, index) {
|
||||||
|
Reference in New Issue
Block a user