br.com.jarch.faces.util.JavaScriptUtils Maven / Gradle / Ivy
package br.com.jarch.faces.util;
import br.com.jarch.core.util.BundleUtils;
import br.com.jarch.core.util.ErrorUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.util.StringUtils;
import org.primefaces.PrimeFaces;
public final class JavaScriptUtils {
private JavaScriptUtils() {
}
public static void execute(String javaScript) {
// RequestContext.getCurrentInstance().execute(javaScript);
PrimeFaces.current().executeScript(javaScript);
}
public static void hideMessageHeader() {
execute("$('.toast').hide('slow', function() { $(this).remove(); });");
}
public static void showMessageHeaderError(String message) {
showMessageHeaderError("", message);
}
public static void showMessageHeaderError(String title, String message) {
toastr("error", title, message);
}
public static void showMessageHeaderWarning(String message) {
showMessageHeaderWarning("", message);
}
public static void showMessageHeaderWarning(String title, String message) {
toastr("warning", title, message);
}
public static void showMessageHeaderInformation(String message) {
showMessageHeaderInformation("", message);
}
public static void showMessageHeaderInformation(String title, String message) {
toastr("info", title, message);
}
public static void showMessageHeaderSuccess(String message) {
showMessageHeaderSuccess("", message);
}
public static void showMessageHeaderSuccess(String title, String message) {
toastr("success", title, message);
}
public static void showMessageHeaderError(Exception ex) {
showMessageHeaderError(String.join("
", ErrorUtils.messagesErro(ex)));
}
public static void showMessageBodySuccessNoRedirect() {
showMessageBodySuccessNoRedirect(BundleUtils.messageBundle("message.procedimentoRealizadoSucesso"), "");
}
private static void toastr(String method, String title, String message) {
hideMessageHeader();
String titleNew = removeCharEspecial(title);
String messageNew = removeCharEspecial(message);
if (!messageNew.startsWith("") && StringUtils.count(messageNew, ":") == 1)
messageNew = "" + messageNew.replace(":", ":");
String javaScript = "toastr." + method + "('" + messageNew + "' , '" + titleNew + "');";
LogUtils.generate(javaScript);
execute(javaScript);
hideMessageProcess(true);
}
private static String removeCharEspecial(String title) {
return title.replace("'", "\"").replace("\n", "").replace("\r", "");
}
public static void showMessageBodySuccessNoRedirect(String title) {
showMessageBodySuccessNoRedirect(title, "");
}
public static void showMessageBodySuccessNoRedirect(String title, String message) {
if (StringUtils.isNullOrEmpty(title))
title = BundleUtils.messageBundle("message.procedimentoRealizadoSucesso");
if (message == null)
message = "";
String js = "showMessageBodySuccessNoRedirect(" +
"'" + title.replace("''", "\"").replace("'", "\"") + "', " +
"'" + message.replace("''", "\"").replace("'", "\"") + "', " +
"'" + BundleUtils.messageBundle("label.cancelado") + "', " +
"'" + BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodySuccessRedirect(String title, String pageReturn) {
showMessageBodySuccessRedirect(title, "", pageReturn);
}
public static void showMessageBodySuccessRedirect(String title, String message, String pageReturn) {
if (title == null)
title = BundleUtils.messageBundle("message.procedimentoRealizadoSucesso");
if (message == null)
message = "";
String js = "showMessageBodySuccessRedirect(" +
"'" + pageReturn + "', " +
"'" + title.replace("''", "\"").replace("'", "\"") + "', " +
"'" + message.replace("''", "\"").replace("'", "\"") + "', " +
"'" + BundleUtils.messageBundle("label.cancelado") + "', " +
"'" + BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodySuccessRedirect(String pageReturn) {
showMessageBodySuccessRedirect(BundleUtils.messageBundle("message.procedimentoRealizadoSucesso"), pageReturn);
}
public static void showMessageBodyWarningNoRedirect(String message) {
// message = removeHtmlTags(message);
String js = "showMessageBodyWarningNoRedirect('" + message + "', " + "'"
+ BundleUtils.messageBundle("message.procedimentoRealizadoAdvertencia") + "', " + "'"
+ BundleUtils.messageBundle("label.cancelado") + "', " + "'"
+ BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodyWarningNoRedirect(String title, String message) {
String js = "showMessageBodyWarningNoRedirect('"
+ title.replace("''", "\"").replace("'", "\"") + "', " + "'"
+ message.replace("''", "\"").replace("'", "\"") + "', " + "'"
+ BundleUtils.messageBundle("label.cancelado") + "', " + "'"
+ BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodyWarningWideNoRedirect(String title, String message) {
String js = "showMessageBodyWarningWideNoRedirect('"
+ title.replace("''", "\"").replace("'", "\"") + "', " + "'"
+ message.replace("''", "\"").replace("'", "\"") + "', " + "'"
+ BundleUtils.messageBundle("label.cancelado") + "', " + "'"
+ BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodyWarningRedirect(String pageReturn) {
String js = "showMessageBodyWarningRedirect('" + pageReturn + "', '" + BundleUtils.messageBundle("message.procedimentoAdvertencia")
+ "', " + "'" + BundleUtils.messageBundle("message.procedimentoRealizadoAdvertencia") + "', " + "'"
+ BundleUtils.messageBundle("label.cancelado") + "', " + "'"
+ BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodyWarningWideRedirect(String pageReturn) {
String js = "showMessageBodyWarningWideRedirect('" + pageReturn + "', '" + BundleUtils.messageBundle("message.procedimentoAdvertencia")
+ "', " + "'" + BundleUtils.messageBundle("message.procedimentoRealizadoAdvertencia") + "', " + "'"
+ BundleUtils.messageBundle("label.cancelado") + "', " + "'"
+ BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodyErrorNoRedirect(String messageErro) {
// messageErro = removeHtmlTags(messageErro);
String js = "showMessageBodyErrorNoRedirect('" + messageErro + "', " +
"'" + BundleUtils.messageBundle("message.procedimentoRealizadoErro") + "', " +
"'" + BundleUtils.messageBundle("label.cancelado") + "', " +
"'" + BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void showMessageBodyErrorRedirect(String pageReturn) {
showMessageBodyErrorRedirect(pageReturn,
BundleUtils.messageBundle("message.procedimentoErro"),
BundleUtils.messageBundle("message.procedimentoRealizadoErro"));
}
public static void showMessageBodyErrorRedirect(String pageReturn, String title, String message) {
// title = removeHtmlTags(title);
// message = removeHtmlTags(message);
String js = "showMessageBodyErrorRedirect('" + pageReturn + "', '" +
title.replace("''", "\"").replace("'", "\"") + "', " + "'"
+ message.replace("''", "\"").replace("'", "\"") + "', " + "'"
+ BundleUtils.messageBundle("label.cancelado") + "', " + "'"
+ BundleUtils.messageBundle("message.voltaParaSistema") + "')";
execute(js);
}
public static void hideMessageProcess(boolean closePopup) {
execute("hideMessageProcess(" + closePopup + ");");
}
public static void showMessageProcess(boolean hideButton) {
execute("showMessageProcess(" + hideButton + ", '" + BundleUtils.messageBundle("label.aguardeProcessando") + "');");
}
public static void showMessageProcessReturnLastPage() {
execute("showMessageProcessReturnLastPage();");
}
public static void focus(String idComponent) {
// FacesContext context = FacesContext.getCurrentInstance();
PrimeFaces.current().focus(idComponent);
// UIComponent forComponent = SearchExpressionUtils.resolveComponent(
// context,
// UIComponent.getCurrentComponent(context),
// idComponent
// );
//
// String clientId = forComponent.getClientId();
// PrimeFaces.current().executeScript("PrimeFaces.focus('" + clientId + "');");
}
// private static String removeHtmlTags(String value) {
// return CharacterUtils.removeHtmlTags(value);
// }
public static void removeQueryParamsUrl() {
execute("" +
"let urlSemParam = window.location.href.replace(window.location.search,'');" +
"window.history.replaceState({}, document.title, urlSemParam);");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy