| 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 | #include "Server/Web/HtmlTagSelect.h" |
| 22 | |
| 23 | namespace Server { namespace Web |
| 24 | { |
| 25 | HtmlTagSelect::HtmlTagSelect(const std::string& name, |
| 26 | const std::map<std::string,std::string>& options, |
| 27 | const std::string& defaultValue) |
| 28 | : HtmlTagSelect(name) |
| 29 | { |
| 30 | for (auto& option : options) |
| 31 | { |
| 32 | AddOption(option.first, option.second, option.first == defaultValue); |
| 33 | } |
| 34 | CheckDefaultKeyValue(); |
| 35 | } |
| 36 | |
| 37 | HtmlTagSelect::HtmlTagSelect(const std::string& name, |
| 38 | const std::map<std::string,DataModel::ObjectIdentifier>& options, |
| 39 | const DataModel::ObjectIdentifier& defaultValue) |
| 40 | : HtmlTagSelect(name) |
| 41 | { |
| 42 | for (auto& option : options) |
| 43 | { |
| 44 | AddOption(option.second, option.first, option.second == defaultValue); |
| 45 | } |
| 46 | CheckDefaultKeyValue(); |
| 47 | } |
| 48 | |
| 49 | HtmlTagSelect::HtmlTagSelect(const std::string& name) |
| 50 | : HtmlTag("div"), |
| 51 | commandID("s_" + name), |
| 52 | defaultKeyValueFound(false) |
| 53 | { |
| 54 | AddId(commandID + "_container"); |
| 55 | AddClass("dropdowncontainer"); |
| 56 | |
| 57 | HtmlTagInputHidden hidden(name, ""); |
| 58 | hidden.AddId(commandID); |
| 59 | AddChildTag(hidden); |
| 60 | |
| 61 | HtmlTagInputText text("skip_" + commandID); |
| 62 | text.AddId("skip_" + commandID); |
| 63 | text.AddAttribute("onclick", "toggleClass('d_" + commandID + "', 'show'); return false;"); |
| 64 | text.AddAttribute("readonly"); |
| 65 | AddChildTag(text); |
| 66 | |
| 67 | HtmlTag dropDown("div"); |
| 68 | dropDown.AddClass("dropdown"); |
| 69 | dropDown.AddId("d_" + commandID); |
| 70 | AddChildTag(dropDown); |
| 71 | } |
| 72 | |
| 73 | HtmlTag HtmlTagSelect::AddAttribute(const std::string& name, const std::string& value) |
| 74 | { |
| 75 | childTags[0].AddAttribute(name, value); |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | void HtmlTagSelect::AddOption(const std::string& key, |
| 80 | const std::string& value, |
| 81 | const bool defaultKeyValue) |
| 82 | { |
| 83 | defaultKeyValueFound |= defaultKeyValue; |
| 84 | if (defaultKeyValue || defaultKey.length() == 0) |
| 85 | { |
| 86 | defaultKey = key; |
| 87 | defaultValue = value; |
| 88 | } |
| 89 | |
| 90 | HtmlTag optionTag("div"); |
| 91 | optionTag.AddAttribute("key", key); |
| 92 | optionTag.AddClass("dropdownentry"); |
| 93 | optionTag.AddContent(value); |
| 94 | if (defaultKeyValue) |
| 95 | { |
| 96 | SetDefaultKeyValue(key, value); |
| 97 | optionTag.AddClass("selected_option"); |
| 98 | } |
| 99 | optionTag.AddAttribute("onclick", "selectValue('" + key + "', '" + value + "', '" + commandID + "');"); |
| 100 | childTags[2].AddChildTag(optionTag); |
| 101 | } |
| 102 | |
| 103 | void HtmlTagSelect::CheckDefaultKeyValue() |
| 104 | { |
| 105 | if (defaultKeyValueFound) |
| 106 | { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | SetDefaultKeyValue(defaultKey, defaultValue); |
| 111 | } |
| 112 | }} // namespace Server::Web |