Improved: Open Screen file from browser (OFBIZ-12038)

Refactoring
This commit is contained in:
James Yong
2020-10-10 18:06:14 +08:00
parent 0088af8b90
commit cbbcb14119
3 changed files with 53 additions and 36 deletions
@@ -168,12 +168,10 @@ public class HtmlWidget extends ModelScreenWidget {
if (insertWidgetBoundaryComments) {
writer.append(HtmlWidgetRenderer.buildBoundaryComment("Begin", "Template", location));
}
HttpServletRequest request = ((HttpServletRequest) context.get("request"));
if (!location.endsWith(".fo.ftl") && request != null) {
String contextPath = request.getContextPath();
writer.append(HtmlWidgetRenderer.beginNamedBorder("Template", location, contextPath));
if (!location.endsWith(".fo.ftl") && HtmlWidgetRenderer.NAMED_BORDER_TYPE != ModelWidget.NamedBorderType.NONE) {
HttpServletRequest request = ((HttpServletRequest) context.get("request"));
writer.append(HtmlWidgetRenderer.beginNamedBorder("Template", location, request.getContextPath()));
}
Template template = null;
if (location.endsWith(".fo.ftl")) { // FOP can't render correctly escaped characters
template = FreeMarkerWorker.getTemplate(location);
@@ -182,7 +180,7 @@ public class HtmlWidget extends ModelScreenWidget {
}
FreeMarkerWorker.renderTemplate(template, context, writer);
if (!location.endsWith(".fo.ftl") && request != null) {
if (!location.endsWith(".fo.ftl") && HtmlWidgetRenderer.NAMED_BORDER_TYPE != ModelWidget.NamedBorderType.NONE) {
writer.append(HtmlWidgetRenderer.endNamedBorder("Template", location));
}
if (insertWidgetBoundaryComments) {
@@ -39,6 +39,11 @@ public class HtmlWidgetRenderer {
*/
public static final String WHITE_SPACE = "\r\n";
/**
* Store property value of widget.dev.namedBorder
*/
public static final ModelWidget.NamedBorderType NAMED_BORDER_TYPE = ModelWidget.widgetNamedBorderType();
/**
* Sets widget comments enabled.
* @param widgetCommentsEnabled the widget comments enabled
@@ -49,8 +54,6 @@ public class HtmlWidgetRenderer {
private boolean widgetCommentsEnabled = false;
private static ModelWidget.NamedBorderType namedBorderType = ModelWidget.widgetNamedBorderType();
/**
* Is widget comments enabled boolean.
* @return the boolean
@@ -77,37 +80,48 @@ public class HtmlWidgetRenderer {
return "<!-- " + boundaryType + " " + widgetType + " " + widgetName + " -->" + WHITE_SPACE;
}
/**
* Always check the following condition is true before running the method:
* HtmlWidgetRenderer.namedBorderType != ModelWidget.NamedBorderType.NONE
* @param widgetType
* @param location
* @param contextPath
* @return
*/
public static String beginNamedBorder(String widgetType, String location, String contextPath) {
if (namedBorderType != ModelWidget.NamedBorderType.NONE) {
List<String> themeBasePathsToExempt = UtilHtml.getVisualThemeFolderNamesToExempt();
if (!themeBasePathsToExempt.stream().anyMatch(location::contains)) {
String fileName = location.substring(location.lastIndexOf("/") + 1);
switch (namedBorderType) {
case SOURCE:
return "<div class='info-container'><span class='info-overlay-item info-cursor-none' data-source='"
+ location + "' data-target='" + contextPath
+ (SeoConfigUtil.isCategoryUrlEnabled(contextPath) ? "" : "/control")
+ "/openSourceFile'>"
+ fileName
+ "</span>";
case LABEL:
return "<div class='info-container'><span class='info-overlay-item'>"
+ fileName
+ "</span>";
default:
return "";
}
List<String> themeBasePathsToExempt = UtilHtml.getVisualThemeFolderNamesToExempt();
if (!themeBasePathsToExempt.stream().anyMatch(location::contains)) {
String fileName = location.substring(location.lastIndexOf("/") + 1);
switch (NAMED_BORDER_TYPE) {
case SOURCE:
return "<div class='info-container'><span class='info-overlay-item info-cursor-none' data-source='"
+ location + "' data-target='" + contextPath
+ (SeoConfigUtil.isCategoryUrlEnabled(contextPath) ? "" : "/control")
+ "/openSourceFile'>"
+ fileName
+ "</span>";
case LABEL:
return "<div class='info-container'><span class='info-overlay-item'>"
+ fileName
+ "</span>";
default:
return "";
}
}
return "";
}
/**
* Always check the following condition is true before running the method:
* HtmlWidgetRenderer.namedBorderType != ModelWidget.NamedBorderType.NONE
* @param widgetType
* @param location
* @return
*/
public static String endNamedBorder(String widgetType, String location) {
if (namedBorderType != ModelWidget.NamedBorderType.NONE) {
List<String> themeBasePathsToExempt = UtilHtml.getVisualThemeFolderNamesToExempt();
if (!themeBasePathsToExempt.stream().anyMatch(location::contains)) {
return "</div>";
}
List<String> themeBasePathsToExempt = UtilHtml.getVisualThemeFolderNamesToExempt();
if (!themeBasePathsToExempt.stream().anyMatch(location::contains)) {
return "</div>";
}
return "";
}
@@ -36,6 +36,7 @@ import org.apache.ofbiz.base.util.collections.MapStack;
import org.apache.ofbiz.webapp.view.AbstractViewHandler;
import org.apache.ofbiz.webapp.view.ViewHandlerException;
import org.apache.ofbiz.widget.model.ModelTheme;
import org.apache.ofbiz.widget.model.ModelWidget;
import org.apache.ofbiz.widget.renderer.FormStringRenderer;
import org.apache.ofbiz.widget.renderer.MenuStringRenderer;
import org.apache.ofbiz.widget.renderer.ScreenRenderer;
@@ -114,11 +115,15 @@ public class MacroScreenViewHandler extends AbstractViewHandler {
context.put("screens", screens);
context.put("simpleEncoder", UtilCodec.getEncoder(visualTheme.getModelTheme().getEncoder(getName())));
screenStringRenderer.renderScreenBegin(writer, context);
// render start of named border for screen
writer.append(HtmlWidgetRenderer.beginNamedBorder("Screen", page, request.getContextPath()));
if (HtmlWidgetRenderer.NAMED_BORDER_TYPE != ModelWidget.NamedBorderType.NONE) {
// render start of named border for screen
writer.append(HtmlWidgetRenderer.beginNamedBorder("Screen", page, request.getContextPath()));
}
screens.render(page);
// render end of named border for screen
writer.append(HtmlWidgetRenderer.endNamedBorder("Screen", page));
if (HtmlWidgetRenderer.NAMED_BORDER_TYPE != ModelWidget.NamedBorderType.NONE) {
// render end of named border for screen
writer.append(HtmlWidgetRenderer.endNamedBorder("Screen", page));
}
screenStringRenderer.renderScreenEnd(writer, context);
writer.flush();
} catch (TemplateException e) {