[LON-CAPA-cvs] cvs: loncom /interface lonhtmlcommon.pm

faziophi faziophi@source.lon-capa.org
Tue, 22 Dec 2009 05:33:37 -0000


faziophi		Tue Dec 22 05:33:37 2009 EDT

  Modified files:              
    /loncom/interface	lonhtmlcommon.pm 
  Log:
  -- Adds jQuery script tags to HTML output
  -- Adds CKEditor script tag where FCKEditor is enabled, while the two editors persist
  -- Adds JavaScript functions to detect block-level HTML in a text-area,
     create and destroy rich text editors on demand using jQuery.
  -- Adds JavaScript event handler using jQuery to toggle text and CSS classes,
     and create/destroy editors.
  -- Adds window.onload code to generate CKEditor instances where desired
     using HTML classes "LC_richAlwaysOn", "LC_richDetectHTML", "LC_richDefaultOn"
     and "LC_richDefaultOff"
  -- When editors are not generated but are allowed, links saying "Rich Formatting >>" 
     are dynamically inserted, and if editor is generated, link saying "<< Plain text"
     is provided to disable editor if allowed
  
  
  
Index: loncom/interface/lonhtmlcommon.pm
diff -u loncom/interface/lonhtmlcommon.pm:1.254 loncom/interface/lonhtmlcommon.pm:1.255
--- loncom/interface/lonhtmlcommon.pm:1.254	Fri Dec 11 17:25:01 2009
+++ loncom/interface/lonhtmlcommon.pm	Tue Dec 22 05:33:36 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.254 2009/12/11 17:25:01 bisitz Exp $
+# $Id: lonhtmlcommon.pm,v 1.255 2009/12/22 05:33:36 faziophi Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -383,8 +383,7 @@
 
 The current setting for this time parameter.  A unix format time
 (time in seconds since the beginning of Jan 1st, 1970, GMT.  
-An undefined value is taken to indicate the value is the current time
-unless it is requested to leave it empty. See $includeempty. 
+An undefined value is taken to indicate the value is the current time.
 Also, to be explicit, a value of 'now' also indicates the current time.
 
 =item $special
@@ -394,9 +393,6 @@
 
 =item $includeempty 
 
-If it is set (true) and no date/time value is provided,
-the date/time fields are left empty.
-
 =item $state
 
 Specifies the initial state of the form elements.  Either 'disabled' or empty.
@@ -416,40 +412,42 @@
     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
     my $now = time;
-
-    my $tzname;
-    my ($sec,$min,$hour,$mday,$month,$year) = ('','',undef,'','','');
-    # other potentially useful values:     wkday,yrday,is_daylight_savings
-
+    my $wasdefined=1;
     if (! defined($state) || $state ne 'disabled') {
         $state = '';
     }
     if (! defined($no_hh_mm_ss)) {
         $no_hh_mm_ss = 0;
     }
-    
     if ($currentvalue eq 'now') {
-        $currentvalue = $now;
+	$currentvalue = $now;
     }
-        
-    # Default value: Set empty date field to current time
-    # unless empty inclusion is requested
-    if ((!$includeempty) && (!$currentvalue)) {
-            $currentvalue = $now;
+    if ((!defined($currentvalue)) || ($currentvalue eq '')) {
+	$wasdefined=0;
+	if ($includeempty) {
+	    $currentvalue = 0;
+	} else {
+	    $currentvalue = $now;
+	}
     }
-    # Do we have a date? Split it!
+    # other potentially useful values:     wkday,yrday,is_daylight_savings
+    my $tzname;
+    my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
     if ($currentvalue) {
-        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue);
-
-        # No values provided for hour, min, sec? Use default 0
-        if (($defhour) || ($defmin) || ($defsec)) {
-            $sec  = ($defsec  ? $defsec  : 0);
-            $min  = ($defmin  ? $defmin  : 0);
-            $hour = ($defhour ? $defhour : 0);
-        }
+        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue); 
+    }
+    unless ($wasdefined) {
+        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now);
+	if (($defhour) || ($defmin) || ($defsec)) {
+	    $sec=($defsec?$defsec:0);
+	    $min=($defmin?$defmin:0);
+	    $hour=($defhour?$defhour:0);
+	} elsif (!$includeempty) {
+	    $sec=0;
+	    $min=0;
+	    $hour=0;
+	}
     }
-
-    # Create Output
     my $result = "\n<!-- $dname date setting form -->\n";
     $result .= <<ENDJS;
 <script type="text/javascript">
@@ -1243,11 +1241,18 @@
 }
 
 sub htmlareaheaders {
-    return if (&htmlareablocked());
-    return if (!&htmlareabrowser());
-    return (<<ENDHEADERS);
+	my $s="";
+	if (!&htmlareablocked() && &htmlareabrowser()) {
+		$s.=(<<ENDEDITOR);
 <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
-ENDHEADERS
+<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
+ENDEDITOR
+	}
+    $s.=(<<ENDJQUERY);
+<script type="text/javascript" src="/adm/jQuery/js/jquery-1.3.2.min.js"></script>
+<script type="text/javascript" src="/adm/jQuery/js/jquery-ui-1.7.2.custom.min.js"></script>
+ENDJQUERY
+	return $s;
 }
 
 # ----------------------------------------------------------------- Preferences
@@ -1286,17 +1291,74 @@
     my $output='<script type="text/javascript" defer="1">'."\n"
               .'// <![CDATA['."\n";
     my $lang = &htmlarea_lang();
-    foreach my $field (@fields) {
-	$output.="
-{
-    var oFCKeditor = new FCKeditor('$field');
-    oFCKeditor.Config['CustomConfigurationsPath'] = 
-	'/fckeditor/loncapaconfig.js';    
-    oFCKeditor.ReplaceTextarea();
-    oFCKeditor.Config['AutoDetectLanguage'] = false;
-    oFCKeditor.Config['DefaultLanguage'] = '$lang';
-}";
+    $output.='
+    
+    function containsBlockHtml(id) {
+		var re = $("#"+id).html().search(/(?:\&lt\;|\<)(br|h1|h2|h3|h4|h5|h6|p|ol|ul|table|pre|address|blockquote|center|div)[\s]*((?:\/[\s]*(?:\&gt\;|\>)|(?:\&gt\;|\>)[\s\S]*(?:\&lt\;|\<)\/[\s]*\1[\s]*\(?:\&gt\;|\>))/im);
+    	return (re >= 0);
+    }
+    
+    function startRichEditor(id) {
+    	CKEDITOR.replace(id, 
+    		{
+    			customConfig: "/ckeditor/loncapaconfig.js",
+    		}
+    	);
     }
+    
+    function destroyRichEditor(id) {
+    	CKEDITOR.instances[id].destroy();
+    }
+    
+    function editorHandler(event) {
+    	var rawid = $(this).attr("id");
+    	var id = new RegExp("LC_rt_(.*)").exec(rawid)[1]
+    	event.preventDefault();
+    	if ($(this).hasClass("LC_enable_rt")) {
+    		startRichEditor(id);
+			$("#LC_rt_"+id).html("<b>&laquo; Plain text</b>");
+			$("#LC_rt_"+id).attr("title", "Disable rich text formatting and edit in plain text");
+			$("#LC_rt_"+id).addClass("LC_disable_rt");
+			$("#LC_rt_"+id).removeClass("LC_enable_rt");
+    	} else {
+			destroyRichEditor(id);
+			$("#LC_rt_"+id).html("<b>Rich formatting &raquo;</b>");
+			$("#LC_rt_"+id).attr("title", "Enable rich text formatting (bold, italic, etc.)");
+			$("#LC_rt_"+id).addClass("LC_enable_rt");
+			$("#LC_rt_"+id).removeClass("LC_disable_rt");
+		}
+	}
+    
+    $(document).ready(function(){
+		$(".LC_richAlwaysOn").each(function() {
+			startRichEditor($(this).attr("id"));
+		});
+		$(".LC_richDetectHtml").each(function() {
+			var id = $(this).attr("id");
+			if(containsBlockHtml(id)) {
+				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
+				startRichEditor(id);
+				$("#LC_rt_"+id).click(editorHandler);				
+			}
+			else {
+				$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
+				$("#LC_rt_"+id).click(editorHandler);
+			}
+		});
+		$(".LC_richDefaultOn").each(function() {
+			var id = $(this).attr("id");
+			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Disable rich text formatting and edit in plain text\" class=\"LC_disable_rt\"><b>&laquo; Plain text</b></a></div>");				
+			startRichEditor(id);
+			$("#LC_rt_"+id).click(editorHandler);
+		});
+		$(".LC_richDefaultOff").each(function() {
+			var id = $(this).attr("id");
+			$(this).before("<div><a href=\"#\" id=\"LC_rt_"+id+"\" title=\"Enable rich text formatting (bold, italic, etc.)\" class=\"LC_enable_rt\"><b>Rich formatting &raquo;</b></a></div>");
+			$("#LC_rt_"+id).click(editorHandler);			
+		});
+	});
+';
+    
     $output.="\nwindow.status='Activated Editfields';\n"
             .'// ]]>'."\n"
             .'</script><br />'.