@nstudio/nativescript-fancyalert
Version:
Fancy alerts for NativeScript.
308 lines • 14.8 kB
JavaScript
import { Color } from '@nativescript/core';
export * from './common';
export class TNSFancyAlert {
static showSuccess(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.SUCCESS, title || 'Success!', subTitle, closeBtnTitle, duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showError(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.ERROR, title || 'Error!', subTitle, closeBtnTitle, duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showNotice(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.NOTICE, title || 'Notice', subTitle, closeBtnTitle, duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showWarning(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.WARNING, title || 'Warning!', subTitle, closeBtnTitle, duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showInfo(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.INFO, title || 'Info', subTitle, closeBtnTitle, duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showEdit(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.EDIT, title || 'Edit', subTitle, closeBtnTitle, duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showWaiting(title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.WAITING, title || 'Waiting...', subTitle, closeBtnTitle, duration || 5, width);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showQuestion(title, subTitle, closeBtnTitle, duration, width, buttons) {
return new Promise((resolve, reject) => {
TNSFancyAlert.show(TNSFancyAlert.SUPPORTED_TYPES.QUESTION, title || 'Waiting...', subTitle, closeBtnTitle || 'Dismiss', duration, width, buttons);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown
resolve();
});
}
static showCustomButtonTimer(buttonIndex, reverse, imageName, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
buttonIndex = buttonIndex || 0;
reverse = reverse || false;
title = title || 'Title';
alert.addTimerToButtonIndexReverse(buttonIndex, reverse);
TNSFancyAlert.showCustom(alert, imageName, color, title, subTitle, closeBtnTitle || 'Dismiss', duration || 5);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown passing back instance to allow further control by developer
resolve(alert);
});
}
static showCustomImage(imageName, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
let image = UIImage.imageNamed(imageName);
alert.showCustomColorTitleSubTitleCloseButtonTitleDuration(image, new Color(color).ios, title, subTitle, closeBtnTitle || 'Ok', duration || 0);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown passing back instance to allow further control by developer
resolve(alert);
});
}
static showCustomButtons(buttons, image, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
for (let btn of buttons) {
const slcBtn = alert.addButtonActionBlock(btn.label, () => {
btn.action();
});
if (btn.applyStyle) {
btn.applyStyle(slcBtn);
}
}
TNSFancyAlert.showCustom(alert, image, color, title, subTitle, null, duration);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown passing back instance to allow further control by developer
resolve(alert);
});
}
static applyTextDisplayOptions(alert) {
if (TNSFancyAlert.textDisplayOptions) {
const systemFont = UIFont.systemFontOfSize(12);
// get default font family name
let fontFamily = systemFont.familyName;
// default size
let fontSize = 14;
// custom font sizes
const titleSize = TNSFancyAlert.textDisplayOptions.titleSize || fontSize;
const bodySize = TNSFancyAlert.textDisplayOptions.bodySize || fontSize;
const buttonSize = TNSFancyAlert.textDisplayOptions.buttonSize || fontSize;
// if one font is specified on any option just use that for all
if (TNSFancyAlert.textDisplayOptions.applyFontToAll) {
if (TNSFancyAlert.textDisplayOptions.titleFont) {
fontFamily = TNSFancyAlert.textDisplayOptions.titleFont;
}
else if (TNSFancyAlert.textDisplayOptions.bodyFont) {
fontFamily = TNSFancyAlert.textDisplayOptions.bodyFont;
}
else if (TNSFancyAlert.textDisplayOptions.buttonFont) {
fontFamily = TNSFancyAlert.textDisplayOptions.buttonFont;
}
}
alert.setTitleFontFamilyWithSize(fontFamily, titleSize);
alert.setBodyTextFontFamilyWithSize(fontFamily, bodySize);
alert.setButtonsTextFontFamilyWithSize(fontFamily, buttonSize);
}
}
static showCustomTextAttributes(attributionBlock, button, image, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
alert.attributedFormatBlock = attributionBlock;
alert.addButtonActionBlock(button.label, () => {
button.action();
resolve();
});
TNSFancyAlert.showCustom(alert, image, color, title, subTitle, null, duration);
});
}
static showTextField(placeholder, initialValue, button, image, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
var textField = alert.addTextField(placeholder);
if (initialValue)
textField.text = initialValue;
alert.addButtonActionBlock(button.label, () => {
textField.resignFirstResponder();
button.action(textField.text);
resolve();
});
TNSFancyAlert.showCustom(alert, image, color, title, subTitle, null, duration);
});
}
static showSwitch(switchLabel, switchColor, button, image, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
var switchView = alert.addSwitchViewWithLabel(switchLabel);
switchView.tintColor = new Color(switchColor).ios;
alert.addButtonActionBlock(button.label, () => {
button.action(switchView.selected);
resolve();
});
TNSFancyAlert.showCustom(alert, image, color, title, subTitle, null, duration);
});
}
static showCustomView(customView, image, color, title, subTitle, closeBtnTitle, duration, width) {
return new Promise((resolve, reject) => {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
alert.addCustomView(customView);
TNSFancyAlert.showCustom(alert, image, color, title, subTitle, closeBtnTitle, duration);
// TODO: find way to resolve only after button is tapped
// right now just resolve after its shown passing back instance to allow further control by developer
resolve(alert);
});
}
/**
* Base Method
**/
static show(type, title, subTitle, closeBtnTitle, duration, width, buttons) {
let alert = TNSFancyAlert.createAlert(width);
TNSFancyAlert.applyTextDisplayOptions(alert);
// add custom buttons
if (buttons) {
for (let btn of buttons) {
alert.addButtonActionBlock(btn.label, () => {
btn.action();
});
}
}
// apply options to instance
TNSFancyAlert.applyOptions(alert);
if (typeof closeBtnTitle === 'undefined')
closeBtnTitle = 'Ok';
alert[`show${type}SubTitleCloseButtonTitleDuration`](title, subTitle, closeBtnTitle, duration || 0);
}
static showCustom(alert, image, color, title, subTitle, closeBtnTitle, duration) {
// apply options to instance
TNSFancyAlert.applyOptions(alert);
if (typeof image === 'undefined')
image = 'nativescript.png';
if (typeof color === 'undefined')
color = '#2B33FF';
if (typeof closeBtnTitle === 'undefined')
closeBtnTitle = 'Ok';
if (typeof image === 'string') {
image = UIImage.imageNamed(image);
}
alert.showCustomColorTitleSubTitleCloseButtonTitleDuration(image, new Color(color).ios, title, subTitle, closeBtnTitle, duration || 0);
}
/**
* Alert Options
*/
static applyOptions(alert) {
alert.shouldDismissOnTapOutside = TNSFancyAlert.shouldDismissOnTapOutside;
if (TNSFancyAlert.hideAnimationType)
alert.hideAnimationType = TNSFancyAlert.hideAnimationType;
if (TNSFancyAlert.showAnimationType)
alert.showAnimationType = TNSFancyAlert.showAnimationType;
if (TNSFancyAlert.backgroundType)
alert.backgroundType = TNSFancyAlert.backgroundType;
if (TNSFancyAlert.customViewColor)
alert.customViewColor = new Color(TNSFancyAlert.customViewColor).ios;
if (TNSFancyAlert.iconTintColor)
alert.iconTintColor = new Color(TNSFancyAlert.iconTintColor).ios;
if (TNSFancyAlert.titleColor)
alert.labelTitle.textColor = new Color(TNSFancyAlert.titleColor).ios;
if (TNSFancyAlert.bodyTextColor)
alert.viewText.textColor = new Color(TNSFancyAlert.bodyTextColor).ios;
alert.tintTopCircle = TNSFancyAlert.tintTopCircle;
if (TNSFancyAlert.cornerRadius)
alert.cornerRadius = TNSFancyAlert.cornerRadius;
if (TNSFancyAlert.backgroundViewColor)
alert.backgroundViewColor = new Color(TNSFancyAlert.backgroundViewColor).ios;
alert.useLargerIcon = TNSFancyAlert.useLargerIcon;
if (TNSFancyAlert.soundURL)
alert.soundURL = NSURL.fileURLWithPath(`${NSBundle.mainBundle.resourcePath}/${TNSFancyAlert.soundURL}`);
}
/**
* Alert Creator
**/
static createAlert(width) {
let alert;
if (width) {
alert = SCLAlertView.alloc().initWithNewWindowWidth(width);
}
else {
alert = SCLAlertView.alloc().initWithNewWindow();
}
if (TNSFancyAlert.dismissCallback) {
alert.alertIsDismissed(TNSFancyAlert.dismissCallback);
}
return alert;
}
}
TNSFancyAlert.SUPPORTED_TYPES = {
SUCCESS: 'Success',
ERROR: 'Error',
NOTICE: 'Notice',
WARNING: 'Warning',
INFO: 'Info',
EDIT: 'Edit',
WAITING: 'Waiting',
QUESTION: 'Question',
};
//Dismiss on tap outside (Default is NO)
TNSFancyAlert.shouldDismissOnTapOutside = false;
TNSFancyAlert.HIDE_ANIMATION_TYPES = {
FadeOut: 0 /* FadeOut */,
SlideOutToBottom: 1 /* SlideOutToBottom */,
SlideOutToTop: 2 /* SlideOutToTop */,
SlideOutToLeft: 3 /* SlideOutToLeft */,
SlideOutToRight: 4 /* SlideOutToRight */,
SlideOutToCenter: 5 /* SlideOutToCenter */,
SlideOutFromCenter: 6 /* SlideOutFromCenter */,
};
TNSFancyAlert.SHOW_ANIMATION_TYPES = {
FadeIn: 0 /* FadeIn */,
SlideInFromBottom: 1 /* SlideInFromBottom */,
SlideInFromTop: 2 /* SlideInFromTop */,
SlideInFromLeft: 3 /* SlideInFromLeft */,
SlideInFromRight: 4 /* SlideInFromRight */,
SlideInFromCenter: 5 /* SlideInFromCenter */,
SlideInToCenter: 6 /* SlideInToCenter */,
};
TNSFancyAlert.BACKGROUND_TYPES = {
Shadow: 0 /* Shadow */,
Blur: 1 /* Blur */,
Transparent: 2 /* Transparent */,
};
//Override top circle tint color with background color
TNSFancyAlert.tintTopCircle = true;
//Make the top circle icon larger
TNSFancyAlert.useLargerIcon = false;
//# sourceMappingURL=index.ios.js.map