Commit | Line | Data |
720accfe |
1 | // S5 slides.js -- released under CC by-sa 2.0 license |
2 | // |
3 | // Please see http://www.meyerweb.com/eric/tools/s5/credits.html for information |
4 | // about all the wonderful and talented contributors to this code! |
5 | |
6 | var snum = 0; |
7 | var smax = 1; |
8 | var undef; |
9 | var slcss = 1; |
10 | var isIE = navigator.appName == 'Microsoft Internet Explorer' ? 1 : 0; |
11 | var isOp = navigator.userAgent.indexOf('Opera') > -1 ? 1 : 0; |
12 | var isGe = navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('Safari') < 1 ? 1 : 0; |
13 | var slideCSS = document.getElementById('slideProj').href; |
14 | |
15 | function isClass(object, className) { |
16 | return (object.className.search('(^|\\s)' + className + '(\\s|$)') != -1); |
17 | } |
18 | |
19 | function GetElementsWithClassName(elementName,className) { |
20 | var allElements = document.getElementsByTagName(elementName); |
21 | var elemColl = new Array(); |
22 | for (i = 0; i< allElements.length; i++) { |
23 | if (isClass(allElements[i], className)) { |
24 | elemColl[elemColl.length] = allElements[i]; |
25 | } |
26 | } |
27 | return elemColl; |
28 | } |
29 | |
30 | function isParentOrSelf(element, id) { |
31 | if (element == null || element.nodeName=='BODY') return false; |
32 | else if (element.id == id) return true; |
33 | else return isParentOrSelf(element.parentNode, id); |
34 | } |
35 | |
36 | function nodeValue(node) { |
37 | var result = ""; |
38 | if (node.nodeType == 1) { |
39 | var children = node.childNodes; |
40 | for ( i = 0; i < children.length; ++i ) { |
41 | result += nodeValue(children[i]); |
42 | } |
43 | } |
44 | else if (node.nodeType == 3) { |
45 | result = node.nodeValue; |
46 | } |
47 | return(result); |
48 | } |
49 | |
50 | function slideLabel() { |
51 | var slideColl = GetElementsWithClassName('div','slide'); |
52 | var list = document.getElementById('jumplist'); |
53 | smax = slideColl.length; |
54 | for (n = 0; n < smax; n++) { |
55 | var obj = slideColl[n]; |
56 | |
57 | var did = 'slide' + n.toString(); |
58 | obj.setAttribute('id',did); |
59 | if(isOp) continue; |
60 | |
61 | var otext = ''; |
62 | var menu = obj.firstChild; |
63 | if (!menu) continue; // to cope with empty slides |
64 | while (menu && menu.nodeType == 3) { |
65 | menu = menu.nextSibling; |
66 | } |
67 | if (!menu) continue; // to cope with slides with only text nodes |
68 | |
69 | var menunodes = menu.childNodes; |
70 | for (o = 0; o < menunodes.length; o++) { |
71 | otext += nodeValue(menunodes[o]); |
72 | } |
73 | list.options[list.length] = new Option(n+' : ' +otext,n); |
74 | } |
75 | } |
76 | |
77 | function currentSlide() { |
78 | var cs; |
79 | if (document.getElementById) { |
80 | cs = document.getElementById('currentSlide'); |
81 | } else { |
82 | cs = document.currentSlide; |
83 | } |
84 | cs.innerHTML = '<span id="csHere">' + snum + '<\/span> ' + |
85 | '<span id="csSep">\/<\/span> ' + |
86 | '<span id="csTotal">' + (smax-1) + '<\/span>'; |
87 | if (snum == 0) { |
88 | cs.style.visibility = 'hidden'; |
89 | } else { |
90 | cs.style.visibility = 'visible'; |
91 | } |
92 | } |
93 | |
94 | function go(inc) { |
95 | if (document.getElementById("slideProj").disabled) return; |
96 | var cid = 'slide' + snum; |
97 | if (inc != 'j') { |
98 | snum += inc; |
99 | lmax = smax - 1; |
100 | if (snum > lmax) snum = 0; |
101 | if (snum < 0) snum = lmax; |
102 | } else { |
103 | snum = parseInt(document.getElementById('jumplist').value); |
104 | } |
105 | var nid = 'slide' + snum; |
106 | var ne = document.getElementById(nid); |
107 | if (!ne) { |
108 | ne = document.getElementById('slide0'); |
109 | snum = 0; |
110 | } |
111 | document.getElementById(cid).style.visibility = 'hidden'; |
112 | ne.style.visibility = 'visible'; |
113 | document.getElementById('jumplist').selectedIndex = snum; |
114 | currentSlide(); |
115 | } |
116 | |
117 | function toggle() { |
118 | var slideColl = GetElementsWithClassName('div','slide'); |
119 | var obj = document.getElementById('slideProj'); |
120 | if (!obj.disabled) { |
121 | obj.disabled = true; |
122 | for (n = 0; n < smax; n++) { |
123 | var slide = slideColl[n]; |
124 | slide.style.visibility = 'visible'; |
125 | } |
126 | } else { |
127 | obj.disabled = false; |
128 | for (n = 0; n < smax; n++) { |
129 | var slide = slideColl[n]; |
130 | slide.style.visibility = 'hidden'; |
131 | } |
132 | slideColl[snum].style.visibility = 'visible'; |
133 | } |
134 | } |
135 | |
136 | function showHide(action) { |
137 | var obj = document.getElementById('jumplist'); |
138 | switch (action) { |
139 | case 's': obj.style.visibility = 'visible'; break; |
140 | case 'h': obj.style.visibility = 'hidden'; break; |
141 | case 'k': |
142 | if (obj.style.visibility != 'visible') { |
143 | obj.style.visibility = 'visible'; |
144 | } else { |
145 | obj.style.visibility = 'hidden'; |
146 | } |
147 | break; |
148 | } |
149 | } |
150 | |
151 | // 'keys' code adapted from MozPoint (http://mozpoint.mozdev.org/) |
152 | function keys(key) { |
153 | if (!key) { |
154 | key = event; |
155 | key.which = key.keyCode; |
156 | } |
157 | switch (key.which) { |
158 | case 10: // return |
159 | case 13: // enter |
160 | if (window.event && isParentOrSelf(window.event.srcElement, "controls")) return; |
161 | if (key.target && isParentOrSelf(key.target, "controls")) return; |
162 | case 32: // spacebar |
163 | case 34: // page down |
164 | case 39: // rightkey |
165 | case 40: // downkey |
166 | go(1); |
167 | break; |
168 | case 33: // page up |
169 | case 37: // leftkey |
170 | case 38: // upkey |
171 | go(-1); |
172 | break; |
173 | case 84: // t |
174 | toggle(); |
175 | break; |
176 | case 67: // c |
177 | showHide('k'); |
178 | break; |
179 | } |
180 | } |
181 | |
182 | function clicker(e) { |
183 | var target; |
184 | if (window.event) { |
185 | target = window.event.srcElement; |
186 | e = window.event; |
187 | } else target = e.target; |
188 | if (target.href != null || isParentOrSelf(target, 'controls')) return true; |
189 | if (!e.which || e.which == 1) go(1); |
190 | } |
191 | |
192 | function slideJump() { |
193 | if (window.location.hash == null) return; |
194 | var sregex = /^#slide(\d+)$/; |
195 | var matches = sregex.exec(window.location.hash); |
196 | var dest = null; |
197 | if (matches != null) { |
198 | dest = parseInt(matches[1]); |
199 | } else { |
200 | var target = window.location.hash.slice(1); |
201 | var targetElement = null; |
202 | var aelements = document.getElementsByTagName("a"); |
203 | for (i = 0; i < aelements.length; i++) { |
204 | var aelement = aelements[i]; |
205 | if ( (aelement.name && aelement.name == target) |
206 | || (aelement.id && aelement.id == target) ) { |
207 | targetElement = aelement; |
208 | break; |
209 | } |
210 | } |
211 | while(targetElement != null && targetElement.nodeName != "body") { |
212 | if (targetElement.className == "slide") break; |
213 | targetElement = targetElement.parentNode; |
214 | } |
215 | if (targetElement != null && targetElement.className == "slide") { |
216 | dest = parseInt(targetElement.id.slice(1)); |
217 | } |
218 | } |
219 | if (dest != null) |
220 | go(dest - snum); |
221 | } |
222 | |
223 | function createControls() { |
224 | controlsDiv = document.getElementById("controls"); |
225 | if (!controlsDiv) return; |
226 | controlsDiv.innerHTML = '<form action="#" id="controlForm">' + |
227 | '<div>' + |
228 | '<a accesskey="t" id="toggle" href="javascript:toggle();">Ø<\/a>' + |
229 | '<a accesskey="z" id="prev" href="javascript:go(-1);">«<\/a>' + |
230 | '<a accesskey="x" id="next" href="javascript:go(1);">»<\/a>' + |
231 | '<\/div>' + |
232 | '<div onmouseover="showHide(\'s\');" onmouseout="showHide(\'h\');"><select id="jumplist" onchange="go(\'j\');"><\/select><\/div>' + |
233 | '<\/form>'; |
234 | } |
235 | |
236 | function notOperaFix() { |
237 | var obj = document.getElementById('slideProj'); |
238 | obj.setAttribute('media','screen'); |
239 | if (isGe) { |
240 | obj.setAttribute('href','null'); // Gecko fix |
241 | obj.setAttribute('href',slideCSS); // Gecko fix |
242 | } |
243 | } |
244 | |
245 | function startup() { |
246 | if (!isOp) createControls(); |
247 | slideLabel(); |
248 | if (!isOp) { |
249 | notOperaFix(); |
250 | slideJump(); |
251 | document.onkeyup = keys; |
252 | document.onclick = clicker; |
253 | } |
254 | } |
255 | |
256 | window.onload = startup; |
257 | |
258 | |
259 | |
260 | |