1
2
3
4
5
6 from pyjamas.ui.SimplePanel import SimplePanel
7 from pyjamas import Factory
8 from pyjamas.ui.VerticalPanel import VerticalPanel
9 from pyjamas.ui.HorizontalPanel import HorizontalPanel
10 from pyjamas.ui.PopupPanel import PopupPanel
11 from pyjamas.ui.Grid import Grid
12 from pyjamas.ui.Composite import Composite
13 from pyjamas.ui.Label import Label
14 from pyjamas.ui.Hyperlink import Hyperlink
15 from pyjamas.ui.HyperlinkImage import HyperlinkImage
16 from pyjamas.ui.HTML import HTML
17 from pyjamas.ui.FocusPanel import FocusPanel
18 from pyjamas.ui.TextBox import TextBox
19 from pyjamas.ui.Image import Image
20 from pyjamas.ui import HasAlignment
21 from pyjamas import DOM
22
23 import pygwt
24
25 import time
26 from datetime import datetime, date
27
29 monthsOfYear = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
30 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
31 daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S']
32 today = 'Today'
33 tomorrow = 'Tomorrow'
34 yesterday = 'Yesterday'
35 cancel = 'Cancel'
36
38 FocusPanel.__init__(self, **kwargs)
39 yr, mth, day = time.strftime("%Y-%m-%d").split("-")
40 self.todayYear = int(yr)
41 self.todayMonth = int(mth)
42 self.todayDay = int(day)
43
44 self.currentMonth = self.todayMonth
45 self.currentYear = self.todayYear
46 self.currentDay = self.todayDay
47
48 self.selectedDateListeners = []
49
50 self.defaultGrid = None
51
52 return
53
54
56 """ _date - object of datetime.date class """
57 self.currentMonth = _date.month
58 self.currentYear = _date.year
59 self.currentDay = _date.day
60
63
66
68 self.selectedDateListeners.append(listener)
69
71 self.selectedDateListeners.remove(listener)
72
74 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
75 return True
76 else:
77 return False
78
80 days = 0
81 if mth in [1, 3, 5, 7, 8, 10, 12]:
82 days=31
83 elif mth in [4, 6, 9, 11]:
84 days = 30
85 elif (mth==2 and self.isLeapYear(year)):
86 days = 29
87 else:
88 days = 28
89 return days
90
95
96 - def show(self, left, top):
104
106 yr, mth, day = self.currentYear, self.currentMonth, self.currentDay
107 self.draw(int(mth), int(yr))
108
109
110 - def draw(self, month , year):
111 tod = time.localtime()
112 mm = tod.tm_mon
113 yy = tod.tm_year
114
115
116
117 hasChangeMonth = False
118 if yy <> self.todayYear or mm <> self.todayMonth:
119 hasChangeMonth = True
120 self.todayYear = yy
121 self.todayMonth = mm
122
123
124 if self.defaultGrid is None:
125 self.drawFull(month, year)
126 else:
127
128
129 if not hasChangeMonth and month == self.todayMonth and \
130 year == self.todayYear:
131 self.middlePanel.setWidget(self.defaultGrid)
132 self.currentMonth = self.todayMonth
133 self.currentYear = self.todayYear
134 else:
135
136 g = self.drawGrid(month, year)
137
138 if hasChangeMonth:
139
140 self.defaultGrid = grid
141
142
143
144
145 txt = "<b>"
146 txt += self.getMonthsOfYear()[month-1] + " " + str(year)
147 txt += "</b>"
148 self.titlePanel.setWidget(HTML(txt))
149 self.setVisible(True)
150
151 return
152
213
232
241
243
244
245 daysInMonth = self.getDaysInMonth(month, year)
246
247 secs = time.mktime((year, month, 1, 0, 0, 0, 0, 0, -1))
248 struct = time.localtime(secs)
249
250 startPos = (struct.tm_wday + 1) % 7
251 slots = startPos + daysInMonth - 1
252 rows = int(slots/7) + 1
253 grid = Grid(rows+1, 7)
254 grid.setWidth("100%")
255 grid.addTableListener(self)
256 self.middlePanel.setWidget(grid)
257
258
259
260 for i in range(7):
261 grid.setText(0, i, self.getDaysOfWeek()[i])
262 grid.cellFormatter.addStyleName(0, i, "calendar-header")
263
264
265
266 day =0
267 pos = 0
268 while pos < startPos:
269 grid.setText(1, pos , " ")
270 grid.cellFormatter.setStyleAttr(1, pos, "background", "#f3f3f3")
271 grid.cellFormatter.addStyleName(1, pos, "calendar-blank-cell")
272 pos += 1
273
274 row = 1
275 day = 1
276 col = startPos
277 while day <= daysInMonth:
278 if pos % 7 == 0 and day <> 1:
279 row += 1
280 col = pos % 7
281 grid.setText(row, col, str(day))
282 if self.currentYear == self.todayYear and \
283 self.currentMonth == self.todayMonth and day == self.todayDay:
284 grid.cellFormatter.addStyleName(row, col, "calendar-cell-today")
285 else:
286 grid.cellFormatter.addStyleName(row, col, "calendar-day-cell")
287 day += 1
288 pos += 1
289
290
291
292 col += 1
293 while col < 7:
294 grid.setText(row, col, " ")
295 grid.cellFormatter.setStyleAttr(row, col, "background", "#f3f3f3")
296 grid.cellFormatter.addStyleName(row, col, "calendar-blank-cell")
297 col += 1
298
299 return grid
300
302 if row == 0:
303 return
304 text = grid.getText(row, col).strip()
305 if text == "":
306 return
307 try:
308 selectedDay = int(text)
309 except ValueError, e:
310 return
311
312 for listener in self.selectedDateListeners:
313 if hasattr(listener, "onDateSelected"):
314 listener.onDateSelected(self.currentYear, self.currentMonth,
315 selectedDay)
316 else:
317 listener(self.currentYear, self.currentMonth, selectedDay)
318 self.setVisible(False)
319
320
323
326
329
332
333 - def onDate(self, event, yy, mm, dd):
334 for listener in self.selectedDateListeners:
335 if hasattr(listener, "onDateSelected"):
336 listener.onDateSelected(yy, mm, dd)
337 else:
338 listener(yy, mm, dd)
339 self.setVisible(False)
340
347
349 tod = time.localtime()
350 mm = tod.tm_mon
351 dd = tod.tm_mday
352 yy = tod.tm_year
353 self.onDate(event, yy, mm, dd)
354
356 tom = time.localtime(time.time() + 3600 * 24)
357 mm = tom.tm_mon
358 dd = tom.tm_mday
359 yy = tom.tm_year
360 self.onDate(event, yy, mm, dd)
361
364
366 self.currentMonth = month
367 self.currentYear = year
368 self.draw(self.currentMonth, self.currentYear)
369
371 if int(self.currentMonth) == 1:
372 self.currentMonth = 12
373 self.currentYear = int(self.currentYear) - 1
374 else:
375 self.currentMonth = int(self.currentMonth) - 1
376 self.draw(self.currentMonth, self.currentYear)
377
379 if int(self.currentMonth) == 12:
380 self.currentMonth = 1
381 self.currentYear = int(self.currentYear) + 1
382 else:
383 self.currentMonth = int(self.currentMonth) + 1
384 self.draw(self.currentMonth, self.currentYear)
385
387 self.currentYear = int(self.currentYear) - 1
388 self.draw(self.currentMonth, self.currentYear)
389
391 self.currentYear = int(self.currentYear) + 1
392 self.draw(self.currentMonth, self.currentYear)
393
394 Factory.registerClass('pyjamas.ui.Calendar', 'Calendar', Calendar)
395
396
398
399 img_base = None
400 icon_img = None
401
402 icon_style = "calendar-img"
403 today_text = "Today"
404 today_style = "calendar-today-link"
405
451
452 - def getTextBox(self):
454
457
459 """ returns datetime.date object or None if empty/unparsable by current format"""
460 _sdate = self.tbox.getText()
461 try:
462 return datetime.strptime(_sdate, self.format).date()
463 except ValueError:
464 return None
465
468
470 secs = time.mktime((int(yyyy), int(mm), int(dd), 0, 0, 0, 0, 0, -1))
471 d = time.strftime(self.format, time.localtime(secs))
472 self.tbox.setText(d)
473
475
476 text = self.tbox.getText().strip()
477
478 if text and len(text) == 8:
479
480 txt = text[0:2] + self.sep + text[2:4] + self.sep + text[4:8]
481 self.tbox.setText(txt)
482
485
489
503
504 Factory.registerClass('pyjamas.ui.Calendar', 'DateField', DateField)
505
506
515
516 Factory.registerClass('pyjamas.ui.Calendar', 'CalendarPopup', CalendarPopup)
517