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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*
RailControl - Model Railway Control Software
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
RailControl is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
RailControl 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/
#include <vector>
#include "DataModel/Text.h"
#include "Utils/Utils.h"
#include "Server/Web/HtmlTag.h"
#include "Server/Web/HtmlTagButtonCancel.h"
#include "Server/Web/HtmlTagButtonCommandWide.h"
#include "Server/Web/HtmlTagButtonOK.h"
#include "Server/Web/HtmlTagButtonPopupWide.h"
#include "Server/Web/HtmlTagInputHidden.h"
#include "Server/Web/HtmlTagInputIntegerWithLabel.h"
#include "Server/Web/HtmlTagInputTextWithLabel.h"
#include "Server/Web/HtmlTagText.h"
#include "Server/Web/WebClient.h"
#include "Server/Web/WebClientText.h"
using namespace DataModel;
using LayoutPosition = DataModel::LayoutItem::LayoutPosition;
using LayoutItemSize = DataModel::LayoutItem::LayoutItemSize;
using LayoutRotation = DataModel::LayoutItem::LayoutRotation;
using std::map;
using std::string;
using std::to_string;
using std::vector;
namespace Server { namespace Web
{
void WebClientText::HandleTextEdit(const map<string, string>& arguments)
{
HtmlTag content;
TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
string name = Languages::GetText(Languages::TextNew);
LayoutPosition posx = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0);
LayoutPosition posy = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0);
LayoutPosition posz = Utils::Utils::GetIntegerMapEntry(arguments, "posz", LayerUndeletable);
LayoutItemSize width = Utils::Utils::GetIntegerMapEntry(arguments, "width", 1);
LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0);
if (textID > TextNone)
{
const DataModel::Text* text = manager.GetText(textID);
if (text)
{
name = text->GetName();
posx = text->GetPosX();
posy = text->GetPosY();
posz = text->GetPosZ();
width = text->GetWidth();
rotation = text->GetRotation();
}
}
content.AddChildTag(HtmlTag("h1").AddContent(name).AddId("popup_title"));
HtmlTag tabMenu("div");
tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("main", Languages::TextBasic, true));
tabMenu.AddChildTag(WebClientStatic::HtmlTagTabMenuItem("position", Languages::TextPosition));
content.AddChildTag(tabMenu);
HtmlTag formContent;
formContent.AddChildTag(HtmlTagInputHidden("cmd", "textsave"));
formContent.AddChildTag(HtmlTagInputHidden("text", to_string(textID)));
HtmlTag mainContent("div");
mainContent.AddId("tab_main");
mainContent.AddClass("tab_content");
mainContent.AddChildTag(HtmlTagInputTextWithLabel("name", Languages::TextName, name).AddAttribute("onkeyup", "updateName();"));
mainContent.AddChildTag(HtmlTagInputIntegerWithLabel("width", Languages::TextWidth, width, DataModel::Text::MinWidth, DataModel::Text::MaxWidth));
formContent.AddChildTag(mainContent);
formContent.AddChildTag(client.HtmlTagTabPosition(posx, posy, posz, rotation));
content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(HtmlTag("form").AddId("editform").AddChildTag(formContent)));
content.AddChildTag(HtmlTagButtonCancel());
content.AddChildTag(HtmlTagButtonOK());
client.ReplyHtmlWithHeader(content);
}
void WebClientText::HandleTextSave(const map<string, string>& arguments)
{
TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
string name = Utils::Utils::GetStringMapEntry(arguments, "name");
LayoutPosition posX = Utils::Utils::GetIntegerMapEntry(arguments, "posx", 0);
LayoutPosition posY = Utils::Utils::GetIntegerMapEntry(arguments, "posy", 0);
LayoutPosition posZ = Utils::Utils::GetIntegerMapEntry(arguments, "posz", 0);
LayoutItemSize width = Utils::Utils::GetIntegerMapEntry(arguments, "width", 1);
LayoutRotation rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", DataModel::LayoutItem::Rotation0);
string result;
if (!manager.TextSave(textID,
name,
posX,
posY,
posZ,
width,
rotation,
result))
{
client.ReplyResponse(WebClient::ResponseError, result);
return;
}
client.ReplyResponse(WebClient::ResponseInfo, Languages::TextTextSaved, name);
}
void WebClientText::HandleTextList()
{
HtmlTag content;
content.AddChildTag(HtmlTag("h1").AddContent(Languages::TextTexts));
HtmlTag table("table");
const map<string,DataModel::Text*> textList = manager.TextListByName();
map<string,string> textArgument;
for (auto& text : textList)
{
HtmlTag row("tr");
row.AddChildTag(HtmlTag("td").AddContent(text.first));
const string& textIdString = to_string(text.second->GetID());
textArgument["text"] = textIdString;
row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextEdit, "textedit_list_" + textIdString, textArgument)));
row.AddChildTag(HtmlTag("td").AddChildTag(HtmlTagButtonPopupWide(Languages::TextDelete, "textaskdelete_" + textIdString, textArgument)));
table.AddChildTag(row);
}
content.AddChildTag(HtmlTag("div").AddClass("popup_content").AddChildTag(table));
content.AddChildTag(HtmlTagButtonCancel());
content.AddChildTag(HtmlTagButtonPopupWide(Languages::TextNew, "textedit_0"));
client.ReplyHtmlWithHeader(content);
}
void WebClientText::HandleTextAskDelete(const map<string, string>& arguments)
{
TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
if (textID == TextNone)
{
client.ReplyHtmlWithHeaderAndParagraph(Languages::TextTextDoesNotExist);
return;
}
const DataModel::Text* text = manager.GetText(textID);
if (!text)
{
client.ReplyHtmlWithHeaderAndParagraph(Languages::TextTextDoesNotExist);
return;
}
HtmlTag content;
const string& textName = text->GetName();
content.AddContent(HtmlTag("h1").AddContent(Languages::TextDeleteText));
content.AddContent(HtmlTag("p").AddContent(Languages::TextAreYouSureToDelete, textName));
content.AddContent(HtmlTag("form").AddId("editform")
.AddContent(HtmlTagInputHidden("cmd", "textdelete"))
.AddContent(HtmlTagInputHidden("text", to_string(textID))
));
content.AddContent(HtmlTagButtonCancel());
content.AddContent(HtmlTagButtonOK());
client.ReplyHtmlWithHeader(content);
}
void WebClientText::HandleTextDelete(const map<string, string>& arguments)
{
TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text", TextNone);
const DataModel::Text* text = manager.GetText(textID);
if (!text)
{
client.ReplyResponse(WebClient::ResponseError, Languages::TextTextDoesNotExist);
return;
}
string name = text->GetName();
string result;
if (!manager.TextDelete(textID, result))
{
client.ReplyResponse(WebClient::ResponseError, result);
return;
}
client.ReplyResponse(WebClient::ResponseInfo, Languages::TextTextDeleted, name);
}
void WebClientText::HandleTextGet(const map<string, string>& arguments)
{
TextID textID = Utils::Utils::GetIntegerMapEntry(arguments, "text");
const DataModel::Text* text = manager.GetText(textID);
if (!text)
{
client.ReplyHtmlWithHeader(HtmlTag());
return;
}
client.ReplyHtmlWithHeader(HtmlTagText(text));
}
}} // namespace Server::Web
|