Mercurial Hosting > nabble
comparison src/nabble/view/web/template/CalendarWidget.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 | |
2 package nabble.view.web.template; | |
3 | |
4 import nabble.naml.compiler.IPrintWriter; | |
5 import nabble.naml.compiler.Interpreter; | |
6 import nabble.naml.compiler.CommandSpec; | |
7 | |
8 import javax.servlet.ServletException; | |
9 import javax.servlet.http.HttpServlet; | |
10 import javax.servlet.http.HttpServletRequest; | |
11 import javax.servlet.http.HttpServletResponse; | |
12 import java.io.IOException; | |
13 import nabble.naml.compiler.PrintWriter; | |
14 import java.util.Calendar; | |
15 import java.util.GregorianCalendar; | |
16 | |
17 | |
18 final class CalendarWidget { | |
19 | |
20 public static final CommandSpec _calendar = new CommandSpec.Builder() | |
21 .parameters("date_time","months","week_days","min_year") | |
22 .build() | |
23 ; | |
24 | |
25 public static void _calendar(IPrintWriter out,Interpreter interp) { | |
26 String dateTime = interp.getArgString("date_time"); | |
27 String months = interp.getArgString("months"); | |
28 String weekDays = interp.getArgString("week_days"); | |
29 String minYear = interp.getArgString("min_year"); | |
30 Calendar cal = Calendar.getInstance(); | |
31 cal.setLenient(false); | |
32 cal.setTimeInMillis(Long.valueOf(dateTime)); | |
33 printCalendar(cal, Integer.valueOf(minYear), months, weekDays, out.getPrintWriter()); | |
34 } | |
35 | |
36 private static void printCalendar(Calendar cal, int minYear, String months, String weekDays, PrintWriter out) { | |
37 int day = cal.get(Calendar.DAY_OF_MONTH); | |
38 String[] monthsArray = months.split("\\|"); | |
39 int month = cal.get(Calendar.MONTH); | |
40 int year = cal.get(Calendar.YEAR); | |
41 int currentYear = Calendar.getInstance().get(Calendar.YEAR); | |
42 | |
43 out.print( "\n<script type=\"text/javascript\">\n function NabbleCalendar(d,m,y) {\n this.day = d;\n this.month = m;\n this.year = y;\n };\n NabbleCalendar.prototype.setDay = function(d) {\n this.day = parseInt(d);\n };\n NabbleCalendar.prototype.getDay = function() {\n return this.day;\n };\n NabbleCalendar.prototype.getMonth = function() {\n return this.month;\n };\n NabbleCalendar.prototype.getYear = function() {\n return this.year;\n };\n NabbleCalendar.prototype.updateCalendar = function() {\n var $m = $('#month');\n var $y = $('#year');\n this.month = parseInt($m.val());\n this.year = parseInt($y.val());\n $m.attr('disabled','y');\n $y.attr('disabled','y');\n $.get('/template/CalendarWidget$Update.jtp?day='+this.day+'&month='+this.month+'&year='+this.year+'&weekdays=" ); | |
44 out.print( (weekDays) ); | |
45 out.print( "', function(data) {\n $('#calendar-wrapper').html(data);\n $m.removeAttr('disabled');\n $y.removeAttr('disabled');\n Nabble.addEventHandlers();\n });\n };\n var nabbleCalendar = new NabbleCalendar(" ); | |
46 out.print( (day) ); | |
47 out.print( ", " ); | |
48 out.print( (month) ); | |
49 out.print( ", " ); | |
50 out.print( (year) ); | |
51 out.print( ");\n Nabble.addEventHandlers = function() {\n $('#calendar-wrapper td.day').css('cursor','pointer').click(function(){\n $('#day'+nabbleCalendar.day).removeClass('current');\n var day = $(this).html();\n nabbleCalendar.setDay(day);\n $(this).addClass('current');\n });\n };\n $(document).ready(function() {\n Nabble.addEventHandlers();\n });\n</script>\n<select id=\"month\" onchange=\"nabbleCalendar.updateCalendar()\">\n " ); | |
52 for (int m = 0;m < 12; m++) { | |
53 out.print( "\n <option value=\"" ); | |
54 out.print( (m) ); | |
55 out.print( "\" " ); | |
56 out.print( (month == m? "selected" : "") ); | |
57 out.print( ">" ); | |
58 out.print( (monthsArray[m]) ); | |
59 out.print( "</option>\n " ); | |
60 } | |
61 out.print( "\n</select>\n<select id=\"year\" onchange=\"nabbleCalendar.updateCalendar()\">\n " ); | |
62 for (int y = minYear;y <= currentYear; y++) { | |
63 out.print( "\n <option value=\"" ); | |
64 out.print( (y) ); | |
65 out.print( "\" " ); | |
66 out.print( (year == y? "selected" : "") ); | |
67 out.print( ">" ); | |
68 out.print( (y) ); | |
69 out.print( "</option>\n " ); | |
70 } | |
71 out.print( "\n</select>\n<div id=\"calendar-wrapper\">\n " ); | |
72 printCalendarTable(cal, weekDays, out); | |
73 out.print( "\n</div>\n" ); | |
74 | |
75 } | |
76 | |
77 private static void printCalendarTable(Calendar cal, String weekDays, PrintWriter out) { | |
78 String[] weekDaysArray = weekDays.split("\\|"); | |
79 int day = cal.get(Calendar.DAY_OF_MONTH); | |
80 int nDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); | |
81 cal.set(Calendar.DAY_OF_MONTH, 1); | |
82 int firstDay = cal.get(Calendar.DAY_OF_WEEK); | |
83 int i; | |
84 | |
85 out.print( "\n<table class=\"calendar\">\n <tr>\n <td class=\"week-header\">" ); | |
86 out.print( (weekDaysArray[0]) ); | |
87 out.print( "</td>\n <td class=\"week-header\">" ); | |
88 out.print( (weekDaysArray[1]) ); | |
89 out.print( "</td>\n <td class=\"week-header\">" ); | |
90 out.print( (weekDaysArray[2]) ); | |
91 out.print( "</td>\n <td class=\"week-header\">" ); | |
92 out.print( (weekDaysArray[3]) ); | |
93 out.print( "</td>\n <td class=\"week-header\">" ); | |
94 out.print( (weekDaysArray[4]) ); | |
95 out.print( "</td>\n <td class=\"week-header\">" ); | |
96 out.print( (weekDaysArray[5]) ); | |
97 out.print( "</td>\n <td class=\"week-header\">" ); | |
98 out.print( (weekDaysArray[6]) ); | |
99 out.print( "</td>\n </tr>\n <tr>\n " ); | |
100 for (i = 1; i < firstDay; i++) { | |
101 out.print( "\n <td class=\"empty\"></td>\n " ); | |
102 } | |
103 out.print( "\n " ); | |
104 int d = 1; | |
105 out.print( "\n " ); | |
106 for (i = firstDay; i <= 7; i++) printDay(d++, day, out); | |
107 out.print( "\n </tr>\n " ); | |
108 while (d <= nDays) { | |
109 out.print( "\n " ); | |
110 int nextStop = d + 7; | |
111 out.print( "\n <tr>\n " ); | |
112 while (d < nextStop && d <= nDays) printDay(d++, day, out); | |
113 out.print( "\n </tr>\n " ); | |
114 } | |
115 out.print( "\n</table>\n" ); | |
116 | |
117 } | |
118 | |
119 private static void printDay(int day, int currentDay, PrintWriter out) { | |
120 | |
121 out.print( "<td id=\"day" ); | |
122 out.print( (day) ); | |
123 out.print( "\" class=\"day" ); | |
124 out.print( (day == currentDay?" current":"") ); | |
125 out.print( "\">" ); | |
126 out.print( (day) ); | |
127 out.print( "</td>" ); | |
128 | |
129 } | |
130 | |
131 public static class Update extends HttpServlet { | |
132 | |
133 protected void service(HttpServletRequest request, HttpServletResponse response) | |
134 throws ServletException, IOException | |
135 { | |
136 PrintWriter out = new PrintWriter(response.getWriter()); | |
137 String weekDays = request.getParameter("weekdays"); | |
138 String day = request.getParameter("day"); | |
139 String month = request.getParameter("month"); | |
140 String year = request.getParameter("year"); | |
141 Calendar cal = new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day)); | |
142 printCalendarTable(cal, weekDays, out); | |
143 } | |
144 | |
145 } | |
146 } | |
147 |