Mercurial Hosting > nabble
comparison src/nabble/view/web/template/CalendarWidget.jtp @ 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 <script type="text/javascript"> | |
44 function NabbleCalendar(d,m,y) { | |
45 this.day = d; | |
46 this.month = m; | |
47 this.year = y; | |
48 }; | |
49 NabbleCalendar.prototype.setDay = function(d) { | |
50 this.day = parseInt(d); | |
51 }; | |
52 NabbleCalendar.prototype.getDay = function() { | |
53 return this.day; | |
54 }; | |
55 NabbleCalendar.prototype.getMonth = function() { | |
56 return this.month; | |
57 }; | |
58 NabbleCalendar.prototype.getYear = function() { | |
59 return this.year; | |
60 }; | |
61 NabbleCalendar.prototype.updateCalendar = function() { | |
62 var $m = $('#month'); | |
63 var $y = $('#year'); | |
64 this.month = parseInt($m.val()); | |
65 this.year = parseInt($y.val()); | |
66 $m.attr('disabled','y'); | |
67 $y.attr('disabled','y'); | |
68 $.get('/template/CalendarWidget$Update.jtp?day='+this.day+'&month='+this.month+'&year='+this.year+'&weekdays=<%=weekDays%>', function(data) { | |
69 $('#calendar-wrapper').html(data); | |
70 $m.removeAttr('disabled'); | |
71 $y.removeAttr('disabled'); | |
72 Nabble.addEventHandlers(); | |
73 }); | |
74 }; | |
75 var nabbleCalendar = new NabbleCalendar(<%=day%>, <%=month%>, <%=year%>); | |
76 Nabble.addEventHandlers = function() { | |
77 $('#calendar-wrapper td.day').css('cursor','pointer').click(function(){ | |
78 $('#day'+nabbleCalendar.day).removeClass('current'); | |
79 var day = $(this).html(); | |
80 nabbleCalendar.setDay(day); | |
81 $(this).addClass('current'); | |
82 }); | |
83 }; | |
84 $(document).ready(function() { | |
85 Nabble.addEventHandlers(); | |
86 }); | |
87 </script> | |
88 <select id="month" onchange="nabbleCalendar.updateCalendar()"> | |
89 <% for (int m = 0;m < 12; m++) { %> | |
90 <option value="<%=m%>" <%=month == m? "selected" : ""%>><%=monthsArray[m]%></option> | |
91 <% } %> | |
92 </select> | |
93 <select id="year" onchange="nabbleCalendar.updateCalendar()"> | |
94 <% for (int y = minYear;y <= currentYear; y++) { %> | |
95 <option value="<%=y%>" <%=year == y? "selected" : ""%>><%=y%></option> | |
96 <% } %> | |
97 </select> | |
98 <div id="calendar-wrapper"> | |
99 <% printCalendarTable(cal, weekDays, out); %> | |
100 </div> | |
101 <% | |
102 } | |
103 | |
104 private static void printCalendarTable(Calendar cal, String weekDays, PrintWriter out) { | |
105 String[] weekDaysArray = weekDays.split("\\|"); | |
106 int day = cal.get(Calendar.DAY_OF_MONTH); | |
107 int nDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); | |
108 cal.set(Calendar.DAY_OF_MONTH, 1); | |
109 int firstDay = cal.get(Calendar.DAY_OF_WEEK); | |
110 int i; | |
111 %> | |
112 <table class="calendar"> | |
113 <tr> | |
114 <td class="week-header"><%=weekDaysArray[0]%></td> | |
115 <td class="week-header"><%=weekDaysArray[1]%></td> | |
116 <td class="week-header"><%=weekDaysArray[2]%></td> | |
117 <td class="week-header"><%=weekDaysArray[3]%></td> | |
118 <td class="week-header"><%=weekDaysArray[4]%></td> | |
119 <td class="week-header"><%=weekDaysArray[5]%></td> | |
120 <td class="week-header"><%=weekDaysArray[6]%></td> | |
121 </tr> | |
122 <tr> | |
123 <% for (i = 1; i < firstDay; i++) {%> | |
124 <td class="empty"></td> | |
125 <% } %> | |
126 <% int d = 1; %> | |
127 <% for (i = firstDay; i <= 7; i++) printDay(d++, day, out); %> | |
128 </tr> | |
129 <% while (d <= nDays) { %> | |
130 <% int nextStop = d + 7; %> | |
131 <tr> | |
132 <% while (d < nextStop && d <= nDays) printDay(d++, day, out); %> | |
133 </tr> | |
134 <% } %> | |
135 </table> | |
136 <% | |
137 } | |
138 | |
139 private static void printDay(int day, int currentDay, PrintWriter out) { | |
140 %><td id="day<%=day%>" class="day<%=day == currentDay?" current":""%>"><%=day%></td><% | |
141 } | |
142 | |
143 public static class Update extends HttpServlet { | |
144 | |
145 protected void service(HttpServletRequest request, HttpServletResponse response) | |
146 throws ServletException, IOException | |
147 { | |
148 PrintWriter out = new PrintWriter(response.getWriter()); | |
149 String weekDays = request.getParameter("weekdays"); | |
150 String day = request.getParameter("day"); | |
151 String month = request.getParameter("month"); | |
152 String year = request.getParameter("year"); | |
153 Calendar cal = new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day)); | |
154 printCalendarTable(cal, weekDays, out); | |
155 } | |
156 | |
157 } | |
158 } | |
159 %> |