Debian Codegraph @

Server/Web/HtmlTagSelect.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <map>
24 #include <string>
25
26 #include "DataModel/ObjectIdentifier.h"
27 #include "Languages.h"
28 #include "Server/Web/HtmlTag.h"
29 #include "Server/Web/HtmlTagInputHidden.h"
30 #include "Server/Web/HtmlTagInputText.h"
31
32 namespace Server { namespace Web
33 {
34 class HtmlTagSelect : public HtmlTag
35 {
36 public:
37 HtmlTagSelect() = delete;
38
39 HtmlTagSelect(const std::string& name,
40 const std::map<std::string,std::string>& options,
41 const std::string& defaultValue = "");
42
43 HtmlTagSelect(const std::string& name,
44 const std::map<std::string,DataModel::ObjectIdentifier>& options,
45 const DataModel::ObjectIdentifier& defaultValue = DataModel::ObjectIdentifier());
46
47 template<typename T>
48 HtmlTagSelect(const std::string& name,
49 const std::map<std::string,T>& options,
50 const int defaultValue = 0) // This can not be Type T, because it would be ambiguous with previous declaration
51 : HtmlTagSelect(name)
52 {
53 for (auto& option : options)
54 {
55 AddOption(std::to_string(option.second), option.first, option.second == defaultValue);
56 }
57 CheckDefaultKeyValue();
58 }
59
60 // T2 must be implicitly convertible to Languages::TextSelector
61 template<typename T1, typename T2>
62 HtmlTagSelect(const std::string& name,
63 const std::map<T1,T2>& options,
64 const T1 defaultValue = 0)
65 : HtmlTagSelect(name)
66 {
67 for (auto& option : options)
68 {
69 AddOption(std::to_string(option.first), Languages::GetText(option.second), option.first == defaultValue);
70 }
71 CheckDefaultKeyValue();
72 }
73
74 template<typename T>
75 HtmlTagSelect(const std::string& name,
76 const std::map<T,std::string>& options,
77 const T defaultValue = 0)
78 : HtmlTagSelect(name)
79 {
80 for (auto& option : options)
81 {
82 AddOption(std::to_string(option.first), option.second, option.first == defaultValue);
83 }
84 CheckDefaultKeyValue();
85 }
86
87 virtual HtmlTag AddAttribute(const std::string& name, const std::string& value = "") override;
88
89 private:
90 HtmlTagSelect(const std::string& name);
91
92 void AddOption(const std::string& key,
93 const std::string& value,
94 const bool defaultKeyValue);
95
96 void CheckDefaultKeyValue();
97
98 inline void SetDefaultKeyValue(const std::string& key,
99 const std::string& value)
100 {
101 childTags[0].AddAttribute("value", key);
102 childTags[1].AddAttribute("value", value);
103 }
104
105 const std::string commandID;
106 bool defaultKeyValueFound;
107 std::string defaultKey;
108 std::string defaultValue;
109 };
110 }} // namespace Server::Web
111