Files
Dreierpack-Manager/src/modern/ui/news_controls.inc
T

303 lines
11 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
void App::reset_news(bool preserve_view) {
if (!preserve_view) {
news_view_ = {{"folder", "inbox"}, {"query", ""}, {"feedback", ""}};
news_world_.reset();
news_source_ = nullptr;
} else
news_view_["feedback"] = "";
news_dialog_.clear();
news_return_focus_.clear();
news_undo_ = Json();
news_undo_revision_ = 0;
news_scroll_ = 0;
news_restore_scroll_ = false;
}
void App::news_before_render() {
if (menu_ != "news") {
if (last_rendered_menu_ == "news")
reset_news(true);
return;
}
if (last_rendered_menu_ != "news")
return;
if (auto *feed = element("news-feed"))
news_scroll_ = feed->GetScrollTop();
news_restore_scroll_ = true;
if (focus_after_render_.empty()) {
if (auto *focused = context_->GetFocusElement();
focused && (focused->GetId().starts_with("news-") ||
focused->GetId() == "window-close"))
focus_after_render_ = focused->GetId();
}
}
void App::news_after_render() {
if (menu_ != "news")
return;
context_->Update();
if (news_restore_scroll_) {
if (auto *feed = element("news-feed"))
feed->SetScrollTop(news_scroll_);
news_restore_scroll_ = false;
}
if (!news_dialog_.empty()) {
Rml::ElementList nodes;
doc_->QuerySelectorAll(nodes, "#news-underlay button,#news-underlay "
"input,.news-tablet>.titlebar button");
for (auto *node : nodes) {
node->SetProperty("pointer-events", "none");
node->SetProperty("tab-index", "none");
}
}
}
void App::news_update_feed() {
const auto s = snap();
const auto data = demo_news_view(
*s.world, s.state, news_view_.value("folder", "inbox"),
news_view_.value("query", ""),
ui_preferences_.at("display").value("language", std::string("de")));
if (!data.value("ok", false))
return;
if (auto *feed = element("news-feed")) {
feed->SetInnerRML(render_news_feed(data));
context_->Update();
feed->SetScrollTop(0);
news_scroll_ = 0;
}
if (auto *feedback = element("news-feedback"))
feedback->SetInnerRML(esc(
std::to_string(data.at("items").size()) +
(data.at("items").size() == 1 ? " Nachricht" : " Nachrichten") + " · " +
std::to_string(data.at("shown_unread").get<int>()) + " ungelesen"));
dirty_ = true;
}
bool App::news_commit(const Json &action, const std::string &feedback,
bool remember) {
const auto before = snap();
const auto result = source_->apply({before.revision, "news", action});
if (!result.ok) {
news_view_["feedback"] =
"Änderung nicht gespeichert. Bitte erneut versuchen.";
refresh();
return false;
}
const auto after = snap();
if (remember) {
Json patch = Json::array();
for (const auto &old : before.state.at("news").at("items")) {
const auto &items = after.state.at("news").at("items");
const auto found =
std::find_if(items.begin(), items.end(), [&](const auto &m) {
return m.at("id") == old.at("id");
});
if (found != items.end() && (old.at("read") != found->at("read") ||
old.at("folder") != found->at("folder")))
patch.push_back(
{{"id", old.at("id")},
{"before",
{{"read", old.at("read")}, {"folder", old.at("folder")}}},
{"after",
{{"read", found->at("read")}, {"folder", found->at("folder")}}}});
}
if (!patch.empty())
news_undo_ = std::move(patch);
}
// An article read can leave a prior undo patch usable. Its compare-before
// guard still rejects a changed record, while the current revision binds UI.
news_undo_revision_ = after.revision;
news_view_["feedback"] = feedback;
refresh();
return true;
}
bool App::dispatch_news(const std::string &action, Rml::Element *target) {
if (!action.starts_with("news-"))
return false;
if (menu_ != "news" || !popup_.empty() || amount_)
return true;
const auto id =
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
if (action == "news-detail-close") {
news_dialog_.clear();
focus_after_render_ =
news_return_focus_.empty()
? "news-tab-" + news_view_.value("folder", "inbox")
: news_return_focus_;
news_return_focus_.clear();
refresh();
return true;
}
if (!news_dialog_.empty() && action != "news-detail-move")
return true;
if (action == "news-folder") {
if (id != "inbox" && id != "archive")
return true;
news_view_["folder"] = id;
news_view_["query"] = "";
news_view_["feedback"] = "";
news_scroll_ = 0;
if (auto *feed = element("news-feed"))
feed->SetScrollTop(0);
focus_after_render_ = "news-tab-" + id;
refresh();
return true;
}
const auto before = snap();
if (action == "news-undo") {
if (!news_undo_.is_array() || news_undo_.empty())
return true;
if (news_world_ != before.world || news_source_ != source_.get() ||
news_undo_revision_ != before.revision) {
news_undo_ = Json();
news_view_["feedback"] = "Die Nachricht wurde inzwischen geändert. "
"Rückgängig ist nicht mehr möglich.";
focus_after_render_ = "news-tab-" + news_view_.value("folder", "inbox");
refresh();
return true;
}
if (news_commit({{"kind", "undo"}, {"patch", news_undo_}},
"Letzte Änderung rückgängig gemacht.", false))
news_undo_ = Json();
focus_after_render_ = "news-tab-" + news_view_.value("folder", "inbox");
return true;
}
if (action == "news-read-all") {
const auto folder = news_view_.value("folder", "inbox");
news_commit(
{{"kind", "read-all"}, {"folder", folder}},
"Alle Nachrichten " +
std::string(folder == "inbox" ? "im Postfach" : "im Archiv") +
" als gelesen markiert.");
focus_after_render_ = "news-tab-" + folder;
return true;
}
const auto requested = action == "news-detail-move" ? news_dialog_ : id;
const auto &items = before.state.at("news").at("items");
const auto found =
std::find_if(items.begin(), items.end(), [&](const auto &m) {
return m.at("id") == requested && m.at("folder") != "trash";
});
if (found == items.end())
return true;
if (action == "news-open") {
if (!found->at("read").get<bool>() &&
!news_commit({{"kind", "read"}, {"id", requested}}, "", false))
return true;
news_dialog_ = requested;
news_return_focus_ = "news-read-" + requested;
focus_after_render_ = "news-detail-close";
refresh();
return true;
}
if (action == "news-move" || action == "news-delete" ||
action == "news-detail-move") {
const auto folder = action == "news-delete" ? "trash"
: found->at("folder") == "archive" ? "inbox"
: "archive";
const auto feedback = std::string(folder) == "trash" ? "Nachricht gelöscht."
: std::string(folder) == "inbox"
? "Ins Postfach verschoben."
: "Nachricht archiviert.";
if (news_commit({{"kind", "move"}, {"id", requested}, {"folder", folder}},
feedback)) {
news_dialog_.clear();
news_return_focus_.clear();
focus_after_render_ = "news-tab-" + news_view_.value("folder", "inbox");
}
return true;
}
return true;
}
bool App::news_event(Rml::Event &event) {
if (menu_ != "news" || !popup_.empty() || amount_)
return false;
const auto type = event.GetType();
auto *target = event.GetTargetElement();
const auto key = type == "keydown"
? Rml::Input::KeyIdentifier(
event.GetParameter<int>("key_identifier", 0))
: Rml::Input::KI_UNKNOWN;
if (!news_dialog_.empty()) {
if (key == Rml::Input::KI_ESCAPE ||
(type == "mouseup" && event.GetParameter<int>("button", 0) == 1)) {
dispatch_news("news-detail-close", nullptr);
event.StopImmediatePropagation();
return true;
}
bool inside = false;
for (auto *n = target; n && n != doc_; n = n->GetParentNode())
if (n->GetId() == "news-detail-layer")
inside = true;
if (!inside && (type == "click" || type == "dblclick" ||
type == "mousedown" || type == "mouseup" ||
(type == "keydown" && key != Rml::Input::KI_TAB))) {
event.StopImmediatePropagation();
return true;
}
}
if ((type == "input" || type == "change") && target &&
target->GetId() == "news-search" && news_dialog_.empty()) {
if (auto *input = dynamic_cast<Rml::ElementFormControl *>(target)) {
auto query = input->GetValue();
if (query.size() > 120)
query.resize(120);
news_view_["query"] = query;
news_view_["feedback"] = "";
news_update_feed();
}
event.StopImmediatePropagation();
return true;
}
if (type == "keydown" && news_dialog_.empty() && target &&
target->GetId().starts_with("news-tab-") &&
(key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT ||
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
const auto next = key == Rml::Input::KI_HOME ? "inbox"
: key == Rml::Input::KI_END ? "archive"
: news_view_.value("folder", "inbox") == "inbox"
? "archive"
: "inbox";
dispatch_news("news-folder", element("news-tab-" + std::string(next)));
event.StopImmediatePropagation();
return true;
}
if (key == Rml::Input::KI_TAB) {
Rml::ElementList controls;
if (auto *root = element(news_dialog_.empty() ? "news-underlay"
: "news-detail-layer"))
root->QuerySelectorAll(controls, "button,input");
if (news_dialog_.empty())
if (auto *close = element("window-close"))
controls.insert(controls.begin(), close);
controls.erase(
std::remove_if(controls.begin(), controls.end(),
[](auto *n) {
return !n->IsVisible(true) ||
n->HasAttribute("disabled") ||
n->template GetAttribute<Rml::String>(
"tabindex", "") == "-1";
}),
controls.end());
if (!controls.empty()) {
const bool backwards = event.GetParameter<int>("shift_key", 0) != 0;
const auto it = std::find(controls.begin(), controls.end(),
context_->GetFocusElement());
const int old = it == controls.end() ? (backwards ? 0 : -1)
: int(it - controls.begin());
auto *next =
controls[size_t((old + (backwards ? -1 : 1) + int(controls.size())) %
int(controls.size()))];
next->Focus(true);
for (auto *n = next; n && n != doc_; n = n->GetParentNode())
if (n->GetId().starts_with("news-row-")) {
n->ScrollIntoView(
Rml::ScrollIntoViewOptions(Rml::ScrollAlignment::Nearest));
break;
}
}
event.StopImmediatePropagation();
dirty_ = true;
return true;
}
return false;
}