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
30 self.selectedDateListeners = []
31 self.selectedDObjListeners = []
32
34 """ - dobj - listener accept datetime.date object rather than y,m,d triple
35 """
36 if dobj:
37 self.selectedDObjListeners.append(listener)
38 else:
39 self.selectedDateListeners.append(listener)
40
42 try:
43 self.selectedDateListeners.remove(listener)
44 except ValueError:
45 self.selectedDObjListeners.remove(listener)
46
48 """ fire event to listeners with date specified in args. Date can be specified either by separate year,month,day or by datetime.date object
49 """
50 for listener in self.selectedDateListeners:
51 getattr(listener, "onDateSelected", listener)(
52 dateobj.year,
53 dateobj.month,
54 dateobj.day,
55 )
56 for listener in self.selectedDObjListeners:
57 getattr(listener, "onDateSelected", listener)(
58 dateobj,
59 )
60
61
62 -class Calendar(FocusPanel, DateSelectedHandler):
63 monthsOfYear = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
64 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
65 daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S']
66 today = 'Today'
67 tomorrow = 'Tomorrow'
68 yesterday = 'Yesterday'
69 cancel = 'Cancel'
70
72 FocusPanel.__init__(self, **kwargs)
73 DateSelectedHandler.__init__(self)
74 yr, mth, day = time.strftime("%Y-%m-%d").split("-")
75 self.todayYear = int(yr)
76 self.todayMonth = int(mth)
77 self.todayDay = int(day)
78
79 self.currentMonth = self.todayMonth
80 self.currentYear = self.todayYear
81 self.currentDay = self.todayDay
82
83
84 self.defaultGrid = None
85
86 return
87
88
90 """ _date - object of datetime.date class """
91 self.currentMonth = _date.month
92 self.currentYear = _date.year
93 self.currentDay = _date.day
94
97
100
102 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
103 return True
104 else:
105 return False
106
108 days = 0
109 if mth in [1, 3, 5, 7, 8, 10, 12]:
110 days = 31
111 elif mth in [4, 6, 9, 11]:
112 days = 30
113 elif (mth == 2 and self.isLeapYear(year)):
114 days = 29
115 else:
116 days = 28
117 return days
118
123
124 - def show(self, left, top):
132
134 yr, mth, day = self.currentYear, self.currentMonth, self.currentDay
135 self.draw(int(mth), int(yr))
136
137
138 - def draw(self, month , year):
139 tod = time.localtime()
140 mm = tod.tm_mon
141 yy = tod.tm_year
142
143
144
145 hasChangeMonth = False
146 if yy <> self.todayYear or mm <> self.todayMonth:
147 hasChangeMonth = True
148 self.todayYear = yy
149 self.todayMonth = mm
150
151
152 if self.defaultGrid is None:
153 self.drawFull(month, year)
154 else:
155
156
157 if not hasChangeMonth and month == self.todayMonth and \
158 year == self.todayYear:
159 self.middlePanel.setWidget(self.defaultGrid)
160 self.currentMonth = self.todayMonth
161 self.currentYear = self.todayYear
162 else:
163
164 g = self.drawGrid(month, year)
165
166 if hasChangeMonth:
167
168 self.defaultGrid = grid
169
170
171
172
173 txt = "<b>"
174 txt += self.getMonthsOfYear()[month - 1] + " " + str(year)
175 txt += "</b>"
176 self.titlePanel.setWidget(HTML(txt))
177 self.setVisible(True)
178
179 return
180
241
260
269
271
272
273 daysInMonth = self.getDaysInMonth(month, year)
274
275 secs = time.mktime((year, month, 1, 0, 0, 0, 0, 0, -1))
276 struct = time.localtime(secs)
277
278 startPos = (struct.tm_wday + 1) % 7
279 slots = startPos + daysInMonth - 1
280 rows = int(slots / 7) + 1
281 grid = Grid(rows + 1, 7)
282 grid.setWidth("100%")
283 grid.addTableListener(self)
284 self.middlePanel.setWidget(grid)
285
286
287
288 for i in range(7):
289 grid.setText(0, i, self.getDaysOfWeek()[i])
290 grid.cellFormatter.addStyleName(0, i, "calendar-header")
291
292
293
294 day = 0
295 pos = 0
296 while pos < startPos:
297 grid.setText(1, pos , " ")
298 grid.cellFormatter.setStyleAttr(1, pos, "background", "#f3f3f3")
299 grid.cellFormatter.addStyleName(1, pos, "calendar-blank-cell")
300 pos += 1
301
302 row = 1
303 day = 1
304 col = startPos
305 while day <= daysInMonth:
306 if pos % 7 == 0 and day <> 1:
307 row += 1
308 col = pos % 7
309 grid.setText(row, col, str(day))
310 if self.currentYear == self.todayYear and \
311 self.currentMonth == self.todayMonth and day == self.todayDay:
312 grid.cellFormatter.addStyleName(row, col, "calendar-cell-today")
313 else:
314 grid.cellFormatter.addStyleName(row, col, "calendar-day-cell")
315 day += 1
316 pos += 1
317
318
319
320 col += 1
321 while col < 7:
322 grid.setText(row, col, " ")
323 grid.cellFormatter.setStyleAttr(row, col, "background", "#f3f3f3")
324 grid.cellFormatter.addStyleName(row, col, "calendar-blank-cell")
325 col += 1
326
327 return grid
328
330 if row == 0:
331 return
332 text = grid.getText(row, col).strip()
333 if text == "":
334 return
335 try:
336 selectedDay = int(text)
337 except ValueError, e:
338 return
339
340 self.fireDateSelectedEvent(date(
341 self.currentYear,
342 self.currentMonth,
343 selectedDay,
344 ))
345 self.setVisible(False)
346
347
350
353
356
359
360 - def onDate(self, event, yy, mm, dd):
363
370
372 tod = time.localtime()
373 mm = tod.tm_mon
374 dd = tod.tm_mday
375 yy = tod.tm_year
376 self.onDate(event, yy, mm, dd)
377
379 tom = time.localtime(time.time() + 3600 * 24)
380 mm = tom.tm_mon
381 dd = tom.tm_mday
382 yy = tom.tm_year
383 self.onDate(event, yy, mm, dd)
384
387
389 self.currentMonth = month
390 self.currentYear = year
391 self.draw(self.currentMonth, self.currentYear)
392
394 if int(self.currentMonth) == 1:
395 self.currentMonth = 12
396 self.currentYear = int(self.currentYear) - 1
397 else:
398 self.currentMonth = int(self.currentMonth) - 1
399 self.draw(self.currentMonth, self.currentYear)
400
402 if int(self.currentMonth) == 12:
403 self.currentMonth = 1
404 self.currentYear = int(self.currentYear) + 1
405 else:
406 self.currentMonth = int(self.currentMonth) + 1
407 self.draw(self.currentMonth, self.currentYear)
408
410 self.currentYear = int(self.currentYear) - 1
411 self.draw(self.currentMonth, self.currentYear)
412
414 self.currentYear = int(self.currentYear) + 1
415 self.draw(self.currentMonth, self.currentYear)
416
417 Factory.registerClass('pyjamas.ui.Calendar', 'Calendar', Calendar)
418
419
420 -class DateField(Composite, DateSelectedHandler):
421
422 img_base = None
423 icon_img = None
424
425 icon_style = "calendar-img"
426 today_text = "Today"
427 today_style = "calendar-today-link"
428
480
487
490
491 - def getTextBox(self):
493
496
498 """ returns datetime.date object or None if empty/unparsable by current format"""
499 _sdate = self.tbox.getText()
500 try:
501 return datetime.strptime(_sdate, self.format).date()
502 except ValueError:
503 return None
504
507
509 secs = time.mktime((int(yyyy), int(mm), int(dd), 0, 0, 0, 0, 0, -1))
510 d = time.strftime(self.format, time.localtime(secs))
511 self.tbox.setText(d)
512 self.emitSelectedDate()
513
515
516 text = self.tbox.getText().strip()
517
518 if text and len(text) == 8:
519
520 txt = text[0:2] + self.sep + text[2:4] + self.sep + text[4:8]
521 self.tbox.setText(txt)
522 self.emitSelectedDate()
523
526
531
545
546 Factory.registerClass('pyjamas.ui.Calendar', 'DateField', DateField)
547
548
557
558 Factory.registerClass('pyjamas.ui.Calendar', 'CalendarPopup', CalendarPopup)
559