fix: server-render unread drawer static strings
window.require is undefined in the NodeBB 4 webpack client build, so the translateText helper never ran and unread drawer strings rendered as raw [[unread:title]] / [[unread:no-unread-topics]]. Move the static strings into the benchpress template (translated at render time) and have client.js only manage the topic <li> rows.
This commit is contained in:
+23
-33
@@ -159,37 +159,29 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function translateText(el) {
|
|
||||||
if (window.require) {
|
|
||||||
window.require(['translator'], function (translator) {
|
|
||||||
translator.translate(el.textContent, (translated) => {
|
|
||||||
el.textContent = translated;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderUnreadMenu(menuEl, topics) {
|
function renderUnreadMenu(menuEl, topics) {
|
||||||
if (!menuEl) {
|
if (!menuEl) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
menuEl.textContent = '';
|
|
||||||
const relativePath = (window.config && window.config.relative_path) || '';
|
const relativePath = (window.config && window.config.relative_path) || '';
|
||||||
|
|
||||||
function addItem(node) {
|
// remove the loading row and any previously injected topic rows;
|
||||||
const li = document.createElement('li');
|
// the empty-state and footer/divider rows are server-rendered and stay put
|
||||||
li.appendChild(node);
|
const loading = menuEl.querySelector('[data-wg-unread-loading]');
|
||||||
menuEl.appendChild(li);
|
if (loading) {
|
||||||
|
loading.remove();
|
||||||
}
|
}
|
||||||
|
toArray(menuEl.querySelectorAll('.wg-unread-topic')).forEach((li) => {
|
||||||
|
li.remove();
|
||||||
|
});
|
||||||
|
|
||||||
|
const emptyEl = menuEl.querySelector('[data-wg-unread-empty]');
|
||||||
|
const footerEl = menuEl.querySelector('[data-wg-unread-footer]');
|
||||||
|
|
||||||
const list = (topics || []).slice(0, 10);
|
const list = (topics || []).slice(0, 10);
|
||||||
if (!list.length) {
|
if (emptyEl) {
|
||||||
const empty = document.createElement('span');
|
emptyEl.classList.toggle('hidden', list.length > 0);
|
||||||
empty.className = 'dropdown-item disabled';
|
|
||||||
empty.textContent = '[[unread:no-unread-topics]]';
|
|
||||||
addItem(empty);
|
|
||||||
translateText(empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list.forEach((topic) => {
|
list.forEach((topic) => {
|
||||||
@@ -203,19 +195,17 @@
|
|||||||
category.textContent = topic.category.name;
|
category.textContent = topic.category.name;
|
||||||
link.appendChild(category);
|
link.appendChild(category);
|
||||||
}
|
}
|
||||||
addItem(link);
|
|
||||||
|
const li = document.createElement('li');
|
||||||
|
li.className = 'wg-unread-topic';
|
||||||
|
li.appendChild(link);
|
||||||
|
|
||||||
|
if (footerEl) {
|
||||||
|
menuEl.insertBefore(li, footerEl);
|
||||||
|
} else {
|
||||||
|
menuEl.appendChild(li);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const divider = document.createElement('hr');
|
|
||||||
divider.className = 'dropdown-divider';
|
|
||||||
addItem(divider);
|
|
||||||
|
|
||||||
const seeAll = document.createElement('a');
|
|
||||||
seeAll.className = 'dropdown-item wg-unread-item--all';
|
|
||||||
seeAll.href = `${relativePath}/unread`;
|
|
||||||
seeAll.textContent = '[[unread:title]]';
|
|
||||||
addItem(seeAll);
|
|
||||||
translateText(seeAll);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWestgateTopbar() {
|
function getWestgateTopbar() {
|
||||||
|
|||||||
@@ -3,5 +3,7 @@
|
|||||||
<span component="unread/count" class="badge rounded-1 bg-primary {{{ if !unreadCount.topic }}}hidden{{{ end }}}">{unreadCount.topic}</span>
|
<span component="unread/count" class="badge rounded-1 bg-primary {{{ if !unreadCount.topic }}}hidden{{{ end }}}">{unreadCount.topic}</span>
|
||||||
</a>
|
</a>
|
||||||
<ul class="dropdown-menu wg-topbar__dropdown unread-dropdown p-1 shadow" role="menu" data-wg-unread-menu>
|
<ul class="dropdown-menu wg-topbar__dropdown unread-dropdown p-1 shadow" role="menu" data-wg-unread-menu>
|
||||||
<li class="dropdown-item disabled"><i class="fa fa-fw fa-spinner fa-spin" aria-hidden="true"></i> [[unread:title]]</li>
|
<li data-wg-unread-loading class="dropdown-item disabled"><i class="fa fa-fw fa-spinner fa-spin" aria-hidden="true"></i> [[unread:title]]</li>
|
||||||
|
<li data-wg-unread-empty class="hidden"><span class="dropdown-item disabled">[[unread:no-unread-topics]]</span></li>
|
||||||
|
<li data-wg-unread-footer><hr class="dropdown-divider"><a class="dropdown-item wg-unread-item--all" href="{relative_path}/unread">[[unread:title]]</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user