diff --git a/source/uim/html/apps/app.d b/source/uim/html/apps/app.d index 14027d7..48c6162 100755 --- a/source/uim/html/apps/app.d +++ b/source/uim/html/apps/app.d @@ -134,13 +134,13 @@ class DH5App { assert(uim.html.elements.meta.toString(H5AppLayout.metas([H5Meta(["a":"b"])]).metas) == ``); } - DH5Obj[] _styles; - DH5Obj[] styles() { return _styles; } + DH5Style[] _styles; + DH5Style[] styles() { return _styles; } O styles(this O)(string content, string[] contents...) { this.styles([content]~contents); return cast(O)this; } // O styles(this O)(string[] links) { foreach(link; links) _styles ~= H5Style(content); return cast(O)this;} - O styles(this O)(string[string] link, string[string][] links...) { this.styles([link]~links); return cast(O)this;} - O styles(this O)(string[string][] links) { foreach(link; links) _styles ~= H5Link(link); return cast(O)this;} + O styles(this O)(string[string] link, string[string][] links...) { this.links([link]~links); return cast(O)this;} + O styles(this O)(string[string][] links) { foreach(link; links) _links ~= H5Link(link); return cast(O)this;} O styles(this O)(DH5Style[] styles...) { this.styles(styles); return cast(O)this;} O styles(this O)(DH5Style[] styles) { _styles ~= styles; return cast(O)this;} @@ -152,18 +152,18 @@ class DH5App { /// TODO } - DH5Script[] _libraries; - DH5Script[] libraries() { return _libraries; } - O libraries(this O)(string lib, string[] libs...) { this.libraries([lib]~libs); return cast(O)this;} - O libraries(this O)(string[] libs) { foreach(lib; libs) _libraries ~= H5Script(["src":lib]); return cast(O)this;} + DH5Script[] _scripts; + DH5Script[] scripts() { return _scripts; } + O scripts(this O)(string lib, string[] libs...) { this.scripts([lib]~libs); return cast(O)this;} + O scripts(this O)(string[] libs) { foreach(lib; libs) _scripts ~= H5Script(lib); return cast(O)this;} - O libraries(this O)(string[string] lib, string[string][] libs...) { this.libraries([lib]~libs); return cast(O)this;} - O libraries(this O)(string[string][] libs) { foreach(lib; libs) _libraries ~= H5Script(lib); return cast(O)this;} + O scripts(this O)(string[string] lib, string[string][] libs...) { this.scripts([lib]~libs); return cast(O)this;} + O scripts(this O)(string[string][] libs) { foreach(lib; libs) _scripts ~= H5Script(lib); return cast(O)this;} - O libraries(this O)(DH5Script[] libs...) { this.libraries(libs); return cast(O)this;} - O libraries(this O)(DH5Script[] libs) { _libraries ~= libs; return cast(O)this;} + O scripts(this O)(DH5Script[] libs...) { this.scripts(libs); return cast(O)this;} + O scripts(this O)(DH5Script[] libs) { _scripts ~= libs; return cast(O)this;} - O clearLibraries(this O)() { _libraries = null; return cast(O)this; } + O clearScripts(this O)() { _scripts = null; return cast(O)this; } unittest { // assert(H5AppLayout.) } @@ -243,8 +243,8 @@ class DH5App { /// TODO } - /// Managing Scripts - auto scripts() { + /* /// Managing Scripts + auto getScripts() { DH5AppScript[] results; foreach(name, obj; _objs) if (auto result = cast(DH5AppScript)obj) results ~= result; return results; } @@ -274,7 +274,7 @@ class DH5App { O clearScripts(this O)() { foreach(name, item; _objs) if (auto obj = cast(DH5AppScript)item) this.remove(name); return cast(O)this; } unittest { /// TODO - } + } */ // Page handling // Get all pages of an app @@ -380,11 +380,11 @@ class DH5App { if (req.path == rootPath) { if ("index" in _objs) { - _index.request(req, res, _parameters); + _index.request(req, res, _parameters.dup); return; } if ("error" in _objs) { - _error.request(req, res, _parameters); + _error.request(req, res, _parameters.dup); return; } } @@ -395,7 +395,7 @@ class DH5App { if (appPath in _objs) { // static urls writeln("Found Obj -> ", appPath); - _objs[appPath].request(req, res, _parameters); + _objs[appPath].request(req, res, _parameters.dup); return; } @@ -433,7 +433,7 @@ class DH5App { }} } if (foundPage) { - obj.request(req, res, _parameters); + obj.request(req, res, _parameters.dup); return; } } diff --git a/source/uim/html/apps/layout.d b/source/uim/html/apps/layout.d index 91b5071..35d680e 100755 --- a/source/uim/html/apps/layout.d +++ b/source/uim/html/apps/layout.d @@ -40,8 +40,8 @@ import uim.html; assert(uim.html.elements.meta.toString(H5AppLayout.metas([H5Meta(["a":"b"])]).metas) == ``); } - DH5Obj[] _links; - DH5Obj[] links() { return _links; } + DH5Link[] _links; + DH5Link[] links() { return _links; } O links(this O)(string[string] link, string[string][] links...) { this.links([link]~links); return cast(O)this;} O links(this O)(string[string][] links) { foreach(link; links) _links ~= H5Link(link); return cast(O)this;} @@ -53,13 +53,13 @@ import uim.html; /// TODO } - DH5Obj[] _styles; - DH5Obj[] styles() { return _styles; } + DH5Style[] _styles; + DH5Style[] styles() { return _styles; } O styles(this O)(string content, string[] contents...) { this.styles([content]~contents); return cast(O)this; } // O styles(this O)(string[] links) { foreach(link; links) _styles ~= H5Style(content); return cast(O)this;} - O styles(this O)(string[string] link, string[string][] links...) { this.styles([link]~links); return cast(O)this;} - O styles(this O)(string[string][] links) { foreach(link; links) _styles ~= H5Link(link); return cast(O)this;} + O styles(this O)(string[string] link, string[string][] links...) { this.links([link]~links); return cast(O)this;} + O styles(this O)(string[string][] links) { foreach(link; links) _links ~= H5Link(link); return cast(O)this;} O styles(this O)(DH5Style[] styles...) { this.styles(styles); return cast(O)this;} O styles(this O)(DH5Style[] styles) { _styles ~= styles; return cast(O)this;} @@ -71,18 +71,18 @@ import uim.html; /// TODO } - DH5Script[] _libraries; - DH5Script[] libraries() { return _libraries; } - O libraries(this O)(string lib, string[] libs...) { this.libraries([lib]~libs); return cast(O)this;} - O libraries(this O)(string[] libs) { foreach(lib; libs) _libraries ~= H5Script(["src":lib]); return cast(O)this;} + DH5Script[] _scripts; + DH5Script[] scripts() { return _scripts; } + O scripts(this O)(string lib, string[] libs...) { this.scripts([lib]~libs); return cast(O)this;} + O scripts(this O)(string[] libs) { foreach(lib; libs) _scripts ~= H5Script(["src":lib]); return cast(O)this;} - O libraries(this O)(string[string] lib, string[string][] libs...) { this.libraries([lib]~libs); return cast(O)this;} - O libraries(this O)(string[string][] libs) { foreach(lib; libs) _libraries ~= H5Script(lib); return cast(O)this;} + O scripts(this O)(string[string] lib, string[string][] libs...) { this.scripts([lib]~libs); return cast(O)this;} + O scripts(this O)(string[string][] libs) { foreach(lib; libs) _scripts ~= H5Script(lib); return cast(O)this;} - O libraries(this O)(DH5Script[] libs...) { this.libraries(libs); return cast(O)this;} - O libraries(this O)(DH5Script[] libs) { _libraries ~= libs; return cast(O)this;} + O scripts(this O)(DH5Script[] libs...) { this.scripts(libs); return cast(O)this;} + O scripts(this O)(DH5Script[] libs) { _scripts ~= libs; return cast(O)this;} - O clearLibraries(this O)() { _libraries = null; return cast(O)this; } + O clearScripts(this O)() { _scripts = null; return cast(O)this; } unittest { // assert(H5AppLayout.) } @@ -103,80 +103,100 @@ import uim.html; string opCall(string content, string[string] parameters = null) { return toString(content, parameters); } override string toString() { return toString("", null); } - string toString(DH5AppPage page, string[string] parameters = null) { - if ("title" !in parameters) { - parameters["title"] = this.title; - } - - if ("lang" !in parameters) { - parameters["lang"] = this.lang; - } - - if (page) { - debug writeln("Reading header settings from page"); - if (page.title) parameters["title"] = this.title; - if (page.lang) parameters["lang"] = this.lang; - - if (page.metas) { - if ("metas" in parameters) parameters["metas"] = page.metas.asString~parameters["metas"]; - else parameters["metas"] = page.metas.asString; - } - if (page.links) { - parameters["links"] = "links" in parameters ? page.styles.asString~parameters["styles"] : page.styles.asString; - } - if (page.styles) { - if ("styles" in parameters) parameters["styles"] = page.styles.asString~parameters["styles"]; - else parameters["styles"] = page.styles.asString; - } - if (page.libraries) { - if ("libraries" in parameters) parameters["libraries"] = page.libraries.asString~parameters["libraries"]; - else parameters["libraries"] = page.libraries.asString; - } - debug writeln(parameters); - } - return toString(page.content, parameters); + string toString(DH5AppPage page, string[string] someParameters = null) { + return toString(page, page.content, someParameters); } - string toString(string content, string[string] parameters = null) { - debug writeln("Reading header settings from app"); - if (this.app) { - if (this.app.metas) { - if ("metas" in parameters) parameters["metas"] = this.app.metas.asString~parameters["metas"]; - else parameters["metas"] = this.app.metas.asString; - } - if (this.app.styles) { - if ("styles" in parameters) parameters["styles"] = this.app.styles.asString~parameters["styles"]; - else parameters["styles"] = this.app.styles.asString; - } - if (this.app.libraries) { - if ("libraries" in parameters) parameters["libraries"] = this.app.libraries.asString~parameters["libraries"]; - else parameters["libraries"] = this.app.libraries.asString; - } - debug writeln(parameters); - } + string toString(DH5AppPage page, string content, string[string] someParameters = null) { + // layout override app, page override layout, parameters override page + string[string] newParameters; + + if (app) newParameters = app.parameters.dup; + // Layout overrides app + foreach(k,v; this.parameters) newParameters[k] = v; + // page overrides layout & app + foreach(k,v; page.parameters) newParameters[k] = v; + // parameters overrides App & layout & page + foreach(k,v; someParameters) newParameters[k] = v; + + DH5Meta[] newMetas; + if (app) newMetas ~= app.metas; + newMetas ~= this.metas; + newMetas ~= page.metas; + + DH5Link[] newLinks; +// if (app) newLinks = app.links; + newLinks ~= this.links; + newLinks ~= page.links; + + DH5Style[] newStyles; + if (app) newStyles ~= app.styles; + newStyles ~= this.styles; + newStyles ~= page.styles; + + DH5Script[] newScripts; + if (app) newScripts ~= app.scripts; + newScripts ~= this.scripts; + newScripts ~= page.scripts; + + return htmlDocument(content, newMetas, newLinks, newStyles, newScripts, newParameters); + } + + string toString(string content, string[string] someParameters = null) { + // layout override app, parameters override layout + string[string] newParameters; + + if (app) newParameters = app.parameters.dup; + // Layout overrides app + foreach(k,v; this.parameters) newParameters[k] = v; + // parameters overrides App & layout & page + foreach(k,v; someParameters) newParameters[k] = v; + + DH5Meta[] newMetas; + if (app) newMetas = app.metas; + newMetas ~= this.metas; + + DH5Link[] newLinks; +// if (app) newLinks = app.links; + newLinks ~= this.links; + + DH5Style[] newStyles; + if (app) newStyles = app.styles; + newStyles ~= this.styles; + + DH5Script[] newScripts; + if (app) newScripts ~= app.scripts; + newScripts ~= this.scripts; + + return htmlDocument(content, newMetas, newLinks, newStyles, newScripts, newParameters); + } + auto htmlDocument(string content, DH5Meta[] newMetas, DH5Link[] newLinks, DH5Style[] newStyles, DH5Script[] newScripts, string[string] parameters = null) { auto finalLang = parameters.get("lang", this.lang); // if lang !in parameters use this.lang auto finalTitle = parameters.get("title", this.title); // if title !in parameters use this.title // creating HTML page _html = H5Html - .attributes("lang", finalLang).attributes("dir", ("dir" in parameters ? parameters["dir"] : "ltr")) + .attributes("lang", finalLang).attributes("dir", parameters.get("dir", "ltr")) // Head part of HTML .head(_headClasses) .head(_headAttributes) - .head(this.metas.asString~parameters.get("metas", "")) .head(finalTitle.length > 0 ? "" ~ finalTitle ~ "":"") - .head(this.styles.asString~parameters.get("styles", "")) + .head(newMetas.asString~parameters.get("metas", "")) + .head(newLinks.asString~parameters.get("links", "")) + .head(newStyles.asString~parameters.get("styles", "")) + .head("style" in parameters ? H5Style(parameters["style"]).toString : "") // Body part of HTML .body_(_bodyClasses) .body_(_bodyAttributes) - .body_(this.layout ? this.layout.toString(content, this.parameters) : content) - .body_(this.libraries.asString~parameters.get("libraries", "")); + .body_(this.layout ? this.layout.toString(content, parameters) : content) + .body_(newScripts.asString~parameters.get("scripts", "")) + .body_("script" in parameters ? H5Script(parameters["script"]).toString : ""); return _html.toString; } unittest { - writeln(H5AppLayout); + // writeln(H5AppLayout); assert(H5AppLayout == ``); assert(H5AppLayout()("xxx") == `xxx`); } diff --git a/source/uim/html/apps/obj.d b/source/uim/html/apps/obj.d index ef161a2..98818d9 100755 --- a/source/uim/html/apps/obj.d +++ b/source/uim/html/apps/obj.d @@ -111,10 +111,10 @@ class DH5AppObj { foreach(k, v; this.parameters) requestParameters[k] = v; foreach(k, v; parameters) requestParameters[k] = v; - foreach(k, v; req.params) requestParameters[k] = v; - foreach(k, v; req.headers) requestParameters[k] = v; - foreach(k, v; req.query) requestParameters[k] = v; - foreach(k, v; req.form) requestParameters[k] = v; + foreach(key; req.params.byKey) requestParameters[key] = req.params[key]; + foreach(key; req.headers.byKey) requestParameters[key] = req.headers[key]; + foreach(key; req.query.byKey) requestParameters[key] = req.query[key]; + foreach(key; req.form.byKey) requestParameters[key] = req.form[key]; res.writeBody(toString(requestParameters), _mimetype); } diff --git a/source/uim/html/apps/page.d b/source/uim/html/apps/page.d index 12fae63..2ab605a 100755 --- a/source/uim/html/apps/page.d +++ b/source/uim/html/apps/page.d @@ -3,7 +3,13 @@ module uim.html.apps.page; import uim.html; class DH5AppPage : DH5AppObj { - this() { super(); this.mimetype("text/html"); } + this() { super(); + clearMetas; + clearLinks; + clearStyles; + clearScripts; + + this.mimetype("text/html"); } this(DH5App anApp) { this().app(anApp); } this(string aName) { this().name(aName); } this(DH5App anApp, string aName) { this(anApp).name(aName); } @@ -61,10 +67,12 @@ class DH5AppPage : DH5AppObj { assert(uim.html.elements.meta.toString(H5AppLayout.metas([H5Meta(["a":"b"])]).metas) == ``); } - DH5Obj[] _links; - DH5Obj[] links() { return _links; } + DH5Link[] _links; + DH5Link[] links() { return _links; } O links(this O)(string[string] link, string[string][] links...) { this.links([link]~links); return cast(O)this;} - O links(this O)(string[string][] links) { foreach(link; links) _links ~= H5Link(link); return cast(O)this;} + O links(this O)(string[string][] links) { + foreach(link; links) _links ~= H5Link(link); + return cast(O)this;} O links(this O)(DH5Link[] links...) { this.links(links); return cast(O)this;} O links(this O)(DH5Link[] links) { _links ~= links; return cast(O)this;} @@ -74,13 +82,13 @@ class DH5AppPage : DH5AppObj { /// TODO } - DH5Obj[] _styles; - DH5Obj[] styles() { return _styles; } + DH5Style[] _styles; + DH5Style[] styles() { return _styles; } O styles(this O)(string content, string[] contents...) { this.styles([content]~contents); return cast(O)this; } // O styles(this O)(string[] links) { foreach(link; links) _styles ~= H5Style(content); return cast(O)this;} - O styles(this O)(string[string] link, string[string][] links...) { this.styles([link]~links); return cast(O)this;} - O styles(this O)(string[string][] links) { foreach(link; links) _styles ~= H5Link(link); return cast(O)this;} + O styles(this O)(string[string] link, string[string][] links...) { this.links([link]~links); return cast(O)this;} + O styles(this O)(string[string][] links) { foreach(link; links) _links ~= H5Link(link); return cast(O)this;} O styles(this O)(DH5Style[] styles...) { this.styles(styles); return cast(O)this;} O styles(this O)(DH5Style[] styles) { _styles ~= styles; return cast(O)this;} @@ -92,25 +100,40 @@ class DH5AppPage : DH5AppObj { /// TODO } - DH5Script[] _libraries; - DH5Script[] libraries() { return _libraries; } - O libraries(this O)(string lib, string[] libs...) { this.libraries([lib]~libs); return cast(O)this;} - O libraries(this O)(string[] libs) { foreach(lib; libs) _libraries ~= H5Script(["src":lib]); return cast(O)this;} + DH5Script[] _scripts; + DH5Script[] scripts() { return _scripts; } + + O scripts(this O)(string newScript, string[] newScripts...) { this.scripts([newScript]~newScripts); return cast(O)this;} + O scripts(this O)(string[] newScripts) { + debug writeln("In AppPAge: O scripts(this O)(string[] newScripts)"); + writeln("Scripts length = ", newScripts.length); + _scripts ~= newScripts.map!(a => H5Script(a)).array; + return cast(O)this;} - O libraries(this O)(string[string] lib, string[string][] libs...) { this.libraries([lib]~libs); return cast(O)this;} - O libraries(this O)(string[string][] libs) { foreach(lib; libs) _libraries ~= H5Script(lib); return cast(O)this;} + O scripts(this O)(string[string] newScript, string[string][] newScripts...) { this.scripts([newScript]~newScripts); return cast(O)this;} + O scripts(this O)(string[string][] newScripts) { _scripts ~= newScripts.map!(a => H5Script(a)).array; return cast(O)this;} - O libraries(this O)(DH5Script[] libs...) { this.libraries(libs); return cast(O)this;} - O libraries(this O)(DH5Script[] libs) { _libraries ~= libs; return cast(O)this;} + O scripts(this O)(DH5Script[] newScripts...) { this.scripts(newScripts); return cast(O)this;} + O scripts(this O)(DH5Script[] newScripts) { _scripts ~= newScripts; return cast(O)this;} - O clearLibraries(this O)() { _libraries = null; return cast(O)this; } + O clearScripts(this O)() { _scripts = null; return cast(O)this; } unittest { // assert(H5AppLayout.) } /// Export to string override string toString(string[string] parameters) { - // if (_layout) return _layout.toString(this.content, this.parameters); + // Check layout + DH5AppLayout lt; + if (app) lt = app.layout; + if (this.layout) lt = this.layout; + + // if layout, use layout + if (lt) return this.layout.toString(this, parameters); + return this.content; // No layout, only content + + // old stuff :-( +/* // if (_layout) return _layout.toString(this.content, this.parameters); // return _content; // // debug // writeln("H5Page: override string toString()"); if (cached) { @@ -123,7 +146,7 @@ class DH5AppPage : DH5AppObj { // // debug // writeln(this.content); if (this.content) { return (this.layout ? this.layout.toString(this, this.parameters) : this.content); } return _toString; - } + */ } unittest { //// writeln(`// writeln(H5AppPage)`); //// writeln(H5AppPage);