| 1 |
efrain |
1 |
var DIALOGUE_SELECTOR = ' [role=dialog]',
|
|
|
2 |
MENUBAR_SELECTOR = '[role=menubar]',
|
|
|
3 |
DOT = '.',
|
|
|
4 |
HAS_ZINDEX = 'moodle-has-zindex';
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* Add some custom methods to the node class to make our lives a little
|
|
|
8 |
* easier within this module.
|
|
|
9 |
*/
|
|
|
10 |
Y.mix(Y.Node.prototype, {
|
|
|
11 |
/**
|
|
|
12 |
* Gets the value of the first option in the select box
|
|
|
13 |
*/
|
|
|
14 |
firstOptionValue: function() {
|
|
|
15 |
if (this.get('nodeName').toLowerCase() !== 'select') {
|
|
|
16 |
return false;
|
|
|
17 |
}
|
|
|
18 |
return this.one('option').get('value');
|
|
|
19 |
},
|
|
|
20 |
/**
|
|
|
21 |
* Gets the value of the last option in the select box
|
|
|
22 |
*/
|
|
|
23 |
lastOptionValue: function() {
|
|
|
24 |
if (this.get('nodeName').toLowerCase() !== 'select') {
|
|
|
25 |
return false;
|
|
|
26 |
}
|
|
|
27 |
return this.all('option').item(this.optionSize() - 1).get('value');
|
|
|
28 |
},
|
|
|
29 |
/**
|
|
|
30 |
* Gets the number of options in the select box
|
|
|
31 |
*/
|
|
|
32 |
optionSize: function() {
|
|
|
33 |
if (this.get('nodeName').toLowerCase() !== 'select') {
|
|
|
34 |
return false;
|
|
|
35 |
}
|
|
|
36 |
return parseInt(this.all('option').size(), 10);
|
|
|
37 |
},
|
|
|
38 |
/**
|
|
|
39 |
* Gets the value of the selected option in the select box
|
|
|
40 |
*/
|
|
|
41 |
selectedOptionValue: function() {
|
|
|
42 |
if (this.get('nodeName').toLowerCase() !== 'select') {
|
|
|
43 |
return false;
|
|
|
44 |
}
|
|
|
45 |
return this.all('option').item(this.get('selectedIndex')).get('value');
|
|
|
46 |
}
|
|
|
47 |
});
|
|
|
48 |
|
|
|
49 |
M.form = M.form || {};
|
|
|
50 |
M.form.dateselector = {
|
|
|
51 |
panel: null,
|
|
|
52 |
calendar: null,
|
|
|
53 |
currentowner: null,
|
|
|
54 |
hidetimeout: null,
|
|
|
55 |
repositiontimeout: null,
|
|
|
56 |
init_date_selectors: function(config) {
|
|
|
57 |
if (this.panel === null) {
|
|
|
58 |
this.initPanel(config);
|
|
|
59 |
}
|
|
|
60 |
Y.all('.fdate_time_selector').each(function() {
|
|
|
61 |
config.node = this;
|
|
|
62 |
new CALENDAR(config);
|
|
|
63 |
});
|
|
|
64 |
Y.all('.fdate_selector').each(function() {
|
|
|
65 |
config.node = this;
|
|
|
66 |
new CALENDAR(config);
|
|
|
67 |
});
|
|
|
68 |
},
|
|
|
69 |
initPanel: function(config) {
|
|
|
70 |
this.panel = new Y.Overlay({
|
|
|
71 |
visible: false,
|
|
|
72 |
bodyContent: Y.Node.create('<div id="dateselector-calendar-content"></div>'),
|
|
|
73 |
id: 'dateselector-calendar-panel',
|
|
|
74 |
constrain: true // constrain panel to viewport.
|
|
|
75 |
});
|
|
|
76 |
this.panel.render(document.body);
|
|
|
77 |
|
|
|
78 |
// Determine the correct zindex by looking at all existing dialogs and menubars in the page.
|
|
|
79 |
this.panel.on('focus', function() {
|
|
|
80 |
var highestzindex = 0;
|
|
|
81 |
Y.all(DIALOGUE_SELECTOR + ', ' + MENUBAR_SELECTOR + ', ' + DOT + HAS_ZINDEX).each(function(node) {
|
|
|
82 |
var zindex = this.findZIndex(node);
|
|
|
83 |
if (zindex > highestzindex) {
|
|
|
84 |
highestzindex = zindex;
|
|
|
85 |
}
|
|
|
86 |
}, this);
|
|
|
87 |
// Only set the zindex if we found a wrapper.
|
|
|
88 |
var zindexvalue = (highestzindex + 1).toString();
|
|
|
89 |
Y.one('#dateselector-calendar-panel').setStyle('zIndex', zindexvalue);
|
|
|
90 |
}, this);
|
|
|
91 |
|
|
|
92 |
this.panel.on('heightChange', this.fix_position, this);
|
|
|
93 |
|
|
|
94 |
Y.one('#dateselector-calendar-panel').on('click', function(e) {
|
|
|
95 |
e.halt();
|
|
|
96 |
});
|
|
|
97 |
Y.one(document.body).on('click', this.document_click, this);
|
|
|
98 |
|
|
|
99 |
this.calendar = new MOODLECALENDAR({
|
|
|
100 |
contentBox: "#dateselector-calendar-content",
|
|
|
101 |
width: "300px",
|
|
|
102 |
showPrevMonth: true,
|
|
|
103 |
showNextMonth: true,
|
|
|
104 |
firstdayofweek: parseInt(config.firstdayofweek, 10),
|
|
|
105 |
WEEKDAYS_MEDIUM: [
|
|
|
106 |
config.sun,
|
|
|
107 |
config.mon,
|
|
|
108 |
config.tue,
|
|
|
109 |
config.wed,
|
|
|
110 |
config.thu,
|
|
|
111 |
config.fri,
|
|
|
112 |
config.sat]
|
|
|
113 |
});
|
|
|
114 |
},
|
|
|
115 |
findZIndex: function(node) {
|
|
|
116 |
// In most cases the zindex is set on the parent of the dialog.
|
|
|
117 |
var zindex = node.getStyle('zIndex') || node.ancestor().getStyle('zIndex');
|
|
|
118 |
if (zindex) {
|
|
|
119 |
return parseInt(zindex, 10);
|
|
|
120 |
}
|
|
|
121 |
return 0;
|
|
|
122 |
},
|
|
|
123 |
cancel_any_timeout: function() {
|
|
|
124 |
if (this.hidetimeout) {
|
|
|
125 |
clearTimeout(this.hidetimeout);
|
|
|
126 |
this.hidetimeout = null;
|
|
|
127 |
}
|
|
|
128 |
if (this.repositiontimeout) {
|
|
|
129 |
clearTimeout(this.repositiontimeout);
|
|
|
130 |
this.repositiontimeout = null;
|
|
|
131 |
}
|
|
|
132 |
},
|
|
|
133 |
delayed_reposition: function() {
|
|
|
134 |
if (this.repositiontimeout) {
|
|
|
135 |
clearTimeout(this.repositiontimeout);
|
|
|
136 |
this.repositiontimeout = null;
|
|
|
137 |
}
|
|
|
138 |
this.repositiontimeout = setTimeout(this.fix_position, 500);
|
|
|
139 |
},
|
|
|
140 |
fix_position: function() {
|
|
|
141 |
if (this.currentowner) {
|
|
|
142 |
var alignpoints = [
|
|
|
143 |
Y.WidgetPositionAlign.BL,
|
|
|
144 |
Y.WidgetPositionAlign.TL
|
|
|
145 |
];
|
|
|
146 |
|
|
|
147 |
// Change the alignment if this is an RTL language.
|
|
|
148 |
if (window.right_to_left()) {
|
|
|
149 |
alignpoints = [
|
|
|
150 |
Y.WidgetPositionAlign.BR,
|
|
|
151 |
Y.WidgetPositionAlign.TR
|
|
|
152 |
];
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
this.panel.set('align', {
|
|
|
156 |
node: this.currentowner.get('node').one('select'),
|
|
|
157 |
points: alignpoints
|
|
|
158 |
});
|
|
|
159 |
}
|
|
|
160 |
},
|
|
|
161 |
document_click: function(e) {
|
|
|
162 |
if (this.currentowner) {
|
|
|
163 |
if (this.currentowner.get('node').ancestor('div').contains(e.target)) {
|
|
|
164 |
setTimeout(function() {
|
|
|
165 |
M.form.dateselector.cancel_any_timeout();
|
|
|
166 |
}, 100);
|
|
|
167 |
} else {
|
|
|
168 |
this.currentowner.release_calendar(e);
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
};
|