01: package org.makumba.forms.html;
02:
03: /**
04: * Implements a calendar editor via a chose pop-up using the library of Matt Kurse - <a
05: * href="http://www.mattkruse.com/javascript/calendarpopup/">http://www.mattkruse.com/javascript/calendarpopup/</a>
06: */
07: public class KruseCalendarEditor implements CalendarEditorProvider {
08:
09: public static final String calendarEditorClass = "calendarEditor";
10:
11: private static CalendarEditorProvider singleton;
12:
13: public StringBuffer formatEditorCode(String inputName,
14: String calendarLinkFormatting) {
15: StringBuffer sb = new StringBuffer();
16: String calendarName = inputName + "Calendar";
17: String anchorname = inputName + "Anchor";
18: String divname = "makCalendarDiv";
19:
20: // TODO: elementById is not necessarily unique, if you have the same field in different forms ?
21: // (not sure, maybe it is unique?)
22: // therefore, maybe better would be to know the form index, and then use forms[index].name ..
23: String dayInput = "document.getElementById('" + inputName
24: + "_0')";
25: String monthInput = "document.getElementById('" + inputName
26: + "_1')";
27: String yearInput = "document.getElementById('" + inputName
28: + "_2')";
29:
30: String dateString = "getDateString(" + yearInput + ", "
31: + monthInput + ", " + dayInput + ")";
32: String linkText = calendarLinkFormatting != null ? calendarLinkFormatting
33: : " ";
34:
35: // append the link
36: // TODO: this will default to 1/1/1900, maybe we should default to today?
37: sb
38: .append("<a class=\""
39: + calendarEditorClass
40: + "\" href=\"#\" onClick=\""
41: + calendarName
42: + ".showCalendar('"
43: + anchorname
44: + "', "
45: + dateString
46: + "); return false;\" title=\"Pick date in calendar\" name=\""
47: + anchorname + "\" ID=\"" + anchorname + "\">"
48: + linkText + "</a>\n");
49:
50: // make the div
51: sb
52: .append("<div id=\""
53: + divname
54: + "\" style=\"position:absolute; visibility:hidden; background-color:white; layer-background-color:white;\"></div>\n");
55:
56: // write the script
57: sb
58: .append("\n<script language=\"javascript\" type=\"text/javascript\">\n");
59: sb.append("var " + calendarName + " = new CalendarPopup(\""
60: + divname + "\");\n");
61: sb.append(calendarName + ".showYearNavigation();\n");
62: sb.append(calendarName
63: + ".setReturnFunction(\"setMultipleValues" + inputName
64: + "\");\n");
65: sb.append("function setMultipleValues" + inputName
66: + "(y,m,d) {\n");
67: sb.append(" " + dayInput + ".selectedIndex=LZ(d)-1;\n");
68: sb.append(" " + monthInput + ".selectedIndex=LZ(m)-1;\n");
69: sb.append(" " + yearInput + ".value=y;\n");
70: sb.append("};\n");
71: sb.append("</script>\n");
72:
73: return sb;
74: }
75:
76: public static CalendarEditorProvider getInstance() {
77: if (singleton == null) {
78: singleton = new KruseCalendarEditor();
79: }
80: return singleton;
81: }
82:
83: public String[] getNeededJavaScriptFileNames() {
84: return new String[] { "kruseAnchorPosition.js",
85: "kruseCalendarPopup.js", "kruseDateFunctions.js",
86: "krusePopupWindow.js", "makumbaDateFunctions.js" };
87: }
88:
89: }
|