-
Notifications
You must be signed in to change notification settings - Fork 24
/
weblayout.cpp
88 lines (71 loc) · 2.47 KB
/
weblayout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Copyright 2010-2015 Jacob Dawid <[email protected]>
//
// This file is part of QtWebServer.
//
// QtWebServer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// QtWebServer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with QtWebServer.
// If not, see <http://www.gnu.org/licenses/>.
//
// It is possible to obtain a commercial license of QtWebServer.
// Please contact Jacob Dawid <[email protected]>
//
#include "weblayout.h"
#include "html/htmldocument.h"
namespace QtWebServer {
WebLayout::WebLayout() {
}
void WebLayout::addCss(QString resourcePath) {
_cssResources << resourcePath;
}
void WebLayout::addJs(QString resourcePath) {
_jsResources << resourcePath;
}
void WebLayout::addWebWidget(WebWidget* webWidget) {
_webWidgets << webWidget;
}
QString WebLayout::renderHtml(const Http::Request& request) {
Html::Document document;
document.setTitle(title());
foreach(QString resource, _cssResources) {
QDomElement linkNode = document.createElement("link");
linkNode.setAttribute("href", resource);
linkNode.setAttribute("rel", "stylesheet");
linkNode.setAttribute("type", "text/css");
document.head().appendChild(linkNode);
}
foreach(QString resource, _jsResources) {
QDomElement scriptNode = document.createElement("script");
scriptNode.setAttribute("src", resource);
scriptNode.setAttribute("type", "text/javascript");
// We have to put in this hack because self-closing script
// tags are not allowed in HTML
QDomText dummyText = document.createTextNode("");
scriptNode.appendChild(dummyText);
document.body().appendChild(scriptNode);
}
foreach(WebWidget *webWidget, _webWidgets) {
document.appendHtml(
document.body(),
webWidget->renderHtml(request)
);
}
return document.toString();
}
void WebLayout::setTitle(QString title) {
_title = title;
}
QString WebLayout::title() {
return _title;
}
} // namespace QtWebServer