Welcome to mirror list, hosted at ThFree Co, Russian Federation.

toc-collapse.js « js « static - github.com/AmazingRise/hugo-theme-diary.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9ffe1ef7b5735ec10d5ed815c96b0437ea3060d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var spy = function () {
  var elems = $(":header");
  if (elems.length == 0) {
    return;
  }
  var currentTop = $(window).scrollTop();
  var currentBottom = $(window).scrollTop() + $(window).height();
  var pageBottom = $('#EOF').offset().top;

  var meetUnread = false
  var currentIndex = elems.length - 1
  elems.each(function (idx) {
    var elemTop = $(this).offset().top;
    var id = $(this).attr('id');
    var navElem = $('#' + id + '-nav');
    if (currentTop + $(this).height() >= elemTop) {
      navElem.addClass('toc-active');
    } else {
      if (meetUnread == false) {
        meetUnread = true
        currentIndex = idx - 1
      }
      navElem.removeClass('toc-active');
    }
  })
  if (currentBottom >= pageBottom) {
    currentIndex = -1
  }
  //console.log(currentIndex);
  //Collapse them
  collapseOthers(currentIndex);
}

var onNavClick = function () {
  spy()
}

var collapseOthers = function (currentIndex) {
  if (currentIndex == -1) {
    $(".collapse").each(function (idx) {
      $(this).collapse("hide");
    });
    return;
  }
  var elems = $(":header");
  //console.log(currentIndex);
  currentId = "#" + elems[currentIndex].id + "-nav";

  $(currentId).parents(".collapse").each(function (idx) {
    $(this).collapse("show");
  });
  $(currentId).parent().next().filter(".collapse").collapse("show");
  $(".collapse").not($(currentId).parents()).not($(currentId).parent().next()).each(function (idx) {
    $(this).collapse("hide");
  });
}

//From https://github.com/lodash/lodash/blob/master/debounce.js
// and https://github.com/lodash/lodash/blob/master/isObject.js