From c6e5e63ddab9ee7192695df7e16b1d0a6d2b2e96 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Mon, 6 Jul 2020 15:02:31 -0700 Subject: [PATCH] chore: format --- .../ui/dialogs/dialogs-common.ts | 5 + nativescript-core/ui/dialogs/index.d.ts | 188 ++++--- nativescript-core/ui/dialogs/index.ios.ts | 525 ++++++++---------- package.json | 6 +- 4 files changed, 335 insertions(+), 389 deletions(-) diff --git a/nativescript-core/ui/dialogs/dialogs-common.ts b/nativescript-core/ui/dialogs/dialogs-common.ts index a4c6a4bad..6966b9fd3 100644 --- a/nativescript-core/ui/dialogs/dialogs-common.ts +++ b/nativescript-core/ui/dialogs/dialogs-common.ts @@ -49,6 +49,11 @@ export interface ActionOptions extends CancelableOptions { * Gets or sets the list of available actions. */ actions?: Array; + + /** + * [iOS only] Gets or sets the indexes of destructive actions. + */ + destructiveActionsIndexes?: Array; } /** diff --git a/nativescript-core/ui/dialogs/index.d.ts b/nativescript-core/ui/dialogs/index.d.ts index e49579772..cc9bdbe6c 100644 --- a/nativescript-core/ui/dialogs/index.d.ts +++ b/nativescript-core/ui/dialogs/index.d.ts @@ -1,5 +1,4 @@ -import { ConfirmOptions, PromptOptions, AlertOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from './dialogs-common'; -export * from './dialogs-common'; +export * from './dialogs-common'; /** * Defines the input type for prompt dialog. */ @@ -139,159 +138,158 @@ export function action(options: ActionOptions): Promise; * Provides options for the dialog. */ export interface CancelableOptions { - /** - * [Android only] Gets or sets if the dialog can be canceled by taping outside of the dialog. - */ - cancelable?: boolean; + /** + * [Android only] Gets or sets if the dialog can be canceled by taping outside of the dialog. + */ + cancelable?: boolean; } /** * Provides options for the dialog. */ export interface ActionOptions extends CancelableOptions { - /** - * Gets or sets the dialog title. - */ - title?: string; + /** + * Gets or sets the dialog title. + */ + title?: string; - /** - * Gets or sets the dialog message. - */ - message?: string; + /** + * Gets or sets the dialog message. + */ + message?: string; - /** - * Gets or sets the Cancel button text. - */ - cancelButtonText?: string; + /** + * Gets or sets the Cancel button text. + */ + cancelButtonText?: string; - /** - * Gets or sets the list of available actions. - */ - actions?: Array; + /** + * Gets or sets the list of available actions. + */ + actions?: Array; - /** - * [iOS only] Gets or sets the indexes of destructive actions. - */ - destructiveActionsIndexes?: Array; + /** + * [iOS only] Gets or sets the indexes of destructive actions. + */ + destructiveActionsIndexes?: Array; } /** * Provides options for the dialog. */ export interface DialogOptions extends CancelableOptions { - /** - * Gets or sets the dialog title. - */ - title?: string; - - /** - * Gets or sets the dialog message. - */ - message?: string; + /** + * Gets or sets the dialog title. + */ + title?: string; + /** + * Gets or sets the dialog message. + */ + message?: string; } /** * Provides options for the alert. */ export interface AlertOptions extends DialogOptions { - /** - * Gets or sets the OK button text. - */ - okButtonText?: string; + /** + * Gets or sets the OK button text. + */ + okButtonText?: string; } /** * Provides options for the confirm dialog. */ export interface ConfirmOptions extends AlertOptions { - /** - * Gets or sets the Cancel button text. - */ - cancelButtonText?: string; + /** + * Gets or sets the Cancel button text. + */ + cancelButtonText?: string; - /** - * Gets or sets the neutral button text. - */ - neutralButtonText?: string; + /** + * Gets or sets the neutral button text. + */ + neutralButtonText?: string; } /** * Provides options for the prompt dialog. */ export interface PromptOptions extends ConfirmOptions { - /** - * Gets or sets the default text to display in the input box. - */ - defaultText?: string; + /** + * Gets or sets the default text to display in the input box. + */ + defaultText?: string; - /** - * Gets or sets the prompt input type (plain text, password, or email). - */ - inputType?: string; + /** + * Gets or sets the prompt input type (plain text, password, or email). + */ + inputType?: string; - /** - * Gets or sets the prompt capitalizationType (none, all, sentences, or words). - */ - capitalizationType?: string; + /** + * Gets or sets the prompt capitalizationType (none, all, sentences, or words). + */ + capitalizationType?: string; } /** * Provides options for the login dialog. */ export interface LoginOptions extends ConfirmOptions { - /** - * Gets or sets the default text to display as hint in the user name input box. - */ - userNameHint?: string; + /** + * Gets or sets the default text to display as hint in the user name input box. + */ + userNameHint?: string; - /** - * Gets or sets the default text to display as hint in the password input box. - */ - passwordHint?: string; + /** + * Gets or sets the default text to display as hint in the password input box. + */ + passwordHint?: string; - /** - * Gets or sets the default text to display in the user name input box. - */ - userName?: string; + /** + * Gets or sets the default text to display in the user name input box. + */ + userName?: string; - /** - * Gets or sets the default text to display in the password input box. - */ - password?: string; + /** + * Gets or sets the default text to display in the password input box. + */ + password?: string; } /** * Provides result data from the prompt dialog. */ export interface PromptResult { - /** - * Gets or sets the prompt dialog boolean result. - */ - result: boolean; + /** + * Gets or sets the prompt dialog boolean result. + */ + result: boolean; - /** - * Gets or sets the text entered in the prompt dialog. - */ - text: string; + /** + * Gets or sets the text entered in the prompt dialog. + */ + text: string; } /** * Provides result data from the login dialog. */ export interface LoginResult { - /** - * Gets or sets the login dialog boolean result. - */ - result: boolean; + /** + * Gets or sets the login dialog boolean result. + */ + result: boolean; - /** - * Gets or sets the user entered in the login dialog. - */ - userName: string; + /** + * Gets or sets the user entered in the login dialog. + */ + userName: string; - /** - * Gets or sets the password entered in the login dialog. - */ - password: string; + /** + * Gets or sets the password entered in the login dialog. + */ + password: string; } diff --git a/nativescript-core/ui/dialogs/index.ios.ts b/nativescript-core/ui/dialogs/index.ios.ts index 78bea48c4..a29614707 100644 --- a/nativescript-core/ui/dialogs/index.ios.ts +++ b/nativescript-core/ui/dialogs/index.ios.ts @@ -45,338 +45,277 @@ function raiseCallback(callback, result) { } } export function alert(arg: any): Promise { - return new Promise((resolve, reject) => { - try { - let options = !isDialogOptions(arg) - ? { title: DialogStrings.ALERT, okButtonText: DialogStrings.OK, message: arg + "" } - : arg; - let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle( - options.title, - options.message, - UIAlertControllerStyle.Alert - ); + return new Promise((resolve, reject) => { + try { + let options = !isDialogOptions(arg) ? { title: DialogStrings.ALERT, okButtonText: DialogStrings.OK, message: arg + '' } : arg; + let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.Alert); - addButtonsToAlertController(alertController, options, () => { - resolve(); - }); + addButtonsToAlertController(alertController, options, () => { + resolve(); + }); - showUIAlertController(alertController); - } catch (ex) { - reject(ex); - } - }); + showUIAlertController(alertController); + } catch (ex) { + reject(ex); + } + }); } export function confirm(arg: any): Promise { - return new Promise((resolve, reject) => { - try { - let options = !isDialogOptions(arg) - ? { - title: DialogStrings.CONFIRM, - okButtonText: DialogStrings.OK, - cancelButtonText: DialogStrings.CANCEL, - message: arg + "", - } - : arg; - let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle( - options.title, - options.message, - UIAlertControllerStyle.Alert - ); + return new Promise((resolve, reject) => { + try { + let options = !isDialogOptions(arg) + ? { + title: DialogStrings.CONFIRM, + okButtonText: DialogStrings.OK, + cancelButtonText: DialogStrings.CANCEL, + message: arg + '', + } + : arg; + let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.Alert); - addButtonsToAlertController(alertController, options, (r) => { - resolve(r); - }); + addButtonsToAlertController(alertController, options, (r) => { + resolve(r); + }); - showUIAlertController(alertController); - } catch (ex) { - reject(ex); - } - }); + showUIAlertController(alertController); + } catch (ex) { + reject(ex); + } + }); } export function prompt(arg: any): Promise { - let options: PromptOptions; + let options: PromptOptions; - let defaultOptions = { - title: DialogStrings.PROMPT, - okButtonText: DialogStrings.OK, - cancelButtonText: DialogStrings.CANCEL, - inputType: inputType.text, - }; + let defaultOptions = { + title: DialogStrings.PROMPT, + okButtonText: DialogStrings.OK, + cancelButtonText: DialogStrings.CANCEL, + inputType: inputType.text, + }; - if (arguments.length === 1) { - if (isString(arg)) { - options = defaultOptions; - options.message = arg; - } else { - options = arg; - } - } else if (arguments.length === 2) { - if (isString(arguments[0]) && isString(arguments[1])) { - options = defaultOptions; - options.message = arguments[0]; - options.defaultText = arguments[1]; - } + if (arguments.length === 1) { + if (isString(arg)) { + options = defaultOptions; + options.message = arg; + } else { + options = arg; } + } else if (arguments.length === 2) { + if (isString(arguments[0]) && isString(arguments[1])) { + options = defaultOptions; + options.message = arguments[0]; + options.defaultText = arguments[1]; + } + } - return new Promise((resolve, reject) => { - try { - let textField: UITextField; - let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle( - options.title, - options.message, - UIAlertControllerStyle.Alert - ); + return new Promise((resolve, reject) => { + try { + let textField: UITextField; + let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.Alert); - alertController.addTextFieldWithConfigurationHandler( - (arg: UITextField) => { - arg.text = isString(options.defaultText) ? options.defaultText : ""; - arg.secureTextEntry = - options && options.inputType === inputType.password; + alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => { + arg.text = isString(options.defaultText) ? options.defaultText : ''; + arg.secureTextEntry = options && options.inputType === inputType.password; - if (options && options.inputType === inputType.email) { - arg.keyboardType = UIKeyboardType.EmailAddress; - } else if (options && options.inputType === inputType.number) { - arg.keyboardType = UIKeyboardType.NumberPad; - } else if (options && options.inputType === inputType.decimal) { - arg.keyboardType = UIKeyboardType.DecimalPad; - } else if (options && options.inputType === inputType.phone) { - arg.keyboardType = UIKeyboardType.PhonePad; - } - - let color = getTextFieldColor(); - if (color) { - arg.textColor = arg.tintColor = color.ios; - } - } - ); - - textField = alertController.textFields.firstObject; - - if (options) { - switch (options.capitalizationType) { - case capitalizationType.all: { - textField.autocapitalizationType = - UITextAutocapitalizationType.AllCharacters; - break; - } - case capitalizationType.sentences: { - textField.autocapitalizationType = - UITextAutocapitalizationType.Sentences; - break; - } - case capitalizationType.words: { - textField.autocapitalizationType = - UITextAutocapitalizationType.Words; - break; - } - default: { - textField.autocapitalizationType = - UITextAutocapitalizationType.None; - } - } - } - - addButtonsToAlertController(alertController, options, (r) => { - resolve({ result: r, text: textField.text }); - }); - - showUIAlertController(alertController); - } catch (ex) { - reject(ex); + if (options && options.inputType === inputType.email) { + arg.keyboardType = UIKeyboardType.EmailAddress; + } else if (options && options.inputType === inputType.number) { + arg.keyboardType = UIKeyboardType.NumberPad; + } else if (options && options.inputType === inputType.decimal) { + arg.keyboardType = UIKeyboardType.DecimalPad; + } else if (options && options.inputType === inputType.phone) { + arg.keyboardType = UIKeyboardType.PhonePad; } - }); + + let color = getTextFieldColor(); + if (color) { + arg.textColor = arg.tintColor = color.ios; + } + }); + + textField = alertController.textFields.firstObject; + + if (options) { + switch (options.capitalizationType) { + case capitalizationType.all: { + textField.autocapitalizationType = UITextAutocapitalizationType.AllCharacters; + break; + } + case capitalizationType.sentences: { + textField.autocapitalizationType = UITextAutocapitalizationType.Sentences; + break; + } + case capitalizationType.words: { + textField.autocapitalizationType = UITextAutocapitalizationType.Words; + break; + } + default: { + textField.autocapitalizationType = UITextAutocapitalizationType.None; + } + } + } + + addButtonsToAlertController(alertController, options, (r) => { + resolve({ result: r, text: textField.text }); + }); + + showUIAlertController(alertController); + } catch (ex) { + reject(ex); + } + }); } export function login(...args: any[]): Promise { - let options: LoginOptions = parseLoginOptions(args); + let options: LoginOptions = parseLoginOptions(args); - return new Promise((resolve, reject) => { - try { - let userNameTextField: UITextField; - let passwordTextField: UITextField; - let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle( - options.title, - options.message, - UIAlertControllerStyle.Alert - ); + return new Promise((resolve, reject) => { + try { + let userNameTextField: UITextField; + let passwordTextField: UITextField; + let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.Alert); - let textFieldColor = getTextFieldColor(); + let textFieldColor = getTextFieldColor(); - alertController.addTextFieldWithConfigurationHandler( - (arg: UITextField) => { - arg.placeholder = "Login"; - arg.placeholder = options.userNameHint ? options.userNameHint : ""; - arg.text = isString(options.userName) ? options.userName : ""; + alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => { + arg.placeholder = 'Login'; + arg.placeholder = options.userNameHint ? options.userNameHint : ''; + arg.text = isString(options.userName) ? options.userName : ''; - if (textFieldColor) { - arg.textColor = arg.tintColor = textFieldColor.ios; - } - } - ); - - alertController.addTextFieldWithConfigurationHandler( - (arg: UITextField) => { - arg.placeholder = "Password"; - arg.secureTextEntry = true; - arg.placeholder = options.passwordHint ? options.passwordHint : ""; - arg.text = isString(options.password) ? options.password : ""; - - if (textFieldColor) { - arg.textColor = arg.tintColor = textFieldColor.ios; - } - } - ); - - userNameTextField = alertController.textFields.firstObject; - passwordTextField = alertController.textFields.lastObject; - - addButtonsToAlertController(alertController, options, (r) => { - resolve({ - result: r, - userName: userNameTextField.text, - password: passwordTextField.text, - }); - }); - - showUIAlertController(alertController); - } catch (ex) { - reject(ex); + if (textFieldColor) { + arg.textColor = arg.tintColor = textFieldColor.ios; } - }); + }); + + alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => { + arg.placeholder = 'Password'; + arg.secureTextEntry = true; + arg.placeholder = options.passwordHint ? options.passwordHint : ''; + arg.text = isString(options.password) ? options.password : ''; + + if (textFieldColor) { + arg.textColor = arg.tintColor = textFieldColor.ios; + } + }); + + userNameTextField = alertController.textFields.firstObject; + passwordTextField = alertController.textFields.lastObject; + + addButtonsToAlertController(alertController, options, (r) => { + resolve({ + result: r, + userName: userNameTextField.text, + password: passwordTextField.text, + }); + }); + + showUIAlertController(alertController); + } catch (ex) { + reject(ex); + } + }); } function showUIAlertController(alertController: UIAlertController) { - let viewController = ios.rootController; + let viewController = ios.rootController; - while (viewController && viewController.presentedViewController) { - viewController = viewController.presentedViewController; + while (viewController && viewController.presentedViewController) { + viewController = viewController.presentedViewController; + } + + if (!viewController) { + Trace.write(`No root controller found to open dialog.`, Trace.categories.Error, Trace.messageType.warn); + + return; + } + + if (alertController.popoverPresentationController) { + alertController.popoverPresentationController.sourceView = viewController.view; + alertController.popoverPresentationController.sourceRect = CGRectMake(viewController.view.bounds.size.width / 2.0, viewController.view.bounds.size.height / 2.0, 1.0, 1.0); + alertController.popoverPresentationController.permittedArrowDirections = 0; + } + + let color = getButtonColors().color; + if (color) { + alertController.view.tintColor = color.ios; + } + + let lblColor = getLabelColor(); + if (lblColor) { + if (alertController.title) { + let title = NSAttributedString.alloc().initWithStringAttributes(alertController.title, { [NSForegroundColorAttributeName]: lblColor.ios }); + alertController.setValueForKey(title, 'attributedTitle'); } - - if (!viewController) { - Trace.write(`No root controller found to open dialog.`, Trace.categories.Error, Trace.messageType.warn); - - return; + if (alertController.message) { + let message = NSAttributedString.alloc().initWithStringAttributes(alertController.message, { [NSForegroundColorAttributeName]: lblColor.ios }); + alertController.setValueForKey(message, 'attributedMessage'); } + } - if (alertController.popoverPresentationController) { - alertController.popoverPresentationController.sourceView = - viewController.view; - alertController.popoverPresentationController.sourceRect = CGRectMake( - viewController.view.bounds.size.width / 2.0, - viewController.view.bounds.size.height / 2.0, - 1.0, - 1.0 - ); - alertController.popoverPresentationController.permittedArrowDirections = 0; - } - - let color = getButtonColors().color; - if (color) { - alertController.view.tintColor = color.ios; - } - - let lblColor = getLabelColor(); - if (lblColor) { - if (alertController.title) { - let title = NSAttributedString.alloc().initWithStringAttributes( - alertController.title, - { [NSForegroundColorAttributeName]: lblColor.ios } - ); - alertController.setValueForKey(title, "attributedTitle"); - } - if (alertController.message) { - let message = NSAttributedString.alloc().initWithStringAttributes( - alertController.message, - { [NSForegroundColorAttributeName]: lblColor.ios } - ); - alertController.setValueForKey(message, "attributedMessage"); - } - } - - viewController.presentModalViewControllerAnimated(alertController, true); + viewController.presentModalViewControllerAnimated(alertController, true); } export function action(): Promise { - let options: ActionOptions; + let options: ActionOptions; - let defaultOptions = { title: null, cancelButtonText: DialogStrings.CANCEL }; + let defaultOptions = { title: null, cancelButtonText: DialogStrings.CANCEL }; - if (arguments.length === 1) { - if (isString(arguments[0])) { - options = defaultOptions; - options.message = arguments[0]; - } else { - options = arguments[0]; - } - } else if (arguments.length === 2) { - if (isString(arguments[0]) && isString(arguments[1])) { - options = defaultOptions; - options.message = arguments[0]; - options.cancelButtonText = arguments[1]; - } - } else if (arguments.length === 3) { - if ( - isString(arguments[0]) && - isString(arguments[1]) && - isDefined(arguments[2]) - ) { - options = defaultOptions; - options.message = arguments[0]; - options.cancelButtonText = arguments[1]; - options.actions = arguments[2]; - } + if (arguments.length === 1) { + if (isString(arguments[0])) { + options = defaultOptions; + options.message = arguments[0]; + } else { + options = arguments[0]; } + } else if (arguments.length === 2) { + if (isString(arguments[0]) && isString(arguments[1])) { + options = defaultOptions; + options.message = arguments[0]; + options.cancelButtonText = arguments[1]; + } + } else if (arguments.length === 3) { + if (isString(arguments[0]) && isString(arguments[1]) && isDefined(arguments[2])) { + options = defaultOptions; + options.message = arguments[0]; + options.cancelButtonText = arguments[1]; + options.actions = arguments[2]; + } + } - return new Promise((resolve, reject) => { - try { - let i: number; - let action: string; - let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle( - options.title, - options.message, - UIAlertControllerStyle.ActionSheet + return new Promise((resolve, reject) => { + try { + let i: number; + let action: string; + let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.ActionSheet); + + if (options.actions) { + for (i = 0; i < options.actions.length; i++) { + action = options.actions[i]; + if (isString(action)) { + const thisActionIsDestructive = options.destructiveActionsIndexes && options.destructiveActionsIndexes.indexOf(i) !== -1; + const dialogType = thisActionIsDestructive ? UIAlertActionStyle.Destructive : UIAlertActionStyle.Default; + alertController.addAction( + UIAlertAction.actionWithTitleStyleHandler(action, dialogType, (arg: UIAlertAction) => { + resolve(arg.title); + }) ); - - if (options.actions) { - for (i = 0; i < options.actions.length; i++) { - action = options.actions[i]; - if (isString(action)) { - const thisActionIsDestructive = - options.destructiveActionsIndexes && - options.destructiveActionsIndexes.indexOf(i) !== -1; - const dialogType = thisActionIsDestructive - ? UIAlertActionStyle.Destructive - : UIAlertActionStyle.Default; - alertController.addAction( - UIAlertAction.actionWithTitleStyleHandler( - action, - dialogType, - (arg: UIAlertAction) => { - resolve(arg.title); - } - ) - ); - } - } - } - - if (isString(options.cancelButtonText)) { - alertController.addAction( - UIAlertAction.actionWithTitleStyleHandler( - options.cancelButtonText, - UIAlertActionStyle.Cancel, - (arg: UIAlertAction) => { - resolve(arg.title); - } - ) - ); - } - - showUIAlertController(alertController); - } catch (ex) { - reject(ex); + } } - }); + } + + if (isString(options.cancelButtonText)) { + alertController.addAction( + UIAlertAction.actionWithTitleStyleHandler(options.cancelButtonText, UIAlertActionStyle.Cancel, (arg: UIAlertAction) => { + resolve(arg.title); + }) + ); + } + + showUIAlertController(alertController); + } catch (ex) { + reject(ex); + } + }); } diff --git a/package.json b/package.json index dab91aea9..de45a77e0 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "css": "^3.0.0", "css-tree": "^1.0.0-alpha.39", "gonzales": "^1.0.7", + "husky": "^4.2.5", + "lint-staged": "^10.2.11", "madge": "^3.9.2", "markdown-snippet-injector": "~0.2.2", "mocha": "^8.0.1", @@ -69,6 +71,8 @@ } }, "lint-staged": { - "**/*": ["prettier --write ."] + "**/*": [ + "prettier --write ." + ] } }