-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtoc-controller.js
112 lines (95 loc) · 3.72 KB
/
toc-controller.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) 2008-2015 Haulmont. All rights reserved.
* Use is subject to license terms, see http://www.cuba-platform.com/license for details.
*/
function isPc() {
var userAgent = navigator.userAgent.toLowerCase();
var notPcOsArr = ["ipad", "iphone", "ipod", "windows phone", "phone", "android"];
for (var i = 0; i < notPcOsArr.length; i++) {
if (userAgent.indexOf(notPcOsArr[i]) >= 0) {
return false;
}
}
return true;
}
$(document).ready(function () {
$(".book").append('<div id="smallDeviceIndicator" style="display: none">');
$('#toc .sectlevel1').treeview({
collapsed: true,
animated: "medium",
persist: "location",
unique: false,
});
var closePanel = $("#treecontrol");
var tocMarker = $("#toc-position-marker");
var book = $(".book");
var tocPanel = $("#toc");
closePanel.bind('click', function (e) {
e.preventDefault();
tocPanel.addClass('toc-collapsed');
book.addClass('toc-collapsed');
tocMarker.addClass('toc-collapsed');
});
tocMarker.bind('click', function (e) {
e.preventDefault();
tocPanel.removeClass('toc-collapsed');
book.removeClass('toc-collapsed');
tocMarker.removeClass('toc-collapsed');
});
if (isPc()) {
var highlightedTocLink;
var highlightEnabled = true;
$(window).resize(function () {
//css media query will set #smallDeviceIndicator float to "left" for small screens
//we had to do this because of browser width measurement issues.
highlightEnabled = $("#smallDeviceIndicator").css("float") == "right";
});
$(window).scroll(function () {
if (!highlightEnabled) return;
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function () {
var markerOffset = tocMarker.offset();
var nearestLink = $.nearest({
x: markerOffset.left,
y: markerOffset.top
}, 'h2 > a, h3 > a, h4 > a, h5 > a, h6 > a')[0];
highlightTocLink(nearestLink);
}, 500));
});
function highlightTocLink(nearestLink) {
if (highlightedTocLink != undefined && highlightedTocLink.href == nearestLink.href) return;
if (highlightedTocLink != undefined) {
highlightedTocLink.removeClass("toc-highlighted")
}
highlightedTocLink = $("#toc a[href='" + $(nearestLink).attr("href") + "']");
if (highlightedTocLink != undefined) {
highlightedTocLink.addClass("toc-highlighted")
var openedNodesDivs = $('div.hitarea', '.treeview').filter('.collapsable-hitarea');
var nodesDivsToBeOpened = $(highlightedTocLink).parents('li').children('div.hitarea');
var nodesDivsToBeClosed = $.map(openedNodesDivs, function (val) {
if ($.inArray(val, nodesDivsToBeOpened) === -1) {
return val;
} else {
return null;
}
});
closeTocNodes($(nodesDivsToBeClosed));
openTocNodes(nodesDivsToBeOpened);
}
}
function closeTocNodes(divs) {
divs.each(function () {
if ($(this).hasClass('collapsable-hitarea')) {
$(this).click();
}
});
}
function openTocNodes(divs) {
divs.each(function () {
if ($(this).hasClass('expandable-hitarea')) {
$(this).click();
}
});
}
}
});