Sha256: 6f27a4703fde6634333f81f726053cfe12ba158441379b4b6418fe74cf7d7a89

Contents?: true

Size: 1.43 KB

Versions: 21

Compression:

Stored size: 1.43 KB

Contents

//= require thredded/core/on_page_load
//= require thredded/core/serialize_form

// Makes topics in the list appear read as soon as the topic link is clicked,
// iff the topic link leads to the last page of the topic.
(() => {
  const Thredded = window.Thredded;

  const COMPONENT_SELECTOR = '[data-thredded-topics]';
  const TOPIC_UNREAD_CLASS = 'thredded--topic-unread';
  const TOPIC_READ_CLASS = 'thredded--topic-read';
  const POSTS_COUNT_SELECTOR = '.thredded--topics--posts-count';
  const POSTS_PER_PAGE = 50;

  function pageNumber(url) {
    const match = url.match(/\/page-(\d)$/);
    return match ? +match[1] : 1;
  }

  function totalPages(numPosts) {
    return Math.ceil(numPosts / POSTS_PER_PAGE);
  }

  function getTopicNode(node) {
    do {
      node = node.parentNode;
    } while (node && node.tagName !== 'ARTICLE');
    return node;
  }

  function initTopicsList(topicsList) {
    topicsList.addEventListener('click', (evt) => {
      const link = evt.target;
      if (link.tagName !== 'A' || link.parentNode.tagName !== 'H1') return;
      const topic = getTopicNode(link);
      if (pageNumber(link.href) === totalPages(+topic.querySelector(POSTS_COUNT_SELECTOR).textContent)) {
        topic.classList.add(TOPIC_READ_CLASS);
        topic.classList.remove(TOPIC_UNREAD_CLASS);
      }
    });
  }

  Thredded.onPageLoad(() => {
    Array.prototype.forEach.call(document.querySelectorAll(COMPONENT_SELECTOR), initTopicsList);
  });
})();

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
thredded-0.16.0 app/assets/javascripts/thredded/components/topics.es6
thredded-0.15.5 app/assets/javascripts/thredded/components/topics.es6
thredded-0.15.4 app/assets/javascripts/thredded/components/topics.es6
thredded-0.15.3 app/assets/javascripts/thredded/components/topics.es6
thredded-0.15.2 app/assets/javascripts/thredded/components/topics.es6
thredded-0.15.1 app/assets/javascripts/thredded/components/topics.es6
threddedDANIEL-0.14.5 app/assets/javascripts/thredded/components/topics.es6
thredded-0.14.4 app/assets/javascripts/thredded/components/topics.es6
thredded-0.14.3 app/assets/javascripts/thredded/components/topics.es6
thredded-0.14.2 app/assets/javascripts/thredded/components/topics.es6
thredded-0.14.1 app/assets/javascripts/thredded/components/topics.es6
thredded-0.14.0 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.8 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.7 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.6 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.5 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.4 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.3 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.2 app/assets/javascripts/thredded/components/topics.es6
thredded-0.13.1 app/assets/javascripts/thredded/components/topics.es6