/[webpac2]/trunk/web/browse.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/web/browse.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 79 - (hide annotations)
Sun Nov 20 21:45:09 2005 UTC (18 years, 6 months ago) by dpavlin
File size: 9396 byte(s)
 r8999@llin:  dpavlin | 2005-11-20 22:46:22 +0100
 select template by clicking on list

1 dpavlin 46 #!/usr/bin/perl -w
2    
3 dpavlin 54 use strict;
4    
5 dpavlin 46 use Cwd qw/abs_path/;
6     use CGI::Carp qw(fatalsToBrowser);
7     use CGI::Simple;
8 dpavlin 49 use File::Slurp;
9 dpavlin 54 use Data::Dumper;
10     use Text::Iconv;
11 dpavlin 46
12     use lib '../lib';
13    
14     use WebPAC::DB;
15     use WebPAC::Output::TT;
16    
17     my $abs_path = abs_path($0);
18     $abs_path =~ s#/[^/]*$#/../#;
19    
20     my $db_path = $abs_path . '/db/';
21 dpavlin 72 my $template_path = "$abs_path/conf/output/tt";
22     opendir(my $dir, $template_path) || die "can't open template path $template_path: $!";
23     my @templates = grep { /\.tt$/i } readdir($dir);
24 dpavlin 67 my $css_file = 'user.css';
25 dpavlin 46
26 dpavlin 54 my $iconv_utf8 = new Text::Iconv('ISO-8859-2', 'UTF-8');
27     my $iconv_loc = new Text::Iconv('UTF-8', 'ISO-8859-2');
28    
29 dpavlin 46 my $db = new WebPAC::DB(
30     path => $db_path,
31     read_only => 1,
32     debug => 1,
33     );
34    
35     my $out = new WebPAC::Output::TT(
36 dpavlin 72 include_path => $template_path,
37 dpavlin 46 filters => { foo => sub { shift } },
38     );
39    
40     my $q = new CGI::Simple;
41     my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 );
42    
43     my $rec = $q->param('rec') || 1;
44 dpavlin 72 my $template_filename = $q->param('template') || $templates[0];
45 dpavlin 46
46 dpavlin 54 print $q->header( -charset => 'utf-8' );
47 dpavlin 46
48 dpavlin 67 ##---- some handy subs
49    
50     sub update_file($$) {
51     my ($path, $content) = @_;
52    
53     $content = $iconv_loc->convert( $content ) || die "no content?";
54    
55     sub _conv_js {
56     my $t = shift || return;
57     return $iconv_loc->convert(chr(hex($t)));
58     }
59     $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
60 dpavlin 68 $content =~ s/^[\n\r]+//s;
61 dpavlin 67 $content =~ s/[\n\r]+$//s;
62    
63     write_file($path . '.new', $content) || die "can't save ${path}.new $!";
64     rename $path . '.new', $path || die "can't rename to $path: $!";
65     }
66    
67     sub get_file_in_html($) {
68     my ($path) = @_;
69    
70     die "no path?" unless ($path);
71    
72     my $content = read_file($path) || die "can't read $path: $!";
73     $content = $q->escapeHTML($iconv_utf8->convert($content));
74    
75     return $content;
76     }
77    
78 dpavlin 79 sub template_list_html($) {
79     my $current = shift || die;
80    
81     my $html = qq{ Template\n<ul> };
82    
83     foreach my $t (sort @templates) {
84     if ($t eq $current) {
85     $html .= qq{ <li><b>$t</b></li> };
86     } else {
87     $html .= qq{ <li><a href="#" onClick="load_template('$t'); return false;">$t</a></li> };
88     }
89     }
90    
91     $html .= qq{ </ul> };
92     return $html;
93     }
94    
95 dpavlin 67 ##----
96    
97 dpavlin 46 if ($q->path_info =~ m#xml#) {
98    
99 dpavlin 70 my $ds = $db->load_ds($rec);
100 dpavlin 46
101 dpavlin 70 if ($ds) {
102 dpavlin 46 print qq{<response>
103 dpavlin 49 <action type='html' target='div_record' errorCode='' errorMessage='' >
104 dpavlin 58 }, $iconv_utf8->convert( $out->apply(
105 dpavlin 72 template => $template_filename,
106 dpavlin 70 data => $ds,
107 dpavlin 58 ) ), qq{
108 dpavlin 46
109 dpavlin 57 </action>
110     <action type='javascript' errorCode='' errorMessage='' >
111 dpavlin 46 <!--
112 dpavlin 49 var el = iwfGetById('div_record_nr');
113 dpavlin 46 if (el) el.innerHTML = '# <b>$rec</b>';
114 dpavlin 49 //iwfShow('div_record');
115     iwfOpacity('div_record', 100);
116 dpavlin 46 //-->
117     </action>
118     </response>
119     };
120     exit;
121     } else {
122 dpavlin 49 print qq{<response>
123     <action type='html' target='div_record' errorCode='' errorMessage='' >
124    
125     <b>Record $rec not found!</b>
126 dpavlin 57 </action>
127     <action type='javascript' errorCode='' errorMessage='' >
128 dpavlin 49 <!--
129     var el = iwfGetById('div_record_nr');
130 dpavlin 59 if (el) el.innerHTML = '<strike>&nbsp;$rec&nbsp;</strike>';
131 dpavlin 49 //-->
132     </action>
133     </response>
134     };
135     exit;
136 dpavlin 46 }
137    
138 dpavlin 79 } elsif ($q->path_info =~ m#template_list#) {
139    
140     print qq{<response>
141     <action type='html' target='div_template_list' errorCode='' errorMessage='' >
142     <div>} . template_list_html($template_filename) . qq{</div>
143     </action>
144     </response>
145     };
146    
147     exit;
148    
149 dpavlin 49 } elsif ($q->path_info =~ m#template#) {
150    
151 dpavlin 72 my $template_path = $out->{'include_path'} . '/' . $template_filename;
152 dpavlin 49
153 dpavlin 54 if ($q->param('save_template')) {
154    
155 dpavlin 67 update_file($template_path, $q->param('tt_template'));
156 dpavlin 58
157 dpavlin 54 print qq{<response>
158 dpavlin 67 <action type='html' target='div_template_status' errorCode='' errorMessage='' >
159 dpavlin 72 <tt>$template_filename</tt> saved
160 dpavlin 54 </action>
161     <action type='js'>
162     <!--
163 dpavlin 67 iwfShow('div_template_status', 1);
164 dpavlin 54 reload_rec();
165 dpavlin 67 iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
166 dpavlin 54 -->
167     </action>
168     </response>
169     };
170     exit;
171    
172     }
173    
174 dpavlin 67 my $tmpl = get_file_in_html($template_path);
175 dpavlin 54
176 dpavlin 49 print qq{<response>
177     <action type='html' target='div_template' errorCode='' errorMessage='' >
178 dpavlin 54
179 dpavlin 67 <form name="frmEditor" action="$self" method="post" iwfTarget="div_template_status" >
180 dpavlin 54
181     <textarea name="tt_template" cols="80" rows="10" style="display: block;">
182 dpavlin 49 $tmpl
183     </textarea>
184 dpavlin 54
185     <br/>
186     <input type="button" name="save_template" value="Save" onclick="javascript:iwfRequest(this);" />
187 dpavlin 61 <!--
188     <input type="checkbox" name="checkin_template" id="checkin_checkbox" label="checkin" /> checkin
189     -->
190 dpavlin 67 &nbsp;&nbsp;<span id="div_template_status" style="color: #808080;">idle</span>
191 dpavlin 54
192 dpavlin 77 <input type="hidden" name="template" value="$template_filename" />
193 dpavlin 54
194     </form>
195 dpavlin 49 </action>
196 dpavlin 61 <action type='js'>
197     <!--
198 dpavlin 79 iwfOpacity('div_template', 100);
199 dpavlin 67 iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
200 dpavlin 61 -->
201     </action>
202 dpavlin 49 </response>
203 dpavlin 54 };
204 dpavlin 49
205 dpavlin 54 exit;
206    
207 dpavlin 67 } elsif ($q->path_info =~ m#css#) {
208    
209     my $css_path = $abs_path . '/web/' . $css_file;
210    
211    
212     if ($q->param('save_css')) {
213     update_file($css_path, $q->param('user_css'));
214    
215     print qq{<response>
216     <action type='html' target='div_css_status' errorCode='' errorMessage='' >
217     <tt>$css_file</tt> saved
218     </action>
219     <action type='js'>
220     <!--
221     iwfShow('div_css_status', 1);
222     // switch css
223     css_rnd++;
224     iwfLog('loading user.css?'+css_rnd);
225     iwfGetById('user_css_link').href = 'user.css?'+css_rnd;
226     iwfHideGentlyDelay('div_css_status', 2, 2000, 1);
227     -->
228     </action>
229     </response>
230     };
231     exit;
232    
233     }
234    
235     my $user_css = get_file_in_html($css_path);
236    
237     print qq{<response>
238     <action type='html' target='div_css' errorCode='' errorMessage='' >
239    
240     <form name="frmCSSEditor" action="$self" method="post" iwfTarget="div_css_status" >
241    
242     <textarea name="user_css" cols="80" rows="10" style="display: block; width: 100%;">
243     $user_css
244     </textarea>
245    
246     <br/>
247     <input type="button" name="save_css" value="Save" onclick="javascript:iwfRequest(this);" />
248     &nbsp;&nbsp;<span id="div_css_status" style="color: #808080;">idle</span>
249     </form>
250     </action>
251     <action type='js'>
252     <!--
253     iwfLog('loaded CSS template');
254     -->
255     </action>
256     </response>
257     };
258    
259     exit;
260    
261 dpavlin 46 } else {
262 dpavlin 72
263 dpavlin 79 my $template_list_html = template_list_html($template_filename);
264 dpavlin 72
265 dpavlin 67 print <<"_END_OF_HEAD_";
266 dpavlin 46 <html>
267     <head>
268     <title>WebPAC simple browse interface</title>
269 dpavlin 67
270     <link id="user_css_link" href="user.css" type="text/css" rel="stylesheet">
271    
272 dpavlin 46 <script type='text/javascript' src='iwf/iwfcore.js'></script>
273     <script type='text/javascript' src='iwf/iwfgui.js'></script>
274     <script type='text/javascript' src='iwf/iwfxml.js'></script>
275     <script type='text/javascript' src='iwf/iwfajax.js'></script>
276 dpavlin 67 <script type='text/javascript' src='iwf/iwconfig.js'></script>
277 dpavlin 46 <script type='text/javascript'>
278    
279     var rec = $rec ;
280     var url = '$self';
281 dpavlin 72 var template_filename = '$template_filename';
282 dpavlin 46
283 dpavlin 67 var css_rnd = 0;
284    
285 dpavlin 46 function update_status(text) {
286 dpavlin 49 var el = iwfGetById('div_record_nr');
287 dpavlin 46 if (el) el.innerHTML = text;
288     }
289    
290     function load_rec(nr) {
291 dpavlin 59 if (nr == 1) {
292     iwfHide('a_left_arr', 1);
293     } else {
294     iwfShow('a_left_arr', 1);
295     }
296 dpavlin 46 update_status(nr+'...');
297 dpavlin 72 iwfRequest( url+'/xml/?template='+template_filename+'&rec='+nr, 'div_record' );
298 dpavlin 49 iwfOpacity('div_record', 30);
299 dpavlin 46 }
300    
301     function inc_rec() {
302     rec++;
303     load_rec(rec);
304     return false;
305     }
306    
307     function dec_rec() {
308 dpavlin 59 if (rec > 1) {
309     rec--;
310     load_rec(rec);
311     }
312 dpavlin 46 return false;
313     }
314    
315 dpavlin 47 function reload_rec() {
316     load_rec(rec);
317     return false;
318     }
319    
320 dpavlin 67 function edit_template() {
321     iwfHideGently('div_css', 30, 1);
322     iwfShowGently('div_template', 30, 1);
323     return false;
324     }
325    
326     function edit_css() {
327     iwfHideGently('div_template', 30, 1);
328     iwfShowGently('div_css', 30, 1);
329     return false;
330     }
331    
332 dpavlin 79 function load_template(name) {
333     iwfLog('changing template to '+name);
334     iwfOpacity('div_template', 30);
335     template_filename = name;
336     // load template editor
337     iwfRequest( url+'/template/?template='+template_filename, 'div_template' );
338     load_rec(rec);
339    
340     // refresh template list
341     iwfRequest( url+'/template_list/?template='+template_filename, 'div_template_list' );
342     }
343    
344 dpavlin 49 function init_page() {
345 dpavlin 67 iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
346     iwfLog('div_template = ' + iwfX('div_template') + ':' + iwfY('div_template'));
347    
348     iwfX('div_css', iwfX('div_template'));
349     iwfY('div_css', iwfY('div_template'));
350    
351     iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
352    
353 dpavlin 79 // load template editor and record
354     load_template(template_filename);
355 dpavlin 67
356     // load css editor
357     iwfRequest( url+'/css/', 'div_css' );
358 dpavlin 49 }
359    
360 dpavlin 46 </script>
361 dpavlin 67
362 dpavlin 46 </head>
363 dpavlin 49 <body onload="init_page();">
364 dpavlin 46
365 dpavlin 67 <div id="iwfLog">
366     </div>
367    
368 dpavlin 46 db_path = <tt>$db_path</tt><br/>
369 dpavlin 67 css = <tt>$css_file</tt>
370 dpavlin 46
371     <div style="background: #e0e0e0; padding: 0.5em; display: block;">
372 dpavlin 59 <a id="a_left_arr" href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>
373 dpavlin 49 <span id="div_record_nr"> none </span>
374 dpavlin 47
375 dpavlin 59 <a id="a_right_arr" href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>
376     <a id="a_reload" href="$self?rec=}, $rec, qq{" onClick="return reload_rec();">&#8634;</a>
377 dpavlin 67 <a href="#" onClick="iwfShowLog(); return false;">&#9636;</a>
378 dpavlin 54
379 dpavlin 46 </div>
380    
381 dpavlin 79
382     <div id="div_template_list" style="display: inline; border: 2px solid #ffcc88; float: right; z-index: 10;">
383     $template_list_html
384     </div>
385    
386 dpavlin 67 <div>
387    
388 dpavlin 79 <div style="border: 2px solid #ffff80;">
389    
390 dpavlin 67 Editor
391     <a id="a_template" href="#" onClick="return edit_template();">template</a>
392     <a id="a_css" href="#" onClick="return edit_css();">css</a>
393    
394 dpavlin 79
395 dpavlin 49 <div id="div_template">
396     <span style="color: #808080;"> no template loaded yet. </span>
397     </div>
398    
399 dpavlin 67 <div id="div_css" style="position: absolute; display: none;">
400     <span style="color: #808080;"> no CSS loaded yet. </span>
401     </div>
402    
403     </div>
404    
405 dpavlin 49 <div id="div_record" style="display: block;">
406 dpavlin 46 <span style="color: #808080;"> no record loaded yet. </span>
407     </div>
408    
409 dpavlin 49
410 dpavlin 46 </body>
411 dpavlin 67 </html>
412     _END_OF_HEAD_
413 dpavlin 46
414 dpavlin 67 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26