Halyul/hexo-theme-mdui

View on GitHub
layout/_partials/js/archives.ejs

Summary

Maintainability
Test Coverage
<script type="text/javascript">
  themeRuntime.scriptsMap.archives = [];
  (function() {
    var fabTopFlag = false;
    var fabEl = document.getElementById('index-fab');
    function fabShowHide() {
      var bodyScrolled = document.documentElement.scrollTop || document.body.scrollTop;
      if (bodyScrolled > 0 && !fabTopFlag) {
        fabEl.classList.remove('mdui-fab-hide');
        fabTopFlag = true;
      } else if (bodyScrolled === 0 && fabTopFlag) {
        fabEl.classList.add('mdui-fab-hide');
        fabTopFlag = false;
      }
    }
    fabShowHide()
    window.addEventListener('scroll', fabShowHide);
    var listener = {
      function: fabShowHide,
      event: 'scroll',
      el: window,
      removed: false
    }
    themeRuntime.scriptsMap.archives.push(listener)
  })();

  (function() {
    var pageTotal = <%= page.total %>
    var pageNext = 2
    themeRuntime.temp = {
      index: 1
    }
    function morePosts() {
      var screenHeight = window.innerHeight;
      var compareHeight = document.body.clientHeight -  (document.documentElement.scrollTop || document.body.scrollTop);
      var loadStatus = pageNext - themeRuntime.temp.index
      if (screenHeight >= compareHeight && loadStatus === 1) {
        if (pageNext <= pageTotal) {
          loadProgress(false)
          var url = window.location.href + "<%= config.pagination_dir %>/" + pageNext
          themeRuntime.temp.index = pageNext;
          //console.log(pageTotal, url)
          fetch(url, {
              method:'GET',
              mode:'cors',
              credentials: 'include'
          })
          .then(function(response) {
            return response.text()
          }).then(function(HTML) {
            var fakeDom = document.createElement('html');
            fakeDom.innerHTML = HTML
            var morePostsHTML = fakeDom.querySelector('.theme-posts').innerHTML
            var postsArea = document.querySelector(".theme-posts")
            loadProgress(true)
            postsArea.insertAdjacentHTML('beforeend', morePostsHTML)
            pageNext++;
          }).catch(function(error) {
            mdui.alert(error, "<%= __('error.title') %>");
          })
        }
      }
    }
    window.addEventListener('scroll', morePosts);
    var listener = {
      function: morePosts,
      event: 'scroll',
      el: window,
      removed: false
    }
    themeRuntime.scriptsMap.archives.push(listener)
  })();
</script>
<script>
  var content_flag = false;
  const appbar = document.querySelector('.theme-appbar')
  const body = document.querySelector('body')
  const title = document.querySelector('.theme-toolbar-bottom__title')
  window.addEventListener('scroll', styleScrolled)
  var listener = {
    function: styleScrolled,
    event: 'scroll',
    el: window,
    removed: false
  }
  themeRuntime.scriptsMap.archives.push(listener)

  function styleScrolled() {
    var scrollTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
    var divTop = document.querySelector('div#contentLocation').offsetTop
    var contentLocation = divTop - scrollTop - 128;
    if (!content_flag && contentLocation < 0 && window.innerWidth < 860) {
      body.classList.remove('theme-archive--style-change');
      content_flag = true;
    } else if (content_flag && contentLocation >= 0 && window.innerWidth < 860) {
      body.classList.add('theme-archive--style-change');
      content_flag = false;
    };
  }
</script>