From 49b5dba003ff2b03d40630982ea637df2cfccab1 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期日, 01 十二月 2019 10:45:00 +0800
Subject: [PATCH] 优化namespace 时对vc.on 做处理

---
 java110-front/src/main/java/com/java110/front/core/VcCreateProcessor.java |   78 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/java110-front/src/main/java/com/java110/front/core/VcCreateProcessor.java b/java110-front/src/main/java/com/java110/front/core/VcCreateProcessor.java
index 91c0529..3f1fee2 100644
--- a/java110-front/src/main/java/com/java110/front/core/VcCreateProcessor.java
+++ b/java110-front/src/main/java/com/java110/front/core/VcCreateProcessor.java
@@ -28,6 +28,7 @@
     private static final String TAG_NAME = "create";
     private static final String NAME = "name";
     private static final int PRECEDENCE = 300;
+    private static final String DEFAULT_NAMESPACE = "default";
 
 
     public VcCreateProcessor(String dialectPrefix) {
@@ -58,7 +59,7 @@
         //灏嗙粍寤哄悕绉板啓鍏ョ粍寤篐TML 绗竴涓爣绛句腑
         addDataComponent(elements, componentName);
 
-        htmlModel.addModel(modelFactory.parse(context.getTemplateData(), doc.body().children().toString()));
+        htmlModel.addModel(modelFactory.parse(context.getTemplateData(), dealHtmlThis(tag, doc.body().children().toString())));
 
         String css = VueComponentTemplate.findTemplateByComponentCode(componentName + "." + VueComponentTemplate.COMPONENT_CSS);
         if (css != null) {
@@ -69,16 +70,47 @@
         //js
         String js = VueComponentTemplate.findTemplateByComponentCode(componentName + "." + VueComponentTemplate.COMPONENT_JS);
         if (js != null) {
-
             js = dealJs(js, tag);
+            js = dealNameSpace(js, tag);
             js = dealJsAddComponentCode(js, tag);
             js = "<script type=\"text/javascript\" " + DIV_PROPERTY_COMPONENT + "=\"" + componentName + "\">//<![CDATA[ \n" + js + "//]]>\n</script>";
-            htmlModel.add(modelFactory.createText(js));
+            htmlModel.add(modelFactory.createText(dealHtmlJs(tag, js)));
         }
 
         logger.debug("瑙f瀽瀹屾垚缁勪欢{},{}", componentName, new Date().getTime());
         structureHandler.replaceWith(htmlModel, true);
 
+    }
+
+    private String dealHtmlThis(IProcessableElementTag tag, String html) {
+
+        if (!tag.hasAttribute("namespace")) {
+            return html;
+        }
+
+        String namespace = tag.getAttributeValue("namespace");
+
+        return html.replace("this.", namespace + "_")
+                .replaceAll("(id)+( )*+=+( )*+'", "id='" + namespace + "_")
+                .replaceAll("(id)+( )*+=+( )*+\"", "id=\"" + namespace + "_");
+    }
+
+    private String dealHtmlJs(IProcessableElementTag tag, String js) {
+
+        if (!tag.hasAttribute("namespace")) {
+            return js;
+        }
+
+        String namespace = tag.getAttributeValue("namespace");
+
+        js = js.replace("this.", "vc.component." + namespace + "_")
+                .replaceAll("(\\$)+( )*+(\\()+( )*+\'+#", "\\$('#" + namespace + "_")
+                .replaceAll("(\\$)+( )*+(\\()+( )*+\"+#", "\\$(\"#" + namespace + "_");
+
+        //灏� 鐩戝惉涔熷仛浼樺寲
+        js = js.replaceAll("(vc.on)+\\('", "vc.on('" + namespace + ",");
+
+        return js;
     }
 
 
@@ -189,4 +221,44 @@
 
         return js.replace("@vc_", code);
     }
+
+    /**
+     * 澶勭悊namespace
+     *
+     * @param js
+     * @param tag
+     * @return
+     */
+    private String dealNameSpace(String js, IProcessableElementTag tag) {
+
+        //鍦╦s 涓娴媝ropTypes 灞炴��
+        if (!js.contains("vc.extends")) {
+            return js;
+        }
+        String namespace = "";
+        if (!tag.hasAttribute("namespace")) {
+            namespace = DEFAULT_NAMESPACE;
+        } else {
+            namespace = tag.getAttributeValue("namespace");
+        }
+
+        //js瀵硅薄涓彃鍏amespace 鍊�
+        int extPos = js.indexOf("vc.extends");
+        String tmpProTypes = js.substring(extPos);
+        int pos = tmpProTypes.indexOf("{") + 1;
+        js = js.substring(0, extPos) + tmpProTypes.substring(0, pos).trim()
+                + "\nnamespace:'" + namespace.trim() + "',\n" + tmpProTypes.substring(pos, tmpProTypes.length());
+        int position = js.indexOf("{");
+        String propsJs = "\n$namespace='" + namespace.trim() + "';\n";
+        js = new StringBuffer(js).insert(position + 1, propsJs).toString();
+        return js;
+    }
+
+    public static void main(String[] args) {
+        String js = "123$ ( '#av').val()";
+        js = js.replaceAll("(\\$)+( )*+(\\()+( )*+\'+#", "\\$('#abg_");
+
+        System.out.println(js);
+
+    }
 }

--
Gitblit v1.8.0