File:  [LON-CAPA] / loncom / homework / daxepage.pm
Revision 1.12: download - view: text, annotated - select for diffs
Sat Mar 23 23:11:52 2024 UTC (7 weeks, 3 days ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Buttons above Daxe Editor iframe include: "Save and Edit", "Save and View",
  and "Discard and View" for consistency with UI for other editors.

    1: # The LearningOnline Network
    2: # Page with Daxe on the left side and the preview on the right side
    3: #
    4: # $Id: daxepage.pm,v 1.12 2024/03/23 23:11:52 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::daxepage;
   31: use strict;
   32: 
   33: use Apache::loncommon();
   34: use Apache::lonnet();
   35: use Apache::lonhtmlcommon();
   36: use Apache::lonxml();
   37: use Apache::edit();
   38: use Apache::lonmenu();
   39: use Apache::lonlocal;
   40: use Apache::Constants qw(:common);
   41: use LONCAPA qw(:DEFAULT :match);
   42: use HTML::Entities();
   43: 
   44: sub handler {
   45:     my $request = shift;
   46:     my $uri = $request->uri;
   47:     $uri =~ s{^/daxepage}{};
   48:     &Apache::loncommon::content_type($request,'text/html');
   49:     my ($is_not_assess,$is_assess);
   50:     if ($uri =~/\.(xml|html|htm|xhtml|xhtm)$/) {
   51:         $is_not_assess = 1;
   52:     } elsif ($uri =~ /$LONCAPA::assess_re/) {
   53:         unless ($uri =~ /\.form$/) {
   54:             $is_assess = 1;
   55:         }
   56:     }
   57:     unless ($is_not_assess || $is_assess) {
   58:         $request->status(406);
   59:         return OK;
   60:     }
   61:     my %editors = &Apache::loncommon::permitted_editors();
   62:     unless ($editors{'daxe'}) {
   63:         my $msg = '<p class="LC_warning">'.
   64:                   &mt('Daxe editor is not enabled for this Authoring Space.').'</p>';
   65:         &do_redirect($request,$uri,$msg);
   66:         return OK;
   67:     }
   68:     if ($is_not_assess) {
   69:         delete($editors{'xml'});
   70:         $editors{'edit'} = 1;
   71:     }
   72:     my %lt = &Apache::lonlocal::texthash(
   73:                                           'noif' => 'No iframe support.',
   74:                                           'show' => 'Show content in pop-up window',
   75:                                           'save' => 'Save',
   76:                                           'oeds' => 'other editors',
   77:                                           'othe' => 'other editor',
   78:                                           'edit' => 'Save and Edit',
   79:                                           'disc' => 'Discard and View',
   80:                                           'save' => 'Save and View',
   81:                                         );
   82:     my $name = $uri;
   83:     $name =~ s/^.*\/([^\/]+)$/$1/;
   84:     my $filearg = '/daxeopen'.$uri;
   85:     my $daxeurl = '/adm/daxe/daxe.html?config=config/loncapa_config.xml&save=/daxesave'.
   86:                   '&file='.$filearg;
   87:     my $headjs = &Apache::loncommon::iframe_wrapper_headjs().
   88:                  &listener_js($filearg).
   89:                  &toggle_LCmenus_js().&saveandview_js().
   90:                  &Apache::edit::js_change_detection();
   91: 
   92:     my ($clickexit,$clicksave,$clickedit);
   93:     if ($is_assess) {
   94:         $headjs .= &Apache::lonxml::setmode_javascript();
   95:         $clickexit = "javascript:setmode(this.form,'view');";
   96:     } else {
   97:         $headjs .= &Apache::lonxml::seteditor_javascript();
   98:         $clickexit = "javascript:seteditmode(this.form,'view');";
   99:     }
  100:     $clicksave = "javascript:daxesave('exit');";
  101:     $clickedit = "javascript:daxesave();";
  102:     my $form_events = &Apache::edit::form_change_detection();
  103:     my $editheader = '<form '.$form_events.' method="post" name="daxeedit" action="'.$uri.'">'.
  104:                      '<input type="hidden" name="problemmode" value="daxe" />'."\n".
  105:                      '<div class="LC_edit_problem_editxml_header">'."\n";
  106:     my $saveeditbutton = '<input type="button" name="submitmode" accesskey="s" value="'.$lt{'edit'}.
  107:                      '" onclick="'.$clickedit.'" />'."\n";
  108:     my $exitbutton = '<input type="button" name="submitmode" accesskey="d" value="'.$lt{'disc'}.
  109:                      '" onclick="'.$clickexit.'" />'."\n";
  110:     my $saveexitbutton = '<input type="button" name="submitmode" accesskey="v" value="'.$lt{'save'}.
  111:                      '" onclick="'.$clicksave.'" />'."\n";
  112:     $editheader .= '<table class="LC_edit_problem_header_title"><tr><td>'.
  113:                    $uri.'</td><td align="right"><span class="LC_nobreak">'.
  114:                    $saveeditbutton.$saveexitbutton.$exitbutton.'</span>';
  115:     if ($editors{'edit'} || $editors{'xml'}) {
  116:         my $other = (($editors{'edit'} && $editors{'xml'})? $lt{'oeds'} : $lt{'othe'});
  117:         $editheader .= '&nbsp;&nbsp;|&nbsp;&nbsp;<span class="LC_nobreak">'.$other.':</span> '.
  118:                        '<span class="LC_nobreak">';
  119:         if ($is_not_assess) {
  120:             $editheader .= '<input type="hidden" name="editmode" value="" />'."\n".
  121:                            '<input type="button" name="editordefault" value="'.$lt{'edit'}.
  122:                            '" onclick="seteditmode(this.form,'."'edit'".');" />'."\n";
  123:         } else {
  124:             if ($editors{'edit'}) {
  125:                 $editheader .= '<input type="button" name="submitmode" accesskey="e" value="'.&mt('Edit').'" '.
  126:                                'onclick="javascript:setmode(this.form,'."'edit'".')" />'."\n";
  127:             }
  128:             if ($editors{'xml'}) {
  129:                 $editheader .= '<input type="button" name="submitmode" accesskey="x" value="'.&mt('EditXML').'" '.
  130:                                'onclick="javascript:setmode(this.form,'."'editxml'".')" />'."\n";
  131:             }
  132:         }
  133:         $editheader .= '</span>';
  134:     }
  135:     $editheader .= '</td></tr></table></div></form>'."\n";
  136:     my $start_collapsed = &collapsible_std_LCmenus();
  137:     my $args = {
  138:                 'collapsible_header' => $editheader,
  139:                 'start_collapsed'    => $start_collapsed,
  140:                };
  141:     my $startpage = &Apache::loncommon::start_page('Daxe: '.$name,$headjs,$args).
  142:                     &Apache::lonmenu::constspaceform();
  143:     my $endpage = &Apache::loncommon::end_page();
  144: 
  145:     # javascript will position the iframe if window was resized (or zoomed)
  146:     my $script = &Apache::loncommon::iframe_wrapper_resizejs();
  147:     my $dest = &HTML::Entities::encode($daxeurl,'&<>"');
  148:     my $noiframe = &Apache::loncommon::modal_link($dest,$lt{'show'},500,400);
  149: 
  150:     $request->print(<<"ENDFRAME");
  151: $startpage
  152: $script
  153: <div class="LC_iframecontainer" style="padding-right: 5px">
  154: <iframe src="$dest" id="lcdiframe">$lt{'noif'} $noiframe</iframe>
  155: </div>
  156: $endpage
  157: ENDFRAME
  158:     return OK;
  159: }
  160: 
  161: sub listener_js {
  162:     my ($filearg) = @_;
  163:     return <<"ENDJS";
  164: <script type="text/javascript">
  165: //<![CDATA[
  166: 
  167: var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
  168: var eventer = window[eventMethod];
  169: var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
  170: 
  171: eventer(messageEvent,function(e) {
  172:     var reqdOrigin = window.location.protocol+'//'+window.location.hostname;
  173:     if (e.origin == reqdOrigin) {
  174:         if (e.data == '$filearg') {
  175:             setmode(document.daxeedit,'view');
  176:         }
  177:         return;
  178:     }
  179: },false);
  180: 
  181: //]]>
  182: </script>
  183: ENDJS
  184: 
  185: }
  186: 
  187: sub saveandview_js {
  188:     return <<"ENDJS";
  189: 
  190: <script type="text/javascript">
  191: //<![CDATA[
  192: 
  193: function daxesave(exit) {
  194:     window.myIframe = document.getElementById("lcdiframe").contentWindow;
  195:     window.myIframe.focus();
  196:     window.myIframe.savelcdoc(exit);
  197:     return;
  198: }
  199: 
  200: //]]>
  201: </script>
  202: ENDJS
  203: }
  204: 
  205: sub toggle_LCmenus_js {
  206:     my %lt = &Apache::lonlocal::texthash(
  207:                                          altc => 'menu state: collapsed',
  208:                                          alte => 'menu state: explanded',
  209:                                          ttlc => 'display standard menus',
  210:                                          ttle => 'hide standard menus',
  211:     );
  212:     return <<"ENDJS";
  213: <script type="text/javascript">
  214: //<![CDATA[
  215: \$(document).ready (function () {
  216:     \$(".LC_collapse_trigger").on("click", function (e) {
  217:         var id = this.id;
  218:         var \$content = \$(this).next (".LC_menus_content");
  219:         var expanded = \$content.hasClass ("shown");
  220:         if (expanded) {
  221:             \$content.removeClass ("shown");
  222:             \$content.addClass ("hidden");
  223:         } else {
  224:             \$content.removeClass ("hidden");
  225:             \$content.addClass ("shown");
  226:         }
  227: 
  228:         \$(this).find ("[aria-expanded]")
  229:         .attr ("aria-expanded", !expanded);
  230: 
  231:         \$(this).find ("[aria-pressed]")
  232:         .attr ("aria-pressed", !expanded);
  233: 
  234:         \$(this).find (".LC_collapsible_indicator").attr ({
  235:         "src":
  236:         (expanded)? "/res/adm/pages/collapsed.png" : "/res/adm/pages/expanded.png",
  237:         "alt":
  238:         (expanded)? "$lt{altc}" : "$lt{alte}",
  239:         "title":
  240:         (expanded)? "$lt{ttlc}" : "$lt{ttle}"
  241:         });
  242: 
  243:         \$(window).trigger('resize');
  244:         \$(this).focus ();
  245:     });
  246: 
  247:     \$("#LC_expandingContainer").attr ("aria-live", "off");
  248: });
  249: //]]>
  250: </script>
  251: ENDJS
  252: 
  253: }
  254: 
  255: sub do_redirect {
  256:     my ($request,$uri,$msg) = @_;
  257:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  258:     $request->print(
  259:         &Apache::loncommon::start_page('Authoring Space',undef,
  260:                                        {'redirect'       => [2,$uri]}).
  261: 
  262:         '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
  263:         "$msg\n".
  264:         &Apache::loncommon::end_page());
  265:     return;
  266: }
  267: 
  268: sub collapsible_std_LCmenus {
  269:     my $daxecollapse = $Apache::lonnet::env{'environment.daxecollapse'};
  270:     unless ($daxecollapse) {
  271:         my %domdefs = &Apache::lonnet::get_domain_defaults($Apache::lonnet::env{'user.domain'});
  272:         if ($domdefs{'daxecollapse'}) {
  273:             $daxecollapse = 'yes';
  274:         }
  275:     }
  276:     if ($daxecollapse eq 'yes') {
  277:         return 1;
  278:     }
  279:     return;
  280: }
  281: 
  282: 1;
  283: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>