1 module uim.vue;
2 
3 	@safe:
4 
5 // Standard Libraries
6 public import std.stdio;
7 public import std.string;
8 
9 // External Libraries
10 public import vibe.vibe;
11 
12 // Libraries
13 public import uim.core;
14 public import uim.oop;
15 public import uim.html;
16 
17 public import uim.javascript;
18 // public import uim.json;
19 
20 // Packages
21 public import uim.vue.apps;
22 public import uim.vue.classes;
23 public import uim.vue.content;
24 public import uim.vue.data;
25 public import uim.vue.base;
26 public import uim.vue.html;
27 public import uim.vue.helpers;
28 public import uim.vue.global;
29 public import uim.vue.routers;
30 public import uim.vue.state;
31 
32 alias string function(string, string[string]) LAYOUTFUNC;
33 /******************** 
34  * vOn - directive to listen to DOM events and run some JavaScript when they’re triggered.
35  * Params:
36  * 		h5 			= HTML5 Obj
37  * 		eventName 	= name of event like "click" or "mousemove"
38  * 		modifiers 	= "stop","prevent","capture","self","once","passive"
39  * 					for mouse action "left", "right", "middle"
40  * Examples:
41  * --------------------
42  * H5DIV.vOn("mousemove", "function") results in `<div v-on:mousemove="function"></div>
43  * -------------------- 
44  ******************** */ 
45 T vOn(T:DH5Obj)(T h5, string eventName, string value, string[] modifiers = null) {
46 	if (modifiers) return h5.attribute("@"~eventName~"."~modifiers.join("."), value);
47 	return h5.attribute("@"~eventName, value);
48 }
49 unittest {
50 	assert(H5Div.vOn("mousemove", "funcName()") == `<div @mousemove="funcName()"></div>`);
51 }
52 
53 /******************** 
54  * vKey - directive to listen to keyboard events and run some JavaScript when they’re triggered.
55  * Params:
56  * 		h5 			= HTML5 Obj
57  * 		modifiers 	= "enter", "tab", "delete", "esc", "space", "up", "down", "left", "right" or keycode
58  * 					also possible to use system keys "ctrl", "alt", "shift", "meta" or in combination with keycode
59  * Examples:
60  * --------------------
61  * H5DIV.vKey("submit") results in `<div @keyup="submit"></div>
62  * -------------------- 
63  ******************** */ 
64 T vKey(T:DH5Obj)(T h5, string value, string[] modifiers = null) {
65 	if (modifiers) return h5.attribute("@keyup."~modifiers.join("."), value);
66 	return h5.attribute("@keyup", value);
67 }
68 
69 /********************
70  * vBind:
71 ******************** */
72 T vBind(T:DH5Obj)(T h5, string name, string value) {
73 	return h5.attribute(":"~name, value);
74 }
75 
76 /********************
77  * vHtml:
78 ******************** */
79 T vHtml(T:DH5Obj)(T h5, DH5Obj value) { return h5.attribute("v-html", value.toHTML); }
80 T vHtml(T:DH5Obj)(T h5, string value) { return h5.attribute("v-html", value); }
81 
82 /********************
83  * vIf:
84 ******************** */
85 T vIf(T:DH5Obj)(T h5, string name, string value) {
86 	return h5.attribute("v-id:"~name, value);
87 }
88 
89 /********************
90  * vFor - directive to render a list of items based on an array
91  * Examples:
92  * --------------------
93  * H5LI("{{ item.message }}").vFor("item in items") results in <li v-for="item in items">{{ item.message }}</li>
94  * H5LI.vFor("item in items")("{{ item.message }}") same result like above
95  * --------------------
96 ******************** */
97 T vFor(T:DH5Obj)(T h5, string value, string key = null) {
98 	if (key) return h5.attribute("v-for", value).attribute(":key", key);
99 	return h5.attribute("v-for", value);
100 }
101 unittest {
102 	assert(H5Div.vFor("item in items") == `<div v-for="item in items"></div>`);
103 	assert(H5Div.vFor("item in items", "item.id") == `<div :key="item.id" v-for="item in items"></div>`);
104 }
105 
106 /********************
107  * vModel:
108 ******************** */
109 T vModel(T:DH5Obj)(T h5, string value) {
110 	return h5.attribute("v-model", value);
111 }
112 
113 /********************
114  * vShow:
115 ******************** */
116 T vShow(T:DH5Obj)(T h5, string value) {
117 	return h5.attribute("v-show", value);
118 }
119 
120 /********************
121  * vElse:
122 ******************** */
123 T vElse(T:DH5Obj)(T h5) { return h5.attribute("v-else", "v-else"); }
124 
125 /********************
126  * vClass:
127 ******************** */
128 T vClass(T:DH5Obj)(T h5, string[] values) { return h5.attribute(":class", "["~values.join(",")~"]"); }
129 T vClass(T:DH5Obj)(T h5, string[string] values, bool sort = true) {
130 	string[] inner;
131 	if (sort) foreach(k; values.byKey().array.sort!("a < b")) {
132 		if (k.indexOf("-") == -1) inner~="%s:%s".format(k, values[k]);
133 		else inner~="'%s':%s".format(k, values[k]);
134 	}
135 	else foreach(k, v; values) {
136 		if (k.indexOf("-") == -1) inner~="%s:%s".format(k, v);
137 		else inner~="'%s':%s".format(k, v);
138 	}
139 	return h5.vClass("{"~inner.join(",")~"}");
140 }
141 T vClass(T:DH5Obj)(T h5, string value) { return h5.attribute(":class", value); }
142 
143 unittest {
144 	assert(H5Div.vKey("function", ["13"]) == `<div @keyup.13="function"></div>`);
145 
146 	assert(H5Li("{{ item.message }}").vFor("item in items") == `<li v-for="item in items">{{ item.message }}</li>`);
147 	assert(H5Li.vFor("item in items")("{{ item.message }}") == `<li v-for="item in items">{{ item.message }}</li>`);
148 
149 	assert(H5Div.vClass("xy") == `<div :class="xy"></div>`);
150 	assert(H5Div.vClass(["x":"y"]) == `<div :class="{x:y}"></div>`);
151 }
152 
153 /********************
154  * toTemplate:
155 ******************** */
156 string toTemplate(DH5Obj[] someH5Objs) {
157 	string result;
158 	foreach(h5; someH5Objs) {
159 		result ~= h5.toTemplate;
160 	}
161 	return result;
162 }
163 
164 string toTemplate(DH5Obj aH5Obj) {	
165 	if (aH5Obj.tag.length == 0) return "%s".format(aH5Obj);
166 	if (aH5Obj.single) {
167 		if (aH5Obj.attributes.length > 0)
168 			return "<%s%s>".format(aH5Obj.tag, aH5Obj.attsToHTML);
169 		return "<%s>".format(aH5Obj.tag);
170 	}
171 	else {
172 		if (aH5Obj.attributes.length > 0)
173 			return "<%s%s>%s</%s>".format(aH5Obj.tag, aH5Obj.attsToHTML, aH5Obj.html.toTemplate, aH5Obj.tag);
174 		return "<%s>%s</%s>".format(aH5Obj.tag, aH5Obj.html.toTemplate, aH5Obj.tag);
175 	}
176 }
177 unittest {
178 }
179