You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2005/09/18 00:44:36 UTC

svn commit: r289859 - in /myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom: calendar/HtmlCalendarRenderer.java calendar/resource/popcalendar.js calendar/resource/popcalendar_init.js date/HtmlDateRenderer.java

Author: mmarinschek
Date: Sat Sep 17 15:44:27 2005
New Revision: 289859

URL: http://svn.apache.org/viewcvs?rev=289859&view=rev
Log:
major revamp of inputCalendar

Added:
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar_init.js
Modified:
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar.js
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java?rev=289859&r1=289858&r2=289859&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java Sat Sep 17 15:44:27 2005
@@ -23,6 +23,7 @@
 import org.apache.myfaces.renderkit.html.HtmlRenderer;
 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
+import org.apache.commons.lang.StringEscapeUtils;
 
 import javax.faces.application.Application;
 import javax.faces.component.UIComponent;
@@ -97,7 +98,8 @@
         if(inputCalendar.isRenderAsPopup())
         {
             if(inputCalendar.isAddResources())
-                addScriptAndCSSResources(facesContext, component);
+                addScriptAndCSSResources(facesContext, symbols, months,
+	                    timeKeeper.getFirstDayOfWeek(),inputCalendar);
 
             String dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext,
                     inputCalendar.getPopupDateFormat());
@@ -242,7 +244,8 @@
      * Used by the x:inputDate renderer : HTMLDateRenderer
      * @throws IOException
      */
-    static public void addScriptAndCSSResources(FacesContext facesContext, UIComponent component) throws IOException{
+    static public void addScriptAndCSSResources(FacesContext facesContext, DateFormatSymbols symbols, String[] months, int firstDayOfWeek,
+                                                UIComponent uiComponent) throws IOException{
         // check to see if javascript has already been written (which could happen if more than one calendar on the same page)
         if (facesContext.getExternalContext().getRequestMap().containsKey(JAVASCRIPT_ENCODED))
         {
@@ -252,17 +255,23 @@
         // Add the javascript and CSS pages
         AddResource.addStyleSheet(HtmlCalendarRenderer.class, "WH/theme.css", facesContext);
         AddResource.addStyleSheet(HtmlCalendarRenderer.class, "DB/theme.css", facesContext);
+        AddResource.addJavaScriptToHeader(HtmlCalendarRenderer.class, "popcalendar_init.js", facesContext);
+
+        StringBuffer imageScript = new StringBuffer();
+        appendImageDirectory(imageScript, facesContext);
+        AddResource.addInlineScriptToHeader(imageScript.toString(),facesContext);
+        AddResource.addInlineScriptToHeader(getLocalizedLanguageScript(symbols,months,firstDayOfWeek,
+                uiComponent),facesContext);
         AddResource.addJavaScriptToHeader(HtmlCalendarRenderer.class, "popcalendar.js", facesContext);
 
         ResponseWriter writer = facesContext.getResponseWriter();
 
-        writer.startElement(HTML.SCRIPT_ELEM, component);
+        writer.startElement(HTML.SCRIPT_ELEM, uiComponent);
         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
         StringBuffer script = new StringBuffer();
         script.append("\n");
-        script.append("loadPopupScript();jscalendarSetImageDirectory(\"").append(JavascriptUtils.encodeString(
-                AddResource.getResourceMappedPath(HtmlCalendarRenderer.class, "DB/", facesContext)
-        )).append("\")");
+        script.append("loadPopupScript();");
+        appendImageDirectory(script, facesContext);
         script.append("\n//");
         writer.writeComment(script.toString());
         writer.endElement(HTML.SCRIPT_ELEM);
@@ -270,8 +279,18 @@
         facesContext.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
     }
 
+    private static void appendImageDirectory(StringBuffer script, FacesContext facesContext)
+    {
+        script.append("jscalendarSetImageDirectory(\"");
+        script.append(JavascriptUtils.encodeString(
+                AddResource.getResourceMappedPath(HtmlCalendarRenderer.class, "DB/", facesContext)
+        ));
+        script.append("\");");
+    }
+
     public static String getLocalizedLanguageScript(DateFormatSymbols symbols,
-            String[] months, int firstDayOfWeek, HtmlInputCalendar inputCalendar)
+                                                    String[] months, int firstDayOfWeek,
+                                                    UIComponent uiComponent)
     {
         int realFirstDayOfWeek = firstDayOfWeek-1/*Java has different starting-point*/;
 
@@ -290,26 +309,30 @@
 
         StringBuffer script = new StringBuffer();
         defineStringArray(script, "jscalendarMonthName", months);
+        defineStringArray(script, "jscalendarMonthName2", months);        
         defineStringArray(script, "jscalendarDayName", weekDays);
         setIntegerVariable(script, "jscalendarStartAt",realFirstDayOfWeek);
 
-        if( inputCalendar != null ){ // To allow null parameter for inputDate tag.
-	        if(inputCalendar.getPopupGotoString()!=null)
-	            setStringVariable(script, "jscalendarGotoString",inputCalendar.getPopupGotoString());
-	        if(inputCalendar.getPopupTodayString()!=null)
-	            setStringVariable(script, "jscalendarTodayString",inputCalendar.getPopupTodayString());
-	        if(inputCalendar.getPopupWeekString()!=null)
-	            setStringVariable(script, "jscalendarWeekString",inputCalendar.getPopupWeekString());
-	        if(inputCalendar.getPopupScrollLeftMessage()!=null)
-	            setStringVariable(script, "jscalendarScrollLeftMessage",inputCalendar.getPopupScrollLeftMessage());
-	        if(inputCalendar.getPopupScrollRightMessage()!=null)
-	            setStringVariable(script, "jscalendarScrollRightMessage",inputCalendar.getPopupScrollRightMessage());
-	        if(inputCalendar.getPopupSelectMonthMessage()!=null)
-	            setStringVariable(script, "jscalendarSelectMonthMessage",inputCalendar.getPopupSelectMonthMessage());
-	        if(inputCalendar.getPopupSelectYearMessage()!=null)
-	            setStringVariable(script, "jscalendarSelectYearMessage",inputCalendar.getPopupSelectYearMessage());
-	        if(inputCalendar.getPopupSelectDateMessage()!=null)
-	            setStringVariable(script, "jscalendarSelectDateMessage",inputCalendar.getPopupSelectDateMessage());
+        if( uiComponent instanceof HtmlInputCalendar ){
+
+            HtmlInputCalendar inputCalendar = (HtmlInputCalendar) uiComponent;
+
+            if(inputCalendar.getPopupGotoString()!=null)
+                setStringVariable(script, "jscalendarGotoString", inputCalendar.getPopupGotoString());
+            if(inputCalendar.getPopupTodayString()!=null)
+                setStringVariable(script, "jscalendarTodayString",inputCalendar.getPopupTodayString());
+            if(inputCalendar.getPopupWeekString()!=null)
+                setStringVariable(script, "jscalendarWeekString",inputCalendar.getPopupWeekString());
+            if(inputCalendar.getPopupScrollLeftMessage()!=null)
+                setStringVariable(script, "jscalendarScrollLeftMessage",inputCalendar.getPopupScrollLeftMessage());
+            if(inputCalendar.getPopupScrollRightMessage()!=null)
+                setStringVariable(script, "jscalendarScrollRightMessage",inputCalendar.getPopupScrollRightMessage());
+            if(inputCalendar.getPopupSelectMonthMessage()!=null)
+                setStringVariable(script, "jscalendarSelectMonthMessage",inputCalendar.getPopupSelectMonthMessage());
+            if(inputCalendar.getPopupSelectYearMessage()!=null)
+                setStringVariable(script, "jscalendarSelectYearMessage",inputCalendar.getPopupSelectYearMessage());
+            if(inputCalendar.getPopupSelectDateMessage()!=null)
+                setStringVariable(script, "jscalendarSelectDateMessage",inputCalendar.getPopupSelectDateMessage());
         }
 
         return script.toString();
@@ -327,7 +350,7 @@
     {
         script.append(name);
         script.append(" = \"");
-        script.append(value);
+        script.append(StringEscapeUtils.escapeJavaScript(value));
         script.append("\";\n");
     }
 
@@ -342,7 +365,7 @@
                 script.append(",");
 
             script.append("\"");
-            script.append(array[i]);
+            script.append(StringEscapeUtils.escapeJavaScript(array[i]));
             script.append("\"");
         }
 
@@ -369,7 +392,7 @@
 
             if(popupButtonString==null)
                 popupButtonString="...";
-            writer.writeAttribute(HTML.VALUE_ATTR, popupButtonString, null);
+            writer.writeAttribute(HTML.VALUE_ATTR, StringEscapeUtils.escapeJavaScript(popupButtonString), null);
             /*
             if (renderButtonAsImage) {
                 writer.writeAttribute(HTML.ID_ATTR, buttonId, null);

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar.js
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar.js?rev=289859&r1=289858&r2=289859&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar.js (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar.js Sat Sep 17 15:44:27 2005
@@ -4,23 +4,6 @@
 //
 // Modified to use the MyFaces lib resources
 
-var	jscalendarFixedX = -1			// x position (-1 if to appear below control)
-var	jscalendarFixedY = -1			// y position (-1 if to appear below control)
-var jscalendarStartAt = 1			// 0 - sunday ; 1 - monday
-var jscalendarShowWeekNumber = 1	// 0 - don't show; 1 - show
-var jscalendarShowToday = 1		// 0 - don't show; 1 - show
-var jscalendarImgDir = "jscalendar/jscalendar-DB/"			// directory for images ... e.g. var jscalendarImgDir="/img/"
-var jscalendarThemePrefix = "jscalendar-DB"
-
-var jscalendarGotoString = "Go To Current Month"
-var jscalendarTodayString = "Today is"
-var jscalendarWeekString = "Wk"
-var jscalendarScrollLeftMessage = "Click to scroll to previous month. Hold mouse button to scroll automatically."
-var jscalendarScrollRightMessage = "Click to scroll to next month. Hold mouse button to scroll automatically."
-var jscalendarSelectMonthMessage = "Click to select a month."
-var jscalendarSelectYearMessage = "Click to select a year."
-var jscalendarSelectDateMessage = "Select [date] as date." // do not replace [date], it will be replaced by date.
-
 var	jscalendarCrossobj, jscalendarCrossMonthObj, jscalendarCrossYearObj,
     jscalendarMonthSelected, jscalendarYearSelected, jscalendarDateSelected,
     jscalendarOmonthSelected, jscalendarOyearSelected, jscalendarOdateSelected,
@@ -44,10 +27,6 @@
 var jscalendarMyFacesCtlType = "x:inputCalendar";
 var jscalendarMyFacesInputDateClientId;
 
-function jscalendarSetImageDirectory(dir){ // For MyFaces only
-	jscalendarImgDir = dir;
-}
-
 /* hides <select> and <applet> objects (for IE only) */
 function jscalendarHideElement( elmID, overDiv ){
   if( jscalendarIe ){
@@ -121,10 +100,6 @@
 
 	document.write ("</table></div><div id='selectMonth' class='"+jscalendarThemePrefix+"-div-style'></div><div id='selectYear' class='"+jscalendarThemePrefix+"-div-style'></div>");
 }
-
-var	jscalendarMonthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
-var	jscalendarMonthName2 = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
-var	jscalendarDayName = jscalendarStartAt==0 ? new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat") : new Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
 
 function jscalendarSwapImage(srcImg, destImg){
 	if (jscalendarIe)

Added: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar_init.js
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar_init.js?rev=289859&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar_init.js (added)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/calendar/resource/popcalendar_init.js Sat Sep 17 15:44:27 2005
@@ -0,0 +1,25 @@
+var	jscalendarFixedX = -1			// x position (-1 if to appear below control)
+var	jscalendarFixedY = -1			// y position (-1 if to appear below control)
+var jscalendarStartAt = 1			// 0 - sunday ; 1 - monday
+var jscalendarShowWeekNumber = 1	// 0 - don't show; 1 - show
+var jscalendarShowToday = 1		// 0 - don't show; 1 - show
+var jscalendarImgDir = "not available"			// directory for images ... e.g. var jscalendarImgDir="/img/"
+var jscalendarThemePrefix = "jscalendar-DB"
+
+var jscalendarGotoString = "Go To Current Month"
+var jscalendarTodayString = "Today is"
+var jscalendarWeekString = "Wk"
+var jscalendarScrollLeftMessage = "Click to scroll to previous month. Hold mouse button to scroll automatically."
+var jscalendarScrollRightMessage = "Click to scroll to next month. Hold mouse button to scroll automatically."
+var jscalendarSelectMonthMessage = "Click to select a month."
+var jscalendarSelectYearMessage = "Click to select a year."
+var jscalendarSelectDateMessage = "Select [date] as date." // do not replace [date], it will be replaced by date.
+
+var	jscalendarMonthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
+var	jscalendarMonthName2 = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
+var	jscalendarDayName = jscalendarStartAt==0 ? new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat") : new Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
+
+
+function jscalendarSetImageDirectory(dir){ // For MyFaces only
+    jscalendarImgDir = dir;
+}
\ No newline at end of file

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java?rev=289859&r1=289858&r2=289859&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java Sat Sep 17 15:44:27 2005
@@ -55,7 +55,7 @@
     private static final String ID_HOURS_POSTFIX = ".hours";
     private static final String ID_MINUTES_POSTFIX = ".minutes";
     private static final String ID_SECONDS_POSTFIX = ".seconds";
-    
+
     protected boolean isDisabled(FacesContext facesContext, HtmlInputDate inputDate) {
         if( !UserRoleUtils.isEnabledOnUserRole(inputDate) )
             return false;
@@ -200,16 +200,19 @@
     }
     
     protected void encodePopupCalendarButton(FacesContext facesContext, UIComponent uiComponent, ResponseWriter writer, String clientId, Locale currentLocale) throws IOException{
-        HtmlCalendarRenderer.addScriptAndCSSResources(facesContext, uiComponent);
-        
+
         DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
 
+        HtmlCalendarRenderer.addScriptAndCSSResources(facesContext, symbols, 
+                HtmlCalendarRenderer.mapMonths(symbols), Calendar.getInstance(currentLocale).getFirstDayOfWeek(),
+                uiComponent);
+
         String localizedLanguageScript = HtmlCalendarRenderer.getLocalizedLanguageScript(
                 							symbols,
                 							HtmlCalendarRenderer.mapMonths(symbols),
                 							Calendar.getInstance(currentLocale).getFirstDayOfWeek(),
                 							null);
-        
+
         writer.startElement(HTML.SCRIPT_ELEM,uiComponent);
         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
         	writer.write(localizedLanguageScript);
@@ -218,18 +221,18 @@
             //writer.write("\n}");
 
         writer.endElement(HTML.SCRIPT_ELEM);
-        
+
         String dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext, null);
-        
+
         // render the button
         writer.startElement(HTML.INPUT_ELEM, uiComponent);
         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_BUTTON, null);
-        
+
         String jsCalendarFunctionCall = "jscalendarPopUpCalendarForInputDate('"+clientId+"','"+dateFormat+"')";
         writer.writeAttribute(HTML.ONCLICK_ATTR, jsCalendarFunctionCall, null);
         writer.writeAttribute(HTML.VALUE_ATTR, "...", null);
         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
-        
+
         writer.endElement(HTML.INPUT_ELEM);
     }