Example #1
0
  public static String mySlugify(String string, Boolean lowercase) {
    string = JavaExtensions.noAccents(string);
    // Apostrophes.
    string = string.replaceAll("([a-z])'s([^a-z])", "$1s$2");
    string = string.replaceAll("[ ]", "-").replaceAll("-{2,}", "-");
    // Get rid of any - at the start and end.
    string.replaceAll("-+$", "").replaceAll("^-+", "");

    return (lowercase ? string.toLowerCase() : string);
  }
Example #2
0
 /**
  * Generates a html link to a controller action
  *
  * @param args tag attributes
  * @param body tag inner body
  * @param out the output writer
  * @param template enclosing template
  * @param fromLine template line number where the tag is defined
  */
 public static void _a(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   ActionDefinition actionDef = (ActionDefinition) args.get("arg");
   if (actionDef == null) {
     actionDef = (ActionDefinition) args.get("action");
   }
   if (!("GET".equals(actionDef.method))) {
     if (!("POST".equals(actionDef.method))) {
       String separator = actionDef.url.indexOf('?') != -1 ? "&" : "?";
       actionDef.url += separator + "x-http-method-override=" + actionDef.method;
       actionDef.method = "POST";
     }
     String id = Codec.UUID();
     out.print(
         "<form method=\"POST\" id=\""
             + id
             + "\" "
             + (args.containsKey("target") ? "target=\"" + args.get("target") + "\"" : "")
             + " style=\"display:none\" action=\""
             + actionDef.url
             + "\">");
     _authenticityToken(args, body, out, template, fromLine);
     out.print("</form>");
     out.print(
         "<a href=\"javascript:document.getElementById('"
             + id
             + "').submit();\" "
             + serialize(args, "href")
             + ">");
     out.print(JavaExtensions.toString(body));
     out.print("</a>");
   } else {
     out.print("<a href=\"" + actionDef.url + "\" " + serialize(args, "href") + ">");
     out.print(JavaExtensions.toString(body));
     out.print("</a>");
   }
 }
Example #3
0
 public static void _cache(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   String key = args.get("arg").toString();
   String duration = null;
   if (args.containsKey("for")) {
     duration = args.get("for").toString();
   }
   Object cached = Cache.get(key);
   if (cached != null) {
     out.print(cached);
     return;
   }
   String result = JavaExtensions.toString(body);
   Cache.set(key, result, duration);
   out.print(result);
 }
Example #4
0
 /**
  * Generates a html form element linked to a controller action
  *
  * @param args tag attributes
  * @param body tag inner body
  * @param out the output writer
  * @param template enclosing template
  * @param fromLine template line number where the tag is defined
  */
 public static void _form(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   ActionDefinition actionDef = (ActionDefinition) args.get("arg");
   if (actionDef == null) {
     actionDef = (ActionDefinition) args.get("action");
   }
   String enctype = (String) args.get("enctype");
   if (enctype == null) {
     enctype = "application/x-www-form-urlencoded";
   }
   if (actionDef.star) {
     actionDef.method = "POST"; // prefer POST for form ....
   }
   if (args.containsKey("method")) {
     actionDef.method = args.get("method").toString();
   }
   String name = null;
   if (args.containsKey("name")) {
     name = args.get("name").toString();
   }
   if (!("GET".equals(actionDef.method) || "POST".equals(actionDef.method))) {
     String separator = actionDef.url.indexOf('?') != -1 ? "&" : "?";
     actionDef.url += separator + "x-http-method-override=" + actionDef.method.toUpperCase();
     actionDef.method = "POST";
   }
   String encoding = Http.Response.current().encoding;
   out.print(
       "<form action=\""
           + actionDef.url
           + "\" method=\""
           + actionDef.method.toLowerCase()
           + "\" accept-charset=\""
           + encoding
           + "\" enctype=\""
           + enctype
           + "\" "
           + serialize(args, "name", "action", "method", "accept-charset", "enctype")
           + (name != null ? "name=\"" + name + "\"" : "")
           + ">");
   if (!("GET".equals(actionDef.method))) {
     _authenticityToken(args, body, out, template, fromLine);
   }
   out.println(JavaExtensions.toString(body));
   out.print("</form>");
 }
Example #5
0
 public static void _get(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   Object name = args.get("arg");
   if (name == null) {
     throw new TemplateExecutionException(
         template.template,
         fromLine,
         "Specify a variable name",
         new TagInternalException("Specify a variable name"));
   }
   Object value = BaseTemplate.layoutData.get().get(name);
   if (value != null) {
     out.print(value);
   } else {
     if (body != null) {
       out.print(JavaExtensions.toString(body));
     }
   }
 }
Example #6
0
 public static void _option(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   Object value = args.get("arg");
   Object selectedValue = TagContext.parent("select").data.get("selected");
   boolean selected =
       selectedValue != null
           && value != null
           && (selectedValue.toString()).equals(value.toString());
   out.print(
       "<option value=\""
           + (value == null ? "" : value)
           + "\" "
           + (selected ? "selected=\"selected\"" : "")
           + " "
           + serialize(args, "selected", "value")
           + ">");
   out.println(JavaExtensions.toString(body));
   out.print("</option>");
 }
Example #7
0
 public static void _pag(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine)
     throws Exception {
   // Refactorizar
   String titulo = (String) args.get("titulo");
   String cab = (String) args.get("cab");
   List lista = (List) args.get("lista");
   int itemPag = (Integer) args.get("itemPag");
   int cantidadPestana = (Integer) args.get("cantidadPestana");
   int cantidadLista = lista.size();
   int cantidadPag = cantidadLista / itemPag;
   int pag = 1;
   if (args.get("pag") != null) {
     pag = (Integer) args.get("pag");
   }
   if (cantidadLista % itemPag != 0) {
     cantidadPag++;
   }
   String html = "";
   html += "<div id='pagination" + titulo + "'>";
   if (pag == 1) {
     if (cantidadPag == 1 || cantidadPag == 0) {
       html += "<div class='pagination'><ul>";
       html += "<li class='active'><a href='#' class='pagination" + titulo + "'>1</a></li>";
       html += "</ul></div>";
     } else {
       int inicioPag = 0;
       int finPag = 0;
       int inicioRango = (cantidadPestana / 2) + 1;
       int finRango = (cantidadPestana / 2) - 1;
       if (cantidadPag < cantidadPestana) {
         inicioPag = 0;
         finPag = cantidadPag;
       } else {
         if (pag > inicioRango && (pag + finRango) < cantidadPag) {
           inicioPag = pag - inicioRango;
           finPag = pag + finRango;
         } else if ((pag + finRango) >= cantidadPag) {
           inicioPag = cantidadPag - cantidadPestana;
           finPag = cantidadPag;
         } else {
           inicioPag = 0;
           finPag = cantidadPestana;
         }
       }
       html += "<div class='pagination'><ul>";
       html +=
           "<li class='active'><a href='#' class='pagination" + titulo + "' key='1'>Pri.</a></li>";
       html +=
           "<li class='active'><a href='#' class='pagination" + titulo + "' key='1'>Ant.</a></li>";
       for (int i = inicioPag; i < finPag; i++) {
         if (pag == (i + 1)) {
           html +=
               "<li class='active'><a href='#' class='pagination"
                   + titulo
                   + "' key='"
                   + (i + 1)
                   + "'>"
                   + (i + 1)
                   + "</a></li>";
         } else {
           html +=
               "<li><a href='#' class='pagination"
                   + titulo
                   + "' key='"
                   + (i + 1)
                   + "'>"
                   + (i + 1)
                   + "</a></li>";
         }
       }
       html += "<li><a href='#' class='pagination" + titulo + "' key='2'>Sig.</a></li>";
       html +=
           "<li><a href='#' class='pagination"
               + titulo
               + "' key='"
               + cantidadPag
               + "'>Ult.</a></li>";
       html += "</ul></div>";
     }
   } else if (cantidadPag == pag) {
     int inicioPag = 0;
     int finPag = 0;
     int inicioRango = (cantidadPestana / 2) + 1;
     int finRango = (cantidadPestana / 2) - 1;
     if (cantidadPag < cantidadPestana) {
       inicioPag = 0;
       finPag = cantidadPag;
     } else {
       if (pag > inicioRango && (pag + finRango) < cantidadPag) {
         inicioPag = pag - inicioRango;
         finPag = pag + finRango;
       } else if ((pag + finRango) >= cantidadPag) {
         inicioPag = cantidadPag - cantidadPestana;
         finPag = cantidadPag;
       } else {
         inicioPag = 0;
         finPag = cantidadPestana;
       }
     }
     html += "<div class='pagination'><ul>";
     html += "<li><a href='#' class='pagination" + titulo + "' key='1'>Pri.</a></li>";
     html +=
         "<li><a href='#' class='pagination" + titulo + "' key='" + (pag - 1) + "'>Ant.</a></li>";
     for (int i = inicioPag; i < finPag; i++) {
       if (pag == (i + 1)) {
         html +=
             "<li class='active'><a href='#' class='pagination"
                 + titulo
                 + "' key='"
                 + (i + 1)
                 + "'>"
                 + (i + 1)
                 + "</a></li>";
       } else {
         html +=
             "<li><a href='#' class='pagination"
                 + titulo
                 + "' key='"
                 + (i + 1)
                 + "'>"
                 + (i + 1)
                 + "</a></li>";
       }
     }
     html +=
         "<li class='active'><a href='#' class='pagination"
             + titulo
             + "' key='"
             + cantidadPag
             + "'>Sig.</a></li>";
     html +=
         "<li class='active'><a href='#' class='pagination"
             + titulo
             + "' key='"
             + cantidadPag
             + "'>Ult.</a></li>";
     html += "</ul></div>";
   } else {
     int inicioPag = 0;
     int finPag = 0;
     int inicioRango = (cantidadPestana / 2) + 1;
     int finRango = (cantidadPestana / 2) - 1;
     if (cantidadPag < cantidadPestana) {
       inicioPag = 0;
       finPag = cantidadPag;
     } else {
       if (pag > inicioRango && (pag + finRango) < cantidadPag) {
         inicioPag = pag - inicioRango;
         finPag = pag + finRango;
       } else if ((pag + finRango) >= cantidadPag) {
         inicioPag = cantidadPag - cantidadPestana;
         finPag = cantidadPag;
       } else {
         inicioPag = 0;
         finPag = cantidadPestana;
       }
     }
     html += "<div class='pagination'><ul>";
     html += "<li><a href='#' class='pagination" + titulo + "' key='1'>Pri.</a></li>";
     html +=
         "<li><a href='#' class='pagination" + titulo + "' key='" + (pag - 1) + "'>Ant.</a></li>";
     for (int i = inicioPag; i < finPag; i++) {
       if (pag == (i + 1)) {
         html +=
             "<li class='active'><a href='#' class='pagination"
                 + titulo
                 + "' key='"
                 + (i + 1)
                 + "'>"
                 + (i + 1)
                 + "</a></li>";
       } else {
         html +=
             "<li><a href='#' class='pagination"
                 + titulo
                 + "' key='"
                 + (i + 1)
                 + "'>"
                 + (i + 1)
                 + "</a></li>";
       }
     }
     html +=
         "<li><a href='#' class='pagination" + titulo + "' key='" + (pag + 1) + "'>Sig.</a></li>";
     html +=
         "<li><a href='#' class='pagination"
             + titulo
             + "' key='"
             + cantidadPag
             + "'>Ult.</a></li>";
     html += "</ul></div>";
   }
   html += "<table class='table table-striped table-bordered table-condensed'><thead><tr>";
   String[] listCab = cab.split(",");
   for (String c : listCab) {
     html += "<th>" + c + "</th>";
   }
   html += "</tr></thead>";
   html += "<tbody>";
   int inicioLista = ((pag - 1) * itemPag);
   int finLista = (pag * itemPag);
   if (finLista > cantidadLista) {
     finLista = cantidadLista;
   }
   for (int i = inicioLista; i < finLista; i++) {
     Object object = lista.get(i);
     body.setProperty("item", object);
     html += JavaExtensions.toString(body);
   }
   html += "</tbody></table>";
   out.println(html);
   out.println("<script type='text/javascript'>");
   out.println("var Paginacion" + titulo + " = {");
   out.println("init : function(){");
   out.println("$('.pagination" + titulo + "').click(Paginacion" + titulo + ".paginacion);");
   out.println("},");
   out.println("paginacion : function(e){");
   out.println("e.preventDefault();");
   out.println("if(!$(this).parents('li').hasClass('active')){");
   out.println(
       "$('#pagination" + titulo + "').load(rutaPaginacion" + titulo + "+'?azar='+Math.random(),");
   out.println("{pag : $(this).attr('key')});");
   out.println("}}}");
   out.println("$(function(){");
   out.println("Paginacion" + titulo + ".init();");
   out.println("});");
   out.println("</script>");
   out.println("</div>");
 }
Example #8
0
 public static void _verbatim(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   out.println(JavaExtensions.toString(body));
 }