From 51cee8e0fb17c0b396e89a1861c7f8622a45e857 Mon Sep 17 00:00:00 2001 From: datagear Date: Thu, 27 Feb 2020 14:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=AB=AF=E7=9C=8B=E6=9D=BF?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E6=96=B9=E5=BC=8F=E7=94=B1=E2=80=9Cdashboard?= =?UTF-8?q?Renderer.render(dashboard)=E2=80=9D=E6=94=B9=E4=B8=BA=E2=80=9Cd?= =?UTF-8?q?ashboardFactory.init(dashboard);dashboard.render();=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HtmlTplDashboardWidgetFmkRenderer.java | 9 +- .../HtmlTplDashboardWidgetHtmlRenderer.java | 38 +++--- .../html/HtmlTplDashboardWidgetRenderer.java | 126 ++++++++++++------ ...tmlTplDashboardWidgetHtmlRendererTest.java | 34 +++-- .../web/datagear-applicationContext.xml | 12 +- .../static/script/datagear-chartFactory.js | 4 +- 6 files changed, 136 insertions(+), 87 deletions(-) diff --git a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetFmkRenderer.java b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetFmkRenderer.java index d402608a..b7f97ee0 100644 --- a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetFmkRenderer.java +++ b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetFmkRenderer.java @@ -52,7 +52,7 @@ import freemarker.template.TemplateScalarModel; * ... * <@theme /> * ... - * <@dashboard var="..." renderer="..."> + * <@dashboard var="..." factory="..."> * ... * <@chart widget="..." var="..." elementId="..." /> * ... @@ -68,7 +68,7 @@ import freemarker.template.TemplateScalarModel; * <@theme />:引入内置CSS主题样式。 *

*

- * <@dashboard>:定义看板,“var”自定义看板JS变量名,可不填;“renderer”自定义看板渲染器JS变量,可不填,默认为{@linkplain HtmlTplDashboardWidgetRenderer#getDefaultDashboardRendererVar()}。 + * <@dashboard>:定义看板,“var”自定义看板JS变量名,可不填;“factory”自定义看板工厂JS变量,可不填,默认为{@linkplain HtmlTplDashboardWidgetRenderer#getDefaultDashboardFactoryVar()}。 *

*

* <@chart @@ -406,7 +406,7 @@ public class HtmlTplDashboardWidgetFmkRenderer exte throws TemplateException, IOException { String dashboardVar = getStringParamValue(params, "var"); - String rendererVar = getStringParamValue(params, "renderer"); + String factoryVar = getStringParamValue(params, "factory"); if (StringUtil.isEmpty(dashboardVar)) dashboardVar = getDefaultDashboardVar(); @@ -439,7 +439,8 @@ public class HtmlTplDashboardWidgetFmkRenderer exte HtmlChartPluginRenderOption.removeOption(renderContext); writeHtmlDashboardJSInit(out, dashboard); - writeHtmlDashboardJSRender(out, dashboard, rendererVar); + writeHtmlDashboardJSFactoryInit(out, dashboard, factoryVar); + writeHtmlDashboardJSRender(out, dashboard); writeScriptEndTag(out); writeNewLine(out); diff --git a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRenderer.java b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRenderer.java index 2ffa915e..5966d0f1 100644 --- a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRenderer.java +++ b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRenderer.java @@ -29,7 +29,7 @@ import org.datagear.util.StringUtil; * *

  * ...
- * <html dg-dashboard-renderer="..." dg-dashboard-var="..." dg-dashboard-import-exclude="...">
+ * <html dg-dashboard-factory="..." dg-dashboard-var="..." dg-dashboard-import-exclude="...">
  * ...
  * <head>
  * ...
@@ -44,7 +44,7 @@ import org.datagear.util.StringUtil;
  * 
* *

- * html dg-dashboard-renderer:选填,定义看板渲染器JS对象的变量名,默认为{@linkplain HtmlTplDashboardWidgetRenderer#getDefaultDashboardRendererVar()} + * html dg-dashboard-factory:选填,定义看板工厂JS对象的变量名,默认为{@linkplain HtmlTplDashboardWidgetRenderer#getDefaultDashboardFactoryVar()} *

*

* html dg-dashboard-var:选填,定义看板JS对象的变量名,默认为{@linkplain #getDefaultDashboardVar()} @@ -71,7 +71,7 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext public static final String DEFAULT_ATTR_NAME_DASHBOARD_VAR = "dg-dashboard-var"; - public static final String DEFAULT_ATTR_NAME_DASHBOARD_RENDERER = "dg-dashboard-renderer"; + public static final String DEFAULT_ATTR_NAME_DASHBOARD_FACTORY = "dg-dashboard-factory"; public static final String DEFAULT_ATTR_NAME_DASHBOARD_IMPORT_EXCLUDE = "dg-dashboard-import-exclude"; @@ -83,8 +83,8 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext /** 属性名:看板JS变量名 */ private String attrNameDashboardVar = DEFAULT_ATTR_NAME_DASHBOARD_VAR; - /** 属性名:看板渲染器JS变量名 */ - private String attrNameDashboardRenderer = DEFAULT_ATTR_NAME_DASHBOARD_RENDERER; + /** 属性名:看板工厂JS变量名 */ + private String attrNameDashboardFactory = DEFAULT_ATTR_NAME_DASHBOARD_FACTORY; /** 属性名:不导入内置库的 */ private String attrNameDashboardImportExclude = DEFAULT_ATTR_NAME_DASHBOARD_IMPORT_EXCLUDE; @@ -128,12 +128,12 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext public String getAttrNameDashboardRenderer() { - return attrNameDashboardRenderer; + return attrNameDashboardFactory; } public void setAttrNameDashboardRenderer(String attrNameDashboardRenderer) { - this.attrNameDashboardRenderer = attrNameDashboardRenderer; + this.attrNameDashboardFactory = attrNameDashboardRenderer; } public String getAttrNameDashboardImportExclude() @@ -445,13 +445,13 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext writeNewLine(out); writeHtmlChartScripts(renderContext, dashboard, dashboardInfo); - writeHtmlDashboardJSInit(out, dashboard); + writeHtmlDashboardJSFactoryInit(out, dashboard, dashboardInfo.getDashboardFactoryVar()); out.write("})();"); writeNewLine(out); - writeHtmlDashboardJSRender(out, dashboard, dashboardInfo.getRendererVar()); + writeHtmlDashboardJSRender(out, dashboard); writeScriptEndTag(out); writeNewLine(out); @@ -581,9 +581,9 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext { dashboardInfo.setDashboardVar(attrValueStr); } - else if (this.attrNameDashboardRenderer.equalsIgnoreCase(attrNameStr)) + else if (this.attrNameDashboardFactory.equalsIgnoreCase(attrNameStr)) { - dashboardInfo.setRendererVar(attrValueStr); + dashboardInfo.setDashboardFactoryVar(attrValueStr); } else if (this.attrNameDashboardImportExclude.equalsIgnoreCase(attrNameStr)) { @@ -813,8 +813,8 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext { /** 看板变量名称 */ private String dashboardVar; - /** 看板渲染器名称 */ - private String rendererVar; + /** 看板工厂名称 */ + private String dashboardFactoryVar; /** 内置导入排除项 */ private String importExclude; /** 图表信息 */ @@ -841,14 +841,14 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext this.dashboardVar = dashboardVar; } - public String getRendererVar() + public String getDashboardFactoryVar() { - return rendererVar; + return dashboardFactoryVar; } - public void setRendererVar(String rendererVar) + public void setDashboardFactoryVar(String dashboardFactoryVar) { - this.rendererVar = rendererVar; + this.dashboardFactoryVar = dashboardFactoryVar; } public String getImportExclude() @@ -874,8 +874,8 @@ public class HtmlTplDashboardWidgetHtmlRenderer ext @Override public String toString() { - return getClass().getSimpleName() + " [dashboardVar=" + dashboardVar + ", rendererVar=" + rendererVar - + ", importExclude=" + importExclude + ", chartInfos=" + chartInfos + "]"; + return getClass().getSimpleName() + " [dashboardVar=" + dashboardVar + ", rendererVar=" + + dashboardFactoryVar + ", importExclude=" + importExclude + ", chartInfos=" + chartInfos + "]"; } } diff --git a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java index 658d5d50..e187ddef 100644 --- a/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java +++ b/datagear-analysis/src/main/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetRenderer.java @@ -36,23 +36,21 @@ import org.datagear.util.StringUtil; /** * 抽象{@linkplain HtmlTplDashboardWidget}渲染器。 *

- * 此类的{@linkplain #writeHtmlDashboardJSRender(Writer, HtmlDashboard, String)}方法的JS看板渲染逻辑为: + * 此类的{@linkplain #writeHtmlDashboardJSFactoryInit(Writer, HtmlDashboard, String)}方法的JS看板渲染逻辑为: *

* *
- * window.onload = function(){
- *   dashboardRenderer.render(dashboard);
- * };
+ * dashboardFactory.init(dashboard);
  * 
*
*

- * 因此,看板页面应该定义如下JS看板渲染器对象: + * 因此,看板页面应该定义如下JS看板工厂对象: *

* *
- * var dashboardRenderer =
+ * var dashboardFactory =
  * {
- *   render : function(dashboard)
+ *   init : function(dashboard)
  *   {
  *     ...
  *   }
@@ -60,7 +58,7 @@ import org.datagear.util.StringUtil;
  * 
*
*

- * 子类在调用此方法时可以传入自定义JS看板渲染器对象的变量名,默认为{@linkplain #getDefaultDashboardRendererVar()}。 + * 子类在调用此方法时可以传入自定义JS看板工厂对象的变量名,默认为{@linkplain #getDefaultDashboardFactoryVar()}。 *

*

* 此类的{@linkplain #getHtmlDashboardImports()}的{@linkplain HtmlDashboardImport#getContent()}可以包含{@linkplain #getContextPathPlaceholder()}占位符, @@ -81,7 +79,7 @@ public abstract class HtmlTplDashboardWidgetRenderer * *

-	 * window.onload = function(){
-	 * dashboardRenderer.render();
-	 * };
+	 * dashboardFactory.init(dashboard);
 	 * 
* *

* * @param out * @param dashboard - * @param dashboardRendererVar - * 如果为{@code null},则使用{@linkplain #getDefaultDashboardRendererVar()} + * @param dashboardFactoryVar + * 如果为{@code null},则使用{@linkplain #getDefaultDashboardFactoryVar()} * @throws IOException */ - protected void writeHtmlDashboardJSRender(Writer out, HtmlDashboard dashboard, String dashboardRendererVar) + protected void writeHtmlDashboardJSFactoryInit(Writer out, HtmlDashboard dashboard, String dashboardFactoryVar) throws IOException { String varName = dashboard.getVarName(); @@ -824,16 +833,44 @@ public abstract class HtmlTplDashboardWidgetRenderer + * + *
+	 * if(typeof $ != "undefined")
+	 * 	$(document).ready(function(){ dashboard.render(); });
+	 * else
+	 * 	window.onload = function(){ dashboard.render(); };
+	 * 
+ *
+ *

+ * + * @param out + * @param dashboard + * @throws IOException + */ + protected void writeHtmlDashboardJSRender(Writer out, HtmlDashboard dashboard) throws IOException + { + String varName = dashboard.getVarName(); + + if (StringUtil.isEmpty(varName)) + throw new IllegalArgumentException(); + + out.write("if(typeof $ != \"undefined\")"); writeNewLine(out); - - out.write("};"); + out.write(" $(document).ready(function(){ " + varName + "." + this.dashboardRenderFuncName + "(); });"); + writeNewLine(out); + out.write("else"); + writeNewLine(out); + out.write(" window.onload = function(){ " + varName + "." + this.dashboardRenderFuncName + "(); };"); writeNewLine(out); } @@ -883,8 +920,8 @@ public abstract class HtmlTplDashboardWidgetRenderer chartWidget, - HtmlRenderContext renderContext) throws RenderException + protected HtmlChart writeHtmlChart(HtmlChartWidget chartWidget, HtmlRenderContext renderContext) + throws RenderException { return chartWidget.render(renderContext); } @@ -1077,10 +1114,14 @@ public abstract class HtmlTplDashboardWidgetRenderer'、'/'、空格、下一个属性名的第一个字符、-1 * @throws IOException */ @@ -1176,7 +1217,8 @@ public abstract class HtmlTplDashboardWidgetRenderer}”。 * * @param in - * @param out 为{@code null}则不写入 + * @param out + * 为{@code null}则不写入 * @throws IOException */ protected void skipHtmlComment(Reader in, Writer out) throws IOException diff --git a/datagear-analysis/src/test/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRendererTest.java b/datagear-analysis/src/test/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRendererTest.java index d67a3aee..2331f86f 100644 --- a/datagear-analysis/src/test/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRendererTest.java +++ b/datagear-analysis/src/test/java/org/datagear/analysis/support/html/HtmlTplDashboardWidgetHtmlRendererTest.java @@ -88,7 +88,7 @@ public class HtmlTplDashboardWidgetHtmlRendererTest // 看板属性,双引号 { - String template = ""; StringWriter out = new StringWriter(); @@ -102,19 +102,19 @@ public class HtmlTplDashboardWidgetHtmlRendererTest String html = getHtmlWithPrint(out); Assert.assertEquals("myDashboard", dashboardInfo.getDashboardVar()); - Assert.assertEquals("myRenderer", dashboardInfo.getRendererVar()); + Assert.assertEquals("myDashboardFactory", dashboardInfo.getDashboardFactoryVar()); Assert.assertEquals("jquery", dashboardInfo.getImportExclude()); Assert.assertFalse(html.contains(IMPORT_CONTENT_JQUERY)); Assert.assertTrue(html.contains(IMPORT_CONTENT_UTIL)); Assert.assertTrue(html.contains(IMPORT_CONTENT_THEME)); Assert.assertTrue(html.contains(IMPORT_CONTENT_STYLE)); Assert.assertTrue(html.contains("var myDashboard")); - Assert.assertTrue(html.contains("myRenderer.render(myDashboard);")); + Assert.assertTrue(html.contains("myDashboardFactory.init(myDashboard);")); } // 看板属性,无引号 { - String template = ""; StringWriter out = new StringWriter(); @@ -128,19 +128,19 @@ public class HtmlTplDashboardWidgetHtmlRendererTest String html = getHtmlWithPrint(out); Assert.assertEquals("myDashboard", dashboardInfo.getDashboardVar()); - Assert.assertEquals("myRenderer", dashboardInfo.getRendererVar()); + Assert.assertEquals("myDashboardFactory", dashboardInfo.getDashboardFactoryVar()); Assert.assertEquals("jquery", dashboardInfo.getImportExclude()); Assert.assertFalse(html.contains(IMPORT_CONTENT_JQUERY)); Assert.assertTrue(html.contains(IMPORT_CONTENT_UTIL)); Assert.assertTrue(html.contains(IMPORT_CONTENT_THEME)); Assert.assertTrue(html.contains(IMPORT_CONTENT_STYLE)); Assert.assertTrue(html.contains("var myDashboard")); - Assert.assertTrue(html.contains("myRenderer.render(myDashboard);")); + Assert.assertTrue(html.contains("myDashboardFactory.init(myDashboard);")); } // 看板属性,单引号 { - String template = ""; StringWriter out = new StringWriter(); @@ -154,19 +154,19 @@ public class HtmlTplDashboardWidgetHtmlRendererTest String html = getHtmlWithPrint(out); Assert.assertEquals("myDashboard", dashboardInfo.getDashboardVar()); - Assert.assertEquals("myRenderer", dashboardInfo.getRendererVar()); + Assert.assertEquals("myDashboardFactory", dashboardInfo.getDashboardFactoryVar()); Assert.assertEquals("jquery", dashboardInfo.getImportExclude()); Assert.assertFalse(html.contains(IMPORT_CONTENT_JQUERY)); Assert.assertTrue(html.contains(IMPORT_CONTENT_UTIL)); Assert.assertTrue(html.contains(IMPORT_CONTENT_THEME)); Assert.assertTrue(html.contains(IMPORT_CONTENT_STYLE)); Assert.assertTrue(html.contains("var myDashboard")); - Assert.assertTrue(html.contains("myRenderer.render(myDashboard);")); + Assert.assertTrue(html.contains("myDashboardFactory.init(myDashboard);")); } // 看板属性,多个导入排除值 { - String template = ""; StringWriter out = new StringWriter(); @@ -180,14 +180,14 @@ public class HtmlTplDashboardWidgetHtmlRendererTest String html = getHtmlWithPrint(out); Assert.assertEquals("myDashboard", dashboardInfo.getDashboardVar()); - Assert.assertEquals("myRenderer", dashboardInfo.getRendererVar()); + Assert.assertEquals("myDashboardFactory", dashboardInfo.getDashboardFactoryVar()); Assert.assertEquals("jquery,theme, style", dashboardInfo.getImportExclude()); Assert.assertFalse(html.contains(IMPORT_CONTENT_JQUERY)); Assert.assertTrue(html.contains(IMPORT_CONTENT_UTIL)); Assert.assertFalse(html.contains(IMPORT_CONTENT_THEME)); Assert.assertFalse(html.contains(IMPORT_CONTENT_STYLE)); Assert.assertTrue(html.contains("var myDashboard")); - Assert.assertTrue(html.contains("myRenderer.render(myDashboard);")); + Assert.assertTrue(html.contains("myDashboardFactory.init(myDashboard);")); } // 看板属性,默认 @@ -205,14 +205,14 @@ public class HtmlTplDashboardWidgetHtmlRendererTest String html = getHtmlWithPrint(out); Assert.assertNull(dashboardInfo.getDashboardVar()); - Assert.assertNull(dashboardInfo.getRendererVar()); + Assert.assertNull(dashboardInfo.getDashboardFactoryVar()); Assert.assertNull(dashboardInfo.getImportExclude()); Assert.assertTrue(html.contains(IMPORT_CONTENT_JQUERY)); Assert.assertTrue(html.contains(IMPORT_CONTENT_UTIL)); Assert.assertTrue(html.contains(IMPORT_CONTENT_THEME)); Assert.assertTrue(html.contains(IMPORT_CONTENT_STYLE)); Assert.assertTrue(html.contains("var dashboard")); - Assert.assertTrue(html.contains("dashboardRenderer.render(dashboard);")); + Assert.assertTrue(html.contains("dashboardFactory.init(dashboard);")); } // 图表属性 @@ -285,8 +285,7 @@ public class HtmlTplDashboardWidgetHtmlRendererTest HtmlRenderAttributes.setHtmlTitleHandler(renderContext, htmlTitleHandler); HtmlDashboard dashboard = this.renderer.createHtmlDashboard(renderContext, dashboardWidget); - this.renderer.renderHtmlDashboard(renderContext, dashboard, - IOUtil.getReader(template)); + this.renderer.renderHtmlDashboard(renderContext, dashboard, IOUtil.getReader(template)); String html = getHtmlWithPrint(out); @@ -301,8 +300,7 @@ public class HtmlTplDashboardWidgetHtmlRendererTest HtmlRenderAttributes.setHtmlTitleHandler(renderContext, htmlTitleHandler); HtmlDashboard dashboard = this.renderer.createHtmlDashboard(renderContext, dashboardWidget); - this.renderer.renderHtmlDashboard(renderContext, dashboard, - IOUtil.getReader(template)); + this.renderer.renderHtmlDashboard(renderContext, dashboard, IOUtil.getReader(template)); String html = getHtmlWithPrint(out); diff --git a/datagear-web/src/main/resources/org/datagear/web/datagear-applicationContext.xml b/datagear-web/src/main/resources/org/datagear/web/datagear-applicationContext.xml index e41714d2..b727389d 100644 --- a/datagear-web/src/main/resources/org/datagear/web/datagear-applicationContext.xml +++ b/datagear-web/src/main/resources/org/datagear/web/datagear-applicationContext.xml @@ -310,13 +310,21 @@ + + + + + + + + - + - + diff --git a/datagear-web/src/main/resources/org/datagear/web/webapp/staticResource/static/script/datagear-chartFactory.js b/datagear-web/src/main/resources/org/datagear/web/webapp/staticResource/static/script/datagear-chartFactory.js index 98d8c3c4..cf6cbc50 100644 --- a/datagear-web/src/main/resources/org/datagear/web/webapp/staticResource/static/script/datagear-chartFactory.js +++ b/datagear-web/src/main/resources/org/datagear/web/webapp/staticResource/static/script/datagear-chartFactory.js @@ -216,7 +216,7 @@ */ chartBase.chartDataSetFirst = function() { - return this.indexChartDataSet(0); + return this.chartDataSetAt(0); }; /** @@ -327,7 +327,7 @@ */ chartBase.resultFirst = function(results) { - return this.indexResult(results, 0); + return this.resultAt(results, 0); }; /**