From 2de878821b59b8905086bde9e1d1d7ecffa322c7 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 31 Oct 2013 15:02:12 -0400 Subject: [PATCH] addressed #466, dynamicaly loaded topics now have proper delete/lock/pin states --- public/src/forum/admin/topics.js | 40 ++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/public/src/forum/admin/topics.js b/public/src/forum/admin/topics.js index 5750e83f37..fed21cbf67 100644 --- a/public/src/forum/admin/topics.js +++ b/public/src/forum/admin/topics.js @@ -5,6 +5,8 @@ define(function() { var topicsListEl = document.querySelector('.topics'), loadMoreEl = document.getElementById('topics_loadmore'); + this.resolveButtonStates(); + $(topicsListEl).on('click', '[data-action]', function() { var $this = $(this), action = this.getAttribute('data-action'), @@ -58,6 +60,9 @@ define(function() { topicsListEl = document.querySelector('.topics'); topicsListEl.innerHTML += html; + + Topics.resolveButtonStates(); + btnEl.innerHTML = 'Load More Topics'; $('span.timeago').timeago(); } else { @@ -69,18 +74,6 @@ define(function() { } }, false); - // Resolve proper button state for all topics - var topicEls = topicsListEl.querySelectorAll('li'), - numTopics = topicEls.length; - for (var x = 0; x < numTopics; x++) { - if (topicEls[x].getAttribute('data-pinned') === '1') topicEls[x].querySelector('[data-action="pin"]').className += ' active'; - if (topicEls[x].getAttribute('data-locked') === '1') topicEls[x].querySelector('[data-action="lock"]').className += ' active'; - if (topicEls[x].getAttribute('data-deleted') === '1') topicEls[x].querySelector('[data-action="delete"]').className += ' active'; - topicEls[x].removeAttribute('data-pinned'); - topicEls[x].removeAttribute('data-locked'); - topicEls[x].removeAttribute('data-deleted'); - } - socket.on('api:topic.pin', function(response) { if (response.status === 'ok') { var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="pin"]'); @@ -118,6 +111,7 @@ define(function() { var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="delete"]'); $(btnEl).addClass('active'); + $(btnEl).siblings('[data-action="lock"]').addClass('active'); } }); @@ -126,9 +120,31 @@ define(function() { var btnEl = document.querySelector('li[data-tid="' + response.tid + '"] button[data-action="delete"]'); $(btnEl).removeClass('active'); + $(btnEl).siblings('[data-action="lock"]').removeClass('active'); } }); }; + Topics.resolveButtonStates = function() { + // Resolve proper button state for all topics + var topicsListEl = document.querySelector('.topics'), + topicEls = topicsListEl.querySelectorAll('li'), + numTopics = topicEls.length; + for (var x = 0; x < numTopics; x++) { + if (topicEls[x].getAttribute('data-pinned') === '1') { + topicEls[x].querySelector('[data-action="pin"]').className += ' active'; + topicEls[x].removeAttribute('data-pinned'); + } + if (topicEls[x].getAttribute('data-locked') === '1') { + topicEls[x].querySelector('[data-action="lock"]').className += ' active'; + topicEls[x].removeAttribute('data-locked'); + } + if (topicEls[x].getAttribute('data-deleted') === '1') { + topicEls[x].querySelector('[data-action="delete"]').className += ' active'; + topicEls[x].removeAttribute('data-deleted'); + } + } + } + return Topics; }); \ No newline at end of file