Fix highlighted days on calendar widget

* CalendarHighlightedDays macro component is removed
* Solution is based on discussion at ZK forum:
  http://www.zkoss.org/forum/listComment/17068-Highlight-calendar-days-with-jQuery

FEA: ItEr75S08MigrationZK5
This commit is contained in:
Manuel Rego Casasnovas 2011-08-04 09:58:28 +02:00
parent a4b0324139
commit 935344eade
6 changed files with 34 additions and 233 deletions

View file

@ -37,7 +37,6 @@ import org.navalplanner.web.common.Level;
import org.navalplanner.web.common.MessagesForUser;
import org.navalplanner.web.common.OnlyOneVisible;
import org.navalplanner.web.common.Util;
import org.navalplanner.web.common.components.CalendarHighlightedDays;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
@ -120,8 +119,7 @@ public class BaseCalendarCRUDController extends GenericForwardComposer {
}
private void highlightDaysOnCalendar() {
((CalendarHighlightedDays) editWindow.getFellow("calendarWidget"))
.highlightDays();
editionController.highlightDaysOnCalendar();
}
public void save() {

View file

@ -56,9 +56,9 @@ import org.navalplanner.web.common.Util;
import org.navalplanner.web.common.Util.Getter;
import org.navalplanner.web.common.Util.ICreation;
import org.navalplanner.web.common.Util.Setter;
import org.navalplanner.web.common.components.CalendarHighlightedDays;
import org.navalplanner.web.common.components.CapacityPicker;
import org.navalplanner.web.common.components.EffortDurationPicker;
import org.zkoss.zk.au.out.AuInvoke;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.CheckEvent;
@ -82,6 +82,7 @@ import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.api.Calendar;
import org.zkoss.zul.api.Window;
/**
@ -484,9 +485,16 @@ public abstract class BaseCalendarEditionController extends
capacityPicker.setValue(baseCalendarModel.getWorkableCapacity());
}
private void highlightDaysOnCalendar() {
((CalendarHighlightedDays) window.getFellow("calendarWidget"))
.highlightDays();
public void highlightDaysOnCalendar() {
Calendar calendar = (Calendar) window.getFellow("calendarWidget");
Map<DayType, List<Integer>> daysByType = getDaysCurrentMonthByType();
Clients.response(new AuInvoke(calendar, "highlightDates", daysByType
.get(DayType.ANCESTOR_EXCEPTION).toArray(), "white", "orange"));
Clients.response(new AuInvoke(calendar, "highlightDates", daysByType
.get(DayType.OWN_EXCEPTION).toArray(), "white", "red"));
Clients.response(new AuInvoke(calendar, "highlightDates", daysByType
.get(DayType.ZERO_HOURS).toArray(), "red", "white"));
}
public Date getSelectedDay() {
@ -516,7 +524,7 @@ public abstract class BaseCalendarEditionController extends
reloadSelectDayInformation();
}
private Map<DayType, String> getDaysCurrentMonthByType() {
private Map<DayType, List<Integer>> getDaysCurrentMonthByType() {
LocalDate currentDate = new LocalDate(baseCalendarModel
.getSelectedDay());
@ -550,30 +558,16 @@ public abstract class BaseCalendarEditionController extends
}
}
Map<DayType, String> result = new HashMap<DayType, String>();
Map<DayType, List<Integer>> result = new HashMap<DayType, List<Integer>>();
result.put(DayType.ANCESTOR_EXCEPTION, StringUtils.join(
ancestorExceptionsDays, ","));
result.put(DayType.OWN_EXCEPTION, StringUtils.join(ownExceptionDays,
","));
result.put(DayType.ZERO_HOURS, StringUtils.join(zeroHoursDays, ","));
result.put(DayType.NORMAL, StringUtils.join(normalDays, ","));
result.put(DayType.ANCESTOR_EXCEPTION, ancestorExceptionsDays);
result.put(DayType.OWN_EXCEPTION, ownExceptionDays);
result.put(DayType.ZERO_HOURS, zeroHoursDays);
result.put(DayType.NORMAL, normalDays);
return result;
}
public String getAncestorExceptionDays() {
return getDaysCurrentMonthByType().get(DayType.ANCESTOR_EXCEPTION);
}
public String getOwnExceptionDays() {
return getDaysCurrentMonthByType().get(DayType.OWN_EXCEPTION);
}
public String getZeroHoursDays() {
return getDaysCurrentMonthByType().get(DayType.ZERO_HOURS);
}
public String getTypeOfDay() {
DayType typeOfDay = baseCalendarModel.getTypeOfDay();
if (typeOfDay == null) {

View file

@ -1,108 +0,0 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
* Copyright (C) 2010-2011 Igalia, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.common.components;
import java.util.Date;
import org.navalplanner.web.common.Util;
import org.zkoss.zk.ui.HtmlMacroComponent;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Calendar;
/**
* ZK macro component that uses the {@link Calendar} component adding the
* possibility to highlight some days in the calendar.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
public class CalendarHighlightedDays extends HtmlMacroComponent {
private Date value;
private String ancestorExceptionDays;
private String ownExceptionDays;
private String zeroHoursDays;
private String calendarUuid;
public void setInternalValue(Date value) {
this.value = value;
Util.saveBindings(this);
highlightDays();
}
public Date getInternalValue() {
return value != null ? new Date(value.getTime()) : null;
}
public void setValue(Date value) {
this.value = value != null ? new Date(value.getTime()) : null;
}
public Date getValue() {
return value != null ? new Date(value.getTime()) : null;
}
public void setAncestorExceptionDays(String ancestorExceptionDays) {
this.ancestorExceptionDays = ancestorExceptionDays;
}
public String getAncestorExceptionDays() {
return ancestorExceptionDays;
}
public void setOwnExceptionDays(String ownExceptionDays) {
this.ownExceptionDays = ownExceptionDays;
}
public String getOwnExceptionDays() {
return ownExceptionDays;
}
public void setZeroHoursDays(String zeroHoursDays) {
this.zeroHoursDays = zeroHoursDays;
}
public String getZeroHoursDays() {
return zeroHoursDays;
}
public void highlightDays() {
String javascript = "highlightDays('" + ancestorExceptionDays
+ "', 'white', 'orange', '" + ownExceptionDays
+ "', 'white', 'red', '" + zeroHoursDays
+ "', 'red', 'white', 'lightgrey', 'white', '"
+ getCalendarUuid() + "');";
Clients.evalJavaScript(javascript);
}
public String getCalendarUuid() {
if (calendarUuid == null) {
Calendar calendar = (Calendar) getLastChild();
calendarUuid = calendar.getUuid();
}
return calendarUuid;
}
}

View file

@ -29,12 +29,6 @@
<extends>hbox</extends>
</component>
<component>
<component-name>calendarhighlighteddays</component-name>
<component-class>org.navalplanner.web.common.components.CalendarHighlightedDays</component-class>
<macro-uri>/common/components/calendarhighlighteddays.zul</macro-uri>
</component>
<component>
<component-name>autocomplete</component-name>
<component-class>org.navalplanner.web.common.components.Autocomplete</component-class>

View file

@ -19,7 +19,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<window id="${arg.top_id}">
<window id="${arg.top_id}" xmlns:w="client">
<tabbox sclass="calendar-tabbox">
<tabs>
@ -68,11 +68,20 @@
<hbox width="100%" pack="stretch">
<vbox id="dayInformation" width="100%">
<calendarhighlighteddays id="calendarWidget"
value="@{calendarController.editionController.selectedDay,access='both'}"
ancestorExceptionDays="@{calendarController.editionController.ancestorExceptionDays,access='both'}"
ownExceptionDays="@{calendarController.editionController.ownExceptionDays,access='both'}"
zeroHoursDays="@{calendarController.editionController.zeroHoursDays,access='both'}" />
<calendar id="calendarWidget"
value="@{calendarController.editionController.selectedDay}">
<attribute w:name="highlightDates">
function (days, textColor, bgColor) {
var nodes = $('td', this).not('.z-outside');
nodes.each(function () {
var day = parseInt($(this).attr('_dt'));
if (jQuery.inArray(day, days) > -1) {
$(this).css({color: textColor, backgroundColor: bgColor});
}
});
}
</attribute>
</calendar>
<grid width="160px" sclass="day-details">
<auxhead>

View file

@ -1,86 +0,0 @@
<!--
This file is part of NavalPlan
Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
Desenvolvemento Tecnolóxico de Galicia
Copyright (C) 2010-2011 Igalia, S.L.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<zk xmlns:n="http://www.zkoss.org/2005/zk/native">
<n:script language="javascript">
<![CDATA[
Array.prototype.in_array = function(p_val) {
for(var i = 0, l = this.length; i < l; i++) {
if(this[i] == p_val) {
return true;
}
}
return false;
}
function highlightDays(ancestorExceptionDays, ancestorExceptionColor, ancestorExceptionBgColor,
ownExceptionDays, ownExceptionColor, ownExceptionBgColor,
zeroHoursDays, zeroHoursColor, zeroHoursBgColor,
outOfMonthColor, outOfMonghBgColor,
calendarUuid) {
var calendar = document.getElementById(calendarUuid);
var nodes = calendar.getElementsByTagName("td");
var ancestorExceptions = ancestorExceptionDays.split(",");
var ownExceptions = ownExceptionDays.split(",");
var zeroHours = zeroHoursDays.split(",");
for (var i = 0; i < nodes.length; i++) {
var month = nodes[i].getAttribute("zk_monofs");
if (month == null) {
continue;
}
if (month == 0) {
var day = nodes[i].getAttribute("zk_day");
if (zeroHours.in_array(day)) {
nodes[i].setAttribute("style",
"color:" + zeroHoursColor + ";background-color:" + zeroHoursBgColor);
} else if (ownExceptions.in_array(day)) {
nodes[i].setAttribute("style",
"color:" + ownExceptionColor + ";background-color:" + ownExceptionBgColor + ";font-weight: bold");
} else if (ancestorExceptions.in_array(day)) {
nodes[i].setAttribute("style",
"color:" + ancestorExceptionColor + ";background-color:" + ancestorExceptionBgColor + ";font-weight: bold");
} else {
nodes[i].removeAttribute("style");
}
} else {
nodes[i].setAttribute("style",
"color:" + outOfMonthColor + ";background-color:" + outOfMonghBgColor);
}
}
}
]]>
</n:script>
<calendar value="@{self.parent.internalValue}" />
</zk>