r8984@llin: dpavlin | 2005-11-20 02:51:47 +0100
[webpac2] / web / browse.cgi
index 5f1b2c3..0ea35b9 100755 (executable)
@@ -1,8 +1,13 @@
 #!/usr/bin/perl -w
 
+use strict;
+
 use Cwd qw/abs_path/;
 use CGI::Carp qw(fatalsToBrowser);
 use CGI::Simple;
+use File::Slurp;
+use Data::Dumper;
+use Text::Iconv;
 
 use lib '../lib';
 
@@ -13,6 +18,13 @@ my $abs_path = abs_path($0);
 $abs_path =~ s#/[^/]*$#/../#;
 
 my $db_path = $abs_path . '/db/';
+my $template_path = "$abs_path/conf/output/tt";
+opendir(my $dir, $template_path) || die "can't open template path $template_path: $!";
+my @templates = grep { /\.tt$/i } readdir($dir);
+my $css_file = 'user.css';
+
+my $iconv_utf8 = new Text::Iconv('ISO-8859-2', 'UTF-8');
+my $iconv_loc = new Text::Iconv('UTF-8', 'ISO-8859-2');
 
 my $db = new WebPAC::DB(
        path => $db_path,
@@ -21,7 +33,7 @@ my $db = new WebPAC::DB(
 );
 
 my $out = new WebPAC::Output::TT(
-       include_path => "$abs_path/conf/output/tt",
+       include_path => $template_path,
        filters => { foo => sub { shift } },
 );
 
@@ -29,64 +41,245 @@ my $q = new CGI::Simple;
 my $self = $q->url( '-path_info'=>1, '-query'=>0, '-full'=>0 );
 
 my $rec = $q->param('rec') || 1;
+my $template_filename = $q->param('template') || $templates[0];
+
+print $q->header( -charset    => 'utf-8' );
+
+##---- some handy subs
+
+sub update_file($$) {
+       my ($path, $content) = @_;
+
+       $content = $iconv_loc->convert( $content ) || die "no content?";
+
+       sub _conv_js {
+               my $t = shift || return;
+               return $iconv_loc->convert(chr(hex($t)));
+       }
+       $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
+       $content =~ s/^[\n\r]+//s;
+       $content =~ s/[\n\r]+$//s;
+
+       write_file($path . '.new', $content) || die "can't save ${path}.new $!";
+       rename $path . '.new', $path || die "can't rename to $path: $!";
+}
+
+sub get_file_in_html($) {
+       my ($path) = @_;
+
+       die "no path?" unless ($path);
+
+       my $content = read_file($path) || die "can't read $path: $!";
+       $content = $q->escapeHTML($iconv_utf8->convert($content));
+
+       return $content;
+}
 
-print $q->header;
+##----
 
 if ($q->path_info =~ m#xml#) {
 
-       my @ds = $db->load_ds($rec);
+       my $ds = $db->load_ds($rec);
 
-       if (@ds) {
+       if ($ds) {
                print qq{<response>
-<action type='html' target='divRecord' errorCode='' errorMessage='' >
-               }, $out->apply(
-                       template => 'html_ffzg.tt',
-                       data => \@ds,
-               ), qq{
+<action type='html' target='div_record' errorCode='' errorMessage='' >
+               }, $iconv_utf8->convert( $out->apply(
+                       template => $template_filename,
+                       data => $ds,
+               ) ), qq{
 
-<script type='text/javascript'>
+</action>
+<action type='javascript' errorCode='' errorMessage='' >
 <!--
-       var el = iwfGetById('divRecordNr');
+       var el = iwfGetById('div_record_nr');
        if (el) el.innerHTML = '# <b>$rec</b>';
-       //iwfShow('divRecord');
-       iwfOpacity('divRecord', 100);
+       //iwfShow('div_record');
+       iwfOpacity('div_record', 100);
 //-->
-</script>
-
 </action>
 </response>
 };
                exit;
        } else {
-               print qq{
-                       <b>Record $rec not found!</b>
-               };
+               print qq{<response>
+<action type='html' target='div_record' errorCode='' errorMessage='' >
+
+<b>Record $rec not found!</b>
+</action>
+<action type='javascript' errorCode='' errorMessage='' >
+<!--
+       var el = iwfGetById('div_record_nr');
+       if (el) el.innerHTML = '<strike>&nbsp;$rec&nbsp;</strike>';
+//-->
+</action>
+</response>
+};
+               exit;
        }
 
+} elsif ($q->path_info =~ m#template#) {
+
+               my $template_path = $out->{'include_path'} . '/' . $template_filename;
+
+               if ($q->param('save_template')) {
+
+                       update_file($template_path, $q->param('tt_template'));
+
+                       print qq{<response>
+<action type='html' target='div_template_status' errorCode='' errorMessage='' >
+<tt>$template_filename</tt> saved
+</action>
+<action type='js'>
+<!--
+iwfShow('div_template_status', 1);
+reload_rec();
+iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
+-->
+</action>
+</response>
+                       };
+                       exit;
+
+               }
+
+               my $tmpl = get_file_in_html($template_path);
+
+               print qq{<response>
+<action type='html' target='div_template' errorCode='' errorMessage='' >
+
+<form name="frmEditor" action="$self" method="post" iwfTarget="div_template_status" >
+
+<textarea name="tt_template" cols="80" rows="10" style="display: block;">
+$tmpl
+</textarea>
+
+<br/>
+<input type="button" name="save_template" value="Save" onclick="javascript:iwfRequest(this);" />
+<!--
+<input type="checkbox" name="checkin_template" id="checkin_checkbox" label="checkin" /> checkin
+-->
+&nbsp;&nbsp;<span id="div_template_status" style="color: #808080;">idle</span>
+
+<input type='hidden' value='hidden post value' name='hidValue' />
+
+</form>
+</action>
+<action type='js'>
+<!--
+iwfHideGentlyDelay('div_template_status', 2, 2000, 1);
+-->
+</action>
+</response>
+               };
+
+               exit;
+
+} elsif ($q->path_info =~ m#css#) {
+
+               my $css_path = $abs_path . '/web/' . $css_file;
+
+
+               if ($q->param('save_css')) {
+                       update_file($css_path, $q->param('user_css'));
+
+                       print qq{<response>
+<action type='html' target='div_css_status' errorCode='' errorMessage='' >
+<tt>$css_file</tt> saved
+</action>
+<action type='js'>
+<!--
+iwfShow('div_css_status', 1);
+// switch css
+css_rnd++;
+iwfLog('loading user.css?'+css_rnd);
+iwfGetById('user_css_link').href = 'user.css?'+css_rnd;
+iwfHideGentlyDelay('div_css_status', 2, 2000, 1);
+-->
+</action>
+</response>
+                       };
+                       exit;
+
+               }
+
+               my $user_css = get_file_in_html($css_path);
+
+               print qq{<response>
+<action type='html' target='div_css' errorCode='' errorMessage='' >
+
+<form name="frmCSSEditor" action="$self" method="post" iwfTarget="div_css_status" >
+
+<textarea name="user_css" cols="80" rows="10" style="display: block; width: 100%;">
+$user_css
+</textarea>
+
+<br/>
+<input type="button" name="save_css" value="Save" onclick="javascript:iwfRequest(this);" />
+&nbsp;&nbsp;<span id="div_css_status" style="color: #808080;">idle</span>
+</form>
+</action>
+<action type='js'>
+<!--
+iwfLog('loaded CSS template');
+-->
+</action>
+</response>
+               };
+
+               exit;
+
 } else {
-       print qq{
+
+       my $template_form = qq{
+               <form action="$self" method="get" style="display: inline;">
+               <select name="template">
+       };
+       foreach my $t (@templates) {
+               my $s = '';
+               $s = ' selected' if ($t eq $template_filename);
+               $template_form .= qq{<option$s>$t</option>};
+       }
+       $template_form .= qq{
+               </select>
+               <input type="submit" name="ch_template" value="Switch"/>
+               </form>
+       };
+
+       print <<"_END_OF_HEAD_";
 <html>
 <head>
 <title>WebPAC simple browse interface</title>
+
+<link id="user_css_link" href="user.css" type="text/css" rel="stylesheet"> 
+
 <script type='text/javascript' src='iwf/iwfcore.js'></script>
 <script type='text/javascript' src='iwf/iwfgui.js'></script>
 <script type='text/javascript' src='iwf/iwfxml.js'></script>
 <script type='text/javascript' src='iwf/iwfajax.js'></script>
+<script type='text/javascript' src='iwf/iwconfig.js'></script>
 <script type='text/javascript'>
 
 var rec = $rec ;
 var url = '$self';
+var template_filename = '$template_filename';
+
+var css_rnd = 0;
 
 function update_status(text) {
-       var el = iwfGetById('divRecordNr');
+       var el = iwfGetById('div_record_nr');
        if (el) el.innerHTML = text;
 }
-       
 
 function load_rec(nr) {
+       if (nr == 1) {
+               iwfHide('a_left_arr', 1);
+       } else {
+               iwfShow('a_left_arr', 1);
+       }
        update_status(nr+'...');
-       iwfRequest( url+'/xml/?rec='+nr, 'divRecord' );
-       iwfOpacity('divRecord', 30);
+       iwfRequest( url+'/xml/?template='+template_filename+'&rec='+nr, 'div_record' );
+       iwfOpacity('div_record', 30);
 }
 
 function inc_rec() {
@@ -96,28 +289,93 @@ function inc_rec() {
 }
 
 function dec_rec() {
-       rec--;
+       if (rec > 1) {
+               rec--;
+               load_rec(rec);
+       }
+       return false;
+}
+
+function reload_rec() {
        load_rec(rec);
        return false;
 }
 
+function edit_template() {
+       iwfHideGently('div_css', 30, 1);
+       iwfShowGently('div_template', 30, 1);
+       return false;
+}
+
+function edit_css() {
+       iwfHideGently('div_template', 30, 1);
+       iwfShowGently('div_css', 30, 1);
+       return false;
+}
+
+function init_page() {
+       iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
+       iwfLog('div_template = ' + iwfX('div_template') + ':' + iwfY('div_template'));
+
+       iwfX('div_css', iwfX('div_template'));
+       iwfY('div_css', iwfY('div_template'));
+
+       iwfLog('div_css = ' + iwfX('div_css') + ':' + iwfY('div_css'));
+
+       load_rec(rec);
+
+       // load template editor
+       iwfRequest( url+'/template/?template='+template_filename, 'div_template' );
+       // load css editor
+       iwfRequest( url+'/css/', 'div_css' );
+}
+
 </script>
+
 </head>
-<body onload="load_rec($rec);">
+<body onload="init_page();">
+
+<div id="iwfLog">
+</div>
 
 db_path = <tt>$db_path</tt><br/>
+template = $template_form<br/>
+css = <tt>$css_file</tt>
 
 <div style="background: #e0e0e0; padding: 0.5em; display: block;">
-       <a href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>&nbsp;
-       <span id="divRecordNr"> none </span>
-       <a href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>&nbsp;
+       <a id="a_left_arr" href="$self?rec=}, $rec - 1, qq{" onClick="return dec_rec();">&#8678;</a>
+       <span id="div_record_nr"> none </span>
+
+       <a id="a_right_arr" href="$self?rec=}, $rec + 1, qq{" onClick="return inc_rec();">&#8680;</a>
+       <a id="a_reload" href="$self?rec=}, $rec, qq{" onClick="return reload_rec();">&#8634;</a>
+       <a href="#" onClick="iwfShowLog(); return false;">&#9636;</a>
+
+</div>
+
+<div>
+
+<div style="display: block;">
+Editor
+<a id="a_template" href="#" onClick="return edit_template();">template</a>
+<a id="a_css" href="#" onClick="return edit_css();">css</a>
+
+<div id="div_template">
+<span style="color: #808080;"> no template loaded yet. </span>
 </div>
 
-<div id="divRecord">
+<div id="div_css" style="position: absolute; display: none;">
+<span style="color: #808080;"> no CSS loaded yet. </span>
+</div>
+
+</div>
+
+<div id="div_record" style="display: block;">
 <span style="color: #808080;"> no record loaded yet. </span>
 </div>
 
+
 </body>
-</html>};
+</html>
+_END_OF_HEAD_
 
-};
+}