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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3272 - (hide annotations) (download) (as text)
Sat Jun 3 15:38:32 2017 UTC (6 years, 10 months ago) by schoenebeck
File MIME type: application/javascript
File size: 5519 byte(s)
* Site Template: Several fixes for touch devices.
* Site Template: Fixed unintended animation on doc load.
* Site Template: Save and restore user selection of TOC state.

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     sessionStorage.setItem("show-toc", $("html").hasClass("no-toc") ? "no" : "yes");
79     }
80 schoenebeck 3270 crdUpdateTOCStatus();
81     }
82    
83 schoenebeck 2732 function crdOnWindowResize() {
84     var wh = crdWindowHeight();
85     $("nav > ul > li ul").css("max-height", wh - 70);
86     }
87    
88     function crdDetectFeatures() {
89 schoenebeck 3268 if ('ontouchstart' in document.documentElement) {
90 schoenebeck 3269 g_isTouch = true;
91 schoenebeck 3268 document.documentElement.className += ' touch';
92     } else {
93 schoenebeck 3269 g_isTouch = false;
94 schoenebeck 3268 document.documentElement.className += ' no-touch';
95     }
96 schoenebeck 3272 $(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 schoenebeck 2732 if ($.support["transform"]) {
105     $("body").addClass("hasTransform");
106     }
107 schoenebeck 3267 var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
108     if (iOS) {
109     $("body").addClass("iOS");
110     }
111 schoenebeck 2732 }
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 schoenebeck 3266 if ("onhashchange" in window) {
131     window.onhashchange = function() {
132     crdScrollToHash();
133     };
134     }
135 schoenebeck 3272 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 schoenebeck 3266 $("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 schoenebeck 3272 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 schoenebeck 3270 $("html").addClass("no-toc");
172     }
173 schoenebeck 3272 document.body.offsetHeight;
174     $("html").removeClass("no-effect");
175 schoenebeck 3270 crdUpdateTOCStatus();
176 schoenebeck 2732 });
177    
178     $(document).ready(function() {
179     crdOnWindowResize();
180 schoenebeck 3266 crdScrollToHash();
181 schoenebeck 2732 });
182    
183     var _crdScrollTimeout;
184    
185     $(window).scroll(function() {
186     crdOnScroll();
187     });

  ViewVC Help
Powered by ViewVC