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