render headline labels on the client

This commit is contained in:
Andrew Dolgov 2021-02-19 15:03:48 +03:00
parent 660a1bbe01
commit 131f34648d
4 changed files with 20 additions and 18 deletions

View File

@ -292,22 +292,17 @@ class Article extends Handler_Protected {
$label = Labels::find_caption($label_id, $_SESSION["uid"]); $label = Labels::find_caption($label_id, $_SESSION["uid"]);
$reply["info-for-headlines"] = array(); $reply["labels-for"] = [];
if ($label) { if ($label) {
foreach ($ids as $id) { foreach ($ids as $id) {
if ($assign) if ($assign)
Labels::add_article($id, $label, $_SESSION["uid"]); Labels::add_article($id, $label, $_SESSION["uid"]);
else else
Labels::remove_article($id, $label, $_SESSION["uid"]); Labels::remove_article($id, $label, $_SESSION["uid"]);
$labels = $this->_get_labels($id, $_SESSION["uid"]); array_push($reply["labels-for"],
["id" => (int)$id, "labels" => $this->_get_labels($id)]);
array_push($reply["info-for-headlines"],
array("id" => $id, "labels" => $this->_format_labels_html($labels)));
} }
} }

View File

@ -216,11 +216,7 @@ class Feeds extends Handler_Protected {
if (!is_array($labels)) $labels = Article::_get_labels($id); if (!is_array($labels)) $labels = Article::_get_labels($id);
$labels_str = "<span class=\"HLLCTR-$id\">"; $line["labels"] = Article::_get_labels($id);
$labels_str .= Article::_format_labels_html($labels);
$labels_str .= "</span>";
$line["labels"] = $labels_str;
if (count($topmost_article_ids) < 3) { if (count($topmost_article_ids) < 3) {
array_push($topmost_article_ids, $id); array_push($topmost_article_ids, $id);

View File

@ -130,6 +130,12 @@ const Article = {
Headlines.toggleUnread(id, 0); Headlines.toggleUnread(id, 0);
}, },
renderLabels: function(id, labels) {
return `<span class="labels" data-labels-for="${id}">${labels.map((label) => `
<span class="label" data-label-id="${label[0]}"
style="color : ${label[2]}; background-color : ${label[3]}">${App.escapeHtml(label[1])}</span>`
).join("")}</span>`;
},
renderEnclosures: function (enclosures) { renderEnclosures: function (enclosures) {
// enclosure list was handled by backend (HOOK_FORMAT_ENCLOSURES) // enclosure list was handled by backend (HOOK_FORMAT_ENCLOSURES)

View File

@ -462,7 +462,7 @@ const Headlines = {
<a class="title" title="${App.escapeHtml(hl.title)}" target="_blank" rel="noopener noreferrer" href="${App.escapeHtml(hl.link)}"> <a class="title" title="${App.escapeHtml(hl.title)}" target="_blank" rel="noopener noreferrer" href="${App.escapeHtml(hl.link)}">
${hl.title}</a> ${hl.title}</a>
<span class="author">${hl.author}</span> <span class="author">${hl.author}</span>
${hl.labels} ${Article.renderLabels(hl.id, hl.labels)}
${hl.cdm_excerpt ? hl.cdm_excerpt : ""} ${hl.cdm_excerpt ? hl.cdm_excerpt : ""}
</span> </span>
@ -527,7 +527,7 @@ const Headlines = {
<span data-article-id="${hl.id}" class="hl-content hlMenuAttach"> <span data-article-id="${hl.id}" class="hl-content hlMenuAttach">
<a class="title" href="${App.escapeHtml(hl.link)}">${hl.title} <span class="preview">${hl.content_preview}</span></a> <a class="title" href="${App.escapeHtml(hl.link)}">${hl.title} <span class="preview">${hl.content_preview}</span></a>
<span class="author">${hl.author}</span> <span class="author">${hl.author}</span>
${hl.labels} ${Article.renderLabels(hl.id, hl.labels)}
</span> </span>
</div> </div>
<span class="feed"> <span class="feed">
@ -1240,11 +1240,16 @@ const Headlines = {
} }
} }
}, },
// TODO: maybe this should cause article to be rendered again, although it might cause flicker etc
onLabelsUpdated: function (data) { onLabelsUpdated: function (data) {
if (data) { if (data) {
data['info-for-headlines'].forEach(function (elem) { data["labels-for"].forEach((row) => {
App.findAll(".HLLCTR-" + elem.id).forEach(function (ctr) { if (this.headlines[row.id]) {
ctr.innerHTML = elem.labels; this.headlines[row.id].labels = row.labels;
}
App.findAll(`span[data-labels-for="${row.id}"]`).forEach((ctr) => {
ctr.innerHTML = Article.renderLabels(row.id, row.labels);
}); });
}); });
} }