Author: nbelaevski
Date: 2007-07-05 19:06:10 -0400 (Thu, 05 Jul 2007)
New Revision: 1510
Modified:
branches/3.0.2/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/css/calendar.xcss
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/scripts/calendar.js
branches/3.0.2/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx
Log:
Calendar is AJAXable now
Modified:
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java
===================================================================
---
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java 2007-07-05
22:14:58 UTC (rev 1509)
+++
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java 2007-07-05
23:06:10 UTC (rev 1510)
@@ -21,17 +21,31 @@
package org.richfaces.component;
+import java.io.IOException;
+import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Set;
import java.util.TimeZone;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.FacesEvent;
+import javax.faces.render.Renderer;
+import org.ajax4jsf.framework.ajax.AjaxChildrenEncoder;
+import org.ajax4jsf.framework.ajax.AjaxContext;
+import org.ajax4jsf.framework.ajax.AjaxEvent;
+import org.richfaces.renderkit.CalendarDataModelItemAdaptor;
+import org.richfaces.renderkit.CalendarRendererBase;
+
/**
* JSF component class
*
@@ -75,9 +89,11 @@
Calendar calendar = Calendar.getInstance(getTimeZone(), getLocale());
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
+ /*
//force recalculation
calendar.getTimeInMillis();
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
+ */
return calendar.getTime();
}
@@ -85,9 +101,11 @@
Calendar calendar = Calendar.getInstance(getTimeZone(), getLocale());
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
+ /*
//force recalculation
calendar.getTimeInMillis();
calendar.set(Calendar.DAY_OF_WEEK, getLastDayOfWeek(calendar));
+ */
return calendar.getTime();
}
@@ -110,6 +128,34 @@
return TimeZone.getDefault();
}
+ public void broadcast(FacesEvent event) throws AbortProcessingException {
+ // TODO Auto-generated method stub
+ super.broadcast(event);
+ if (event instanceof AjaxEvent) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
+
+ ajaxContext.setResponseData(getPreload());
+ }
+ }
+
+ public Object getPreload() {
+ Date[] preloadDateRange = getPreloadDateRange();
+ if (preloadDateRange != null && preloadDateRange.length != 0) {
+ CalendarDataModel calendarDataModel = (CalendarDataModel) getData();
+ if (calendarDataModel != null) {
+ CalendarDataModelItem[] calendarDataModelItems =
calendarDataModel.getData(preloadDateRange);
+
+ HashMap args = new HashMap();
+ args.put("startDate",
CalendarRendererBase.formatDate(preloadDateRange[0]));
+ args.put("days", new AdaptingCollection(calendarDataModelItems));
+ return args;
+ }
+ }
+
+ return null;
+ }
+
public Date[] getPreloadDateRange() {
Date dateRangeBegin = this.getPreloadDateRangeBegin();
Date dateRangeEnd = this.getPreloadDateRangeEnd();
@@ -133,4 +179,48 @@
return (Date[]) dates.toArray(new Date[dates.size()]);
}
+
+
+ public void encodeAjaxChild(FacesContext context, String path, Set ids,
+ Set renderedAreas) throws IOException {
+
+ Renderer renderer = this.getRenderer(context);
+ if (renderer instanceof AjaxChildrenEncoder) {
+ ((AjaxChildrenEncoder) renderer).encodeAjaxChild(context, path, ids, renderedAreas);
+ }
+ }
}
+
+class AdaptingCollection extends AbstractCollection {
+ private CalendarDataModelItem[] items;
+
+ public AdaptingCollection(CalendarDataModelItem[] items) {
+ super();
+ this.items = items;
+ }
+
+ public Iterator iterator() {
+ return new Iterator() {
+ private int idx = 0;
+ private CalendarDataModelItemAdaptor adaptor = new CalendarDataModelItemAdaptor();
+
+ public boolean hasNext() {
+ return idx < items.length;
+ }
+
+ public Object next() {
+ adaptor.setItem(items[idx++]);
+ return adaptor;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ };
+ }
+
+ public int size() {
+ return this.items.length;
+ }
+}
Modified:
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
===================================================================
---
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2007-07-05
22:14:58 UTC (rev 1509)
+++
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2007-07-05
23:06:10 UTC (rev 1510)
@@ -27,12 +27,17 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
+import org.ajax4jsf.framework.ajax.AjaxEvent;
+import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
import org.ajax4jsf.framework.util.javascript.JSFunction;
+import org.ajax4jsf.framework.util.javascript.JSFunctionDefinition;
+import org.ajax4jsf.framework.util.javascript.JSReference;
import org.ajax4jsf.framework.util.javascript.ScriptUtils;
import org.richfaces.component.CalendarDataModel;
import org.richfaces.component.CalendarDataModelItem;
@@ -46,6 +51,9 @@
*/
public class CalendarRendererBase extends TemplateEncoderRendererBase {
+ public static final String DATE_SCROLL = "DateScroll";
+
+
/* (non-Javadoc)
* @see org.ajax4jsf.framework.renderer.RendererBase#getComponentClass()
*/
@@ -60,6 +68,28 @@
return HtmlUtil.qualifySize(size.toString());
}
+
+ protected void doDecode(FacesContext context, UIComponent component) {
+ // TODO Auto-generated method stub
+ super.doDecode(context, component);
+
+ String clientId = component.getClientId(context);
+ Map requestParameterMap = context.getExternalContext().getRequestParameterMap();
+ String currentDateString = (String) requestParameterMap.get(clientId + DATE_SCROLL);
+ if (currentDateString != null) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ calendar.set(Calendar.DATE, 1);
+ int idx = currentDateString.indexOf('/');
+ calendar.set(Calendar.MONTH, Integer.parseInt(currentDateString.substring(0, idx)));
+ calendar.set(Calendar.YEAR, Integer.parseInt(currentDateString.substring(idx + 1)));
+
+ ((UICalendar) component).setCurrentDate(calendar.getTime());
+
+ new AjaxEvent(component).queue();
+ }
+ }
+
public void encodeChildren(FacesContext context, UIComponent calendar)
throws IOException {
@@ -74,22 +104,49 @@
}
public void writePreloadBody(FacesContext context, UICalendar calendar) throws
IOException {
+ Object preload = calendar.getPreload();
+ if (preload != null) {
+ ResponseWriter writer = context.getResponseWriter();
+ writer.write(ScriptUtils.toScript(preload));
+ }
+ }
+
+ public void writeSubmitFunction(FacesContext context, UICalendar calendar) throws
IOException {
ResponseWriter writer = context.getResponseWriter();
- Date[] preloadDateRange = calendar.getPreloadDateRange();
- if (preloadDateRange != null && preloadDateRange.length != 0) {
- CalendarDataModel calendarDataModel = (CalendarDataModel) calendar.getData();
- if (calendarDataModel != null) {
- CalendarDataModelItem[] calendarDataModelItems =
calendarDataModel.getData(preloadDateRange);
-
- HashMap args = new HashMap();
- args.put("startDate", formatDate(preloadDateRange[0]));
- args.put("days", new AdaptingCollection(calendarDataModelItems));
- writer.write(ScriptUtils.toScript(args));
- }
+
+ String clientId = calendar.getClientId(context);
+
+ JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(calendar, context,
+ AjaxRendererUtils.AJAX_FUNCTION_NAME);
+ ajaxFunction.addParameter(JSReference.NULL);
+ Map options = AjaxRendererUtils.buildEventOptions(context, calendar);
+ options.put("calendar", JSReference.THIS);
+ //ajax single
+ options.put("control", new JSReference(JSReference.THIS.toScript(),
"id"));
+
+ String oncomplete = AjaxRendererUtils.getAjaxOncomplete(calendar);
+ JSFunctionDefinition oncompleteDefinition = new JSFunctionDefinition();
+ oncompleteDefinition.addParameter("request");
+ oncompleteDefinition.addParameter("event");
+ oncompleteDefinition.addParameter("data");
+ oncompleteDefinition.addToBody("this.calendar.load(data);");
+ if (oncomplete != null) {
+ oncompleteDefinition.addToBody(oncomplete);
}
+
+ options.put("oncomplete", oncompleteDefinition);
+ Map parametersMap = (Map) options.get("parameters");
+ JSReference requestValue = new JSReference("requestValue");
+ parametersMap.remove(clientId);
+ parametersMap.put(clientId + DATE_SCROLL, requestValue);
+ ajaxFunction.addParameter(options);
+ JSFunctionDefinition definition = new JSFunctionDefinition();
+ definition.addParameter(requestValue);
+ definition.addToBody(ajaxFunction);
+ writer.write(definition.toScript());
}
- protected static Object formatDate(Date date) {
+ public static Object formatDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
JSFunction result = new JSFunction("new Date");
@@ -102,36 +159,3 @@
}
-class AdaptingCollection extends AbstractCollection {
- private CalendarDataModelItem[] items;
-
- public AdaptingCollection(CalendarDataModelItem[] items) {
- super();
- this.items = items;
- }
-
- public Iterator iterator() {
- return new Iterator() {
- private int idx = 0;
- private CalendarDataModelItemAdaptor adaptor = new CalendarDataModelItemAdaptor();
-
- public boolean hasNext() {
- return idx < items.length;
- }
-
- public Object next() {
- adaptor.setItem(items[idx++]);
- return adaptor;
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- };
- }
-
- public int size() {
- return this.items.length;
- }
-}
Modified:
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/css/calendar.xcss
===================================================================
---
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/css/calendar.xcss 2007-07-05
22:14:58 UTC (rev 1509)
+++
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/css/calendar.xcss 2007-07-05
23:06:10 UTC (rev 1510)
@@ -3,41 +3,132 @@
xmlns:u='http:/jsf.exadel.com/template/util'
xmlns="http://www.w3.org/1999/xhtml" >
<f:verbatim><![CDATA[
- .Dayclass {
- color: #000000;
- font-weight:bold;
- }
- .Weekendclass {
- color:#800000;
- font-weight:bold;
- }
- .Commondayclass {
- color: #000080;
- }
- .Hovereddayclass {
- color:red;
- }
- .Weekenddayclass {
- color:#800000;
- }
- .Hoveredweekclass {
- color:red;
- }
- .Disableddayclass {
- color:gray;
- }
- .Todayclass {
- background-color: #f0f0f0;
- }
- .Selecteddayclass {
- background-color: #f08080;
- }
- .CalendarSplash {
- background-repeat: no-repeat;
- background-position: center center;
- }
+.calendar_exterior{
+ border : 1px solid #c0c0c0; /*panelBorderColor*/
+}
+.calendar_btn{
+ cursor : pointer;
+}
+.calendar_header{
+ border-bottom : 1px solid #c0c0c0; /*panelBorderColor*/
+ background : #C7D7EC; /*additionalBackgroundColor*/
+ font-size : 11px; /*generalSizeFont*/
+ font-family : verdana; /*generalFamilyFont*/
+ padding : 7px;
+ height : 22px;
+}
+
+.cell_size{
+ width : 25px;
+ height : 22px;
+}
+
+.calendar_cell{
+ background-color : #FFFFFF; /*generalBackgroundColor*/
+ border-bottom : 1px solid #c0c0c0; /*panelBorderColor*/
+ border-right : 1px solid #c0c0c0; /*panelBorderColor*/
+ font-size : 11px; /*generalSizeFont*/
+ font-family : verdana; /*generalFamilyFont*/
+ padding : 0px;
+ vertical-align : middle;
+ text-align : center;
+}
+
+.calendar_tool{
+ border-bottom : 1px solid #c0c0c0; /*panelBorderColor*/
+ background-color : #224986; /*headerBackgroundColor*/
+ font-size : 11px; /*headerSizeFont*/
+ font-family : verdana; /*headerFamilyFont*/
+ color : #ffffff; /*headerTextColor*/
+ font-weight : bold;
+ vertical-align : middle;
+ text-align : center;
+ width : 25px;
+ height : 22px;
+}
+
+.calendar_month{
+ border-bottom : 1px solid #c0c0c0; /*panelBorderColor*/
+ background-color : #224986; /*headerBackgroundColor*/
+ font-size : 11px; /*headerSizeFont*/
+ font-family : verdana; /*headerFamilyFont*/
+ color : #ffffff; /*headerTextColor*/
+ font-weight : bold;
+ vertical-align : middle;
+ text-align : center;
+}
+
+.calendar_days{
+ border-bottom : 1px solid #c0c0c0; /*panelBorderColor*/
+ border-right : 0px solid #c0c0c0; /*panelBorderColor*/
+ background : #C7D7EC; /*additionalBackgroundColor*/
+ font-size : 11px; /*generalSizeFont*/
+ font-family : verdana; /*generalFamilyFont*/
+ vertical-align : middle;
+ text-align : center;
+ height : 22px;
+}
+
+.calendar_week{
+ border-bottom : 1px solid #c0c0c0; /*panelBorderColor*/
+ border-right : 1px solid #c0c0c0; /*panelBorderColor*/
+ font-size : 11px; /*generalSizeFont*/
+ font-family : verdana; /*generalFamilyFont*/
+ background-color : #f5f5f5; /*unknown parameter - need to add*/
+ vertical-align : middle;
+ text-align : center;
+ width : 25px;
+}
+
+.calendar_holly{
+ background-color : #ffebda; /*unknown parameter - need to add*/
+ color : #ff7800; /*unknown parameter - need to add*/
+}
+
+.calendar_current{
+ background-color : #ff7800; /*unknown parameter - need to add*/
+ color : #ffebda; /*unknown parameter - need to add*/
+ font-weight : bold;
+}
+
+.calendar_spec{
+ background-color : #e4f5e2; /*unknown parameter - need to add*/
+}
+
+.calendar_select{
+ background-color : #224986; /*headerBackgroundColor*/
+ color : #FFFFFF; /*headerTextColor*/
+ font-weight : bold;
+}
+
+
+.right_cell{
+ border-right : 0px;
+}
+
+.bottom_cell{
+ border-bottom : 0px;
+}
+
+.calendar_toolfooter{
+ border-top : 1px solid #c0c0c0; /*panelBorderColor*/
+ border-right : 0px solid #c0c0c0; /*panelBorderColor*/
+ background : #C7D7EC; /*additionalBackgroundColor*/
+ font-size : 11px; /*generalSizeFont*/
+ font-family : verdana; /*generalFamilyFont*/
+ padding : 0px 7px 0px 7px;
+ height : 22px;
+}
+
+.calendar_footer{
+ border-top : 1px solid #c0c0c0; /*panelBorderColor*/
+ border-right : 0px solid #c0c0c0; /*panelBorderColor*/
+ background : #C7D7EC; /*additionalBackgroundColor*/
+ font-size : 11px; /*generalSizeFont*/
+ font-family : verdana; /*generalFamilyFont*/
+ padding : 7px;
+ height : 22px;
+}
]]>
</f:verbatim>
- <u:selector name=".CalendarSplash">
- </u:selector>
</f:template>
\ No newline at end of file
Modified:
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/scripts/calendar.js
===================================================================
---
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/scripts/calendar.js 2007-07-05
22:14:58 UTC (rev 1509)
+++
branches/3.0.2/sandbox/calendar/src/main/resources/org/richfaces/renderkit/html/scripts/calendar.js 2007-07-05
23:06:10 UTC (rev 1510)
@@ -111,7 +111,7 @@
this.daysData = {startDate:null, days:[]};
this.days = [];
- var htmlTextHeader = '<div
id="div_'+this.params.dayListTableId+'"></div><table
cellspacing="0" border="1" frame="void"
rules="all" id="'+this.params.dayListTableId+'">\n';
+ var htmlTextHeader = '<div
id="div_'+this.params.dayListTableId+'"></div><table
border="0" cellpadding="0" cellspacing="0"
class="calendar_exterior"
id="'+this.params.dayListTableId+'">\n';
var controlsHeaderContext = {
nextYearControl: Calendar.nextYearControl,
previousYearControl: Calendar.previousYearControl,
@@ -158,6 +158,8 @@
obj.innerHTML =
htmlTextHeader+htmlControlsHeader+htmlTextWeekDayBar+htmlTextWeek+htmlTextFooter;
this.prepareEvents();
+
+ this.submitFunction = this.params.submitFunction.bind(this);
},
isWeekend: function(weekday) {
@@ -382,12 +384,14 @@
nextMonth: function() {
this.currentDate = new Date(this.currentDate.getFullYear(),
this.currentDate.getMonth()+1,1);
- this.update();
+ this.submitFunction(this.currentDate.getMonth() + '/' +
this.currentDate.getFullYear());
+ //this.update();
},
prevMonth: function() {
this.currentDate = new Date(this.currentDate.getFullYear(),
this.currentDate.getMonth()-1,1);
- this.update();
+ this.submitFunction(this.currentDate.getMonth() + '/' +
this.currentDate.getFullYear());
+ //this.update();
},
nextYear: function() {
@@ -460,29 +464,29 @@
};
Calendar.header =
- new E('table',{'cellspacing':'0',
'cellpadding':'0', 'border':'0'},
+ new E('table',{'border': '0', 'cellpadding':
'0', 'cellspacing': '0', 'width': '100%'},
[
new E('tbody',{},
[
new E('tr',{},
[
- new E('td',{'align':'left', 'width':'1%'},
+ new E('td',{'class': 'calendar_tool calendar_btn'},
[
new ET(function (context) { return Richfaces.eval("previousYearControl",
context)})
]),
- new E('td',{'align':'left', 'width':'1%'},
+ new E('td',{'class': 'calendar_tool calendar_btn'},
[
new ET(function (context) { return Richfaces.eval("previousMonthControl",
context)})
]),
- new E('td',{'align':'center',
'width':'96%'},
+ new E('td',{'class': 'calendar_month calendar_btn'},
[
new ET(function (context) { return Richfaces.eval("dateControl",
context)})
]),
- new E('td',{'align':'right', 'width':'1%'},
+ new E('td',{'class': 'calendar_tool calendar_btn'},
[
new ET(function (context) { return Richfaces.eval("nextMonthControl",
context)})
]),
- new E('td',{'align':'right', 'width':'1%'},
+ new E('td',{'class': 'calendar_tool calendar_btn'},
[
new ET(function (context) { return Richfaces.eval("nextYearControl",
context)})
])
Modified:
branches/3.0.2/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx
===================================================================
---
branches/3.0.2/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx 2007-07-05
22:14:58 UTC (rev 1509)
+++
branches/3.0.2/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx 2007-07-05
23:06:10 UTC (rev 1510)
@@ -20,6 +20,10 @@
<script type="text/javascript">
new Calendar('#{clientId}', {
+ submitFunction:
+ <jsp:scriptlet>/*<![CDATA[*/
+ writeSubmitFunction(context, component);
+ /*]]>*/</jsp:scriptlet>,
dayListTableId: '#{clientId}Day',
weekNumberBarId: '#{clientId}WeekNum',
weekDayBarId: '#{clientId}WeekDay',
Modified: branches/3.0.2/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp
===================================================================
---
branches/3.0.2/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp 2007-07-05
22:14:58 UTC (rev 1509)
+++
branches/3.0.2/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp 2007-07-05
23:06:10 UTC (rev 1510)
@@ -7,6 +7,7 @@
</head>
<body>
<f:view>
+ <h:form>
<calendar:calendar id="calendar" data="#{calendarDataModel}"
locale="#{calendarBean.locale}">
<f:facet name="header">
@@ -31,6 +32,7 @@
<calendar:calendar id="calendar1" />
+ </h:form>
</f:view>
</body>
</html>