Sha256: 7539431ad837a8a52732090bf9f3332672f44ac46e6963cf6187cd2860995fda

Contents?: true

Size: 1.07 KB

Versions: 15

Compression:

Stored size: 1.07 KB

Contents

(($) => {
  const COMPONENT_SELECTOR = '[data-thredded-topics]';

  const TOPIC_SELECTOR = 'article';
  const TOPIC_LINK_SELECTOR = 'h1 a';
  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);
  }

  class ThreddedTopics {
    init($nodes) {
      $nodes.on('click', TOPIC_LINK_SELECTOR, (evt) => {
        const $topic = $(evt.target).closest(TOPIC_SELECTOR);
        if (pageNumber($topic.find('a').prop('href')) == totalPages(+$topic.find(POSTS_COUNT_SELECTOR).text())) {
          $topic.addClass(TOPIC_READ_CLASS).removeClass(TOPIC_UNREAD_CLASS);
        }
      });
    }
  }

  window.Thredded.onPageLoad(() => {
    const $nodes = $(COMPONENT_SELECTOR);
    if ($nodes.length) {
      new ThreddedTopics().init($nodes);
    }
  });
})(jQuery);

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
thredded-0.12.4 app/assets/javascripts/thredded/components/topics.es6
thredded-0.12.3 app/assets/javascripts/thredded/components/topics.es6
thredded-0.12.2 app/assets/javascripts/thredded/components/topics.es6
thredded-0.12.1 app/assets/javascripts/thredded/components/topics.es6
thredded-0.12.0 app/assets/javascripts/thredded/components/topics.es6
thredded-0.11.1 app/assets/javascripts/thredded/components/topics.es6
thredded-0.11.0 app/assets/javascripts/thredded/components/topics.es6
thredded-0.10.1 app/assets/javascripts/thredded/components/topics.es6
thredded-0.10.0 app/assets/javascripts/thredded/components/topics.es6
thredded-0.9.4 app/assets/javascripts/thredded/components/topics.es6
thredded-0.9.3 app/assets/javascripts/thredded/components/topics.es6
thredded-0.9.2 app/assets/javascripts/thredded/components/topics.es6
thredded-0.9.1 app/assets/javascripts/thredded/components/topics.es6
thredded-0.8.4 app/assets/javascripts/thredded/components/topics.es6
thredded-0.8.2 app/assets/javascripts/thredded/components/topics.es6