1 module uim.vue.base.mixins;
2 
3 import uim.vue;
4 
5 @safe:
6 
7 class DVUEMixin : DVUEObj {
8 	this() { super(); }
9 	this(DVUEApp anApp) { this(); _app = anApp; }
10 	this(string aName) { this(); _name = aName; }
11 	this(DVUEApp anApp, string aName) { this(anApp); _name = aName; }
12 
13 	mixin(TProperty!("DVUEApp", "app"));
14 
15 	private string[] _imports;
16 	O imports(this O)(string[string] someImports) { foreach(k, v; someImports) this.imports(k, v); return cast(O)this; }
17 	O imports(this O)(string anName, string fromFile) { this.imports(anName~" from '"~fromFile~"'"); return cast(O)this; }
18 	O imports(this O)(string[] someImports) { foreach(imp; someImports) this.imports(imp); return cast(O)this; }
19 	O imports(this O)(string anImport) { _imports ~= "import "~anImport~";"; return cast(O)this; }
20 	unittest {
21 		assert(VUEMixin.imports(["name":"file.js"]) == "import name from 'file.js';");
22 		assert(VUEMixin.imports(["name from 'file.js'", "othername from 'otherfile.js'"]) == "import name from 'file.js';import othername from 'otherfile.js';");
23 
24 		assert(VUEMixin.imports("name", "file.js") == "import name from 'file.js';");
25 		assert(VUEMixin.imports("name from 'file.js'") == "import name from 'file.js';");
26 	}
27 
28 	private string[] _exports;
29 	O exportsDefault(this O)(DJS anExport) { this.exportsDefault(anExport.toString); return cast(O)this; }
30 	O exportsDefault(this O)(string anExport) { this.exports("default "~anExport); return cast(O)this; }
31 	O exports(this O)(DJS[] someExport) { foreach(ex; someExports) this.exports(ex); return cast(O)this; }
32 	O exports(this O)(string[] anExport) { foreach(ex; someExports) this.exports(ex); return cast(O)this; }
33 	O exports(this O)(DJS anExport) { this.exports(anExport.toString); return cast(O)this; }
34 	O exports(this O)(string anExport) { _exports ~= "export "~anExport~";"; return cast(O)this; }
35 	unittest {
36 		assert(VUEMixin.exports("const foo = Math.sqrt(2)") == "export const foo = Math.sqrt(2);");
37 	}
38 
39 	mixin(TProperty!("string", "content"));
40 	O content(this O)(DJS js) { _content = js.toString; return cast(O)this; }
41 
42 	override bool opEquals(string txt) { return (txt == toString); }
43 
44 	void request(HTTPServerRequest req, HTTPServerResponse res) {
45 		res.writeBody(toJS, "text/javascript");
46 	}
47 	string toJS() {
48 		string result;
49 		result = toString;
50 		return result;
51 	}
52 	override string toString() {
53 		string result;
54 		result ~= _imports.join("");
55 		result ~= _exports.join("");
56 		return result;
57 	}
58 }
59 auto VUEMixin() { return new DVUEMixin(); }
60 auto VUEMixin(string aName) { return new DVUEMixin(aName); }
61 auto VUEMixin(DVUEApp anApp) { return new DVUEMixin(anApp); }
62 auto VUEMixin(DVUEApp anApp, string aName) { return new DVUEMixin(anApp, aName); }
63 
64 unittest {
65 
66 }