4ea0da4826db170d941c45487014f53a8763eb77
[booh] / lib / booh / html-merges.rb
1 #
2 #                         *  BOOH  *
3 #
4 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
5 #
6 # The acronyn sucks, however this is a tribute to Dragon Ball by
7 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
8 # Ball is named "Boo". But there was already a free software project
9 # called Boo, so this one will be it "Booh". Or whatever.
10 #
11 #
12 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
13 #
14 # This software may be freely redistributed under the terms of the GNU
15 # public license version 2.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 # holds static data to merge in the html "themes"
22
23 require 'gettext'
24 bindtextdomain("booh")
25
26 require 'booh/booh-lib'
27 include Booh
28
29 $image_head_code = <<'EOF'
30 <meta name="generator" content="Generated by Booh! http://zarb.org/~gc/html/booh.html">
31
32 <script language="JavaScript1.1" type="text/JavaScript">
33
34 var images = new Array(~~images~~);
35 ~~other_images~~
36 var other_sizes = new Array(~~other_sizes~~);
37 var captions = new Array(~~captions~~);
38
39 var images_ary = new Array();
40 var images_loaded = new Array();
41 var current = 0;
42 var slideshow = 0;
43 var slideshow_pause = 3;
44 var slideshow_timer = null;
45
46 for (i = 0; i < images.length; i++) { 
47     /* this array will contain 0 if image not yet loaded, 1 when loading,
48      * 2 when complete */
49     images_loaded[i] = 0;
50 }
51
52 function dbg(t) {
53     document.getElementById('dbg_text').firstChild.data += t + "\n";
54 }
55
56 /* load image, return 1 if image is finished loading */
57 function load(i) {
58     if (images_loaded[i] == 0) {
59         images_ary[i] = new Image();
60         images_ary[i].src = images[i];
61         images_loaded[i] = 1;
62     }
63     if (images_loaded[i] == 1) {
64         if (images_ary[i].complete) {
65             images_loaded[i] = 2;
66         } else {
67             return 0;
68         }
69     }
70     return 1;
71 }
72
73 function preload() { 
74
75     /* favor current image, if user clicked on `last' or something */
76     load(current);
77
78     /* don't blindly preload all images at the beginning,
79      * but rather load them one by one, in order to get
80      * next ones faster, beginning with next to current
81      */
82     for (i = current + 1; i < images.length && i <= current + 5; i++) { 
83         if (load(i) == 0) {
84             setTimeout("preload()", 500);
85             return;
86         }
87     }
88     for (i = current - 1; i >= current - 3; i--) { 
89         if (i >= 0) {
90             if (load(i) == 0) {
91                 setTimeout("preload()", 500);
92                 return;
93             }
94         }
95     }
96
97     setTimeout("preload()", 500);
98 }
99
100 function init() {
101
102     /* retrieve GET parameters */
103     all_params = location.href.split("?")
104     if (all_params.length > 1) {
105         params = all_params[1].split("&");
106         for (i = 0; i < params.length; i++) {
107             keyvalue = params[i].split("=");
108             if (keyvalue[0] == "slideshow_pause") {
109                 slideshow_pause = keyvalue[1];
110             }
111             if (keyvalue[0] == "run_slideshow") {
112                 toggle_slideshow();
113             }
114             if (keyvalue[0] == "current") {
115                 for (i = 0; i < images.length; i++) {
116                     if (images[i] == keyvalue[1]) {
117                         current = i;
118                         break;
119                     }
120                 }
121             }
122         }
123     }
124
125     preload();
126     display_current();
127     if (images.length == 1) {
128         document.getElementById("b_slideshow").disabled = true;
129     }
130 }
131
132 function update_sensibilities() {
133     if (current == 0) {
134         document.getElementById("b_first").disabled = true;
135         document.getElementById("b_previous").disabled = true;
136     } else {
137         document.getElementById("b_first").disabled = false;
138         document.getElementById("b_previous").disabled = false;
139     }
140
141     if (current == images.length - 1) {
142         document.getElementById("b_next").disabled = true;
143         document.getElementById("b_last").disabled = true;
144     } else {
145         document.getElementById("b_next").disabled = false;
146         document.getElementById("b_last").disabled = false;
147     }
148 }
149
150 function set_cursor_(value, element) {
151
152     if (!element || !element.style) {
153         return;
154     }
155
156     element.style.cursor = value;
157
158     children = element.childNodes;
159     for (i = 0; i < children.length; i++) {
160         set_cursor_(value, children.item[i]);
161     }
162 }
163
164 function set_cursor(value) {
165     set_cursor_(value, document.getElementById('body'));
166 }
167
168 function show_current_text() {
169     /* don't show text if image not yet loaded because navigator
170      * won't refresh it during load */
171     if (images_loaded[current] == 2) {
172         eval("document.getElementById('image_counter')" +
173                      ".firstChild.data = '" + ( current + 1 ) + "/" + images.length + "'");
174         eval("document.getElementById('main_text')" +
175                      ".firstChild.data = \"" + ( captions[current] || images[current] ) + "\"");
176         for (i = 0; i < other_sizes.length; i++) { 
177             eval("linkelems = document.getElementById('link" + other_sizes[i] + "').href.split('?');" +
178                  "document.getElementById('link" + other_sizes[i] + "').href = linkelems[0] + '?current=" + eval("images_" + other_sizes[i] + "[current]") + "'");
179         }
180         set_cursor("default");
181     } else {
182         setTimeout("show_current_text()", 100);
183         set_cursor("wait");
184     }
185 }
186
187 function display_current() {
188     eval("document.main_img.src = images[" + current + "]");
189     show_current_text();
190     update_sensibilities();
191 }
192
193 function first() {
194     if (slideshow == 1) {
195         toggle_slideshow(true);
196     }
197     
198     current = 0;
199     display_current();
200 }
201
202 function next() {
203     if (slideshow == 1) {
204         toggle_slideshow(true);
205     }
206
207     if (current < images.length - 1) {
208         current++;
209         display_current();
210     }
211 }
212
213 function previous() {
214     if (slideshow == 1) {
215         toggle_slideshow(true);
216     }
217
218     if (current > 0) {
219         current--;
220         display_current();
221     }
222 }
223
224 function last() {
225     if (slideshow == 1) {
226         toggle_slideshow(true);
227     }
228
229     current = images.length - 1;
230     display_current();
231 }
232
233 function toggle_slideshow(now) {
234     if (slideshow == 0) {
235         document.getElementById("b_slideshow").value = "~~stop_slideshow~~";
236         slideshow = 1;
237         if (current == images.length - 1) {
238             current = -1;
239         }
240         if (now) {
241             run_slideshow();
242         } else {
243             setTimeout("run_slideshow()", slideshow_pause * 1000);
244         }
245     } else {
246         clearTimeout(slideshow_timer);
247         document.getElementById("b_slideshow").value = "~~run_slideshow~~";
248         slideshow = 0;
249     }
250 }
251
252 function run_slideshow() {
253     if (slideshow == 0) {
254         return;
255     }
256
257     if (images_loaded[current + 1] == 2) {
258         current++;
259         display_current();
260         slideshow_timer = setTimeout("run_slideshow()", slideshow_pause * 1000);
261     } else {
262         slideshow_timer = setTimeout("run_slideshow()", 500);
263     }
264
265     if (current == images.length - 1) {
266         toggle_slideshow(true);
267     }
268 }
269 </script>
270 EOF
271
272 $image_head_code.sub!('~~run_slideshow~~', utf8(_('Run slideshow!')))
273 $image_head_code.sub!('~~stop_slideshow~~', utf8(_('Stop slideshow')))
274
275 $body_additions = <<'EOF'
276 onload="init()" id="body"
277 EOF
278
279 $button_first = '
280     <form><input type="button"
281                  onclick="first()"
282                  value="' + utf8(_('<<- First')) + '"
283                  id="b_first"></form>';
284
285 $button_previous = '
286     <form><input type="button"
287                  onclick="previous()"
288                  value="' + utf8(_('<- Previous')) + '"
289                  id="b_previous"></form>';
290
291 $button_next = '
292     <form><input type="button"
293                  onclick="next()"
294                  value="' + utf8(_('Next ->')) + '"
295                  id="b_next"></form>';
296
297 $button_last = '
298     <form><input type="button"
299                  onclick="last()"
300                  value="' + utf8(_('Last ->>')) + '"
301                  id="b_last"></form>';
302
303 $button_slideshow = '
304   <form><input type="button"
305                onclick="toggle_slideshow(true)"
306                value="' + utf8(_('Run slideshow!')) + '"
307                id="b_slideshow">
308   </form>';
309
310 $image = <<'EOF'
311   <img name="main_img">
312 EOF
313
314 $image_counter_additions = <<'EOF'
315   id="image_counter"
316 EOF
317
318 $caption_additions = <<'EOF'
319   id="main_text"
320 EOF
321
322 $body_code = <<'EOF'
323 EOF
324
325
326 $thumbnails_head_code = <<'EOF'
327 <script language="JavaScript1.1" type="text/JavaScript">
328     var expires = new Date(new Date().getTime() + (30 * 86400000));  // 30 days
329     document.cookie = 'booh-preferred-size-~~theme~~=~~current_size~~'
330                       + '; expires=' + expires.toGMTString()
331                       + '; path=/';
332 </script>
333 EOF
334
335
336 $preferred_size_reloader = <<'EOF'
337 <html>
338     <head>
339         <script language="JavaScript1.1" type="text/JavaScript">
340
341 function getPreferredSize() {
342     if (document.cookie) {
343         var index = document.cookie.indexOf('booh-preferred-size-~~theme~~');
344         if (index != -1) {
345             var oleft = (document.cookie.indexOf('=', index) + 1);
346             var oright = document.cookie.indexOf(';', index);
347             if (oright == -1) {
348                 oright = document.cookie.length;
349             }
350             return 'thumbnails-' + document.cookie.substring(oleft, oright) + '.html';
351         }
352     }
353     return 'thumbnails-~~default_size~~.html';
354 }
355
356 window.location.href = getPreferredSize();
357
358         </script>
359     </head>
360 </html>
361 EOF