Passed
Push — master ( 3b5e35...c2637c )
by Zhenyu
01:45
created

site.js ➔ loadState   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
nop 1
1
/* global anchors */
2
3
// add anchor links to headers
4
anchors.options.placement = 'left';
5
anchors.add('h3');
6
7
// Filter UI
8
var tocElements = document.getElementById('toc').getElementsByTagName('li');
9
10
document.getElementById('filter-input').addEventListener('keyup', function(e) {
11
  var i, element, children;
12
13
  // enter key
14
  if (e.keyCode === 13) {
15
    // go to the first displayed item in the toc
16
    for (i = 0; i < tocElements.length; i++) {
17
      element = tocElements[i];
18
      if (!element.classList.contains('display-none')) {
19
        location.replace(element.firstChild.href);
20
        return e.preventDefault();
21
      }
22
    }
23
  }
24
25
  var match = function() {
26
    return true;
27
  };
28
29
  var value = this.value.toLowerCase();
30
31
  if (!value.match(/^\s*$/)) {
32
    match = function(element) {
33
      var html = element.firstChild.innerHTML;
34
      return html && html.toLowerCase().indexOf(value) !== -1;
35
    };
36
  }
37
38
  for (i = 0; i < tocElements.length; i++) {
39
    element = tocElements[i];
40
    children = Array.from(element.getElementsByTagName('li'));
41
    if (match(element) || children.some(match)) {
0 ignored issues
show
Bug introduced by
The call to match seems to have too many arguments starting with element.
Loading history...
42
      element.classList.remove('display-none');
43
    } else {
44
      element.classList.add('display-none');
45
    }
46
  }
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
47
});
48
49
var items = document.getElementsByClassName('toggle-sibling');
50
for (var j = 0; j < items.length; j++) {
51
  items[j].addEventListener('click', toggleSibling);
52
}
53
54
function toggleSibling() {
55
  var stepSibling = this.parentNode.getElementsByClassName('toggle-target')[0];
56
  var icon = this.getElementsByClassName('icon')[0];
57
  var klass = 'display-none';
58
  if (stepSibling.classList.contains(klass)) {
59
    stepSibling.classList.remove(klass);
60
    icon.innerHTML = '▾';
61
  } else {
62
    stepSibling.classList.add(klass);
63
    icon.innerHTML = '▸';
64
  }
65
}
66
67
function showHashTarget(targetId) {
68
  if (targetId) {
69
    var hashTarget = document.getElementById(targetId);
70
    // new target is hidden
71
    if (
72
      hashTarget &&
73
      hashTarget.offsetHeight === 0 &&
74
      hashTarget.parentNode.parentNode.classList.contains('display-none')
75
    ) {
76
      hashTarget.parentNode.parentNode.classList.remove('display-none');
77
    }
78
  }
79
}
80
81
function scrollIntoView(targetId) {
82
  // Only scroll to element if we don't have a stored scroll position.
83
  if (targetId && !history.state) {
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
84
    var hashTarget = document.getElementById(targetId);
85
    if (hashTarget) {
86
      hashTarget.scrollIntoView();
87
    }
88
  }
89
}
90
91
function gotoCurrentTarget() {
92
  showHashTarget(location.hash.substring(1));
93
  scrollIntoView(location.hash.substring(1));
94
}
95
96
window.addEventListener('hashchange', gotoCurrentTarget);
97
gotoCurrentTarget();
98
99
var toclinks = document.getElementsByClassName('pre-open');
100
for (var k = 0; k < toclinks.length; k++) {
101
  toclinks[k].addEventListener('mousedown', preOpen, false);
102
}
103
104
function preOpen() {
105
  showHashTarget(this.hash.substring(1));
106
}
107
108
var split_left = document.querySelector('#split-left');
109
var split_right = document.querySelector('#split-right');
110
var split_parent = split_left.parentNode;
111
var cw_with_sb = split_left.clientWidth;
112
split_left.style.overflow = 'hidden';
113
var cw_without_sb = split_left.clientWidth;
114
split_left.style.overflow = '';
115
116
Split(['#split-left', '#split-right'], {
117
  elementStyle: function(dimension, size, gutterSize) {
118
    return {
119
      'flex-basis': 'calc(' + size + '% - ' + gutterSize + 'px)'
120
    };
121
  },
122
  gutterStyle: function(dimension, gutterSize) {
123
    return {
124
      'flex-basis': gutterSize + 'px'
125
    };
126
  },
127
  gutterSize: 20,
128
  sizes: [33, 67]
129
});
130
131
// Chrome doesn't remember scroll position properly so do it ourselves.
132
// Also works on Firefox and Edge.
133
134
function updateState() {
135
  history.replaceState(
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
136
    {
137
      left_top: split_left.scrollTop,
138
      right_top: split_right.scrollTop
139
    },
140
    document.title
141
  );
142
}
143
144
function loadState(ev) {
145
  if (ev) {
146
    // Edge doesn't replace change history.state on popstate.
147
    history.replaceState(ev.state, document.title);
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
148
  }
149
  if (history.state) {
150
    split_left.scrollTop = history.state.left_top;
151
    split_right.scrollTop = history.state.right_top;
152
  }
153
}
154
155
window.addEventListener('load', function() {
156
  // Restore after Firefox scrolls to hash.
157
  setTimeout(function() {
158
    loadState();
159
    // Update with initial scroll position.
160
    updateState();
161
    // Update scroll positions only after we've loaded because Firefox
162
    // emits an initial scroll event with 0.
163
    split_left.addEventListener('scroll', updateState);
164
    split_right.addEventListener('scroll', updateState);
165
  }, 1);
166
});
167
168
window.addEventListener('popstate', loadState);
169