Skip to content

Commit ec4debd

Browse files
committed
directives and filters added
1 parent 01b4cf2 commit ec4debd

File tree

14 files changed

+391
-13
lines changed

14 files changed

+391
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.jeemv.springboot.vuejs</groupId>
88
<artifactId>springboot-vuejs</artifactId>
9-
<version>1.0.5</version>
9+
<version>1.0.6</version>
1010

1111
<name>springboot-vuejs</name>
1212
<url>https://github.com/jeeMv/SpringBoot-VueJS</url>

src/main/java/io/github/jeemv/springboot/vuejs/AbstractVueJS.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import io.github.jeemv.springboot.vuejs.beans.RawObject;
77
import io.github.jeemv.springboot.vuejs.parts.VueComputeds;
88
import io.github.jeemv.springboot.vuejs.parts.VueData;
9+
import io.github.jeemv.springboot.vuejs.parts.VueDirective;
10+
import io.github.jeemv.springboot.vuejs.parts.VueDirectives;
11+
import io.github.jeemv.springboot.vuejs.parts.VueFilter;
12+
import io.github.jeemv.springboot.vuejs.parts.VueFilters;
913
import io.github.jeemv.springboot.vuejs.parts.VueHook;
1014
import io.github.jeemv.springboot.vuejs.parts.VueMethods;
1115
import io.github.jeemv.springboot.vuejs.parts.VueWatcher;
@@ -16,26 +20,20 @@ public abstract class AbstractVueJS {
1620
protected VueMethods methods;
1721
protected VueComputeds computed;
1822
protected VueWatchers watchers;
23+
protected VueDirectives directives;
24+
protected VueFilters filters;
1925
protected Map<String, VueHook> hooks;
2026

2127
public AbstractVueJS() {
2228
data=new VueData();
2329
methods=new VueMethods();
2430
computed=new VueComputeds();
2531
watchers=new VueWatchers();
32+
directives=new VueDirectives();
33+
filters=new VueFilters();
2634
hooks=new HashMap<>();
2735
}
2836

29-
protected String wrapScript(String script) {
30-
if(script==null || "".equals(script)) {
31-
return "";
32-
}
33-
if(!script.startsWith("<script>")) {
34-
script="<script>"+script+"</script>";
35-
}
36-
return script;
37-
}
38-
3937
/**
4038
* Adds a data
4139
* @param key the name of the data
@@ -212,6 +210,26 @@ protected void addHook(String name,String body) {
212210
hooks.put(name, new VueHook(body));
213211
}
214212

213+
/**
214+
* Adds a new directive in the vue instance
215+
* @param name The directive name
216+
* @return The created directive
217+
*/
218+
public VueDirective addDirective(String name) {
219+
return directives.add(name);
220+
}
221+
222+
/**
223+
* Adds a new filter in the vue instance
224+
* @param name The filter name
225+
* @param body The method body
226+
* @param args The filter arguments
227+
* @return The created filter
228+
*/
229+
public VueFilter addFilter(String name,String body,String...args) {
230+
return filters.add(name,body,args);
231+
}
232+
215233
/**
216234
* Adds code (body) for the beforeCreate hook
217235
* @param body the code to execute
@@ -298,6 +316,10 @@ public VueComputeds getComputed() {
298316
return computed;
299317
}
300318

319+
public VueDirectives getDirectives() {
320+
return directives;
321+
}
322+
301323
@Override
302324
public String toString() {
303325
return getScript();
@@ -306,4 +328,10 @@ public String toString() {
306328
public VueWatchers getWatchers() {
307329
return watchers;
308330
}
331+
332+
333+
public VueFilters getFilters() {
334+
return filters;
335+
}
336+
309337
}

src/main/java/io/github/jeemv/springboot/vuejs/VueJS.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
import com.fasterxml.jackson.core.JsonProcessingException;
77

88
import io.github.jeemv.springboot.vuejs.components.VueComponent;
9+
import io.github.jeemv.springboot.vuejs.parts.AbstractVueComposition;
10+
import io.github.jeemv.springboot.vuejs.parts.VueDirective;
11+
import io.github.jeemv.springboot.vuejs.parts.VueFilter;
912
import io.github.jeemv.springboot.vuejs.utilities.JsUtils;
1013

1114
/**
1215
* VueJS instance
1316
* @author jcheron
14-
* @version 1.0.0.2
17+
* @version 1.0.0.3
1518
*/
1619
public class VueJS extends AbstractVueJS{
1720
protected String el;
1821
protected String[] delimiters;
1922
protected Map<String,VueComponent> globalComponents;
23+
protected Map<String,AbstractVueComposition> globalElements;
2024

2125
/**
2226
* @param element the DOM selector for the VueJS application
@@ -26,6 +30,7 @@ public VueJS(String element) {
2630
this.el=element;
2731
this.setDelimiters("<%", "%>");
2832
globalComponents=new HashMap<>();
33+
globalElements=new HashMap<>();
2934
}
3035

3136
/**
@@ -48,6 +53,9 @@ public String getScript() {
4853
for(Entry<String, VueComponent> entry:globalComponents.entrySet()) {
4954
script+=entry.getValue();
5055
}
56+
for(Entry<String, AbstractVueComposition> entry:globalElements.entrySet()) {
57+
script+=entry.getValue().getScript();
58+
}
5159
script+="new Vue("+JsUtils.objectToJSON(this)+");";
5260
return "<script>"+script+"</script>";
5361
} catch (JsonProcessingException e) {
@@ -56,12 +64,41 @@ public String getScript() {
5664
return script;
5765
}
5866

67+
/**
68+
* Adds a new global component
69+
* @param name The component name
70+
* @return The created component
71+
*/
5972
public VueComponent addGlobalComponent(String name) {
6073
VueComponent component= new VueComponent(name);
6174
globalComponents.put(name,component);
6275
return component;
6376
}
6477

78+
/**
79+
* Adds a new global directive
80+
* @param name The directive name
81+
* @return The created directive
82+
*/
83+
public VueDirective addGlobalDirective(String name) {
84+
VueDirective directive=new VueDirective(name);
85+
globalElements.put(name, directive);
86+
return directive;
87+
}
88+
89+
/**
90+
* Adds a new global filter
91+
* @param name The filter name
92+
* @param body The filter body
93+
* @param args The filter arguments
94+
* @return The created filter
95+
*/
96+
public VueFilter addGlobalFilter(String name,String body,String...args) {
97+
VueFilter filter=new VueFilter(name,body,args);
98+
globalElements.put(name, filter);
99+
return filter;
100+
}
101+
65102
public String getEl() {
66103
return el;
67104
}

src/main/java/io/github/jeemv/springboot/vuejs/components/VueComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public String getScript() {
5454
if(name!=null && !"".equals(name)) {
5555
script="Vue.component('"+name+"',"+JsUtils.objectToJSON(this)+");";
5656
if(!internal)
57-
script= wrapScript(script);
57+
script= JsUtils.wrapScript(script);
5858
}else {
5959
script=JsUtils.objectToJSON(this);
6060
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.jeemv.springboot.vuejs.parts;
2+
3+
import io.github.jeemv.springboot.vuejs.utilities.JsUtils;
4+
5+
public abstract class AbstractVueComposition {
6+
protected String name;
7+
protected boolean internal;
8+
9+
public AbstractVueComposition() {
10+
this(null,true);
11+
}
12+
13+
public AbstractVueComposition(String name) {
14+
this(name,true);
15+
}
16+
17+
public AbstractVueComposition(String name,boolean internal) {
18+
this.name=name;
19+
this.internal=internal;
20+
}
21+
22+
public String getScript() {
23+
String script="";
24+
if(name!=null && !"".equals(name)) {
25+
script="Vue."+getType()+"('"+name+"',"+this.toString()+");";
26+
if(!internal)
27+
script= JsUtils.wrapScript(script);
28+
}else {
29+
script=this.toString();
30+
}
31+
return script;
32+
}
33+
34+
public abstract String getType();
35+
36+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.github.jeemv.springboot.vuejs.parts;
2+
3+
public class VueDirective extends AbstractVueComposition{
4+
protected VueMethods methods;
5+
6+
public VueDirective() {
7+
super();
8+
methods=new VueMethods();
9+
}
10+
11+
public VueDirective(String name) {
12+
super(name);
13+
methods=new VueMethods();
14+
}
15+
16+
/**
17+
* called only once, when the directive is first bound to the element. This is where you can do one-time setup work.
18+
* arguments of the method are el, binding and vnode
19+
* @param body The javascript code associated
20+
* @return the directive
21+
*/
22+
public VueDirective onBind(String body) {
23+
return on("bind", body);
24+
}
25+
26+
/**
27+
* called after the containing component’s VNode has updated, but possibly before its children have updated.
28+
* arguments of the method are el, binding, vnode and oldVnode
29+
* @param body The javascript code associated
30+
* @return The directive
31+
*/
32+
public VueDirective onUpdate(String body) {
33+
methods.add("update", body, "el", "binding", "vnode","oldVnode");
34+
return this;
35+
}
36+
37+
/**
38+
* called after the containing component’s VNode and the VNodes of its children have updated.
39+
* arguments of the method are el, binding, vnode and oldVnode
40+
* @param body The javascript code associated
41+
* @return The directive
42+
*/
43+
public VueDirective onComponentUpdated(String body) {
44+
methods.add("componentUpdated", body, "el", "binding", "vnode","oldVnode");
45+
return this;
46+
}
47+
48+
/**
49+
* called when the bound element has been inserted into its parent node (this only guarantees parent node presence, not necessarily in-document).
50+
* arguments of the method are el, binding and vnode
51+
* @param body The javascript code associated
52+
* @return The directive
53+
*/
54+
public VueDirective onInserted(String body) {
55+
return on("inserted", body);
56+
}
57+
58+
59+
/**
60+
* called only once, when the directive is unbound from the element.
61+
* arguments of the method are el, binding and vnode
62+
* @param body The javascript code associated
63+
* @return The directive
64+
*/
65+
public VueDirective onUnbind(String body) {
66+
return on("unbind", body);
67+
}
68+
69+
protected VueDirective on(String event,String body) {
70+
methods.add(event, body, "el", "binding", "vnode");
71+
return this;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return methods.toString();
77+
}
78+
79+
@Override
80+
public String getType() {
81+
return "directive";
82+
}
83+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.jeemv.springboot.vuejs.parts;
2+
3+
4+
public class VueDirectives extends VuePart {
5+
public VueDirective add(String name) {
6+
VueDirective directive=new VueDirective();
7+
elements.put(name,directive);
8+
return directive;
9+
}
10+
@Override
11+
public String toString() {
12+
String datas=super.toString();
13+
if(datas!=null) {
14+
return datas;
15+
}
16+
return "";
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.jeemv.springboot.vuejs.parts;
2+
3+
public class VueFilter extends AbstractVueComposition{
4+
protected VueMethod method;
5+
6+
public VueFilter(String body,String...args) {
7+
super();
8+
method=new VueMethod(body,getArgs(args));
9+
}
10+
11+
public VueFilter(String name,String body,String...args) {
12+
super(name);
13+
method=new VueMethod(body,getArgs(args));
14+
}
15+
16+
17+
private String[] getArgs(String...args) {
18+
String[] dest = new String[args.length + 1];
19+
dest[0] = "value";
20+
System.arraycopy(args, 0, dest, 1, args.length);
21+
return dest;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return method.toString();
27+
}
28+
29+
@Override
30+
public String getType() {
31+
return "filter";
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.jeemv.springboot.vuejs.parts;
2+
3+
4+
public class VueFilters extends VuePart {
5+
6+
public VueFilter add(String name,String body,String...args) {
7+
VueFilter filter=new VueFilter(name,body,args);
8+
elements.put(name,filter);
9+
return filter;
10+
}
11+
@Override
12+
public String toString() {
13+
String datas=super.toString();
14+
if(datas!=null) {
15+
return datas;
16+
}
17+
return "";
18+
}
19+
}

0 commit comments

Comments
 (0)