/[svn]/doc/tmpl/js/crd.js
ViewVC logotype

Annotation of /doc/tmpl/js/crd.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3273 - (hide annotations) (download) (as text)
Sun Jun 4 15:59:26 2017 UTC (6 years, 10 months ago) by schoenebeck
File MIME type: application/javascript
File size: 5609 byte(s)
- Site Template: Fixed misbehavior on private browser sessions.

1 schoenebeck 2732 /*
2     CrudeDoc Java Script
3 schoenebeck 3266 Copyright (c) 2015 - 2017 Christian Schoenebeck. All rights reserved.
4 schoenebeck 2732 http://www.crudebyte.com
5     */
6    
7 schoenebeck 3269 var g_isTouch = false;
8 schoenebeck 3272 var g_isMouse = false;
9 schoenebeck 3269
10 schoenebeck 3270 function crdWindowWidth() {
11     return window.innerWidth ||
12     document.documentElement.clientWidth ||
13     document.getElementsByTagName('body')[0].clientWidth;
14     }
15    
16 schoenebeck 2732 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 schoenebeck 3266 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 schoenebeck 2732 }
29    
30 schoenebeck 3266 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 schoenebeck 2732 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 schoenebeck 3270 function crdUpdateTOCStatus() {
66     var noTOC = $("html").hasClass("no-toc");
67     $("nav > .foldbtn a").html(
68     noTOC ? "&gt;&gt;&gt;" : "&lt;&lt;&lt;"
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 schoenebeck 3272 if (typeof(Storage) !== "undefined") {
78 schoenebeck 3273 try {
79     sessionStorage.setItem("show-toc", $("html").hasClass("no-toc") ? "no" : "yes");
80     } catch (e) {}
81 schoenebeck 3272 }
82 schoenebeck 3270 crdUpdateTOCStatus();
83     }
84    
85 schoenebeck 2732 function crdOnWindowResize() {
86     var wh = crdWindowHeight();
87     $("nav > ul > li ul").css("max-height", wh - 70);
88     }
89    
90     function crdDetectFeatures() {
91 schoenebeck 3268 if ('ontouchstart' in document.documentElement) {
92 schoenebeck 3269 g_isTouch = true;
93 schoenebeck 3268 document.documentElement.className += ' touch';
94     } else {
95 schoenebeck 3269 g_isTouch = false;
96 schoenebeck 3268 document.documentElement.className += ' no-touch';
97     }
98 schoenebeck 3272 $(window).bind('mousemove.hasMouse', function(){
99     $(window).unbind('.hasMouse');
100     g_isMouse = true;
101     $(document).tooltip();
102     }).bind('touchstart.hasMouse', function(){
103     $(window).unbind('.hasMouse');
104     g_isMouse = false;
105     });
106 schoenebeck 2732 if ($.support["transform"]) {
107     $("body").addClass("hasTransform");
108     }
109 schoenebeck 3267 var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
110     if (iOS) {
111     $("body").addClass("iOS");
112     }
113 schoenebeck 2732 }
114    
115     $(function() {
116     crdDetectFeatures();
117     $("article img[caption]").each(function() {
118     $(this).after(
119     "<div class='imgcptn'>" +
120     $(this).attr("caption") + "</div>"
121     );
122     });
123     $("body > aside *[section]").click(function(e) {
124     //e.preventDefault();
125     crdScrollTo(this);
126     return false;
127     });
128     crdOnScroll();
129     $(window).resize(function() {
130     crdOnWindowResize();
131     });
132 schoenebeck 3266 if ("onhashchange" in window) {
133     window.onhashchange = function() {
134     crdScrollToHash();
135     };
136     }
137 schoenebeck 3272 if (g_isTouch) {
138     $("nav > ul > li").each(function() {
139     $(this).click(function() {
140     var active = $(this).hasClass("active");
141     $("nav li.active").removeClass("active");
142     if (!active)
143     $(this).addClass("active");
144     });
145     });
146     $("article").click(function() {
147     $("nav li.active").removeClass("active");
148     });
149     }
150 schoenebeck 3266 $("article a[href]").each(function() {
151     var href = $(this).attr("href");
152     if (href && href.length > 1 && href.charAt(0) == '#') {
153     $(this).click(function(e) {
154     var anch = $(this).attr("href");
155     crdScrollToHash(anch);
156     });
157     }
158     });
159 schoenebeck 3272 var showToc = null;
160     if (typeof(Storage) !== "undefined") {
161 schoenebeck 3273 try {
162     var s = sessionStorage.getItem("show-toc");
163     if (s)
164     showToc = s != "no";
165     } catch (e) {}
166 schoenebeck 3272 }
167     if (!$("body aside") || !$("body aside").length)
168     showToc = false;
169     if (showToc === null) {
170     var w = crdWindowWidth();
171     showToc = (w >= 1200);
172     }
173     $("html").addClass("no-effect");
174     if (!showToc) {
175 schoenebeck 3270 $("html").addClass("no-toc");
176     }
177 schoenebeck 3272 document.body.offsetHeight;
178     $("html").removeClass("no-effect");
179 schoenebeck 3270 crdUpdateTOCStatus();
180 schoenebeck 2732 });
181    
182     $(document).ready(function() {
183     crdOnWindowResize();
184 schoenebeck 3266 crdScrollToHash();
185 schoenebeck 2732 });
186    
187     var _crdScrollTimeout;
188    
189     $(window).scroll(function() {
190     crdOnScroll();
191     });

  ViewVC Help
Powered by ViewVC