1 |
/* |
2 |
CrudeDoc Java Script |
3 |
Copyright (c) 2015 - 2017 Christian Schoenebeck. All rights reserved. |
4 |
http://www.crudebyte.com |
5 |
*/ |
6 |
|
7 |
var g_isTouch = false; |
8 |
var g_isMouse = false; |
9 |
|
10 |
function crdWindowWidth() { |
11 |
return window.innerWidth || |
12 |
document.documentElement.clientWidth || |
13 |
document.getElementsByTagName('body')[0].clientWidth; |
14 |
} |
15 |
|
16 |
function crdWindowHeight() { |
17 |
return window.innerHeight || |
18 |
document.documentElement.clientHeight || |
19 |
document.getElementsByTagName('body')[0].clientHeight; |
20 |
} |
21 |
|
22 |
function crdScrollTo(e) { |
23 |
$('html, body').animate({ |
24 |
scrollTop: $( "#" + $(e).attr("section") ).offset().top - 68 |
25 |
}, 300); |
26 |
var obj = $( "#" + $(e).attr("section") ); |
27 |
$(obj).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(120).fadeOut(100).fadeIn(120); |
28 |
} |
29 |
|
30 |
function crdScrollToHash(h) { |
31 |
if (typeof h === 'undefined') { |
32 |
h = location.hash; |
33 |
if (!h || h.length < 2) return; |
34 |
} |
35 |
$('html, body').animate({ |
36 |
scrollTop: $( h ).offset().top - 68 |
37 |
}, 300); |
38 |
$(h).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(120).fadeOut(100).fadeIn(120); |
39 |
} |
40 |
|
41 |
function crdOnScroll() { |
42 |
//var headers = $("body > article :header:in-viewport"); |
43 |
var headers = $("body > article :header"); |
44 |
var wst = $(window).scrollTop(); |
45 |
if (wst <= 0) { |
46 |
for (var i = 0; i < headers.length; ++i) { |
47 |
$("body > aside *[section]").removeClass("current"); |
48 |
var link = $("body > aside *[section='" + headers[i].id + "']"); |
49 |
$(link).addClass("current"); |
50 |
return; |
51 |
} |
52 |
} |
53 |
var wh = crdWindowHeight(); |
54 |
for (var i = headers.length - 1; i >= 0; --i) { |
55 |
var offset = headers[i].offsetTop - wst + wh/2; |
56 |
if (headers[i].id.length > 0 && (offset <= 0 || offset <= wh)) { |
57 |
$("body > aside *[section]").removeClass("current"); |
58 |
var link = $("body > aside *[section='" + headers[i].id + "']"); |
59 |
$(link).addClass("current"); |
60 |
break; |
61 |
} |
62 |
} |
63 |
} |
64 |
|
65 |
function crdUpdateTOCStatus() { |
66 |
var noTOC = $("html").hasClass("no-toc"); |
67 |
$("nav > .foldbtn a").html( |
68 |
noTOC ? ">>>" : "<<<" |
69 |
); |
70 |
$("nav > .foldbtn a").attr("title", |
71 |
noTOC ? "Show content menu." : "Hide content menu." |
72 |
) |
73 |
} |
74 |
|
75 |
function crdToggleTOC() { |
76 |
$("html").toggleClass("no-toc"); |
77 |
if (typeof(Storage) !== "undefined") { |
78 |
sessionStorage.setItem("show-toc", $("html").hasClass("no-toc") ? "no" : "yes"); |
79 |
} |
80 |
crdUpdateTOCStatus(); |
81 |
} |
82 |
|
83 |
function crdOnWindowResize() { |
84 |
var wh = crdWindowHeight(); |
85 |
$("nav > ul > li ul").css("max-height", wh - 70); |
86 |
} |
87 |
|
88 |
function crdDetectFeatures() { |
89 |
if ('ontouchstart' in document.documentElement) { |
90 |
g_isTouch = true; |
91 |
document.documentElement.className += ' touch'; |
92 |
} else { |
93 |
g_isTouch = false; |
94 |
document.documentElement.className += ' no-touch'; |
95 |
} |
96 |
$(window).bind('mousemove.hasMouse', function(){ |
97 |
$(window).unbind('.hasMouse'); |
98 |
g_isMouse = true; |
99 |
$(document).tooltip(); |
100 |
}).bind('touchstart.hasMouse', function(){ |
101 |
$(window).unbind('.hasMouse'); |
102 |
g_isMouse = false; |
103 |
}); |
104 |
if ($.support["transform"]) { |
105 |
$("body").addClass("hasTransform"); |
106 |
} |
107 |
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; |
108 |
if (iOS) { |
109 |
$("body").addClass("iOS"); |
110 |
} |
111 |
} |
112 |
|
113 |
$(function() { |
114 |
crdDetectFeatures(); |
115 |
$("article img[caption]").each(function() { |
116 |
$(this).after( |
117 |
"<div class='imgcptn'>" + |
118 |
$(this).attr("caption") + "</div>" |
119 |
); |
120 |
}); |
121 |
$("body > aside *[section]").click(function(e) { |
122 |
//e.preventDefault(); |
123 |
crdScrollTo(this); |
124 |
return false; |
125 |
}); |
126 |
crdOnScroll(); |
127 |
$(window).resize(function() { |
128 |
crdOnWindowResize(); |
129 |
}); |
130 |
if ("onhashchange" in window) { |
131 |
window.onhashchange = function() { |
132 |
crdScrollToHash(); |
133 |
}; |
134 |
} |
135 |
if (g_isTouch) { |
136 |
$("nav > ul > li").each(function() { |
137 |
$(this).click(function() { |
138 |
var active = $(this).hasClass("active"); |
139 |
$("nav li.active").removeClass("active"); |
140 |
if (!active) |
141 |
$(this).addClass("active"); |
142 |
}); |
143 |
}); |
144 |
$("article").click(function() { |
145 |
$("nav li.active").removeClass("active"); |
146 |
}); |
147 |
} |
148 |
$("article a[href]").each(function() { |
149 |
var href = $(this).attr("href"); |
150 |
if (href && href.length > 1 && href.charAt(0) == '#') { |
151 |
$(this).click(function(e) { |
152 |
var anch = $(this).attr("href"); |
153 |
crdScrollToHash(anch); |
154 |
}); |
155 |
} |
156 |
}); |
157 |
var showToc = null; |
158 |
if (typeof(Storage) !== "undefined") { |
159 |
var s = sessionStorage.getItem("show-toc"); |
160 |
if (s) |
161 |
showToc = s != "no"; |
162 |
} |
163 |
if (!$("body aside") || !$("body aside").length) |
164 |
showToc = false; |
165 |
if (showToc === null) { |
166 |
var w = crdWindowWidth(); |
167 |
showToc = (w >= 1200); |
168 |
} |
169 |
$("html").addClass("no-effect"); |
170 |
if (!showToc) { |
171 |
$("html").addClass("no-toc"); |
172 |
} |
173 |
document.body.offsetHeight; |
174 |
$("html").removeClass("no-effect"); |
175 |
crdUpdateTOCStatus(); |
176 |
}); |
177 |
|
178 |
$(document).ready(function() { |
179 |
crdOnWindowResize(); |
180 |
crdScrollToHash(); |
181 |
}); |
182 |
|
183 |
var _crdScrollTimeout; |
184 |
|
185 |
$(window).scroll(function() { |
186 |
crdOnScroll(); |
187 |
}); |