Skip to content

Node.js based forum software built for the modern web

fix: invoke intents trigger on world page items

Julian Lame7b1d73b4d81
diff --git a/public/src/client/world.js b/public/src/client/world.js
index fb0bcd2ce0..1ad948082c 100644
--- a/public/src/client/world.js
+++ b/public/src/client/world.js
@@ -4,9 +4,11 @@ define('forum/world', [
'forum/infinitescroll', 'search', 'sort', 'hooks',
'alerts', 'api', 'bootbox', 'helpers', 'forum/category/tools',
'translator', 'quickreply', 'handleBack', 'imagesloaded',
+ 'forum/topic/votes',
+ 'modules/intents',
], function (infinitescroll, search, sort, hooks,
alerts, api, bootbox, helpers, categoryTools,
- translator, quickreply, handleBack, imagesLoaded) {
+ translator, quickreply, handleBack, imagesLoaded, votes, intents) {
const World = {};
World.init = function () {
@@ -121,6 +123,8 @@ define('forum/world', [
handleIgnoreWatch(category.cid);
});
+ intents.addHandlers();
+
hooks.fire('action:topics.loaded', { topics: ajaxify.data.topics });
hooks.fire('action:category.loaded', { cid: ajaxify.data.cid });
};
@@ -164,33 +168,19 @@ define('forum/world', [
});
});
- feedEl.on('click', '[data-action="upvote"]', function () {
- const $this = $(this);
- const isUpvoted = $this.attr('data-upvoted') === 'true';
- const pid = $this.attr('data-pid');
- const upvoteCount = parseInt($this.attr('data-upvotes'), 10);
- const method = isUpvoted ? 'del' : 'put';
- const delta = 1;
- api[method](`/posts/${pid}/vote`, { delta }, function (err) {
- if (err) {
- return alerts.error(err);
- }
+ feedEl.on('click', '[data-action="upvote"]', function (e) {
+ e.preventDefault();
+ votes.toggleVote($(this), '.upvoted', 1);
+ return false;
+ });
- const newUpvoteCount = upvoteCount + (isUpvoted ? -1 : 1);
- $this.find('[component="upvote-count"]').text(
- helpers.humanReadableNumber(newUpvoteCount)
- );
- $this.attr('data-upvotes', newUpvoteCount);
- $this.attr('data-upvoted', isUpvoted ? 'false' : 'true');
- $this.find('i').toggleClass('fa text-danger', !isUpvoted)
- .toggleClass('fa-regular text-muted', isUpvoted);
-
- hooks.fire('action:post.toggleVote', {
- pid: pid,
- delta: delta,
- unvote: method === 'del',
- });
- });
+ // Handle UI updates for upvotes in the world feed
+ hooks.onPage('action:post.toggleVote', function (data) {
+ if (data.unvote) {
+ $(`[data-pid="${data.pid}"][data-action="upvote"]`).removeClass('upvoted');
+ } else {
+ $(`[data-pid="${data.pid}"][data-action="upvote"]`).addClass('upvoted');
+ }
});
feedEl.on('click', '[data-action="reply"]', function () {
diff --git a/public/src/modules/intents.js b/public/src/modules/intents.js
index 8e7227361d..ce0c930799 100644
--- a/public/src/modules/intents.js
+++ b/public/src/modules/intents.js
@@ -126,12 +126,16 @@ export function register() {
try {
await refresh(handle);
+ console.log('refresh done');
map = list();
+ console.log('map is now', map);
handles = Array.from(map.entries()).map(([handle, intents]) => ({
handle,
intents: mapIntentNames(intents).join(', '),
}));
+ console.log('handles', handles);
const html = await app.parseAndTranslate('modals/intents/register', 'handles', { handles });
+ console.log('target el', modal.find('#intents-registered-list'));
modal.find('#intents-registered-list').html(html);
} catch (e) {
alerts.error(e.message);
@@ -159,6 +163,7 @@ export function register() {
const INTENTS_GUEST_SELECTORS = '[component="topic/reply/guest"], [component="category/post/guest"]';
+// called by various init scripts in different pages' js to add handlers for "Log in to post" buttons, et al.
export function addHandlers() {
document.removeEventListener('click', _intentsHandler);
document.addEventListener('click', _intentsHandler, true); // capture phase
diff --git a/src/views/partials/feed/item.tpl b/src/views/partials/feed/item.tpl
index daf69dfd44..95b62cf971 100644
--- a/src/views/partials/feed/item.tpl
+++ b/src/views/partials/feed/item.tpl
@@ -65,7 +65,7 @@
<a href="#" data-pid="{./pid}" data-action="bookmark" data-bookmarked="{./bookmarked}" data-bookmarks="{./bookmarks}" class="btn btn-link btn-sm text-body"><i class="fa-fw fa-bookmark {{{ if ./bookmarked }}}fa text-primary{{{ else }}}fa-regular text-muted{{{ end }}}"></i> <span component="bookmark-count">{humanReadableNumber(./bookmarks)}</span></a>
- <a href="#" data-pid="{./pid}" data-action="upvote" data-upvoted="{./upvoted}" data-upvotes="{./upvotes}" class="btn btn-link btn-sm text-body"><i class="fa-fw fa-heart {{{ if ./upvoted }}}fa text-danger{{{ else }}}fa-regular text-muted{{{ end }}}"></i> <span component="upvote-count">{humanReadableNumber(./upvotes)}</span></a>
+ <a href="#" data-pid="{./pid}" data-action="upvote" data-upvoted="{./upvoted}" data-upvotes="{./upvotes}" class="btn btn-link btn-sm text-body{{{ if ./upvoted }}} upvoted{{{ end }}}"><i class="fa-fw fa-heart {{{ if ./upvoted }}}fa text-danger{{{ else }}}fa-regular text-muted{{{ end }}}"></i> <span component="upvote-count">{humanReadableNumber(./upvotes)}</span></a>
<a href="#" data-pid="{./pid}" data-is-main="{./isMainPost}" data-tid="{./tid}" data-action="reply" class="btn btn-link btn-sm text-body"><i class="fa-fw fa fa-reply text-muted"></i> [[topic:reply]]</a>
</div>