1 module uim.html.apps.obj;
2 
3 import uim.html;
4 
5 class DH5AppObj {
6 	this() { 
7 		this
8 		.created(DateTime(2017, 1, 1, 1, 1, 1))
9 		.changed(DateTime(2017, 1, 1, 1, 1, 1)); }
10 	this(DH5App anApp) { this().app(anApp); }
11 	this(string aName) { this().name(aName); }
12 	this(DH5App anApp, string aName) { this(anApp).name(aName); }
13 
14 	SysTime _accessTime, _modificationTime;
15 	string[] _htmlModes = ["*"];
16 		
17 	bool opEquals(string txt) { return toString == txt; }
18 
19 	O sourceFile(this O)(string path) { 	
20 		std.file.getTimes(path, _accessTime, _modificationTime);
21 		return cast(O)this; }
22 	
23 	/**
24 	* app 
25 	* 
26 	* Property which contains the (parent) app
27 	* DH5App app() - return app if exists
28 	* (this O) app(DH5App newApp) - setting new app / returns obj
29 	* 
30 	* Adding this obj to another app will changes this property to the new app 
31 	**/
32 	mixin(OProperty!("DH5App", "app"));
33 	unittest {
34 		assert(H5AppPage(H5App("test")).app.name == "test");
35 		assert(H5AppPage(H5App("test")).app(H5App("test2")).app.name == "test2");
36 	}
37 
38 	/**
39 	 * name - string that represents the name of the application
40 	 */
41 	protected string _name;
42 	auto name() { return _name; } 
43 	O name(this O)(string newName) { _name = newName; return cast(O)this; }
44 	unittest {
45 		assert(H5AppPage("test").name == "test");
46 		assert(H5AppPage.name("test").name == "test");
47 	}
48 
49 	mixin(OProperty!("DateTime", "created"));
50 	O created(this O)(SysTime value) { this.created(cast(DateTime)value); return cast(O)this; }
51 	unittest {
52 		// TODO
53 	}
54 
55 	mixin(OProperty!("DateTime", "changed"));
56 	O changed(this O)(SysTime value) { this.changed(cast(DateTime)value); return cast(O)this; }
57 	unittest {
58 		// TODO
59 	}
60 
61 	auto path() { 
62 		if (_app) return app.rootPath ~ name;
63 		return "/"~name; }
64 
65 	/// Mimetype of obj
66 	string _mimetype;
67 	auto mimetype() { return _mimetype; }
68 	O mimetype(this O)(string newMimetype) { _mimetype = newMimetype; return cast(O)this; }
69 	unittest {
70 		assert(H5AppObj.mimetype("text/xml").mimetype == "text/xml");
71 		assert(H5AppObj.mimetype("text/xml").mimetype("applications/javascript").mimetype == "applications/javascript");
72 	}
73 
74 	/// Name of obj
75 	bool _cached;
76 	auto cached() { return _cached; }
77 	O cached(this O)(bool newcached) { _cached = newcached; return cast(O)this; }
78 	unittest {
79 		/// TODO
80 	}
81 
82 	/// Page parameters - will be used to communicate between components
83 	mixin(XStringAA!"parameters");
84 	unittest {
85 		assert(H5AppObj.parameters == null);
86 		assert(H5AppObj.parameters(["x":"y"]).parameters == ["x":"y"]);
87 		assert(H5AppObj.parameters("x", "y").parameters == ["x":"y"]);
88 	}
89 
90 	/// Content of obj
91 	string _content;
92 	string content() { 
93 		// // debug // writeln("H5AppObj: string content()");
94 		return _content; }
95 	O content(this O)(DH5Obj[] addContent) { foreach(c; addContent) _content ~= c.toString; return cast(O)this; }
96 	O content(this O)(DH5Obj[] addContent...) { foreach(c; addContent) _content ~= c.toString; return cast(O)this; }
97 	O content(this O)(string addContent) { _content ~= addContent; return cast(O)this; }
98 	O clearContent(this O)() { _content = null; return cast(O)this; }
99 	unittest {
100 		assert(H5AppObj.content("test").content == "test");
101 		assert(H5AppObj.content("double").content("test").content == "doubletest");
102 		assert(H5AppObj.content("double").content("test").clearContent.content == "");
103 		assert(H5AppObj.content("double").content("test").clearContent.content("test").content == "test");
104 	}
105 
106 	/// Response to HTTP request
107 	void request(HTTPServerRequest req, HTTPServerResponse res, string[string] parameters = null) {
108 		// debug // writeln("H5AppObj: void request()");
109 		string[string] requestParameters;
110 		if (this.app) foreach(k, v; this.app.parameters) requestParameters[k] = v;
111 		foreach(k, v; this.parameters) requestParameters[k] = v;
112 		foreach(k, v; parameters) requestParameters[k] = v;
113 
114 		foreach(key; req.params.byKey) requestParameters[key] = req.params[key];
115 		foreach(key; req.headers.byKey) requestParameters[key] = req.headers[key];
116 		foreach(key; req.query.byKey) requestParameters[key] = req.query[key];
117 		foreach(key; req.form.byKey) requestParameters[key] = req.form[key];
118 
119 		res.writeBody(toString(requestParameters), _mimetype); 
120 	}
121 	unittest {
122 		/// TODO
123 	}
124 
125 	string _toString;
126 	/// Export to string
127 	override string toString() { string[string] pm; return toString(pm); }
128 	string toString(string[string] parameters) {
129 		debug writeln("H5AppObj: override string toString()");
130 		debug writeln("Content...", this.content);
131 		
132 		debug writeln("Is cached?");
133 		if (cached) {
134 			debug writeln("Yes, is cached");
135 			if (_toString.length == 0) _toString = this.content; 
136 			return _toString; 
137 		}
138 
139 		debug writeln("Is not cached");
140 		return this.content; 
141 	}
142 	unittest {
143 		assert(H5AppObj.content("test").toString == "test");
144 		assert(H5AppObj.content("double").content("test").toString == "doubletest");
145 		assert(H5AppObj.content("double").content("test").clearContent.toString == "");
146 		assert(H5AppObj.content("double").content("test").clearContent.content("test").toString == "test");
147 	}
148 }
149 auto H5AppObj() { return new DH5AppObj(); }
150 auto H5AppObj(DH5App anApp) { return new DH5AppObj(anApp); }
151 auto H5AppObj(string aName) { return new DH5AppObj(aName); }
152 auto H5AppObj(DH5App anApp, string aName) { return new DH5AppObj(anApp, aName); }
153 
154 unittest {
155 	/// TODO
156 }