Fixes #223 allow student attendance to be disabled at site-level
[moodle-mod_attendance.git] / calendar.js
1 YUI().use('yui2-container', 'yui2-calendar', function(Y) {
2 var YAHOO = Y.YUI2;
3
4 document.body.className += ' yui-skin-sam';
5
6 YAHOO.util.Event.onDOMReady(function(){
7
8 var Event = YAHOO.util.Event,
9 Dom = YAHOO.util.Dom,
10 dialog, calendar;
11
12 var showBtn = Dom.get("show");
13
14 Event.on(showBtn, "click", function() {
15
16 function resetHandler() {
17 calendar.cfg.setProperty("pagedate", calendar.today);
18 calendar.render();
19 }
20
21 function closeHandler() {
22 dialog.hide();
23 }
24
25 // Lazy Dialog Creation - Wait to create the Dialog, and setup document click listeners,
26 // until the first time the button is clicked.
27 if (!dialog) {
28
29 // Hide Calendar if we click anywhere in the document other than the calendar.
30 Event.on(document, "click", function(e) {
31 var el = Event.getTarget(e);
32 var dialogEl = dialog.element;
33 if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != showBtn && !Dom.isAncestor(showBtn, el)) {
34 dialog.hide();
35 }
36 });
37
38 dialog = new YAHOO.widget.Dialog("attcalendarcontainer", {
39 visible:false,
40 context:["show", "tl", "bl"],
41 buttons:[{text: M.str.attendance.caltoday, handler: resetHandler, isDefault:true},
42 {text: M.str.attendance.calclose, handler: closeHandler}],
43 draggable:false,
44 close:false
45 });
46 dialog.setHeader('');
47 dialog.setBody('<div id="cal"></div>');
48 dialog.render(document.body);
49
50 dialog.showEvent.subscribe(function() {
51 if (YAHOO.env.ua.ie) {
52 // Since we're hiding the table using yui-overlay-hidden, we
53 // want to let the dialog know that the content size has changed, when
54 // shown.
55 dialog.fireEvent("changeContent");
56 }
57 });
58 }
59
60 // Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
61 if (!calendar) {
62
63 calendar = new YAHOO.widget.Calendar("cal", {
64 iframe:false, // Turn iframe off, since container has iframe support.
65 hide_blank_weeks:true // Enable, to demonstrate how we handle changing height, using changeContent.
66 });
67
68 calendar.cfg.setProperty("start_weekday", M.attendance.cal_start_weekday);
69 calendar.cfg.setProperty("MONTHS_LONG", M.attendance.cal_months);
70 calendar.cfg.setProperty("WEEKDAYS_SHORT", M.attendance.cal_week_days);
71 calendar.select(new Date(M.attendance.cal_cur_date * 1000));
72 calendar.render();
73
74 calendar.selectEvent.subscribe(function() {
75 if (calendar.getSelectedDates().length > 0) {
76
77 Dom.get("curdate").value = calendar.getSelectedDates()[0] / 1000;
78
79 Dom.get("currentdate").submit();
80 }
81 dialog.hide();
82 });
83
84 calendar.renderEvent.subscribe(function() {
85 // Tell Dialog it's contents have changed, which allows
86 // container to redraw the underlay (for IE6/Safari2).
87 dialog.fireEvent("changeContent");
88 });
89 }
90
91 var seldate = calendar.getSelectedDates();
92
93 if (seldate.length > 0) {
94 // Set the pagedate to show the selected date if it exists.
95 calendar.cfg.setProperty("pagedate", seldate[0]);
96 calendar.render();
97 }
98
99 dialog.show();
100 });
101 });
102 });