From e761d1d7830eaa48a57adc61794ed020e432017e Mon Sep 17 00:00:00 2001
From: 吴学文 <wuxuewen@wuxuewendeMacBook-Pro.local>
Date: 星期一, 06 五月 2019 01:30:39 +0800
Subject: [PATCH] 前段框架微调整
---
WebService/src/main/java/com/java110/web/core/VueComponentElement.java | 69 ++++++++++++++++++++++++++++++++++
1 files changed, 68 insertions(+), 1 deletions(-)
diff --git a/WebService/src/main/java/com/java110/web/core/VueComponentElement.java b/WebService/src/main/java/com/java110/web/core/VueComponentElement.java
index 1ea09e4..9e5b871 100644
--- a/WebService/src/main/java/com/java110/web/core/VueComponentElement.java
+++ b/WebService/src/main/java/com/java110/web/core/VueComponentElement.java
@@ -1,5 +1,6 @@
package com.java110.web.core;
+import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.Arguments;
@@ -7,6 +8,7 @@
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Macro;
import org.thymeleaf.dom.Node;
+import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.processor.element.AbstractMarkupSubstitutionElementProcessor;
import org.thymeleaf.util.DOMUtils;
@@ -24,6 +26,8 @@
private static final String DIV_PROPERTY_COMPONENT = "data-component";
+ private static final int DEFAULT_PRECEDENCE = 1000;
+
protected VueComponentElement(String elementName) {
super(elementName);
@@ -36,6 +40,7 @@
List<Node> nodes = new ArrayList<>();
//鑾峰彇妯℃澘鍚嶇О
String componentName = element.getAttributeValue("name");
+
logger.debug("姝e湪瑙f瀽缁勪欢{}", componentName);
String html = VueComponentTemplate.findTemplateByComponentCode(componentName + "." + VueComponentTemplate.COMPONENT_HTML);
if (html == null) {
@@ -60,6 +65,9 @@
//js
String js = VueComponentTemplate.findTemplateByComponentCode(componentName + "." + VueComponentTemplate.COMPONENT_JS);
if (js != null) {
+
+ js = dealJs(js, element);
+ js = dealJsAddComponentCode(js, element);
js = "<script type=\"text/javascript\">//<![CDATA[ \n" + js + "//]]>\n</script>";
Node nodeJs = new Macro(js);
nodes.add(nodeJs);
@@ -67,6 +75,65 @@
return nodes;
+ }
+
+ /**
+ * 澶勭悊js
+ *
+ * @param element 椤甸潰鍏冪礌
+ * @param js js鏂囦欢鍐呭
+ * @return js 鏂囦欢鍐呭
+ */
+ private String dealJs(String js, Element element) {
+
+ //鍦╦s 涓娴媝ropTypes 灞炴��
+ if (!js.contains("propTypes")) {
+ return js;
+ }
+
+ //瑙f瀽propTypes淇℃伅
+ String tmpProTypes = js.substring(js.indexOf("propTypes"));
+ tmpProTypes = tmpProTypes.substring(tmpProTypes.indexOf("{") + 1, tmpProTypes.indexOf("}")).trim();
+
+ if (StringUtils.isEmpty(tmpProTypes)) {
+ return js;
+ }
+
+ String[] tmpType = tmpProTypes.split(",");
+ for (String type : tmpType) {
+ if (StringUtils.isEmpty(type) || !type.contains(":")) {
+ continue;
+ }
+ String[] types = type.split(":");
+ String attrKey = types[0].replace(" ", "").replace("\n", "");
+ if (!element.hasAttribute(attrKey)) {
+ String componentName = element.getAttributeValue("name");
+ logger.error("缁勪欢[%s]鏈厤缃粍浠跺睘鎬� %s", componentName, attrKey);
+ throw new TemplateProcessingException("缁勪欢[" + componentName + "]鏈厤缃粍浠跺睘鎬�" + attrKey);
+ }
+ String vcType = element.getAttributeValue(attrKey);
+ js = js.replace(attrKey, "'" + vcType + "'");
+
+ }
+ return js;
+ }
+
+ /**
+ * 澶勭悊js 鍙橀噺鍜� 鏂规硶閮藉姞鍏� 缁勪欢缂栫爜
+ *
+ * @param element 椤甸潰鍏冪礌
+ * @param js js鏂囦欢鍐呭
+ * @return js 鏂囦欢鍐呭
+ */
+ private String dealJsAddComponentCode(String js, Element element) {
+
+ if (!element.hasAttribute("code")) {
+ return js;
+ }
+
+ String code = element.getAttributeValue("code");
+
+ return js.replace("@vc_", code);
}
/**
@@ -82,6 +149,6 @@
@Override
public int getPrecedence() {
- return 1000;
+ return DEFAULT_PRECEDENCE;
}
}
--
Gitblit v1.8.0