Port report menu package and club/player profiles (by Codex)
This commit is contained in:
1 parent
201eef88e4
commit
13461c693a
23 files changed
+2122
-137
No files matched your search
@@ -0,0 +1,275 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
namespace {
|
||||
bool report_picker_menu(const std::string &menu) {
|
||||
return menu == "tables" || menu == "strengths" || menu == "league" ||
|
||||
menu == "other";
|
||||
}
|
||||
bool report_picker_kind(const std::string &kind) {
|
||||
return kind == "country" || kind == "league" || kind == "filter" ||
|
||||
kind == "competition";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<std::pair<std::string, std::string>>
|
||||
App::report_picker_items(const std::string &kind) const {
|
||||
std::vector<std::pair<std::string, std::string>> items;
|
||||
if (!world_ || !report_picker_kind(kind))
|
||||
return items;
|
||||
if (kind == "filter" || kind == "competition") {
|
||||
const std::vector<std::string> captions =
|
||||
kind == "filter"
|
||||
? std::vector<std::string>{"Alle Spiele", "Hinrunde", "Rückrunde",
|
||||
"Heim", "Auswärts"}
|
||||
: std::vector<std::string>{"Meisterschaft", "Champ. League", "EM",
|
||||
"DFB-Pokal", "Pokalsieger", "Hallenmasters",
|
||||
"UEFA-Cup", "WM", "Super-Cup"};
|
||||
for (size_t i = 0; i < captions.size(); ++i)
|
||||
items.emplace_back(std::to_string(i), captions[i]);
|
||||
return items;
|
||||
}
|
||||
if (kind == "country") {
|
||||
std::set<std::string> seen;
|
||||
for (const auto *league : world_->leagues()) {
|
||||
const auto nation = league->value("nation", "");
|
||||
if (!nation.empty() && seen.insert(nation).second)
|
||||
items.emplace_back(nation, world_->nation_name(nation));
|
||||
}
|
||||
const std::array<std::string, 5> gold_order = {
|
||||
"Deutschland", "Spanien", "Italien", "Frankreich", "England"};
|
||||
const auto rank = [&](const std::string &caption) {
|
||||
return std::find(gold_order.begin(), gold_order.end(), caption) -
|
||||
gold_order.begin();
|
||||
};
|
||||
std::stable_sort(items.begin(), items.end(), [&](const auto &a, const auto &b) {
|
||||
const auto first = rank(a.second), second = rank(b.second);
|
||||
return first != second ? first < second : a.second < b.second;
|
||||
});
|
||||
return items;
|
||||
}
|
||||
const auto reports = snap().state.at("reports");
|
||||
const auto *league = world_->find("competitions", reports.at("league").get<std::string>());
|
||||
if (league)
|
||||
for (const auto *candidate : world_->leagues(league->value("nation", "")))
|
||||
items.emplace_back(candidate->value("id", ""), world_->name(*candidate));
|
||||
return items;
|
||||
}
|
||||
|
||||
std::string App::report_picker_value(const std::string &kind) const {
|
||||
if (!world_ || !report_picker_kind(kind))
|
||||
return {};
|
||||
const auto reports = snap().state.at("reports");
|
||||
if (kind == "filter" || kind == "competition")
|
||||
return std::to_string(reports.at(kind).get<int>());
|
||||
const auto league = reports.at("league").get<std::string>();
|
||||
if (kind == "league")
|
||||
return league;
|
||||
const auto *row = world_->find("competitions", league);
|
||||
return row ? row->value("nation", "") : "";
|
||||
}
|
||||
|
||||
std::string App::report_picker(const std::string &kind, double left,
|
||||
double width) {
|
||||
if (!report_picker_menu(menu_) || !report_picker_kind(kind))
|
||||
return {};
|
||||
const auto items = report_picker_items(kind);
|
||||
const auto selected = report_picker_value(kind);
|
||||
std::string caption = "—";
|
||||
for (const auto &[value, label] : items)
|
||||
if (value == selected)
|
||||
caption = label;
|
||||
const bool open = report_picker_ == kind;
|
||||
const std::string menu_label = kind == "country" ? "Land"
|
||||
: kind == "league" ? "Liga"
|
||||
: kind == "filter" ? "Spiele" : "Wettbewerb";
|
||||
std::string out =
|
||||
"<div class=\"report-picker-wrapper\" data-report-picker=\"" + kind +
|
||||
"\" style=\"left:" + scalar(left) + "dp;width:" + scalar(width) + "dp;\">" +
|
||||
btn("report-picker", "<span class=\"report-picker-label\">" + esc(caption) +
|
||||
"</span><span class=\"report-picker-arrow\" aria-hidden=\"true\">"
|
||||
"<span></span></span>",
|
||||
"report-picker-trigger",
|
||||
"id=\"report-picker-" + kind + "\" data-kind=\"" + kind +
|
||||
"\" aria-haspopup=\"menu\" aria-expanded=\"" +
|
||||
(open ? "true" : "false") +
|
||||
"\" aria-controls=\"report-picker-options-" + kind + "\" " +
|
||||
(items.empty() ? "disabled" : "")) +
|
||||
"<div class=\"report-picker-popup\" id=\"report-picker-options-" + kind +
|
||||
"\" role=\"menu\" aria-label=\"" + menu_label + "\" style=\"display:" +
|
||||
(open ? "block" : "none") + ";\">";
|
||||
for (size_t i = 0; i < items.size(); ++i) {
|
||||
const bool checked = items[i].first == selected;
|
||||
const auto marker =
|
||||
"<span class=\"report-picker-check-slot\" aria-hidden=\"true\">" +
|
||||
std::string(checked ? "<span class=\"report-picker-check\"></span>" : "") +
|
||||
"</span>";
|
||||
out += btn("report-choice", marker + "<span class=\"report-picker-option-label\">" +
|
||||
esc(items[i].second) + "</span>",
|
||||
"report-picker-option",
|
||||
"id=\"report-option-" + kind + "-" + std::to_string(i) +
|
||||
"\" data-kind=\"" + kind + "\" data-value=\"" +
|
||||
esc(items[i].first) + "\" data-index=\"" + std::to_string(i) +
|
||||
"\" role=\"menuitemradio\" aria-checked=\"" +
|
||||
(checked ? "true" : "false") + "\" style=\"tab-index:none;\"");
|
||||
}
|
||||
return out + "</div></div>";
|
||||
}
|
||||
|
||||
void App::close_report_picker(bool focus) {
|
||||
if (report_picker_.empty())
|
||||
return;
|
||||
const auto kind = report_picker_;
|
||||
report_picker_.clear();
|
||||
if (auto *panel = element("report-picker-options-" + kind))
|
||||
panel->SetProperty("display", "none");
|
||||
auto *trigger = element("report-picker-" + kind);
|
||||
if (trigger)
|
||||
trigger->SetAttribute("aria-expanded", "false");
|
||||
// Commit visibility before the parent Tab trap examines ancestor visibility.
|
||||
// No node is replaced, so an outside mouse event retains its original target.
|
||||
context_->Update();
|
||||
if (focus && trigger)
|
||||
trigger->Focus(keyboard_navigation_);
|
||||
dirty_ = true;
|
||||
}
|
||||
|
||||
bool App::report_picker_action(const std::string &action, Rml::Element *target) {
|
||||
if (action != "report-picker" && action != "report-choice")
|
||||
return false;
|
||||
if (!report_picker_menu(menu_) || !target)
|
||||
return true;
|
||||
const auto kind = target->GetAttribute<Rml::String>("data-kind", "");
|
||||
if (!report_picker_kind(kind))
|
||||
return true;
|
||||
const auto items = report_picker_items(kind);
|
||||
if (items.empty())
|
||||
return true;
|
||||
if (action == "report-picker") {
|
||||
if (report_picker_ == kind) {
|
||||
close_report_picker(true);
|
||||
return true;
|
||||
}
|
||||
close_report_picker(false);
|
||||
report_picker_ = kind;
|
||||
const auto selected = report_picker_value(kind);
|
||||
const auto found = std::find_if(items.begin(), items.end(), [&](const auto &item) {
|
||||
return item.first == selected;
|
||||
});
|
||||
const auto index = found == items.end() ? 0 : found - items.begin();
|
||||
focus_after_render_ = "report-option-" + kind + "-" + std::to_string(index);
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
if (report_picker_ != kind)
|
||||
return true;
|
||||
const auto value = target->GetAttribute<Rml::String>("data-value", "");
|
||||
if (std::none_of(items.begin(), items.end(), [&](const auto &item) {
|
||||
return item.first == value;
|
||||
}))
|
||||
return true;
|
||||
const Json selection = kind == "filter" || kind == "competition"
|
||||
? Json(std::stoi(value)) : Json(value);
|
||||
close_report_picker(false);
|
||||
// Commit even an unchanged country: the Gold resets its first league.
|
||||
apply("reports", {{"kind", kind}, {"value", selection}});
|
||||
focus_after_render_ = "report-picker-" + kind;
|
||||
refresh();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool App::report_picker_event(Rml::Event &event) {
|
||||
if (!report_picker_menu(menu_))
|
||||
return false;
|
||||
const auto type = event.GetType();
|
||||
const auto ancestor = [&](const std::string &attribute) -> Rml::Element * {
|
||||
for (auto *node = event.GetTargetElement(); node && node != doc_;
|
||||
node = node->GetParentNode())
|
||||
if (node->HasAttribute(attribute))
|
||||
return node;
|
||||
return nullptr;
|
||||
};
|
||||
if (!report_picker_.empty()) {
|
||||
// Defer the right-button dismissal to mouseup so the same gesture cannot
|
||||
// also fall through to the menu's right-button close handler.
|
||||
if ((type == "mousedown" || type == "mouseup") &&
|
||||
event.GetParameter<int>("button", 0) == 1) {
|
||||
if (type == "mouseup")
|
||||
close_report_picker(true);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (type == "contextmenu") {
|
||||
close_report_picker(true);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (type == "mousedown") {
|
||||
auto *wrapper = ancestor("data-report-picker");
|
||||
if (!wrapper || wrapper->GetAttribute<Rml::String>("data-report-picker", "") !=
|
||||
report_picker_)
|
||||
close_report_picker(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (type != "keydown")
|
||||
return false;
|
||||
const auto key = Rml::Input::KeyIdentifier(
|
||||
event.GetParameter<int>("key_identifier", 0));
|
||||
if (!report_picker_.empty()) {
|
||||
if (key == Rml::Input::KI_ESCAPE) {
|
||||
keyboard_navigation_ = true;
|
||||
close_report_picker(true);
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
if (key == Rml::Input::KI_TAB) {
|
||||
keyboard_navigation_ = true;
|
||||
close_report_picker(true);
|
||||
return false;
|
||||
}
|
||||
if (key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN ||
|
||||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END) {
|
||||
const auto items = report_picker_items(report_picker_);
|
||||
if (items.empty())
|
||||
return false;
|
||||
int current = -1;
|
||||
for (auto *node = context_->GetFocusElement(); node && node != doc_;
|
||||
node = node->GetParentNode())
|
||||
if (node->HasAttribute("data-index") &&
|
||||
node->GetAttribute<Rml::String>("data-kind", "") == report_picker_) {
|
||||
current = node->GetAttribute<int>("data-index", -1);
|
||||
break;
|
||||
}
|
||||
const int count = int(items.size());
|
||||
const int next = key == Rml::Input::KI_HOME ? 0
|
||||
: key == Rml::Input::KI_END ? count - 1
|
||||
: (current + (key == Rml::Input::KI_UP ? -1 : 1) + count) % count;
|
||||
if (auto *node = element("report-option-" + report_picker_ + "-" +
|
||||
std::to_string(next))) {
|
||||
keyboard_navigation_ = true;
|
||||
node->Focus(true);
|
||||
node->ScrollIntoView({Rml::ScrollAlignment::Nearest});
|
||||
dirty_ = true;
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
// Enter/Space use the shared native button click handler, without a draft mutation.
|
||||
return false;
|
||||
}
|
||||
auto *trigger = ancestor("data-kind");
|
||||
if (!trigger || trigger->GetAttribute<Rml::String>("data-action", "") !=
|
||||
"report-picker" ||
|
||||
(key != Rml::Input::KI_UP && key != Rml::Input::KI_DOWN))
|
||||
return false;
|
||||
const auto kind = trigger->GetAttribute<Rml::String>("data-kind", "");
|
||||
keyboard_navigation_ = true;
|
||||
report_picker_action("report-picker", trigger);
|
||||
if (key == Rml::Input::KI_UP && report_picker_ == kind) {
|
||||
const auto items = report_picker_items(kind);
|
||||
if (!items.empty())
|
||||
focus_after_render_ = "report-option-" + kind + "-" +
|
||||
std::to_string(items.size() - 1);
|
||||
}
|
||||
event.StopImmediatePropagation();
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user